JSModuleRecord.cpp [plain text]
#include "config.h"
#include "JSModuleRecord.h"
#include "Error.h"
#include "Interpreter.h"
#include "JSCInlines.h"
#include "JSModuleEnvironment.h"
#include "JSModuleNamespaceObject.h"
#include "UnlinkedModuleProgramCodeBlock.h"
namespace JSC {
const ClassInfo JSModuleRecord::s_info = { "ModuleRecord", &Base::s_info, 0, CREATE_METHOD_TABLE(JSModuleRecord) };
Structure* JSModuleRecord::createStructure(VM& vm, JSGlobalObject* globalObject, JSValue prototype)
{
return Structure::create(vm, globalObject, prototype, TypeInfo(ObjectType, StructureFlags), info());
}
JSModuleRecord* JSModuleRecord::create(ExecState* exec, VM& vm, Structure* structure, const Identifier& moduleKey, const SourceCode& sourceCode, const VariableEnvironment& declaredVariables, const VariableEnvironment& lexicalVariables)
{
JSModuleRecord* instance = new (NotNull, allocateCell<JSModuleRecord>(vm.heap)) JSModuleRecord(vm, structure, moduleKey, sourceCode, declaredVariables, lexicalVariables);
instance->finishCreation(exec, vm);
return instance;
}
JSModuleRecord::JSModuleRecord(VM& vm, Structure* structure, const Identifier& moduleKey, const SourceCode& sourceCode, const VariableEnvironment& declaredVariables, const VariableEnvironment& lexicalVariables)
: Base(vm, structure, moduleKey)
, m_sourceCode(sourceCode)
, m_declaredVariables(declaredVariables)
, m_lexicalVariables(lexicalVariables)
{
}
void JSModuleRecord::destroy(JSCell* cell)
{
JSModuleRecord* thisObject = static_cast<JSModuleRecord*>(cell);
thisObject->JSModuleRecord::~JSModuleRecord();
}
void JSModuleRecord::finishCreation(ExecState* exec, VM& vm)
{
Base::finishCreation(exec, vm);
ASSERT(inherits(info()));
}
void JSModuleRecord::visitChildren(JSCell* cell, SlotVisitor& visitor)
{
JSModuleRecord* thisObject = jsCast<JSModuleRecord*>(cell);
Base::visitChildren(thisObject, visitor);
visitor.append(thisObject->m_moduleProgramExecutable);
}
void JSModuleRecord::link(ExecState* exec)
{
VM& vm = exec->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
ModuleProgramExecutable* executable = ModuleProgramExecutable::create(exec, sourceCode());
if (!executable) {
throwSyntaxError(exec, scope);
return;
}
m_moduleProgramExecutable.set(vm, this, executable);
instantiateDeclarations(exec, executable);
}
void JSModuleRecord::instantiateDeclarations(ExecState* exec, ModuleProgramExecutable* moduleProgramExecutable)
{
VM& vm = exec->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
SymbolTable* symbolTable = moduleProgramExecutable->moduleEnvironmentSymbolTable();
JSModuleEnvironment* moduleEnvironment = JSModuleEnvironment::create(vm, exec->lexicalGlobalObject(), exec->lexicalGlobalObject(), symbolTable, jsTDZValue(), this);
for (const auto& pair : exportEntries()) {
const ExportEntry& exportEntry = pair.value;
if (exportEntry.type == JSModuleRecord::ExportEntry::Type::Indirect) {
Resolution resolution = resolveExport(exec, exportEntry.exportName);
switch (resolution.type) {
case Resolution::Type::NotFound:
throwSyntaxError(exec, scope, makeString("Indirectly exported binding name '", String(exportEntry.exportName.impl()), "' is not found."));
return;
case Resolution::Type::Ambiguous:
throwSyntaxError(exec, scope, makeString("Indirectly exported binding name '", String(exportEntry.exportName.impl()), "' cannot be resolved due to ambiguous multiple bindings."));
return;
case Resolution::Type::Error:
throwSyntaxError(exec, scope, makeString("Indirectly exported binding name 'default' cannot be resolved by star export entries."));
return;
case Resolution::Type::Resolved:
break;
}
}
}
for (const auto& pair : importEntries()) {
const ImportEntry& importEntry = pair.value;
AbstractModuleRecord* importedModule = hostResolveImportedModule(exec, importEntry.moduleRequest);
if (importEntry.isNamespace(vm)) {
JSModuleNamespaceObject* namespaceObject = importedModule->getModuleNamespace(exec);
RETURN_IF_EXCEPTION(scope, void());
bool putResult = false;
symbolTablePutTouchWatchpointSet(moduleEnvironment, exec, importEntry.localName, namespaceObject, false, true, putResult);
} else {
Resolution resolution = importedModule->resolveExport(exec, importEntry.importName);
switch (resolution.type) {
case Resolution::Type::NotFound:
throwSyntaxError(exec, scope, makeString("Importing binding name '", String(importEntry.importName.impl()), "' is not found."));
return;
case Resolution::Type::Ambiguous:
throwSyntaxError(exec, scope, makeString("Importing binding name '", String(importEntry.importName.impl()), "' cannot be resolved due to ambiguous multiple bindings."));
return;
case Resolution::Type::Error:
throwSyntaxError(exec, scope, makeString("Importing binding name 'default' cannot be resolved by star export entries."));
return;
case Resolution::Type::Resolved:
break;
}
}
}
for (const auto& variable : declaredVariables()) {
SymbolTableEntry entry = symbolTable->get(variable.key.get());
VarOffset offset = entry.varOffset();
if (!offset.isStack()) {
bool putResult = false;
symbolTablePutTouchWatchpointSet(moduleEnvironment, exec, Identifier::fromUid(exec, variable.key.get()), jsUndefined(), false, true, putResult);
}
}
UnlinkedModuleProgramCodeBlock* unlinkedCodeBlock = moduleProgramExecutable->unlinkedModuleProgramCodeBlock();
for (size_t i = 0, numberOfFunctions = unlinkedCodeBlock->numberOfFunctionDecls(); i < numberOfFunctions; ++i) {
UnlinkedFunctionExecutable* unlinkedFunctionExecutable = unlinkedCodeBlock->functionDecl(i);
SymbolTableEntry entry = symbolTable->get(unlinkedFunctionExecutable->name().impl());
VarOffset offset = entry.varOffset();
if (!offset.isStack()) {
ASSERT(!unlinkedFunctionExecutable->name().isEmpty());
if (vm.typeProfiler() || vm.controlFlowProfiler()) {
vm.functionHasExecutedCache()->insertUnexecutedRange(moduleProgramExecutable->sourceID(),
unlinkedFunctionExecutable->typeProfilingStartOffset(),
unlinkedFunctionExecutable->typeProfilingEndOffset());
}
JSFunction* function = JSFunction::create(vm, unlinkedFunctionExecutable->link(vm, moduleProgramExecutable->source()), moduleEnvironment);
bool putResult = false;
symbolTablePutTouchWatchpointSet(moduleEnvironment, exec, unlinkedFunctionExecutable->name(), function, false, true, putResult);
}
}
m_moduleEnvironment.set(vm, this, moduleEnvironment);
}
JSValue JSModuleRecord::evaluate(ExecState* exec)
{
if (!m_moduleProgramExecutable)
return jsUndefined();
JSValue result = exec->interpreter()->execute(m_moduleProgramExecutable.get(), exec, m_moduleEnvironment.get());
m_moduleProgramExecutable.clear();
return result;
}
}