diff --git a/benchmark/vm/compile-script-in-isolate-cache.js b/benchmark/vm/compile-script-in-isolate-cache.js index 7eceb0eba0d215..7c909d840b4316 100644 --- a/benchmark/vm/compile-script-in-isolate-cache.js +++ b/benchmark/vm/compile-script-in-isolate-cache.js @@ -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 = {}; diff --git a/src/heap_utils.cc b/src/heap_utils.cc index e385955a5d5fce..8b477bdedf176a 100644 --- a/src/heap_utils.cc +++ b/src/heap_utils.cc @@ -18,6 +18,7 @@ #endif using v8::Array; +using v8::Data; using v8::Boolean; using v8::Context; using v8::EmbedderGraph; @@ -49,17 +50,20 @@ class JSGraphJSNode : public EmbedderGraph::Node { const char* Name() override { return ""; } size_t SizeInBytes() override { return 0; } bool IsEmbedderNode() override { return false; } - Local JSValue() { return PersistentToLocal::Strong(persistent_); } + Local V8Value() { return PersistentToLocal::Strong(persistent_); } int IdentityHash() { - Local v = JSValue(); + Local d = V8Value(); + // TODO(joyeecheung): return something better? + if (!d->IsValue()) return reinterpret_cast(this); + Local v = d.As(); if (v->IsObject()) return v.As()->GetIdentityHash(); if (v->IsName()) return v.As()->GetIdentityHash(); if (v->IsInt32()) return v.As()->Value(); return 0; } - JSGraphJSNode(Isolate* isolate, Local val) + JSGraphJSNode(Isolate* isolate, Local val) : persistent_(isolate, val) { CHECK(!val.IsEmpty()); } @@ -72,19 +76,27 @@ class JSGraphJSNode : public EmbedderGraph::Node { struct Equal { inline bool operator()(JSGraphJSNode* a, JSGraphJSNode* b) const { - return a->JSValue()->SameValue(b->JSValue()); + Local data_a = a->V8Value(); + Local data_b = a->V8Value(); + if (data_a->IsValue()) { + if (!data_b->IsValue()) { + return false; + } + return data_a.As()->SameValue(data_b.As()); + } + return data_a == data_b; } }; private: - Global persistent_; + Global persistent_; }; class JSGraph : public EmbedderGraph { public: explicit JSGraph(Isolate* isolate) : isolate_(isolate) {} - Node* V8Node(const Local& value) override { + Node* V8Node(const Local& value) override { std::unique_ptr n { new JSGraphJSNode(isolate_, value) }; auto it = engine_nodes_.find(n.get()); if (it != engine_nodes_.end()) @@ -153,8 +165,8 @@ class JSGraph : public EmbedderGraph { if (nodes->Set(context, i++, obj).IsNothing()) return MaybeLocal(); if (!n->IsEmbedderNode()) { - value = static_cast(n.get())->JSValue(); - if (obj->Set(context, value_string, value).IsNothing()) + Local data = static_cast(n.get())->V8Value(); + if (data->IsValue() && obj->Set(context, value_string, data.As()).IsNothing()) return MaybeLocal(); } } diff --git a/src/node_contextify.cc b/src/node_contextify.cc index e1ac63c881abef..fa0d6d02be8db0 100644 --- a/src/node_contextify.cc +++ b/src/node_contextify.cc @@ -21,6 +21,7 @@ #include "node_contextify.h" +#include "cppgc/allocation.h" #include "base_object-inl.h" #include "memory_tracker-inl.h" #include "module_wrap.h" @@ -826,8 +827,9 @@ void ContextifyScript::New(const FunctionCallbackInfo& args) { id_symbol = args[7].As(); } - ContextifyScript* contextify_script = - new ContextifyScript(env, args.This()); + + ContextifyScript* contextify_script = cppgc::MakeGarbageCollected( + env->isolate()->GetCppHeap()->GetAllocationHandle(), env, args.This()); if (*TRACE_EVENT_API_GET_CATEGORY_GROUP_ENABLED( TRACING_CATEGORY_NODE2(vm, script)) != 0) { @@ -887,8 +889,6 @@ void ContextifyScript::New(const FunctionCallbackInfo& args) { } contextify_script->script_.Reset(isolate, v8_script); - contextify_script->script_.SetWeak(); - contextify_script->object()->SetInternalField(kUnboundScriptSlot, v8_script); std::unique_ptr new_cached_data; if (produce_cached_data) { @@ -990,10 +990,9 @@ bool ContextifyScript::InstanceOf(Environment* env, void ContextifyScript::CreateCachedData( const FunctionCallbackInfo& args) { Environment* env = Environment::GetCurrent(args); - ContextifyScript* wrapped_script; - ASSIGN_OR_RETURN_UNWRAP(&wrapped_script, args.Holder()); - Local unbound_script = - PersistentToLocal::Default(env->isolate(), wrapped_script->script_); + ContextifyScript* wrapped_script = CppgcMixin::Unwrap(args.Holder()); + CHECK_NOT_NULL(wrapped_script); + Local unbound_script = wrapped_script->script_.Get(env->isolate()); std::unique_ptr cached_data( ScriptCompiler::CreateCodeCache(unbound_script)); if (!cached_data) { @@ -1010,8 +1009,8 @@ void ContextifyScript::CreateCachedData( void ContextifyScript::RunInContext(const FunctionCallbackInfo& args) { Environment* env = Environment::GetCurrent(args); - ContextifyScript* wrapped_script; - ASSIGN_OR_RETURN_UNWRAP(&wrapped_script, args.Holder()); + ContextifyScript* wrapped_script = CppgcMixin::Unwrap(args.Holder()); + CHECK_NOT_NULL(wrapped_script); CHECK_EQ(args.Length(), 5); CHECK(args[0]->IsObject() || args[0]->IsNull()); @@ -1081,10 +1080,9 @@ bool ContextifyScript::EvalMachine(Local context, TryCatchScope try_catch(env); Isolate::SafeForTerminationScope safe_for_termination(env->isolate()); - ContextifyScript* wrapped_script; - ASSIGN_OR_RETURN_UNWRAP(&wrapped_script, args.Holder(), false); - Local unbound_script = - PersistentToLocal::Default(env->isolate(), wrapped_script->script_); + ContextifyScript* wrapped_script = CppgcMixin::Unwrap(args.Holder()); + CHECK_NOT_NULL(wrapped_script); + Local unbound_script = wrapped_script->script_.Get(env->isolate()); Local