Index: third_party/WebKit/Source/bindings/core/v8/ScriptModule.cpp |
diff --git a/third_party/WebKit/Source/bindings/core/v8/ScriptModule.cpp b/third_party/WebKit/Source/bindings/core/v8/ScriptModule.cpp |
new file mode 100644 |
index 0000000000000000000000000000000000000000..69ccf99f817a28d4a7eecdf051ed5fa9a4d6c285 |
--- /dev/null |
+++ b/third_party/WebKit/Source/bindings/core/v8/ScriptModule.cpp |
@@ -0,0 +1,102 @@ |
+// Copyright 2017 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "bindings/core/v8/ScriptModule.h" |
+ |
+#include "bindings/core/v8/ScriptModuleResolver.h" |
+#include "bindings/core/v8/V8Binding.h" |
+#include "bindings/core/v8/V8PerContextData.h" |
+#include "core/dom/Modulator.h" |
+ |
+namespace blink { |
+ |
+ScriptModule::ScriptModule(v8::Isolate* isolate, v8::Local<v8::Module> module) |
+ : m_module(SharedPersistent<v8::Module>::create(module, isolate)), |
+ m_identityHash(static_cast<unsigned>(module->GetIdentityHash())) {} |
+ |
+ScriptModule::~ScriptModule() {} |
+ |
+ScriptModule ScriptModule::compile(v8::Isolate* isolate, |
+ const String& source, |
+ const String& fileName) { |
+ // TODO(adamk): Pass more info into ScriptOrigin. |
+ v8::ScriptOrigin origin(v8String(isolate, fileName)); |
+ v8::ScriptCompiler::Source scriptSource(v8String(isolate, source), origin); |
+ v8::TryCatch tryCatch(isolate); |
+ tryCatch.SetVerbose(true); |
+ v8::Local<v8::Module> module; |
+ // TODO(kouhei): Add a helper function in V8ScriptRunner so that we can |
+ // centralize all code to compile & run scripts in V8ScriptRunner. |
+ if (!v8Call(v8::ScriptCompiler::CompileModule(isolate, &scriptSource), module, |
+ tryCatch)) { |
+ // TODO(adamk): Signal failure somehow. |
+ return ScriptModule(isolate, module); |
+ } |
+ return ScriptModule(isolate, module); |
+} |
+ |
+v8::MaybeLocal<v8::Module> ScriptModule::resolveModuleCallback( |
+ v8::Local<v8::Context> context, |
+ v8::Local<v8::String> specifier, |
+ v8::Local<v8::Module> referrer) { |
+ v8::Isolate* isolate = context->GetIsolate(); |
+ Modulator* modulator = V8PerContextData::from(context)->modulator(); |
+ DCHECK(modulator); |
+ |
+ ScriptModule referrerRecord(isolate, referrer); |
+ // TODO(kouhei): Construct ScriptModuleIdentifier w/o creating extra |
+ // Persistent. |
+ ScriptModule resolved = modulator->scriptModuleResolver()->resolve( |
+ toCoreStringWithNullCheck(specifier), referrerRecord.identifier()); |
+ if (resolved.isNull()) |
+ return v8::MaybeLocal<v8::Module>(); |
+ |
+ return v8::MaybeLocal<v8::Module>(resolved.m_module->newLocal(isolate)); |
+} |
+ |
+bool ScriptModule::instantiate(ScriptState* scriptState) { |
+ DCHECK(!isNull()); |
+ v8::Local<v8::Context> context = scriptState->context(); |
+ // TODO(adamk): pass in a real callback. |
+ return m_module->newLocal(scriptState->isolate()) |
+ ->Instantiate(context, &resolveModuleCallback); |
+} |
+ |
+void ScriptModule::evaluate(ScriptState* scriptState) { |
+ v8::TryCatch tryCatch(scriptState->isolate()); |
+ tryCatch.SetVerbose(true); |
+ v8::Local<v8::Value> result; |
+ if (!v8Call(m_module->newLocal(scriptState->isolate()) |
+ ->Evaluate(scriptState->context()), |
+ result, tryCatch)) { |
+ // TODO(adamk): report error |
+ } |
+} |
+ |
+Vector<String> ScriptModule::moduleRequests(ScriptState* scriptState) { |
+ if (isNull()) |
+ return Vector<String>(); |
+ |
+ 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.
|
+ |
+ Vector<String> ret; |
+ |
+ int length = module->GetModuleRequestsLength(); |
+ ret.reserveInitialCapacity(length); |
+ for (int i = 0; i < length; ++i) { |
+ v8::Local<v8::String> v8Name = module->GetModuleRequest(i); |
+ 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.
|
+ ret.push_back(name); |
+ } |
+ return ret; |
+} |
+ |
+ScriptModuleIdentifier ScriptModule::identifier() { |
+ if (isNull()) |
+ return ScriptModuleIdentifier(); |
+ |
+ return ScriptModuleIdentifier(m_module, m_identityHash); |
+} |
+ |
+} // namespace blink |