You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
spytheman helped me debug this issue.
The function signature of string_at (alphabet[remainder]) is u8 string_at(string s, int idx), where int is 32bit (as disclosed in the V docs).
The C backend silently downcasts u64 to int in order to match the function type signature of the underlying builtin string function. But it is not possible to do this for the JS backend, which will give an error.
Reproduction Steps
fnu64_to_custom_base(n u64) string {
alphabet:="abcdefghijklmnopqrstuvwxyz0123456789"alpha_len:=u64(alphabet.len)
mutresult:=''ifn==0 {
return alphabet[0].ascii_str()
}
mutnum:= n
for num >0 {
remainder:= num % alpha_len
result= alphabet[remainder].ascii_str() + result
num= num / alpha_len
}
return result
}
fnmain() {
println(u64_to_custom_base(69))
}
> v -b js_browser .
> node .\xxxxx.js
xxxxx.js:7541
result = new string(new string( u8_ascii_str(new u8(alphabet.str.charCodeAt(remainder)),).valueOf() + result.valueOf()));
^
TypeError: Cannot convert a BigInt value to a number
at String.charCodeAt (<anonymous>)
at main__BaseConverter_u64_to_custom_base (xxxxx.js:7541:76)
at main__DocTree_render (xxxxx.js:8453:51)
at js_main (xxxxx.js:7714:21)
at xxxxx:9300:25
Node.js v20.13.0
Expected Behavior
Expected an error during compile time, alternatively for JS to downcast the u64 to int and print base-encoded string.
Current Behavior
V generates JS and C code for this without any warnings or errors. The code works in C but not in JS.
Possible Solution
spytheman suggests this should be a checker error so that it is caught earlier and does not rely strictly on backend behavior.
Personally, I agree since it might cause unexplained and unintended consequences later that the user might not be aware of.
However, I'm not against preventing this conversion even without it being explicitly listed on the conversion chart
It would be nice if it threw an error only when it's non-explicit, i.e. alphabet[remainder] and not alphabet[int(remainder)]. I'm also not sure if its planned for int to always be 32 bits, if its not, it might be nice to have a compile time pseudo variable for @INT_SIZE to forward-proof conversion behavior i.e. no need to downcast if the underlying function later uses a 64bit integer.
Describe the bug
spytheman helped me debug this issue.
The function signature of string_at (
alphabet[remainder]
) isu8 string_at(string s, int idx)
, where int is 32bit (as disclosed in the V docs).The C backend silently downcasts u64 to int in order to match the function type signature of the underlying builtin string function. But it is not possible to do this for the JS backend, which will give an error.
Reproduction Steps
Expected Behavior
Expected an error during compile time, alternatively for JS to downcast the u64 to int and print base-encoded string.
Current Behavior
V generates JS and C code for this without any warnings or errors. The code works in C but not in JS.
Possible Solution
spytheman suggests this should be a checker error so that it is caught earlier and does not rely strictly on backend behavior.
Personally, I agree since it might cause unexplained and unintended consequences later that the user might not be aware of.
However, I'm not against preventing this conversion even without it being explicitly listed on the conversion chart
It would be nice if it threw an error only when it's non-explicit, i.e.
alphabet[remainder]
and notalphabet[int(remainder)]
. I'm also not sure if its planned for int to always be 32 bits, if its not, it might be nice to have a compile time pseudo variable for@INT_SIZE
to forward-proof conversion behavior i.e. no need to downcast if the underlying function later uses a 64bit integer.Additional Information/Context
No response
V version
V 0.4.9 b94da8a
Environment details (OS name and version, etc.)
Note
You can use the 👍 reaction to increase the issue's priority for developers.
Please note that only the 👍 reaction to the issue itself counts as a vote.
Other reactions and those to comments will not be taken into account.
The text was updated successfully, but these errors were encountered: