| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -371,13 +371,11 @@ void ContextifyContext::CreatePerIsolateProperties( | |||
| 371 | 371 | IsolateData* isolate_data, Local<ObjectTemplate> target) { | |
| 372 | 372 | Isolate* isolate = isolate_data->isolate(); | |
| 373 | 373 | SetMethod(isolate, target, "makeContext", MakeContext); | |
| 374 | - SetMethod(isolate, target, "compileFunction", CompileFunction); | ||
| 375 | 374 | } | |
| 376 | 375 | ||
| 377 | 376 | void ContextifyContext::RegisterExternalReferences( | |
| 378 | 377 | ExternalReferenceRegistry* registry) { | |
| 379 | 378 | registry->Register(MakeContext); | |
| 380 | - registry->Register(CompileFunction); | ||
| 381 | 379 | registry->Register(PropertyQueryCallback); | |
| 382 | 380 | registry->Register(PropertyGetterCallback); | |
| 383 | 381 | registry->Register(PropertySetterCallback); | |
@@ -1161,22 +1159,6 @@ Maybe<void> StoreCodeCacheResult( | |||
| 1161 | 1159 | return JustVoid(); | |
| 1162 | 1160 | } | |
| 1163 | 1161 | ||
| 1164 | - // TODO(RaisinTen): Reuse in ContextifyContext::CompileFunction(). | ||
| 1165 | - MaybeLocal<Function> CompileFunction(Local<Context> context, | ||
| 1166 | - Local<String> filename, | ||
| 1167 | - Local<String> content, | ||
| 1168 | - LocalVector<String>* parameters) { | ||
| 1169 | - ScriptOrigin script_origin(filename, 0, 0, true); | ||
| 1170 | - ScriptCompiler::Source script_source(content, script_origin); | ||
| 1171 | - | ||
| 1172 | - return ScriptCompiler::CompileFunction(context, | ||
| 1173 | - &script_source, | ||
| 1174 | - parameters->size(), | ||
| 1175 | - parameters->data(), | ||
| 1176 | - 0, | ||
| 1177 | - nullptr); | ||
| 1178 | - } | ||
| 1179 | - | ||
| 1180 | 1162 | bool ContextifyScript::InstanceOf(Environment* env, | |
| 1181 | 1163 | const Local<Value>& value) { | |
| 1182 | 1164 | return !value.IsEmpty() && | |
@@ -1382,7 +1364,19 @@ ContextifyScript::ContextifyScript(Environment* env, Local<Object> object) | |||
| 1382 | 1364 | ||
| 1383 | 1365 | ContextifyScript::~ContextifyScript() {} | |
| 1384 | 1366 | ||
| 1385 | - void ContextifyContext::CompileFunction( | ||
| 1367 | + void ContextifyFunction::RegisterExternalReferences( | ||
| 1368 | + ExternalReferenceRegistry* registry) { | ||
| 1369 | + registry->Register(CompileFunction); | ||
| 1370 | + } | ||
| 1371 | + | ||
| 1372 | + void ContextifyFunction::CreatePerIsolateProperties( | ||
| 1373 | + IsolateData* isolate_data, Local<ObjectTemplate> target) { | ||
| 1374 | + Isolate* isolate = isolate_data->isolate(); | ||
| 1375 | + | ||
| 1376 | + SetMethod(isolate, target, "compileFunction", CompileFunction); | ||
| 1377 | + } | ||
| 1378 | + | ||
| 1379 | + void ContextifyFunction::CompileFunction( | ||
| 1386 | 1380 | const FunctionCallbackInfo<Value>& args) { | |
| 1387 | 1381 | Environment* env = Environment::GetCurrent(args); | |
| 1388 | 1382 | Isolate* isolate = env->isolate(); | |
@@ -1532,7 +1526,7 @@ static LocalVector<String> GetCJSParameters(IsolateData* data) { | |||
| 1532 | 1526 | return result; | |
| 1533 | 1527 | } | |
| 1534 | 1528 | ||
| 1535 | - MaybeLocal<Object> ContextifyContext::CompileFunctionAndCacheResult( | ||
| 1529 | + MaybeLocal<Object> ContextifyFunction::CompileFunctionAndCacheResult( | ||
| 1536 | 1530 | Environment* env, | |
| 1537 | 1531 | Local<Context> parsing_context, | |
| 1538 | 1532 | ScriptCompiler::Source* source, | |
@@ -1963,6 +1957,7 @@ void CreatePerIsolateProperties(IsolateData* isolate_data, | |||
| 1963 | 1957 | ||
| 1964 | 1958 | ContextifyContext::CreatePerIsolateProperties(isolate_data, target); | |
| 1965 | 1959 | ContextifyScript::CreatePerIsolateProperties(isolate_data, target); | |
| 1960 | + ContextifyFunction::CreatePerIsolateProperties(isolate_data, target); | ||
| 1966 | 1961 | ||
| 1967 | 1962 | SetMethod(isolate, target, "startSigintWatchdog", StartSigintWatchdog); | |
| 1968 | 1963 | SetMethod(isolate, target, "stopSigintWatchdog", StopSigintWatchdog); | |
@@ -2015,6 +2010,7 @@ static void CreatePerContextProperties(Local<Object> target, | |||
| 2015 | 2010 | void RegisterExternalReferences(ExternalReferenceRegistry* registry) { | |
| 2016 | 2011 | ContextifyContext::RegisterExternalReferences(registry); | |
| 2017 | 2012 | ContextifyScript::RegisterExternalReferences(registry); | |
| 2013 | + ContextifyFunction::RegisterExternalReferences(registry); | ||
| 2018 | 2014 | ||
| 2019 | 2015 | registry->Register(CompileFunctionForCJSLoader); | |
| 2020 | 2016 | registry->Register(StartSigintWatchdog); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -86,18 +86,6 @@ class ContextifyContext : public BaseObject { | |||
| 86 | 86 | static bool IsStillInitializing(const ContextifyContext* ctx); | |
| 87 | 87 | static void MakeContext(const v8::FunctionCallbackInfo<v8::Value>& args); | |
| 88 | 88 | static void IsContext(const v8::FunctionCallbackInfo<v8::Value>& args); | |
| 89 | - static void CompileFunction( | ||
| 90 | - const v8::FunctionCallbackInfo<v8::Value>& args); | ||
| 91 | - static v8::MaybeLocal<v8::Object> CompileFunctionAndCacheResult( | ||
| 92 | - Environment* env, | ||
| 93 | - v8::Local<v8::Context> parsing_context, | ||
| 94 | - v8::ScriptCompiler::Source* source, | ||
| 95 | - v8::LocalVector<v8::String> params, | ||
| 96 | - v8::LocalVector<v8::Object> context_extensions, | ||
| 97 | - v8::ScriptCompiler::CompileOptions options, | ||
| 98 | - bool produce_cached_data, | ||
| 99 | - v8::Local<v8::Symbol> id_symbol, | ||
| 100 | - const errors::TryCatchScope& try_catch); | ||
| 101 | 89 | static v8::Intercepted PropertyQueryCallback( | |
| 102 | 90 | v8::Local<v8::Name> property, | |
| 103 | 91 | const v8::PropertyCallbackInfo<v8::Integer>& args); | |
@@ -177,6 +165,29 @@ class ContextifyScript : public BaseObject { | |||
| 177 | 165 | v8::Global<v8::UnboundScript> script_; | |
| 178 | 166 | }; | |
| 179 | 167 | ||
| 168 | + class ContextifyFunction final { | ||
| 169 | + public: | ||
| 170 | + static void RegisterExternalReferences(ExternalReferenceRegistry* registry); | ||
| 171 | + static void CreatePerIsolateProperties(IsolateData* isolate_data, | ||
| 172 | + v8::Local<v8::ObjectTemplate> target); | ||
| 173 | + | ||
| 174 | + static void CompileFunction(const v8::FunctionCallbackInfo<v8::Value>& args); | ||
| 175 | + static v8::MaybeLocal<v8::Object> CompileFunctionAndCacheResult( | ||
| 176 | + Environment* env, | ||
| 177 | + v8::Local<v8::Context> parsing_context, | ||
| 178 | + v8::ScriptCompiler::Source* source, | ||
| 179 | + v8::LocalVector<v8::String> params, | ||
| 180 | + v8::LocalVector<v8::Object> context_extensions, | ||
| 181 | + v8::ScriptCompiler::CompileOptions options, | ||
| 182 | + bool produce_cached_data, | ||
| 183 | + v8::Local<v8::Symbol> id_symbol, | ||
| 184 | + const errors::TryCatchScope& try_catch); | ||
| 185 | + | ||
| 186 | + private: | ||
| 187 | + ContextifyFunction() = delete; | ||
| 188 | + ~ContextifyFunction() = delete; | ||
| 189 | + }; | ||
| 190 | + | ||
| 180 | 191 | v8::Maybe<void> StoreCodeCacheResult( | |
| 181 | 192 | Environment* env, | |
| 182 | 193 | v8::Local<v8::Object> target, | |
@@ -185,12 +196,6 @@ v8::Maybe<void> StoreCodeCacheResult( | |||
| 185 | 196 | bool produce_cached_data, | |
| 186 | 197 | std::unique_ptr<v8::ScriptCompiler::CachedData> new_cached_data); | |
| 187 | 198 | ||
| 188 | - v8::MaybeLocal<v8::Function> CompileFunction( | ||
| 189 | - v8::Local<v8::Context> context, | ||
| 190 | - v8::Local<v8::String> filename, | ||
| 191 | - v8::Local<v8::String> content, | ||
| 192 | - v8::LocalVector<v8::String>* parameters); | ||
| 193 | - | ||
| 194 | 199 | } // namespace contextify | |
| 195 | 200 | } // namespace node | |
| 196 | 201 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -41,6 +41,7 @@ using v8::MaybeLocal; | |||
| 41 | 41 | using v8::NewStringType; | |
| 42 | 42 | using v8::Object; | |
| 43 | 43 | using v8::ScriptCompiler; | |
| 44 | + using v8::ScriptOrigin; | ||
| 44 | 45 | using v8::String; | |
| 45 | 46 | using v8::Value; | |
| 46 | 47 | ||
@@ -460,16 +461,23 @@ std::optional<std::string> GenerateCodeCache(std::string_view main_path, | |||
| 460 | 461 | FIXED_ONE_BYTE_STRING(isolate, "__filename"), | |
| 461 | 462 | FIXED_ONE_BYTE_STRING(isolate, "__dirname"), | |
| 462 | 463 | }); | |
| 463 | - | ||
| 464 | - // TODO(RaisinTen): Using the V8 code cache prevents us from using `import()` | ||
| 465 | - // in the SEA code. Support it. | ||
| 466 | - // Refs: https://github.com/nodejs/node/pull/48191#discussion_r1213271430 | ||
| 464 | + ScriptOrigin script_origin(filename, 0, 0, true); | ||
| 465 | + ScriptCompiler::Source script_source(content, script_origin); | ||
| 466 | + MaybeLocal<Function> maybe_fn = | ||
| 467 | + ScriptCompiler::CompileFunction(context, | ||
| 468 | + &script_source, | ||
| 469 | + parameters.size(), | ||
| 470 | + parameters.data(), | ||
| 471 | + 0, | ||
| 472 | + nullptr); | ||
| 467 | 473 | Local<Function> fn; | |
| 468 | - if (!contextify::CompileFunction(context, filename, content, ¶meters) | ||
| 469 | - .ToLocal(&fn)) { | ||
| 474 | + if (!maybe_fn.ToLocal(&fn)) { | ||
| 470 | 475 | return std::nullopt; | |
| 471 | 476 | } | |
| 472 | 477 | ||
| 478 | + // TODO(RaisinTen): Using the V8 code cache prevents us from using `import()` | ||
| 479 | + // in the SEA code. Support it. | ||
| 480 | + // Refs: https://github.com/nodejs/node/pull/48191#discussion_r1213271430 | ||
| 473 | 481 | std::unique_ptr<ScriptCompiler::CachedData> cache{ | |
| 474 | 482 | ScriptCompiler::CreateCodeCacheForFunction(fn)}; | |
| 475 | 483 | std::string code_cache(cache->data, cache->data + cache->length); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -42,8 +42,11 @@ using v8::HandleScope; | |||
| 42 | 42 | using v8::Isolate; | |
| 43 | 43 | using v8::Local; | |
| 44 | 44 | using v8::LocalVector; | |
| 45 | + using v8::MaybeLocal; | ||
| 45 | 46 | using v8::Object; | |
| 46 | 47 | using v8::ObjectTemplate; | |
| 48 | + using v8::ScriptCompiler; | ||
| 49 | + using v8::ScriptOrigin; | ||
| 47 | 50 | using v8::SnapshotCreator; | |
| 48 | 51 | using v8::StartupData; | |
| 49 | 52 | using v8::String; | |
@@ -1487,9 +1490,18 @@ void CompileSerializeMain(const FunctionCallbackInfo<Value>& args) { | |||
| 1487 | 1490 | FIXED_ONE_BYTE_STRING(isolate, "__filename"), | |
| 1488 | 1491 | FIXED_ONE_BYTE_STRING(isolate, "__dirname"), | |
| 1489 | 1492 | }); | |
| 1493 | + | ||
| 1494 | + ScriptOrigin script_origin(filename, 0, 0, true); | ||
| 1495 | + ScriptCompiler::Source script_source(source, script_origin); | ||
| 1496 | + MaybeLocal<Function> maybe_fn = | ||
| 1497 | + ScriptCompiler::CompileFunction(context, | ||
| 1498 | + &script_source, | ||
| 1499 | + parameters.size(), | ||
| 1500 | + parameters.data(), | ||
| 1501 | + 0, | ||
| 1502 | + nullptr); | ||
| 1490 | 1503 | Local<Function> fn; | |
| 1491 | - if (contextify::CompileFunction(context, filename, source, ¶meters) | ||
| 1492 | - .ToLocal(&fn)) { | ||
| 1504 | + if (maybe_fn.ToLocal(&fn)) { | ||
| 1493 | 1505 | args.GetReturnValue().Set(fn); | |
| 1494 | 1506 | } | |
| 1495 | 1507 | } | |
| Back | FazBrowse Home | New Git URL |
0 commit comments