OLD | NEW |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "bindings/core/v8/ScriptModule.h" | 5 #include "bindings/core/v8/ScriptModule.h" |
6 | 6 |
7 #include "bindings/core/v8/V8Binding.h" | 7 #include "bindings/core/v8/V8Binding.h" |
| 8 #include "core/dom/Modulator.h" |
| 9 #include "core/dom/ScriptModuleResolver.h" |
8 | 10 |
9 namespace blink { | 11 namespace blink { |
10 | 12 |
11 ScriptModule::ScriptModule(v8::Isolate* isolate, v8::Local<v8::Module> module) | 13 ScriptModule::ScriptModule(v8::Isolate* isolate, v8::Local<v8::Module> module) |
12 : m_module(SharedPersistent<v8::Module>::create(module, isolate)) {} | 14 : m_module(SharedPersistent<v8::Module>::create(module, isolate)) {} |
13 | 15 |
14 ScriptModule::~ScriptModule() {} | 16 ScriptModule::~ScriptModule() {} |
15 | 17 |
16 ScriptModule ScriptModule::compile(v8::Isolate* isolate, | 18 ScriptModule ScriptModule::compile(v8::Isolate* isolate, |
17 const String& source, | 19 const String& source, |
18 const String& fileName) { | 20 const String& fileName) { |
19 v8::TryCatch tryCatch(isolate); | 21 v8::TryCatch tryCatch(isolate); |
20 tryCatch.SetVerbose(true); | 22 tryCatch.SetVerbose(true); |
21 v8::Local<v8::Module> module; | 23 v8::Local<v8::Module> module; |
22 if (!v8Call(V8ScriptRunner::compileModule(isolate, source, fileName), module, | 24 if (!v8Call(V8ScriptRunner::compileModule(isolate, source, fileName), module, |
23 tryCatch)) { | 25 tryCatch)) { |
24 // TODO(adamk): Signal failure somehow. | 26 // TODO(adamk): Signal failure somehow. |
25 return ScriptModule(isolate, module); | 27 return ScriptModule(isolate, module); |
26 } | 28 } |
27 return ScriptModule(isolate, module); | 29 return ScriptModule(isolate, module); |
28 } | 30 } |
29 | 31 |
30 v8::MaybeLocal<v8::Module> dummyCallback(v8::Local<v8::Context> context, | 32 ScriptValue ScriptModule::instantiate(ScriptState* scriptState) { |
31 v8::Local<v8::String> specifier, | 33 v8::Isolate* isolate = scriptState->isolate(); |
32 v8::Local<v8::Module> referrer) { | 34 v8::TryCatch tryCatch(isolate); |
33 return v8::MaybeLocal<v8::Module>(); | |
34 } | |
35 | 35 |
36 bool ScriptModule::instantiate(ScriptState* scriptState) { | |
37 DCHECK(!isNull()); | 36 DCHECK(!isNull()); |
38 v8::Local<v8::Context> context = scriptState->context(); | 37 v8::Local<v8::Context> context = scriptState->context(); |
39 // TODO(adamk): pass in a real callback. | 38 bool success = m_module->newLocal(scriptState->isolate()) |
40 return m_module->newLocal(scriptState->isolate()) | 39 ->Instantiate(context, &resolveModuleCallback); |
41 ->Instantiate(context, &dummyCallback); | 40 if (!success) { |
| 41 DCHECK(tryCatch.HasCaught()); |
| 42 return ScriptValue(scriptState, tryCatch.Exception()); |
| 43 } |
| 44 DCHECK(!tryCatch.HasCaught()); |
| 45 return ScriptValue(); |
42 } | 46 } |
43 | 47 |
44 void ScriptModule::evaluate(ScriptState* scriptState) { | 48 void ScriptModule::evaluate(ScriptState* scriptState) { |
45 v8::Isolate* isolate = scriptState->isolate(); | 49 v8::Isolate* isolate = scriptState->isolate(); |
46 v8::TryCatch tryCatch(isolate); | 50 v8::TryCatch tryCatch(isolate); |
47 tryCatch.SetVerbose(true); | 51 tryCatch.SetVerbose(true); |
48 v8::Local<v8::Value> result; | 52 v8::Local<v8::Value> result; |
49 if (!v8Call(V8ScriptRunner::evaluateModule(m_module->newLocal(isolate), | 53 if (!v8Call(V8ScriptRunner::evaluateModule(m_module->newLocal(isolate), |
50 scriptState->context(), isolate), | 54 scriptState->context(), isolate), |
51 result, tryCatch)) { | 55 result, tryCatch)) { |
52 // TODO(adamk): report error | 56 // TODO(adamk): report error |
53 } | 57 } |
54 } | 58 } |
55 | 59 |
| 60 v8::MaybeLocal<v8::Module> ScriptModule::resolveModuleCallback( |
| 61 v8::Local<v8::Context> context, |
| 62 v8::Local<v8::String> specifier, |
| 63 v8::Local<v8::Module> referrer) { |
| 64 v8::Isolate* isolate = context->GetIsolate(); |
| 65 Modulator* modulator = V8PerContextData::from(context)->modulator(); |
| 66 DCHECK(modulator); |
| 67 |
| 68 ScriptModule referrerRecord(isolate, referrer); |
| 69 ExceptionState exceptionState(isolate, ExceptionState::ExecutionContext, |
| 70 "ScriptModule", "resolveModuleCallback"); |
| 71 ScriptModule resolved = modulator->scriptModuleResolver()->resolve( |
| 72 toCoreStringWithNullCheck(specifier), referrerRecord, exceptionState); |
| 73 if (resolved.isNull()) { |
| 74 DCHECK(exceptionState.hadException()); |
| 75 return v8::MaybeLocal<v8::Module>(); |
| 76 } |
| 77 |
| 78 DCHECK(!exceptionState.hadException()); |
| 79 return v8::MaybeLocal<v8::Module>(resolved.m_module->newLocal(isolate)); |
| 80 } |
| 81 |
56 } // namespace blink | 82 } // namespace blink |
OLD | NEW |