From 488474618cd45f33e0dcfc2f29a516ab9caa8006 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C3=ABl=20Zasso?= Date: Tue, 11 Oct 2022 09:07:46 +0200 Subject: [PATCH] deps: V8: cherry-pick c3dffe6e2bda Original commit message: [api] Expose parsed module source map urls Source map urls can be parsed from the magic comments. Expose them with public apis on the UnboundModuleScript, similar to the UnboundScript. Change-Id: Ia5dfdc8ff25f825c9fa7d241d0d79ba20028586b Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/3917379 Reviewed-by: Camillo Bruni Commit-Queue: Chengzhong Wu (legendecas) Cr-Commit-Position: refs/heads/main@{#83527} Refs: https://github.com/v8/v8/commit/c3dffe6e2bdab79bb5bfb94be9fcc6a16adf6cf5 PR-URL: https://github.com/nodejs/node/pull/44958 Reviewed-By: Jiawen Geng Reviewed-By: Beth Griggs Reviewed-By: Rafael Gonzaga Reviewed-By: Richard Lau --- common.gypi | 2 +- deps/v8/include/v8-script.h | 10 +++- deps/v8/src/api/api.cc | 60 +++++++++++++++++------- deps/v8/src/logging/runtime-call-stats.h | 2 + deps/v8/test/cctest/test-api.cc | 53 +++++++++++++++------ 5 files changed, 95 insertions(+), 32 deletions(-) diff --git a/common.gypi b/common.gypi index 2a06e7d1401df0..ccfd1bccb167d7 100644 --- a/common.gypi +++ b/common.gypi @@ -36,7 +36,7 @@ # Reset this number to 0 on major V8 upgrades. # Increment by one for each non-official patch applied to deps/v8. - 'v8_embedder_string': '-node.14', + 'v8_embedder_string': '-node.15', ##### V8 defaults for Node.js ##### diff --git a/deps/v8/include/v8-script.h b/deps/v8/include/v8-script.h index dbd98ed55b4ce0..e2ba845268d921 100644 --- a/deps/v8/include/v8-script.h +++ b/deps/v8/include/v8-script.h @@ -92,7 +92,15 @@ class V8_EXPORT UnboundScript { * A compiled JavaScript module, not yet tied to a Context. */ class V8_EXPORT UnboundModuleScript : public Data { - // Only used as a container for code caching. + public: + /** + * Data read from magic sourceURL comments. + */ + Local GetSourceURL(); + /** + * Data read from magic sourceMappingURL comments. + */ + Local GetSourceMappingURL(); }; /** diff --git a/deps/v8/src/api/api.cc b/deps/v8/src/api/api.cc index 608da3369fabaf..cf755cafc2cb41 100644 --- a/deps/v8/src/api/api.cc +++ b/deps/v8/src/api/api.cc @@ -1936,8 +1936,32 @@ void ObjectTemplate::SetCodeLike() { // --- S c r i p t s --- -// Internally, UnboundScript is a SharedFunctionInfo, and Script is a -// JSFunction. +// Internally, UnboundScript and UnboundModuleScript are SharedFunctionInfos, +// and Script is a JSFunction. + +namespace { +inline Local GetSharedFunctionInfoSourceMappingURL( + i::Isolate* isolate, i::Handle obj) { + ENTER_V8_NO_SCRIPT_NO_EXCEPTION(isolate); + if (obj->script().IsScript()) { + i::Object url = i::Script::cast(obj->script()).source_mapping_url(); + return Utils::ToLocal(i::Handle(url, isolate)); + } else { + return Local(); + } +} + +inline Local GetSharedFunctionInfoSourceURL( + i::Isolate* isolate, i::Handle obj) { + ENTER_V8_NO_SCRIPT_NO_EXCEPTION(isolate); + if (obj->script().IsScript()) { + i::Object url = i::Script::cast(obj->script()).source_url(); + return Utils::ToLocal(i::Handle(url, isolate)); + } else { + return Local(); + } +} +} // namespace ScriptCompiler::CachedData::CachedData(const uint8_t* data_, int length_, BufferPolicy buffer_policy_) @@ -2022,14 +2046,8 @@ Local UnboundScript::GetSourceURL() { i::Handle obj = i::Handle::cast(Utils::OpenHandle(this)); i::Isolate* i_isolate = obj->GetIsolate(); - ENTER_V8_NO_SCRIPT_NO_EXCEPTION(i_isolate); API_RCS_SCOPE(i_isolate, UnboundScript, GetSourceURL); - if (obj->script().IsScript()) { - i::Object url = i::Script::cast(obj->script()).source_url(); - return Utils::ToLocal(i::Handle(url, i_isolate)); - } else { - return Local(); - } + return GetSharedFunctionInfoSourceURL(i_isolate, obj); } Local UnboundScript::GetSourceMappingURL() { @@ -2037,13 +2055,23 @@ Local UnboundScript::GetSourceMappingURL() { i::Handle::cast(Utils::OpenHandle(this)); i::Isolate* i_isolate = obj->GetIsolate(); API_RCS_SCOPE(i_isolate, UnboundScript, GetSourceMappingURL); - ENTER_V8_NO_SCRIPT_NO_EXCEPTION(i_isolate); - if (obj->script().IsScript()) { - i::Object url = i::Script::cast(obj->script()).source_mapping_url(); - return Utils::ToLocal(i::Handle(url, i_isolate)); - } else { - return Local(); - } + return GetSharedFunctionInfoSourceMappingURL(i_isolate, obj); +} + +Local UnboundModuleScript::GetSourceURL() { + i::Handle obj = + i::Handle::cast(Utils::OpenHandle(this)); + i::Isolate* i_isolate = obj->GetIsolate(); + API_RCS_SCOPE(i_isolate, UnboundModuleScript, GetSourceURL); + return GetSharedFunctionInfoSourceURL(i_isolate, obj); +} + +Local UnboundModuleScript::GetSourceMappingURL() { + i::Handle obj = + i::Handle::cast(Utils::OpenHandle(this)); + i::Isolate* i_isolate = obj->GetIsolate(); + API_RCS_SCOPE(i_isolate, UnboundModuleScript, GetSourceMappingURL); + return GetSharedFunctionInfoSourceMappingURL(i_isolate, obj); } MaybeLocal Script::Run(Local context) { diff --git a/deps/v8/src/logging/runtime-call-stats.h b/deps/v8/src/logging/runtime-call-stats.h index e6998d4bfb1330..4c02309b7493dd 100644 --- a/deps/v8/src/logging/runtime-call-stats.h +++ b/deps/v8/src/logging/runtime-call-stats.h @@ -278,6 +278,8 @@ class RuntimeCallTimer final { V(Uint32Array_New) \ V(Uint8Array_New) \ V(Uint8ClampedArray_New) \ + V(UnboundModuleScript_GetSourceMappingURL) \ + V(UnboundModuleScript_GetSourceURL) \ V(UnboundScript_GetColumnNumber) \ V(UnboundScript_GetId) \ V(UnboundScript_GetLineNumber) \ diff --git a/deps/v8/test/cctest/test-api.cc b/deps/v8/test/cctest/test-api.cc index 759851f6997596..51294fccc8d888 100644 --- a/deps/v8/test/cctest/test-api.cc +++ b/deps/v8/test/cctest/test-api.cc @@ -22920,33 +22920,58 @@ TEST(ScriptPositionInfo) { } } -void CheckMagicComments(v8::Isolate* isolate, Local