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
4 changes: 3 additions & 1 deletion packages/bench/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ add_compile_definitions(
"NODE_ADDON_API_ENABLE_MAYBE"
)
add_link_options(
"-sMIN_CHROME_VERSION=84"
"-sMIN_CHROME_VERSION=85"
"-sALLOW_MEMORY_GROWTH=1"
"-sMODULARIZE=1"
)
Expand All @@ -38,11 +38,13 @@ target_link_libraries(emnapic PRIVATE emnapi-basic fib)
target_link_options(emnapic PRIVATE
"-sEXPORTED_FUNCTIONS=['_napi_register_wasm_v1','_malloc','_free']"
"-sEXPORT_NAME=emnapic"
"-sEXPORTED_RUNTIME_METHODS=['emnapiInit']"
)

add_executable(emnapicpp "${CMAKE_CURRENT_SOURCE_DIR}/src/lib.cpp")
target_link_libraries(emnapicpp PRIVATE emnapi-basic fib)
target_link_options(emnapicpp PRIVATE
"-sEXPORTED_FUNCTIONS=['_napi_register_wasm_v1','_malloc','_free']"
"-sEXPORT_NAME=emnapicpp"
"-sEXPORTED_RUNTIME_METHODS=['emnapiInit']"
)
4 changes: 2 additions & 2 deletions packages/emnapi/common.gypi
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@
'-sNODEJS_CATCH_EXIT=0',
'-sNODEJS_CATCH_REJECTION=0',
'-sWASM_BIGINT=1',
'-sMIN_CHROME_VERSION=84',
'-sMIN_CHROME_VERSION=85',
'-sMIN_NODE_VERSION=161500',
'-sSTACK_SIZE=<(stack_size)',
'-sDEFAULT_PTHREAD_STACK_SIZE=<(stack_size)',
Expand All @@ -102,7 +102,7 @@
'-sNODEJS_CATCH_EXIT=0',
'-sNODEJS_CATCH_REJECTION=0',
'-sWASM_BIGINT=1',
'-sMIN_CHROME_VERSION=84',
'-sMIN_CHROME_VERSION=85',
'-sMIN_NODE_VERSION=161500',
'-sSTACK_SIZE=<(stack_size)',
'-sDEFAULT_PTHREAD_STACK_SIZE=<(stack_size)',
Expand Down
13 changes: 10 additions & 3 deletions packages/emnapi/src/function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export function napi_get_cb_info (env: napi_env, cbinfo: napi_callback_info, arg
$CHECK_ENV!(env)
const envObject = emnapiCtx.envStore.get(env)!
if (!cbinfo) return envObject.setLastError(napi_status.napi_invalid_arg)
const cbinfoValue = emnapiCtx.cbinfoStack.get(cbinfo)!
const cbinfoValue = emnapiCtx.scopeStore.get(cbinfo)!.callbackInfo

from64('argc')
from64('argv')
Expand Down Expand Up @@ -176,8 +176,15 @@ export function napi_get_new_target (

from64('result')

const cbinfoValue = emnapiCtx.cbinfoStack.get(cbinfo)!
const value = cbinfoValue.getNewTarget(envObject)
const cbinfoValue = emnapiCtx.scopeStore.get(cbinfo)!.callbackInfo
const { thiz, fn } = cbinfoValue

const value = thiz == null || thiz.constructor == null
? 0
: thiz instanceof fn
? envObject.ensureHandleId(thiz.constructor)
: 0

makeSetValue('result', 0, 'value', '*')
return envObject.clearLastError()
}
38 changes: 23 additions & 15 deletions packages/emnapi/src/internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,32 @@ export function emnapiCreateFunction<F extends (...args: any[]) => any> (envObje
const functionName = (!utf8name || !length) ? '' : (emnapiString.UTF8ToString(utf8name, length))

let f: F
const napiCallback = makeDynCall('ppp', 'cb')
const callback = (envObject: Env) => {
return napiCallback(envObject.id, envObject.ctx.scopeStore.currentScope.id)
}

const makeFunction = () => function (this: any): any {
'use strict'
const cbinfo = emnapiCtx.cbinfoStack.push(this, data, arguments, f)
const scope = emnapiCtx.openScope(envObject)
const makeFunction = (envObject: Env, callback: (env: Env) => any) => function (this: any, ...args: any[]): any {
const scope = envObject.ctx.openScope(envObject)
const callbackInfo = scope.callbackInfo
callbackInfo.data = data
callbackInfo.args = args
callbackInfo.thiz = this
callbackInfo.fn = f
try {
return envObject.callIntoModule((envObject) => {
const napiValue = makeDynCall('ppp', 'cb')(envObject.id, cbinfo)
return (!napiValue) ? undefined : emnapiCtx.handleStore.get(napiValue)!.value
})
const napiValue = envObject.callIntoModule(callback)
return (!napiValue) ? undefined : envObject.ctx.handleStore.get(napiValue)!.value
} finally {
emnapiCtx.cbinfoStack.pop()
emnapiCtx.closeScope(envObject, scope)
callbackInfo.data = 0
callbackInfo.args = undefined!
callbackInfo.thiz = undefined
callbackInfo.fn = undefined!
envObject.ctx.closeScope(envObject, scope)
}
}

if (functionName === '') {
f = makeFunction() as F
f = makeFunction(envObject, callback) as F
return { status: napi_status.napi_ok, f }
}

Expand All @@ -39,7 +47,7 @@ export function emnapiCreateFunction<F extends (...args: any[]) => any> (envObje

// #if DYNAMIC_EXECUTION
if (emnapiCtx.feature.supportNewFunction) {
const _ = makeFunction()
const _ = makeFunction(envObject, callback)
try {
f = (new Function('_',
'return function ' + functionName + '(){' +
Expand All @@ -48,15 +56,15 @@ export function emnapiCreateFunction<F extends (...args: any[]) => any> (envObje
'};'
))(_)
} catch (_err) {
f = makeFunction() as F
f = makeFunction(envObject, callback) as F
if (emnapiCtx.feature.canSetFunctionName) Object.defineProperty(f, 'name', { value: functionName })
}
} else {
f = makeFunction() as F
f = makeFunction(envObject, callback) as F
if (emnapiCtx.feature.canSetFunctionName) Object.defineProperty(f, 'name', { value: functionName })
}
// #else
f = makeFunction() as F
f = makeFunction(envObject, callback) as F
if (emnapiCtx.feature.canSetFunctionName) Object.defineProperty(f, 'name', { value: functionName })
// #endif
return { status: napi_status.napi_ok, f }
Expand Down
76 changes: 0 additions & 76 deletions packages/runtime/src/CallbackInfo.ts

This file was deleted.

2 changes: 0 additions & 2 deletions packages/runtime/src/Context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import {
NAPI_VERSION_EXPERIMENTAL,
NODE_API_DEFAULT_MODULE_API_VERSION
} from './util'
import { CallbackInfoStack } from './CallbackInfo'
import { NotSupportWeakRefError, NotSupportBufferError } from './errors'
import { Reference, ReferenceWithData, ReferenceWithFinalizer, type ReferenceOwnership } from './Reference'
import { type IDeferrdValue, Deferred } from './Deferred'
Expand Down Expand Up @@ -117,7 +116,6 @@ export class Context {
public refStore = new Store<Reference>()
public deferredStore = new Store<Deferred>()
public handleStore = new HandleStore()
public cbinfoStack = new CallbackInfoStack()
private readonly refCounter?: NodejsWaitingRequestCounter
private readonly cleanupQueue: CleanupQueue

Expand Down
17 changes: 16 additions & 1 deletion packages/runtime/src/HandleScope.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
import type { Handle, HandleStore } from './Handle'
import { External } from './External'

export interface ICallbackInfo {
thiz: any
data: void_p
args: ArrayLike<any>
fn: Function
}

export class HandleScope {
public handleStore: HandleStore
public id: number
public parent: HandleScope | null
public child: HandleScope | null
public start: number
public end: number
public _escapeCalled: boolean
private _escapeCalled: boolean
public callbackInfo: ICallbackInfo

public constructor (handleStore: HandleStore, id: number, parentScope: HandleScope | null, start: number, end = start) {
this.handleStore = handleStore
Expand All @@ -19,6 +27,12 @@ export class HandleScope {
this.start = start
this.end = end
this._escapeCalled = false
this.callbackInfo = {
thiz: undefined,
data: 0,
args: undefined!,
fn: undefined!
}
}

public add<V> (value: V): Handle<V> {
Expand All @@ -32,6 +46,7 @@ export class HandleScope {
}

public dispose (): void {
if (this._escapeCalled) this._escapeCalled = false
if (this.start === this.end) return
this.handleStore.erase(this.start, this.end)
}
Expand Down
32 changes: 8 additions & 24 deletions packages/runtime/src/ScopeStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,16 @@ import { HandleScope } from './HandleScope'
export class ScopeStore {
private readonly _rootScope: HandleScope
public currentScope: HandleScope
private readonly _values: [undefined, ...HandleScope[]]

constructor () {
this._rootScope = new HandleScope(null!, 0, null, 1, HandleStore.MIN_ID)
this.currentScope = this._rootScope
this._values = [undefined]
}

get (id: number): HandleScope | undefined {
id = Number(id)
let scope = this.currentScope
while (scope !== this._rootScope) {
if (scope.id === id) {
return scope
}
scope = scope.parent!
}
return undefined
return this._values[id]
}

openScope (envObject: Env): HandleScope {
Expand All @@ -29,9 +23,10 @@ export class ScopeStore {

if (scope !== null) {
scope.start = scope.end = currentScope.end
scope._escapeCalled = false
} else {
scope = new HandleScope(envObject.ctx.handleStore, currentScope.id + 1, currentScope, currentScope.end)
const id = currentScope.id + 1
scope = new HandleScope(envObject.ctx.handleStore, id, currentScope, currentScope.end)
this._values[id] = scope
}
this.currentScope = scope

Expand All @@ -48,18 +43,7 @@ export class ScopeStore {
}

dispose (): void {
let scope: HandleScope | null = this.currentScope
while (scope !== null) {
scope.handleStore = null!
scope.id = 0
scope.parent = null
scope.start = HandleStore.MIN_ID
scope.end = HandleStore.MIN_ID
scope._escapeCalled = false
const child: HandleScope | null = scope.child
scope.child = null
scope = child
}
this.currentScope = null!
this.currentScope = this._rootScope
this._values.length = 1
}
}
3 changes: 1 addition & 2 deletions packages/runtime/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
export { CallbackInfo, CallbackInfoStack } from './CallbackInfo'
export { createContext, getDefaultContext, Context, type CleanupHookCallbackFunction } from './Context'
export { Deferred, type IDeferrdValue } from './Deferred'
export { Env, NodeEnv, type IReferenceBinding } from './env'
Expand All @@ -7,7 +6,7 @@ export { External, isExternal, getExternalValue } from './External'
export { Finalizer } from './Finalizer'
export { TrackedFinalizer } from './TrackedFinalizer'
export { Handle, ConstHandle, HandleStore } from './Handle'
export { HandleScope } from './HandleScope'
export { HandleScope, type ICallbackInfo } from './HandleScope'
export { Persistent } from './Persistent'
export { Reference, ReferenceWithData, ReferenceWithFinalizer, ReferenceOwnership } from './Reference'
export { RefTracker } from './RefTracker'
Expand Down
2 changes: 1 addition & 1 deletion packages/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ if(IS_EMSCRIPTEN)
"-sNODEJS_CATCH_EXIT=0"
"-sWASM_BIGINT=1"
"-sALLOW_MEMORY_GROWTH=1"
"-sMIN_CHROME_VERSION=84"
"-sMIN_CHROME_VERSION=85"
"-sSTACK_SIZE=1048576"
"-sDEFAULT_PTHREAD_STACK_SIZE=1048576"
"-sINITIAL_MEMORY=16777216"
Expand Down