Skip to content

Commit

Permalink
Replace joo_global_object by globalThis
Browse files Browse the repository at this point in the history
See #103
  • Loading branch information
dannywillems committed Aug 15, 2023
1 parent b24ff40 commit 37bf643
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 33 deletions.
26 changes: 13 additions & 13 deletions kimchi/js/bindings.js
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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);
Expand Down Expand Up @@ -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];
Expand All @@ -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];
}
Expand All @@ -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.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
6 changes: 3 additions & 3 deletions kimchi/js/js_backend/bigint256.js
Original file line number Diff line number Diff line change
@@ -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_
Expand Down
7 changes: 3 additions & 4 deletions kimchi/js/js_backend/finite_field.js
Original file line number Diff line number Diff line change
@@ -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
*/

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) ===
Expand All @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion kimchi/js/web/web_backend.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
// Provides: plonk_wasm
var plonk_wasm = joo_global_object.plonk_wasm;
var plonk_wasm = globalThis.plonk_wasm;
6 changes: 3 additions & 3 deletions ocaml/overrides.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* global joo_global_object
/* global globalThis
*/

//Provides: caml_raise_with_string (const, const)
Expand Down Expand Up @@ -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');
}
6 changes: 3 additions & 3 deletions scripts/build-snarkyjs-node-artifacts.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
6 changes: 3 additions & 3 deletions scripts/build-snarkyjs-web.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions scripts/update-snarkyjs-bindings.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 37bf643

Please sign in to comment.