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