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 "WebAssemblyInstanceConstructor.h"
28
29#if ENABLE(WEBASSEMBLY)
30
31#include "FunctionPrototype.h"
32#include "JSCInlines.h"
33#include "JSModuleEnvironment.h"
34#include "JSModuleNamespaceObject.h"
35#include "JSToWasm.h"
36#include "JSWebAssemblyHelpers.h"
37#include "JSWebAssemblyInstance.h"
38#include "JSWebAssemblyLinkError.h"
39#include "JSWebAssemblyMemory.h"
40#include "JSWebAssemblyModule.h"
41#include "WasmPlan.h"
42#include "WasmToJS.h"
43#include "WasmWorklist.h"
44#include "WebAssemblyFunction.h"
45#include "WebAssemblyInstancePrototype.h"
46#include "WebAssemblyModuleRecord.h"
47
48#include "WebAssemblyInstanceConstructor.lut.h"
49
50namespace JSC {
51
52const ClassInfo WebAssemblyInstanceConstructor::s_info = { "Function", &Base::s_info, &constructorTableWebAssemblyInstance, nullptr, CREATE_METHOD_TABLE(WebAssemblyInstanceConstructor) };
53
54/* Source for WebAssemblyInstanceConstructor.lut.h
55 @begin constructorTableWebAssemblyInstance
56 @end
57 */
58
59using Wasm::Plan;
60
61static EncodedJSValue JSC_HOST_CALL constructJSWebAssemblyInstance(ExecState* exec)
62{
63 VM& vm = exec->vm();
64 auto scope = DECLARE_THROW_SCOPE(vm);
65
66 // If moduleObject is not a WebAssembly.Module instance, a TypeError is thrown.
67 JSWebAssemblyModule* module = jsDynamicCast<JSWebAssemblyModule*>(vm, exec->argument(0));
68 if (!module)
69 return JSValue::encode(throwException(exec, scope, createTypeError(exec, "first argument to WebAssembly.Instance must be a WebAssembly.Module"_s, defaultSourceAppender, runtimeTypeForValue(vm, exec->argument(0)))));
70
71 // If the importObject parameter is not undefined and Type(importObject) is not Object, a TypeError is thrown.
72 JSValue importArgument = exec->argument(1);
73 JSObject* importObject = importArgument.getObject();
74 if (!importArgument.isUndefined() && !importObject)
75 return JSValue::encode(throwException(exec, scope, createTypeError(exec, "second argument to WebAssembly.Instance must be undefined or an Object"_s, defaultSourceAppender, runtimeTypeForValue(vm, importArgument))));
76
77 Structure* instanceStructure = InternalFunction::createSubclassStructure(exec, exec->newTarget(), exec->lexicalGlobalObject()->webAssemblyInstanceStructure());
78 RETURN_IF_EXCEPTION(scope, { });
79
80 JSWebAssemblyInstance* instance = JSWebAssemblyInstance::create(vm, exec, JSWebAssemblyInstance::createPrivateModuleKey(), module, importObject, instanceStructure, Ref<Wasm::Module>(module->module()), Wasm::CreationMode::FromJS);
81 RETURN_IF_EXCEPTION(scope, { });
82
83 instance->finalizeCreation(vm, exec, module->module().compileSync(&vm.wasmContext, instance->memoryMode(), &Wasm::createJSToWasmWrapper, &Wasm::wasmToJSException), importObject, Wasm::CreationMode::FromJS);
84 RETURN_IF_EXCEPTION(scope, { });
85 return JSValue::encode(instance);
86}
87
88static EncodedJSValue JSC_HOST_CALL callJSWebAssemblyInstance(ExecState* exec)
89{
90 VM& vm = exec->vm();
91 auto scope = DECLARE_THROW_SCOPE(vm);
92 return JSValue::encode(throwConstructorCannotBeCalledAsFunctionTypeError(exec, scope, "WebAssembly.Instance"));
93}
94
95WebAssemblyInstanceConstructor* WebAssemblyInstanceConstructor::create(VM& vm, Structure* structure, WebAssemblyInstancePrototype* thisPrototype)
96{
97 auto* constructor = new (NotNull, allocateCell<WebAssemblyInstanceConstructor>(vm.heap)) WebAssemblyInstanceConstructor(vm, structure);
98 constructor->finishCreation(vm, thisPrototype);
99 return constructor;
100}
101
102Structure* WebAssemblyInstanceConstructor::createStructure(VM& vm, JSGlobalObject* globalObject, JSValue prototype)
103{
104 return Structure::create(vm, globalObject, prototype, TypeInfo(InternalFunctionType, StructureFlags), info());
105}
106
107void WebAssemblyInstanceConstructor::finishCreation(VM& vm, WebAssemblyInstancePrototype* prototype)
108{
109 Base::finishCreation(vm, "Instance"_s, NameVisibility::Visible, NameAdditionMode::WithoutStructureTransition);
110 putDirectWithoutTransition(vm, vm.propertyNames->prototype, prototype, PropertyAttribute::DontEnum | PropertyAttribute::DontDelete | PropertyAttribute::ReadOnly);
111 putDirectWithoutTransition(vm, vm.propertyNames->length, jsNumber(1), PropertyAttribute::ReadOnly | PropertyAttribute::DontEnum | PropertyAttribute::DontDelete);
112}
113
114WebAssemblyInstanceConstructor::WebAssemblyInstanceConstructor(VM& vm, Structure* structure)
115 : Base(vm, structure, callJSWebAssemblyInstance, constructJSWebAssemblyInstance)
116{
117}
118
119} // namespace JSC
120
121#endif // ENABLE(WEBASSEMBLY)
122
123