Skip to content

Commit

Permalink
fix: show better error for parse errors
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Jan 16, 2024
1 parent 4d396b2 commit 3273a03
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/plugin/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const unplugin = createUnplugin<UnwasmPluginOptions>((opts) => {
if (_parseCache[name]) {
return _parseCache[name];
}
const parsed = parseWasm(source);
const parsed = parseWasm(source, { name });
const imports: Record<string, string[]> = Object.create(null);
const exports: string[] = [];
for (const mod of parsed.modules) {
Expand Down
17 changes: 15 additions & 2 deletions src/tools/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,21 @@ export type ParseResult = {
modules: ParsedWasmModule[];
};

export function parseWasm(source: Buffer | ArrayBuffer): ParseResult {
const ast = _parseWasm(source) as any;
export function parseWasm(
source: Buffer | ArrayBuffer,
opts: { name?: string } = {},
): ParseResult {
let ast: any;
try {
ast = _parseWasm(source);
} catch (error) {
throw new Error(
`[unwasm] Failed to parse ${opts.name || "wasm module"}: ${error}`,
{
cause: error,
},
);
}

const modules = [];

Expand Down

0 comments on commit 3273a03

Please sign in to comment.