Bug 1885602 - Part 5: Implement navigating to the SUMO help topic from the menu heade...
[gecko.git] / dom / bindings / CallbackInterface.cpp
blobb000ea22d1c1e9aea165c36dfcf485019cebdac7
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "mozilla/dom/CallbackInterface.h"
8 #include "jsapi.h"
9 #include "js/CallAndConstruct.h" // JS::IsCallable
10 #include "js/CharacterEncoding.h"
11 #include "js/PropertyAndElement.h" // JS_GetProperty, JS_GetPropertyById
12 #include "mozilla/dom/BindingUtils.h"
13 #include "nsPrintfCString.h"
15 namespace mozilla::dom {
17 bool CallbackInterface::GetCallableProperty(
18 BindingCallContext& cx, JS::Handle<jsid> aPropId,
19 JS::MutableHandle<JS::Value> aCallable) {
20 JS::Rooted<JSObject*> obj(cx, CallbackKnownNotGray());
21 if (!JS_GetPropertyById(cx, obj, aPropId, aCallable)) {
22 return false;
24 if (!aCallable.isObject() || !JS::IsCallable(&aCallable.toObject())) {
25 JS::Rooted<JSString*> propId(cx, aPropId.toString());
26 JS::UniqueChars propName = JS_EncodeStringToUTF8(cx, propId);
27 nsPrintfCString description("Property '%s'", propName.get());
28 cx.ThrowErrorMessage<MSG_NOT_CALLABLE>(description.get());
29 return false;
32 return true;
35 } // namespace mozilla::dom