Skip to content

Commit

Permalink
src: fix cppgc incompatibility in v8
Browse files Browse the repository at this point in the history
  • Loading branch information
codebytere committed Jun 30, 2022
1 parent 08d6a82 commit 312149f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
15 changes: 13 additions & 2 deletions src/base_object-inl.h
Expand Up @@ -32,10 +32,19 @@

namespace node {

// This just has to be different from the Chromium ones:
// https://source.chromium.org/chromium/chromium/src/+/main:gin/public/gin_embedders.h;l=18-23;drc=5a758a97032f0b656c3c36a3497560762495501a
// Otherwise, when Node is loaded in an isolate which uses cppgc, cppgc will
// misinterpret the data stored in the embedder fields and try to garbage
// collect them.
static uint16_t kNodeEmbedderId = 0x90de;

BaseObject::BaseObject(Environment* env, v8::Local<v8::Object> object)
: persistent_handle_(env->isolate(), object), env_(env) {
CHECK_EQ(false, object.IsEmpty());
CHECK_GT(object->InternalFieldCount(), 0);
CHECK_GT(object->InternalFieldCount(), BaseObject::kSlot);
object->SetAlignedPointerInInternalField(BaseObject::kEmbedderType,
&kNodeEmbedderId);
object->SetAlignedPointerInInternalField(
BaseObject::kSlot,
static_cast<void*>(this));
Expand Down Expand Up @@ -151,7 +160,9 @@ bool BaseObject::IsWeakOrDetached() const {
void BaseObject::LazilyInitializedJSTemplateConstructor(
const v8::FunctionCallbackInfo<v8::Value>& args) {
DCHECK(args.IsConstructCall());
DCHECK_GT(args.This()->InternalFieldCount(), 0);
DCHECK_GT(args.This()->InternalFieldCount(), BaseObject::kSlot);
args.This()->SetAlignedPointerInInternalField(
BaseObject::kEmbedderType, &kNodeEmbedderId);
args.This()->SetAlignedPointerInInternalField(BaseObject::kSlot, nullptr);
}

Expand Down
4 changes: 2 additions & 2 deletions src/base_object.h
Expand Up @@ -40,9 +40,9 @@ class TransferData;

class BaseObject : public MemoryRetainer {
public:
enum InternalFields { kSlot, kInternalFieldCount };
enum InternalFields { kEmbedderType, kSlot, kInternalFieldCount };

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

0 comments on commit 312149f

Please sign in to comment.