Skip to content

Commit b0c688b

Browse files
authored
feat: Persistent and Global (#154)
1 parent 2872002 commit b0c688b

File tree

14 files changed

+640
-110
lines changed

14 files changed

+640
-110
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@ out
1212
/script/emnapi.zip
1313
/script/*.cmake
1414
*.tgz
15+
temp

packages/emnapi/include/node/emnapi.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,17 @@ napi_status emnapi_get_memory_address(napi_env env,
8181
emnapi_ownership* ownership,
8282
bool* runtime_allocated);
8383

84+
EMNAPI_EXTERN void emnapi_debug(
85+
const char* file,
86+
int lineno,
87+
const char* str,
88+
void* value,
89+
int type
90+
);
91+
92+
#define DEBUGGER_LOG(str, value, type) \
93+
emnapi_debug(__FILE__, __LINE__, str, value, type)
94+
8495
EXTERN_C_END
8596

8697
#endif

packages/emnapi/src/emnapi.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,24 @@ import { from64, makeSetValue, makeGetValue } from 'emscripten:parse-tools'
44
import { type MemoryViewDescriptor, type ArrayBufferPointer, emnapiExternalMemory } from './memory'
55
import { napi_add_finalizer } from './wrap'
66
import { $CHECK_ARG, $PREAMBLE, $CHECK_ENV, $GET_RETURN_STATUS } from './macro'
7+
import { emnapiString } from './string'
8+
9+
/**
10+
* @__sig vpippi
11+
*/
12+
export function emnapi_debug (file: number, lineno: number, str: number, ptr: Ptr, type: number) {
13+
from64('str')
14+
from64('file')
15+
const string = str ? emnapiString.UTF8ToString(str, -1) : ''
16+
const message = `[emnapi_debug] ${emnapiString.UTF8ToString(file, -1)}:${lineno} ${string} ${ptr}`
17+
const logArgs: any[] = [message]
18+
if (ptr) {
19+
logArgs.push(type === 1 ? emnapiCtx.getRef(ptr)?.persistent : emnapiCtx.jsValueFromNapiValue(ptr))
20+
}
21+
console.log(...logArgs)
22+
// eslint-disable-next-line no-debugger
23+
debugger
24+
}
725

826
/**
927
* @__sig ipippppp

packages/emnapi/src/v8/internal.cc

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ namespace v8 {
66
extern "C" {
77
V8_EXTERN internal::Address _v8_globalize_reference(
88
internal::Isolate* isolate, internal::Address value);
9+
V8_EXTERN internal::Address _v8_copy_global_reference(internal::Address from);
10+
V8_EXTERN internal::Address _v8_local_from_global_reference(internal::Address ref);
11+
V8_EXTERN void _v8_move_global_reference(internal::Address from,
12+
internal::Address to);
913
V8_EXTERN void _v8_dispose_global(internal::Address global_handle);
1014
V8_EXTERN void _v8_make_weak(internal::Address location,
1115
void* data,
@@ -64,6 +68,24 @@ void DisposeGlobal(internal::Address* global_handle) {
6468
delete global_handle;
6569
}
6670

71+
internal::Address* CopyGlobalReference(internal::Address* from) {
72+
internal::Address ref_id = _v8_copy_global_reference(*from);
73+
return new internal::Address(ref_id);
74+
}
75+
76+
internal::Address LocalFromGlobalReference(internal::Address global_handle) {
77+
return _v8_local_from_global_reference(global_handle);
78+
}
79+
80+
void MoveGlobalReference(internal::Address** from, internal::Address** to) {
81+
if (*from == *to) {
82+
return;
83+
}
84+
*to = *from;
85+
*from = nullptr;
86+
// _v8_move_global_reference(**from, **to);
87+
}
88+
6789
namespace {
6890
static void WeakCallback(WeakCallbackInfo<void>::Callback weak_callback,
6991
void* data,

packages/emnapi/src/v8/internal.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
11
import { from64, makeDynCall } from 'emscripten:parse-tools'
22

3+
/**
4+
* @__deps $emnapiCtx
5+
* @__sig pp
6+
*/
7+
export function _v8_local_from_global_reference (ref: Ptr): Ptr {
8+
const reference = emnapiCtx.getRef(ref)
9+
if (reference === undefined) return 1
10+
const id = reference.get(emnapiCtx)
11+
return id || 1
12+
}
13+
314
/**
415
* @__deps $emnapiCtx
516
* @__sig ppp
@@ -10,6 +21,27 @@ export function _v8_globalize_reference (isolate: Ptr, value: Ptr): Ptr {
1021
return emnapiCtx.createReference(undefined, jsValue, 1, ReferenceOwnership.kUserland as any).id
1122
}
1223

24+
/**
25+
* @__deps $emnapiCtx
26+
* @__sig pp
27+
*/
28+
export function _v8_copy_global_reference (from: Ptr): Ptr {
29+
const ref = emnapiCtx.getRef(from)
30+
if (ref === undefined) return 0
31+
return ref.copy().id
32+
}
33+
34+
/**
35+
* @__deps $emnapiCtx
36+
* @__sig vpp
37+
*/
38+
export function _v8_move_global_reference (from: Ptr, to: Ptr): void {
39+
const refFrom = emnapiCtx.getRef(from)
40+
const refTo = emnapiCtx.getRef(to)
41+
if (refFrom === undefined || refTo === undefined) return
42+
refFrom.move(refTo)
43+
}
44+
1345
/**
1446
* @__deps $emnapiCtx
1547
* @__sig vp

packages/runtime/src/Context.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ export class Context {
212212
ownership: ReferenceOwnership
213213
): Reference {
214214
return Reference.create(
215-
this.getCurrentScope(),
215+
this,
216216
envObject,
217217
handle_id,
218218
initialRefcount,
@@ -228,7 +228,7 @@ export class Context {
228228
data: void_p
229229
): Reference {
230230
return ReferenceWithData.create(
231-
this.getCurrentScope(),
231+
this,
232232
envObject,
233233
handle_id,
234234
initialRefcount,
@@ -247,7 +247,7 @@ export class Context {
247247
finalize_hint: void_p = 0
248248
): Reference {
249249
return ReferenceWithFinalizer.create(
250-
this.getCurrentScope(),
250+
this,
251251
envObject,
252252
handle_id,
253253
initialRefcount,
@@ -495,6 +495,10 @@ export class Context {
495495
return this.handleStore.deepDeref(napiValue)
496496
}
497497

498+
public deleteHandle (napiValue: number | bigint): void {
499+
this.handleStore.dealloc(napiValue)
500+
}
501+
498502
public isExternal (value: unknown): boolean {
499503
return isExternal(value)
500504
}

packages/runtime/src/Finalizer.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,29 @@ export class Finalizer {
1212
this._makeDynCall_vppp = envObject.makeDynCall_vppp
1313
}
1414

15+
public copy (): Finalizer {
16+
const newFina: Finalizer = Object.create(Finalizer.prototype)
17+
newFina.envObject = this.envObject
18+
newFina._makeDynCall_vppp = this._makeDynCall_vppp
19+
newFina._finalizeCallback = this._finalizeCallback
20+
newFina._finalizeData = this._finalizeData
21+
newFina._finalizeHint = this._finalizeHint
22+
return newFina
23+
}
24+
25+
public move (target: Finalizer): void {
26+
target._finalizeCallback = this._finalizeCallback
27+
target._finalizeData = this._finalizeData
28+
target._finalizeHint = this._finalizeHint
29+
target.envObject = this.envObject
30+
target._makeDynCall_vppp = this._makeDynCall_vppp
31+
32+
this._finalizeCallback = 0
33+
this._finalizeData = 0
34+
this._finalizeHint = 0
35+
this.dispose()
36+
}
37+
1538
public callback (): napi_finalize { return this._finalizeCallback }
1639
public data (): void_p { return this._finalizeData }
1740
public hint (): void_p { return this._finalizeHint }

packages/runtime/src/RefTracker.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ export class RefTracker extends Disposable {
77
/** @virtual */
88
public finalize (): void {}
99

10-
private _next: RefTracker | null = null
11-
private _prev: RefTracker | null = null
10+
protected _next: RefTracker | null = null
11+
protected _prev: RefTracker | null = null
1212

1313
public link (list: RefTracker): void {
1414
this._prev = list

0 commit comments

Comments
 (0)