dymokomi/luce-js
Source of release 0.6.1
0.6.1 / src / engine / bigint_builtins.lucb
#==============================================================================================
#
# bigint_builtins - The BigInt constructor, its methods and its intrinsic
#
# DESCRIPTION:
# Port of quickjs.c "BigInt" builtins (lines 56232-56377): BigInt(), thisBigIntValue,
# BigInt.prototype.toString and valueOf, BigInt.asUintN and asIntN, their function
# tables and JS_AddIntrinsicBigInt.
#
#==============================================================================================
# mark: Constructor and methods =================================================================
func js_bigint_constructor(ctx: Context*, new_target: Value, argc: i32, argv: Value*) -> Value!:
if not js_is_undefined(new_target):
try js_throw_type_error_not_a_constructor(ctx, new_target)
return try js_to_bigint_ctor_free(ctx, js_dup_value(ctx, argv[0]))
## The BigInt value of `this_val`, a big integer or a BigInt object (thisBigIntValue).
func js_this_bigint_value(ctx: Context*, this_val: Value) -> Value!:
if js_is_big_int(this_val):
return js_dup_value(ctx, this_val)
if this_val.tag == Tag.object:
let p = this_val.u.obj
if p.class_id == ClassId.big_int:
if js_is_big_int(p.u.object_data):
return js_dup_value(ctx, p.u.object_data)
try js_throw_type_error(ctx, "not a BigInt")
func js_bigint_to_string_method(ctx: Context*, this_val: Value, argc: i32, argv: Value*) -> Value!:
let val = try js_this_bigint_value(ctx, this_val)
defer js_free_value(ctx, val)
var base: i32 = 10
if argc != 0 and not js_is_undefined(argv[0]):
base = try js_get_radix(ctx, argv[0])
return try js_bigint_to_string1(ctx, val, base)
func js_bigint_value_of(ctx: Context*, this_val: Value, argc: i32, argv: Value*) -> Value!:
return try js_this_bigint_value(ctx, this_val)
## BigInt.asUintN (as_int_n == 0) and BigInt.asIntN (as_int_n == 1).
func js_bigint_as_uint_n(ctx: Context*, this_val: Value, argc: i32, argv: Value*, as_int_n: i32) -> Value!:
var bits: u64 = 0
try js_to_index(ctx, &bits, argv[0])
let a = try js_to_bigint(ctx, argv[1])
if bits == 0:
js_free_value(ctx, a)
return js_new_short_big_int_raw(0)
elif a.tag == Tag.short_big_int:
# fast case
if bits >= (u64)js_short_big_int_bits:
return a
let shift = 64 - bits
var v = (u64)(i64)js_value_get_short_big_int(a)
v = v << shift
if as_int_n != 0:
v = (u64)((i64)v >> shift)
else:
v = v >> shift
return js_new_short_big_int_raw((i32)v)
else:
let p = a.u.big_int
if bits >= (u64)p.len * (u64)js_limb_bits:
return a
let len = (i32)((bits + (u64)js_limb_bits - 1) // (u64)js_limb_bits)
var r = js_bigint_new(ctx, len) catch failure:
js_free_value(ctx, a)
error(failure.code, failure.message)
r.len = (u32)len
let r_tab = js_bigint_tab(r)
let p_tab = js_bigint_tab(p)
for i in 0..<(usize)(len - 1):
r_tab[i] = p_tab[i]
let shift = (0 -% bits) & (u64)(js_limb_bits - 1)
# 0 <= shift <= js_limb_bits - 1
var v = p_tab[(usize)(len - 1)] << (u32)shift
if as_int_n != 0:
v = (u32)((i32)v >> (u32)shift)
else:
v = v >> (u32)shift
r_tab[(usize)(len - 1)] = v
r = js_bigint_normalize(ctx, r)
js_free_value(ctx, a)
return js_compact_bigint(ctx, r)
# mark: Tables and intrinsic ====================================================================
let js_bigint_funcs: CFunctionListEntry[2] = [
CFunctionListEntry(name = "asUintN", def_type = def_cfunc, length = 2,
cproto = CFunctionEnum.generic_magic, generic_magic = js_bigint_as_uint_n, magic = 0),
CFunctionListEntry(name = "asIntN", def_type = def_cfunc, length = 2,
cproto = CFunctionEnum.generic_magic, generic_magic = js_bigint_as_uint_n, magic = 1),
]
let js_bigint_proto_funcs: CFunctionListEntry[3] = [
CFunctionListEntry(name = "toString", def_type = def_cfunc, length = 0, generic = js_bigint_to_string_method),
CFunctionListEntry(name = "valueOf", def_type = def_cfunc, length = 0, generic = js_bigint_value_of),
CFunctionListEntry(name = "[Symbol.toStringTag]", def_type = def_prop_string,
prop_flags = (u8)prop_configurable, string_value = "BigInt"),
]
## Add the BigInt constructor and prototype to the context (JS_AddIntrinsicBigInt).
func js_add_intrinsic_bigint(ctx: Context*) -> !:
let obj1 = try js_new_c_constructor(ctx, (i32)ClassId.big_int, "BigInt",
js_bigint_constructor, 1, CFunctionEnum.constructor_or_func, 0,
js_undefined(),
js_bigint_funcs,
js_bigint_proto_funcs,
0)
js_free_value(ctx, obj1)