| 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 v8::MaybeLocal<v8::Module> dummyCallback(v8::Local<v8::Context> context, |
| 31 v8::Local<v8::String> specifier, | 33 v8::Local<v8::String> specifier, |
| 32 v8::Local<v8::Module> referrer) { | 34 v8::Local<v8::Module> referrer) { |
| 33 return v8::MaybeLocal<v8::Module>(); | 35 return v8::MaybeLocal<v8::Module>(); |
| 34 } | 36 } |
| 35 | 37 |
| 36 bool ScriptModule::instantiate(ScriptState* scriptState) { | 38 v8::MaybeLocal<v8::Module> ScriptModule::resolveModuleCallback( |
| 39 v8::Local<v8::Context> context, |
| 40 v8::Local<v8::String> specifier, |
| 41 v8::Local<v8::Module> referrer) { |
| 42 v8::Isolate* isolate = context->GetIsolate(); |
| 43 Modulator* modulator = V8PerContextData::from(context)->modulator(); |
| 44 DCHECK(modulator); |
| 45 |
| 46 ScriptModule referrerRecord(isolate, referrer); |
| 47 ExceptionState exceptionState(isolate, ExceptionState::ExecutionContext, |
| 48 "ScriptModule", "resolveModuleCallback"); |
| 49 ScriptModule resolved = modulator->scriptModuleResolver()->resolve( |
| 50 toCoreStringWithNullCheck(specifier), referrerRecord, exceptionState); |
| 51 if (resolved.isNull()) { |
| 52 DCHECK(exceptionState.hadException()); |
| 53 return v8::MaybeLocal<v8::Module>(); |
| 54 } |
| 55 |
| 56 return v8::MaybeLocal<v8::Module>(resolved.m_module->newLocal(isolate)); |
| 57 } |
| 58 |
| 59 ScriptValue ScriptModule::instantiate(ScriptState* scriptState) { |
| 60 v8::Isolate* isolate = scriptState->isolate(); |
| 61 v8::TryCatch tryCatch(isolate); |
| 62 |
| 37 DCHECK(!isNull()); | 63 DCHECK(!isNull()); |
| 38 v8::Local<v8::Context> context = scriptState->context(); | 64 v8::Local<v8::Context> context = scriptState->context(); |
| 39 // TODO(adamk): pass in a real callback. | 65 bool success = m_module->newLocal(scriptState->isolate()) |
| 40 return m_module->newLocal(scriptState->isolate()) | 66 ->Instantiate(context, &resolveModuleCallback); |
| 41 ->Instantiate(context, &dummyCallback); | 67 if (!success) { |
| 68 DCHECK(tryCatch.HasCaught()); |
| 69 return ScriptValue(scriptState, tryCatch.Exception()); |
| 70 } |
| 71 return ScriptValue(); |
| 42 } | 72 } |
| 43 | 73 |
| 44 void ScriptModule::evaluate(ScriptState* scriptState) { | 74 void ScriptModule::evaluate(ScriptState* scriptState) { |
| 45 v8::Isolate* isolate = scriptState->isolate(); | 75 v8::Isolate* isolate = scriptState->isolate(); |
| 46 v8::TryCatch tryCatch(isolate); | 76 v8::TryCatch tryCatch(isolate); |
| 47 tryCatch.SetVerbose(true); | 77 tryCatch.SetVerbose(true); |
| 48 v8::Local<v8::Value> result; | 78 v8::Local<v8::Value> result; |
| 49 if (!v8Call(V8ScriptRunner::evaluateModule(m_module->newLocal(isolate), | 79 if (!v8Call(V8ScriptRunner::evaluateModule(m_module->newLocal(isolate), |
| 50 scriptState->context(), isolate), | 80 scriptState->context(), isolate), |
| 51 result, tryCatch)) { | 81 result, tryCatch)) { |
| 52 // TODO(adamk): report error | 82 // TODO(adamk): report error |
| 53 } | 83 } |
| 54 } | 84 } |
| 55 | 85 |
| 56 } // namespace blink | 86 } // namespace blink |
| OLD | NEW |