11// Provides: plonk_wasm
2- var plonk_wasm = require ( './plonk_wasm.js' ) ;
3- var native = null ;
4- try {
5- native = require ( '../native/plonk_napi.node' ) ;
6- } catch ( e ) {
7- // native not available, keep WASM
8- }
2+ var plonk_wasm = ( function ( ) {
3+ var wasm = require ( './plonk_wasm.js' ) ;
94
10- function snakeToCamel ( name ) {
11- return name . replace ( / _ ( [ a - z ] ) / g, function ( _match , ch ) {
12- return ch . toUpperCase ( ) ;
13- } ) ;
14- }
5+ try {
6+ var native = require ( '@o1js/native-' + process . platform + '-' + process . arch )
157
16- function override ( functionName ) {
17- if ( ! native ) return ;
18- var camel = snakeToCamel ( functionName ) ;
19- var impl = native [ functionName ] || native [ camel ] ;
20- if ( typeof impl === 'function' ) {
21- plonk_wasm [ functionName ] = impl ;
8+ // THIS IS A RUNTIME OVERRIDE
9+ // YOU HAVE TO RUN IT TO SEE IF IT BREAKS
10+ // IT WON'T CRASH UNLESS O1JS_REQUIRE_NATIVE_BINDINGS
11+ // IS SET
12+ var overrides = [
13+ "prover_to_json" ,
14+ "prover_index_from_bytes" ,
15+ "prover_index_to_bytes" ,
16+ "caml_pasta_fp_poseidon_block_cipher" ,
17+ "caml_pasta_fq_poseidon_block_cipher" ,
18+ "caml_pasta_fp_plonk_proof_create" ,
19+ ]
20+
21+ overrides . forEach ( function ( override ) {
22+ wasm [ override ] = native [ override ]
23+ } )
24+
25+ wasm . native = true ;
26+ } catch ( e ) {
27+ if ( process . env . O1JS_REQUIRE_NATIVE_BINDINGS ) {
28+ console . error ( e )
29+ console . log ( "native didn't load" )
30+ process . exit ( 1 ) ;
31+ }
2232 }
23- }
2433
25- // Overwrite only the functions that are already available in native
26- override ( 'caml_pasta_fp_poseidon_block_cipher' ) ;
27- override ( 'caml_pasta_fq_poseidon_block_cipher' ) ;
28- override ( 'prover_to_json' ) ;
29- override ( 'prover_index_from_bytes' ) ;
34+ return wasm
35+ } ) ( )
0 commit comments