| 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 "JSWebAssemblyModule.h" |
| 28 | |
| 29 | #if ENABLE(WEBASSEMBLY) |
| 30 | |
| 31 | #include "JSCInlines.h" |
| 32 | #include "JSWebAssemblyCodeBlock.h" |
| 33 | #include "JSWebAssemblyCompileError.h" |
| 34 | #include "JSWebAssemblyMemory.h" |
| 35 | #include "WasmCallee.h" |
| 36 | #include "WasmFormat.h" |
| 37 | #include "WasmMemory.h" |
| 38 | #include "WasmModule.h" |
| 39 | #include "WasmPlan.h" |
| 40 | #include "WebAssemblyToJSCallee.h" |
| 41 | #include <wtf/StdLibExtras.h> |
| 42 | |
| 43 | namespace JSC { |
| 44 | |
| 45 | const ClassInfo JSWebAssemblyModule::s_info = { "WebAssembly.Module" , &Base::s_info, nullptr, nullptr, CREATE_METHOD_TABLE(JSWebAssemblyModule) }; |
| 46 | |
| 47 | JSWebAssemblyModule* JSWebAssemblyModule::createStub(VM& vm, ExecState* exec, Structure* structure, Wasm::Module::ValidationResult&& result) |
| 48 | { |
| 49 | auto scope = DECLARE_THROW_SCOPE(vm); |
| 50 | if (!result.has_value()) { |
| 51 | auto* error = JSWebAssemblyCompileError::create(exec, vm, structure->globalObject()->webAssemblyCompileErrorStructure(), result.error()); |
| 52 | RETURN_IF_EXCEPTION(scope, nullptr); |
| 53 | throwException(exec, scope, error); |
| 54 | return nullptr; |
| 55 | } |
| 56 | |
| 57 | auto* module = new (NotNull, allocateCell<JSWebAssemblyModule>(vm.heap)) JSWebAssemblyModule(vm, structure, result.value().releaseNonNull()); |
| 58 | module->finishCreation(vm); |
| 59 | return module; |
| 60 | } |
| 61 | |
| 62 | Structure* JSWebAssemblyModule::createStructure(VM& vm, JSGlobalObject* globalObject, JSValue prototype) |
| 63 | { |
| 64 | return Structure::create(vm, globalObject, prototype, TypeInfo(ObjectType, StructureFlags), info()); |
| 65 | } |
| 66 | |
| 67 | |
| 68 | JSWebAssemblyModule::JSWebAssemblyModule(VM& vm, Structure* structure, Ref<Wasm::Module>&& module) |
| 69 | : Base(vm, structure) |
| 70 | , m_module(WTFMove(module)) |
| 71 | { |
| 72 | } |
| 73 | |
| 74 | void JSWebAssemblyModule::finishCreation(VM& vm) |
| 75 | { |
| 76 | Base::finishCreation(vm); |
| 77 | ASSERT(inherits(vm, info())); |
| 78 | |
| 79 | // On success, a new WebAssembly.Module object is returned with [[Module]] set to the validated Ast.module. |
| 80 | SymbolTable* exportSymbolTable = SymbolTable::create(vm); |
| 81 | const Wasm::ModuleInformation& moduleInformation = m_module->moduleInformation(); |
| 82 | for (auto& exp : moduleInformation.exports) { |
| 83 | auto offset = exportSymbolTable->takeNextScopeOffset(NoLockingNecessary); |
| 84 | String field = String::fromUTF8(exp.field); |
| 85 | exportSymbolTable->set(NoLockingNecessary, AtomicString(field).impl(), SymbolTableEntry(VarOffset(offset))); |
| 86 | } |
| 87 | |
| 88 | m_exportSymbolTable.set(vm, this, exportSymbolTable); |
| 89 | m_callee.set(vm, this, WebAssemblyToJSCallee::create(vm, globalObject(vm)->webAssemblyToJSCalleeStructure(), this)); |
| 90 | } |
| 91 | |
| 92 | void JSWebAssemblyModule::destroy(JSCell* cell) |
| 93 | { |
| 94 | static_cast<JSWebAssemblyModule*>(cell)->JSWebAssemblyModule::~JSWebAssemblyModule(); |
| 95 | Wasm::SignatureInformation::tryCleanup(); |
| 96 | } |
| 97 | |
| 98 | const Wasm::ModuleInformation& JSWebAssemblyModule::moduleInformation() const |
| 99 | { |
| 100 | return m_module->moduleInformation(); |
| 101 | } |
| 102 | |
| 103 | SymbolTable* JSWebAssemblyModule::exportSymbolTable() const |
| 104 | { |
| 105 | return m_exportSymbolTable.get(); |
| 106 | } |
| 107 | |
| 108 | Wasm::SignatureIndex JSWebAssemblyModule::signatureIndexFromFunctionIndexSpace(unsigned functionIndexSpace) const |
| 109 | { |
| 110 | return m_module->signatureIndexFromFunctionIndexSpace(functionIndexSpace); |
| 111 | } |
| 112 | |
| 113 | WebAssemblyToJSCallee* JSWebAssemblyModule::callee() const |
| 114 | { |
| 115 | return m_callee.get(); |
| 116 | } |
| 117 | |
| 118 | JSWebAssemblyCodeBlock* JSWebAssemblyModule::codeBlock(Wasm::MemoryMode mode) |
| 119 | { |
| 120 | return m_codeBlocks[static_cast<size_t>(mode)].get(); |
| 121 | } |
| 122 | |
| 123 | Wasm::Module& JSWebAssemblyModule::module() |
| 124 | { |
| 125 | return m_module.get(); |
| 126 | } |
| 127 | |
| 128 | void JSWebAssemblyModule::setCodeBlock(VM& vm, Wasm::MemoryMode mode, JSWebAssemblyCodeBlock* codeBlock) |
| 129 | { |
| 130 | m_codeBlocks[static_cast<size_t>(mode)].set(vm, this, codeBlock); |
| 131 | } |
| 132 | |
| 133 | void JSWebAssemblyModule::visitChildren(JSCell* cell, SlotVisitor& visitor) |
| 134 | { |
| 135 | JSWebAssemblyModule* thisObject = jsCast<JSWebAssemblyModule*>(cell); |
| 136 | ASSERT_GC_OBJECT_INHERITS(thisObject, info()); |
| 137 | |
| 138 | Base::visitChildren(thisObject, visitor); |
| 139 | visitor.append(thisObject->m_exportSymbolTable); |
| 140 | visitor.append(thisObject->m_callee); |
| 141 | for (unsigned i = 0; i < Wasm::NumberOfMemoryModes; ++i) |
| 142 | visitor.append(thisObject->m_codeBlocks[i]); |
| 143 | } |
| 144 | |
| 145 | } // namespace JSC |
| 146 | |
| 147 | #endif // ENABLE(WEBASSEMBLY) |
| 148 | |