1 | /* |
2 | This file is part of the WebKit open source project. |
3 | This file has been generated by generate-bindings.pl. DO NOT MODIFY! |
4 | |
5 | This library is free software; you can redistribute it and/or |
6 | modify it under the terms of the GNU Library General Public |
7 | License as published by the Free Software Foundation; either |
8 | version 2 of the License, or (at your option) any later version. |
9 | |
10 | This library is distributed in the hope that it will be useful, |
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
13 | Library General Public License for more details. |
14 | |
15 | You should have received a copy of the GNU Library General Public License |
16 | along with this library; see the file COPYING.LIB. If not, write to |
17 | the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
18 | Boston, MA 02110-1301, USA. |
19 | */ |
20 | |
21 | #include "config.h" |
22 | #include "JSCrypto.h" |
23 | |
24 | #include "JSDOMAttribute.h" |
25 | #include "JSDOMBinding.h" |
26 | #include "JSDOMConstructorNotConstructable.h" |
27 | #include "JSDOMConvertBufferSource.h" |
28 | #include "JSDOMExceptionHandling.h" |
29 | #include "JSDOMOperation.h" |
30 | #include "JSDOMWrapperCache.h" |
31 | #include "ScriptExecutionContext.h" |
32 | #include <JavaScriptCore/FunctionPrototype.h> |
33 | #include <JavaScriptCore/HeapSnapshotBuilder.h> |
34 | #include <JavaScriptCore/JSCInlines.h> |
35 | #include <wtf/GetPtr.h> |
36 | #include <wtf/PointerPreparations.h> |
37 | #include <wtf/URL.h> |
38 | |
39 | #if ENABLE(WEB_CRYPTO) |
40 | #include "JSDOMConvertInterface.h" |
41 | #include "JSDOMGlobalObject.h" |
42 | #include "JSSubtleCrypto.h" |
43 | #endif |
44 | |
45 | |
46 | namespace WebCore { |
47 | using namespace JSC; |
48 | |
49 | // Functions |
50 | |
51 | JSC::EncodedJSValue JSC_HOST_CALL jsCryptoPrototypeFunctionGetRandomValues(JSC::ExecState*); |
52 | |
53 | // Attributes |
54 | |
55 | JSC::EncodedJSValue jsCryptoConstructor(JSC::ExecState*, JSC::EncodedJSValue, JSC::PropertyName); |
56 | bool setJSCryptoConstructor(JSC::ExecState*, JSC::EncodedJSValue, JSC::EncodedJSValue); |
57 | #if ENABLE(WEB_CRYPTO) |
58 | JSC::EncodedJSValue jsCryptoSubtle(JSC::ExecState*, JSC::EncodedJSValue, JSC::PropertyName); |
59 | #endif |
60 | |
61 | class JSCryptoPrototype : public JSC::JSNonFinalObject { |
62 | public: |
63 | using Base = JSC::JSNonFinalObject; |
64 | static JSCryptoPrototype* create(JSC::VM& vm, JSDOMGlobalObject* globalObject, JSC::Structure* structure) |
65 | { |
66 | JSCryptoPrototype* ptr = new (NotNull, JSC::allocateCell<JSCryptoPrototype>(vm.heap)) JSCryptoPrototype(vm, globalObject, structure); |
67 | ptr->finishCreation(vm); |
68 | return ptr; |
69 | } |
70 | |
71 | DECLARE_INFO; |
72 | static JSC::Structure* createStructure(JSC::VM& vm, JSC::JSGlobalObject* globalObject, JSC::JSValue prototype) |
73 | { |
74 | return JSC::Structure::create(vm, globalObject, prototype, JSC::TypeInfo(JSC::ObjectType, StructureFlags), info()); |
75 | } |
76 | |
77 | private: |
78 | JSCryptoPrototype(JSC::VM& vm, JSC::JSGlobalObject*, JSC::Structure* structure) |
79 | : JSC::JSNonFinalObject(vm, structure) |
80 | { |
81 | } |
82 | |
83 | void finishCreation(JSC::VM&); |
84 | }; |
85 | |
86 | using JSCryptoConstructor = JSDOMConstructorNotConstructable<JSCrypto>; |
87 | |
88 | template<> JSValue JSCryptoConstructor::prototypeForStructure(JSC::VM& vm, const JSDOMGlobalObject& globalObject) |
89 | { |
90 | UNUSED_PARAM(vm); |
91 | return globalObject.functionPrototype(); |
92 | } |
93 | |
94 | template<> void JSCryptoConstructor::initializeProperties(VM& vm, JSDOMGlobalObject& globalObject) |
95 | { |
96 | putDirect(vm, vm.propertyNames->prototype, JSCrypto::prototype(vm, globalObject), JSC::PropertyAttribute::DontDelete | JSC::PropertyAttribute::ReadOnly | JSC::PropertyAttribute::DontEnum); |
97 | putDirect(vm, vm.propertyNames->name, jsNontrivialString(&vm, String("Crypto"_s )), JSC::PropertyAttribute::ReadOnly | JSC::PropertyAttribute::DontEnum); |
98 | putDirect(vm, vm.propertyNames->length, jsNumber(0), JSC::PropertyAttribute::ReadOnly | JSC::PropertyAttribute::DontEnum); |
99 | } |
100 | |
101 | template<> const ClassInfo JSCryptoConstructor::s_info = { "Crypto" , &Base::s_info, nullptr, nullptr, CREATE_METHOD_TABLE(JSCryptoConstructor) }; |
102 | |
103 | /* Hash table for prototype */ |
104 | |
105 | static const HashTableValue JSCryptoPrototypeTableValues[] = |
106 | { |
107 | { "constructor" , static_cast<unsigned>(JSC::PropertyAttribute::DontEnum), NoIntrinsic, { (intptr_t)static_cast<PropertySlot::GetValueFunc>(jsCryptoConstructor), (intptr_t) static_cast<PutPropertySlot::PutValueFunc>(setJSCryptoConstructor) } }, |
108 | #if ENABLE(WEB_CRYPTO) |
109 | { "subtle" , static_cast<unsigned>(JSC::PropertyAttribute::ReadOnly | JSC::PropertyAttribute::CustomAccessor | JSC::PropertyAttribute::DOMAttribute), NoIntrinsic, { (intptr_t)static_cast<PropertySlot::GetValueFunc>(jsCryptoSubtle), (intptr_t) static_cast<PutPropertySlot::PutValueFunc>(0) } }, |
110 | #else |
111 | { 0, 0, NoIntrinsic, { 0, 0 } }, |
112 | #endif |
113 | { "getRandomValues" , static_cast<unsigned>(JSC::PropertyAttribute::Function), NoIntrinsic, { (intptr_t)static_cast<RawNativeFunction>(jsCryptoPrototypeFunctionGetRandomValues), (intptr_t) (1) } }, |
114 | }; |
115 | |
116 | const ClassInfo JSCryptoPrototype::s_info = { "CryptoPrototype" , &Base::s_info, nullptr, nullptr, CREATE_METHOD_TABLE(JSCryptoPrototype) }; |
117 | |
118 | void JSCryptoPrototype::finishCreation(VM& vm) |
119 | { |
120 | Base::finishCreation(vm); |
121 | reifyStaticProperties(vm, JSCrypto::info(), JSCryptoPrototypeTableValues, *this); |
122 | } |
123 | |
124 | const ClassInfo JSCrypto::s_info = { "Crypto" , &Base::s_info, nullptr, nullptr, CREATE_METHOD_TABLE(JSCrypto) }; |
125 | |
126 | JSCrypto::JSCrypto(Structure* structure, JSDOMGlobalObject& globalObject, Ref<Crypto>&& impl) |
127 | : JSDOMWrapper<Crypto>(structure, globalObject, WTFMove(impl)) |
128 | { |
129 | } |
130 | |
131 | void JSCrypto::finishCreation(VM& vm) |
132 | { |
133 | Base::finishCreation(vm); |
134 | ASSERT(inherits(vm, info())); |
135 | |
136 | } |
137 | |
138 | JSObject* JSCrypto::createPrototype(VM& vm, JSDOMGlobalObject& globalObject) |
139 | { |
140 | return JSCryptoPrototype::create(vm, &globalObject, JSCryptoPrototype::createStructure(vm, &globalObject, globalObject.objectPrototype())); |
141 | } |
142 | |
143 | JSObject* JSCrypto::prototype(VM& vm, JSDOMGlobalObject& globalObject) |
144 | { |
145 | return getDOMPrototype<JSCrypto>(vm, globalObject); |
146 | } |
147 | |
148 | JSValue JSCrypto::getConstructor(VM& vm, const JSGlobalObject* globalObject) |
149 | { |
150 | return getDOMConstructor<JSCryptoConstructor>(vm, *jsCast<const JSDOMGlobalObject*>(globalObject)); |
151 | } |
152 | |
153 | void JSCrypto::destroy(JSC::JSCell* cell) |
154 | { |
155 | JSCrypto* thisObject = static_cast<JSCrypto*>(cell); |
156 | thisObject->JSCrypto::~JSCrypto(); |
157 | } |
158 | |
159 | template<> inline JSCrypto* IDLAttribute<JSCrypto>::cast(ExecState& state, EncodedJSValue thisValue) |
160 | { |
161 | return jsDynamicCast<JSCrypto*>(state.vm(), JSValue::decode(thisValue)); |
162 | } |
163 | |
164 | template<> inline JSCrypto* IDLOperation<JSCrypto>::cast(ExecState& state) |
165 | { |
166 | return jsDynamicCast<JSCrypto*>(state.vm(), state.thisValue()); |
167 | } |
168 | |
169 | EncodedJSValue jsCryptoConstructor(ExecState* state, EncodedJSValue thisValue, PropertyName) |
170 | { |
171 | VM& vm = state->vm(); |
172 | auto throwScope = DECLARE_THROW_SCOPE(vm); |
173 | auto* prototype = jsDynamicCast<JSCryptoPrototype*>(vm, JSValue::decode(thisValue)); |
174 | if (UNLIKELY(!prototype)) |
175 | return throwVMTypeError(state, throwScope); |
176 | return JSValue::encode(JSCrypto::getConstructor(state->vm(), prototype->globalObject())); |
177 | } |
178 | |
179 | bool setJSCryptoConstructor(ExecState* state, EncodedJSValue thisValue, EncodedJSValue encodedValue) |
180 | { |
181 | VM& vm = state->vm(); |
182 | auto throwScope = DECLARE_THROW_SCOPE(vm); |
183 | auto* prototype = jsDynamicCast<JSCryptoPrototype*>(vm, JSValue::decode(thisValue)); |
184 | if (UNLIKELY(!prototype)) { |
185 | throwVMTypeError(state, throwScope); |
186 | return false; |
187 | } |
188 | // Shadowing a built-in constructor |
189 | return prototype->putDirect(vm, vm.propertyNames->constructor, JSValue::decode(encodedValue)); |
190 | } |
191 | |
192 | #if ENABLE(WEB_CRYPTO) |
193 | static inline JSValue jsCryptoSubtleGetter(ExecState& state, JSCrypto& thisObject, ThrowScope& throwScope) |
194 | { |
195 | UNUSED_PARAM(throwScope); |
196 | UNUSED_PARAM(state); |
197 | auto& impl = thisObject.wrapped(); |
198 | JSValue result = toJS<IDLInterface<SubtleCrypto>>(state, *thisObject.globalObject(), throwScope, impl.subtle()); |
199 | return result; |
200 | } |
201 | |
202 | EncodedJSValue jsCryptoSubtle(ExecState* state, EncodedJSValue thisValue, PropertyName) |
203 | { |
204 | return IDLAttribute<JSCrypto>::get<jsCryptoSubtleGetter, CastedThisErrorBehavior::Assert>(*state, thisValue, "subtle" ); |
205 | } |
206 | |
207 | #endif |
208 | |
209 | static inline JSC::EncodedJSValue jsCryptoPrototypeFunctionGetRandomValuesBody(JSC::ExecState* state, typename IDLOperation<JSCrypto>::ClassParameter castedThis, JSC::ThrowScope& throwScope) |
210 | { |
211 | UNUSED_PARAM(state); |
212 | UNUSED_PARAM(throwScope); |
213 | auto& impl = castedThis->wrapped(); |
214 | if (UNLIKELY(state->argumentCount() < 1)) |
215 | return throwVMError(state, throwScope, createNotEnoughArgumentsError(state)); |
216 | auto returnValue = state->uncheckedArgument(0); |
217 | auto array = convert<IDLArrayBufferView>(*state, returnValue, [](JSC::ExecState& state, JSC::ThrowScope& scope) { throwArgumentTypeError(state, scope, 0, "array" , "Crypto" , "getRandomValues" , "ArrayBufferView" ); }); |
218 | RETURN_IF_EXCEPTION(throwScope, encodedJSValue()); |
219 | propagateException(*state, throwScope, impl.getRandomValues(array.releaseNonNull())); |
220 | return JSValue::encode(returnValue); |
221 | } |
222 | |
223 | EncodedJSValue JSC_HOST_CALL jsCryptoPrototypeFunctionGetRandomValues(ExecState* state) |
224 | { |
225 | return IDLOperation<JSCrypto>::call<jsCryptoPrototypeFunctionGetRandomValuesBody>(*state, "getRandomValues" ); |
226 | } |
227 | |
228 | void JSCrypto::heapSnapshot(JSCell* cell, HeapSnapshotBuilder& builder) |
229 | { |
230 | auto* thisObject = jsCast<JSCrypto*>(cell); |
231 | builder.setWrappedObjectForCell(cell, &thisObject->wrapped()); |
232 | if (thisObject->scriptExecutionContext()) |
233 | builder.setLabelForCell(cell, "url " + thisObject->scriptExecutionContext()->url().string()); |
234 | Base::heapSnapshot(cell, builder); |
235 | } |
236 | |
237 | bool JSCryptoOwner::isReachableFromOpaqueRoots(JSC::Handle<JSC::Unknown> handle, void*, SlotVisitor& visitor, const char** reason) |
238 | { |
239 | auto* jsCrypto = jsCast<JSCrypto*>(handle.slot()->asCell()); |
240 | ScriptExecutionContext* root = WTF::getPtr(jsCrypto->wrapped().scriptExecutionContext()); |
241 | if (!root) |
242 | return false; |
243 | if (UNLIKELY(reason)) |
244 | *reason = "Reachable from ScriptExecutionContext" ; |
245 | return visitor.containsOpaqueRoot(root); |
246 | } |
247 | |
248 | void JSCryptoOwner::finalize(JSC::Handle<JSC::Unknown> handle, void* context) |
249 | { |
250 | auto* jsCrypto = static_cast<JSCrypto*>(handle.slot()->asCell()); |
251 | auto& world = *static_cast<DOMWrapperWorld*>(context); |
252 | uncacheWrapper(world, &jsCrypto->wrapped(), jsCrypto); |
253 | } |
254 | |
255 | #if ENABLE(BINDING_INTEGRITY) |
256 | #if PLATFORM(WIN) |
257 | #pragma warning(disable: 4483) |
258 | extern "C" { extern void (*const __identifier("??_7Crypto@WebCore@@6B@" )[])(); } |
259 | #else |
260 | extern "C" { extern void* _ZTVN7WebCore6CryptoE[]; } |
261 | #endif |
262 | #endif |
263 | |
264 | JSC::JSValue toJSNewlyCreated(JSC::ExecState*, JSDOMGlobalObject* globalObject, Ref<Crypto>&& impl) |
265 | { |
266 | |
267 | #if ENABLE(BINDING_INTEGRITY) |
268 | void* actualVTablePointer = *(reinterpret_cast<void**>(impl.ptr())); |
269 | #if PLATFORM(WIN) |
270 | void* expectedVTablePointer = WTF_PREPARE_VTBL_POINTER_FOR_INSPECTION(__identifier("??_7Crypto@WebCore@@6B@" )); |
271 | #else |
272 | void* expectedVTablePointer = WTF_PREPARE_VTBL_POINTER_FOR_INSPECTION(&_ZTVN7WebCore6CryptoE[2]); |
273 | #endif |
274 | |
275 | // If this fails Crypto does not have a vtable, so you need to add the |
276 | // ImplementationLacksVTable attribute to the interface definition |
277 | static_assert(std::is_polymorphic<Crypto>::value, "Crypto is not polymorphic" ); |
278 | |
279 | // If you hit this assertion you either have a use after free bug, or |
280 | // Crypto has subclasses. If Crypto has subclasses that get passed |
281 | // to toJS() we currently require Crypto you to opt out of binding hardening |
282 | // by adding the SkipVTableValidation attribute to the interface IDL definition |
283 | RELEASE_ASSERT(actualVTablePointer == expectedVTablePointer); |
284 | #endif |
285 | return createWrapper<Crypto>(globalObject, WTFMove(impl)); |
286 | } |
287 | |
288 | JSC::JSValue toJS(JSC::ExecState* state, JSDOMGlobalObject* globalObject, Crypto& impl) |
289 | { |
290 | return wrap(state, globalObject, impl); |
291 | } |
292 | |
293 | Crypto* JSCrypto::toWrapped(JSC::VM& vm, JSC::JSValue value) |
294 | { |
295 | if (auto* wrapper = jsDynamicCast<JSCrypto*>(vm, value)) |
296 | return &wrapper->wrapped(); |
297 | return nullptr; |
298 | } |
299 | |
300 | } |
301 | |