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/. */
6
7 #include "mozilla/dom/CallbackInterface.h"
8 #include "jsapi.h"
9 #include "js/CharacterEncoding.h"
10 #include "mozilla/dom/BindingUtils.h"
11 #include "nsPrintfCString.h"
12
13 namespace mozilla {
14 namespace dom {
15
GetCallableProperty(BindingCallContext & cx,JS::Handle<jsid> aPropId,JS::MutableHandle<JS::Value> aCallable)16 bool CallbackInterface::GetCallableProperty(
17 BindingCallContext& cx, JS::Handle<jsid> aPropId,
18 JS::MutableHandle<JS::Value> aCallable) {
19 JS::Rooted<JSObject*> obj(cx, CallbackKnownNotGray());
20 if (!JS_GetPropertyById(cx, obj, aPropId, aCallable)) {
21 return false;
22 }
23 if (!aCallable.isObject() || !JS::IsCallable(&aCallable.toObject())) {
24 JS::RootedString propId(cx, JSID_TO_STRING(aPropId));
25 JS::UniqueChars propName = JS_EncodeStringToUTF8(cx, propId);
26 nsPrintfCString description("Property '%s'", propName.get());
27 cx.ThrowErrorMessage<MSG_NOT_CALLABLE>(description.get());
28 return false;
29 }
30
31 return true;
32 }
33
34 } // namespace dom
35 } // namespace mozilla
36