You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@couchdb.apache.org by va...@apache.org on 2025/03/17 05:10:54 UTC
(couchdb) 13/38: BigInt support for QuickJS
This is an automated email from the ASF dual-hosted git repository.
vatamane pushed a commit to branch merge-3.4.3
in repository https://gitbox.apache.org/repos/asf/couchdb.git
commit bf919a4a63898ba81e262a7adc80a8f098aa5627
Author: Nick Vatamaniuc <va...@gmail.com>
AuthorDate: Fri Feb 28 13:17:03 2025 -0500
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.
---
src/couch/test/eunit/couch_js_tests.erl | 27 ++++++++++++++++++++++++++-
src/couch_quickjs/c_src/couchjs.c | 1 +
2 files changed, 27 insertions(+), 1 deletion(-)
diff --git a/src/couch/test/eunit/couch_js_tests.erl b/src/couch/test/eunit/couch_js_tests.erl
index 456ed0979..29b303ffb 100644
--- a/src/couch/test/eunit/couch_js_tests.erl
+++ b/src/couch/test/eunit/couch_js_tests.erl
@@ -37,7 +37,8 @@ couch_js_test_() ->
?TDEF(should_replace_broken_utf16),
?TDEF(should_allow_js_string_mutations),
?TDEF(should_bump_timing_and_call_stats),
- ?TDEF(should_exit_on_internal_error, 60)
+ ?TDEF(should_exit_on_internal_error, 60),
+ ?TDEF(should_use_bigint)
])
}
}.
@@ -440,6 +441,30 @@ should_exit_on_internal_error(_) ->
end,
?assert(couch_stats:sample([couchdb, query_server, process_errors]) > 0).
+%% erlfmt-ignore
+should_use_bigint(_) ->
+ Proc = couch_query_servers:get_os_process(<<"javascript">>),
+ Src = <<"
+ function(doc) {
+ const x = 147573952589676412928n;
+ let z = x + BigInt(doc.y);
+ emit('z', z.toString());
+ }
+ ">>,
+ case couch_server:get_js_engine() of
+ <<"quickjs">> ->
+ true = prompt(Proc, [<<"add_fun">>, Src]),
+ X = 147573952589676412928,
+ Y = 73786976294838206464,
+ Doc = {[{<<"y">>, integer_to_binary(Y)}]},
+ Result = prompt(Proc, [<<"map_doc">>, Doc]),
+ ?assertMatch([[[<<"z">>, <<_/binary>>]]], Result),
+ [[[<<"z">>, Z]]] = Result,
+ ?assertEqual(X + Y, binary_to_integer(Z));
+ <<"spidermonkey">> ->
+ ?assertThrow({compilation_error, _}, prompt(Proc, [<<"add_fun">>, Src]))
+ end.
+
sample_time(Stat) ->
couch_stats:sample([couchdb, query_server, time, Stat]).
diff --git a/src/couch_quickjs/c_src/couchjs.c b/src/couch_quickjs/c_src/couchjs.c
index 7958acda5..be4f02e91 100644
--- a/src/couch_quickjs/c_src/couchjs.c
+++ b/src/couch_quickjs/c_src/couchjs.c
@@ -94,6 +94,7 @@ static void add_cx_methods(JSContext* cx) {
JS_AddIntrinsicRegExp(cx);
JS_AddIntrinsicMapSet(cx);
JS_AddIntrinsicDate(cx);
+ JS_AddIntrinsicBigInt(cx);
}
// Creates a new JSContext with only the provided sandbox function