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_
Mark ⚠Value JS_MarkValue: stop the collector from reclaimingval.- JS_
NewArray ⚠ JS_NewArray: an empty array.- JS_
NewAtom ⚠String JS_NewAtomString: a string, interning it on the way.- JS_
NewBig ⚠Int64 JS_NewBigInt64: aBigIntfrom ani64.- JS_
NewBig ⚠Int128 JS_NewBigInt128: aBigIntfrom a 128-bit two’s-complement pair.- JS_
NewBig ⚠Uint64 JS_NewBigUint64: aBigIntfrom au64.- JS_
NewDate ⚠ JS_NewDate: aDateforepoch_ms.- JS_
NewError ⚠ JS_NewError: a plainError.- JS_
NewObject ⚠Proto JS_NewObjectProto: an ordinary object withproto.- JS_
NewString ⚠Len JS_NewStringLen: a string fromlen1bytes.- JS_
NewSymbol ⚠ JS_NewSymbol: a symbol withdescription, optionally from the global registry.- JS_
This ⚠Date Value JS_ThisDateValue:this’s[[DateValue]], or-1when it is not aDate.- JS_
ToBig ⚠Int64 JS_ToBigInt64: aBigInt(or a number) as ani64, modulo 2^64.- JS_
ToBig ⚠Int128 JS_ToBigInt128: aBigIntas a 128-bit two’s-complement pair.- JS_
ToBig ⚠Int128 Sat JS_ToBigInt128Sat:JS_ToBigInt128clamped to the 128-bit range.- JS_
ToCStringNZ ⚠ JS_ToCStringNZ:crate::objects::JS_ToCStringLen2without the length.- JS_
ToInt32 ⚠ JS_ToInt32:ToInt32(wrapping), or-1with a pending exception.- JS_
ToInt64 ⚠ JS_ToInt64:ToInt64, refusing a value outside thei64range.- JS_
ToInt32 ⚠Sat JS_ToInt32Sat:ToInt32clamped to theintrange instead of wrapped.- JS_
ToInt64 ⚠Clamp JS_ToInt64Clamp:JS_ToInt64Satand then an affine shift, which is how the reference reads aDate’s time value with a timezone correction.- JS_
ToInt64 ⚠Ext JS_ToInt64Ext:ToInt64, but aBigIntis accepted too.- JS_
ToInt64 ⚠Sat JS_ToInt64Sat:ToInt64clamped to thei64range.- JS_
ToObject ⚠ JS_ToObject:ToObject, boxing a primitive and refusingnull/undefined.- JS_
ToPrimitive ⚠ JS_ToPrimitive:ToPrimitivewith ahint(0default,1number,2string).- JS_
ToProperty ⚠Key JS_ToPropertyKey:ToPropertyKey, which keeps a symbol as a symbol.- JS_
ToString ⚠ JS_ToString:ToString, which callstoString/valueOfand so can throw.- JS_
ToUT ⚠F32String JS_ToUTF32String: the code points of a string, as ajs_mallocblock.