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 "bindings/core/v8/V8PerContextData.h" |
| 9 #include "core/dom/DOMException.h" |
| 10 #include "core/dom/ExceptionCode.h" |
| 11 #include "core/dom/Modulator.h" |
| 12 #include "core/dom/ScriptModuleResolver.h" |
8 | 13 |
9 namespace blink { | 14 namespace blink { |
10 | 15 |
11 ScriptModule::ScriptModule(v8::Isolate* isolate, v8::Local<v8::Module> module) | 16 ScriptModule::ScriptModule(v8::Isolate* isolate, v8::Local<v8::Module> module) |
12 : m_module(SharedPersistent<v8::Module>::create(module, isolate)) {} | 17 : m_module(SharedPersistent<v8::Module>::create(module, isolate)), |
| 18 m_identityHash(static_cast<unsigned>(module->GetIdentityHash())) {} |
13 | 19 |
14 ScriptModule::~ScriptModule() {} | 20 ScriptModule::~ScriptModule() {} |
15 | 21 |
16 ScriptModule ScriptModule::compile(v8::Isolate* isolate, | 22 ScriptModule ScriptModule::compile(v8::Isolate* isolate, |
17 const String& source, | 23 const String& source, |
18 const String& fileName) { | 24 const String& fileName) { |
19 v8::TryCatch tryCatch(isolate); | 25 v8::TryCatch tryCatch(isolate); |
20 tryCatch.SetVerbose(true); | 26 tryCatch.SetVerbose(true); |
21 v8::Local<v8::Module> module; | 27 v8::Local<v8::Module> module; |
22 if (!v8Call(V8ScriptRunner::compileModule(isolate, source, fileName), module, | 28 if (!v8Call(V8ScriptRunner::compileModule(isolate, source, fileName), module, |
23 tryCatch)) { | 29 tryCatch)) { |
24 // TODO(adamk): Signal failure somehow. | 30 return ScriptModule(); |
25 return ScriptModule(isolate, module); | |
26 } | 31 } |
27 return ScriptModule(isolate, module); | 32 return ScriptModule(isolate, module); |
28 } | 33 } |
29 | 34 |
30 v8::MaybeLocal<v8::Module> dummyCallback(v8::Local<v8::Context> context, | 35 v8::MaybeLocal<v8::Module> ScriptModule::resolveModuleCallback( |
31 v8::Local<v8::String> specifier, | 36 v8::Local<v8::Context> context, |
32 v8::Local<v8::Module> referrer) { | 37 v8::Local<v8::String> specifier, |
33 return v8::MaybeLocal<v8::Module>(); | 38 v8::Local<v8::Module> referrer) { |
| 39 v8::Isolate* isolate = context->GetIsolate(); |
| 40 Modulator* modulator = V8PerContextData::from(context)->modulator(); |
| 41 DCHECK(modulator); |
| 42 |
| 43 ScriptModule referrerRecord(isolate, referrer); |
| 44 ExceptionState exceptionState(isolate, ExceptionState::ExecutionContext, |
| 45 "ScriptModule", "resolveModuleCallback"); |
| 46 ScriptModule resolved = modulator->scriptModuleResolver()->resolve( |
| 47 toCoreStringWithNullCheck(specifier), referrerRecord, |
| 48 exceptionState); |
| 49 if (resolved.isNull()) { |
| 50 DCHECK(exceptionState.hadException()); |
| 51 return v8::MaybeLocal<v8::Module>(); |
| 52 } |
| 53 |
| 54 return v8::MaybeLocal<v8::Module>(resolved.m_module->newLocal(isolate)); |
34 } | 55 } |
35 | 56 |
36 bool ScriptModule::instantiate(ScriptState* scriptState) { | 57 ScriptValue ScriptModule::instantiate(ScriptState* scriptState) { |
| 58 v8::Isolate* isolate = scriptState->isolate(); |
| 59 v8::TryCatch tryCatch(isolate); |
| 60 |
37 DCHECK(!isNull()); | 61 DCHECK(!isNull()); |
38 v8::Local<v8::Context> context = scriptState->context(); | 62 v8::Local<v8::Context> context = scriptState->context(); |
39 // TODO(adamk): pass in a real callback. | 63 bool success = m_module->newLocal(scriptState->isolate()) |
40 return m_module->newLocal(scriptState->isolate()) | 64 ->Instantiate(context, &resolveModuleCallback); |
41 ->Instantiate(context, &dummyCallback); | 65 if (!success) { |
| 66 DCHECK(tryCatch.HasCaught()); |
| 67 return ScriptValue(scriptState, tryCatch.Exception()); |
| 68 } |
| 69 // DCHECK(!tryCatch.HasCaught()); |
| 70 return ScriptValue(); |
42 } | 71 } |
43 | 72 |
44 void ScriptModule::evaluate(ScriptState* scriptState) { | 73 void ScriptModule::evaluate(ScriptState* scriptState) { |
45 v8::Isolate* isolate = scriptState->isolate(); | 74 v8::Isolate* isolate = scriptState->isolate(); |
46 v8::TryCatch tryCatch(isolate); | 75 v8::TryCatch tryCatch(isolate); |
47 tryCatch.SetVerbose(true); | 76 tryCatch.SetVerbose(true); |
48 v8::Local<v8::Value> result; | 77 v8::Local<v8::Value> result; |
49 if (!v8Call(V8ScriptRunner::evaluateModule(m_module->newLocal(isolate), | 78 if (!v8Call(V8ScriptRunner::evaluateModule(m_module->newLocal(isolate), |
50 scriptState->context(), isolate), | 79 scriptState->context(), isolate), |
51 result, tryCatch)) { | 80 result, tryCatch)) { |
52 // TODO(adamk): report error | 81 // TODO(adamk): report error |
53 } | 82 } |
54 } | 83 } |
55 | 84 |
| 85 Vector<String> ScriptModule::moduleRequests(ScriptState* scriptState) { |
| 86 if (isNull()) |
| 87 return Vector<String>(); |
| 88 |
| 89 v8::Local<v8::Module> module = m_module->newLocal(scriptState->isolate()); |
| 90 |
| 91 Vector<String> ret; |
| 92 |
| 93 int length = module->GetModuleRequestsLength(); |
| 94 ret.reserveInitialCapacity(length); |
| 95 for (int i = 0; i < length; ++i) { |
| 96 v8::Local<v8::String> v8Name = module->GetModuleRequest(i); |
| 97 String name = toCoreString(v8Name); |
| 98 ret.push_back(name); |
| 99 } |
| 100 return ret; |
| 101 } |
| 102 |
56 } // namespace blink | 103 } // namespace blink |
OLD | NEW |