@@ -17,14 +17,17 @@ import type {
1717 VariableStatement ,
1818 NodeArray ,
1919 VisitResult ,
20- Statement
20+ Statement ,
21+ Identifier
2122} from 'typescript'
2223import { cloneNode , type CloneNodeOptions } from 'ts-clone-node'
2324
24- import ts = require ( 'typescript' )
25+ import * as TS from 'typescript'
2526import { join , resolve } from 'path'
2627import { getDefaultBaseOptions , type BaseTransformOptions } from '@emnapi/ts-transform-emscripten-esm-library'
2728
29+ const ts = ( 'default' in TS ? TS . default : TS ) as typeof TS
30+
2831export type CStyleBoolean = 0 | 1
2932
3033export interface Defines {
@@ -33,6 +36,7 @@ export interface Defines {
3336
3437export 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 , [
0 commit comments