Skip to content

Commit 3a4fe77

Browse files
author
Jan Krems
committed
deps: cherry-pick dbfe4a49d8 from upstream V8
Original commit message: Introduce ScriptOrModule and HostDefinedOptions This patch introduces a new container type ScriptOrModule which provides the name and the host defined options of the script/module. This patch also introduces a new PrimitivesArray that can hold Primitive values, which the embedder can use to store metadata. The HostDefinedOptions is passed to V8 through the ScriptOrigin, and passed back to the embedder through HostImportModuleDynamically for module loading. Bug: v8:5785, v8:6658, v8:6683 Cq-Include-Trybots: master.tryserver.chromium.linux:linux_chromium_rel_ng Change-Id: I56c26fc9a680b273ac0a6691e5ad75f15b8dc80a Reviewed-on: https://chromium-review.googlesource.com/622158 Reviewed-by: Adam Klein <[email protected]> Reviewed-by: Georg Neis <[email protected]> Commit-Queue: Sathya Gunasekaran <[email protected]> Cr-Commit-Position: refs/heads/master@{#47724} PR-URL: #16889 Refs: v8/v8@dbfe4a4 Refs: #15713 Reviewed-By: Ben Noordhuis <[email protected]> Reviewed-By: Michaël Zasso <[email protected]>
1 parent 29423b4 commit 3a4fe77

File tree

20 files changed

+268
-57
lines changed

20 files changed

+268
-57
lines changed

common.gypi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727

2828
# Reset this number to 0 on major V8 upgrades.
2929
# Increment by one for each non-official patch applied to deps/v8.
30-
'v8_embedder_string': '-node.11',
30+
'v8_embedder_string': '-node.12',
3131

3232
# Enable disassembler for `--print-code` v8 options
3333
'v8_enable_disassembler': 1,

deps/v8/include/v8.h

Lines changed: 60 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ class String;
104104
class StringObject;
105105
class Symbol;
106106
class SymbolObject;
107+
class PrimitiveArray;
107108
class Private;
108109
class Uint32;
109110
class Utils;
@@ -978,6 +979,48 @@ class V8_EXPORT Data {
978979
Data();
979980
};
980981

982+
/**
983+
* This is an unfinished experimental feature, and is only exposed
984+
* here for internal testing purposes. DO NOT USE.
985+
*
986+
* A container type that holds relevant metadata for module loading.
987+
*
988+
* This is passed back to the embedder as part of
989+
* HostImportDynamicallyCallback for module loading.
990+
*/
991+
class V8_EXPORT ScriptOrModule {
992+
public:
993+
/**
994+
* The name that was passed by the embedder as ResourceName to the
995+
* ScriptOrigin. This can be either a v8::String or v8::Undefined.
996+
*/
997+
Local<Value> GetResourceName();
998+
999+
/**
1000+
* The options that were passed by the embedder as HostDefinedOptions to
1001+
* the ScriptOrigin.
1002+
*/
1003+
Local<PrimitiveArray> GetHostDefinedOptions();
1004+
};
1005+
1006+
/**
1007+
* This is an unfinished experimental feature, and is only exposed
1008+
* here for internal testing purposes. DO NOT USE.
1009+
*
1010+
* An array to hold Primitive values. This is used by the embedder to
1011+
* pass host defined options to the ScriptOptions during compilation.
1012+
*
1013+
* This is passed back to the embedder as part of
1014+
* HostImportDynamicallyCallback for module loading.
1015+
*
1016+
*/
1017+
class V8_EXPORT PrimitiveArray {
1018+
public:
1019+
static Local<PrimitiveArray> New(Isolate* isolate, int length);
1020+
int Length() const;
1021+
void Set(int index, Local<Primitive> item);
1022+
Local<Primitive> Get(int index);
1023+
};
9811024

9821025
/**
9831026
* The optional attributes of ScriptOrigin.
@@ -1027,13 +1070,15 @@ class ScriptOrigin {
10271070
Local<Value> source_map_url = Local<Value>(),
10281071
Local<Boolean> resource_is_opaque = Local<Boolean>(),
10291072
Local<Boolean> is_wasm = Local<Boolean>(),
1030-
Local<Boolean> is_module = Local<Boolean>());
1073+
Local<Boolean> is_module = Local<Boolean>(),
1074+
Local<PrimitiveArray> host_defined_options = Local<PrimitiveArray>());
10311075

10321076
V8_INLINE Local<Value> ResourceName() const;
10331077
V8_INLINE Local<Integer> ResourceLineOffset() const;
10341078
V8_INLINE Local<Integer> ResourceColumnOffset() const;
10351079
V8_INLINE Local<Integer> ScriptID() const;
10361080
V8_INLINE Local<Value> SourceMapUrl() const;
1081+
V8_INLINE Local<PrimitiveArray> HostDefinedOptions() const;
10371082
V8_INLINE ScriptOriginOptions Options() const { return options_; }
10381083

10391084
private:
@@ -1043,6 +1088,7 @@ class ScriptOrigin {
10431088
ScriptOriginOptions options_;
10441089
Local<Integer> script_id_;
10451090
Local<Value> source_map_url_;
1091+
Local<PrimitiveArray> host_defined_options_;
10461092
};
10471093

10481094
/**
@@ -1289,6 +1335,7 @@ class V8_EXPORT ScriptCompiler {
12891335
Local<Integer> resource_column_offset;
12901336
ScriptOriginOptions resource_options;
12911337
Local<Value> source_map_url;
1338+
Local<PrimitiveArray> host_defined_options;
12921339

12931340
// Cached data from previous compilation (if a kConsume*Cache flag is
12941341
// set), or hold newly generated cache data (kProduce*Cache flags) are
@@ -6209,8 +6256,8 @@ typedef void (*DeprecatedCallCompletedCallback)();
62096256
* embedder to load a module. This is used as part of the dynamic
62106257
* import syntax.
62116258
*
6212-
* The referrer is the name of the file which calls the dynamic
6213-
* import. The referrer can be used to resolve the module location.
6259+
* The referrer contains metadata about the script/module that calls
6260+
* import.
62146261
*
62156262
* The specifier is the name of the module that should be imported.
62166263
*
@@ -6225,7 +6272,8 @@ typedef void (*DeprecatedCallCompletedCallback)();
62256272
* that exception by returning an empty MaybeLocal.
62266273
*/
62276274
typedef MaybeLocal<Promise> (*HostImportModuleDynamicallyCallback)(
6228-
Local<Context> context, Local<String> referrer, Local<String> specifier);
6275+
Local<Context> context, Local<ScriptOrModule> referrer,
6276+
Local<String> specifier);
62296277

62306278
/**
62316279
* PromiseHook with type kInit is called when a new promise is
@@ -9545,7 +9593,8 @@ ScriptOrigin::ScriptOrigin(Local<Value> resource_name,
95459593
Local<Integer> script_id,
95469594
Local<Value> source_map_url,
95479595
Local<Boolean> resource_is_opaque,
9548-
Local<Boolean> is_wasm, Local<Boolean> is_module)
9596+
Local<Boolean> is_wasm, Local<Boolean> is_module,
9597+
Local<PrimitiveArray> host_defined_options)
95499598
: resource_name_(resource_name),
95509599
resource_line_offset_(resource_line_offset),
95519600
resource_column_offset_(resource_column_offset),
@@ -9555,10 +9604,14 @@ ScriptOrigin::ScriptOrigin(Local<Value> resource_name,
95559604
!is_wasm.IsEmpty() && is_wasm->IsTrue(),
95569605
!is_module.IsEmpty() && is_module->IsTrue()),
95579606
script_id_(script_id),
9558-
source_map_url_(source_map_url) {}
9607+
source_map_url_(source_map_url),
9608+
host_defined_options_(host_defined_options) {}
95599609

95609610
Local<Value> ScriptOrigin::ResourceName() const { return resource_name_; }
95619611

9612+
Local<PrimitiveArray> ScriptOrigin::HostDefinedOptions() const {
9613+
return host_defined_options_;
9614+
}
95629615

95639616
Local<Integer> ScriptOrigin::ResourceLineOffset() const {
95649617
return resource_line_offset_;
@@ -9575,7 +9628,6 @@ Local<Integer> ScriptOrigin::ScriptID() const { return script_id_; }
95759628

95769629
Local<Value> ScriptOrigin::SourceMapUrl() const { return source_map_url_; }
95779630

9578-
95799631
ScriptCompiler::Source::Source(Local<String> string, const ScriptOrigin& origin,
95809632
CachedData* data)
95819633
: source_string(string),
@@ -9584,9 +9636,9 @@ ScriptCompiler::Source::Source(Local<String> string, const ScriptOrigin& origin,
95849636
resource_column_offset(origin.ResourceColumnOffset()),
95859637
resource_options(origin.Options()),
95869638
source_map_url(origin.SourceMapUrl()),
9639+
host_defined_options(origin.HostDefinedOptions()),
95879640
cached_data(data) {}
95889641

9589-
95909642
ScriptCompiler::Source::Source(Local<String> string,
95919643
CachedData* data)
95929644
: source_string(string), cached_data(data) {}

deps/v8/src/api.cc

Lines changed: 71 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,8 @@ static ScriptOrigin GetScriptOriginForScript(i::Isolate* isolate,
278278
i::Handle<i::Script> script) {
279279
i::Handle<i::Object> scriptName(script->GetNameOrSourceURL(), isolate);
280280
i::Handle<i::Object> source_map_url(script->source_mapping_url(), isolate);
281+
i::Handle<i::FixedArray> host_defined_options(script->host_defined_options(),
282+
isolate);
281283
v8::Isolate* v8_isolate =
282284
reinterpret_cast<v8::Isolate*>(script->GetIsolate());
283285
ScriptOriginOptions options(script->origin_options());
@@ -290,7 +292,8 @@ static ScriptOrigin GetScriptOriginForScript(i::Isolate* isolate,
290292
Utils::ToLocal(source_map_url),
291293
v8::Boolean::New(v8_isolate, options.IsOpaque()),
292294
v8::Boolean::New(v8_isolate, script->type() == i::Script::TYPE_WASM),
293-
v8::Boolean::New(v8_isolate, options.IsModule()));
295+
v8::Boolean::New(v8_isolate, options.IsModule()),
296+
Utils::ToLocal(host_defined_options));
294297
return origin;
295298
}
296299

@@ -2082,13 +2085,68 @@ Local<Value> Script::Run() {
20822085
RETURN_TO_LOCAL_UNCHECKED(Run(context), Value);
20832086
}
20842087

2088+
Local<Value> ScriptOrModule::GetResourceName() {
2089+
i::Handle<i::Script> obj = Utils::OpenHandle(this);
2090+
i::Isolate* isolate = obj->GetIsolate();
2091+
ENTER_V8_NO_SCRIPT_NO_EXCEPTION(isolate);
2092+
i::Handle<i::Object> val(obj->name(), isolate);
2093+
return ToApiHandle<Value>(val);
2094+
}
2095+
2096+
Local<PrimitiveArray> ScriptOrModule::GetHostDefinedOptions() {
2097+
i::Handle<i::Script> obj = Utils::OpenHandle(this);
2098+
i::Isolate* isolate = obj->GetIsolate();
2099+
ENTER_V8_NO_SCRIPT_NO_EXCEPTION(isolate);
2100+
i::Handle<i::FixedArray> val(obj->host_defined_options(), isolate);
2101+
return ToApiHandle<PrimitiveArray>(val);
2102+
}
20852103

20862104
Local<UnboundScript> Script::GetUnboundScript() {
20872105
i::Handle<i::Object> obj = Utils::OpenHandle(this);
20882106
return ToApiHandle<UnboundScript>(
20892107
i::Handle<i::SharedFunctionInfo>(i::JSFunction::cast(*obj)->shared()));
20902108
}
20912109

2110+
// static
2111+
Local<PrimitiveArray> PrimitiveArray::New(Isolate* v8_isolate, int length) {
2112+
i::Isolate* isolate = reinterpret_cast<i::Isolate*>(v8_isolate);
2113+
ENTER_V8_NO_SCRIPT_NO_EXCEPTION(isolate);
2114+
Utils::ApiCheck(length >= 0, "v8::PrimitiveArray::New",
2115+
"length must be equal or greater than zero");
2116+
i::Handle<i::FixedArray> array = isolate->factory()->NewFixedArray(length);
2117+
return ToApiHandle<PrimitiveArray>(array);
2118+
}
2119+
2120+
int PrimitiveArray::Length() const {
2121+
i::Handle<i::FixedArray> array = Utils::OpenHandle(this);
2122+
i::Isolate* isolate = array->GetIsolate();
2123+
ENTER_V8_NO_SCRIPT_NO_EXCEPTION(isolate);
2124+
return array->length();
2125+
}
2126+
2127+
void PrimitiveArray::Set(int index, Local<Primitive> item) {
2128+
i::Handle<i::FixedArray> array = Utils::OpenHandle(this);
2129+
i::Isolate* isolate = array->GetIsolate();
2130+
ENTER_V8_NO_SCRIPT_NO_EXCEPTION(isolate);
2131+
Utils::ApiCheck(index >= 0 && index < array->length(),
2132+
"v8::PrimitiveArray::Set",
2133+
"index must be greater than or equal to 0 and less than the "
2134+
"array length");
2135+
i::Handle<i::Object> i_item = Utils::OpenHandle(*item);
2136+
array->set(index, *i_item);
2137+
}
2138+
2139+
Local<Primitive> PrimitiveArray::Get(int index) {
2140+
i::Handle<i::FixedArray> array = Utils::OpenHandle(this);
2141+
i::Isolate* isolate = array->GetIsolate();
2142+
ENTER_V8_NO_SCRIPT_NO_EXCEPTION(isolate);
2143+
Utils::ApiCheck(index >= 0 && index < array->length(),
2144+
"v8::PrimitiveArray::Get",
2145+
"index must be greater than or equal to 0 and less than the "
2146+
"array length");
2147+
i::Handle<i::Object> i_item(array->get(index), isolate);
2148+
return ToApiHandle<Primitive>(i_item);
2149+
}
20922150

20932151
Module::Status Module::GetStatus() const {
20942152
i::Handle<i::Module> self = Utils::OpenHandle(this);
@@ -2225,11 +2283,16 @@ MaybeLocal<UnboundScript> ScriptCompiler::CompileUnboundInternal(
22252283
TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("v8.compile"), "V8.CompileScript");
22262284
i::Handle<i::Object> name_obj;
22272285
i::Handle<i::Object> source_map_url;
2286+
i::Handle<i::FixedArray> host_defined_options =
2287+
isolate->factory()->empty_fixed_array();
22282288
int line_offset = 0;
22292289
int column_offset = 0;
22302290
if (!source->resource_name.IsEmpty()) {
22312291
name_obj = Utils::OpenHandle(*(source->resource_name));
22322292
}
2293+
if (!source->host_defined_options.IsEmpty()) {
2294+
host_defined_options = Utils::OpenHandle(*(source->host_defined_options));
2295+
}
22332296
if (!source->resource_line_offset.IsEmpty()) {
22342297
line_offset = static_cast<int>(source->resource_line_offset->Value());
22352298
}
@@ -2243,7 +2306,7 @@ MaybeLocal<UnboundScript> ScriptCompiler::CompileUnboundInternal(
22432306
result = i::Compiler::GetSharedFunctionInfoForScript(
22442307
str, name_obj, line_offset, column_offset, source->resource_options,
22452308
source_map_url, isolate->native_context(), NULL, &script_data, options,
2246-
i::NOT_NATIVES_CODE);
2309+
i::NOT_NATIVES_CODE, host_defined_options);
22472310
has_pending_exception = result.is_null();
22482311
if (has_pending_exception && script_data != NULL) {
22492312
// This case won't happen during normal operation; we have compiled
@@ -2508,6 +2571,10 @@ MaybeLocal<Script> ScriptCompiler::Compile(Local<Context> context,
25082571
if (!origin.ResourceName().IsEmpty()) {
25092572
script->set_name(*Utils::OpenHandle(*(origin.ResourceName())));
25102573
}
2574+
if (!origin.HostDefinedOptions().IsEmpty()) {
2575+
script->set_host_defined_options(
2576+
*Utils::OpenHandle(*(origin.HostDefinedOptions())));
2577+
}
25112578
if (!origin.ResourceLineOffset().IsEmpty()) {
25122579
script->set_line_offset(
25132580
static_cast<int>(origin.ResourceLineOffset()->Value()));
@@ -9828,7 +9895,8 @@ MaybeLocal<UnboundScript> debug::CompileInspectorScript(Isolate* v8_isolate,
98289895
i::Handle<i::Object>(), isolate->native_context(), NULL, &script_data,
98299896
ScriptCompiler::kNoCompileOptions,
98309897
i::FLAG_expose_inspector_scripts ? i::NOT_NATIVES_CODE
9831-
: i::INSPECTOR_CODE);
9898+
: i::INSPECTOR_CODE,
9899+
i::Handle<i::FixedArray>());
98329900
has_pending_exception = result.is_null();
98339901
RETURN_ON_FAILED_EXECUTION(UnboundScript);
98349902
}

deps/v8/src/api.h

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,10 @@ class RegisteredExtension {
111111
V(NativeWeakMap, JSWeakMap) \
112112
V(debug::GeneratorObject, JSGeneratorObject) \
113113
V(debug::Script, Script) \
114-
V(Promise, JSPromise)
114+
V(Promise, JSPromise) \
115+
V(Primitive, Object) \
116+
V(PrimitiveArray, FixedArray) \
117+
V(ScriptOrModule, Script)
115118

116119
class Utils {
117120
public:
@@ -209,6 +212,12 @@ class Utils {
209212
v8::internal::Handle<v8::internal::JSWeakMap> obj);
210213
static inline Local<Function> CallableToLocal(
211214
v8::internal::Handle<v8::internal::JSReceiver> obj);
215+
static inline Local<Primitive> ToLocalPrimitive(
216+
v8::internal::Handle<v8::internal::Object> obj);
217+
static inline Local<PrimitiveArray> ToLocal(
218+
v8::internal::Handle<v8::internal::FixedArray> obj);
219+
static inline Local<ScriptOrModule> ScriptOrModuleToLocal(
220+
v8::internal::Handle<v8::internal::Script> obj);
212221

213222
#define DECLARE_OPEN_HANDLE(From, To) \
214223
static inline v8::internal::Handle<v8::internal::To> \
@@ -325,6 +334,9 @@ MAKE_TO_LOCAL(Uint32ToLocal, Object, Uint32)
325334
MAKE_TO_LOCAL(ExternalToLocal, JSObject, External)
326335
MAKE_TO_LOCAL(NativeWeakMapToLocal, JSWeakMap, NativeWeakMap)
327336
MAKE_TO_LOCAL(CallableToLocal, JSReceiver, Function)
337+
MAKE_TO_LOCAL(ToLocalPrimitive, Object, Primitive)
338+
MAKE_TO_LOCAL(ToLocal, FixedArray, PrimitiveArray)
339+
MAKE_TO_LOCAL(ScriptOrModuleToLocal, Script, ScriptOrModule)
328340

329341
#undef MAKE_TO_LOCAL_TYPED_ARRAY
330342
#undef MAKE_TO_LOCAL

deps/v8/src/bootstrapper.cc

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3539,7 +3539,8 @@ bool Bootstrapper::CompileNative(Isolate* isolate, Vector<const char> name,
35393539
Handle<SharedFunctionInfo> function_info =
35403540
Compiler::GetSharedFunctionInfoForScript(
35413541
source, script_name, 0, 0, ScriptOriginOptions(), Handle<Object>(),
3542-
context, NULL, NULL, ScriptCompiler::kNoCompileOptions, natives_flag);
3542+
context, NULL, NULL, ScriptCompiler::kNoCompileOptions, natives_flag,
3543+
Handle<FixedArray>());
35433544
if (function_info.is_null()) return false;
35443545

35453546
DCHECK(context->IsNativeContext());
@@ -3602,7 +3603,7 @@ bool Genesis::CompileExtension(Isolate* isolate, v8::Extension* extension) {
36023603
function_info = Compiler::GetSharedFunctionInfoForScript(
36033604
source, script_name, 0, 0, ScriptOriginOptions(), Handle<Object>(),
36043605
context, extension, NULL, ScriptCompiler::kNoCompileOptions,
3605-
EXTENSION_CODE);
3606+
EXTENSION_CODE, Handle<FixedArray>());
36063607
if (function_info.is_null()) return false;
36073608
cache->Add(name, function_info);
36083609
}

deps/v8/src/compiler.cc

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1192,7 +1192,8 @@ Handle<SharedFunctionInfo> Compiler::GetSharedFunctionInfoForScript(
11921192
int column_offset, ScriptOriginOptions resource_options,
11931193
Handle<Object> source_map_url, Handle<Context> context,
11941194
v8::Extension* extension, ScriptData** cached_data,
1195-
ScriptCompiler::CompileOptions compile_options, NativesFlag natives) {
1195+
ScriptCompiler::CompileOptions compile_options, NativesFlag natives,
1196+
Handle<FixedArray> host_defined_options) {
11961197
Isolate* isolate = source->GetIsolate();
11971198
if (compile_options == ScriptCompiler::kNoCompileOptions) {
11981199
cached_data = NULL;
@@ -1288,6 +1289,9 @@ Handle<SharedFunctionInfo> Compiler::GetSharedFunctionInfoForScript(
12881289
if (!source_map_url.is_null()) {
12891290
script->set_source_mapping_url(*source_map_url);
12901291
}
1292+
if (!host_defined_options.is_null()) {
1293+
script->set_host_defined_options(*host_defined_options);
1294+
}
12911295

12921296
// Compile the function and add it to the cache.
12931297
ParseInfo parse_info(script);

deps/v8/src/compiler.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ class V8_EXPORT_PRIVATE Compiler : public AllStatic {
113113
Handle<Object> source_map_url, Handle<Context> context,
114114
v8::Extension* extension, ScriptData** cached_data,
115115
ScriptCompiler::CompileOptions compile_options,
116-
NativesFlag is_natives_code);
116+
NativesFlag is_natives_code, Handle<FixedArray> host_defined_options);
117117

118118
// Create a shared function info object for a Script that has already been
119119
// parsed while the script was being loaded from a streamed source.

0 commit comments

Comments
 (0)