mirror of
https://github.com/vlang/v.git
synced 2025-09-12 08:57:09 -04:00
jsgen: fix inconsistent output (u32) in JS backend (#20691)
This commit is contained in:
parent
804a7bdda5
commit
afa1a9abc2
@ -348,15 +348,25 @@ fn (mut g JsGen) gen_builtin_type_defs() {
|
|||||||
to_jsval: '+this'
|
to_jsval: '+this'
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
// u16 / u32 requires special handling in JavaScript to correctly represent it as an unsigned 32-bit integer.
|
// u16 and u32 requires special handling in JavaScript to correctly represent it.
|
||||||
// The '>>> 0' bit operation ensures it is treated as unsigned, covering the full 0 to 2^32-1 range.
|
// u16, '>>> 0' combined with a mask of 0xffff limits it to the 0 to 2^16-1 range, correctly handling values as unsigned 16-bit integers.
|
||||||
// For u16, '>>> 0' combined with a mask of 0xffff limits it to the 0 to 2^16-1 range, correctly handling values as unsigned 16-bit integers.
|
'u16' {
|
||||||
'u16', 'u32' {
|
|
||||||
g.gen_builtin_prototype(
|
g.gen_builtin_prototype(
|
||||||
typ_name: typ_name
|
typ_name: typ_name
|
||||||
default_value: 'new Number(0)'
|
default_value: 'new Number(0)'
|
||||||
constructor: "this.val = Math.floor(Number(val) & ('" + typ_name +
|
constructor: 'this.val = Math.floor(Number(val) & 0xffff) >>> 0'
|
||||||
'\' === "u16" ? 0xffff : 0xffffffff)) >>> 0'
|
value_of: 'Number(this.val)'
|
||||||
|
to_string: 'this.valueOf().toString()'
|
||||||
|
eq: 'new bool(self.valueOf() === other.valueOf())'
|
||||||
|
to_jsval: '+this'
|
||||||
|
)
|
||||||
|
}
|
||||||
|
// u32 '>>> 0' combined with a mask of 0xffffffff limits it to the 0 to 2^32-1 range, correctly handling values as unsigned 32-bit integers.
|
||||||
|
'u32' {
|
||||||
|
g.gen_builtin_prototype(
|
||||||
|
typ_name: typ_name
|
||||||
|
default_value: 'new Number(0)'
|
||||||
|
constructor: 'this.val = Math.floor(Number(val) & 0xffffffff) >>> 0'
|
||||||
value_of: 'Number(this.val)'
|
value_of: 'Number(this.val)'
|
||||||
to_string: 'this.valueOf().toString()'
|
to_string: 'this.valueOf().toString()'
|
||||||
eq: 'new bool(self.valueOf() === other.valueOf())'
|
eq: 'new bool(self.valueOf() === other.valueOf())'
|
||||||
|
@ -120,11 +120,19 @@ fn (mut g JsGen) gen_expr_to_string(expr ast.Expr, etype ast.Type) {
|
|||||||
is_var_mut := expr.is_auto_deref_var()
|
is_var_mut := expr.is_auto_deref_var()
|
||||||
str_fn_name := g.get_str_fn(typ)
|
str_fn_name := g.get_str_fn(typ)
|
||||||
g.write('${str_fn_name}(')
|
g.write('${str_fn_name}(')
|
||||||
|
|
||||||
if str_method_expects_ptr && !is_ptr {
|
if str_method_expects_ptr && !is_ptr {
|
||||||
g.write('new \$ref(')
|
g.write('new \$ref(')
|
||||||
}
|
}
|
||||||
|
if typ == ast.u32_type && expr is ast.CastExpr {
|
||||||
|
g.write('new u32(')
|
||||||
|
}
|
||||||
|
|
||||||
g.expr(expr)
|
g.expr(expr)
|
||||||
|
|
||||||
|
if typ == ast.u32_type && expr is ast.CastExpr {
|
||||||
|
g.write(')')
|
||||||
|
}
|
||||||
if (!str_method_expects_ptr && is_ptr && !is_shared) || is_var_mut {
|
if (!str_method_expects_ptr && is_ptr && !is_shared) || is_var_mut {
|
||||||
g.write('.val')
|
g.write('.val')
|
||||||
}
|
}
|
||||||
|
2
vlib/v/gen/js/tests/testdata/u32.out
vendored
2
vlib/v/gen/js/tests/testdata/u32.out
vendored
@ -1,4 +1,4 @@
|
|||||||
-1
|
4294967295
|
||||||
0
|
0
|
||||||
true
|
true
|
||||||
25600000
|
25600000
|
||||||
|
Loading…
x
Reference in New Issue
Block a user