@ohos-ports/oxc-parser-binding-wasm32-wasi 0.149.0-beta.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,296 @@
1
+ export interface Comment {
2
+ type: 'Line' | 'Block'
3
+ value: string
4
+ start: number
5
+ end: number
6
+ }
7
+
8
+ export interface ErrorLabel {
9
+ message: string | null
10
+ start: number
11
+ end: number
12
+ }
13
+
14
+ export interface OxcError {
15
+ severity: Severity
16
+ message: string
17
+ labels: Array<ErrorLabel>
18
+ helpMessage: string | null
19
+ codeframe: string | null
20
+ }
21
+
22
+ export declare const enum Severity {
23
+ Error = 'Error',
24
+ Warning = 'Warning',
25
+ Advice = 'Advice'
26
+ }
27
+ export declare class ParseResult {
28
+ get program(): import("@oxc-project/types").Program
29
+ get module(): EcmaScriptModule
30
+ get comments(): Array<Comment>
31
+ get errors(): Array<OxcError>
32
+ }
33
+
34
+ export interface DynamicImport {
35
+ start: number
36
+ end: number
37
+ moduleRequest: Span
38
+ }
39
+
40
+ export interface EcmaScriptModule {
41
+ /**
42
+ * Has ESM syntax.
43
+ *
44
+ * i.e. `import` and `export` statements, and `import.meta`.
45
+ *
46
+ * Dynamic imports `import('foo')` are ignored since they can be used in non-ESM files.
47
+ */
48
+ hasModuleSyntax: boolean
49
+ /** Import statements. */
50
+ staticImports: Array<StaticImport>
51
+ /** Export statements. */
52
+ staticExports: Array<StaticExport>
53
+ /** Dynamic import expressions. */
54
+ dynamicImports: Array<DynamicImport>
55
+ /** Span positions` of `import.meta` */
56
+ importMetas: Array<Span>
57
+ }
58
+
59
+ export interface ExportExportName {
60
+ kind: ExportExportNameKind
61
+ name: string | null
62
+ start: number | null
63
+ end: number | null
64
+ }
65
+
66
+ export declare const enum ExportExportNameKind {
67
+ /** `export { name } */
68
+ Name = 'Name',
69
+ /** `export default expression` */
70
+ Default = 'Default',
71
+ /** `export * from "mod" */
72
+ None = 'None'
73
+ }
74
+
75
+ export interface ExportImportName {
76
+ kind: ExportImportNameKind
77
+ name: string | null
78
+ start: number | null
79
+ end: number | null
80
+ }
81
+
82
+ export declare const enum ExportImportNameKind {
83
+ /** `export { name } */
84
+ Name = 'Name',
85
+ /** `export * as ns from "mod"` */
86
+ All = 'All',
87
+ /** `export * from "mod"` */
88
+ AllButDefault = 'AllButDefault',
89
+ /** Does not have a specifier. */
90
+ None = 'None'
91
+ }
92
+
93
+ export interface ExportLocalName {
94
+ kind: ExportLocalNameKind
95
+ name: string | null
96
+ start: number | null
97
+ end: number | null
98
+ }
99
+
100
+ export declare const enum ExportLocalNameKind {
101
+ /** `export { name } */
102
+ Name = 'Name',
103
+ /** `export default expression` */
104
+ Default = 'Default',
105
+ /**
106
+ * If the exported value is not locally accessible from within the module.
107
+ * `export default function () {}`
108
+ */
109
+ None = 'None'
110
+ }
111
+
112
+ export interface ImportName {
113
+ kind: ImportNameKind
114
+ name: string | null
115
+ start: number | null
116
+ end: number | null
117
+ }
118
+
119
+ export declare const enum ImportNameKind {
120
+ /** `import { x } from "mod"` */
121
+ Name = 'Name',
122
+ /** `import * as ns from "mod"` */
123
+ NamespaceObject = 'NamespaceObject',
124
+ /** `import defaultExport from "mod"` */
125
+ Default = 'Default'
126
+ }
127
+
128
+ /**
129
+ * Parse JS/TS source asynchronously on a separate thread.
130
+ *
131
+ * Note that not all of the workload can happen on a separate thread.
132
+ * Parsing on Rust side does happen in a separate thread, but deserialization of the AST to JS objects
133
+ * has to happen on current thread. This synchronous deserialization work typically outweighs
134
+ * the asynchronous parsing by a factor of between 3 and 20.
135
+ *
136
+ * i.e. the majority of the workload cannot be parallelized by using this method.
137
+ *
138
+ * Generally `parseSync` is preferable to use as it does not have the overhead of spawning a thread.
139
+ * If you need to parallelize parsing multiple files, it is recommended to use worker threads.
140
+ */
141
+ export declare function parse(filename: string, sourceText: string, options?: ParserOptions | undefined | null): Promise<ParseResult>
142
+
143
+ export interface ParserOptions {
144
+ /** Treat the source text as `js`, `jsx`, `ts`, `tsx` or `dts`. */
145
+ lang?: 'js' | 'jsx' | 'ts' | 'tsx' | 'dts'
146
+ /** Treat the source text as `script` or `module` code. */
147
+ sourceType?: 'script' | 'module' | 'commonjs' | 'unambiguous' | undefined
148
+ /**
149
+ * Return an AST which includes TypeScript-related properties, or excludes them.
150
+ *
151
+ * `'js'` is default for JS / JSX files.
152
+ * `'ts'` is default for TS / TSX files.
153
+ * The type of the file is determined from `lang` option, or extension of provided `filename`.
154
+ */
155
+ astType?: 'js' | 'ts'
156
+ /**
157
+ * Controls whether the `range` property is included on AST nodes.
158
+ * The `range` property is a `[number, number]` which indicates the start/end offsets
159
+ * of the node in the file contents.
160
+ *
161
+ * @default false
162
+ */
163
+ range?: boolean
164
+ /**
165
+ * Emit `ParenthesizedExpression` and `TSParenthesizedType` in AST.
166
+ *
167
+ * If this option is true, parenthesized expressions are represented by
168
+ * (non-standard) `ParenthesizedExpression` and `TSParenthesizedType` nodes that
169
+ * have a single `expression` property containing the expression inside parentheses.
170
+ *
171
+ * @default true
172
+ */
173
+ preserveParens?: boolean
174
+ /**
175
+ * Produce semantic errors with an additional AST pass.
176
+ * Semantic errors depend on symbols and scopes, where the parser does not construct.
177
+ * This adds a small performance overhead.
178
+ *
179
+ * @default false
180
+ */
181
+ showSemanticErrors?: boolean
182
+ }
183
+
184
+ /**
185
+ * Parse JS/TS source synchronously on current thread.
186
+ *
187
+ * This is generally preferable over `parse` (async) as it does not have the overhead
188
+ * of spawning a thread, and the majority of the workload cannot be parallelized anyway
189
+ * (see `parse` documentation for details).
190
+ *
191
+ * If you need to parallelize parsing multiple files, it is recommended to use worker threads
192
+ * with `parseSync` rather than using `parse`.
193
+ */
194
+ export declare function parseSync(filename: string, sourceText: string, options?: ParserOptions | undefined | null): ParseResult
195
+
196
+ /** Returns `true` if raw transfer is supported on this platform. */
197
+ export declare function rawTransferSupported(): boolean
198
+
199
+ export interface Span {
200
+ start: number
201
+ end: number
202
+ }
203
+
204
+ export interface StaticExport {
205
+ start: number
206
+ end: number
207
+ entries: Array<StaticExportEntry>
208
+ }
209
+
210
+ export interface StaticExportEntry {
211
+ start: number
212
+ end: number
213
+ moduleRequest: ValueSpan | null
214
+ /** The name under which the desired binding is exported by the module`. */
215
+ importName: ExportImportName
216
+ /** The name used to export this binding by this module. */
217
+ exportName: ExportExportName
218
+ /** The name that is used to locally access the exported value from within the importing module. */
219
+ localName: ExportLocalName
220
+ /**
221
+ * Whether the export is a TypeScript `export type`.
222
+ *
223
+ * Examples:
224
+ *
225
+ * ```ts
226
+ * export type * from 'mod';
227
+ * export type * as ns from 'mod';
228
+ * export type { foo };
229
+ * export { type foo }:
230
+ * export type { foo } from 'mod';
231
+ * ```
232
+ */
233
+ isType: boolean
234
+ }
235
+
236
+ export interface StaticImport {
237
+ /** Start of import statement. */
238
+ start: number
239
+ /** End of import statement. */
240
+ end: number
241
+ /**
242
+ * Import source.
243
+ *
244
+ * ```js
245
+ * import { foo } from "mod";
246
+ * // ^^^
247
+ * ```
248
+ */
249
+ moduleRequest: ValueSpan
250
+ /**
251
+ * Import specifiers.
252
+ *
253
+ * Empty for `import "mod"`.
254
+ */
255
+ entries: Array<StaticImportEntry>
256
+ }
257
+
258
+ export interface StaticImportEntry {
259
+ /**
260
+ * The name under which the desired binding is exported by the module.
261
+ *
262
+ * ```js
263
+ * import { foo } from "mod";
264
+ * // ^^^
265
+ * import { foo as bar } from "mod";
266
+ * // ^^^
267
+ * ```
268
+ */
269
+ importName: ImportName
270
+ /**
271
+ * The name that is used to locally access the imported value from within the importing module.
272
+ * ```js
273
+ * import { foo } from "mod";
274
+ * // ^^^
275
+ * import { foo as bar } from "mod";
276
+ * // ^^^
277
+ * ```
278
+ */
279
+ localName: ValueSpan
280
+ /**
281
+ * Whether this binding is for a TypeScript type-only import.
282
+ *
283
+ * `true` for the following imports:
284
+ * ```ts
285
+ * import type { foo } from "mod";
286
+ * import { type foo } from "mod";
287
+ * ```
288
+ */
289
+ isType: boolean
290
+ }
291
+
292
+ export interface ValueSpan {
293
+ value: string
294
+ start: number
295
+ end: number
296
+ }
Binary file
@@ -0,0 +1,45 @@
1
+ import {
2
+ instantiateNapiModuleSync,
3
+ MessageHandler,
4
+ WASI,
5
+ emnapiAsyncWorkPlugin,
6
+ emnapiTSFNPlugin,
7
+ } from '@napi-rs/wasm-runtime'
8
+
9
+ const handler = new MessageHandler({
10
+ onLoad({ wasmModule, wasmMemory }) {
11
+ const wasi = new WASI({
12
+ print: function () {
13
+ // eslint-disable-next-line no-console
14
+ console.log.apply(console, arguments)
15
+ },
16
+ printErr: function() {
17
+ // eslint-disable-next-line no-console
18
+ console.error.apply(console, arguments)
19
+
20
+ },
21
+ })
22
+ return instantiateNapiModuleSync(wasmModule, {
23
+ childThread: true,
24
+ wasi,
25
+ // The wasm links a "basic" emnapi archive (no C async-work /
26
+ // threadsafe-function implementations), so every thread that
27
+ // instantiates it must provide the JavaScript implementations
28
+ // through the emnapi plugins.
29
+ plugins: [emnapiAsyncWorkPlugin, emnapiTSFNPlugin],
30
+ overwriteImports(importObject) {
31
+ importObject.env = {
32
+ ...importObject.env,
33
+ ...importObject.napi,
34
+ ...importObject.emnapi,
35
+ memory: wasmMemory,
36
+ }
37
+ },
38
+ })
39
+ },
40
+
41
+ })
42
+
43
+ globalThis.onmessage = function (e) {
44
+ handler.handle(e)
45
+ }
@@ -0,0 +1,81 @@
1
+ import fs from "node:fs";
2
+ import { createRequire } from "node:module";
3
+ import { parse } from "node:path";
4
+ import { parentPort, Worker } from "node:worker_threads";
5
+
6
+ const require = createRequire(import.meta.url);
7
+
8
+ const {
9
+ // HarmonyOS adaptation: use the JS WASI from @napi-rs/wasm-runtime instead
10
+ // of node:wasi (node:wasi's uvwasi_init is broken on HarmonyOS, always
11
+ // returning UVWASI_EBADF).
12
+ WASI,
13
+ instantiateNapiModuleSync,
14
+ MessageHandler,
15
+ getDefaultContext,
16
+ emnapiAsyncWorkPlugin,
17
+ emnapiTSFNPlugin,
18
+ } = require("@napi-rs/wasm-runtime");
19
+
20
+ if (parentPort) {
21
+ parentPort.on("message", (data) => {
22
+ globalThis.onmessage({ data });
23
+ });
24
+ }
25
+
26
+ Object.assign(globalThis, {
27
+ self: globalThis,
28
+ require,
29
+ Worker,
30
+ importScripts: function (f) {
31
+ ;(0, eval)(fs.readFileSync(f, "utf8") + "//# sourceURL=" + f);
32
+ },
33
+ postMessage: function (msg) {
34
+ if (parentPort) {
35
+ parentPort.postMessage(msg);
36
+ }
37
+ },
38
+ });
39
+
40
+ const emnapiContext = getDefaultContext();
41
+
42
+ const __rootDir = parse(process.cwd()).root;
43
+
44
+ const handler = new MessageHandler({
45
+ onLoad({ wasmModule, wasmMemory }) {
46
+ const wasi = new WASI({
47
+ version: 'preview1',
48
+ env: process.env,
49
+ // HarmonyOS adaptation: JS WASI from @napi-rs/wasm-runtime requires an
50
+ // explicit `fs`; preopen cwd as a read-write root (node:wasi is broken
51
+ // on HarmonyOS with UVWASI_EBADF).
52
+ fs,
53
+ preopens: {
54
+ [process.cwd()]: process.cwd(),
55
+ },
56
+ });
57
+
58
+ return instantiateNapiModuleSync(wasmModule, {
59
+ childThread: true,
60
+ wasi,
61
+ context: emnapiContext,
62
+ // The wasm links a "basic" emnapi archive (no C async-work /
63
+ // threadsafe-function implementations), so every thread that
64
+ // instantiates it must provide the JavaScript implementations
65
+ // through the emnapi plugins.
66
+ plugins: [emnapiAsyncWorkPlugin, emnapiTSFNPlugin],
67
+ overwriteImports(importObject) {
68
+ importObject.env = {
69
+ ...importObject.env,
70
+ ...importObject.napi,
71
+ ...importObject.emnapi,
72
+ memory: wasmMemory
73
+ };
74
+ },
75
+ });
76
+ },
77
+ });
78
+
79
+ globalThis.onmessage = function (e) {
80
+ handler.handle(e);
81
+ };