Skip to content

Commit af1a9cf

Browse files
authored
[Backport] build: backport emscripten parse tools changes to v1 (#165)
1 parent c3768ac commit af1a9cf

4 files changed

Lines changed: 40 additions & 14 deletions

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
/lib
2+
/dist

packages/ts-transform-emscripten-parse-tools/package.json

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,22 @@
33
"version": "1.4.5",
44
"private": true,
55
"description": "",
6-
"type": "commonjs",
7-
"main": "./lib/index.js",
8-
"types": "./lib/index.d.ts",
6+
"type": "module",
7+
"main": "./dist/index.js",
8+
"types": "./dist/index.d.ts",
9+
"exports": {
10+
".": {
11+
"types": {
12+
"module-sync": "./dist/index.d.ts",
13+
"import": "./dist/index.d.ts",
14+
"require": "./dist/index.d.ts",
15+
"default": "./dist/index.d.ts"
16+
},
17+
"module-sync": "./dist/index.js",
18+
"default": "./dist/index.js"
19+
},
20+
"./package.json": "./package.json"
21+
},
922
"scripts": {
1023
"build": "tsc"
1124
},

packages/ts-transform-emscripten-parse-tools/src/index.ts

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,17 @@ import type {
1717
VariableStatement,
1818
NodeArray,
1919
VisitResult,
20-
Statement
20+
Statement,
21+
Identifier
2122
} from 'typescript'
2223
import { cloneNode, type CloneNodeOptions } from 'ts-clone-node'
2324

24-
import ts = require('typescript')
25+
import * as TS from 'typescript'
2526
import { join, resolve } from 'path'
2627
import { getDefaultBaseOptions, type BaseTransformOptions } from '@emnapi/ts-transform-emscripten-esm-library'
2728

29+
const ts = ('default' in TS ? TS.default : TS) as typeof TS
30+
2831
export type CStyleBoolean = 0 | 1
2932

3033
export interface Defines {
@@ -33,6 +36,7 @@ export interface Defines {
3336

3437
export interface TransformOptions extends BaseTransformOptions {
3538
defines?: Defines
39+
plugin?: boolean
3640
}
3741

3842
// function isEmscriptenMacro (text: string): boolean {
@@ -83,7 +87,7 @@ function getDataViewGetMethod (defines: Record<string, any>, type: Type): string
8387
case 'u64': return 'getBigUint64'
8488
case 'float': return 'getFloat32'
8589
case 'double': return 'getFloat64'
86-
case '*': return defines.MEMORY64 ? 'getBigInt64' : 'getInt32'
90+
case '*': return defines.MEMORY64 ? 'getBigUint64' : 'getUint32'
8791
default: throw new Error(`unknown data type: ${type as string}`)
8892
}
8993
}
@@ -100,7 +104,7 @@ function getDataViewSetMethod (defines: Record<string, any>, type: Type): string
100104
case 'u64': return 'setBigUint64'
101105
case 'float': return 'setFloat32'
102106
case 'double': return 'setFloat64'
103-
case '*': return defines.MEMORY64 ? 'setBigInt64' : 'setInt32'
107+
case '*': return defines.MEMORY64 ? 'setBigUint64' : 'setUint32'
104108
default: throw new Error(`unknown data type: ${type as string}`)
105109
}
106110
}
@@ -243,11 +247,13 @@ class Transform {
243247

244248
readonly runtimeModuleSpecifier: string
245249
readonly parseToolsModuleSpecifier: string
250+
readonly plugin: boolean
246251

247252
constructor (public program: Program, context: TransformationContext, config?: TransformOptions) {
248253
const { runtimeModuleSpecifier, parseToolsModuleSpecifier } = getDefaultBaseOptions(config)
249254
this.runtimeModuleSpecifier = runtimeModuleSpecifier
250255
this.parseToolsModuleSpecifier = parseToolsModuleSpecifier
256+
this.plugin = config?.plugin ?? false
251257

252258
this.ctx = context
253259
this.defines = config?.defines ?? {}
@@ -262,6 +268,12 @@ class Transform {
262268
this.insertWasmTableImport = false
263269
}
264270

271+
createLazyEmscriptenRuntimeSymbol (name: string): CallExpression | Identifier {
272+
return this.plugin
273+
? this.ctx.factory.createCallExpression(this.ctx.factory.createIdentifier(name), undefined, [])
274+
: this.ctx.factory.createIdentifier(name)
275+
}
276+
265277
createHeapDataViewDeclaration (): VariableStatement {
266278
return this.ctx.factory.createVariableStatement(
267279
undefined,
@@ -274,7 +286,7 @@ class Transform {
274286
this.ctx.factory.createIdentifier('DataView'),
275287
undefined,
276288
[this.ctx.factory.createPropertyAccessExpression(
277-
this.ctx.factory.createIdentifier('wasmMemory'),
289+
this.createLazyEmscriptenRuntimeSymbol('wasmMemory'),
278290
this.ctx.factory.createIdentifier('buffer')
279291
)]
280292
)
@@ -663,7 +675,7 @@ class Transform {
663675

664676
return this.ctx.factory.createParenthesizedExpression(this.ctx.factory.createCallExpression(
665677
this.ctx.factory.createPropertyAccessExpression(
666-
this.ctx.factory.createIdentifier('wasmTable'),
678+
this.createLazyEmscriptenRuntimeSymbol('wasmTable'),
667679
this.ctx.factory.createIdentifier('get')
668680
),
669681
undefined,
@@ -817,7 +829,7 @@ function createTransformerFactory (program: Program, config: TransformOptions):
817829

818830
injectedSrc = factory.updateSourceFile(injectedSrc, newStatements)
819831

820-
const doNotInsertImport = join(__dirname, '../../emnapi/src/core/init.ts')
832+
const doNotInsertImport = join(import.meta.dirname, '../../emnapi/src/core/init.ts')
821833

822834
if (process.platform === 'win32') {
823835
const resolvedFileName = resolve(src.fileName)
@@ -832,7 +844,7 @@ function createTransformerFactory (program: Program, config: TransformOptions):
832844

833845
let resultSrc = injectedSrc
834846
let importNames: string[] | null = null
835-
if (transform.insertWasmMemoryImport) {
847+
if (transform.insertWasmMemoryImport && !config.plugin) {
836848
importNames = getImportsOfModule(resultSrc)
837849
if (!importNames.includes('wasmMemory')) {
838850
resultSrc = factory.updateSourceFile(resultSrc, [
@@ -849,7 +861,7 @@ function createTransformerFactory (program: Program, config: TransformOptions):
849861
])
850862
}
851863
}
852-
if (transform.insertWasmTableImport) {
864+
if (transform.insertWasmTableImport && !config.plugin) {
853865
importNames = getImportsOfModule(resultSrc)
854866
if (!importNames.includes('wasmTable')) {
855867
resultSrc = factory.updateSourceFile(resultSrc, [

packages/ts-transform-emscripten-parse-tools/tsconfig.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
{
22
"extends": "../shared/tsconfig.base.json",
33
"compilerOptions": {
4-
"target": "ES2019",
4+
"target": "ES2021",
55
"module": "NodeNext",
66
"moduleResolution": "NodeNext",
77
"strict": true,
88
"declaration": true,
99
"sourceMap": true,
1010
"declarationMap": true,
11-
"outDir": "lib"
11+
"outDir": "dist"
1212
},
1313
"include": [
1414
"./src/**/*",

0 commit comments

Comments
 (0)