1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
|
/* -*- mode: C++; c-basic-offset: 4; indent-tabs-mode: nil; -*- */
// SPDX-License-Identifier: MIT OR LGPL-2.0-or-later
// SPDX-FileCopyrightText: 2008 litl, LLC
#include <config.h>
#include <stddef.h> // for size_t
#include <girepository.h>
#include <glib.h>
#include <js/CallArgs.h>
#include <js/Class.h>
#include <js/ErrorReport.h> // for JSEXN_TYPEERR
#include <js/Object.h> // for GetClass
#include <js/PropertyAndElement.h>
#include <js/PropertyDescriptor.h> // for JSPROP_READONLY
#include <js/PropertySpec.h> // for JSPropertySpec, JS_PS_END, JS_STR...
#include <js/RootingAPI.h>
#include <js/TypeDecls.h>
#include <js/Utility.h> // for UniqueChars
#include <js/Value.h>
#include <jsapi.h> // for JS_NewObjectForConstructor, JS_NewObjectWithG...
#include "gi/cwrapper.h"
#include "gi/function.h"
#include "gi/param.h"
#include "gi/repo.h"
#include "gi/wrapperutils.h"
#include "cjs/atoms.h"
#include "cjs/context-private.h"
#include "cjs/jsapi-class.h"
#include "cjs/jsapi-util.h"
#include "cjs/macros.h"
#include "cjs/mem-private.h"
#include "util/log.h"
extern struct JSClass gjs_param_class;
// Reserved slots
static const size_t POINTER = 0;
struct Param : GjsAutoParam {
explicit Param(GParamSpec* param)
: GjsAutoParam(param, GjsAutoTakeOwnership()) {}
};
[[nodiscard]] static GParamSpec* param_value(JSContext* cx,
JS::HandleObject obj) {
if (!JS_InstanceOf(cx, obj, &gjs_param_class, nullptr))
return nullptr;
auto* priv = JS::GetMaybePtrFromReservedSlot<Param>(obj, POINTER);
return priv ? priv->get() : nullptr;
}
/*
* The *resolved out parameter, on success, should be false to indicate that id
* was not resolved; and true if id was resolved.
*/
GJS_JSAPI_RETURN_CONVENTION
static bool
param_resolve(JSContext *context,
JS::HandleObject obj,
JS::HandleId id,
bool *resolved)
{
if (!param_value(context, obj)) {
/* instance, not prototype */
*resolved = false;
return true;
}
JS::UniqueChars name;
if (!gjs_get_string_id(context, id, &name))
return false;
if (!name) {
*resolved = false;
return true; /* not resolved, but no error */
}
GjsAutoObjectInfo info = g_irepository_find_by_gtype(nullptr, G_TYPE_PARAM);
GjsAutoFunctionInfo method_info =
g_object_info_find_method(info, name.get());
if (!method_info) {
*resolved = false;
return true;
}
#if GJS_VERBOSE_ENABLE_GI_USAGE
_gjs_log_info_usage(method_info);
#endif
if (g_function_info_get_flags (method_info) & GI_FUNCTION_IS_METHOD) {
gjs_debug(GJS_DEBUG_GOBJECT,
"Defining method %s in prototype for GObject.ParamSpec",
method_info.name());
if (!gjs_define_function(context, obj, G_TYPE_PARAM, method_info))
return false;
*resolved = true; /* we defined the prop in obj */
}
return true;
}
GJS_JSAPI_RETURN_CONVENTION
static bool gjs_param_constructor(JSContext* cx, unsigned argc, JS::Value* vp) {
JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
if (!args.isConstructing()) {
gjs_throw_constructor_error(cx);
return false;
}
JS::RootedObject new_object(
cx, JS_NewObjectForConstructor(cx, &gjs_param_class, args));
if (!new_object)
return false;
GJS_INC_COUNTER(param);
args.rval().setObject(*new_object);
return true;
}
static void param_finalize(JS::GCContext*, JSObject* obj) {
Param* priv = JS::GetMaybePtrFromReservedSlot<Param>(obj, POINTER);
gjs_debug_lifecycle(GJS_DEBUG_GPARAM, "finalize, obj %p priv %p", obj,
priv);
if (!priv)
return; /* wrong class? */
GJS_DEC_COUNTER(param);
JS::SetReservedSlot(obj, POINTER, JS::UndefinedValue());
delete priv;
}
/* The bizarre thing about this vtable is that it applies to both
* instances of the object, and to the prototype that instances of the
* class have.
*/
static const struct JSClassOps gjs_param_class_ops = {
nullptr, // addProperty
nullptr, // deleteProperty
nullptr, // enumerate
nullptr, // newEnumerate
param_resolve,
nullptr, // mayResolve
param_finalize};
static JSPropertySpec proto_props[] = {
JS_STRING_SYM_PS(toStringTag, "GObject_ParamSpec", JSPROP_READONLY),
JS_PS_END};
static constexpr js::ClassSpec class_spec = {
nullptr, // createConstructor
nullptr, // createPrototype
nullptr, // constructorFunctions
nullptr, // constructorProperties
nullptr, // prototypeFunctions
proto_props, // prototypeProperties
nullptr // finishInit
};
struct JSClass gjs_param_class = {
"GObject_ParamSpec",
JSCLASS_HAS_RESERVED_SLOTS(1) | JSCLASS_BACKGROUND_FINALIZE,
&gjs_param_class_ops, &class_spec};
GJS_JSAPI_RETURN_CONVENTION
static JSObject*
gjs_lookup_param_prototype(JSContext *context)
{
const GjsAtoms& atoms = GjsContextPrivate::atoms(context);
JS::RootedObject in_object(
context, gjs_lookup_namespace_object_by_name(context, atoms.gobject()));
if (G_UNLIKELY (!in_object))
return nullptr;
JS::RootedValue value(context);
if (!JS_GetPropertyById(context, in_object, atoms.param_spec(), &value) ||
G_UNLIKELY(!value.isObject()))
return nullptr;
JS::RootedObject constructor(context, &value.toObject());
g_assert(constructor);
if (!JS_GetPropertyById(context, constructor, atoms.prototype(), &value) ||
G_UNLIKELY(!value.isObjectOrNull()))
return nullptr;
return value.toObjectOrNull();
}
bool
gjs_define_param_class(JSContext *context,
JS::HandleObject in_object)
{
JS::RootedObject prototype(context), constructor(context);
if (!gjs_init_class_dynamic(
context, in_object, nullptr, "GObject", "ParamSpec",
&gjs_param_class, gjs_param_constructor, 0,
proto_props, // props of prototype
nullptr, // funcs of prototype
nullptr, // props of constructor, MyConstructor.myprop
nullptr, // funcs of constructor
&prototype, &constructor))
return false;
if (!gjs_wrapper_define_gtype_prop(context, constructor, G_TYPE_PARAM))
return false;
GjsAutoObjectInfo info = g_irepository_find_by_gtype(nullptr, G_TYPE_PARAM);
if (!gjs_define_static_methods<InfoType::Object>(context, constructor,
G_TYPE_PARAM, info))
return false;
gjs_debug(GJS_DEBUG_GPARAM,
"Defined class ParamSpec prototype is %p class %p in object %p",
prototype.get(), &gjs_param_class, in_object.get());
return true;
}
JSObject*
gjs_param_from_g_param(JSContext *context,
GParamSpec *gparam)
{
JSObject *obj;
if (!gparam)
return nullptr;
gjs_debug(GJS_DEBUG_GPARAM,
"Wrapping %s '%s' on %s with JSObject",
g_type_name(G_TYPE_FROM_INSTANCE((GTypeInstance*) gparam)),
gparam->name,
g_type_name(gparam->owner_type));
JS::RootedObject proto(context, gjs_lookup_param_prototype(context));
obj = JS_NewObjectWithGivenProto(context, JS::GetClass(proto), proto);
GJS_INC_COUNTER(param);
auto* priv = new Param(gparam);
JS::SetReservedSlot(obj, POINTER, JS::PrivateValue(priv));
gjs_debug(GJS_DEBUG_GPARAM,
"JSObject created with param instance %p type %s", gparam,
g_type_name(G_TYPE_FROM_INSTANCE(gparam)));
return obj;
}
GParamSpec*
gjs_g_param_from_param(JSContext *context,
JS::HandleObject obj)
{
if (!obj)
return nullptr;
return param_value(context, obj);
}
bool
gjs_typecheck_param(JSContext *context,
JS::HandleObject object,
GType expected_type,
bool throw_error)
{
bool result;
if (!gjs_typecheck_instance(context, object, &gjs_param_class, throw_error))
return false;
GParamSpec* param = param_value(context, object);
if (!param) {
if (throw_error) {
gjs_throw_custom(context, JSEXN_TYPEERR, nullptr,
"Object is GObject.ParamSpec.prototype, not an "
"object instance - cannot convert to a GObject."
"ParamSpec instance");
}
return false;
}
if (expected_type != G_TYPE_NONE)
result = g_type_is_a(G_TYPE_FROM_INSTANCE(param), expected_type);
else
result = true;
if (!result && throw_error) {
gjs_throw_custom(context, JSEXN_TYPEERR, nullptr,
"Object is of type %s - cannot convert to %s",
g_type_name(G_TYPE_FROM_INSTANCE(param)),
g_type_name(expected_type));
}
return result;
}
|