From b0f5abbea372b0cc4d27f267de271766f5c8a11a Mon Sep 17 00:00:00 2001 From: neis Date: Wed, 18 Jan 2017 22:59:20 -0800 Subject: [PATCH] [modules] Add an IsModule flag to ScriptOriginOptions. Since the script origin is part of the key used in the compilation cache, this ensures that the cache never confuses a module with a non-module script. BUG=v8:1569,v8:5685 Review-Url: https://codereview.chromium.org/2611643002 Cr-Commit-Position: refs/heads/master@{#42490} --- include/v8.h | 34 ++++++++++++++----- src/api.cc | 25 ++++++++------ src/bootstrapper.cc | 5 ++- src/compiler.cc | 5 ++- src/compiler.h | 2 +- src/d8.cc | 4 ++- src/objects.h | 2 +- test/cctest/compiler/test-linkage.cc | 2 +- .../bytecode-expectations-printer.cc | 6 +++- test/cctest/test-compiler.cc | 2 +- test/cctest/test-modules.cc | 16 ++++++--- test/cctest/test-serialize.cc | 5 ++- 12 files changed, 70 insertions(+), 38 deletions(-) diff --git a/include/v8.h b/include/v8.h index 2f90933772..3ac47f8a89 100644 --- a/include/v8.h +++ b/include/v8.h @@ -962,20 +962,31 @@ class V8_EXPORT Data { class ScriptOriginOptions { public: V8_INLINE ScriptOriginOptions(bool is_shared_cross_origin = false, - bool is_opaque = false, bool is_wasm = false) + bool is_opaque = false, bool is_wasm = false, + bool is_module = false) : flags_((is_shared_cross_origin ? kIsSharedCrossOrigin : 0) | - (is_wasm ? kIsWasm : 0) | (is_opaque ? kIsOpaque : 0)) {} + (is_wasm ? kIsWasm : 0) | (is_opaque ? kIsOpaque : 0) | + (is_module ? kIsModule : 0)) {} V8_INLINE ScriptOriginOptions(int flags) - : flags_(flags & (kIsSharedCrossOrigin | kIsOpaque | kIsWasm)) {} + : flags_(flags & + (kIsSharedCrossOrigin | kIsOpaque | kIsWasm | kIsModule)) {} + bool IsSharedCrossOrigin() const { return (flags_ & kIsSharedCrossOrigin) != 0; } bool IsOpaque() const { return (flags_ & kIsOpaque) != 0; } bool IsWasm() const { return (flags_ & kIsWasm) != 0; } + bool IsModule() const { return (flags_ & kIsModule) != 0; } + int Flags() const { return flags_; } private: - enum { kIsSharedCrossOrigin = 1, kIsOpaque = 1 << 1, kIsWasm = 1 << 2 }; + enum { + kIsSharedCrossOrigin = 1, + kIsOpaque = 1 << 1, + kIsWasm = 1 << 2, + kIsModule = 1 << 3 + }; const int flags_; }; @@ -992,7 +1003,8 @@ class ScriptOrigin { Local script_id = Local(), Local source_map_url = Local(), Local resource_is_opaque = Local(), - Local is_wasm = Local()); + Local is_wasm = Local(), + Local is_module = Local()); V8_INLINE Local ResourceName() const; V8_INLINE Local ResourceLineOffset() const; @@ -1183,6 +1195,8 @@ class V8_EXPORT ScriptCompiler { // alive. V8_INLINE const CachedData* GetCachedData() const; + V8_INLINE const ScriptOriginOptions& GetResourceOptions() const; + // Prevent copying. Source(const Source&) = delete; Source& operator=(const Source&) = delete; @@ -1425,7 +1439,7 @@ class V8_EXPORT ScriptCompiler { private: static V8_WARN_UNUSED_RESULT MaybeLocal CompileUnboundInternal( - Isolate* isolate, Source* source, CompileOptions options, bool is_module); + Isolate* isolate, Source* source, CompileOptions options); }; @@ -8987,14 +9001,15 @@ ScriptOrigin::ScriptOrigin(Local resource_name, Local script_id, Local source_map_url, Local resource_is_opaque, - Local is_wasm) + Local is_wasm, Local is_module) : resource_name_(resource_name), resource_line_offset_(resource_line_offset), resource_column_offset_(resource_column_offset), options_(!resource_is_shared_cross_origin.IsEmpty() && resource_is_shared_cross_origin->IsTrue(), !resource_is_opaque.IsEmpty() && resource_is_opaque->IsTrue(), - !is_wasm.IsEmpty() && is_wasm->IsTrue()), + !is_wasm.IsEmpty() && is_wasm->IsTrue(), + !is_module.IsEmpty() && is_module->IsTrue()), script_id_(script_id), source_map_url_(source_map_url) {} @@ -9043,6 +9058,9 @@ const ScriptCompiler::CachedData* ScriptCompiler::Source::GetCachedData() return cached_data; } +const ScriptOriginOptions& ScriptCompiler::Source::GetResourceOptions() const { + return resource_options; +} Local Boolean::New(Isolate* isolate, bool value) { return value ? True(isolate) : False(isolate); diff --git a/src/api.cc b/src/api.cc index a2cfe61a8d..d86739ff23 100644 --- a/src/api.cc +++ b/src/api.cc @@ -283,7 +283,8 @@ static ScriptOrigin GetScriptOriginForScript(i::Isolate* isolate, v8::Integer::New(v8_isolate, script->id()), Utils::ToLocal(source_map_url), v8::Boolean::New(v8_isolate, options.IsOpaque()), - v8::Boolean::New(v8_isolate, script->type() == i::Script::TYPE_WASM)); + v8::Boolean::New(v8_isolate, script->type() == i::Script::TYPE_WASM), + v8::Boolean::New(v8_isolate, options.IsModule())); return origin; } @@ -2103,8 +2104,7 @@ MaybeLocal Module::Evaluate(Local context) { } MaybeLocal ScriptCompiler::CompileUnboundInternal( - Isolate* v8_isolate, Source* source, CompileOptions options, - bool is_module) { + Isolate* v8_isolate, Source* source, CompileOptions options) { i::Isolate* isolate = reinterpret_cast(v8_isolate); PREPARE_FOR_EXECUTION_WITH_ISOLATE(isolate, ScriptCompiler, CompileUnbound, UnboundScript); @@ -2149,7 +2149,7 @@ MaybeLocal ScriptCompiler::CompileUnboundInternal( result = i::Compiler::GetSharedFunctionInfoForScript( str, name_obj, line_offset, column_offset, source->resource_options, source_map_url, isolate->native_context(), NULL, &script_data, options, - i::NOT_NATIVES_CODE, is_module); + i::NOT_NATIVES_CODE); has_pending_exception = result.is_null(); if (has_pending_exception && script_data != NULL) { // This case won't happen during normal operation; we have compiled @@ -2178,24 +2178,26 @@ MaybeLocal ScriptCompiler::CompileUnboundInternal( MaybeLocal ScriptCompiler::CompileUnboundScript( Isolate* v8_isolate, Source* source, CompileOptions options) { - return CompileUnboundInternal(v8_isolate, source, options, false); + DCHECK(!source->GetResourceOptions().IsModule()); + return CompileUnboundInternal(v8_isolate, source, options); } Local ScriptCompiler::CompileUnbound(Isolate* v8_isolate, Source* source, CompileOptions options) { - RETURN_TO_LOCAL_UNCHECKED( - CompileUnboundInternal(v8_isolate, source, options, false), - UnboundScript); + DCHECK(!source->GetResourceOptions().IsModule()); + RETURN_TO_LOCAL_UNCHECKED(CompileUnboundInternal(v8_isolate, source, options), + UnboundScript); } MaybeLocal