Skip to content

Commit

Permalink
deploy: bd42bff
Browse files Browse the repository at this point in the history
  • Loading branch information
thesayyn committed Feb 17, 2024
1 parent fa8823b commit dc80ae4
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 12 deletions.
5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,7 @@ crate-type = ["cdylib"]

[dependencies]
program = {path = "../program"}
wasm-bindgen = "0.2"
wasm-bindgen = "0.2"

[profile.release]
panic = "unwind"
2 changes: 1 addition & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

const editor = document.querySelector("wc-codemirror");

requestAnimationFrame(() => editor.setValue("(2000 / 2 == 1000) - 2 == 998"))
requestAnimationFrame(() => editor.setValue("(2000 / 2) - 2 == 998"))

const card = document.querySelector("sp-card");

Expand Down
3 changes: 2 additions & 1 deletion pkg/web.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ export type InitInput = RequestInfo | URL | Response | BufferSource | WebAssembl

export interface InitOutput {
readonly memory: WebAssembly.Memory;
readonly execute: (a: number, b: number) => number;
readonly execute: (a: number, b: number, c: number) => void;
readonly __wbindgen_free: (a: number, b: number, c: number) => void;
readonly __wbindgen_add_to_stack_pointer: (a: number) => number;
readonly __wbindgen_malloc: (a: number, b: number) => number;
readonly __wbindgen_realloc: (a: number, b: number, c: number, d: number) => number;
}
Expand Down
49 changes: 45 additions & 4 deletions pkg/web.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,55 @@ function passStringToWasm0(arg, malloc, realloc) {
WASM_VECTOR_LEN = offset;
return ptr;
}

let cachedInt32Memory0 = null;

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

const heap = new Array(128).fill(undefined);

heap.push(undefined, null, true, false);

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

let heap_next = heap.length;

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

function takeObject(idx) {
const ret = getObject(idx);
dropObject(idx);
return ret;
}
/**
* @param {string} source
* @returns {boolean}
*/
export function execute(source) {
const ptr0 = passStringToWasm0(source, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
const len0 = WASM_VECTOR_LEN;
const ret = wasm.execute(ptr0, len0);
return ret !== 0;
try {
const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
const ptr0 = passStringToWasm0(source, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
const len0 = WASM_VECTOR_LEN;
wasm.execute(retptr, ptr0, len0);
var r0 = getInt32Memory0()[retptr / 4 + 0];
var r1 = getInt32Memory0()[retptr / 4 + 1];
var r2 = getInt32Memory0()[retptr / 4 + 2];
if (r2) {
throw takeObject(r1);
}
return r0 !== 0;
} finally {
wasm.__wbindgen_add_to_stack_pointer(16);
}
}

async function __wbg_load(module, imports) {
Expand Down Expand Up @@ -151,6 +191,7 @@ function __wbg_init_memory(imports, maybe_memory) {
function __wbg_finalize_init(instance, module) {
wasm = instance.exports;
__wbg_init.__wbindgen_wasm_module = module;
cachedInt32Memory0 = null;
cachedUint8Memory0 = null;


Expand Down
Binary file modified pkg/web_bg.wasm
Binary file not shown.
3 changes: 2 additions & 1 deletion pkg/web_bg.wasm.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
/* tslint:disable */
/* eslint-disable */
export const memory: WebAssembly.Memory;
export function execute(a: number, b: number): number;
export function execute(a: number, b: number, c: number): void;
export function __wbindgen_free(a: number, b: number, c: number): void;
export function __wbindgen_add_to_stack_pointer(a: number): number;
export function __wbindgen_malloc(a: number, b: number): number;
export function __wbindgen_realloc(a: number, b: number, c: number, d: number): number;
17 changes: 13 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,23 @@ extern "C" {
fn log(message: String);
}

#[wasm_bindgen]
pub fn execute(source: &str) -> bool {
log(format!("parsing {}", source));
fn eval(source: &str) -> bool {
match Program::new(source) {
Ok(p) => p.execute(Context::default()),
Err(err) => {
error(format!("parse error {}", err));
error(format!("cel-rs: parse error {}", err));
false
},
}
}


#[wasm_bindgen]
pub fn execute(source: &str) -> Result<bool, JsError> {
log(format!("cel-rs: parsing {}", source));
let res = std::panic::catch_unwind(|| eval(source));
match res {
Ok(b) => Ok(b),
Err(e) => Err(JsError::new(format!("{:?}", e).as_str()))
}
}

0 comments on commit dc80ae4

Please sign in to comment.