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 | #pragma once |
27 | |
28 | #if ENABLE(WEBASSEMBLY) |
29 | |
30 | #include "JSArrayBuffer.h" |
31 | #include "JSCJSValue.h" |
32 | #include "JSSourceCode.h" |
33 | #include "WebAssemblyFunction.h" |
34 | #include "WebAssemblyWrapperFunction.h" |
35 | |
36 | namespace JSC { |
37 | |
38 | ALWAYS_INLINE uint32_t toNonWrappingUint32(ExecState* exec, JSValue value) |
39 | { |
40 | VM& vm = exec->vm(); |
41 | auto throwScope = DECLARE_THROW_SCOPE(vm); |
42 | double doubleValue = value.toInteger(exec); |
43 | RETURN_IF_EXCEPTION(throwScope, { }); |
44 | if (doubleValue < 0 || doubleValue > UINT_MAX) { |
45 | throwException(exec, throwScope, |
46 | createRangeError(exec, "Expect an integer argument in the range: [0, 2^32 - 1]"_s )); |
47 | return { }; |
48 | } |
49 | |
50 | return static_cast<uint32_t>(doubleValue); |
51 | } |
52 | |
53 | ALWAYS_INLINE std::pair<const uint8_t*, size_t> getWasmBufferFromValue(ExecState* exec, JSValue value) |
54 | { |
55 | VM& vm = exec->vm(); |
56 | auto throwScope = DECLARE_THROW_SCOPE(vm); |
57 | |
58 | if (auto* source = jsDynamicCast<JSSourceCode*>(vm, value)) { |
59 | auto* provider = static_cast<WebAssemblySourceProvider*>(source->sourceCode().provider()); |
60 | return { provider->data().data(), provider->data().size() }; |
61 | } |
62 | |
63 | // If the given bytes argument is not a BufferSource, a TypeError exception is thrown. |
64 | JSArrayBuffer* arrayBuffer = value.getObject() ? jsDynamicCast<JSArrayBuffer*>(vm, value.getObject()) : nullptr; |
65 | JSArrayBufferView* arrayBufferView = value.getObject() ? jsDynamicCast<JSArrayBufferView*>(vm, value.getObject()) : nullptr; |
66 | if (!(arrayBuffer || arrayBufferView)) { |
67 | throwException(exec, throwScope, createTypeError(exec, |
68 | "first argument must be an ArrayBufferView or an ArrayBuffer"_s , defaultSourceAppender, runtimeTypeForValue(vm, value))); |
69 | return { nullptr, 0 }; |
70 | } |
71 | |
72 | if (arrayBufferView ? arrayBufferView->isNeutered() : arrayBuffer->impl()->isNeutered()) { |
73 | throwException(exec, throwScope, createTypeError(exec, |
74 | "underlying TypedArray has been detatched from the ArrayBuffer"_s , defaultSourceAppender, runtimeTypeForValue(vm, value))); |
75 | return { nullptr, 0 }; |
76 | } |
77 | |
78 | uint8_t* base = arrayBufferView ? static_cast<uint8_t*>(arrayBufferView->vector()) : static_cast<uint8_t*>(arrayBuffer->impl()->data()); |
79 | size_t byteSize = arrayBufferView ? arrayBufferView->length() : arrayBuffer->impl()->byteLength(); |
80 | return { base, byteSize }; |
81 | } |
82 | |
83 | ALWAYS_INLINE Vector<uint8_t> createSourceBufferFromValue(VM& vm, ExecState* exec, JSValue value) |
84 | { |
85 | auto throwScope = DECLARE_THROW_SCOPE(vm); |
86 | const uint8_t* data; |
87 | size_t byteSize; |
88 | std::tie(data, byteSize) = getWasmBufferFromValue(exec, value); |
89 | RETURN_IF_EXCEPTION(throwScope, Vector<uint8_t>()); |
90 | |
91 | Vector<uint8_t> result; |
92 | if (!result.tryReserveCapacity(byteSize)) { |
93 | throwException(exec, throwScope, createOutOfMemoryError(exec)); |
94 | return result; |
95 | } |
96 | |
97 | result.grow(byteSize); |
98 | memcpy(result.data(), data, byteSize); |
99 | return result; |
100 | } |
101 | |
102 | ALWAYS_INLINE bool isWebAssemblyHostFunction(VM& vm, JSObject* object, WebAssemblyFunction*& wasmFunction, WebAssemblyWrapperFunction*& wasmWrapperFunction) |
103 | { |
104 | if (object->inherits<WebAssemblyFunction>(vm)) { |
105 | wasmFunction = jsCast<WebAssemblyFunction*>(object); |
106 | wasmWrapperFunction = nullptr; |
107 | return true; |
108 | } |
109 | if (object->inherits<WebAssemblyWrapperFunction>(vm)) { |
110 | wasmWrapperFunction = jsCast<WebAssemblyWrapperFunction*>(object); |
111 | wasmFunction = nullptr; |
112 | return true; |
113 | } |
114 | return false; |
115 | } |
116 | |
117 | ALWAYS_INLINE bool isWebAssemblyHostFunction(VM& vm, JSValue value, WebAssemblyFunction*& wasmFunction, WebAssemblyWrapperFunction*& wasmWrapperFunction) |
118 | { |
119 | if (!value.isObject()) |
120 | return false; |
121 | return isWebAssemblyHostFunction(vm, jsCast<JSObject*>(value), wasmFunction, wasmWrapperFunction); |
122 | } |
123 | |
124 | |
125 | ALWAYS_INLINE bool isWebAssemblyHostFunction(VM& vm, JSObject* object) |
126 | { |
127 | WebAssemblyFunction* unused; |
128 | WebAssemblyWrapperFunction* unused2; |
129 | return isWebAssemblyHostFunction(vm, object, unused, unused2); |
130 | } |
131 | |
132 | } // namespace JSC |
133 | |
134 | #endif // ENABLE(WEBASSEMBLY) |
135 | |