blob: bc5a9c36e3342aa77189877d0a36ff6b7656fd82 [file] [log] [blame]
// 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 "v8/include/v8.h"
#include "wtf/Allocator.h"
#include "wtf/Vector.h"
#include "wtf/text/WTFString.h"
namespace blink {
// ScriptModule wraps a handle to a v8::Module for use in core.
//
// Using ScriptModules needs a ScriptState and its scope to operate in. You
// should always provide the same ScriptState and not try to reuse ScriptModules
// across different contexts.
// Currently all ScriptModule users can easily access its context Modulator, so
// we use it to fill ScriptState in.
class CORE_EXPORT ScriptModule final {
DISALLOW_NEW_EXCEPT_PLACEMENT_NEW();
public:
static ScriptModule compile(v8::Isolate*,
const String& source,
const String& fileName);
ScriptModule() {}
ScriptModule(const ScriptModule& module) : m_module(module.m_module) {}
~ScriptModule();
// Returns exception, if any.
ScriptValue instantiate(ScriptState*);
void evaluate(ScriptState*);
Vector<String> moduleRequests(ScriptState*);
bool isNull() const { return !m_module || m_module->isEmpty(); }
private:
ScriptModule(v8::Isolate*, v8::Local<v8::Module>);
static v8::MaybeLocal<v8::Module> resolveModuleCallback(
v8::Local<v8::Context>,
v8::Local<v8::String> specifier,
v8::Local<v8::Module> referrer);
RefPtr<SharedPersistent<v8::Module>> m_module;
};
} // namespace blink
#endif // ScriptModule_h