WebAssemblyInstanceConstructor.cpp [plain text]
#include "config.h"
#include "WebAssemblyInstanceConstructor.h"
#if ENABLE(WEBASSEMBLY)
#include "FunctionPrototype.h"
#include "JSCInlines.h"
#include "JSModuleEnvironment.h"
#include "JSModuleNamespaceObject.h"
#include "JSWebAssemblyInstance.h"
#include "JSWebAssemblyMemory.h"
#include "JSWebAssemblyModule.h"
#include "WebAssemblyFunction.h"
#include "WebAssemblyInstancePrototype.h"
#include "WebAssemblyModuleRecord.h"
#include "WebAssemblyInstanceConstructor.lut.h"
namespace JSC {
static const bool verbose = false;
const ClassInfo WebAssemblyInstanceConstructor::s_info = { "Function", &Base::s_info, &constructorTableWebAssemblyInstance, CREATE_METHOD_TABLE(WebAssemblyInstanceConstructor) };
static EncodedJSValue JSC_HOST_CALL constructJSWebAssemblyInstance(ExecState* exec)
{
auto& vm = exec->vm();
auto throwScope = DECLARE_THROW_SCOPE(vm);
auto* globalObject = exec->lexicalGlobalObject();
JSWebAssemblyModule* jsModule = jsDynamicCast<JSWebAssemblyModule*>(exec->argument(0));
if (!jsModule)
return JSValue::encode(throwException(exec, throwScope, createTypeError(exec, ASCIILiteral("first argument to WebAssembly.Instance must be a WebAssembly.Module"), defaultSourceAppender, runtimeTypeForValue(exec->argument(0)))));
const Wasm::ModuleInformation& moduleInformation = jsModule->moduleInformation();
JSValue importArgument = exec->argument(1);
JSObject* importObject = importArgument.getObject();
if (!importArgument.isUndefined() && !importObject)
return JSValue::encode(throwException(exec, throwScope, createTypeError(exec, ASCIILiteral("second argument to WebAssembly.Instance must be undefined or an Object"), defaultSourceAppender, runtimeTypeForValue(importArgument))));
if (moduleInformation.imports.size() && !importObject)
return JSValue::encode(throwException(exec, throwScope, createTypeError(exec, ASCIILiteral("second argument to WebAssembly.Instance must be Object because the WebAssembly.Module has imports"), defaultSourceAppender, runtimeTypeForValue(importArgument))));
Identifier moduleKey = Identifier::fromUid(PrivateName(PrivateName::Description, "WebAssemblyInstance"));
WebAssemblyModuleRecord* moduleRecord = WebAssemblyModuleRecord::create(exec, vm, globalObject->webAssemblyModuleRecordStructure(), moduleKey, moduleInformation);
RETURN_IF_EXCEPTION(throwScope, { });
Structure* instanceStructure = InternalFunction::createSubclassStructure(exec, exec->newTarget(), globalObject->WebAssemblyInstanceStructure());
RETURN_IF_EXCEPTION(throwScope, { });
JSWebAssemblyInstance* instance = JSWebAssemblyInstance::create(vm, instanceStructure, jsModule, moduleRecord->getModuleNamespace(exec));
RETURN_IF_EXCEPTION(throwScope, { });
unsigned numImportFunctions = 0;
unsigned numImportGlobals = 0;
bool hasMemoryImport = false;
bool hasTableImport = false;
for (auto& import : moduleInformation.imports) {
JSValue importModuleValue = importObject->get(exec, import.module);
RETURN_IF_EXCEPTION(throwScope, { });
if (!importModuleValue.isObject())
return JSValue::encode(throwException(exec, throwScope, createTypeError(exec, ASCIILiteral("import must be an object"), defaultSourceAppender, runtimeTypeForValue(importModuleValue))));
JSObject* object = jsCast<JSObject*>(importModuleValue);
JSValue value = object->get(exec, import.field);
RETURN_IF_EXCEPTION(throwScope, { });
switch (import.kind) {
case Wasm::ExternalKind::Function: {
if (!value.isFunction())
return JSValue::encode(throwException(exec, throwScope, createTypeError(exec, ASCIILiteral("import function must be callable"), defaultSourceAppender, runtimeTypeForValue(value))));
JSCell* cell = value.asCell();
if (WebAssemblyFunction* importedExports = jsDynamicCast<WebAssemblyFunction*>(object)) {
RELEASE_ASSERT_NOT_REACHED();
UNUSED_PARAM(importedExports);
break;
}
instance->setImportFunction(vm, cell, numImportFunctions++);
break;
}
case Wasm::ExternalKind::Table: {
RELEASE_ASSERT(!hasTableImport); hasTableImport = true;
JSWebAssemblyTable* table = jsDynamicCast<JSWebAssemblyTable*>(value);
if (!table)
return JSValue::encode(throwException(exec, throwScope, createTypeError(exec, ASCIILiteral("Table import is not an instance of WebAssembly.Table"))));
uint32_t expectedInitial = moduleInformation.tableInformation.initial();
uint32_t actualInitial = table->size();
if (actualInitial < expectedInitial)
return JSValue::encode(throwException(exec, throwScope, createTypeError(exec, ASCIILiteral("Table import provided an 'initial' that is too small"))));
if (std::optional<uint32_t> expectedMaximum = moduleInformation.tableInformation.maximum()) {
std::optional<uint32_t> actualMaximum = table->maximum();
if (!actualMaximum) {
return JSValue::encode(
throwException(exec, throwScope, createTypeError(exec, ASCIILiteral("Table import does not have a 'maximum' but the module requires that it does"))));
}
if (*actualMaximum > *expectedMaximum) {
return JSValue::encode(
throwException(exec, throwScope, createTypeError(exec, ASCIILiteral("Imported Table's 'maximum' is larger than the module's expected 'maximum'"))));
}
}
instance->setTable(vm, table);
break;
}
case Wasm::ExternalKind::Memory: {
RELEASE_ASSERT(!hasMemoryImport); RELEASE_ASSERT(moduleInformation.memory);
hasMemoryImport = true;
JSWebAssemblyMemory* memory = jsDynamicCast<JSWebAssemblyMemory*>(value);
if (!memory)
return JSValue::encode(throwException(exec, throwScope, createTypeError(exec, ASCIILiteral("Memory import is not an instance of WebAssembly.Memory"))));
Wasm::PageCount expectedInitial = moduleInformation.memory.initial();
Wasm::PageCount actualInitial = memory->memory()->initial();
if (actualInitial < expectedInitial)
return JSValue::encode(throwException(exec, throwScope, createTypeError(exec, ASCIILiteral("Memory import provided an 'initial' that is too small"))));
if (Wasm::PageCount expectedMaximum = moduleInformation.memory.maximum()) {
Wasm::PageCount actualMaximum = memory->memory()->maximum();
if (!actualMaximum) {
return JSValue::encode(
throwException(exec, throwScope, createTypeError(exec, ASCIILiteral("Memory import did not have a 'maximum' but the module requires that it does"))));
}
if (actualMaximum > expectedMaximum) {
return JSValue::encode(
throwException(exec, throwScope, createTypeError(exec, ASCIILiteral("Memory imports 'maximum' is larger than the module's expected 'maximum"))));
}
}
instance->setMemory(vm, memory);
break;
}
case Wasm::ExternalKind::Global: {
ASSERT(moduleInformation.globals[import.kindIndex].mutability == Wasm::Global::Immutable);
if (!value.isNumber())
return JSValue::encode(throwException(exec, throwScope, createTypeError(exec, ASCIILiteral("imported global must be a number"), defaultSourceAppender, runtimeTypeForValue(value))));
switch (moduleInformation.globals[import.kindIndex].type) {
case Wasm::I32:
instance->setGlobal(numImportGlobals++, value.toInt32(exec));
break;
case Wasm::F32:
instance->setGlobal(numImportGlobals++, bitwise_cast<uint32_t>(value.toFloat(exec)));
break;
case Wasm::F64:
instance->setGlobal(numImportGlobals++, bitwise_cast<uint64_t>(value.asNumber()));
break;
default:
RELEASE_ASSERT_NOT_REACHED();
}
ASSERT(!throwScope.exception());
break;
}
}
}
{
if (!!moduleInformation.memory && moduleInformation.memory.isImport()) {
RELEASE_ASSERT(hasMemoryImport);
}
if (moduleInformation.memory && !hasMemoryImport) {
RELEASE_ASSERT(!moduleInformation.memory.isImport());
std::unique_ptr<Wasm::Memory> memory = std::make_unique<Wasm::Memory>(moduleInformation.memory.initial(), moduleInformation.memory.maximum());
if (!memory->isValid())
return JSValue::encode(throwException(exec, throwScope, createOutOfMemoryError(exec)));
instance->setMemory(vm,
JSWebAssemblyMemory::create(vm, exec->lexicalGlobalObject()->WebAssemblyMemoryStructure(), WTFMove(memory)));
}
}
{
if (!!moduleInformation.tableInformation && moduleInformation.tableInformation.isImport()) {
RELEASE_ASSERT(hasTableImport);
}
if (!!moduleInformation.tableInformation && !hasTableImport) {
RELEASE_ASSERT(!moduleInformation.tableInformation.isImport());
JSWebAssemblyTable* table = JSWebAssemblyTable::create(exec, vm, exec->lexicalGlobalObject()->WebAssemblyTableStructure(),
moduleInformation.tableInformation.initial(), moduleInformation.tableInformation.maximum());
ASSERT(!throwScope.exception());
ASSERT(table);
instance->setTable(vm, table);
}
}
{
ASSERT(numImportGlobals == moduleInformation.firstInternalGlobal);
for (size_t globalIndex = numImportGlobals; globalIndex < moduleInformation.globals.size(); ++globalIndex) {
const auto& global = moduleInformation.globals[globalIndex];
ASSERT(global.initializationType != Wasm::Global::IsImport);
if (global.initializationType == Wasm::Global::FromGlobalImport) {
ASSERT(global.initialBitsOrImportNumber < numImportGlobals);
instance->setGlobal(globalIndex, instance->loadI64Global(global.initialBitsOrImportNumber));
} else
instance->setGlobal(globalIndex, global.initialBitsOrImportNumber);
}
}
moduleRecord->link(exec, instance);
RETURN_IF_EXCEPTION(throwScope, { });
if (verbose)
moduleRecord->dump();
JSValue startResult = moduleRecord->evaluate(exec);
UNUSED_PARAM(startResult);
RETURN_IF_EXCEPTION(throwScope, { });
return JSValue::encode(instance);
}
static EncodedJSValue JSC_HOST_CALL callJSWebAssemblyInstance(ExecState* state)
{
VM& vm = state->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
return JSValue::encode(throwConstructorCannotBeCalledAsFunctionTypeError(state, scope, "WebAssembly.Instance"));
}
WebAssemblyInstanceConstructor* WebAssemblyInstanceConstructor::create(VM& vm, Structure* structure, WebAssemblyInstancePrototype* thisPrototype)
{
auto* constructor = new (NotNull, allocateCell<WebAssemblyInstanceConstructor>(vm.heap)) WebAssemblyInstanceConstructor(vm, structure);
constructor->finishCreation(vm, thisPrototype);
return constructor;
}
Structure* WebAssemblyInstanceConstructor::createStructure(VM& vm, JSGlobalObject* globalObject, JSValue prototype)
{
return Structure::create(vm, globalObject, prototype, TypeInfo(ObjectType, StructureFlags), info());
}
void WebAssemblyInstanceConstructor::finishCreation(VM& vm, WebAssemblyInstancePrototype* prototype)
{
Base::finishCreation(vm, ASCIILiteral("Instance"));
putDirectWithoutTransition(vm, vm.propertyNames->prototype, prototype, DontEnum | DontDelete | ReadOnly);
putDirectWithoutTransition(vm, vm.propertyNames->length, jsNumber(1), ReadOnly | DontEnum | DontDelete);
}
WebAssemblyInstanceConstructor::WebAssemblyInstanceConstructor(VM& vm, Structure* structure)
: Base(vm, structure)
{
}
ConstructType WebAssemblyInstanceConstructor::getConstructData(JSCell*, ConstructData& constructData)
{
constructData.native.function = constructJSWebAssemblyInstance;
return ConstructType::Host;
}
CallType WebAssemblyInstanceConstructor::getCallData(JSCell*, CallData& callData)
{
callData.native.function = callJSWebAssemblyInstance;
return CallType::Host;
}
void WebAssemblyInstanceConstructor::visitChildren(JSCell* cell, SlotVisitor& visitor)
{
auto* thisObject = jsCast<WebAssemblyInstanceConstructor*>(cell);
ASSERT_GC_OBJECT_INHERITS(thisObject, info());
Base::visitChildren(thisObject, visitor);
}
}
#endif // ENABLE(WEBASSEMBLY)