| 1 | /* |
| 2 | * Copyright (C) 2016-2017 Apple Inc. All rights reserved. |
| 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions |
| 6 | * are met: |
| 7 | * 1. Redistributions of source code must retain the above copyright |
| 8 | * notice, this list of conditions and the following disclaimer. |
| 9 | * 2. Redistributions in binary form must reproduce the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer in the |
| 11 | * documentation and/or other materials provided with the distribution. |
| 12 | * |
| 13 | * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY |
| 14 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 15 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 16 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR |
| 17 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 18 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 19 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 20 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
| 21 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 22 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 23 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 24 | */ |
| 25 | |
| 26 | #include "config.h" |
| 27 | #include "WebAssemblyModuleConstructor.h" |
| 28 | |
| 29 | #if ENABLE(WEBASSEMBLY) |
| 30 | |
| 31 | #include "ArrayBuffer.h" |
| 32 | #include "ExceptionHelpers.h" |
| 33 | #include "FunctionPrototype.h" |
| 34 | #include "JSArrayBuffer.h" |
| 35 | #include "JSCInlines.h" |
| 36 | #include "JSTypedArrays.h" |
| 37 | #include "JSWebAssemblyCompileError.h" |
| 38 | #include "JSWebAssemblyHelpers.h" |
| 39 | #include "JSWebAssemblyModule.h" |
| 40 | #include "ObjectConstructor.h" |
| 41 | #include "SymbolTable.h" |
| 42 | #include "WasmCallee.h" |
| 43 | #include "WasmModuleInformation.h" |
| 44 | #include "WasmPlan.h" |
| 45 | #include "WebAssemblyModulePrototype.h" |
| 46 | #include <wtf/StdLibExtras.h> |
| 47 | |
| 48 | namespace JSC { |
| 49 | static EncodedJSValue JSC_HOST_CALL webAssemblyModuleCustomSections(ExecState*); |
| 50 | static EncodedJSValue JSC_HOST_CALL webAssemblyModuleImports(ExecState*); |
| 51 | static EncodedJSValue JSC_HOST_CALL webAssemblyModuleExports(ExecState*); |
| 52 | } |
| 53 | |
| 54 | #include "WebAssemblyModuleConstructor.lut.h" |
| 55 | |
| 56 | namespace JSC { |
| 57 | |
| 58 | const ClassInfo WebAssemblyModuleConstructor::s_info = { "Function" , &Base::s_info, &constructorTableWebAssemblyModule, nullptr, CREATE_METHOD_TABLE(WebAssemblyModuleConstructor) }; |
| 59 | |
| 60 | /* Source for WebAssemblyModuleConstructor.lut.h |
| 61 | @begin constructorTableWebAssemblyModule |
| 62 | customSections webAssemblyModuleCustomSections DontEnum|Function 2 |
| 63 | imports webAssemblyModuleImports DontEnum|Function 1 |
| 64 | exports webAssemblyModuleExports DontEnum|Function 1 |
| 65 | @end |
| 66 | */ |
| 67 | |
| 68 | EncodedJSValue JSC_HOST_CALL webAssemblyModuleCustomSections(ExecState* exec) |
| 69 | { |
| 70 | VM& vm = exec->vm(); |
| 71 | auto* globalObject = exec->lexicalGlobalObject(); |
| 72 | auto throwScope = DECLARE_THROW_SCOPE(vm); |
| 73 | |
| 74 | JSWebAssemblyModule* module = jsDynamicCast<JSWebAssemblyModule*>(vm, exec->argument(0)); |
| 75 | if (!module) |
| 76 | return JSValue::encode(throwException(exec, throwScope, createTypeError(exec, "WebAssembly.Module.customSections called with non WebAssembly.Module argument"_s ))); |
| 77 | |
| 78 | const String sectionNameString = exec->argument(1).getString(exec); |
| 79 | RETURN_IF_EXCEPTION(throwScope, { }); |
| 80 | |
| 81 | JSArray* result = constructEmptyArray(exec, nullptr, globalObject); |
| 82 | RETURN_IF_EXCEPTION(throwScope, { }); |
| 83 | |
| 84 | const auto& customSections = module->moduleInformation().customSections; |
| 85 | for (const Wasm::CustomSection& section : customSections) { |
| 86 | if (String::fromUTF8(section.name) == sectionNameString) { |
| 87 | auto buffer = ArrayBuffer::tryCreate(section.payload.data(), section.payload.size()); |
| 88 | if (!buffer) |
| 89 | return JSValue::encode(throwException(exec, throwScope, createOutOfMemoryError(exec))); |
| 90 | |
| 91 | result->push(exec, JSArrayBuffer::create(vm, globalObject->arrayBufferStructure(ArrayBufferSharingMode::Default), WTFMove(buffer))); |
| 92 | RETURN_IF_EXCEPTION(throwScope, { }); |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | return JSValue::encode(result); |
| 97 | } |
| 98 | |
| 99 | EncodedJSValue JSC_HOST_CALL webAssemblyModuleImports(ExecState* exec) |
| 100 | { |
| 101 | VM& vm = exec->vm(); |
| 102 | auto* globalObject = exec->lexicalGlobalObject(); |
| 103 | auto throwScope = DECLARE_THROW_SCOPE(vm); |
| 104 | |
| 105 | JSWebAssemblyModule* module = jsDynamicCast<JSWebAssemblyModule*>(vm, exec->argument(0)); |
| 106 | if (!module) |
| 107 | return JSValue::encode(throwException(exec, throwScope, createTypeError(exec, "WebAssembly.Module.imports called with non WebAssembly.Module argument"_s ))); |
| 108 | |
| 109 | JSArray* result = constructEmptyArray(exec, nullptr, globalObject); |
| 110 | RETURN_IF_EXCEPTION(throwScope, { }); |
| 111 | |
| 112 | const auto& imports = module->moduleInformation().imports; |
| 113 | if (imports.size()) { |
| 114 | Identifier module = Identifier::fromString(exec, "module" ); |
| 115 | Identifier name = Identifier::fromString(exec, "name" ); |
| 116 | Identifier kind = Identifier::fromString(exec, "kind" ); |
| 117 | for (const Wasm::Import& imp : imports) { |
| 118 | JSObject* obj = constructEmptyObject(exec); |
| 119 | RETURN_IF_EXCEPTION(throwScope, { }); |
| 120 | obj->putDirect(vm, module, jsString(exec, String::fromUTF8(imp.module))); |
| 121 | obj->putDirect(vm, name, jsString(exec, String::fromUTF8(imp.field))); |
| 122 | obj->putDirect(vm, kind, jsString(exec, String(makeString(imp.kind)))); |
| 123 | result->push(exec, obj); |
| 124 | RETURN_IF_EXCEPTION(throwScope, { }); |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | return JSValue::encode(result); |
| 129 | } |
| 130 | |
| 131 | EncodedJSValue JSC_HOST_CALL webAssemblyModuleExports(ExecState* exec) |
| 132 | { |
| 133 | VM& vm = exec->vm(); |
| 134 | auto* globalObject = exec->lexicalGlobalObject(); |
| 135 | auto throwScope = DECLARE_THROW_SCOPE(vm); |
| 136 | |
| 137 | JSWebAssemblyModule* module = jsDynamicCast<JSWebAssemblyModule*>(vm, exec->argument(0)); |
| 138 | if (!module) |
| 139 | return JSValue::encode(throwException(exec, throwScope, createTypeError(exec, "WebAssembly.Module.exports called with non WebAssembly.Module argument"_s ))); |
| 140 | |
| 141 | JSArray* result = constructEmptyArray(exec, nullptr, globalObject); |
| 142 | RETURN_IF_EXCEPTION(throwScope, { }); |
| 143 | |
| 144 | const auto& exports = module->moduleInformation().exports; |
| 145 | if (exports.size()) { |
| 146 | Identifier name = Identifier::fromString(exec, "name" ); |
| 147 | Identifier kind = Identifier::fromString(exec, "kind" ); |
| 148 | for (const Wasm::Export& exp : exports) { |
| 149 | JSObject* obj = constructEmptyObject(exec); |
| 150 | RETURN_IF_EXCEPTION(throwScope, { }); |
| 151 | obj->putDirect(vm, name, jsString(exec, String::fromUTF8(exp.field))); |
| 152 | obj->putDirect(vm, kind, jsString(exec, String(makeString(exp.kind)))); |
| 153 | result->push(exec, obj); |
| 154 | RETURN_IF_EXCEPTION(throwScope, { }); |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | return JSValue::encode(result); |
| 159 | } |
| 160 | |
| 161 | static EncodedJSValue JSC_HOST_CALL constructJSWebAssemblyModule(ExecState* exec) |
| 162 | { |
| 163 | VM& vm = exec->vm(); |
| 164 | auto scope = DECLARE_THROW_SCOPE(vm); |
| 165 | |
| 166 | Vector<uint8_t> source = createSourceBufferFromValue(vm, exec, exec->argument(0)); |
| 167 | RETURN_IF_EXCEPTION(scope, { }); |
| 168 | |
| 169 | RELEASE_AND_RETURN(scope, JSValue::encode(WebAssemblyModuleConstructor::createModule(exec, WTFMove(source)))); |
| 170 | } |
| 171 | |
| 172 | static EncodedJSValue JSC_HOST_CALL callJSWebAssemblyModule(ExecState* exec) |
| 173 | { |
| 174 | VM& vm = exec->vm(); |
| 175 | auto scope = DECLARE_THROW_SCOPE(vm); |
| 176 | return JSValue::encode(throwConstructorCannotBeCalledAsFunctionTypeError(exec, scope, "WebAssembly.Module" )); |
| 177 | } |
| 178 | |
| 179 | JSWebAssemblyModule* WebAssemblyModuleConstructor::createModule(ExecState* exec, Vector<uint8_t>&& buffer) |
| 180 | { |
| 181 | VM& vm = exec->vm(); |
| 182 | auto scope = DECLARE_THROW_SCOPE(vm); |
| 183 | |
| 184 | auto* structure = InternalFunction::createSubclassStructure(exec, exec->newTarget(), exec->lexicalGlobalObject()->webAssemblyModuleStructure()); |
| 185 | RETURN_IF_EXCEPTION(scope, nullptr); |
| 186 | |
| 187 | RELEASE_AND_RETURN(scope, JSWebAssemblyModule::createStub(vm, exec, structure, Wasm::Module::validateSync(&vm.wasmContext, WTFMove(buffer)))); |
| 188 | } |
| 189 | |
| 190 | WebAssemblyModuleConstructor* WebAssemblyModuleConstructor::create(VM& vm, Structure* structure, WebAssemblyModulePrototype* thisPrototype) |
| 191 | { |
| 192 | auto* constructor = new (NotNull, allocateCell<WebAssemblyModuleConstructor>(vm.heap)) WebAssemblyModuleConstructor(vm, structure); |
| 193 | constructor->finishCreation(vm, thisPrototype); |
| 194 | return constructor; |
| 195 | } |
| 196 | |
| 197 | Structure* WebAssemblyModuleConstructor::createStructure(VM& vm, JSGlobalObject* globalObject, JSValue prototype) |
| 198 | { |
| 199 | return Structure::create(vm, globalObject, prototype, TypeInfo(InternalFunctionType, StructureFlags), info()); |
| 200 | } |
| 201 | |
| 202 | void WebAssemblyModuleConstructor::finishCreation(VM& vm, WebAssemblyModulePrototype* prototype) |
| 203 | { |
| 204 | Base::finishCreation(vm, "Module"_s , NameVisibility::Visible, NameAdditionMode::WithoutStructureTransition); |
| 205 | putDirectWithoutTransition(vm, vm.propertyNames->prototype, prototype, PropertyAttribute::DontEnum | PropertyAttribute::DontDelete | PropertyAttribute::ReadOnly); |
| 206 | putDirectWithoutTransition(vm, vm.propertyNames->length, jsNumber(1), PropertyAttribute::ReadOnly | PropertyAttribute::DontEnum | PropertyAttribute::DontDelete); |
| 207 | } |
| 208 | |
| 209 | WebAssemblyModuleConstructor::WebAssemblyModuleConstructor(VM& vm, Structure* structure) |
| 210 | : Base(vm, structure, callJSWebAssemblyModule, constructJSWebAssemblyModule) |
| 211 | { |
| 212 | } |
| 213 | |
| 214 | } // namespace JSC |
| 215 | |
| 216 | #endif // ENABLE(WEBASSEMBLY) |
| 217 | |
| 218 | |