Skip to content

Commit

Permalink
Adds wasm methods for serialising and deserialising an acir structure (
Browse files Browse the repository at this point in the history
…#331)

* add serialisation methods for acir

* update generated wasm:

wasm-pack build --target nodejs
  • Loading branch information
kevaundray authored Sep 2, 2022
1 parent e6d14b1 commit 6c74bf2
Show file tree
Hide file tree
Showing 8 changed files with 104 additions and 35 deletions.
Empty file removed crates/wasm/pkg/.gitignore
Empty file.
10 changes: 10 additions & 0 deletions crates/wasm/pkg/noir_wasm.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,13 @@
* @returns {any}
*/
export function compile(src: string): any;
/**
* @param {Uint8Array} bytes
* @returns {any}
*/
export function acir_from_bytes(bytes: Uint8Array): any;
/**
* @param {any} acir
* @returns {Uint8Array}
*/
export function acir_to_bytes(acir: any): Uint8Array;
100 changes: 73 additions & 27 deletions crates/wasm/pkg/noir_wasm.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ let cachedTextDecoder = new TextDecoder('utf-8', { ignoreBOM: true, fatal: true

cachedTextDecoder.decode();

let cachedUint8Memory0;
let cachedUint8Memory0 = new Uint8Array();

function getUint8Memory0() {
if (cachedUint8Memory0.byteLength === 0) {
cachedUint8Memory0 = new Uint8Array(wasm.memory.buffer);
Expand Down Expand Up @@ -37,18 +38,6 @@ function addHeapObject(obj) {

function getObject(idx) { return heap[idx]; }

function dropObject(idx) {
if (idx < 36) return;
heap[idx] = heap_next;
heap_next = idx;
}

function takeObject(idx) {
const ret = getObject(idx);
dropObject(idx);
return ret;
}

let WASM_VECTOR_LEN = 0;

let cachedTextEncoder = new TextEncoder('utf-8');
Expand Down Expand Up @@ -103,6 +92,27 @@ function passStringToWasm0(arg, malloc, realloc) {
WASM_VECTOR_LEN = offset;
return ptr;
}

let cachedInt32Memory0 = new Int32Array();

function getInt32Memory0() {
if (cachedInt32Memory0.byteLength === 0) {
cachedInt32Memory0 = new Int32Array(wasm.memory.buffer);
}
return cachedInt32Memory0;
}

function dropObject(idx) {
if (idx < 36) return;
heap[idx] = heap_next;
heap_next = idx;
}

function takeObject(idx) {
const ret = getObject(idx);
dropObject(idx);
return ret;
}
/**
* @param {string} src
* @returns {any}
Expand All @@ -114,19 +124,49 @@ module.exports.compile = function(src) {
return takeObject(ret);
};

let cachedInt32Memory0;
function getInt32Memory0() {
if (cachedInt32Memory0.byteLength === 0) {
cachedInt32Memory0 = new Int32Array(wasm.memory.buffer);
}
return cachedInt32Memory0;
function passArray8ToWasm0(arg, malloc) {
const ptr = malloc(arg.length * 1);
getUint8Memory0().set(arg, ptr / 1);
WASM_VECTOR_LEN = arg.length;
return ptr;
}
/**
* @param {Uint8Array} bytes
* @returns {any}
*/
module.exports.acir_from_bytes = function(bytes) {
const ptr0 = passArray8ToWasm0(bytes, wasm.__wbindgen_export_0);
const len0 = WASM_VECTOR_LEN;
const ret = wasm.acir_from_bytes(ptr0, len0);
return takeObject(ret);
};

function getArrayU8FromWasm0(ptr, len) {
return getUint8Memory0().subarray(ptr / 1, ptr / 1 + len);
}
/**
* @param {any} acir
* @returns {Uint8Array}
*/
module.exports.acir_to_bytes = function(acir) {
try {
const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
wasm.acir_to_bytes(retptr, addHeapObject(acir));
var r0 = getInt32Memory0()[retptr / 4 + 0];
var r1 = getInt32Memory0()[retptr / 4 + 1];
var v0 = getArrayU8FromWasm0(r0, r1).slice();
wasm.__wbindgen_export_2(r0, r1 * 1);
return v0;
} finally {
wasm.__wbindgen_add_to_stack_pointer(16);
}
};

function handleError(f, args) {
try {
return f.apply(this, args);
} catch (e) {
wasm.__wbindgen_export_2(addHeapObject(e));
wasm.__wbindgen_export_3(addHeapObject(e));
}
}

Expand All @@ -135,18 +175,27 @@ module.exports.__wbindgen_json_parse = function(arg0, arg1) {
return addHeapObject(ret);
};

module.exports.__wbg_readfile_381ecedf0ec0c1aa = function() { return handleError(function (arg0, arg1, arg2) {
const ret = read_file(getStringFromWasm0(arg1, arg2));
module.exports.__wbindgen_json_serialize = function(arg0, arg1) {
const obj = getObject(arg1);
const ret = JSON.stringify(obj === undefined ? null : obj);
const ptr0 = passStringToWasm0(ret, wasm.__wbindgen_export_0, wasm.__wbindgen_export_1);
const len0 = WASM_VECTOR_LEN;
getInt32Memory0()[arg0 / 4 + 1] = len0;
getInt32Memory0()[arg0 / 4 + 0] = ptr0;
}, arguments) };
};

module.exports.__wbindgen_object_drop_ref = function(arg0) {
takeObject(arg0);
};

module.exports.__wbg_readfile_381ecedf0ec0c1aa = function() { return handleError(function (arg0, arg1, arg2) {
const ret = read_file(getStringFromWasm0(arg1, arg2));
const ptr0 = passStringToWasm0(ret, wasm.__wbindgen_export_0, wasm.__wbindgen_export_1);
const len0 = WASM_VECTOR_LEN;
getInt32Memory0()[arg0 / 4 + 1] = len0;
getInt32Memory0()[arg0 / 4 + 0] = ptr0;
}, arguments) };

module.exports.__wbg_new_693216e109162396 = function() {
const ret = new Error();
return addHeapObject(ret);
Expand All @@ -164,7 +213,7 @@ module.exports.__wbg_error_09919627ac0992f5 = function(arg0, arg1) {
try {
console.error(getStringFromWasm0(arg0, arg1));
} finally {
wasm.__wbindgen_export_3(arg0, arg1);
wasm.__wbindgen_export_2(arg0, arg1);
}
};

Expand All @@ -176,6 +225,3 @@ const wasmInstance = new WebAssembly.Instance(wasmModule, imports);
wasm = wasmInstance.exports;
module.exports.__wasm = wasm;

cachedInt32Memory0 = new Int32Array(wasm.memory.buffer);
cachedUint8Memory0 = new Uint8Array(wasm.memory.buffer);

Binary file modified crates/wasm/pkg/noir_wasm_bg.wasm
Binary file not shown.
7 changes: 5 additions & 2 deletions crates/wasm/pkg/noir_wasm_bg.wasm.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@
/* eslint-disable */
export const memory: WebAssembly.Memory;
export function compile(a: number, b: number): number;
export function acir_from_bytes(a: number, b: number): number;
export function acir_to_bytes(a: number, b: number): void;
export function rust_psm_on_stack(a: number, b: number, c: number, d: number): void;
export function rust_psm_stack_direction(): number;
export function rust_psm_stack_pointer(): number;
export function rust_psm_replace_stack(a: number, b: number, c: number): void;
export function __wbindgen_export_0(a: number): number;
export function __wbindgen_export_1(a: number, b: number, c: number): number;
export function __wbindgen_export_2(a: number): void;
export function __wbindgen_export_3(a: number, b: number): void;
export function __wbindgen_add_to_stack_pointer(a: number): number;
export function __wbindgen_export_2(a: number, b: number): void;
export function __wbindgen_export_3(a: number): void;
5 changes: 0 additions & 5 deletions crates/wasm/pkg/package-lock.json

This file was deleted.

2 changes: 1 addition & 1 deletion crates/wasm/pkg/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@noir-lang/noir_wasm",
"version": "0.2.0",
"version": "0.3.0",
"files": [
"noir_wasm_bg.wasm",
"noir_wasm.js",
Expand Down
15 changes: 15 additions & 0 deletions crates/wasm/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use acvm::acir::circuit::Circuit;
use std::path::PathBuf;
use wasm_bindgen::prelude::*;

Expand All @@ -11,3 +12,17 @@ pub fn compile(src: String) -> JsValue {
let compiled_program = noirc_driver::Driver::compile_file(path, language);
JsValue::from_serde(&compiled_program).unwrap()
}
// Deserialises bytes into ACIR structure
#[wasm_bindgen]
pub fn acir_from_bytes(bytes: Vec<u8>) -> JsValue {
console_error_panic_hook::set_once();
let circuit = Circuit::from_bytes(&bytes);
JsValue::from_serde(&circuit).unwrap()
}

#[wasm_bindgen]
pub fn acir_to_bytes(acir: JsValue) -> Vec<u8> {
console_error_panic_hook::set_once();
let circuit: Circuit = acir.into_serde().unwrap();
circuit.to_bytes()
}

0 comments on commit 6c74bf2

Please sign in to comment.