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