Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow symbol as reference target #43

Merged
merged 2 commits into from
Mar 29, 2023
Merged
Changes from 1 commit
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
Next Next commit
allow symbol as reference target
toyobayashi committed Mar 29, 2023

Verified

This commit was signed with the committer’s verified signature.
adidahiya Adi Dahiya
commit 2c254fa7f299a73d52df33b8ca3a055c126da140
2 changes: 2 additions & 0 deletions packages/emnapi/src/core/string.ts
Original file line number Diff line number Diff line change
@@ -195,6 +195,7 @@ function emnapiUtf8ToString (ptr: number, length: number): string {
return UTF8ToString(ptr)
}
length = length >>> 0
if (!length) return ''
const HEAPU8 = new Uint8Array(wasmMemory.buffer)
const shared = (typeof SharedArrayBuffer === 'function') && (wasmMemory.buffer instanceof SharedArrayBuffer)
return emnapiUtf8Decoder.decode(shared ? HEAPU8.slice(ptr, ptr + length) : HEAPU8.subarray(ptr, ptr + length))
@@ -225,6 +226,7 @@ function emnapiUtf16ToString (ptr: number, length: number): string {
return UTF16ToString(ptr)
}
length = length >>> 0
if (!length) return ''
const HEAPU8 = new Uint8Array(wasmMemory.buffer)
const shared = (typeof SharedArrayBuffer === 'function') && (wasmMemory.buffer instanceof SharedArrayBuffer)
return emnapiUtf16leDecoder.decode(shared ? HEAPU8.slice(ptr, ptr + length * 2) : HEAPU8.subarray(ptr, ptr + length * 2))
2 changes: 2 additions & 0 deletions packages/emnapi/src/emscripten/string.ts
Original file line number Diff line number Diff line change
@@ -90,6 +90,7 @@ mergeInto(LibraryManager.library, {
return UTF8ToString(ptr)
}
length = length >>> 0
if (!length) return ''
return emnapiUtf8Decoder.decode($getUnsharedTextDecoderView('HEAPU8', 'ptr', 'ptr + length'))
},

@@ -135,6 +136,7 @@ mergeInto(LibraryManager.library, {
return UTF16ToString(ptr)
}
length = length >>> 0
if (!length) return ''
return emnapiUtf16leDecoder.decode($getUnsharedTextDecoderView('HEAPU8', 'ptr', 'ptr + length * 2'))
}
})
4 changes: 2 additions & 2 deletions packages/emnapi/src/life.ts
Original file line number Diff line number Diff line change
@@ -76,8 +76,8 @@ function napi_create_reference (
$CHECK_ARG!(envObject, result)

const handle = emnapiCtx.handleStore.get(value)!
if (!(handle.isObject() || handle.isFunction())) {
return envObject.setLastError(napi_status.napi_object_expected)
if (!(handle.isObject() || handle.isFunction() || (emnapiCtx.feature.supportWeakSymbol && handle.isSymbol()))) {
return envObject.setLastError(napi_status.napi_invalid_arg)
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const ref = emnapiCtx.createReference(envObject, handle.id, initial_refcount >>> 0, Ownership.kUserland as any)
30 changes: 22 additions & 8 deletions packages/emnapi/src/value/convert2napi.ts
Original file line number Diff line number Diff line change
@@ -60,17 +60,21 @@ function napi_create_double (env: napi_env, value: double, result: Pointer<napi_
function napi_create_string_latin1 (env: napi_env, str: const_char_p, length: size_t, result: Pointer<napi_value>): napi_status {
$CHECK_ENV!(env)
const envObject = emnapiCtx.envStore.get(env)!
$CHECK_ARG!(envObject, result)
$from64('str')
$from64('length')
const autoLength = length === -1
length = length >>> 0
if (!((length === 0xffffffff) || (length <= 2147483647)) || (!str)) {
if (length !== 0) {
$CHECK_ARG!(envObject, str)
}
$CHECK_ARG!(envObject, result)
$from64('str')
if (!(autoLength || (length <= 2147483647))) {
return envObject.setLastError(napi_status.napi_invalid_arg)
}

let latin1String = ''
let len = 0
if (length === -1) {
if (autoLength) {
while (true) {
const ch = $makeGetValue('str', 0, 'u8') as number
if (!ch) break
@@ -96,11 +100,16 @@ function napi_create_string_latin1 (env: napi_env, str: const_char_p, length: si
function napi_create_string_utf16 (env: napi_env, str: const_char16_t_p, length: size_t, result: Pointer<napi_value>): napi_status {
$CHECK_ENV!(env)
const envObject = emnapiCtx.envStore.get(env)!
$from64('length')
const autoLength = length === -1
const sizelength = length >>> 0
if (length !== 0) {
$CHECK_ARG!(envObject, str)
}
$CHECK_ARG!(envObject, result)
$from64('str')
$from64('length')

if (((length < -1) || (length > 2147483647)) || (!str)) {
if (!(autoLength || (sizelength <= 2147483647))) {
return envObject.setLastError(napi_status.napi_invalid_arg)
}

@@ -115,11 +124,16 @@ function napi_create_string_utf16 (env: napi_env, str: const_char16_t_p, length:
function napi_create_string_utf8 (env: napi_env, str: const_char_p, length: size_t, result: Pointer<napi_value>): napi_status {
$CHECK_ENV!(env)
const envObject = emnapiCtx.envStore.get(env)!
$from64('length')
const autoLength = length === -1
const sizelength = length >>> 0
if (length !== 0) {
$CHECK_ARG!(envObject, str)
}
$CHECK_ARG!(envObject, result)
$from64('str')
$from64('length')

if (((length < -1) || (length > 2147483647)) || (!str)) {
if (!(autoLength || (sizelength <= 2147483647))) {
return envObject.setLastError(napi_status.napi_invalid_arg)
}
const utf8String = emnapiUtf8ToString(str, length)
8 changes: 7 additions & 1 deletion packages/emnapi/src/value/create.ts
Original file line number Diff line number Diff line change
@@ -428,7 +428,13 @@ function node_api_symbol_for (env: napi_env, utf8description: const_char_p, leng
$from64('utf8description')
$from64('result')

if (((length < -1) || (length > 2147483647)) || (!utf8description)) {
const autoLength = length === -1
const sizelength = length >>> 0
if (length !== 0) {
$CHECK_ARG!(envObject, utf8description)
}

if (!(autoLength || (sizelength <= 2147483647))) {
return envObject.setLastError(napi_status.napi_invalid_arg)
}

2 changes: 2 additions & 0 deletions packages/runtime/src/Context.ts
Original file line number Diff line number Diff line change
@@ -10,6 +10,7 @@ import {
_global,
supportReflect,
supportFinalizer,
supportWeakSymbol,
supportBigInt,
supportNewFunction,
canSetFunctionName,
@@ -115,6 +116,7 @@ export class Context {
public feature = {
supportReflect,
supportFinalizer,
supportWeakSymbol,
supportBigInt,
supportNewFunction,
canSetFunctionName,
80 changes: 49 additions & 31 deletions packages/runtime/src/Persistent.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,29 @@
import { supportFinalizer } from './util'

export class Persistent<T extends object> {
private _ref: T | WeakRef<T> | null
class StrongRef<T> {
private _value: T

constructor (value: T) {
this._value = value
}

deref (): T {
return this._value
}

dispose (): void {
this._value = undefined!
}
}

export class Persistent<T> {
private _ref: StrongRef<T> | WeakRef<T extends object ? T : never> | undefined
private _param: any
private _callback: ((param: any) => void) | undefined

private static readonly _registry = supportFinalizer
? new FinalizationRegistry((value: Persistent<any>) => {
value._ref = null
value._ref = undefined
const callback = value._callback
const param = value._param
value._callback = undefined
@@ -19,62 +35,64 @@ export class Persistent<T extends object> {
: undefined!

constructor (value: T) {
this._ref = value
this._ref = new StrongRef(value)
}

setWeak<P> (param: P, callback: (param: P) => void): void {
if (this._ref === null) return
if (!supportFinalizer) return
if (this._ref instanceof WeakRef) return
this._param = param
this._callback = callback
Persistent._registry.register(this._ref, this, this)
this._ref = new WeakRef<T>(this._ref)
if (!supportFinalizer || this._ref === undefined || this._ref instanceof WeakRef) return
const value = this._ref.deref()
try {
// try {
Persistent._registry.register(value as any, this, this)
const weakRef = new WeakRef<any>(value)
this._ref.dispose()
this._ref = weakRef
// } catch (_) {
// Persistent._registry.register(this._ref, this, this)
// this._ref = new WeakRef<any>(this._ref)
// }
this._param = param
this._callback = callback
} catch (_) {}
}

clearWeak (): void {
if (this._ref === null) return
if (!supportFinalizer) return
if (!supportFinalizer || this._ref === undefined) return
if (this._ref instanceof WeakRef) {
try {
Persistent._registry.unregister(this)
} catch (_) {}
this._param = undefined
this._callback = undefined
this._ref = this._ref.deref() as T
const value = this._ref.deref()
if (value === undefined) {
this._ref = value
} else {
this._ref = new StrongRef(value as T)
}
}
}

reset (other?: T | WeakRef<T>): void {
reset (): void {
if (supportFinalizer) {
try {
Persistent._registry.unregister(this)
} catch (_) {}
}
this._param = undefined
this._callback = undefined
if (other) {
this._ref = other
} else {
this._ref = null
if (this._ref instanceof StrongRef) {
this._ref.dispose()
}
this._ref = undefined
}

isEmpty (): boolean {
return this._ref === null
return this._ref === undefined
}

deref (): T | undefined {
if (!supportFinalizer) {
return (this._ref as T | null) ?? undefined
}

if (this._ref === null) return undefined

if (this._ref instanceof WeakRef) {
return this._ref.deref()
}

return this._ref
if (this._ref === undefined) return undefined
return this._ref.deref()
}
}
6 changes: 1 addition & 5 deletions packages/runtime/src/Reference.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type { IStoreValue } from './Store'
import { isReferenceType } from './util'
import type { Env } from './env'
import { RefBase } from './RefBase'
import { Persistent } from './Persistent'
@@ -22,12 +21,9 @@ export class Reference extends RefBase implements IStoreValue {
finalize_hint: void_p = 0
): Reference {
const handle = envObject.ctx.handleStore.get(handle_id)!
if (!isReferenceType(handle.value)) {
throw new TypeError('Invalid reference value')
}
const ref = new Reference(envObject, initialRefcount, ownership, finalize_callback, finalize_data, finalize_hint)
envObject.ctx.refStore.add(ref)
ref.persistent = new Persistent<object>(handle.value)
ref.persistent = new Persistent(handle.value)

if (initialRefcount === 0) {
ref._setWeak()
12 changes: 12 additions & 0 deletions packages/runtime/src/util.ts
Original file line number Diff line number Diff line change
@@ -70,6 +70,18 @@ export const canSetFunctionName = /*#__PURE__*/ (function () {

export const supportReflect = typeof Reflect === 'object'
export const supportFinalizer = (typeof FinalizationRegistry !== 'undefined') && (typeof WeakRef !== 'undefined')
export const supportWeakSymbol = /*#__PURE__*/ (function () {
try {
// eslint-disable-next-line symbol-description
const sym = Symbol()
// eslint-disable-next-line no-new
new WeakRef(sym as any)
new WeakMap().set(sym as any, undefined)
} catch (_) {
return false
}
return true
})()
export const supportBigInt = typeof BigInt !== 'undefined'

export function isReferenceType (v: any): v is object {
2 changes: 2 additions & 0 deletions packages/test/async/async.test.js
Original file line number Diff line number Diff line change
@@ -90,6 +90,8 @@ async function main () {
throw new Error('uncaught')
}))
})

process.exitCode = 0
}

module.exports = main()
53 changes: 31 additions & 22 deletions packages/test/ref/ref.test.js
Original file line number Diff line number Diff line change
@@ -5,6 +5,8 @@
const assert = require('assert')
const { load } = require('../util')
const { gcUntil } = require('../common')
const emnapi = require('../../runtime')
const context = emnapi.getDefaultContext()

const p = load('ref')
module.exports = p.then(test_reference => {
@@ -16,31 +18,38 @@ module.exports = p.then(test_reference => {
// Run each test function in sequence,
// with an async delay and GC call between each.
async function runTests () {
// emnapi can not create reference on a symbol
// https://github.com/tc39/proposal-symbols-as-weakmap-keys
const symbolAsWeakMapKeys = () => {
// https://github.com/tc39/proposal-symbols-as-weakmap-keys
console.log('test symbolAsWeakMapKeys');
(() => {
const symbol = test_reference.createSymbol('testSym')
test_reference.createReference(symbol, 0)
assert.strictEqual(test_reference.referenceValue, symbol)
})()
test_reference.deleteReference();

// (() => {
// const symbol = test_reference.createSymbol('testSym')
// test_reference.createReference(symbol, 0)
// assert.strictEqual(test_reference.referenceValue, symbol)
// })()
// test_reference.deleteReference();
(() => {
const symbol = test_reference.createSymbolFor('testSymFor')
test_reference.createReference(symbol, 0)
assert.strictEqual(test_reference.referenceValue, symbol)
assert.strictEqual(test_reference.referenceValue, Symbol.for('testSymFor'))
})()
test_reference.deleteReference();

// (() => {
// const symbol = test_reference.createSymbolFor('testSymFor')
// test_reference.createReference(symbol, 0)
// assert.strictEqual(test_reference.referenceValue, symbol)
// assert.strictEqual(test_reference.referenceValue, Symbol.for('testSymFor'))
// })()
// test_reference.deleteReference();
(() => {
const symbol = test_reference.createSymbolForEmptyString()
test_reference.createReference(symbol, 0)
assert.strictEqual(test_reference.referenceValue, symbol)
assert.strictEqual(test_reference.referenceValue, Symbol.for(''))
})()
test_reference.deleteReference()
}

// (() => {
// const symbol = test_reference.createSymbolForEmptyString()
// test_reference.createReference(symbol, 0)
// assert.strictEqual(test_reference.referenceValue, symbol)
// assert.strictEqual(test_reference.referenceValue, Symbol.for(''))
// })()
// test_reference.deleteReference()
if (context.feature.supportWeakSymbol) {
symbolAsWeakMapKeys()
} else {
assert.throws(symbolAsWeakMapKeys, /Invalid argument/)
}

assert.throws(() => test_reference.createSymbolForIncorrectLength(),
/Invalid argument/);