Skip to content

Commit c3efda3

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

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

src/base_object-inl.h

+14-2
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,21 @@
3232

3333
namespace node {
3434

35+
namespace {
36+
// This just has to be different from the Chromium ones:
37+
// https://source.chromium.org/chromium/chromium/src/+/main:gin/public/gin_embedders.h;l=18-23;drc=5a758a97032f0b656c3c36a3497560762495501a
38+
// Otherwise, when Node is loaded in an isolate which uses cppgc, cppgc will
39+
// misinterpret the data stored in the embedder fields and try to garbage
40+
// collect them.
41+
static uint16_t kNodeEmbedderId = 0x90de;
42+
}
43+
3544
BaseObject::BaseObject(Environment* env, v8::Local<v8::Object> object)
3645
: persistent_handle_(env->isolate(), object), env_(env) {
3746
CHECK_EQ(false, object.IsEmpty());
38-
CHECK_GT(object->InternalFieldCount(), 0);
47+
CHECK_GT(object->InternalFieldCount(), BaseObject::kSlot);
48+
object->SetAlignedPointerInInternalField(BaseObject::kEmbedderType,
49+
&kNodeEmbedderId);
3950
object->SetAlignedPointerInInternalField(
4051
BaseObject::kSlot,
4152
static_cast<void*>(this));
@@ -151,7 +162,8 @@ bool BaseObject::IsWeakOrDetached() const {
151162
void BaseObject::LazilyInitializedJSTemplateConstructor(
152163
const v8::FunctionCallbackInfo<v8::Value>& args) {
153164
DCHECK(args.IsConstructCall());
154-
DCHECK_GT(args.This()->InternalFieldCount(), 0);
165+
DCHECK_GT(args.This()->InternalFieldCount(), BaseObject::kSlot);
166+
args.This()->SetAlignedPointerInInternalField(BaseObject::kEmbedderType, &kNodeEmbedderId);
155167
args.This()->SetAlignedPointerInInternalField(BaseObject::kSlot, nullptr);
156168
}
157169

src/base_object.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class TransferData;
4040

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

4545
// Associates this object with `object`. It uses the 0th internal field for
4646
// that, and in particular aborts if there is no such field.

0 commit comments

Comments
 (0)