1 | /* |
2 | * Copyright (C) 2016-2018 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 "WebAssemblyMemoryConstructor.h" |
28 | |
29 | #if ENABLE(WEBASSEMBLY) |
30 | |
31 | #include "FunctionPrototype.h" |
32 | #include "JSCInlines.h" |
33 | #include "JSWebAssemblyHelpers.h" |
34 | #include "JSWebAssemblyMemory.h" |
35 | #include "WasmMemory.h" |
36 | #include "WasmPageCount.h" |
37 | #include "WebAssemblyMemoryPrototype.h" |
38 | #include <wtf/Optional.h> |
39 | |
40 | #include "WebAssemblyMemoryConstructor.lut.h" |
41 | |
42 | namespace JSC { |
43 | |
44 | const ClassInfo WebAssemblyMemoryConstructor::s_info = { "Function" , &Base::s_info, &constructorTableWebAssemblyMemory, nullptr, CREATE_METHOD_TABLE(WebAssemblyMemoryConstructor) }; |
45 | |
46 | /* Source for WebAssemblyMemoryConstructor.lut.h |
47 | @begin constructorTableWebAssemblyMemory |
48 | @end |
49 | */ |
50 | |
51 | static EncodedJSValue JSC_HOST_CALL constructJSWebAssemblyMemory(ExecState* exec) |
52 | { |
53 | VM& vm = exec->vm(); |
54 | auto throwScope = DECLARE_THROW_SCOPE(vm); |
55 | if (exec->argumentCount() != 1) |
56 | return JSValue::encode(throwException(exec, throwScope, createTypeError(exec, "WebAssembly.Memory expects exactly one argument"_s ))); |
57 | |
58 | JSObject* memoryDescriptor; |
59 | { |
60 | JSValue argument = exec->argument(0); |
61 | if (!argument.isObject()) |
62 | return JSValue::encode(throwException(exec, throwScope, createTypeError(exec, "WebAssembly.Memory expects its first argument to be an object"_s ))); |
63 | memoryDescriptor = jsCast<JSObject*>(argument); |
64 | } |
65 | |
66 | Wasm::PageCount initialPageCount; |
67 | { |
68 | Identifier initial = Identifier::fromString(&vm, "initial" ); |
69 | JSValue minSizeValue = memoryDescriptor->get(exec, initial); |
70 | RETURN_IF_EXCEPTION(throwScope, encodedJSValue()); |
71 | uint32_t size = toNonWrappingUint32(exec, minSizeValue); |
72 | RETURN_IF_EXCEPTION(throwScope, encodedJSValue()); |
73 | if (!Wasm::PageCount::isValid(size)) |
74 | return JSValue::encode(throwException(exec, throwScope, createRangeError(exec, "WebAssembly.Memory 'initial' page count is too large"_s ))); |
75 | if (Wasm::PageCount(size).bytes() > MAX_ARRAY_BUFFER_SIZE) |
76 | return JSValue::encode(throwException(exec, throwScope, createOutOfMemoryError(exec))); |
77 | initialPageCount = Wasm::PageCount(size); |
78 | } |
79 | |
80 | Wasm::PageCount maximumPageCount; |
81 | { |
82 | Identifier maximum = Identifier::fromString(&vm, "maximum" ); |
83 | bool hasProperty = memoryDescriptor->hasProperty(exec, maximum); |
84 | RETURN_IF_EXCEPTION(throwScope, encodedJSValue()); |
85 | if (hasProperty) { |
86 | JSValue maxSizeValue = memoryDescriptor->get(exec, maximum); |
87 | RETURN_IF_EXCEPTION(throwScope, encodedJSValue()); |
88 | uint32_t size = toNonWrappingUint32(exec, maxSizeValue); |
89 | RETURN_IF_EXCEPTION(throwScope, encodedJSValue()); |
90 | if (!Wasm::PageCount::isValid(size)) |
91 | return JSValue::encode(throwException(exec, throwScope, createRangeError(exec, "WebAssembly.Memory 'maximum' page count is too large"_s ))); |
92 | maximumPageCount = Wasm::PageCount(size); |
93 | |
94 | if (initialPageCount > maximumPageCount) { |
95 | return JSValue::encode(throwException(exec, throwScope, |
96 | createRangeError(exec, "'maximum' page count must be than greater than or equal to the 'initial' page count"_s ))); |
97 | } |
98 | } |
99 | } |
100 | |
101 | auto* jsMemory = JSWebAssemblyMemory::create(exec, vm, exec->lexicalGlobalObject()->webAssemblyMemoryStructure()); |
102 | RETURN_IF_EXCEPTION(throwScope, encodedJSValue()); |
103 | |
104 | RefPtr<Wasm::Memory> memory = Wasm::Memory::tryCreate(initialPageCount, maximumPageCount, |
105 | [&vm] (Wasm::Memory::NotifyPressure) { vm.heap.collectAsync(CollectionScope::Full); }, |
106 | [&vm] (Wasm::Memory::SyncTryToReclaim) { vm.heap.collectSync(CollectionScope::Full); }, |
107 | [&vm, jsMemory] (Wasm::Memory::GrowSuccess, Wasm::PageCount oldPageCount, Wasm::PageCount newPageCount) { jsMemory->growSuccessCallback(vm, oldPageCount, newPageCount); }); |
108 | if (!memory) |
109 | return JSValue::encode(throwException(exec, throwScope, createOutOfMemoryError(exec))); |
110 | |
111 | jsMemory->adopt(memory.releaseNonNull()); |
112 | |
113 | return JSValue::encode(jsMemory); |
114 | } |
115 | |
116 | static EncodedJSValue JSC_HOST_CALL callJSWebAssemblyMemory(ExecState* exec) |
117 | { |
118 | VM& vm = exec->vm(); |
119 | auto throwScope = DECLARE_THROW_SCOPE(vm); |
120 | return JSValue::encode(throwConstructorCannotBeCalledAsFunctionTypeError(exec, throwScope, "WebAssembly.Memory" )); |
121 | } |
122 | |
123 | WebAssemblyMemoryConstructor* WebAssemblyMemoryConstructor::create(VM& vm, Structure* structure, WebAssemblyMemoryPrototype* thisPrototype) |
124 | { |
125 | auto* constructor = new (NotNull, allocateCell<WebAssemblyMemoryConstructor>(vm.heap)) WebAssemblyMemoryConstructor(vm, structure); |
126 | constructor->finishCreation(vm, thisPrototype); |
127 | return constructor; |
128 | } |
129 | |
130 | Structure* WebAssemblyMemoryConstructor::createStructure(VM& vm, JSGlobalObject* globalObject, JSValue prototype) |
131 | { |
132 | return Structure::create(vm, globalObject, prototype, TypeInfo(InternalFunctionType, StructureFlags), info()); |
133 | } |
134 | |
135 | void WebAssemblyMemoryConstructor::finishCreation(VM& vm, WebAssemblyMemoryPrototype* prototype) |
136 | { |
137 | Base::finishCreation(vm, "Memory"_s , NameVisibility::Visible, NameAdditionMode::WithoutStructureTransition); |
138 | putDirectWithoutTransition(vm, vm.propertyNames->prototype, prototype, PropertyAttribute::DontEnum | PropertyAttribute::DontDelete | PropertyAttribute::ReadOnly); |
139 | putDirectWithoutTransition(vm, vm.propertyNames->length, jsNumber(1), PropertyAttribute::ReadOnly | PropertyAttribute::DontEnum | PropertyAttribute::DontDelete); |
140 | } |
141 | |
142 | WebAssemblyMemoryConstructor::WebAssemblyMemoryConstructor(VM& vm, Structure* structure) |
143 | : Base(vm, structure, callJSWebAssemblyMemory, constructJSWebAssemblyMemory) |
144 | { |
145 | } |
146 | |
147 | } // namespace JSC |
148 | |
149 | #endif // ENABLE(WEBASSEMBLY) |
150 | |
151 | |