/work/obj-fuzz/dom/bindings/SpeechRecognitionErrorBinding.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | /* THIS FILE IS AUTOGENERATED FROM SpeechRecognitionError.webidl BY Codegen.py - DO NOT EDIT */ |
2 | | |
3 | | #include "AtomList.h" |
4 | | #include "EventBinding.h" |
5 | | #include "SpeechRecognition.h" |
6 | | #include "SpeechRecognitionErrorBinding.h" |
7 | | #include "WrapperFactory.h" |
8 | | #include "jsapi.h" |
9 | | #include "mozilla/OwningNonNull.h" |
10 | | #include "mozilla/Preferences.h" |
11 | | #include "mozilla/dom/BindingUtils.h" |
12 | | #include "mozilla/dom/DOMJSClass.h" |
13 | | #include "mozilla/dom/NonRefcountedDOMObject.h" |
14 | | #include "mozilla/dom/Nullable.h" |
15 | | #include "mozilla/dom/PrimitiveConversions.h" |
16 | | #include "mozilla/dom/ScriptSettings.h" |
17 | | #include "mozilla/dom/SimpleGlobalObject.h" |
18 | | #include "mozilla/dom/SpeechRecognitionError.h" |
19 | | #include "mozilla/dom/XrayExpandoClass.h" |
20 | | |
21 | | namespace mozilla { |
22 | | namespace dom { |
23 | | |
24 | | namespace binding_detail {}; // Just to make sure it's known as a namespace |
25 | | using namespace mozilla::dom::binding_detail; |
26 | | |
27 | | |
28 | | namespace SpeechRecognitionErrorCodeValues { |
29 | | extern const EnumEntry strings[9] = { |
30 | | {"no-speech", 9}, |
31 | | {"aborted", 7}, |
32 | | {"audio-capture", 13}, |
33 | | {"network", 7}, |
34 | | {"not-allowed", 11}, |
35 | | {"service-not-allowed", 19}, |
36 | | {"bad-grammar", 11}, |
37 | | {"language-not-supported", 22}, |
38 | | { nullptr, 0 } |
39 | | }; |
40 | | } // namespace SpeechRecognitionErrorCodeValues |
41 | | |
42 | | bool |
43 | | ToJSValue(JSContext* aCx, SpeechRecognitionErrorCode aArgument, JS::MutableHandle<JS::Value> aValue) |
44 | 0 | { |
45 | 0 | MOZ_ASSERT(uint32_t(aArgument) < ArrayLength(SpeechRecognitionErrorCodeValues::strings)); |
46 | 0 | JSString* resultStr = |
47 | 0 | JS_NewStringCopyN(aCx, SpeechRecognitionErrorCodeValues::strings[uint32_t(aArgument)].value, |
48 | 0 | SpeechRecognitionErrorCodeValues::strings[uint32_t(aArgument)].length); |
49 | 0 | if (!resultStr) { |
50 | 0 | return false; |
51 | 0 | } |
52 | 0 | aValue.setString(resultStr); |
53 | 0 | return true; |
54 | 0 | } |
55 | | |
56 | | |
57 | | |
58 | | SpeechRecognitionErrorInit::SpeechRecognitionErrorInit() |
59 | | : EventInit(FastDictionaryInitializer()) |
60 | 0 | { |
61 | 0 | // Safe to pass a null context if we pass a null value |
62 | 0 | Init(nullptr, JS::NullHandleValue); |
63 | 0 | } |
64 | | |
65 | | |
66 | | |
67 | | bool |
68 | | SpeechRecognitionErrorInit::InitIds(JSContext* cx, SpeechRecognitionErrorInitAtoms* atomsCache) |
69 | 0 | { |
70 | 0 | MOZ_ASSERT(!*reinterpret_cast<jsid**>(atomsCache)); |
71 | 0 |
|
72 | 0 | // Initialize these in reverse order so that any failure leaves the first one |
73 | 0 | // uninitialized. |
74 | 0 | if (!atomsCache->message_id.init(cx, "message") || |
75 | 0 | !atomsCache->error_id.init(cx, "error")) { |
76 | 0 | return false; |
77 | 0 | } |
78 | 0 | return true; |
79 | 0 | } |
80 | | |
81 | | bool |
82 | | SpeechRecognitionErrorInit::Init(JSContext* cx, JS::Handle<JS::Value> val, const char* sourceDescription, bool passedToJSImpl) |
83 | 0 | { |
84 | 0 | // Passing a null JSContext is OK only if we're initing from null, |
85 | 0 | // Since in that case we will not have to do any property gets |
86 | 0 | // Also evaluate isNullOrUndefined in order to avoid false-positive |
87 | 0 | // checkers by static analysis tools |
88 | 0 | MOZ_ASSERT_IF(!cx, val.isNull() && val.isNullOrUndefined()); |
89 | 0 | SpeechRecognitionErrorInitAtoms* atomsCache = nullptr; |
90 | 0 | if (cx) { |
91 | 0 | atomsCache = GetAtomCache<SpeechRecognitionErrorInitAtoms>(cx); |
92 | 0 | if (!*reinterpret_cast<jsid**>(atomsCache) && !InitIds(cx, atomsCache)) { |
93 | 0 | return false; |
94 | 0 | } |
95 | 0 | } |
96 | 0 | |
97 | 0 | // Per spec, we init the parent's members first |
98 | 0 | if (!EventInit::Init(cx, val)) { |
99 | 0 | return false; |
100 | 0 | } |
101 | 0 | |
102 | 0 | bool isNull = val.isNullOrUndefined(); |
103 | 0 | // We only need these if !isNull, in which case we have |cx|. |
104 | 0 | Maybe<JS::Rooted<JSObject *> > object; |
105 | 0 | Maybe<JS::Rooted<JS::Value> > temp; |
106 | 0 | if (!isNull) { |
107 | 0 | MOZ_ASSERT(cx); |
108 | 0 | object.emplace(cx, &val.toObject()); |
109 | 0 | temp.emplace(cx); |
110 | 0 | } |
111 | 0 | if (!isNull) { |
112 | 0 | if (!JS_GetPropertyById(cx, *object, atomsCache->error_id, temp.ptr())) { |
113 | 0 | return false; |
114 | 0 | } |
115 | 0 | } |
116 | 0 | if (!isNull && !temp->isUndefined()) { |
117 | 0 | { |
118 | 0 | int index; |
119 | 0 | if (!FindEnumStringIndex<true>(cx, temp.ref(), SpeechRecognitionErrorCodeValues::strings, "SpeechRecognitionErrorCode", "'error' member of SpeechRecognitionErrorInit", &index)) { |
120 | 0 | return false; |
121 | 0 | } |
122 | 0 | MOZ_ASSERT(index >= 0); |
123 | 0 | mError = static_cast<SpeechRecognitionErrorCode>(index); |
124 | 0 | } |
125 | 0 | } else { |
126 | 0 | mError = SpeechRecognitionErrorCode::No_speech; |
127 | 0 | } |
128 | 0 | mIsAnyMemberPresent = true; |
129 | 0 |
|
130 | 0 | if (!isNull) { |
131 | 0 | if (!JS_GetPropertyById(cx, *object, atomsCache->message_id, temp.ptr())) { |
132 | 0 | return false; |
133 | 0 | } |
134 | 0 | } |
135 | 0 | if (!isNull && !temp->isUndefined()) { |
136 | 0 | if (!ConvertJSValueToString(cx, temp.ref(), eStringify, eStringify, mMessage)) { |
137 | 0 | return false; |
138 | 0 | } |
139 | 0 | } else { |
140 | 0 | static const char16_t data[] = { 0 }; |
141 | 0 | mMessage.Rebind(data, ArrayLength(data) - 1); |
142 | 0 | } |
143 | 0 | mIsAnyMemberPresent = true; |
144 | 0 | return true; |
145 | 0 | } |
146 | | |
147 | | bool |
148 | | SpeechRecognitionErrorInit::Init(const nsAString& aJSON) |
149 | 0 | { |
150 | 0 | AutoJSAPI jsapi; |
151 | 0 | JSObject* cleanGlobal = SimpleGlobalObject::Create(SimpleGlobalObject::GlobalType::BindingDetail); |
152 | 0 | if (!cleanGlobal) { |
153 | 0 | return false; |
154 | 0 | } |
155 | 0 | if (!jsapi.Init(cleanGlobal)) { |
156 | 0 | return false; |
157 | 0 | } |
158 | 0 | JSContext* cx = jsapi.cx(); |
159 | 0 | JS::Rooted<JS::Value> json(cx); |
160 | 0 | bool ok = ParseJSON(cx, aJSON, &json); |
161 | 0 | NS_ENSURE_TRUE(ok, false); |
162 | 0 | return Init(cx, json); |
163 | 0 | } |
164 | | |
165 | | bool |
166 | | SpeechRecognitionErrorInit::ToObjectInternal(JSContext* cx, JS::MutableHandle<JS::Value> rval) const |
167 | 0 | { |
168 | 0 | SpeechRecognitionErrorInitAtoms* atomsCache = GetAtomCache<SpeechRecognitionErrorInitAtoms>(cx); |
169 | 0 | if (!*reinterpret_cast<jsid**>(atomsCache) && !InitIds(cx, atomsCache)) { |
170 | 0 | return false; |
171 | 0 | } |
172 | 0 | |
173 | 0 | // Per spec, we define the parent's members first |
174 | 0 | if (!EventInit::ToObjectInternal(cx, rval)) { |
175 | 0 | return false; |
176 | 0 | } |
177 | 0 | JS::Rooted<JSObject*> obj(cx, &rval.toObject()); |
178 | 0 |
|
179 | 0 | do { |
180 | 0 | // block for our 'break' successCode and scope for 'temp' and 'currentValue' |
181 | 0 | JS::Rooted<JS::Value> temp(cx); |
182 | 0 | SpeechRecognitionErrorCode const & currentValue = mError; |
183 | 0 | if (!ToJSValue(cx, currentValue, &temp)) { |
184 | 0 | return false; |
185 | 0 | } |
186 | 0 | if (!JS_DefinePropertyById(cx, obj, atomsCache->error_id, temp, JSPROP_ENUMERATE)) { |
187 | 0 | return false; |
188 | 0 | } |
189 | 0 | break; |
190 | 0 | } while(false); |
191 | 0 |
|
192 | 0 | do { |
193 | 0 | // block for our 'break' successCode and scope for 'temp' and 'currentValue' |
194 | 0 | JS::Rooted<JS::Value> temp(cx); |
195 | 0 | nsString const & currentValue = mMessage; |
196 | 0 | if (!xpc::NonVoidStringToJsval(cx, currentValue, &temp)) { |
197 | 0 | return false; |
198 | 0 | } |
199 | 0 | if (!JS_DefinePropertyById(cx, obj, atomsCache->message_id, temp, JSPROP_ENUMERATE)) { |
200 | 0 | return false; |
201 | 0 | } |
202 | 0 | break; |
203 | 0 | } while(false); |
204 | 0 |
|
205 | 0 | return true; |
206 | 0 | } |
207 | | |
208 | | bool |
209 | | SpeechRecognitionErrorInit::ToJSON(nsAString& aJSON) const |
210 | 0 | { |
211 | 0 | AutoJSAPI jsapi; |
212 | 0 | jsapi.Init(); |
213 | 0 | JSContext *cx = jsapi.cx(); |
214 | 0 | // It's safe to use UnprivilegedJunkScopeOrWorkerGlobal here |
215 | 0 | // because we'll only be creating objects, in ways that have no |
216 | 0 | // side-effects, followed by a call to JS::ToJSONMaybeSafely, |
217 | 0 | // which likewise guarantees no side-effects for the sorts of |
218 | 0 | // things we will pass it. |
219 | 0 | JSAutoRealm ar(cx, UnprivilegedJunkScopeOrWorkerGlobal()); |
220 | 0 | JS::Rooted<JS::Value> val(cx); |
221 | 0 | if (!ToObjectInternal(cx, &val)) { |
222 | 0 | return false; |
223 | 0 | } |
224 | 0 | JS::Rooted<JSObject*> obj(cx, &val.toObject()); |
225 | 0 | return StringifyToJSON(cx, obj, aJSON); |
226 | 0 | } |
227 | | |
228 | | void |
229 | | SpeechRecognitionErrorInit::TraceDictionary(JSTracer* trc) |
230 | 0 | { |
231 | 0 | EventInit::TraceDictionary(trc); |
232 | 0 | } |
233 | | |
234 | | SpeechRecognitionErrorInit& |
235 | | SpeechRecognitionErrorInit::operator=(const SpeechRecognitionErrorInit& aOther) |
236 | 0 | { |
237 | 0 | EventInit::operator=(aOther); |
238 | 0 | mError = aOther.mError; |
239 | 0 | mMessage = aOther.mMessage; |
240 | 0 | return *this; |
241 | 0 | } |
242 | | |
243 | | namespace binding_detail { |
244 | | } // namespace binding_detail |
245 | | |
246 | | |
247 | | namespace SpeechRecognitionError_Binding { |
248 | | |
249 | | static_assert(IsRefcounted<NativeType>::value == IsRefcounted<Event_Binding::NativeType>::value, |
250 | | "Can't inherit from an interface with a different ownership model."); |
251 | | |
252 | | MOZ_CAN_RUN_SCRIPT static bool |
253 | | get_error(JSContext* cx, JS::Handle<JSObject*> obj, mozilla::dom::SpeechRecognitionError* self, JSJitGetterCallArgs args) |
254 | 0 | { |
255 | 0 | AUTO_PROFILER_LABEL_FAST("get SpeechRecognitionError.error", DOM, cx); |
256 | 0 |
|
257 | 0 | SpeechRecognitionErrorCode result(self->Error()); |
258 | 0 | MOZ_ASSERT(!JS_IsExceptionPending(cx)); |
259 | 0 | if (!ToJSValue(cx, result, args.rval())) { |
260 | 0 | return false; |
261 | 0 | } |
262 | 0 | return true; |
263 | 0 | } |
264 | | |
265 | | static const JSJitInfo error_getterinfo = { |
266 | | { (JSJitGetterOp)get_error }, |
267 | | { prototypes::id::SpeechRecognitionError }, |
268 | | { PrototypeTraits<prototypes::id::SpeechRecognitionError>::Depth }, |
269 | | JSJitInfo::Getter, |
270 | | JSJitInfo::AliasEverything, /* aliasSet. Not relevant for setters. */ |
271 | | JSVAL_TYPE_STRING, /* returnType. Not relevant for setters. */ |
272 | | false, /* isInfallible. False in setters. */ |
273 | | false, /* isMovable. Not relevant for setters. */ |
274 | | false, /* isEliminatable. Not relevant for setters. */ |
275 | | false, /* isAlwaysInSlot. Only relevant for getters. */ |
276 | | false, /* isLazilyCachedInSlot. Only relevant for getters. */ |
277 | | false, /* isTypedMethod. Only relevant for methods. */ |
278 | | 0 /* Reserved slot index, if we're stored in a slot, else 0. */ |
279 | | }; |
280 | | static_assert(0 <= JSJitInfo::maxSlotIndex, "We won't fit"); |
281 | | static_assert(0 < 1, "There is no slot for us"); |
282 | | |
283 | | MOZ_CAN_RUN_SCRIPT static bool |
284 | | get_message(JSContext* cx, JS::Handle<JSObject*> obj, mozilla::dom::SpeechRecognitionError* self, JSJitGetterCallArgs args) |
285 | 0 | { |
286 | 0 | AUTO_PROFILER_LABEL_FAST("get SpeechRecognitionError.message", DOM, cx); |
287 | 0 |
|
288 | 0 | DOMString result; |
289 | 0 | self->GetMessage(result); |
290 | 0 | MOZ_ASSERT(!JS_IsExceptionPending(cx)); |
291 | 0 | if (!xpc::StringToJsval(cx, result, args.rval())) { |
292 | 0 | return false; |
293 | 0 | } |
294 | 0 | return true; |
295 | 0 | } |
296 | | |
297 | | static const JSJitInfo message_getterinfo = { |
298 | | { (JSJitGetterOp)get_message }, |
299 | | { prototypes::id::SpeechRecognitionError }, |
300 | | { PrototypeTraits<prototypes::id::SpeechRecognitionError>::Depth }, |
301 | | JSJitInfo::Getter, |
302 | | JSJitInfo::AliasEverything, /* aliasSet. Not relevant for setters. */ |
303 | | JSVAL_TYPE_UNKNOWN, /* returnType. Not relevant for setters. */ |
304 | | false, /* isInfallible. False in setters. */ |
305 | | false, /* isMovable. Not relevant for setters. */ |
306 | | false, /* isEliminatable. Not relevant for setters. */ |
307 | | false, /* isAlwaysInSlot. Only relevant for getters. */ |
308 | | false, /* isLazilyCachedInSlot. Only relevant for getters. */ |
309 | | false, /* isTypedMethod. Only relevant for methods. */ |
310 | | 0 /* Reserved slot index, if we're stored in a slot, else 0. */ |
311 | | }; |
312 | | static_assert(0 <= JSJitInfo::maxSlotIndex, "We won't fit"); |
313 | | static_assert(0 < 1, "There is no slot for us"); |
314 | | |
315 | | MOZ_CAN_RUN_SCRIPT static bool |
316 | | get_isTrusted(JSContext* cx, JS::Handle<JSObject*> obj, mozilla::dom::SpeechRecognitionError* self, JSJitGetterCallArgs args) |
317 | 0 | { |
318 | 0 | AUTO_PROFILER_LABEL_FAST("get SpeechRecognitionError.isTrusted", DOM, cx); |
319 | 0 |
|
320 | 0 | bool result(self->IsTrusted()); |
321 | 0 | MOZ_ASSERT(!JS_IsExceptionPending(cx)); |
322 | 0 | args.rval().setBoolean(result); |
323 | 0 | return true; |
324 | 0 | } |
325 | | |
326 | | static const JSJitInfo isTrusted_getterinfo = { |
327 | | { (JSJitGetterOp)get_isTrusted }, |
328 | | { prototypes::id::SpeechRecognitionError }, |
329 | | { PrototypeTraits<prototypes::id::SpeechRecognitionError>::Depth }, |
330 | | JSJitInfo::Getter, |
331 | | JSJitInfo::AliasDOMSets, /* aliasSet. Not relevant for setters. */ |
332 | | JSVAL_TYPE_BOOLEAN, /* returnType. Not relevant for setters. */ |
333 | | true, /* isInfallible. False in setters. */ |
334 | | true, /* isMovable. Not relevant for setters. */ |
335 | | true, /* isEliminatable. Not relevant for setters. */ |
336 | | false, /* isAlwaysInSlot. Only relevant for getters. */ |
337 | | false, /* isLazilyCachedInSlot. Only relevant for getters. */ |
338 | | false, /* isTypedMethod. Only relevant for methods. */ |
339 | | 0 /* Reserved slot index, if we're stored in a slot, else 0. */ |
340 | | }; |
341 | | static_assert(0 <= JSJitInfo::maxSlotIndex, "We won't fit"); |
342 | | static_assert(0 < 1, "There is no slot for us"); |
343 | | |
344 | | static bool |
345 | | _addProperty(JSContext* cx, JS::Handle<JSObject*> obj, JS::Handle<jsid> id, JS::Handle<JS::Value> val) |
346 | 0 | { |
347 | 0 | mozilla::dom::SpeechRecognitionError* self = UnwrapPossiblyNotInitializedDOMObject<mozilla::dom::SpeechRecognitionError>(obj); |
348 | 0 | // We don't want to preserve if we don't have a wrapper, and we |
349 | 0 | // obviously can't preserve if we're not initialized. |
350 | 0 | if (self && self->GetWrapperPreserveColor()) { |
351 | 0 | PreserveWrapper(self); |
352 | 0 | } |
353 | 0 | return true; |
354 | 0 | } |
355 | | |
356 | | static void |
357 | | _finalize(js::FreeOp* fop, JSObject* obj) |
358 | 0 | { |
359 | 0 | mozilla::dom::SpeechRecognitionError* self = UnwrapPossiblyNotInitializedDOMObject<mozilla::dom::SpeechRecognitionError>(obj); |
360 | 0 | if (self) { |
361 | 0 | ClearWrapper(self, self, obj); |
362 | 0 | AddForDeferredFinalization<mozilla::dom::SpeechRecognitionError>(self); |
363 | 0 | } |
364 | 0 | } |
365 | | |
366 | | static size_t |
367 | | _objectMoved(JSObject* obj, JSObject* old) |
368 | 0 | { |
369 | 0 | mozilla::dom::SpeechRecognitionError* self = UnwrapPossiblyNotInitializedDOMObject<mozilla::dom::SpeechRecognitionError>(obj); |
370 | 0 | if (self) { |
371 | 0 | UpdateWrapper(self, self, obj, old); |
372 | 0 | } |
373 | 0 |
|
374 | 0 | return 0; |
375 | 0 | } |
376 | | |
377 | | // We deliberately use brace-elision to make Visual Studio produce better initalization code. |
378 | | #if defined(__clang__) |
379 | | #pragma clang diagnostic push |
380 | | #pragma clang diagnostic ignored "-Wmissing-braces" |
381 | | #endif |
382 | | static const JSPropertySpec sAttributes_specs[] = { |
383 | | { "error", JSPROP_ENUMERATE, GenericGetter<NormalThisPolicy, ThrowExceptions>, &error_getterinfo, nullptr, nullptr }, |
384 | | { "message", JSPROP_ENUMERATE, GenericGetter<NormalThisPolicy, ThrowExceptions>, &message_getterinfo, nullptr, nullptr }, |
385 | | { nullptr, 0, nullptr, nullptr, nullptr, nullptr } |
386 | | }; |
387 | | #if defined(__clang__) |
388 | | #pragma clang diagnostic pop |
389 | | #endif |
390 | | |
391 | | |
392 | | static const Prefable<const JSPropertySpec> sAttributes[] = { |
393 | | { nullptr, &sAttributes_specs[0] }, |
394 | | { nullptr, nullptr } |
395 | | }; |
396 | | |
397 | | static_assert(1 <= 1ull << NUM_BITS_PROPERTY_INFO_PREF_INDEX, |
398 | | "We have a prefable index that is >= (1 << NUM_BITS_PROPERTY_INFO_PREF_INDEX)"); |
399 | | static_assert(2 <= 1ull << NUM_BITS_PROPERTY_INFO_SPEC_INDEX, |
400 | | "We have a spec index that is >= (1 << NUM_BITS_PROPERTY_INFO_SPEC_INDEX)"); |
401 | | |
402 | | // We deliberately use brace-elision to make Visual Studio produce better initalization code. |
403 | | #if defined(__clang__) |
404 | | #pragma clang diagnostic push |
405 | | #pragma clang diagnostic ignored "-Wmissing-braces" |
406 | | #endif |
407 | | static const JSPropertySpec sUnforgeableAttributes_specs[] = { |
408 | | { "isTrusted", JSPROP_ENUMERATE | JSPROP_PERMANENT, GenericGetter<NormalThisPolicy, ThrowExceptions>, &isTrusted_getterinfo, nullptr, nullptr }, |
409 | | { nullptr, 0, nullptr, nullptr, nullptr, nullptr } |
410 | | }; |
411 | | #if defined(__clang__) |
412 | | #pragma clang diagnostic pop |
413 | | #endif |
414 | | |
415 | | |
416 | | static const Prefable<const JSPropertySpec> sUnforgeableAttributes[] = { |
417 | | { nullptr, &sUnforgeableAttributes_specs[0] }, |
418 | | { nullptr, nullptr } |
419 | | }; |
420 | | |
421 | | static_assert(1 <= 1ull << NUM_BITS_PROPERTY_INFO_PREF_INDEX, |
422 | | "We have a prefable index that is >= (1 << NUM_BITS_PROPERTY_INFO_PREF_INDEX)"); |
423 | | static_assert(1 <= 1ull << NUM_BITS_PROPERTY_INFO_SPEC_INDEX, |
424 | | "We have a spec index that is >= (1 << NUM_BITS_PROPERTY_INFO_SPEC_INDEX)"); |
425 | | |
426 | | |
427 | | static uint16_t sNativeProperties_sortedPropertyIndices[3]; |
428 | | static PropertyInfo sNativeProperties_propertyInfos[3]; |
429 | | |
430 | | static const NativePropertiesN<2> sNativeProperties = { |
431 | | false, 0, |
432 | | false, 0, |
433 | | false, 0, |
434 | | true, 0 /* sAttributes */, |
435 | | false, 0, |
436 | | true, 1 /* sUnforgeableAttributes */, |
437 | | false, 0, |
438 | | -1, |
439 | | 3, |
440 | | sNativeProperties_sortedPropertyIndices, |
441 | | { |
442 | | { sAttributes, &sNativeProperties_propertyInfos[0] }, |
443 | | { sUnforgeableAttributes, &sNativeProperties_propertyInfos[2] } |
444 | | } |
445 | | }; |
446 | | static_assert(3 < 1ull << CHAR_BIT * sizeof(sNativeProperties.propertyInfoCount), |
447 | | "We have a property info count that is oversized"); |
448 | | |
449 | | static bool |
450 | | _constructor(JSContext* cx, unsigned argc, JS::Value* vp) |
451 | 0 | { |
452 | 0 | AUTO_PROFILER_LABEL_FAST("SpeechRecognitionError constructor", DOM, cx); |
453 | 0 |
|
454 | 0 | JS::CallArgs args = JS::CallArgsFromVp(argc, vp); |
455 | 0 | JS::Rooted<JSObject*> obj(cx, &args.callee()); |
456 | 0 | if (!args.isConstructing()) { |
457 | 0 | // XXXbz wish I could get the name from the callee instead of |
458 | 0 | // Adding more relocations |
459 | 0 | return ThrowConstructorWithoutNew(cx, "SpeechRecognitionError"); |
460 | 0 | } |
461 | 0 | |
462 | 0 | JS::Rooted<JSObject*> desiredProto(cx); |
463 | 0 | if (!GetDesiredProto(cx, args, &desiredProto)) { |
464 | 0 | return false; |
465 | 0 | } |
466 | 0 | |
467 | 0 | if (MOZ_UNLIKELY(args.length() < 1)) { |
468 | 0 | return ThrowErrorMessage(cx, MSG_MISSING_ARGUMENTS, "SpeechRecognitionError"); |
469 | 0 | } |
470 | 0 | GlobalObject global(cx, obj); |
471 | 0 | if (global.Failed()) { |
472 | 0 | return false; |
473 | 0 | } |
474 | 0 | |
475 | 0 | bool objIsXray = xpc::WrapperFactory::IsXrayWrapper(obj); |
476 | 0 | binding_detail::FakeString arg0; |
477 | 0 | if (!ConvertJSValueToString(cx, args[0], eStringify, eStringify, arg0)) { |
478 | 0 | return false; |
479 | 0 | } |
480 | 0 | binding_detail::FastSpeechRecognitionErrorInit arg1; |
481 | 0 | if (!arg1.Init(cx, (args.hasDefined(1)) ? args[1] : JS::NullHandleValue, "Argument 2 of SpeechRecognitionError.constructor", false)) { |
482 | 0 | return false; |
483 | 0 | } |
484 | 0 | Maybe<JSAutoRealm> ar; |
485 | 0 | if (objIsXray) { |
486 | 0 | obj = js::CheckedUnwrap(obj); |
487 | 0 | if (!obj) { |
488 | 0 | return false; |
489 | 0 | } |
490 | 0 | ar.emplace(cx, obj); |
491 | 0 | if (!JS_WrapObject(cx, &desiredProto)) { |
492 | 0 | return false; |
493 | 0 | } |
494 | 0 | } |
495 | 0 | FastErrorResult rv; |
496 | 0 | auto result(StrongOrRawPtr<mozilla::dom::SpeechRecognitionError>(mozilla::dom::SpeechRecognitionError::Constructor(global, NonNullHelper(Constify(arg0)), Constify(arg1), rv))); |
497 | 0 | if (MOZ_UNLIKELY(rv.MaybeSetPendingException(cx))) { |
498 | 0 | return false; |
499 | 0 | } |
500 | 0 | MOZ_ASSERT(!JS_IsExceptionPending(cx)); |
501 | 0 | static_assert(!IsPointer<decltype(result)>::value, |
502 | 0 | "NewObject implies that we need to keep the object alive with a strong reference."); |
503 | 0 | if (!GetOrCreateDOMReflector(cx, result, args.rval(), desiredProto)) { |
504 | 0 | MOZ_ASSERT(JS_IsExceptionPending(cx)); |
505 | 0 | return false; |
506 | 0 | } |
507 | 0 | return true; |
508 | 0 | } |
509 | | |
510 | | static const js::ClassOps sInterfaceObjectClassOps = { |
511 | | nullptr, /* addProperty */ |
512 | | nullptr, /* delProperty */ |
513 | | nullptr, /* enumerate */ |
514 | | nullptr, /* newEnumerate */ |
515 | | nullptr, /* resolve */ |
516 | | nullptr, /* mayResolve */ |
517 | | nullptr, /* finalize */ |
518 | | _constructor, /* call */ |
519 | | nullptr, /* hasInstance */ |
520 | | _constructor, /* construct */ |
521 | | nullptr, /* trace */ |
522 | | }; |
523 | | |
524 | | static const DOMIfaceAndProtoJSClass sInterfaceObjectClass = { |
525 | | { |
526 | | "Function", |
527 | | JSCLASS_IS_DOMIFACEANDPROTOJSCLASS | JSCLASS_HAS_RESERVED_SLOTS(DOM_INTERFACE_SLOTS_BASE), |
528 | | &sInterfaceObjectClassOps, |
529 | | JS_NULL_CLASS_SPEC, |
530 | | JS_NULL_CLASS_EXT, |
531 | | &sInterfaceObjectClassObjectOps |
532 | | }, |
533 | | eInterface, |
534 | | true, |
535 | | prototypes::id::SpeechRecognitionError, |
536 | | PrototypeTraits<prototypes::id::SpeechRecognitionError>::Depth, |
537 | | sNativePropertyHooks, |
538 | | "function SpeechRecognitionError() {\n [native code]\n}", |
539 | | Event_Binding::GetConstructorObject |
540 | | }; |
541 | | |
542 | | static const DOMIfaceAndProtoJSClass sPrototypeClass = { |
543 | | { |
544 | | "SpeechRecognitionErrorPrototype", |
545 | | JSCLASS_IS_DOMIFACEANDPROTOJSCLASS | JSCLASS_HAS_RESERVED_SLOTS(DOM_INTERFACE_PROTO_SLOTS_BASE + 1 /* slot for the JSObject holding the unforgeable properties */), |
546 | | JS_NULL_CLASS_OPS, |
547 | | JS_NULL_CLASS_SPEC, |
548 | | JS_NULL_CLASS_EXT, |
549 | | JS_NULL_OBJECT_OPS |
550 | | }, |
551 | | eInterfacePrototype, |
552 | | false, |
553 | | prototypes::id::SpeechRecognitionError, |
554 | | PrototypeTraits<prototypes::id::SpeechRecognitionError>::Depth, |
555 | | sNativePropertyHooks, |
556 | | "[object SpeechRecognitionErrorPrototype]", |
557 | | Event_Binding::GetProtoObject |
558 | | }; |
559 | | |
560 | | bool |
561 | | ConstructorEnabled(JSContext* aCx, JS::Handle<JSObject*> aObj) |
562 | 0 | { |
563 | 0 | static bool sPrefValue; |
564 | 0 | static bool sPrefCacheSetUp = false; |
565 | 0 | if (!sPrefCacheSetUp) { |
566 | 0 | sPrefCacheSetUp = true; |
567 | 0 | Preferences::AddBoolVarCache(&sPrefValue, "media.webspeech.recognition.enable"); |
568 | 0 | } |
569 | 0 |
|
570 | 0 | return sPrefValue && |
571 | 0 | SpeechRecognition::IsAuthorized(aCx, aObj); |
572 | 0 | } |
573 | | |
574 | | static const js::ClassOps sClassOps = { |
575 | | _addProperty, /* addProperty */ |
576 | | nullptr, /* delProperty */ |
577 | | nullptr, /* enumerate */ |
578 | | nullptr, /* newEnumerate */ |
579 | | nullptr, /* resolve */ |
580 | | nullptr, /* mayResolve */ |
581 | | _finalize, /* finalize */ |
582 | | nullptr, /* call */ |
583 | | nullptr, /* hasInstance */ |
584 | | nullptr, /* construct */ |
585 | | nullptr, /* trace */ |
586 | | }; |
587 | | |
588 | | static const js::ClassExtension sClassExtension = { |
589 | | nullptr, /* weakmapKeyDelegateOp */ |
590 | | _objectMoved /* objectMovedOp */ |
591 | | }; |
592 | | |
593 | | static const DOMJSClass sClass = { |
594 | | { "SpeechRecognitionError", |
595 | | JSCLASS_IS_DOMJSCLASS | JSCLASS_FOREGROUND_FINALIZE | JSCLASS_HAS_RESERVED_SLOTS(1) | JSCLASS_SKIP_NURSERY_FINALIZE, |
596 | | &sClassOps, |
597 | | JS_NULL_CLASS_SPEC, |
598 | | &sClassExtension, |
599 | | JS_NULL_OBJECT_OPS |
600 | | }, |
601 | | { prototypes::id::Event, prototypes::id::SpeechRecognitionError, prototypes::id::_ID_Count, prototypes::id::_ID_Count, prototypes::id::_ID_Count, prototypes::id::_ID_Count, prototypes::id::_ID_Count, prototypes::id::_ID_Count }, |
602 | | IsBaseOf<nsISupports, mozilla::dom::SpeechRecognitionError >::value, |
603 | | sNativePropertyHooks, |
604 | | FindAssociatedGlobalForNative<mozilla::dom::SpeechRecognitionError>::Get, |
605 | | GetProtoObjectHandle, |
606 | | GetCCParticipant<mozilla::dom::SpeechRecognitionError>::Get() |
607 | | }; |
608 | | static_assert(1 == DOM_INSTANCE_RESERVED_SLOTS, |
609 | | "Must have the right minimal number of reserved slots."); |
610 | | static_assert(1 >= 1, |
611 | | "Must have enough reserved slots."); |
612 | | |
613 | | const JSClass* |
614 | | GetJSClass() |
615 | 0 | { |
616 | 0 | return sClass.ToJSClass(); |
617 | 0 | } |
618 | | |
619 | | bool |
620 | | Wrap(JSContext* aCx, mozilla::dom::SpeechRecognitionError* aObject, nsWrapperCache* aCache, JS::Handle<JSObject*> aGivenProto, JS::MutableHandle<JSObject*> aReflector) |
621 | 0 | { |
622 | 0 | static_assert(!IsBaseOf<NonRefcountedDOMObject, mozilla::dom::SpeechRecognitionError>::value, |
623 | 0 | "Shouldn't have wrappercached things that are not refcounted."); |
624 | 0 | MOZ_ASSERT(static_cast<mozilla::dom::SpeechRecognitionError*>(aObject) == |
625 | 0 | reinterpret_cast<mozilla::dom::SpeechRecognitionError*>(aObject), |
626 | 0 | "Multiple inheritance for mozilla::dom::SpeechRecognitionError is broken."); |
627 | 0 | MOZ_ASSERT(static_cast<mozilla::dom::Event*>(aObject) == |
628 | 0 | reinterpret_cast<mozilla::dom::Event*>(aObject), |
629 | 0 | "Multiple inheritance for mozilla::dom::Event is broken."); |
630 | 0 | MOZ_ASSERT(ToSupportsIsCorrect(aObject)); |
631 | 0 | MOZ_ASSERT_IF(aGivenProto, js::IsObjectInContextCompartment(aGivenProto, aCx)); |
632 | 0 | MOZ_ASSERT(!aCache->GetWrapper(), |
633 | 0 | "You should probably not be using Wrap() directly; use " |
634 | 0 | "GetOrCreateDOMReflector instead"); |
635 | 0 |
|
636 | 0 | MOZ_ASSERT(ToSupportsIsOnPrimaryInheritanceChain(aObject, aCache), |
637 | 0 | "nsISupports must be on our primary inheritance chain"); |
638 | 0 |
|
639 | 0 | JS::Rooted<JSObject*> global(aCx, FindAssociatedGlobal(aCx, aObject->GetParentObject())); |
640 | 0 | if (!global) { |
641 | 0 | return false; |
642 | 0 | } |
643 | 0 | MOZ_ASSERT(JS_IsGlobalObject(global)); |
644 | 0 | MOZ_ASSERT(JS::ObjectIsNotGray(global)); |
645 | 0 |
|
646 | 0 | // That might have ended up wrapping us already, due to the wonders |
647 | 0 | // of XBL. Check for that, and bail out as needed. |
648 | 0 | aReflector.set(aCache->GetWrapper()); |
649 | 0 | if (aReflector) { |
650 | | #ifdef DEBUG |
651 | | AssertReflectorHasGivenProto(aCx, aReflector, aGivenProto); |
652 | | #endif // DEBUG |
653 | | return true; |
654 | 0 | } |
655 | 0 |
|
656 | 0 | JSAutoRealm ar(aCx, global); |
657 | 0 | JS::Handle<JSObject*> canonicalProto = GetProtoObjectHandle(aCx); |
658 | 0 | if (!canonicalProto) { |
659 | 0 | return false; |
660 | 0 | } |
661 | 0 | JS::Rooted<JSObject*> proto(aCx); |
662 | 0 | if (aGivenProto) { |
663 | 0 | proto = aGivenProto; |
664 | 0 | // Unfortunately, while aGivenProto was in the compartment of aCx |
665 | 0 | // coming in, we changed compartments to that of "parent" so may need |
666 | 0 | // to wrap the proto here. |
667 | 0 | if (js::GetContextCompartment(aCx) != js::GetObjectCompartment(proto)) { |
668 | 0 | if (!JS_WrapObject(aCx, &proto)) { |
669 | 0 | return false; |
670 | 0 | } |
671 | 0 | } |
672 | 0 | } else { |
673 | 0 | proto = canonicalProto; |
674 | 0 | } |
675 | 0 |
|
676 | 0 | BindingJSObjectCreator<mozilla::dom::SpeechRecognitionError> creator(aCx); |
677 | 0 | creator.CreateObject(aCx, sClass.ToJSClass(), proto, aObject, aReflector); |
678 | 0 | if (!aReflector) { |
679 | 0 | return false; |
680 | 0 | } |
681 | 0 | |
682 | 0 | aCache->SetWrapper(aReflector); |
683 | 0 |
|
684 | 0 | // Important: do unforgeable property setup after we have handed |
685 | 0 | // over ownership of the C++ object to obj as needed, so that if |
686 | 0 | // we fail and it ends up GCed it won't have problems in the |
687 | 0 | // finalizer trying to drop its ownership of the C++ object. |
688 | 0 | JS::Rooted<JSObject*> unforgeableHolder(aCx, |
689 | 0 | &js::GetReservedSlot(canonicalProto, DOM_INTERFACE_PROTO_SLOTS_BASE).toObject()); |
690 | 0 | if (!JS_InitializePropertiesFromCompatibleNativeObject(aCx, aReflector, unforgeableHolder)) { |
691 | 0 | aCache->ReleaseWrapper(aObject); |
692 | 0 | aCache->ClearWrapper(); |
693 | 0 | return false; |
694 | 0 | } |
695 | 0 | creator.InitializationSucceeded(); |
696 | 0 |
|
697 | 0 | MOZ_ASSERT(aCache->GetWrapperPreserveColor() && |
698 | 0 | aCache->GetWrapperPreserveColor() == aReflector); |
699 | 0 | // If proto != canonicalProto, we have to preserve our wrapper; |
700 | 0 | // otherwise we won't be able to properly recreate it later, since |
701 | 0 | // we won't know what proto to use. Note that we don't check |
702 | 0 | // aGivenProto here, since it's entirely possible (and even |
703 | 0 | // somewhat common) to have a non-null aGivenProto which is the |
704 | 0 | // same as canonicalProto. |
705 | 0 | if (proto != canonicalProto) { |
706 | 0 | PreserveWrapper(aObject); |
707 | 0 | } |
708 | 0 |
|
709 | 0 | return true; |
710 | 0 | } |
711 | | |
712 | | const NativePropertyHooks sNativePropertyHooks[] = { { |
713 | | nullptr, |
714 | | nullptr, |
715 | | nullptr, |
716 | | { sNativeProperties.Upcast(), nullptr }, |
717 | | prototypes::id::SpeechRecognitionError, |
718 | | constructors::id::SpeechRecognitionError, |
719 | | Event_Binding::sNativePropertyHooks, |
720 | | &DefaultXrayExpandoObjectClass |
721 | | } }; |
722 | | |
723 | | void |
724 | | CreateInterfaceObjects(JSContext* aCx, JS::Handle<JSObject*> aGlobal, ProtoAndIfaceCache& aProtoAndIfaceCache, bool aDefineOnGlobal) |
725 | 0 | { |
726 | 0 | JS::Handle<JSObject*> parentProto(Event_Binding::GetProtoObjectHandle(aCx)); |
727 | 0 | if (!parentProto) { |
728 | 0 | return; |
729 | 0 | } |
730 | 0 | |
731 | 0 | JS::Handle<JSObject*> constructorProto(Event_Binding::GetConstructorObjectHandle(aCx)); |
732 | 0 | if (!constructorProto) { |
733 | 0 | return; |
734 | 0 | } |
735 | 0 | |
736 | 0 | static bool sIdsInited = false; |
737 | 0 | if (!sIdsInited && NS_IsMainThread()) { |
738 | 0 | if (!InitIds(aCx, sNativeProperties.Upcast())) { |
739 | 0 | return; |
740 | 0 | } |
741 | 0 | sIdsInited = true; |
742 | 0 | } |
743 | 0 |
|
744 | 0 | JS::Heap<JSObject*>* protoCache = &aProtoAndIfaceCache.EntrySlotOrCreate(prototypes::id::SpeechRecognitionError); |
745 | 0 | JS::Heap<JSObject*>* interfaceCache = &aProtoAndIfaceCache.EntrySlotOrCreate(constructors::id::SpeechRecognitionError); |
746 | 0 | dom::CreateInterfaceObjects(aCx, aGlobal, parentProto, |
747 | 0 | &sPrototypeClass.mBase, protoCache, |
748 | 0 | nullptr, |
749 | 0 | constructorProto, &sInterfaceObjectClass.mBase, 1, nullptr, |
750 | 0 | interfaceCache, |
751 | 0 | sNativeProperties.Upcast(), |
752 | 0 | nullptr, |
753 | 0 | "SpeechRecognitionError", aDefineOnGlobal, |
754 | 0 | nullptr, |
755 | 0 | false); |
756 | 0 |
|
757 | 0 | JS::Rooted<JSObject*> unforgeableHolder(aCx); |
758 | 0 | { |
759 | 0 | JS::Rooted<JSObject*> holderProto(aCx, *protoCache); |
760 | 0 | unforgeableHolder = JS_NewObjectWithoutMetadata(aCx, sClass.ToJSClass(), holderProto); |
761 | 0 | if (!unforgeableHolder) { |
762 | 0 | *protoCache = nullptr; |
763 | 0 | if (interfaceCache) { |
764 | 0 | *interfaceCache = nullptr; |
765 | 0 | } |
766 | 0 | return; |
767 | 0 | } |
768 | 0 | } |
769 | 0 |
|
770 | 0 | if (!DefineUnforgeableAttributes(aCx, unforgeableHolder, sUnforgeableAttributes)) { |
771 | 0 | *protoCache = nullptr; |
772 | 0 | if (interfaceCache) { |
773 | 0 | *interfaceCache = nullptr; |
774 | 0 | } |
775 | 0 | return; |
776 | 0 | } |
777 | 0 |
|
778 | 0 | if (*protoCache) { |
779 | 0 | js::SetReservedSlot(*protoCache, DOM_INTERFACE_PROTO_SLOTS_BASE, |
780 | 0 | JS::ObjectValue(*unforgeableHolder)); |
781 | 0 | } |
782 | 0 | } |
783 | | |
784 | | JSObject* |
785 | | GetConstructorObject(JSContext* aCx) |
786 | 0 | { |
787 | 0 | return GetConstructorObjectHandle(aCx); |
788 | 0 | } |
789 | | |
790 | | } // namespace SpeechRecognitionError_Binding |
791 | | |
792 | | |
793 | | |
794 | | } // namespace dom |
795 | | } // namespace mozilla |