Skip to content

Commit de008ae

Browse files
codebyterejoyeecheung
authored andcommitted
src: fix cppgc incompatibility in v8
1 parent c1d6595 commit de008ae

2 files changed

Lines changed: 15 additions & 4 deletions

File tree

src/base_object.h

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,18 @@ namespace worker {
3838
class TransferData;
3939
}
4040

41+
// This just has to be different from the Chromium ones:
42+
// https://source.chromium.org/chromium/chromium/src/+/main:gin/public/gin_embedders.h;l=18-23;drc=5a758a97032f0b656c3c36a3497560762495501a
43+
// Otherwise, when Node is loaded in an isolate which uses cppgc, cppgc will
44+
// misinterpret the data stored in the embedder fields and try to garbage
45+
// collect them.
46+
static uint16_t kNodeEmbedderId = 0x90de;
47+
4148
class BaseObject : public MemoryRetainer {
4249
public:
43-
enum InternalFields { kSlot, kInternalFieldCount };
50+
enum InternalFields { kEmbedderType, kSlot, kInternalFieldCount };
4451

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

src/env.cc

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2126,7 +2126,9 @@ void Environment::RunWeakRefCleanup() {
21262126
BaseObject::BaseObject(Environment* env, Local<Object> object)
21272127
: persistent_handle_(env->isolate(), object), env_(env) {
21282128
CHECK_EQ(false, object.IsEmpty());
2129-
CHECK_GT(object->InternalFieldCount(), 0);
2129+
CHECK_GE(object->InternalFieldCount(), BaseObject::kInternalFieldCount);
2130+
object->SetAlignedPointerInInternalField(BaseObject::kEmbedderType,
2131+
&kNodeEmbedderId);
21302132
object->SetAlignedPointerInInternalField(BaseObject::kSlot,
21312133
static_cast<void*>(this));
21322134
env->AddCleanupHook(DeleteMe, static_cast<void*>(this));
@@ -2180,7 +2182,9 @@ void BaseObject::MakeWeak() {
21802182
void BaseObject::LazilyInitializedJSTemplateConstructor(
21812183
const FunctionCallbackInfo<Value>& args) {
21822184
DCHECK(args.IsConstructCall());
2183-
DCHECK_GT(args.This()->InternalFieldCount(), 0);
2185+
CHECK_GE(args.This()->InternalFieldCount(), BaseObject::kInternalFieldCount);
2186+
args.This()->SetAlignedPointerInInternalField(BaseObject::kEmbedderType,
2187+
&kNodeEmbedderId);
21842188
args.This()->SetAlignedPointerInInternalField(BaseObject::kSlot, nullptr);
21852189
}
21862190

0 commit comments

Comments
 (0)