Skip to content

Commit 004474c

Browse files
committed
refactor: remove LocalFromGlobalReference
1 parent 466d27e commit 004474c

File tree

6 files changed

+38
-58
lines changed

6 files changed

+38
-58
lines changed

packages/emnapi/src/v8/internal.cc

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ Isolate::Isolate(): data_{} {
4545
*reinterpret_cast<internal::Address*>(data_ + internal::Internals::kIsolateRootsOffset + v8::internal::kApiSystemPointerSize * v8::internal::Internals::kEmptyStringRootIndex) = static_cast<internal::Address>(v8impl::Constant::kEmptyString);
4646
}
4747

48+
struct GlobalHandle {
49+
Address object;
50+
Address ref;
51+
};
52+
4853
}
4954

5055
namespace api_internal {
@@ -60,17 +65,17 @@ void FromJustIsNothing() {
6065
internal::Address* GlobalizeReference(internal::Isolate* isolate,
6166
internal::Address value) {
6267
internal::Address ref_id = _v8_globalize_reference(isolate, value);
63-
return new internal::Address(ref_id);
68+
return reinterpret_cast<internal::Address*>(new internal::GlobalHandle{ref_id, ref_id});
6469
}
6570

6671
void DisposeGlobal(internal::Address* global_handle) {
6772
_v8_dispose_global(*global_handle);
68-
delete global_handle;
73+
delete reinterpret_cast<internal::GlobalHandle*>(global_handle);
6974
}
7075

7176
internal::Address* CopyGlobalReference(internal::Address* from) {
7277
internal::Address ref_id = _v8_copy_global_reference(*from);
73-
return new internal::Address(ref_id);
78+
return reinterpret_cast<internal::Address*>(new internal::GlobalHandle{ref_id, ref_id});
7479
}
7580

7681
internal::Address LocalFromGlobalReference(internal::Address global_handle) {

packages/runtime/src/Handle.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ export class HandleStore extends BaseArrayStore<any> {
6565
? (id: number | bigint): any => {
6666
const value = this._values[id as number]
6767
if (value instanceof Reference) return value.deref()
68-
if (value instanceof WeakRef && Number(id) >= this._allocator.next) {
68+
if (value instanceof WeakRef && this.isOutOfScope(id)) {
6969
return value.deref()
7070
}
7171
return value
@@ -77,6 +77,10 @@ export class HandleStore extends BaseArrayStore<any> {
7777
}
7878
}
7979

80+
public isOutOfScope (id: number | bigint): boolean {
81+
return Number(id) >= this._allocator.next
82+
}
83+
8084
public deepDeref<R = any> (id: number | bigint): R | undefined {
8185
return this._deref(id) as R
8286
}

packages/runtime/src/Isolate.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,6 @@ export class Isolate {
4444
public jsValueFromNapiValue<T = any> (napiValue: number | bigint): T | undefined {
4545
return this._handleStore.deepDeref(napiValue)
4646
}
47-
48-
public deleteHandle (napiValue: number | bigint): void {
49-
this._handleStore.dealloc(napiValue)
50-
}
5147
//#endregion
5248

5349
//#region References
@@ -64,6 +60,15 @@ export class Isolate {
6460
public getRef (ref: napi_ref): Reference | undefined {
6561
return this._handleStore.deref(ref)
6662
}
63+
64+
public removeRef (ref: napi_ref) {
65+
if (this._handleStore.isOutOfScope(ref)) {
66+
this._handleStore.dealloc(ref)
67+
} else {
68+
const value = this._handleStore.deepDeref(ref)
69+
this._handleStore.assign(ref, value)
70+
}
71+
}
6772
//#endregion
6873

6974
//#region Exception handling

packages/runtime/src/Reference.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,8 +234,8 @@ export class Reference extends RefTracker {
234234
public override dispose (): void {
235235
if (this.id === 0) return
236236
this.unlink()
237+
this.ctx.removeRef(this.id)
237238
this.persistent.reset()
238-
this.ctx.deleteHandle(this.id)
239239
super.dispose()
240240
this.ctx = undefined!
241241
this.envObject = undefined!

packages/runtime/src/Store.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,12 @@ export class BaseArrayStore<T> extends Disposable implements ObjectAllocator<T>
6363
this._values = Array(initialCapacity) as [undefined, ...(T | undefined)[]]
6464
}
6565

66+
/** @virtual */
67+
public assign (id: number | bigint, value: T): T {
68+
this._values[id as number] = value
69+
return value
70+
}
71+
6672
/** @virtual */
6773
public deref<R extends T = T> (id: number | bigint): R | undefined {
6874
return this._values[id as number] as R

patches/v8.patch

Lines changed: 9 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,7 @@
1-
diff -urN node-22.16.0/v8-function-callback.h node/v8-function-callback.h
2-
--- node-22.16.0/v8-function-callback.h 2025-07-16 14:33:02
3-
+++ node/v8-function-callback.h 2025-07-16 14:48:45
4-
@@ -314,7 +314,7 @@
5-
if (V8_UNLIKELY(handle.IsEmpty())) {
6-
SetTheHole();
7-
} else {
8-
- SetInternal(handle.ptr());
9-
+ SetInternal(api_internal::LocalFromGlobalReference(handle.ptr()));
10-
}
11-
}
12-
13-
@@ -325,7 +325,7 @@
14-
#ifdef V8_ENABLE_CHECKS
15-
internal::VerifyHandleIsNonEmpty(handle.IsEmpty());
16-
#endif // V8_ENABLE_CHECKS
17-
- SetInternal(handle.ptr());
18-
+ SetInternal(api_internal::LocalFromGlobalReference(handle.ptr()));
19-
}
20-
21-
template <typename T>
221
diff -urN node-22.16.0/v8-local-handle.h node/v8-local-handle.h
23-
--- node-22.16.0/v8-local-handle.h 2025-07-16 14:33:02
24-
+++ node/v8-local-handle.h 2025-07-16 14:43:55
25-
@@ -80,6 +80,7 @@
26-
namespace api_internal {
27-
// Called when ToLocalChecked is called on an empty Local.
28-
V8_EXPORT void ToLocalEmpty();
29-
+V8_EXPORT internal::Address LocalFromGlobalReference(internal::Address global_reference);
30-
} // namespace api_internal
31-
32-
/**
33-
@@ -341,7 +342,9 @@
34-
35-
V8_INLINE static Local<T> New(Isolate* isolate,
36-
const PersistentBase<T>& that) {
37-
- return New(isolate, that.template value<T, true>());
38-
+ T* value = that.template value<T, true>();
39-
+ internal::Address address = api_internal::LocalFromGlobalReference(reinterpret_cast<internal::Address>(value));
40-
+ return New(isolate, address);
41-
}
42-
43-
V8_INLINE static Local<T> New(Isolate* isolate,
44-
@@ -717,7 +720,8 @@
2+
--- node-22.16.0/v8-local-handle.h 2025-07-18 10:49:05
3+
+++ node/v8-local-handle.h 2025-07-31 16:44:50
4+
@@ -717,7 +717,8 @@
455
template <class T>
466
V8_INLINE Local<T> Escape(Local<T> value) {
477
#ifdef V8_ENABLE_DIRECT_LOCAL
@@ -52,8 +12,8 @@ diff -urN node-22.16.0/v8-local-handle.h node/v8-local-handle.h
5212
if (value.IsEmpty()) return value;
5313
return Local<T>::FromSlot(EscapeSlot(value.slot()));
5414
diff -urN node-22.16.0/v8-object.h node/v8-object.h
55-
--- node-22.16.0/v8-object.h 2025-07-16 14:33:02
56-
+++ node/v8-object.h 2025-07-16 14:44:57
15+
--- node-22.16.0/v8-object.h 2025-07-18 10:49:05
16+
+++ node/v8-object.h 2025-07-18 10:49:05
5717
@@ -739,69 +739,15 @@
5818
// --- Implementation ---
5919

@@ -125,8 +85,8 @@ diff -urN node-22.16.0/v8-object.h node/v8-object.h
12585
}
12686

12787
diff -urN node-22.16.0/v8-value.h node/v8-value.h
128-
--- node-22.16.0/v8-value.h 2025-07-16 14:33:02
129-
+++ node/v8-value.h 2025-07-16 14:47:53
88+
--- node-22.16.0/v8-value.h 2025-07-18 10:49:05
89+
+++ node/v8-value.h 2025-07-18 10:49:05
13090
@@ -524,11 +524,7 @@
13191
}
13292

@@ -200,8 +160,8 @@ diff -urN node-22.16.0/v8-value.h node/v8-value.h
200160

201161
bool Value::QuickIsString() const {
202162
diff -urN node-22.16.0/v8config.h node/v8config.h
203-
--- node-22.16.0/v8config.h 2025-07-16 14:33:02
204-
+++ node/v8config.h 2025-07-16 14:35:09
163+
--- node-22.16.0/v8config.h 2025-07-18 10:49:05
164+
+++ node/v8config.h 2025-07-18 10:49:05
205165
@@ -768,14 +768,14 @@
206166
// The V8_HOST_ARCH_* macros correspond to the architecture on which V8, as a
207167
// virtual machine and compiler, runs. Don't confuse this with the architecture

0 commit comments

Comments
 (0)