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 "WebAssemblyMemoryPrototype.h"
28
29#if ENABLE(WEBASSEMBLY)
30
31#include "FunctionPrototype.h"
32#include "JSArrayBuffer.h"
33#include "JSCInlines.h"
34#include "JSWebAssemblyMemory.h"
35#include "JSWebAssemblyHelpers.h"
36
37namespace JSC {
38static EncodedJSValue JSC_HOST_CALL webAssemblyMemoryProtoFuncGrow(ExecState*);
39static EncodedJSValue JSC_HOST_CALL webAssemblyMemoryProtoFuncBuffer(ExecState*);
40}
41
42#include "WebAssemblyMemoryPrototype.lut.h"
43
44
45namespace JSC {
46
47const ClassInfo WebAssemblyMemoryPrototype::s_info = { "WebAssembly.Memory", &Base::s_info, &prototypeTableWebAssemblyMemory, nullptr, CREATE_METHOD_TABLE(WebAssemblyMemoryPrototype) };
48
49/* Source for WebAssemblyMemoryPrototype.lut.h
50@begin prototypeTableWebAssemblyMemory
51 grow webAssemblyMemoryProtoFuncGrow DontEnum|Function 1
52 buffer webAssemblyMemoryProtoFuncBuffer DontEnum|Accessor 0
53@end
54*/
55
56ALWAYS_INLINE JSWebAssemblyMemory* getMemory(ExecState* exec, VM& vm, JSValue value)
57{
58 auto throwScope = DECLARE_THROW_SCOPE(vm);
59
60 JSWebAssemblyMemory* memory = jsDynamicCast<JSWebAssemblyMemory*>(vm, value);
61 if (!memory) {
62 throwException(exec, throwScope,
63 createTypeError(exec, "WebAssembly.Memory.prototype.buffer getter called with non WebAssembly.Memory |this| value"_s));
64 return nullptr;
65 }
66 return memory;
67}
68
69EncodedJSValue JSC_HOST_CALL webAssemblyMemoryProtoFuncGrow(ExecState* exec)
70{
71 VM& vm = exec->vm();
72 auto throwScope = DECLARE_THROW_SCOPE(vm);
73
74 JSWebAssemblyMemory* memory = getMemory(exec, vm, exec->thisValue());
75 RETURN_IF_EXCEPTION(throwScope, { });
76
77 uint32_t delta = toNonWrappingUint32(exec, exec->argument(0));
78 RETURN_IF_EXCEPTION(throwScope, { });
79
80 Wasm::PageCount result = memory->grow(vm, exec, delta);
81 RETURN_IF_EXCEPTION(throwScope, { });
82
83 return JSValue::encode(jsNumber(result.pageCount()));
84}
85
86EncodedJSValue JSC_HOST_CALL webAssemblyMemoryProtoFuncBuffer(ExecState* exec)
87{
88 VM& vm = exec->vm();
89 auto throwScope = DECLARE_THROW_SCOPE(vm);
90
91 JSWebAssemblyMemory* memory = getMemory(exec, vm, exec->thisValue());
92 RETURN_IF_EXCEPTION(throwScope, { });
93 return JSValue::encode(memory->buffer(exec->vm(), exec->lexicalGlobalObject()));
94}
95
96WebAssemblyMemoryPrototype* WebAssemblyMemoryPrototype::create(VM& vm, JSGlobalObject*, Structure* structure)
97{
98 auto* object = new (NotNull, allocateCell<WebAssemblyMemoryPrototype>(vm.heap)) WebAssemblyMemoryPrototype(vm, structure);
99 object->finishCreation(vm);
100 return object;
101}
102
103Structure* WebAssemblyMemoryPrototype::createStructure(VM& vm, JSGlobalObject* globalObject, JSValue prototype)
104{
105 return Structure::create(vm, globalObject, prototype, TypeInfo(ObjectType, StructureFlags), info());
106}
107
108void WebAssemblyMemoryPrototype::finishCreation(VM& vm)
109{
110 Base::finishCreation(vm);
111 ASSERT(inherits(vm, info()));
112}
113
114WebAssemblyMemoryPrototype::WebAssemblyMemoryPrototype(VM& vm, Structure* structure)
115 : Base(vm, structure)
116{
117}
118
119} // namespace JSC
120
121#endif // ENABLE(WEBASSEMBLY)
122