Skip to content

Commit

Permalink
transform emnapiDefineVar
Browse files Browse the repository at this point in the history
  • Loading branch information
toyobayashi committed Apr 4, 2023
1 parent f5fad5f commit 99cac98
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 5 deletions.
8 changes: 3 additions & 5 deletions packages/emnapi/src/core/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,8 @@ function emnapiImplementHelper (_name: string, _sig: string | undefined, compile
}
}

function emnapiDefineVar (name: string, value: any, deps?: string[], postset?: string): void {
if (typeof value === 'function' && postset) {
value()
} else if (typeof value === 'object' && value !== null && postset) {
value.init()
function emnapiDefineVar (_name: string, _value: any, _deps?: string[], postset?: string): void {
if (typeof postset === 'function') {
(postset as () => void)()
}
}
28 changes: 28 additions & 0 deletions packages/emnapi/transformer/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,34 @@ class Transform {
this.ctx.factory.createNodeArray(arr, node.arguments.hasTrailingComma)
)
}
if (functionName === 'emnapiDefineVar') {
const arr = node.arguments.slice()
if (arr.length > 2) {
arr[2] = this.ctx.factory.createIdentifier('undefined')
}
if (ts.isStringLiteral(arr[3])) {
const factory = this.ctx.factory
const source = ts.createSourceFile('/emnapiDefineVar_' + (arr[0] as StringLiteral).text, (arr[3] as StringLiteral).text, ts.ScriptTarget.ES5, true, ts.ScriptKind.JS)
arr[3] = factory.createFunctionExpression(
undefined,
undefined,
undefined,
undefined,
[],
undefined,
factory.createBlock(
[...source.statements],
true
)
)
}
return this.ctx.factory.updateCallExpression(
node,
node.expression,
node.typeArguments,
this.ctx.factory.createNodeArray(arr, node.arguments.hasTrailingComma)
)
}
return ts.visitEachChild(node, this.visitor, this.ctx)
}
if (ts.isIdentifier(node) && node.text === '$POINTER_SIZE') {
Expand Down

0 comments on commit 99cac98

Please sign in to comment.