From 37bf643636bcf6fd346b3743931cc40d880bc35d Mon Sep 17 00:00:00 2001 From: Danny Willems Date: Tue, 1 Aug 2023 19:16:31 +0200 Subject: [PATCH] Replace joo_global_object by globalThis See https://github.com/o1-labs/snarkyjs-bindings/issues/103 --- kimchi/js/bindings.js | 26 ++++++++++++------------ kimchi/js/js_backend/bigint256.js | 6 +++--- kimchi/js/js_backend/finite_field.js | 7 +++---- kimchi/js/web/web_backend.js | 2 +- ocaml/overrides.js | 6 +++--- scripts/build-snarkyjs-node-artifacts.sh | 6 +++--- scripts/build-snarkyjs-web.sh | 6 +++--- scripts/update-snarkyjs-bindings.sh | 6 +++--- 8 files changed, 32 insertions(+), 33 deletions(-) diff --git a/kimchi/js/bindings.js b/kimchi/js/bindings.js index cf7eae55..bc8e77e9 100644 --- a/kimchi/js/bindings.js +++ b/kimchi/js/bindings.js @@ -1,4 +1,4 @@ -/* global joo_global_object, plonk_wasm, caml_js_to_bool, caml_jsstring_of_string, +/* global globalThis, plonk_wasm, caml_js_to_bool, caml_jsstring_of_string, caml_string_of_jsstring caml_create_bytes, caml_bytes_unsafe_set, caml_bytes_unsafe_get, caml_ml_bytes_length, UInt64, caml_int64_of_int32 @@ -20,7 +20,7 @@ var caml_bytes_of_uint8array = function (uint8array) { // Requires: caml_ml_bytes_length, caml_bytes_unsafe_get var caml_bytes_to_uint8array = function (ocaml_bytes) { var length = caml_ml_bytes_length(ocaml_bytes); - var bytes = new joo_global_object.Uint8Array(length); + var bytes = new globalThis.Uint8Array(length); for (var i = 0; i < length; i++) { // No need to convert here: OCaml Char.t is just an int under the hood. bytes[i] = caml_bytes_unsafe_get(ocaml_bytes, i); @@ -426,10 +426,10 @@ var caml_u8array_vector_to_rust_flat_vector = function (v) { var i = 1; // The first entry is the OCaml tag for arrays var len = v.length - i; if (len === 0) { - return new joo_global_object.Uint8Array(0); + return new globalThis.Uint8Array(0); } var inner_len = v[i].length; - var res = new joo_global_object.Uint8Array(len * inner_len); + var res = new globalThis.Uint8Array(len * inner_len); for (var pos = 0; i <= len; i++) { for (var j = 0; j < inner_len; j++, pos++) { res[pos] = v[i][j]; @@ -445,7 +445,7 @@ var caml_u8array_vector_of_rust_flat_vector = function (v, inner_len) { var res = new Array(output_len + 1); res[0] = 0; // OCaml tag before array contents, so that we can use this with arrays or vectors for (var i = 1, pos = 0; i <= output_len; i++) { - var inner_res = new joo_global_object.Uint8Array(inner_len); + var inner_res = new globalThis.Uint8Array(inner_len); for (var j = 0; j < inner_len; j++, pos++) { inner_res[j] = v[pos]; } @@ -457,7 +457,7 @@ var caml_u8array_vector_of_rust_flat_vector = function (v, inner_len) { // Provides: js_class_vector_to_rust_vector var js_class_vector_to_rust_vector = function (v) { var len = v.length; - var res = new joo_global_object.Uint32Array(len); + var res = new globalThis.Uint32Array(len); for (var i = 0; i < len; i++) { // Beware: caller may need to do finalizer things to avoid these // pointers disappearing out from under us. @@ -501,7 +501,7 @@ var caml_fp_vector_get = function (v, i) { 'caml_fp_vector_get: Index out of bounds, got ' + i + '/' + (v.length - 1) ); } - return new joo_global_object.Uint8Array(value); + return new globalThis.Uint8Array(value); }; // Provides: caml_fp_vector_to_rust @@ -540,7 +540,7 @@ var caml_fq_vector_get = function (v, i) { 'caml_fq_vector_get: Index out of bounds, got ' + i + '/' + (v.length - 1) ); } - return new joo_global_object.Uint8Array(value); + return new globalThis.Uint8Array(value); }; // Provides: caml_fq_vector_to_rust @@ -557,11 +557,11 @@ var caml_fq_vector_of_rust = function (v) { }; // Provides: free_finalization_registry -var free_finalization_registry = new joo_global_object.FinalizationRegistry( - function (instance_representative) { - instance_representative.free(); - } -); +var free_finalization_registry = new globalThis.FinalizationRegistry(function ( + instance_representative +) { + instance_representative.free(); +}); // Provides: free_on_finalize // Requires: free_finalization_registry diff --git a/kimchi/js/js_backend/bigint256.js b/kimchi/js/js_backend/bigint256.js index 646e8855..d1794e8b 100644 --- a/kimchi/js/js_backend/bigint256.js +++ b/kimchi/js/js_backend/bigint256.js @@ -1,11 +1,11 @@ -/* global joo_global_object, caml_js_to_bool, caml_jsstring_of_string, caml_string_of_jsstring, +/* global globalThis, caml_js_to_bool, caml_jsstring_of_string, caml_string_of_jsstring, caml_ml_bytes_length, caml_bytes_unsafe_get, caml_create_bytes, caml_bytes_unsafe_set */ // Provides: BigInt_ -var BigInt_ = joo_global_object.BigInt; +var BigInt_ = globalThis.BigInt; // Provides: Uint8Array_ -var Uint8Array_ = joo_global_object.Uint8Array; +var Uint8Array_ = globalThis.Uint8Array; // Provides: caml_bigint_of_bytes // Requires: BigInt_ diff --git a/kimchi/js/js_backend/finite_field.js b/kimchi/js/js_backend/finite_field.js index 7169882c..9f9d2979 100644 --- a/kimchi/js/js_backend/finite_field.js +++ b/kimchi/js/js_backend/finite_field.js @@ -1,4 +1,4 @@ -/* global joo_global_object, Uint8Array_, BigInt_ +/* global globalThis, Uint8Array_, BigInt_ caml_bigint_of_bytes, caml_js_to_bool, caml_string_of_jsstring */ @@ -139,7 +139,7 @@ function caml_finite_field_is_square(x, p) { // Requires: Uint8Array_ var caml_random_bytes = (function () { // have to use platform-dependent secure randomness - var crypto = joo_global_object.crypto; + var crypto = globalThis.crypto; if (crypto !== undefined && crypto.getRandomValues !== undefined) { // browser / deno return function randomBytes(n) { @@ -489,7 +489,7 @@ var caml_bindings_debug = false; var _test_finite_field = caml_bindings_debug && (function test() { - var console = joo_global_object.console; + var console = globalThis.console; // t is computed correctly from p = 2^32 * t + 1 console.assert( caml_pasta_pm1_odd_factor * (BigInt_(1) << BigInt_(32)) + BigInt_(1) === @@ -499,7 +499,6 @@ var _test_finite_field = caml_pasta_qm1_odd_factor * (BigInt_(1) << BigInt_(32)) + BigInt_(1) === caml_pasta_q_bigint ); - // the primitive root of unity is computed correctly as 5^t var generator = BigInt_(5); var root_fp = caml_finite_field_power( diff --git a/kimchi/js/web/web_backend.js b/kimchi/js/web/web_backend.js index 57c4928e..53db813a 100644 --- a/kimchi/js/web/web_backend.js +++ b/kimchi/js/web/web_backend.js @@ -1,2 +1,2 @@ // Provides: plonk_wasm -var plonk_wasm = joo_global_object.plonk_wasm; +var plonk_wasm = globalThis.plonk_wasm; diff --git a/ocaml/overrides.js b/ocaml/overrides.js index f7597ad1..e1654fcb 100644 --- a/ocaml/overrides.js +++ b/ocaml/overrides.js @@ -1,4 +1,4 @@ -/* global joo_global_object +/* global globalThis */ //Provides: caml_raise_with_string (const, const) @@ -57,8 +57,8 @@ function caml_fatal_uncaught_exception(err) { return acc.string; } var str = collect_strings(err, {}); - if (str !== undefined) throw joo_global_object.Error(str); + if (str !== undefined) throw globalThis.Error(str); // otherwise, just throw an unhelpful error console.dir(err, { depth: 10 }); - throw joo_global_object.Error('Unknown error thrown from OCaml'); + throw globalThis.Error('Unknown error thrown from OCaml'); } diff --git a/scripts/build-snarkyjs-node-artifacts.sh b/scripts/build-snarkyjs-node-artifacts.sh index 6073bcf9..31c1ba76 100755 --- a/scripts/build-snarkyjs-node-artifacts.sh +++ b/scripts/build-snarkyjs-node-artifacts.sh @@ -57,9 +57,9 @@ rm -f _build/snarky_js_node.bc.map # better error messages # TODO: find a less hacky way to make adjustments to jsoo compiler output # `s` is the jsoo representation of the error message string, and `s.c` is the actual JS string -sed -i 's/function failwith(s){throw \[0,Failure,s\]/function failwith(s){throw joo_global_object.Error(s.c)/' "$BINDINGS_PATH"/snarky_js_node.bc.cjs -sed -i 's/function invalid_arg(s){throw \[0,Invalid_argument,s\]/function invalid_arg(s){throw joo_global_object.Error(s.c)/' "$BINDINGS_PATH"/snarky_js_node.bc.cjs -sed -i 's/return \[0,Exn,t\]/return joo_global_object.Error(t.c)/' "$BINDINGS_PATH"/snarky_js_node.bc.cjs +sed -i 's/function failwith(s){throw \[0,Failure,s\]/function failwith(s){throw globalThis.Error(s.c)/' "$BINDINGS_PATH"/snarky_js_node.bc.cjs +sed -i 's/function invalid_arg(s){throw \[0,Invalid_argument,s\]/function invalid_arg(s){throw globalThis.Error(s.c)/' "$BINDINGS_PATH"/snarky_js_node.bc.cjs +sed -i 's/return \[0,Exn,t\]/return globalThis.Error(t.c)/' "$BINDINGS_PATH"/snarky_js_node.bc.cjs # TODO: this doesn't cover all cases, maybe should rewrite to_exn instead sed -i 's/function raise(t){throw caml_call1(to_exn$0,t)}/function raise(t){throw Error(t?.[1]?.c ?? "Unknown error thrown by raise")}/' "$BINDINGS_PATH"/snarky_js_node.bc.cjs diff --git a/scripts/build-snarkyjs-web.sh b/scripts/build-snarkyjs-web.sh index d30b0cb3..3be4607d 100755 --- a/scripts/build-snarkyjs-web.sh +++ b/scripts/build-snarkyjs-web.sh @@ -14,9 +14,9 @@ cp $BUILD_PATH/snarky_js_web*.js $WEB_BINDINGS/ # better error messages # `s` is the jsoo representation of the error message string, and `s.c` is the actual JS string -sed -i 's/function failwith(s){throw \[0,Failure,s\]/function failwith(s){throw joo_global_object.Error(s.c)/' $WEB_BINDINGS/snarky_js_web.bc.js -sed -i 's/function invalid_arg(s){throw \[0,Invalid_argument,s\]/function invalid_arg(s){throw joo_global_object.Error(s.c)/' $WEB_BINDINGS/snarky_js_web.bc.js -sed -i 's/return \[0,Exn,t\]/return joo_global_object.Error(t.c)/' $WEB_BINDINGS/snarky_js_web.bc.js +sed -i 's/function failwith(s){throw \[0,Failure,s\]/function failwith(s){throw globalThis.Error(s.c)/' $WEB_BINDINGS/snarky_js_web.bc.js +sed -i 's/function invalid_arg(s){throw \[0,Invalid_argument,s\]/function invalid_arg(s){throw globalThis.Error(s.c)/' $WEB_BINDINGS/snarky_js_web.bc.js +sed -i 's/return \[0,Exn,t\]/return globalThis.Error(t.c)/' $WEB_BINDINGS/snarky_js_web.bc.js sed -i 's/function raise(t){throw caml_call1(to_exn$0,t)}/function raise(t){throw Error(t?.[1]?.c ?? "some error")}/' $WEB_BINDINGS/snarky_js_web.bc.js pushd $WEB_BINDINGS diff --git a/scripts/update-snarkyjs-bindings.sh b/scripts/update-snarkyjs-bindings.sh index 8230ea2e..66595226 100755 --- a/scripts/update-snarkyjs-bindings.sh +++ b/scripts/update-snarkyjs-bindings.sh @@ -37,9 +37,9 @@ chmod -R 666 "$WEB_BINDINGS"/* # better error messages # `s` is the jsoo representation of the error message string, and `s.c` is the actual JS string -sed -i 's/function failwith(s){throw \[0,Failure,s\]/function failwith(s){throw joo_global_object.Error(s.c)/' $WEB_BINDINGS/snarky_js_web.bc.js -sed -i 's/function invalid_arg(s){throw \[0,Invalid_argument,s\]/function invalid_arg(s){throw joo_global_object.Error(s.c)/' $WEB_BINDINGS/snarky_js_web.bc.js -sed -i 's/return \[0,Exn,t\]/return joo_global_object.Error(t.c)/' $WEB_BINDINGS/snarky_js_web.bc.js +sed -i 's/function failwith(s){throw \[0,Failure,s\]/function failwith(s){throw globalThis.Error(s.c)/' $WEB_BINDINGS/snarky_js_web.bc.js +sed -i 's/function invalid_arg(s){throw \[0,Invalid_argument,s\]/function invalid_arg(s){throw globalThis.Error(s.c)/' $WEB_BINDINGS/snarky_js_web.bc.js +sed -i 's/return \[0,Exn,t\]/return globalThis.Error(t.c)/' $WEB_BINDINGS/snarky_js_web.bc.js sed -i 's/function raise(t){throw caml_call1(to_exn$0,t)}/function raise(t){throw Error(t?.[1]?.c ?? "Unknown error thrown by raise")}/' $WEB_BINDINGS/snarky_js_web.bc.js # optimize wasm / minify JS (we don't do this with jsoo to not break the error message fix above)