Skip to content
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
43 changes: 23 additions & 20 deletions napi/parser/wrap.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,7 @@ export function wrap(result) {
return {
get program() {
if (!program) {
// Note: This code is repeated in `wasm/parser/update-bindings.mjs` and `crates/oxc-wasm/update-bindings.mjs`.
// Any changes should be applied in those 2 scripts too.
program = JSON.parse(result.program, function(key, value) {
// Set `value` field of `Literal`s for `BigInt`s and `RegExp`s.
// This is not possible to do on Rust side, as neither can be represented correctly in JSON.
if (value === null && key === 'value' && Object.hasOwn(this, 'type') && this.type === 'Literal') {
if (Object.hasOwn(this, 'bigint')) {
return BigInt(this.bigint);
}
if (Object.hasOwn(this, 'regex')) {
const { regex } = this;
try {
return RegExp(regex.pattern, regex.flags);
} catch (_err) {
// Invalid regexp, or valid regexp using syntax not supported by this version of NodeJS
}
}
}
return value;
});
program = jsonParseAst(result.program);
}
return program;
},
Expand All @@ -40,3 +21,25 @@ export function wrap(result) {
},
};
}

// Used by napi/playground/patch.mjs
export function jsonParseAst(ast) {
return JSON.parse(ast, function(key, value) {
// Set `value` field of `Literal`s for `BigInt`s and `RegExp`s.
// This is not possible to do on Rust side, as neither can be represented correctly in JSON.
if (value === null && key === 'value' && Object.hasOwn(this, 'type') && this.type === 'Literal') {
if (Object.hasOwn(this, 'bigint')) {
return BigInt(this.bigint);
}
if (Object.hasOwn(this, 'regex')) {
const { regex } = this;
try {
return RegExp(regex.pattern, regex.flags);
} catch (_err) {
// Invalid regexp, or valid regexp using syntax not supported by this version of NodeJS
}
}
}
return value;
});
}
1 change: 1 addition & 0 deletions napi/playground/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* auto-generated by NAPI-RS */
/* eslint-disable */
export declare class Oxc {
ast: object
astJson: string
ir: string
controlFlowGraph: string
Expand Down
20 changes: 20 additions & 0 deletions napi/playground/patch.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,25 @@ let data = fs.readFileSync(filename, 'utf-8');
data = data.replace(
'__emnapiInstantiateNapiModuleSync(__wasmFile',
'await (await import("@napi-rs/wasm-runtime")).instantiateNapiModule(__wasmFile',
).replace(
`export const Oxc = __napiModule.exports.Oxc`,
`
import { jsonParseAst } from "../parser/wrap.mjs"

export function Oxc() {
const oxc = new __napiModule.exports.Oxc();
return new Proxy(oxc, {
get(_target, p, _receiver) {
if (p === 'ast') {
return jsonParseAst(oxc.astJson);
}
if (typeof oxc[p] === 'function') {
return oxc[p].bind(oxc);
}
return Reflect.get(...arguments);
}
})
}
`,
);
fs.writeFileSync(filename, data);
19 changes: 18 additions & 1 deletion napi/playground/playground.wasi-browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,22 @@ const {
}
},
})
export const Oxc = __napiModule.exports.Oxc

import { jsonParseAst } from "../parser/wrap.mjs"

export function Oxc() {
const oxc = new __napiModule.exports.Oxc();
return new Proxy(oxc, {
get(_target, p, _receiver) {
if (p === 'ast') {
return jsonParseAst(oxc.astJson);
}
if (typeof oxc[p] === 'function') {
return oxc[p].bind(oxc);
}
return Reflect.get(...arguments);
}
})
}

export const Severity = __napiModule.exports.Severity
2 changes: 2 additions & 0 deletions napi/playground/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ mod options;
#[derive(Default)]
#[napi]
pub struct Oxc {
#[napi(ts_type = "object")]
pub ast: (),
pub ast_json: String,
pub ir: String,
pub control_flow_graph: String,
Expand Down
Loading