Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "bindings/core/v8/ScriptModule.h" | |
| 6 | |
| 7 #include "bindings/core/v8/ScriptModuleResolver.h" | |
| 8 #include "bindings/core/v8/V8Binding.h" | |
| 9 #include "bindings/core/v8/V8PerContextData.h" | |
| 10 #include "core/dom/Modulator.h" | |
| 11 | |
| 12 namespace blink { | |
| 13 | |
| 14 ScriptModule::ScriptModule(v8::Isolate* isolate, v8::Local<v8::Module> module) | |
| 15 : m_module(SharedPersistent<v8::Module>::create(module, isolate)), | |
| 16 m_identityHash(static_cast<unsigned>(module->GetIdentityHash())) {} | |
| 17 | |
| 18 ScriptModule::~ScriptModule() {} | |
| 19 | |
| 20 ScriptModule ScriptModule::compile(v8::Isolate* isolate, | |
| 21 const String& source, | |
| 22 const String& fileName) { | |
| 23 // TODO(adamk): Pass more info into ScriptOrigin. | |
| 24 v8::ScriptOrigin origin(v8String(isolate, fileName)); | |
| 25 v8::ScriptCompiler::Source scriptSource(v8String(isolate, source), origin); | |
| 26 v8::TryCatch tryCatch(isolate); | |
| 27 tryCatch.SetVerbose(true); | |
| 28 v8::Local<v8::Module> module; | |
| 29 // TODO(kouhei): Add a helper function in V8ScriptRunner so that we can | |
| 30 // centralize all code to compile & run scripts in V8ScriptRunner. | |
| 31 if (!v8Call(v8::ScriptCompiler::CompileModule(isolate, &scriptSource), module, | |
| 32 tryCatch)) { | |
| 33 // TODO(adamk): Signal failure somehow. | |
| 34 return ScriptModule(isolate, module); | |
| 35 } | |
| 36 return ScriptModule(isolate, module); | |
| 37 } | |
| 38 | |
| 39 v8::MaybeLocal<v8::Module> ScriptModule::resolveModuleCallback( | |
| 40 v8::Local<v8::Context> context, | |
| 41 v8::Local<v8::String> specifier, | |
| 42 v8::Local<v8::Module> referrer) { | |
| 43 v8::Isolate* isolate = context->GetIsolate(); | |
| 44 Modulator* modulator = V8PerContextData::from(context)->modulator(); | |
| 45 DCHECK(modulator); | |
| 46 | |
| 47 ScriptModule referrerRecord(isolate, referrer); | |
| 48 // TODO(kouhei): Construct ScriptModuleIdentifier w/o creating extra | |
| 49 // Persistent. | |
| 50 ScriptModule resolved = modulator->scriptModuleResolver()->resolve( | |
| 51 toCoreStringWithNullCheck(specifier), referrerRecord.identifier()); | |
| 52 if (resolved.isNull()) | |
| 53 return v8::MaybeLocal<v8::Module>(); | |
| 54 | |
| 55 return v8::MaybeLocal<v8::Module>(resolved.m_module->newLocal(isolate)); | |
| 56 } | |
| 57 | |
| 58 bool ScriptModule::instantiate(ScriptState* scriptState) { | |
| 59 DCHECK(!isNull()); | |
| 60 v8::Local<v8::Context> context = scriptState->context(); | |
| 61 // TODO(adamk): pass in a real callback. | |
| 62 return m_module->newLocal(scriptState->isolate()) | |
| 63 ->Instantiate(context, &resolveModuleCallback); | |
| 64 } | |
| 65 | |
| 66 void ScriptModule::evaluate(ScriptState* scriptState) { | |
| 67 v8::TryCatch tryCatch(scriptState->isolate()); | |
| 68 tryCatch.SetVerbose(true); | |
| 69 v8::Local<v8::Value> result; | |
| 70 if (!v8Call(m_module->newLocal(scriptState->isolate()) | |
| 71 ->Evaluate(scriptState->context()), | |
| 72 result, tryCatch)) { | |
| 73 // TODO(adamk): report error | |
| 74 } | |
| 75 } | |
| 76 | |
| 77 Vector<String> ScriptModule::moduleRequests(ScriptState* scriptState) { | |
| 78 if (isNull()) | |
| 79 return Vector<String>(); | |
| 80 | |
| 81 v8::Local<v8::Module> module = m_module->newLocal(scriptState->isolate()); | |
|
dominicc (has gone to gerrit)
2017/01/11 03:23:47
HandleScope here?
kouhei (in TOK)
2017/01/17 05:26:12
Provided by caller.
| |
| 82 | |
| 83 Vector<String> ret; | |
| 84 | |
| 85 int length = module->GetModuleRequestsLength(); | |
| 86 ret.reserveInitialCapacity(length); | |
| 87 for (int i = 0; i < length; ++i) { | |
| 88 v8::Local<v8::String> v8Name = module->GetModuleRequest(i); | |
| 89 String name = toCoreStringWithNullCheck(v8Name); | |
|
dominicc (has gone to gerrit)
2017/01/11 03:23:47
Just toCoreString? Why would you expect null?
kouhei (in TOK)
2017/01/17 05:26:12
Done.
| |
| 90 ret.push_back(name); | |
| 91 } | |
| 92 return ret; | |
| 93 } | |
| 94 | |
| 95 ScriptModuleIdentifier ScriptModule::identifier() { | |
| 96 if (isNull()) | |
| 97 return ScriptModuleIdentifier(); | |
| 98 | |
| 99 return ScriptModuleIdentifier(m_module, m_identityHash); | |
| 100 } | |
| 101 | |
| 102 } // namespace blink | |
| OLD | NEW |