JSValue Representation

JSValue is a Javascript value which can be a primitive type (such as Number, String, ...) or an Object.

kwui is compiling QuickJS with strict nan-boxing, for the purpose of cross-platform compatibility. JSValue is 64-bit on 32-bit and 64-bit platforms.

JSValue Format

QuickJS number is double-precision floating-point number. double type is 64-bit, comprised of 1 sign bit, 11 exponent bits and 52 mantissa bits.

   7         6        5        4        3        2        1        0
seeeeeee|eeeemmmm|mmmmmmmm|mmmmmmmm|mmmmmmmm|mmmmmmmm|mmmmmmmm|mmmmmmmm

JSValue store non-NaN float with all bits binary reversed, therefore the first 12 bits are non-zero.

NaN numbers, and other primitive types, are represented by tag and value.

00000000|0000tttt|vvvvvvvv|vvvvvvvv|vvvvvvvv|vvvvvvvv|vvvvvvvv|vvvvvvvv
12-bits zero |tag|  48-bit placeholder for values: pointers, strings

Primitive Typetagvalue
NaN number70
-Inf number71
+Inf number72
int1as-is
false20
true21
null30
undefined40
exception60
object8JSObject*
string11JSString*
symbol12JSAtomStruct*

Reference counted JSValue type (such as object, string, symbol), the most significant bit of tag is 1.