Skip to main content

Module print

Module print 

Source
Expand description

The reference’s JS_PrintValue, which lives in the engine (quickjs.c) and not in the host library. std.__printObject is a one-line wrapper over it, and so is the reporting of an uncaught exception — which is why this belongs here rather than in quickjs-host. std.__sprintObject / std.__printObject: the reference’s JS_PrintValue.

§Not JSON.stringify, and not console.log

JS_PrintValue is quickjs’s own dumper, with its own rules, and both of quickjs’s front-ends print through it. It is not JSON: keys are bare when they are identifiers, values are truncated, cycles are marked, and a Map prints as a Map rather than as {}. Matching it byte for byte is the whole job, so every rule below is taken from js_print_value/js_print_object in quickjs.c rather than invented:

[ 1, 2, 3 ]              arrays get spaces inside the brackets
[  ]                     and so does an empty one
{  }                     an empty object has two spaces
{ a: 1, "a b": 2 }       bare key when it is an identifier, quoted otherwise
{ a: { b: [Array] } }    past `max_depth` (2) an object prints as its class name
{ self: [circular 0] }   a cycle names the depth it was first seen at
[ 0: 1, 2: 3 ]           a sparse array prints index: value pairs
Map(1) { 1 => 2 }
Set(2) { 1, 2 }
[Function f]

The defaults are the reference’s: max_depth = 2, max_string_length = 1000, max_item_count = 100. Hitting a limit is visible in the output (... 3 more items, ... 5 more characters), which is why the numbers have to be exact.

Functions§

sprint
std.__sprintObject(value) — the same text std.__printObject would write.