Skip to content

Commit

Permalink
feat(js): add EszipError type
Browse files Browse the repository at this point in the history
This commit adds a new error type that is returned from the JS API. It
has fields to describe the location (specifier + line + col) of an
error, in addition to storing the error message.
  • Loading branch information
lucacasonato committed Sep 26, 2022
1 parent 3eee9d3 commit 17affaa
Show file tree
Hide file tree
Showing 10 changed files with 324 additions and 128 deletions.
21 changes: 11 additions & 10 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ deno_graph = "0.34.0"
eszip = { path = "../" }
import_map = "0.12.1"
js-sys = { version = "0.3.58" }
serde = { version = "1.0.145", features = ["derive"] }
tokio = { version = "1.16", features = ["io-std", "io-util"] }
wasm-bindgen = { version = "0.2.81", features = ["serde-serialize"] }
wasm-bindgen-futures = { version = "0.4.31" }
web-sys = { version = "0.3.58", features = ["ReadableStreamByobReader"] }
web-sys = { version = "0.3.58", features = ["ReadableStreamByobReader"] }
27 changes: 23 additions & 4 deletions lib/eszip_test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { build, Parser } from "./mod.ts";
import { build, EszipError, Parser } from "./mod.ts";
import {
assert,
assertEquals,
assertRejects,
} from "https://deno.land/std@0.123.0/testing/asserts.ts";
} from "https://deno.land/std@0.155.0/testing/asserts.ts";

Deno.test("roundtrip build + parse", async () => {
const eszip = await build([
Expand Down Expand Up @@ -60,7 +60,11 @@ Deno.test("build default loader", async () => {
});

Deno.test("build with import map", async () => {
const eszip = await build(["data:application/javascript,import 'std/fs/mod.ts'"], undefined, "data:application/json,{\"imports\":{\"std/\":\"https://deno.land/std/\"}}");
const eszip = await build(
["data:application/javascript,import 'std/fs/mod.ts'"],
undefined,
'data:application/json,{"imports":{"std/":"https://deno.land/std/"}}',
);
assert(eszip instanceof Uint8Array);
});

Expand All @@ -71,7 +75,22 @@ Deno.test("loader errors", async () => {
["https://deno.land/[email protected]/fs/mod.ts"],
(specifier: string) => Promise.reject(new Error("oops")),
),
undefined,
Error,
"oops",
);
});

Deno.test("eszip error", async () => {
const err = await assertRejects(
() => build(["file:///does/not/exist.ts"]),
EszipError,
);
assertEquals(
err.specifier,
"file:///does/not/exist.ts",
);
assertEquals(
err.message,
"Module not found.",
);
});
152 changes: 78 additions & 74 deletions lib/eszip_wasm.generated.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
// @generated file from wasmbuild -- do not edit
// deno-lint-ignore-file
// deno-fmt-ignore-file
// source-hash: 0e24dd809865bd2b3c39b005f133376180af8ad2
// source-hash: fb80f0e7843e78531797481c6d40ef345434781e
let wasm;

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

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

function getObject(idx) {
return heap[idx];
}
const cachedTextDecoder = new TextDecoder("utf-8", {
ignoreBOM: true,
fatal: true,
});

let WASM_VECTOR_LEN = 0;
cachedTextDecoder.decode();

let cachedUint8Memory0;
function getUint8Memory0() {
Expand All @@ -22,6 +19,31 @@ function getUint8Memory0() {
return cachedUint8Memory0;
}

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

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

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

let heap_next = heap.length;

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 getObject(idx) {
return heap[idx];
}

let WASM_VECTOR_LEN = 0;

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

const encodeString = function (arg, view) {
Expand Down Expand Up @@ -77,8 +99,6 @@ function getInt32Memory0() {
return cachedInt32Memory0;
}

let heap_next = heap.length;

function dropObject(idx) {
if (idx < 36) return;
heap[idx] = heap_next;
Expand All @@ -91,26 +111,6 @@ function takeObject(idx) {
return ret;
}

const 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;
}

const CLOSURE_DTORS = new FinalizationRegistry((state) => {
wasm.__wbindgen_export_2.get(state.dtor)(state.a, state.b);
});
Expand Down Expand Up @@ -139,9 +139,9 @@ function makeMutClosure(arg0, arg1, dtor, f) {
CLOSURE_DTORS.register(real, state, state);
return real;
}
function __wbg_adapter_18(arg0, arg1, arg2) {
function __wbg_adapter_20(arg0, arg1, arg2) {
wasm
._dyn_core__ops__function__FnMut__A____Output___R_as_wasm_bindgen__closure__WasmClosure___describe__invoke__hd4f05ebc54a3a357(
._dyn_core__ops__function__FnMut__A____Output___R_as_wasm_bindgen__closure__WasmClosure___describe__invoke__h95e8c15aeb3cd0e8(
arg0,
arg1,
addHeapObject(arg2),
Expand Down Expand Up @@ -177,8 +177,8 @@ function handleError(f, args) {
wasm.__wbindgen_exn_store(addHeapObject(e));
}
}
function __wbg_adapter_50(arg0, arg1, arg2, arg3) {
wasm.wasm_bindgen__convert__closures__invoke2_mut__hccb8632b1071bfde(
function __wbg_adapter_52(arg0, arg1, arg2, arg3) {
wasm.wasm_bindgen__convert__closures__invoke2_mut__h0f236b45974ddf70(
arg0,
arg1,
addHeapObject(arg2),
Expand Down Expand Up @@ -276,6 +276,10 @@ export class Parser {

const imports = {
__wbindgen_placeholder__: {
__wbindgen_json_parse: function (arg0, arg1) {
const ret = JSON.parse(getStringFromWasm0(arg0, arg1));
return addHeapObject(ret);
},
__wbindgen_string_get: function (arg0, arg1) {
const obj = getObject(arg1);
const ret = typeof (obj) === "string" ? obj : undefined;
Expand All @@ -297,42 +301,6 @@ const imports = {
const ret = getStringFromWasm0(arg0, arg1);
return addHeapObject(ret);
},
__wbindgen_json_serialize: function (arg0, arg1) {
const obj = getObject(arg1);
const ret = JSON.stringify(obj === undefined ? null : obj);
const ptr0 = passStringToWasm0(
ret,
wasm.__wbindgen_malloc,
wasm.__wbindgen_realloc,
);
const len0 = WASM_VECTOR_LEN;
getInt32Memory0()[arg0 / 4 + 1] = len0;
getInt32Memory0()[arg0 / 4 + 0] = ptr0;
},
__wbg_new_651776e932b7e9c7: function (arg0, arg1) {
const ret = new Error(getStringFromWasm0(arg0, arg1));
return addHeapObject(ret);
},
__wbindgen_memory: function () {
const ret = wasm.memory;
return addHeapObject(ret);
},
__wbg_buffer_de1150f91b23aa89: function (arg0) {
const ret = getObject(arg0).buffer;
return addHeapObject(ret);
},
__wbg_newwithbyteoffsetandlength_9ca61320599a2c84: function (
arg0,
arg1,
arg2,
) {
const ret = new Uint8Array(getObject(arg0), arg1 >>> 0, arg2 >>> 0);
return addHeapObject(ret);
},
__wbg_new_97cf52648830a70d: function (arg0) {
const ret = new Uint8Array(getObject(arg0));
return addHeapObject(ret);
},
__wbg_new_2ab697f1555e0dbc: function () {
const ret = new Array();
return addHeapObject(ret);
Expand All @@ -345,6 +313,18 @@ const imports = {
const ret = Promise.resolve(getObject(arg0));
return addHeapObject(ret);
},
__wbindgen_json_serialize: function (arg0, arg1) {
const obj = getObject(arg1);
const ret = JSON.stringify(obj === undefined ? null : obj);
const ptr0 = passStringToWasm0(
ret,
wasm.__wbindgen_malloc,
wasm.__wbindgen_realloc,
);
const len0 = WASM_VECTOR_LEN;
getInt32Memory0()[arg0 / 4 + 1] = len0;
getInt32Memory0()[arg0 / 4 + 0] = ptr0;
},
__wbg_newwithlength_e833b89f9db02732: function (arg0) {
const ret = new Uint8Array(arg0 >>> 0);
return addHeapObject(ret);
Expand Down Expand Up @@ -378,6 +358,18 @@ const imports = {
const ret = getObject(arg0).length;
return ret;
},
__wbindgen_memory: function () {
const ret = wasm.memory;
return addHeapObject(ret);
},
__wbg_buffer_de1150f91b23aa89: function (arg0) {
const ret = getObject(arg0).buffer;
return addHeapObject(ret);
},
__wbg_new_97cf52648830a70d: function (arg0) {
const ret = new Uint8Array(getObject(arg0));
return addHeapObject(ret);
},
__wbg_set_a0172b213e2469e9: function (arg0, arg1, arg2) {
getObject(arg0).set(getObject(arg1), arg2 >>> 0);
},
Expand All @@ -398,7 +390,7 @@ const imports = {
const a = state0.a;
state0.a = 0;
try {
return __wbg_adapter_50(a, state0.b, arg0, arg1);
return __wbg_adapter_52(a, state0.b, arg0, arg1);
} finally {
state0.a = a;
}
Expand All @@ -409,6 +401,18 @@ const imports = {
state0.a = state0.b = 0;
}
},
__wbg_new_651776e932b7e9c7: function (arg0, arg1) {
const ret = new Error(getStringFromWasm0(arg0, arg1));
return addHeapObject(ret);
},
__wbg_newwithbyteoffsetandlength_9ca61320599a2c84: function (
arg0,
arg1,
arg2,
) {
const ret = new Uint8Array(getObject(arg0), arg1 >>> 0, arg2 >>> 0);
return addHeapObject(ret);
},
__wbg_new_693216e109162396: function () {
const ret = new Error();
return addHeapObject(ret);
Expand Down Expand Up @@ -457,8 +461,8 @@ const imports = {
const ret = getObject(arg0).then(getObject(arg1), getObject(arg2));
return addHeapObject(ret);
},
__wbindgen_closure_wrapper9485: function (arg0, arg1, arg2) {
const ret = makeMutClosure(arg0, arg1, 143, __wbg_adapter_18);
__wbindgen_closure_wrapper9469: function (arg0, arg1, arg2) {
const ret = makeMutClosure(arg0, arg1, 143, __wbg_adapter_20);
return addHeapObject(ret);
},
},
Expand Down
Binary file modified lib/eszip_wasm_bg.wasm
Binary file not shown.
Loading

0 comments on commit 17affaa

Please sign in to comment.