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 all commits
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",
);
});
56 changes: 31 additions & 25 deletions lib/eszip_wasm.generated.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,15 @@ function takeObject(idx) {
return ret;
}

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 cachegetUint8Memory0 = null;
Expand Down Expand Up @@ -95,15 +104,6 @@ function getInt32Memory0() {
return cachegetInt32Memory0;
}

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 cachedTextDecoder = new TextDecoder("utf-8", {
ignoreBOM: true,
fatal: true,
Expand Down Expand Up @@ -241,7 +241,7 @@ function handleError(f, args) {
wasm.__wbindgen_exn_store(addHeapObject(e));
}
}
function __wbg_adapter_60(arg0, arg1, arg2, arg3) {
function __wbg_adapter_62(arg0, arg1, arg2, arg3) {
wasm.wasm_bindgen__convert__closures__invoke2_mut__h7fbd29c1fe4dda8a(
arg0,
arg1,
Expand Down Expand Up @@ -364,18 +364,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_f6f2ef297faa6b4a: function (arg0) {
var ret = getObject(arg0).value;
return isLikeNone(ret) ? 0 : addHeapObject(ret);
Expand Down Expand Up @@ -420,7 +408,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 All @@ -431,6 +419,20 @@ const imports = {
state0.a = state0.b = 0;
}
},
__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_string_new: function (arg0, arg1) {
var ret = getStringFromWasm0(arg0, arg1);
return addHeapObject(ret);
Expand All @@ -447,6 +449,10 @@ const imports = {
getInt32Memory0()[arg0 / 4 + 1] = len0;
getInt32Memory0()[arg0 / 4 + 0] = ptr0;
},
__wbg_new_55259b13834a484c: function (arg0, arg1) {
var ret = new Error(getStringFromWasm0(arg0, arg1));
return addHeapObject(ret);
},
__wbg_newwithbyteoffsetandlength_278ec7532799393a: function (
arg0,
arg1,
Expand Down Expand Up @@ -578,8 +584,8 @@ const imports = {
var ret = getObject(arg0).then(getObject(arg1));
return addHeapObject(ret);
},
__wbindgen_closure_wrapper8543: function (arg0, arg1, arg2) {
var ret = makeMutClosure(arg0, arg1, 134, __wbg_adapter_24);
__wbindgen_closure_wrapper8569: function (arg0, arg1, arg2) {
var ret = makeMutClosure(arg0, arg1, 135, __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))),
);
}