Skip to main content

Module conversions

Module conversions 

Source
Expand description

Conversions: the JS_To* family, and the constructors that make a JSValue out of a Rust primitive.

§The rule every function here follows

A conversion that can run script — ToNumber calls valueOf, ToString calls toString, ToPrimitive calls either — can throw, and the C API says so by answering -1 (a c_int) or jsvalue::EXCEPTION (a JSValue) with the reason left in the context’s pending slot. Nothing here answers “a best guess”: JS_ToInt32 on a Symbol is -1, not 0, which is what makes new Point(Symbol(), 0) a TypeError rather than a point at the origin.

§Saturating and wrapping are different functions

JS_ToInt32 wraps (ToInt32: modulo 2³²), JS_ToInt32Sat clamps, JS_ToInt64 refuses a value that does not fit and JS_ToInt64Sat clamps. The reference keeps them apart and so does this file: a Number.MAX_VALUE argument is -1 from one and 2147483647 from the other, and the difference is observable from a script.

Functions§

JS_MarkValue⚠
JS_MarkValue: stop the collector from reclaiming val.
JS_NewArray⚠
JS_NewArray: an empty array.
JS_NewAtomString⚠
JS_NewAtomString: a string, interning it on the way.
JS_NewBigInt64⚠
JS_NewBigInt64: a BigInt from an i64.
JS_NewBigInt128⚠
JS_NewBigInt128: a BigInt from a 128-bit two’s-complement pair.
JS_NewBigUint64⚠
JS_NewBigUint64: a BigInt from a u64.
JS_NewDate⚠
JS_NewDate: a Date for epoch_ms.
JS_NewError⚠
JS_NewError: a plain Error.
JS_NewObjectProto⚠
JS_NewObjectProto: an ordinary object with proto.
JS_NewStringLen⚠
JS_NewStringLen: a string from len1 bytes.
JS_NewSymbol⚠
JS_NewSymbol: a symbol with description, optionally from the global registry.
JS_ThisDateValue⚠
JS_ThisDateValue: this’s [[DateValue]], or -1 when it is not a Date.
JS_ToBigInt64⚠
JS_ToBigInt64: a BigInt (or a number) as an i64, modulo 2^64.
JS_ToBigInt128⚠
JS_ToBigInt128: a BigInt as a 128-bit two’s-complement pair.
JS_ToBigInt128Sat⚠
JS_ToBigInt128Sat: JS_ToBigInt128 clamped to the 128-bit range.
JS_ToCStringNZ⚠
JS_ToCStringNZ: crate::objects::JS_ToCStringLen2 without the length.
JS_ToInt32⚠
JS_ToInt32: ToInt32 (wrapping), or -1 with a pending exception.
JS_ToInt64⚠
JS_ToInt64: ToInt64, refusing a value outside the i64 range.
JS_ToInt32Sat⚠
JS_ToInt32Sat: ToInt32 clamped to the int range instead of wrapped.
JS_ToInt64Clamp⚠
JS_ToInt64Clamp: JS_ToInt64Sat and then an affine shift, which is how the reference reads a Date’s time value with a timezone correction.
JS_ToInt64Ext⚠
JS_ToInt64Ext: ToInt64, but a BigInt is accepted too.
JS_ToInt64Sat⚠
JS_ToInt64Sat: ToInt64 clamped to the i64 range.
JS_ToObject⚠
JS_ToObject: ToObject, boxing a primitive and refusing null/undefined.
JS_ToPrimitive⚠
JS_ToPrimitive: ToPrimitive with a hint (0 default, 1 number, 2 string).
JS_ToPropertyKey⚠
JS_ToPropertyKey: ToPropertyKey, which keeps a symbol as a symbol.
JS_ToString⚠
JS_ToString: ToString, which calls toString/valueOf and so can throw.
JS_ToUTF32String⚠
JS_ToUTF32String: the code points of a string, as a js_malloc block.