Skip to content

Commit 6df21c5

Browse files
committed
BigInt support for QuickJS
Literals have the `n` suffix: `0n`, `-20n`. They can be parsed from a string:(ex: `x = BigInt("12..34")`) and serialized back to a string with `toString()` (ex: `emit(x.toString())`). Currently this is a minimal, standards compliant implementation which doesn't do automatic JSON parsing directly to BigInts or automatically serializing to JSON. It should be possible to introduce that in the future, but it's better to start from something simpler first.
1 parent b2ff65a commit 6df21c5

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

src/couch/test/eunit/couch_js_tests.erl

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ couch_js_test_() ->
3737
?TDEF(should_replace_broken_utf16),
3838
?TDEF(should_allow_js_string_mutations),
3939
?TDEF(should_bump_timing_and_call_stats),
40-
?TDEF(should_exit_on_internal_error, 60)
40+
?TDEF(should_exit_on_internal_error, 60),
41+
?TDEF(should_use_bigint)
4142
])
4243
}
4344
}.
@@ -440,6 +441,30 @@ should_exit_on_internal_error(_) ->
440441
end,
441442
?assert(couch_stats:sample([couchdb, query_server, process_errors]) > 0).
442443

444+
%% erlfmt-ignore
445+
should_use_bigint(_) ->
446+
Proc = couch_query_servers:get_os_process(<<"javascript">>),
447+
Src = <<"
448+
function(doc) {
449+
const x = 147573952589676412928n;
450+
let z = x + BigInt(doc.y);
451+
emit('z', z.toString());
452+
}
453+
">>,
454+
case couch_server:get_js_engine() of
455+
<<"quickjs">> ->
456+
true = prompt(Proc, [<<"add_fun">>, Src]),
457+
X = 147573952589676412928,
458+
Y = 73786976294838206464,
459+
Doc = {[{<<"y">>, integer_to_binary(Y)}]},
460+
Result = prompt(Proc, [<<"map_doc">>, Doc]),
461+
?assertMatch([[[<<"z">>, <<_/binary>>]]], Result),
462+
[[[<<"z">>, Z]]] = Result,
463+
?assertEqual(X + Y, binary_to_integer(Z));
464+
<<"spidermonkey">> ->
465+
?assertThrow({compilation_error, _}, prompt(Proc, [<<"add_fun">>, Src]))
466+
end.
467+
443468
sample_time(Stat) ->
444469
couch_stats:sample([couchdb, query_server, time, Stat]).
445470

src/couch_quickjs/c_src/couchjs.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ static void add_cx_methods(JSContext* cx) {
9494
JS_AddIntrinsicRegExp(cx);
9595
JS_AddIntrinsicMapSet(cx);
9696
JS_AddIntrinsicDate(cx);
97+
JS_AddIntrinsicBigInt(cx);
9798
}
9899

99100
// Creates a new JSContext with only the provided sandbox function

0 commit comments

Comments
 (0)