Skip to content

Commit

Permalink
vm: migrate ContextifyScript to cppgc
Browse files Browse the repository at this point in the history
  • Loading branch information
joyeecheung committed Aug 18, 2024
1 parent 0c7e2a5 commit e94cf59
Showing 3 changed files with 49 additions and 36 deletions.
12 changes: 6 additions & 6 deletions benchmark/vm/compile-script-in-isolate-cache.js
Original file line number Diff line number Diff line change
@@ -5,17 +5,17 @@
const common = require('../common.js');
const fs = require('fs');
const vm = require('vm');
const fixtures = require('../../test/common/fixtures.js');
const scriptPath = fixtures.path('snapshot', 'typescript.js');
const path = require('path');

const bench = common.createBenchmark(main, {
type: ['with-dynamic-import-callback', 'without-dynamic-import-callback'],
n: [100],
filename: ['test/fixtures/snapshot/typescript.js', 'test/fixtures/syntax/good_syntax.js'],
n: [1000],
});

const scriptSource = fs.readFileSync(scriptPath, 'utf8');

function main({ n, type }) {
function main({ n, type, filename }) {
const scriptPath = path.resolve(__dirname, '..', '..', filename);
const scriptSource = fs.readFileSync(scriptPath, 'utf8');
let script;
bench.start();
const options = {};
52 changes: 32 additions & 20 deletions src/node_contextify.cc
Original file line number Diff line number Diff line change
@@ -22,6 +22,7 @@
#include "node_contextify.h"

#include "base_object-inl.h"
#include "cppgc/allocation.h"
#include "memory_tracker-inl.h"
#include "module_wrap.h"
#include "node_context_data.h"
@@ -915,6 +916,12 @@ void ContextifyScript::RegisterExternalReferences(
registry->Register(RunInContext);
}

ContextifyScript* ContextifyScript::New(Environment* env,
Local<Object> object) {
return cppgc::MakeGarbageCollected<ContextifyScript>(
env->isolate()->GetCppHeap()->GetAllocationHandle(), env, object);
}

void ContextifyScript::New(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);
Isolate* isolate = env->isolate();
@@ -965,8 +972,7 @@ void ContextifyScript::New(const FunctionCallbackInfo<Value>& args) {
id_symbol = args[7].As<Symbol>();
}

ContextifyScript* contextify_script =
new ContextifyScript(env, args.This());
ContextifyScript* contextify_script = New(env, args.This());

if (*TRACE_EVENT_API_GET_CATEGORY_GROUP_ENABLED(
TRACING_CATEGORY_NODE2(vm, script)) != 0) {
@@ -1025,8 +1031,6 @@ void ContextifyScript::New(const FunctionCallbackInfo<Value>& args) {
}

contextify_script->script_.Reset(isolate, v8_script);
contextify_script->script_.SetWeak();
contextify_script->object()->SetInternalField(kUnboundScriptSlot, v8_script);

std::unique_ptr<ScriptCompiler::CachedData> new_cached_data;
if (produce_cached_data) {
@@ -1128,12 +1132,10 @@ bool ContextifyScript::InstanceOf(Environment* env,
void ContextifyScript::CreateCachedData(
const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);
ContextifyScript* wrapped_script;
ASSIGN_OR_RETURN_UNWRAP(&wrapped_script, args.This());
Local<UnboundScript> unbound_script =
PersistentToLocal::Default(env->isolate(), wrapped_script->script_);
ContextifyScript* wrapped_script = wrapped_script =
Unwrap<ContextifyScript>(args.This());
std::unique_ptr<ScriptCompiler::CachedData> cached_data(
ScriptCompiler::CreateCodeCache(unbound_script));
ScriptCompiler::CreateCodeCache(wrapped_script->unbound_script()));
if (!cached_data) {
args.GetReturnValue().Set(Buffer::New(env, 0).ToLocalChecked());
} else {
@@ -1147,9 +1149,8 @@ void ContextifyScript::CreateCachedData(

void ContextifyScript::RunInContext(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);

ContextifyScript* wrapped_script;
ASSIGN_OR_RETURN_UNWRAP(&wrapped_script, args.This());
ContextifyScript* wrapped_script = wrapped_script =
Unwrap<ContextifyScript>(args.This());

CHECK_EQ(args.Length(), 5);
CHECK(args[0]->IsObject() || args[0]->IsNull());
@@ -1218,11 +1219,10 @@ bool ContextifyScript::EvalMachine(Local<Context> context,
}

TryCatchScope try_catch(env);
ContextifyScript* wrapped_script;
ASSIGN_OR_RETURN_UNWRAP(&wrapped_script, args.This(), false);
Local<UnboundScript> unbound_script =
PersistentToLocal::Default(env->isolate(), wrapped_script->script_);
Local<Script> script = unbound_script->BindToCurrentContext();
ContextifyScript* wrapped_script = wrapped_script =
Unwrap<ContextifyScript>(args.This());
Local<Script> script =
wrapped_script->unbound_script()->BindToCurrentContext();

#if HAVE_INSPECTOR
if (break_on_first_line) {
@@ -1304,9 +1304,21 @@ bool ContextifyScript::EvalMachine(Local<Context> context,
return true;
}

ContextifyScript::ContextifyScript(Environment* env, Local<Object> object)
: BaseObject(env, object) {
MakeWeak();
Local<UnboundScript> ContextifyScript::unbound_script() const {
return script_.Get(env()->isolate());
}

void ContextifyScript::set_unbound_script(Local<UnboundScript> script) {
script_.Reset(env()->isolate(), script);
}

void ContextifyScript::Trace(cppgc::Visitor* visitor) const {
CppgcMixin::Trace(visitor);
visitor->Trace(script_);
}

ContextifyScript::ContextifyScript(Environment* env, Local<Object> object) {
InitializeCppgc(this, env, object);
}

ContextifyScript::~ContextifyScript() {}
21 changes: 11 additions & 10 deletions src/node_contextify.h
Original file line number Diff line number Diff line change
@@ -4,6 +4,7 @@
#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS

#include "base_object-inl.h"
#include "cppgc_helpers.h"
#include "node_context_data.h"
#include "node_errors.h"

@@ -139,23 +140,23 @@ class ContextifyContext : public BaseObject {
std::unique_ptr<v8::MicrotaskQueue> microtask_queue_;
};

class ContextifyScript : public BaseObject {
class ContextifyScript final : public cppgc::GarbageCollected<ContextifyScript>,
public cppgc::NameProvider,
public CppgcMixin {
public:
enum InternalFields {
kUnboundScriptSlot = BaseObject::kInternalFieldCount,
kInternalFieldCount
};

SET_NO_MEMORY_INFO()
SET_MEMORY_INFO_NAME(ContextifyScript)
SET_SELF_SIZE(ContextifyScript)
SET_CPPGC_NAME(ContextifyScript)
void Trace(cppgc::Visitor* visitor) const final;

ContextifyScript(Environment* env, v8::Local<v8::Object> object);
~ContextifyScript() override;

v8::Local<v8::UnboundScript> unbound_script() const;
void set_unbound_script(v8::Local<v8::UnboundScript>);

static void CreatePerIsolateProperties(IsolateData* isolate_data,
v8::Local<v8::ObjectTemplate> target);
static void RegisterExternalReferences(ExternalReferenceRegistry* registry);
static ContextifyScript* New(Environment* env, v8::Local<v8::Object> object);
static void New(const v8::FunctionCallbackInfo<v8::Value>& args);
static bool InstanceOf(Environment* env, const v8::Local<v8::Value>& args);
static void CreateCachedData(const v8::FunctionCallbackInfo<v8::Value>& args);
@@ -170,7 +171,7 @@ class ContextifyScript : public BaseObject {
const v8::FunctionCallbackInfo<v8::Value>& args);

private:
v8::Global<v8::UnboundScript> script_;
v8::TracedReference<v8::UnboundScript> script_;
};

v8::Maybe<bool> StoreCodeCacheResult(

0 comments on commit e94cf59

Please sign in to comment.