Skip to content

Commit 08ca821

Browse files
added o1js_bindings
1 parent 08c34e6 commit 08ca821

19 files changed

+3250
-0
lines changed

src/dune-project

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@
125125
(package (name node_error_service))
126126
(package (name node_status_service))
127127
(package (name non_zero_curve_point))
128+
(package (name o1js_bindings))
128129
(package (name o1trace))
129130
(package (name o1trace_webkit_event))
130131
(package (name one_or_two))
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
*
2+
!.gitignore

src/lib/o1js_bindings/dune

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
(executable
2+
(instrumentation
3+
(backend bisect_ppx))
4+
(libraries fields_derivers.zkapps mina_base yojson)
5+
(link_flags (-linkall))
6+
(modes native)
7+
(modules o1js_types)
8+
(name o1js_types)
9+
(package o1js_bindings)
10+
(preprocess
11+
(pps ppx_version))
12+
(public_name o1js-types))
13+
14+
(rule
15+
(targets jsLayout.json)
16+
(action
17+
(with-stdout-to
18+
jsLayout.json
19+
(run %{exe:./o1js_types.exe}))))
20+
21+
(executable
22+
(instrumentation
23+
(backend bisect_ppx))
24+
(libraries
25+
base
26+
base58_check
27+
core_kernel
28+
hash_prefixes
29+
mina_base
30+
mina_signature_kind
31+
pickles
32+
protocol_version
33+
random_oracle
34+
random_oracle.permutation.ocaml
35+
sponge
36+
yojson)
37+
(link_flags (-linkall))
38+
(name o1js_constants)
39+
(modes native)
40+
(modules o1js_constants)
41+
(package o1js_bindings)
42+
(preprocess
43+
(pps ppx_version))
44+
(public_name o1js-constants))
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
(env
2+
(_
3+
(js_of_ocaml
4+
(compilation_mode whole_program))))
5+
6+
(rule
7+
(enabled_if
8+
(= %{env:PREBUILT_KIMCHI_BINDINGS_JS_NODE_JS=n} n))
9+
(targets node_js_plonk_wasm.js node_js_plonk_wasm_bg.wasm)
10+
(deps
11+
(:d1 ../../crypto/kimchi_bindings/js/node_js/plonk_wasm.js)
12+
(:d2 ../../crypto/kimchi_bindings/js/node_js/plonk_wasm_bg.wasm))
13+
(mode
14+
(promote
15+
(into ../artifacts)))
16+
(action
17+
(progn
18+
(run cp %{d1} node_js_plonk_wasm.js)
19+
(run cp %{d2} node_js_plonk_wasm_bg.wasm))))
20+
21+
(rule
22+
(enabled_if
23+
(= %{env:PREBUILT_KIMCHI_BINDINGS_JS_WEB=n} n))
24+
(targets web_plonk_wasm.js web_plonk_wasm_bg.wasm)
25+
(deps
26+
(:d1 ../../crypto/kimchi_bindings/js/web/plonk_wasm.js)
27+
(:d2 ../../crypto/kimchi_bindings/js/web/plonk_wasm_bg.wasm))
28+
(mode
29+
(promote
30+
(into ../artifacts)))
31+
(action
32+
(progn
33+
(run cp %{d1} web_plonk_wasm.js)
34+
(run cp %{d2} web_plonk_wasm_bg.wasm))))
35+
36+
(rule
37+
(enabled_if
38+
(<> %{env:PREBUILT_KIMCHI_BINDINGS_JS_NODE_JS=n} n))
39+
(targets node_js_plonk_wasm.js node_js_plonk_wasm_bg.wasm)
40+
(mode
41+
(promote
42+
(into ../artifacts)))
43+
(action
44+
(progn
45+
(run
46+
cp
47+
%{env:PREBUILT_KIMCHI_BINDINGS_JS_NODE_JS=n}/plonk_wasm.js
48+
node_js_plonk_wasm.js)
49+
(run
50+
cp
51+
%{env:PREBUILT_KIMCHI_BINDINGS_JS_NODE_JS=n}/plonk_wasm_bg.wasm
52+
node_js_plonk_wasm_bg.wasm))))
53+
54+
(rule
55+
(enabled_if
56+
(<> %{env:PREBUILT_KIMCHI_BINDINGS_JS_WEB=n} n))
57+
(targets web_plonk_wasm.js web_plonk_wasm_bg.wasm)
58+
(mode
59+
(promote
60+
(into ../artifacts)))
61+
(action
62+
(progn
63+
(run
64+
cp
65+
%{env:PREBUILT_KIMCHI_BINDINGS_JS_WEB=n}/plonk_wasm.js
66+
web_plonk_wasm.js)
67+
(run
68+
cp
69+
%{env:PREBUILT_KIMCHI_BINDINGS_JS_WEB=n}/plonk_wasm_bg.wasm
70+
web_plonk_wasm_bg.wasm))))
71+
72+
(executable
73+
(name o1js_node)
74+
(modules o1js_node)
75+
(modes js)
76+
(promote (into ../artifacts))
77+
(link_flags :standard -noautolink -g)
78+
(js_of_ocaml
79+
(flags :standard +toplevel.js +dynlink.js --source-map)
80+
(link_flags :standard --source-map)
81+
(javascript_files overrides.js))
82+
(libraries o1js_bindings.lib bindings_js.node_backend)
83+
(link_deps node_js_plonk_wasm.js node_js_plonk_wasm_bg.wasm)
84+
(instrumentation
85+
(backend bisect_ppx))
86+
(forbidden_libraries async core re2 ctypes)
87+
(preprocess
88+
(pps ppx_version js_of_ocaml-ppx)))
89+
90+
(executable
91+
(name o1js_web)
92+
(modules o1js_web)
93+
(modes js)
94+
(promote (into ../artifacts))
95+
(link_flags :standard -noautolink)
96+
(js_of_ocaml
97+
(flags :standard +toplevel.js +dynlink.js)
98+
(javascript_files overrides.js))
99+
(libraries o1js_bindings.lib bindings_js.web_backend)
100+
(link_deps web_plonk_wasm.js web_plonk_wasm_bg.wasm)
101+
(instrumentation
102+
(backend bisect_ppx))
103+
(forbidden_libraries async core re2 ctypes)
104+
(preprocess
105+
(pps ppx_version js_of_ocaml-ppx)))
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
let () = O1js_bindings_lib.export_global ()
2+
3+
let () = O1js_bindings_lib.export ()
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
let () = O1js_bindings_lib.export_global ()
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/* global caml_named_value, caml_global_data, caml_string_of_jsstring
2+
*/
3+
4+
//Provides: caml_wrap_exception const (const)
5+
//Requires: caml_global_data,caml_string_of_jsstring,caml_named_value
6+
//Requires: caml_return_exn_constant
7+
function caml_wrap_exception(e) {
8+
if (e instanceof Array) return e;
9+
if (e instanceof globalThis.Error && caml_named_value('jsError'))
10+
return [0, caml_named_value('jsError'), e];
11+
//fallback: wrapped in Failure
12+
return [0, caml_global_data.Failure, caml_string_of_jsstring(String(e))];
13+
}
14+
15+
//Provides: caml_raise_with_string (const, const)
16+
function caml_raise_with_string(tag, msg) {
17+
throw globalThis.Error(msg.c);
18+
}
19+
20+
//Provides: custom_reraise_exn
21+
function custom_reraise_exn(exn, fallbackMessage) {
22+
// this handles the common case of a JS Error reraised by OCaml
23+
// in that case, the error will first be wrapped in OCaml with "caml_wrap_exception"
24+
// (defined in js_of_ocaml-compiler / jslib.js)
25+
// which results in [0, caml_named_value("jsError"), err]
26+
var err = exn[2];
27+
if (err instanceof globalThis.Error) {
28+
throw err;
29+
} else {
30+
throw Error(fallbackMessage);
31+
}
32+
}
33+
34+
/**
35+
* This overrides the handler for uncaught exceptions in js_of_ocaml,
36+
* fixing the flaw that by default, no actual `Error`s are thrown,
37+
* but other objects (arrays) which are missing an error trace.
38+
* This override should make it much easier to find the source of an error.
39+
*/
40+
//Provides: caml_fatal_uncaught_exception
41+
function caml_fatal_uncaught_exception(err) {
42+
// first, we search for an actual error inside `err`,
43+
// since this is the best thing to throw
44+
function throw_errors(err) {
45+
if (err instanceof Error) throw err;
46+
else if (Array.isArray(err)) {
47+
err.forEach(throw_errors);
48+
}
49+
}
50+
throw_errors(err);
51+
// if this didn't throw an error, let's log whatever we got
52+
console.dir(err, { depth: 20 });
53+
// now, try to collect all strings in the error and throw that
54+
function collect_strings(err, acc) {
55+
var str = undefined;
56+
if (typeof err === 'string') {
57+
str = err;
58+
} else if (err && err.constructor && err.constructor.name === 'MlBytes') {
59+
str = err.c;
60+
} else if (Array.isArray(err)) {
61+
err.forEach(function (e) {
62+
collect_strings(e, acc);
63+
});
64+
}
65+
if (!str) return acc.string;
66+
if (acc.string === undefined) acc.string = str;
67+
else acc.string = acc.string + '\n' + str;
68+
return acc.string;
69+
}
70+
var str = collect_strings(err, {});
71+
if (str !== undefined) throw globalThis.Error(str);
72+
// otherwise, just throw an unhelpful error
73+
console.dir(err, { depth: 10 });
74+
throw globalThis.Error('Unknown error thrown from OCaml');
75+
}

0 commit comments

Comments
 (0)