Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: propagate load errors through wasm #57

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions lib/eszip_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { build, Parser } from "./mod.ts";
import {
assert,
assertEquals,
assertRejects,
} from "https://deno.land/std@0.123.0/testing/asserts.ts";

Deno.test("roundtrip build + parse", async () => {
Expand Down Expand Up @@ -57,3 +58,15 @@ Deno.test("build default loader", async () => {
const eszip = await build(["https://deno.land/std@0.123.0/fs/mod.ts"]);
assert(eszip instanceof Uint8Array);
});

Deno.test("loader errors", async () => {
await assertRejects(
() =>
build(
["https://deno.land/std@0.123.0/fs/mod.ts"],
(specifier: string) => Promise.reject(new Error("oops")),
),
undefined,
"oops",
);
});
130 changes: 67 additions & 63 deletions lib/eszip_wasm.generated.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,7 @@ function getObject(idx) {
return heap[idx];
}

let heap_next = heap.length;

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 cachedTextDecoder = new TextDecoder("utf-8", {
ignoreBOM: true,
fatal: true,
});

cachedTextDecoder.decode();
let WASM_VECTOR_LEN = 0;

let cachegetUint8Memory0 = null;
function getUint8Memory0() {
Expand All @@ -42,21 +23,6 @@ function getUint8Memory0() {
return cachegetUint8Memory0;
}

function getStringFromWasm0(ptr, len) {
return cachedTextDecoder.decode(getUint8Memory0().subarray(ptr, ptr + len));
}

function addHeapObject(obj) {
if (heap_next === heap.length) heap.push(heap.length + 1);
const idx = heap_next;
heap_next = heap[idx];

heap[idx] = obj;
return idx;
}

let WASM_VECTOR_LEN = 0;

let cachedTextEncoder = new TextEncoder("utf-8");

const encodeString = function (arg, view) {
Expand Down Expand Up @@ -100,6 +66,10 @@ function passStringToWasm0(arg, malloc, realloc) {
return ptr;
}

function isLikeNone(x) {
return x === undefined || x === null;
}

let cachegetInt32Memory0 = null;
function getInt32Memory0() {
if (
Expand All @@ -111,8 +81,38 @@ function getInt32Memory0() {
return cachegetInt32Memory0;
}

function isLikeNone(x) {
return x === undefined || x === null;
let heap_next = heap.length;

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 cachedTextDecoder = new TextDecoder("utf-8", {
ignoreBOM: true,
fatal: true,
});

cachedTextDecoder.decode();

function getStringFromWasm0(ptr, len) {
return cachedTextDecoder.decode(getUint8Memory0().subarray(ptr, ptr + len));
}

function addHeapObject(obj) {
if (heap_next === heap.length) heap.push(heap.length + 1);
const idx = heap_next;
heap_next = heap[idx];

heap[idx] = obj;
return idx;
}

function debugString(val) {
Expand Down Expand Up @@ -210,7 +210,7 @@ function makeMutClosure(arg0, arg1, dtor, f) {
}
function __wbg_adapter_24(arg0, arg1, arg2) {
wasm
._dyn_core__ops__function__FnMut__A____Output___R_as_wasm_bindgen__closure__WasmClosure___describe__invoke__h73c5e4bcef6d2cfc(
._dyn_core__ops__function__FnMut__A____Output___R_as_wasm_bindgen__closure__WasmClosure___describe__invoke__hc4c17f990ab4b253(
arg0,
arg1,
addHeapObject(arg2),
Expand Down Expand Up @@ -241,8 +241,8 @@ function handleError(f, args) {
wasm.__wbindgen_exn_store(addHeapObject(e));
}
}
function __wbg_adapter_60(arg0, arg1, arg2, arg3) {
wasm.wasm_bindgen__convert__closures__invoke2_mut__h2afd50e4799521ec(
function __wbg_adapter_62(arg0, arg1, arg2, arg3) {
wasm.wasm_bindgen__convert__closures__invoke2_mut__h4055494abbe69ef3(
arg0,
arg1,
addHeapObject(arg2),
Expand Down Expand Up @@ -340,6 +340,20 @@ export class Parser {

const imports = {
__wbindgen_placeholder__: {
__wbindgen_string_get: function (arg0, arg1) {
const obj = getObject(arg1);
var ret = typeof (obj) === "string" ? obj : undefined;
var ptr0 = isLikeNone(ret)
? 0
: passStringToWasm0(
ret,
wasm.__wbindgen_malloc,
wasm.__wbindgen_realloc,
);
var len0 = WASM_VECTOR_LEN;
getInt32Memory0()[arg0 / 4 + 1] = len0;
getInt32Memory0()[arg0 / 4 + 0] = ptr0;
},
__wbindgen_object_drop_ref: function (arg0) {
takeObject(arg0);
},
Expand All @@ -363,14 +377,10 @@ const imports = {
getInt32Memory0()[arg0 / 4 + 1] = len0;
getInt32Memory0()[arg0 / 4 + 0] = ptr0;
},
__wbg_new_16f24b0728c5e67b: function () {
var ret = new Array();
__wbg_new_55259b13834a484c: function (arg0, arg1) {
var ret = new Error(getStringFromWasm0(arg0, arg1));
return addHeapObject(ret);
},
__wbg_push_a72df856079e6930: function (arg0, arg1) {
var ret = getObject(arg0).push(getObject(arg1));
return ret;
},
__wbindgen_memory: function () {
var ret = wasm.memory;
return addHeapObject(ret);
Expand All @@ -391,6 +401,14 @@ const imports = {
var ret = new Uint8Array(getObject(arg0));
return addHeapObject(ret);
},
__wbg_new_16f24b0728c5e67b: function () {
var ret = new Array();
return addHeapObject(ret);
},
__wbg_push_a72df856079e6930: function (arg0, arg1) {
var ret = getObject(arg0).push(getObject(arg1));
return ret;
},
__wbg_newwithlength_5f4ce114a24dfe1e: function (arg0) {
var ret = new Uint8Array(arg0 >>> 0);
return addHeapObject(ret);
Expand All @@ -412,20 +430,6 @@ const imports = {
var ret = getObject(arg0).toString();
return addHeapObject(ret);
},
__wbindgen_string_get: function (arg0, arg1) {
const obj = getObject(arg1);
var ret = typeof (obj) === "string" ? obj : undefined;
var ptr0 = isLikeNone(ret)
? 0
: passStringToWasm0(
ret,
wasm.__wbindgen_malloc,
wasm.__wbindgen_realloc,
);
var len0 = WASM_VECTOR_LEN;
getInt32Memory0()[arg0 / 4 + 1] = len0;
getInt32Memory0()[arg0 / 4 + 0] = ptr0;
},
__wbg_value_db741b03e25c1e0d: function (arg0) {
var ret = getObject(arg0).value;
return isLikeNone(ret) ? 0 : addHeapObject(ret);
Expand Down Expand Up @@ -458,7 +462,7 @@ const imports = {
const a = state0.a;
state0.a = 0;
try {
return __wbg_adapter_60(a, state0.b, arg0, arg1);
return __wbg_adapter_62(a, state0.b, arg0, arg1);
} finally {
state0.a = a;
}
Expand Down Expand Up @@ -580,8 +584,8 @@ const imports = {
var ret = getObject(arg0).then(getObject(arg1));
return addHeapObject(ret);
},
__wbindgen_closure_wrapper8872: function (arg0, arg1, arg2) {
var ret = makeMutClosure(arg0, arg1, 129, __wbg_adapter_24);
__wbindgen_closure_wrapper8892: function (arg0, arg1, arg2) {
var ret = makeMutClosure(arg0, arg1, 131, __wbg_adapter_24);
return addHeapObject(ret);
},
},
Expand Down
Binary file modified lib/eszip_wasm_bg.wasm
Binary file not shown.
13 changes: 10 additions & 3 deletions lib/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ pub async fn build_eszip(
std::panic::set_hook(Box::new(console_error_panic_hook::hook));
let roots: Vec<deno_graph::ModuleSpecifier> = roots
.into_serde()
.map_err(|e| JsValue::from_str(&e.to_string()))?;
.map_err(|e| js_sys::Error::new(&e.to_string()))?;
let mut loader = GraphLoader(loader);
let graph = deno_graph::create_graph(
roots
Expand All @@ -263,8 +263,11 @@ pub async fn build_eszip(
None,
)
.await;
graph
.valid()
.map_err(|e| js_sys::Error::new(&e.to_string()))?;
let eszip = eszip::EszipV2::from_graph(graph, Default::default())
.map_err(|e| JsValue::from_str(&e.to_string()))?;
.map_err(|e| js_sys::Error::new(&e.to_string()))?;
Ok(Uint8Array::from(eszip.into_bytes().as_slice()))
}

Expand Down Expand Up @@ -303,7 +306,11 @@ impl Loader for GraphLoader {
};
response
.map(|value| value.into_serde().unwrap())
.map_err(|_| anyhow::anyhow!("load rejected or errored"))
.map_err(|err| {
anyhow::anyhow!(err
.as_string()
.unwrap_or_else(|| "an error occured during loading".to_string()))
})
})
}
}
Expand Down
6 changes: 5 additions & 1 deletion lib/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,9 @@ export function build(
roots: string[],
loader: (url: string) => Promise<LoadResponse | undefined> = load,
): Promise<Uint8Array> {
return _build(roots, loader);
return _build(
roots,
(specifier: string) =>
loader(specifier).catch((err) => Promise.reject(String(err))),
);
}