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..4293006c32d0d4eab11c0e6d3f9adac527aa78e4 |
--- /dev/null |
+++ b/third_party/WebKit/Source/bindings/core/v8/ScriptModule.cpp |
@@ -0,0 +1,99 @@ |
+// 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" |
+ |
+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; |
+ if (!v8Call(v8::ScriptCompiler::CompileModule(isolate, &scriptSource), module, |
haraken
2017/01/06 05:47:58
Nit: I want to create a helper function in V8Scrip
kouhei (in TOK)
2017/01/11 01:41:57
Acknowledged. TODO for now.
|
+ 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(); |
+ ScriptModuleResolver* resolver = |
+ V8PerContextData::from(context)->moduleResolver(); |
+ DCHECK(resolver); |
+ ScriptModule referrerRecord(isolate, referrer); |
+ // TODO(kouhei): Construct ScriptModuleIdentifier w/o creating extra |
+ // Persistent. |
+ ScriptModule resolved = resolver->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()), |
haraken
2017/01/06 05:47:58
Ditto.
kouhei (in TOK)
2017/01/11 01:41:57
Acknowledged.
|
+ 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()); |
+ |
+ 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); |
+ ret.push_back(name); |
+ } |
+ return ret; |
+} |
+ |
+ScriptModuleIdentifier ScriptModule::identifier() { |
+ if (isNull()) |
+ return ScriptModuleIdentifier(); |
+ |
+ return ScriptModuleIdentifier(m_module, m_identityHash); |
+} |
+ |
+} // namespace blink |