Skip to content

Commit 4005d89

Browse files
nyaxtCommit Bot
authored andcommitted
[ES6 modules] Update module script error state predicate and transition.
This CL updates the following spec concepts: "#concept-module-script-is-errored" - Null record is now also treated as an error "#concept-module-script-pre-instantiation-error" - Renamed from module script's error. "#concept-module-script-module-record" - Implement "set its [[HostDefined]] field to undefined" step. "#concept-module-script-set-pre-instantiation-error" - Don't set script's state to errored. This change is to be introduced in whatwg html spec PR: whatwg/html#2674 The actual implementation behavior change will be introduced in separate CLs. Bug: 594639, 727299, whatwg/html#2674 Change-Id: Ie9734344030f99bcdaa3d595f8dbec87d44a7c92 Reviewed-on: https://chromium-review.googlesource.com/541097 Commit-Queue: Kouhei Ueno <kouhei@chromium.org> Reviewed-by: Yutaka Hirano <yhirano@chromium.org> Cr-Commit-Position: refs/heads/master@{#482213}
1 parent 73bb506 commit 4005d89

File tree

13 files changed

+73
-49
lines changed

13 files changed

+73
-49
lines changed

third_party/WebKit/Source/bindings/core/v8/ScriptModuleTest.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ class TestScriptModuleResolver final : public ScriptModuleResolver {
3535
// Implements ScriptModuleResolver:
3636

3737
void RegisterModuleScript(ModuleScript*) override { NOTREACHED(); }
38+
void UnregisterModuleScript(ModuleScript*) override { NOTREACHED(); }
3839

3940
ScriptModule Resolve(const String& specifier,
4041
const ScriptModule&,

third_party/WebKit/Source/core/dom/ModulatorImpl.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -185,9 +185,7 @@ void ModulatorImpl::ExecuteModule(const ModuleScript* module_script) {
185185

186186
// Step 3. "If s is errored, then report the exception given by s's error for
187187
// s and abort these steps." [spec text]
188-
// TODO(kouhei): Update "is errored".
189-
ModuleInstantiationState instantiationState = module_script->State();
190-
if (instantiationState == ModuleInstantiationState::kErrored) {
188+
if (module_script->IsErrored()) {
191189
v8::Isolate* isolate = script_state_->GetIsolate();
192190
ScriptModule::ReportException(
193191
script_state_.Get(), module_script->CreateErrorInternal(isolate),

third_party/WebKit/Source/core/dom/ModuleMapTest.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ class TestScriptModuleResolver final : public ScriptModuleResolver {
5656
register_module_script_call_count_++;
5757
}
5858

59+
void UnregisterModuleScript(ModuleScript*) override {
60+
FAIL() << "UnregisterModuleScript shouldn't be called in ModuleMapTest";
61+
}
62+
5963
ScriptModule Resolve(const String& specifier,
6064
const ScriptModule& referrer,
6165
ExceptionState&) override {

third_party/WebKit/Source/core/dom/ModulePendingScript.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ void ModulePendingScriptTreeClient::SetPendingScript(
2424

2525
void ModulePendingScriptTreeClient::NotifyModuleTreeLoadFinished(
2626
ModuleScript* module_script) {
27-
DCHECK(!(module_script && module_script->State() ==
28-
ModuleInstantiationState::kUninstantiated));
27+
DCHECK(!module_script || module_script->IsErrored() ||
28+
module_script->State() == ModuleInstantiationState::kInstantiated);
2929
DCHECK(!finished_);
3030
finished_ = true;
3131
module_script_ = module_script;

third_party/WebKit/Source/core/dom/ModuleScript.cpp

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ ModuleScript::ModuleScript(Modulator* settings_object,
151151
: settings_object_(settings_object),
152152
record_(this),
153153
base_url_(base_url),
154-
error_(this),
154+
preinstantiation_error_(this),
155155
nonce_(nonce),
156156
parser_state_(parser_state),
157157
credentials_mode_(credentials_mode),
@@ -185,31 +185,23 @@ bool ModuleScript::HasEmptyRecord() const {
185185
void ModuleScript::SetErrorAndClearRecord(ScriptValue error) {
186186
DVLOG(1) << "ModuleScript[" << this << "]::SetErrorAndClearRecord()";
187187

188-
// https://html.spec.whatwg.org/multipage/webappapis.html#error-a-module-script
189-
// Step 1. Assert: script's state is not "errored".
190-
DCHECK_NE(state_, ModuleInstantiationState::kErrored);
191-
192-
// Step 2. If script's module record is set, then:
188+
// https://html.spec.whatwg.org/multipage/webappapis.html#concept-module-script-set-pre-instantiation-error
189+
// Step 1. "If script's module record is not null, ..." [spec text]
193190
if (!record_.IsEmpty()) {
194-
// Step 2.1. Set script module record's [[HostDefined]] field to undefined.
195-
// TODO(kouhei): Implement this step.
196-
// if (ScriptModuleResolver* resolver =
197-
// modulator_->GetScriptModuleResolver())
198-
// resolver->UnregisterModuleScript(this);
199-
NOTIMPLEMENTED();
200-
201-
// Step 2.2. Set script's module record to null.
202-
record_.Clear();
191+
// "set its [[HostDefined]] field to undefined." [spec text]
192+
if (ScriptModuleResolver* resolver =
193+
settings_object_->GetScriptModuleResolver())
194+
resolver->UnregisterModuleScript(this);
203195
}
204196

205-
// Step 3. Set script's state to "errored".
206-
state_ = ModuleInstantiationState::kErrored;
197+
// Step 2. "Set script's module record to null." [spec text]
198+
record_.Clear();
207199

208-
// Step 4. Set script's error to error.
200+
// Step 3. "Set script's pre-instantiation error to error." [spec text]
209201
DCHECK(!error.IsEmpty());
210202
{
211203
ScriptState::Scope scope(error.GetScriptState());
212-
error_.Set(error.GetIsolate(), error.V8Value());
204+
preinstantiation_error_.Set(error.GetIsolate(), error.V8Value());
213205
}
214206
}
215207

@@ -230,7 +222,7 @@ DEFINE_TRACE_WRAPPERS(ModuleScript) {
230222
// TODO(mlippautz): Support TraceWrappers(const
231223
// TraceWrapperV8Reference<v8::Module>&) to remove the cast.
232224
visitor->TraceWrappers(record_.Cast<v8::Value>());
233-
visitor->TraceWrappers(error_);
225+
visitor->TraceWrappers(preinstantiation_error_);
234226
}
235227

236228
bool ModuleScript::IsEmpty() const {

third_party/WebKit/Source/core/dom/ModuleScript.h

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -61,15 +61,18 @@ class CORE_EXPORT ModuleScript final : public Script, public TraceWrapperBase {
6161

6262
ModuleInstantiationState State() const { return state_; }
6363

64-
// https://html.spec.whatwg.org/multipage/webappapis.html#error-a-module-script
64+
// https://html.spec.whatwg.org/multipage/webappapis.html#concept-module-script-is-errored
65+
bool IsErrored() const { return record_.IsEmpty(); }
66+
67+
// https://html.spec.whatwg.org/multipage/webappapis.html#concept-module-script-set-pre-instantiation-error
6568
void SetErrorAndClearRecord(ScriptValue error);
6669

6770
// Implements Step 7.2 of:
6871
// https://html.spec.whatwg.org/multipage/webappapis.html#internal-module-script-graph-fetching-procedure
6972
void SetInstantiationSuccess();
7073

7174
v8::Local<v8::Value> CreateError(v8::Isolate* isolate) const {
72-
return error_.NewLocal(isolate);
75+
return preinstantiation_error_.NewLocal(isolate);
7376
}
7477

7578
ParserDisposition ParserState() const { return parser_state_; }
@@ -113,8 +116,10 @@ class CORE_EXPORT ModuleScript final : public Script, public TraceWrapperBase {
113116
friend class ModuleTreeLinkerTestModulator;
114117
// Access this func only via ModulatorImpl::GetError(),
115118
// or via Modulator mocks for unit tests.
119+
// TODO(kouhei): Needs update after V8 change. The error may also be stored
120+
// inside record_.
116121
v8::Local<v8::Value> CreateErrorInternal(v8::Isolate* isolate) const {
117-
return error_.NewLocal(isolate);
122+
return preinstantiation_error_.NewLocal(isolate);
118123
}
119124

120125
// https://html.spec.whatwg.org/multipage/webappapis.html#settings-object
@@ -129,9 +134,10 @@ class CORE_EXPORT ModuleScript final : public Script, public TraceWrapperBase {
129134
// https://html.spec.whatwg.org/multipage/webappapis.html#concept-module-script-instantiation-state
130135
ModuleInstantiationState state_ = ModuleInstantiationState::kUninstantiated;
131136

132-
// https://html.spec.whatwg.org/multipage/webappapis.html#concept-module-script-error
137+
// https://html.spec.whatwg.org/multipage/webappapis.html#concept-module-script-pre-instantiation-error
133138
//
134-
// |record_| and |error_| are TraceWrappers()ed and kept alive via the path of
139+
// |record_| and |preinstantiation_error_| are TraceWrappers()ed and kept
140+
// alive via the path of
135141
// DOMWindow -> Modulator/ModulatorImpl -> ModuleMap -> ModuleMap::Entry
136142
// -> ModuleScript, or
137143
// Modulator/ModulatorImpl -> ModuleTreeLinkerRegistry -> ModuleTreeLinker
@@ -140,8 +146,7 @@ class CORE_EXPORT ModuleScript final : public Script, public TraceWrapperBase {
140146
// ModulePendingScriptTreeClient -> ModuleScript.
141147
// All the classes/references on the path above should be
142148
// TraceWrapperBase/TraceWrapperMember<>/etc.,
143-
// but other references to those classes can be normal Member<>.
144-
TraceWrapperV8Reference<v8::Value> error_;
149+
TraceWrapperV8Reference<v8::Value> preinstantiation_error_;
145150

146151
// https://html.spec.whatwg.org/multipage/webappapis.html#concept-module-script-nonce
147152
const String nonce_;

third_party/WebKit/Source/core/dom/ScriptModuleResolver.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,14 @@ class CORE_EXPORT ScriptModuleResolver
2828
virtual ~ScriptModuleResolver() {}
2929
DEFINE_INLINE_VIRTUAL_TRACE() {}
3030

31-
// Notify the ScriptModuleResolver that a ModuleScript exists.
31+
// Notifies the ScriptModuleResolver that a ModuleScript exists.
3232
// This hook gives a chance for the resolver impl to populate module record
3333
// identifier -> ModuleScript mapping entry.
3434
virtual void RegisterModuleScript(ModuleScript*) = 0;
3535

36+
// Notifies the ScriptModuleResolver to clear its ModuleScript mapping.
37+
virtual void UnregisterModuleScript(ModuleScript*) = 0;
38+
3639
// Implements "Runtime Semantics: HostResolveImportedModule"
3740
// https://tc39.github.io/ecma262/#sec-hostresolveimportedmodule
3841
// This returns a null ScriptModule when an exception is thrown.

third_party/WebKit/Source/core/dom/ScriptModuleResolverImpl.cpp

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,30 @@ void ScriptModuleResolverImpl::RegisterModuleScript(
1616
if (module_script->Record().IsNull())
1717
return;
1818

19-
DVLOG(1) << "ScriptModuleResolverImpl::registerModuleScript(url=\""
19+
DVLOG(1) << "ScriptModuleResolverImpl::RegisterModuleScript(url="
2020
<< module_script->BaseURL().GetString()
21-
<< "\", hash=" << ScriptModuleHash::GetHash(module_script->Record())
21+
<< ", hash=" << ScriptModuleHash::GetHash(module_script->Record())
2222
<< ")";
2323

2424
auto result =
2525
record_to_module_script_map_.Set(module_script->Record(), module_script);
2626
DCHECK(result.is_new_entry);
2727
}
2828

29+
void ScriptModuleResolverImpl::UnregisterModuleScript(
30+
ModuleScript* module_script) {
31+
DCHECK(module_script);
32+
if (module_script->Record().IsNull())
33+
return;
34+
35+
DVLOG(1) << "ScriptModuleResolverImpl::UnregisterModuleScript(url="
36+
<< module_script->BaseURL().GetString()
37+
<< ", hash=" << ScriptModuleHash::GetHash(module_script->Record())
38+
<< ")";
39+
40+
record_to_module_script_map_.erase(module_script->Record());
41+
}
42+
2943
ScriptModule ScriptModuleResolverImpl::Resolve(
3044
const String& specifier,
3145
const ScriptModule& referrer,
@@ -68,7 +82,8 @@ ScriptModule ScriptModuleResolverImpl::Resolve(
6882

6983
// Step 5. If resolved module script's instantiation state is "errored", then
7084
// throw resolved module script's instantiation error.
71-
if (module_script->State() == ModuleInstantiationState::kErrored) {
85+
// TODO(kouhei): Update spec references.
86+
if (module_script->IsErrored()) {
7287
ScriptValue error = modulator_->GetError(module_script);
7388
exception_state.RethrowV8Exception(error.V8Value());
7489
return ScriptModule();

third_party/WebKit/Source/core/dom/ScriptModuleResolverImpl.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ class CORE_EXPORT ScriptModuleResolverImpl final
4242
// Implements ScriptModuleResolver:
4343

4444
void RegisterModuleScript(ModuleScript*) final;
45+
void UnregisterModuleScript(ModuleScript*) final;
46+
4547
// Implements "Runtime Semantics: HostResolveImportedModule" per HTML spec.
4648
// https://html.spec.whatwg.org/#hostresolveimportedmodule(referencingmodule,-specifier)
4749
ScriptModule Resolve(const String& specifier,

third_party/WebKit/Source/core/loader/modulescript/ModuleScriptLoaderTest.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,8 +160,7 @@ TEST_F(ModuleScriptLoaderTest, InvalidSpecifier) {
160160
EXPECT_TRUE(client->WasNotifyFinished())
161161
<< "ModuleScriptLoader should finish synchronously.";
162162
ASSERT_TRUE(client->GetModuleScript());
163-
EXPECT_EQ(client->GetModuleScript()->State(),
164-
ModuleInstantiationState::kErrored);
163+
EXPECT_TRUE(client->GetModuleScript()->IsErrored());
165164
}
166165

167166
TEST_F(ModuleScriptLoaderTest, fetchInvalidURL) {

0 commit comments

Comments
 (0)