Skip to content

Commit 096447c

Browse files
committed
src: fix cppgc incompatibility in v8
1 parent 08d6a82 commit 096447c

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

src/base_object-inl.h

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,19 @@
3232

3333
namespace node {
3434

35+
// This just has to be different from the Chromium ones:
36+
// https://source.chromium.org/chromium/chromium/src/+/main:gin/public/gin_embedders.h;l=18-23;drc=5a758a97032f0b656c3c36a3497560762495501a
37+
// Otherwise, when Node is loaded in an isolate which uses cppgc, cppgc will
38+
// misinterpret the data stored in the embedder fields and try to garbage
39+
// collect them.
40+
static uint16_t kNodeEmbedderId = 0x90de;
41+
3542
BaseObject::BaseObject(Environment* env, v8::Local<v8::Object> object)
3643
: persistent_handle_(env->isolate(), object), env_(env) {
3744
CHECK_EQ(false, object.IsEmpty());
38-
CHECK_GT(object->InternalFieldCount(), 0);
45+
CHECK_GE(object->InternalFieldCount(), BaseObject::kInternalFieldCount);
46+
object->SetAlignedPointerInInternalField(BaseObject::kEmbedderType,
47+
&kNodeEmbedderId);
3948
object->SetAlignedPointerInInternalField(
4049
BaseObject::kSlot,
4150
static_cast<void*>(this));
@@ -151,7 +160,9 @@ bool BaseObject::IsWeakOrDetached() const {
151160
void BaseObject::LazilyInitializedJSTemplateConstructor(
152161
const v8::FunctionCallbackInfo<v8::Value>& args) {
153162
DCHECK(args.IsConstructCall());
154-
DCHECK_GT(args.This()->InternalFieldCount(), 0);
163+
CHECK_GE(args.This()->InternalFieldCount(), BaseObject::kInternalFieldCount);
164+
args.This()->SetAlignedPointerInInternalField(
165+
BaseObject::kEmbedderType, &kNodeEmbedderId);
155166
args.This()->SetAlignedPointerInInternalField(BaseObject::kSlot, nullptr);
156167
}
157168

src/base_object.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@ class TransferData;
4040

4141
class BaseObject : public MemoryRetainer {
4242
public:
43-
enum InternalFields { kSlot, kInternalFieldCount };
43+
enum InternalFields { kEmbedderType, kSlot, kInternalFieldCount };
4444

45-
// Associates this object with `object`. It uses the 0th internal field for
45+
// Associates this object with `object`. It uses the 1st internal field for
4646
// that, and in particular aborts if there is no such field.
4747
inline BaseObject(Environment* env, v8::Local<v8::Object> object);
4848
inline ~BaseObject() override;

0 commit comments

Comments
 (0)