Skip to content

Commit 4884746

Browse files
targosnodejs-github-bot
authored andcommitted
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 <[email protected]> Commit-Queue: Chengzhong Wu (legendecas) <[email protected]> Cr-Commit-Position: refs/heads/main@{#83527} Refs: v8/v8@c3dffe6 PR-URL: #44958 Reviewed-By: Jiawen Geng <[email protected]> Reviewed-By: Beth Griggs <[email protected]> Reviewed-By: Rafael Gonzaga <[email protected]> Reviewed-By: Richard Lau <[email protected]>
1 parent 34ba631 commit 4884746

File tree

5 files changed

+95
-32
lines changed

5 files changed

+95
-32
lines changed

common.gypi

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636

3737
# Reset this number to 0 on major V8 upgrades.
3838
# Increment by one for each non-official patch applied to deps/v8.
39-
'v8_embedder_string': '-node.14',
39+
'v8_embedder_string': '-node.15',
4040

4141
##### V8 defaults for Node.js #####
4242

deps/v8/include/v8-script.h

+9-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,15 @@ class V8_EXPORT UnboundScript {
9292
* A compiled JavaScript module, not yet tied to a Context.
9393
*/
9494
class V8_EXPORT UnboundModuleScript : public Data {
95-
// Only used as a container for code caching.
95+
public:
96+
/**
97+
* Data read from magic sourceURL comments.
98+
*/
99+
Local<Value> GetSourceURL();
100+
/**
101+
* Data read from magic sourceMappingURL comments.
102+
*/
103+
Local<Value> GetSourceMappingURL();
96104
};
97105

98106
/**

deps/v8/src/api/api.cc

+44-16
Original file line numberDiff line numberDiff line change
@@ -1936,8 +1936,32 @@ void ObjectTemplate::SetCodeLike() {
19361936

19371937
// --- S c r i p t s ---
19381938

1939-
// Internally, UnboundScript is a SharedFunctionInfo, and Script is a
1940-
// JSFunction.
1939+
// Internally, UnboundScript and UnboundModuleScript are SharedFunctionInfos,
1940+
// and Script is a JSFunction.
1941+
1942+
namespace {
1943+
inline Local<Value> GetSharedFunctionInfoSourceMappingURL(
1944+
i::Isolate* isolate, i::Handle<i::SharedFunctionInfo> obj) {
1945+
ENTER_V8_NO_SCRIPT_NO_EXCEPTION(isolate);
1946+
if (obj->script().IsScript()) {
1947+
i::Object url = i::Script::cast(obj->script()).source_mapping_url();
1948+
return Utils::ToLocal(i::Handle<i::Object>(url, isolate));
1949+
} else {
1950+
return Local<String>();
1951+
}
1952+
}
1953+
1954+
inline Local<Value> GetSharedFunctionInfoSourceURL(
1955+
i::Isolate* isolate, i::Handle<i::SharedFunctionInfo> obj) {
1956+
ENTER_V8_NO_SCRIPT_NO_EXCEPTION(isolate);
1957+
if (obj->script().IsScript()) {
1958+
i::Object url = i::Script::cast(obj->script()).source_url();
1959+
return Utils::ToLocal(i::Handle<i::Object>(url, isolate));
1960+
} else {
1961+
return Local<String>();
1962+
}
1963+
}
1964+
} // namespace
19411965

19421966
ScriptCompiler::CachedData::CachedData(const uint8_t* data_, int length_,
19431967
BufferPolicy buffer_policy_)
@@ -2022,28 +2046,32 @@ Local<Value> UnboundScript::GetSourceURL() {
20222046
i::Handle<i::SharedFunctionInfo> obj =
20232047
i::Handle<i::SharedFunctionInfo>::cast(Utils::OpenHandle(this));
20242048
i::Isolate* i_isolate = obj->GetIsolate();
2025-
ENTER_V8_NO_SCRIPT_NO_EXCEPTION(i_isolate);
20262049
API_RCS_SCOPE(i_isolate, UnboundScript, GetSourceURL);
2027-
if (obj->script().IsScript()) {
2028-
i::Object url = i::Script::cast(obj->script()).source_url();
2029-
return Utils::ToLocal(i::Handle<i::Object>(url, i_isolate));
2030-
} else {
2031-
return Local<String>();
2032-
}
2050+
return GetSharedFunctionInfoSourceURL(i_isolate, obj);
20332051
}
20342052

20352053
Local<Value> UnboundScript::GetSourceMappingURL() {
20362054
i::Handle<i::SharedFunctionInfo> obj =
20372055
i::Handle<i::SharedFunctionInfo>::cast(Utils::OpenHandle(this));
20382056
i::Isolate* i_isolate = obj->GetIsolate();
20392057
API_RCS_SCOPE(i_isolate, UnboundScript, GetSourceMappingURL);
2040-
ENTER_V8_NO_SCRIPT_NO_EXCEPTION(i_isolate);
2041-
if (obj->script().IsScript()) {
2042-
i::Object url = i::Script::cast(obj->script()).source_mapping_url();
2043-
return Utils::ToLocal(i::Handle<i::Object>(url, i_isolate));
2044-
} else {
2045-
return Local<String>();
2046-
}
2058+
return GetSharedFunctionInfoSourceMappingURL(i_isolate, obj);
2059+
}
2060+
2061+
Local<Value> UnboundModuleScript::GetSourceURL() {
2062+
i::Handle<i::SharedFunctionInfo> obj =
2063+
i::Handle<i::SharedFunctionInfo>::cast(Utils::OpenHandle(this));
2064+
i::Isolate* i_isolate = obj->GetIsolate();
2065+
API_RCS_SCOPE(i_isolate, UnboundModuleScript, GetSourceURL);
2066+
return GetSharedFunctionInfoSourceURL(i_isolate, obj);
2067+
}
2068+
2069+
Local<Value> UnboundModuleScript::GetSourceMappingURL() {
2070+
i::Handle<i::SharedFunctionInfo> obj =
2071+
i::Handle<i::SharedFunctionInfo>::cast(Utils::OpenHandle(this));
2072+
i::Isolate* i_isolate = obj->GetIsolate();
2073+
API_RCS_SCOPE(i_isolate, UnboundModuleScript, GetSourceMappingURL);
2074+
return GetSharedFunctionInfoSourceMappingURL(i_isolate, obj);
20472075
}
20482076

20492077
MaybeLocal<Value> Script::Run(Local<Context> context) {

deps/v8/src/logging/runtime-call-stats.h

+2
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,8 @@ class RuntimeCallTimer final {
278278
V(Uint32Array_New) \
279279
V(Uint8Array_New) \
280280
V(Uint8ClampedArray_New) \
281+
V(UnboundModuleScript_GetSourceMappingURL) \
282+
V(UnboundModuleScript_GetSourceURL) \
281283
V(UnboundScript_GetColumnNumber) \
282284
V(UnboundScript_GetId) \
283285
V(UnboundScript_GetLineNumber) \

deps/v8/test/cctest/test-api.cc

+39-14
Original file line numberDiff line numberDiff line change
@@ -22920,33 +22920,58 @@ TEST(ScriptPositionInfo) {
2292022920
}
2292122921
}
2292222922

22923-
void CheckMagicComments(v8::Isolate* isolate, Local<Script> script,
22923+
template <typename T>
22924+
void CheckMagicComments(v8::Isolate* isolate, Local<T> unbound_script,
2292422925
const char* expected_source_url,
2292522926
const char* expected_source_mapping_url) {
2292622927
if (expected_source_url != nullptr) {
22927-
v8::String::Utf8Value url(isolate,
22928-
script->GetUnboundScript()->GetSourceURL());
22928+
v8::String::Utf8Value url(isolate, unbound_script->GetSourceURL());
2292922929
CHECK_EQ(0, strcmp(expected_source_url, *url));
2293022930
} else {
22931-
CHECK(script->GetUnboundScript()->GetSourceURL()->IsUndefined());
22931+
CHECK(unbound_script->GetSourceURL()->IsUndefined());
2293222932
}
2293322933
if (expected_source_mapping_url != nullptr) {
22934-
v8::String::Utf8Value url(
22935-
isolate, script->GetUnboundScript()->GetSourceMappingURL());
22934+
v8::String::Utf8Value url(isolate, unbound_script->GetSourceMappingURL());
2293622935
CHECK_EQ(0, strcmp(expected_source_mapping_url, *url));
2293722936
} else {
22938-
CHECK(script->GetUnboundScript()->GetSourceMappingURL()->IsUndefined());
22937+
CHECK(unbound_script->GetSourceMappingURL()->IsUndefined());
2293922938
}
2294022939
}
2294122940

22942-
void SourceURLHelper(v8::Isolate* isolate, const char* source,
22941+
void SourceURLHelper(v8::Isolate* isolate, const char* source_text,
2294322942
const char* expected_source_url,
2294422943
const char* expected_source_mapping_url) {
22945-
Local<Script> script = v8_compile(source);
22946-
CheckMagicComments(isolate, script, expected_source_url,
22947-
expected_source_mapping_url);
22948-
}
22944+
// Check scripts
22945+
{
22946+
Local<Script> script = v8_compile(source_text);
22947+
CheckMagicComments(isolate, script->GetUnboundScript(), expected_source_url,
22948+
expected_source_mapping_url);
22949+
}
2294922950

22951+
// Check modules
22952+
{
22953+
Local<v8::String> source_str = v8_str(source_text);
22954+
// Set a different resource name with the case above to invalidate the
22955+
// cache.
22956+
v8::ScriptOrigin origin(isolate,
22957+
v8_str("module.js"), // resource name
22958+
0, // line offset
22959+
0, // column offset
22960+
true, // is cross origin
22961+
-1, // script id
22962+
Local<Value>(), // source map URL
22963+
false, // is opaque
22964+
false, // is WASM
22965+
true // is ES Module
22966+
);
22967+
v8::ScriptCompiler::Source source(source_str, origin, nullptr);
22968+
22969+
Local<v8::Module> module =
22970+
v8::ScriptCompiler::CompileModule(isolate, &source).ToLocalChecked();
22971+
CheckMagicComments(isolate, module->GetUnboundModuleScript(),
22972+
expected_source_url, expected_source_mapping_url);
22973+
}
22974+
}
2295022975

2295122976
TEST(ScriptSourceURLAndSourceMappingURL) {
2295222977
LocalContext env;
@@ -23245,8 +23270,8 @@ void RunStreamingTest(const char** chunks, v8::ScriptType type,
2324523270
script.ToLocalChecked()->Run(env.local()).ToLocalChecked());
2324623271
// All scripts are supposed to return the fixed value 13 when ran.
2324723272
CHECK_EQ(13, result->Int32Value(env.local()).FromJust());
23248-
CheckMagicComments(isolate, script.ToLocalChecked(), expected_source_url,
23249-
expected_source_mapping_url);
23273+
CheckMagicComments(isolate, script.ToLocalChecked()->GetUnboundScript(),
23274+
expected_source_url, expected_source_mapping_url);
2325023275
} else {
2325123276
CHECK(script.IsEmpty());
2325223277
}

0 commit comments

Comments
 (0)