Commit 57c86c65 authored by adamk's avatar adamk Committed by Commit bot

Create bare-bones ScriptModule class

Provides a ScriptModule wrapper around v8::Module, as
well as primordial forms of the three basic operations
on modules:

  - compile(), a static method which takes a string and
    returns a ScriptModule
  - instantiate(), which "links" a module with its dependencies
    (the initial implementation does no actual linking)
  - evaluate(), which runs the module (and its dependencies;
    naturally this version doesn't do the latter)

Landing this in its early state makes it easier for others
working on <script type="module"> support to make progress
on other CLs, many of which will interact with this class

BUG=594639

Review-Url: https://codereview.chromium.org/2566513002
Cr-Commit-Position: refs/heads/master@{#443112}
parent d6ab9a5f
......@@ -92,6 +92,8 @@ bindings_core_v8_files =
"core/v8/ScriptEventListener.h",
"core/v8/ScriptFunction.cpp",
"core/v8/ScriptFunction.h",
"core/v8/ScriptModule.cpp",
"core/v8/ScriptModule.h",
"core/v8/ScriptPromise.cpp",
"core/v8/ScriptPromise.h",
"core/v8/ScriptPromiseProperties.h",
......
// 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/V8Binding.h"
namespace blink {
ScriptModule::ScriptModule(v8::Isolate* isolate, v8::Local<v8::Module> module)
: m_module(SharedPersistent<v8::Module>::create(module, isolate)) {}
ScriptModule::~ScriptModule() {}
ScriptModule ScriptModule::compile(v8::Isolate* isolate,
const String& source,
const String& fileName) {
v8::TryCatch tryCatch(isolate);
tryCatch.SetVerbose(true);
v8::Local<v8::Module> module;
if (!v8Call(V8ScriptRunner::compileModule(isolate, source, fileName), module,
tryCatch)) {
// TODO(adamk): Signal failure somehow.
return ScriptModule(isolate, module);
}
return ScriptModule(isolate, module);
}
v8::MaybeLocal<v8::Module> dummyCallback(v8::Local<v8::Context> context,
v8::Local<v8::String> specifier,
v8::Local<v8::Module> referrer) {
return v8::MaybeLocal<v8::Module>();
}
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, &dummyCallback);
}
void ScriptModule::evaluate(ScriptState* scriptState) {
v8::Isolate* isolate = scriptState->isolate();
v8::TryCatch tryCatch(isolate);
tryCatch.SetVerbose(true);
v8::Local<v8::Value> result;
if (!v8Call(V8ScriptRunner::evaluateModule(m_module->newLocal(isolate),
scriptState->context(), isolate),
result, tryCatch)) {
// TODO(adamk): report error
}
}
} // namespace blink
// 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.
#ifndef ScriptModule_h
#define ScriptModule_h
#include "bindings/core/v8/ScriptState.h"
#include "bindings/core/v8/SharedPersistent.h"
#include "core/CoreExport.h"
#include "wtf/Allocator.h"
#include "wtf/text/WTFString.h"
#include <v8.h>
namespace blink {
class CORE_EXPORT ScriptModule final {
DISALLOW_NEW_EXCEPT_PLACEMENT_NEW();
public:
static ScriptModule compile(v8::Isolate*,
const String& source,
const String& fileName);
ScriptModule(const ScriptModule& module) : m_module(module.m_module) {}
~ScriptModule();
bool instantiate(ScriptState*);
void evaluate(ScriptState*);
bool isNull() const { return m_module->isEmpty(); }
private:
ScriptModule(v8::Isolate*, v8::Local<v8::Module>);
RefPtr<SharedPersistent<v8::Module>> m_module;
};
} // namespace blink
#endif // ScriptModule_h
......@@ -493,6 +493,18 @@ v8::MaybeLocal<v8::Script> V8ScriptRunner::compileScript(
return (*compileFn)(isolate, code, origin);
}
v8::MaybeLocal<v8::Module> V8ScriptRunner::compileModule(
v8::Isolate* isolate,
const String& source,
const String& fileName) {
TRACE_EVENT1("v8", "v8.compileModule", "fileName", fileName.utf8());
// TODO(adamk): Add Inspector integration?
// TODO(adamk): Pass more info into ScriptOrigin.
v8::ScriptOrigin origin(v8String(isolate, fileName));
v8::ScriptCompiler::Source scriptSource(v8String(isolate, source), origin);
return v8::ScriptCompiler::CompileModule(isolate, &scriptSource);
}
v8::MaybeLocal<v8::Value> V8ScriptRunner::runCompiledScript(
v8::Isolate* isolate,
v8::Local<v8::Script> script,
......@@ -660,6 +672,16 @@ v8::MaybeLocal<v8::Value> V8ScriptRunner::callInternalFunction(
return result;
}
v8::MaybeLocal<v8::Value> V8ScriptRunner::evaluateModule(
v8::Local<v8::Module> module,
v8::Local<v8::Context> context,
v8::Isolate* isolate) {
TRACE_EVENT0("v8", "v8.evaluateModule");
v8::MicrotasksScope microtasksScope(isolate,
v8::MicrotasksScope::kRunMicrotasks);
return module->Evaluate(context);
}
v8::MaybeLocal<v8::Object> V8ScriptRunner::instantiateObject(
v8::Isolate* isolate,
v8::Local<v8::ObjectTemplate> objectTemplate) {
......
......@@ -81,6 +81,9 @@ class CORE_EXPORT V8ScriptRunner final {
CachedMetadataHandler* = nullptr,
AccessControlStatus = SharableCrossOrigin,
V8CacheOptions = V8CacheOptionsDefault);
static v8::MaybeLocal<v8::Module> compileModule(v8::Isolate*,
const String& source,
const String& fileName);
static v8::MaybeLocal<v8::Value> runCompiledScript(v8::Isolate*,
v8::Local<v8::Script>,
ExecutionContext*);
......@@ -110,6 +113,9 @@ class CORE_EXPORT V8ScriptRunner final {
int argc,
v8::Local<v8::Value> info[],
v8::Isolate*);
static v8::MaybeLocal<v8::Value> evaluateModule(v8::Local<v8::Module>,
v8::Local<v8::Context>,
v8::Isolate*);
static v8::MaybeLocal<v8::Object> instantiateObject(
v8::Isolate*,
v8::Local<v8::ObjectTemplate>);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment