~ubuntu-branches/ubuntu/raring/gjs/raring

« back to all changes in this revision

Viewing changes to gi/object.c

  • Committer: Package Import Robot
  • Author(s): Robert Ancell
  • Date: 2012-08-22 16:55:24 UTC
  • mfrom: (1.10.3)
  • Revision ID: package-import@ubuntu.com-20120822165524-v99vw4xhpokm5wc0
Tags: 1.33.9-0ubuntu1
New upstream release

Show diffs side-by-side

added added

removed removed

Lines of Context:
39
39
 
40
40
#include <gjs/gjs-module.h>
41
41
#include <gjs/compat.h>
 
42
#include <gjs/type-module.h>
42
43
 
43
44
#include <util/log.h>
44
45
 
54
55
 
55
56
    /* a list of all signal connections, used when tracing */
56
57
    GList *signals;
 
58
 
 
59
    /* the GObjectClass wrapped by this JS Object (only used for
 
60
       prototypes) */
 
61
    GTypeClass *klass;
57
62
} ObjectInstance;
58
63
 
59
64
typedef struct {
1044
1049
    ObjectInstance *priv;
1045
1050
    GList *iter;
1046
1051
 
1047
 
    /* DO NOT use priv_from_js here: that uses JS_BeginRequest,
1048
 
       but this is called from the GC thread, and deadlocks
1049
 
       We know we're of the right JSClass anyway.
1050
 
    */
1051
 
    priv = JS_GetPrivate(tracer->context, obj);
 
1052
    priv = priv_from_js(tracer->context, obj);
1052
1053
 
1053
1054
    for (iter = priv->signals; iter; iter = iter->next) {
1054
1055
        ConnectData *cd = iter->data;
1118
1119
        priv->info = NULL;
1119
1120
    }
1120
1121
 
 
1122
    if (priv->klass) {
 
1123
        g_type_class_unref (priv->klass);
 
1124
        priv->klass = NULL;
 
1125
    }
 
1126
 
1121
1127
    GJS_DEC_COUNTER(object);
1122
1128
    g_slice_free(ObjectInstance, priv);
1123
1129
}
1162
1168
    ConnectData *connect_data;
1163
1169
    JSBool ret = JS_FALSE;
1164
1170
 
 
1171
    if (!do_base_typecheck(context, obj, JS_TRUE))
 
1172
        return JS_FALSE;
 
1173
 
1165
1174
    priv = priv_from_js(context, obj);
1166
1175
    gjs_debug_gsignal("connect obj %p priv %p argc %d", obj, priv, argc);
1167
1176
    if (priv == NULL) {
1261
1270
    ObjectInstance *priv;
1262
1271
    gulong id;
1263
1272
 
 
1273
    if (!do_base_typecheck(context, obj, JS_TRUE))
 
1274
        return JS_FALSE;
 
1275
 
1264
1276
    priv = priv_from_js(context, obj);
1265
1277
    gjs_debug_gsignal("disconnect obj %p priv %p argc %d", obj, priv, argc);
1266
1278
 
1311
1323
    jsval retval;
1312
1324
    JSBool ret = JS_FALSE;
1313
1325
 
 
1326
    if (!do_base_typecheck(context, obj, JS_TRUE))
 
1327
        return JS_FALSE;
 
1328
 
1314
1329
    priv = priv_from_js(context, obj);
1315
1330
    gjs_debug_gsignal("emit obj %p priv %p argc %d", obj, priv, argc);
1316
1331
 
1431
1446
    const char *name;
1432
1447
    jsval retval;
1433
1448
 
 
1449
    if (!do_base_typecheck(context, obj, JS_TRUE))
 
1450
        return JS_FALSE;
 
1451
 
1434
1452
    priv = priv_from_js(context, obj);
1435
1453
 
1436
1454
    if (priv == NULL) {
1717
1735
 
1718
1736
    g_assert(gjs_object_has_property(context, in_object, constructor_name));
1719
1737
 
 
1738
    GJS_INC_COUNTER(object);
1720
1739
    priv = g_slice_new0(ObjectInstance);
1721
1740
    priv->info = info;
1722
1741
    if (info)
1723
1742
        g_base_info_ref( (GIBaseInfo*) priv->info);
1724
1743
    priv->gtype = gtype;
 
1744
    priv->klass = g_type_class_ref (gtype);
1725
1745
    JS_SetPrivate(context, prototype, priv);
1726
1746
 
1727
1747
    gjs_debug(GJS_DEBUG_GOBJECT, "Defined class %s prototype %p class %p in object %p",
1884
1904
        return NULL;
1885
1905
 
1886
1906
    priv = priv_from_js(context, obj);
1887
 
 
1888
 
    if (priv == NULL) {
1889
 
        gjs_throw(context,
1890
 
                  "Object instance or prototype has not been properly initialized yet. "
1891
 
                  "Did you forget to chain-up from _init()?");
1892
 
        return NULL;
1893
 
    }
1894
 
 
1895
 
    if (priv->gobj == NULL) {
1896
 
        gjs_throw(context,
1897
 
                  "Object is %s.%s.prototype, not an object instance - cannot convert to GObject*",
1898
 
                  priv->info ? g_base_info_get_namespace( (GIBaseInfo*) priv->info) : "",
1899
 
                  priv->info ? g_base_info_get_name( (GIBaseInfo*) priv->info) : g_type_name(priv->gtype));
1900
 
        return NULL;
1901
 
    }
1902
 
 
1903
1907
    return priv->gobj;
1904
1908
}
1905
1909
 
 
1910
JSBool
 
1911
gjs_typecheck_object(JSContext     *context,
 
1912
                     JSObject      *object,
 
1913
                     GType          expected_type,
 
1914
                     JSBool         throw)
 
1915
{
 
1916
    ObjectInstance *priv;
 
1917
    JSBool result;
 
1918
 
 
1919
    if (!do_base_typecheck(context, object, throw))
 
1920
        return JS_FALSE;
 
1921
 
 
1922
    priv = priv_from_js(context, object);
 
1923
 
 
1924
    if (priv == NULL) {
 
1925
        if (throw) {
 
1926
            gjs_throw(context,
 
1927
                      "Object instance or prototype has not been properly initialized yet. "
 
1928
                      "Did you forget to chain-up from _init()?");
 
1929
        }
 
1930
 
 
1931
        return JS_FALSE;
 
1932
    }
 
1933
 
 
1934
    if (priv->gobj == NULL) {
 
1935
        if (throw) {
 
1936
            gjs_throw(context,
 
1937
                      "Object is %s.%s.prototype, not an object instance - cannot convert to GObject*",
 
1938
                      priv->info ? g_base_info_get_namespace( (GIBaseInfo*) priv->info) : "",
 
1939
                      priv->info ? g_base_info_get_name( (GIBaseInfo*) priv->info) : g_type_name(priv->gtype));
 
1940
        }
 
1941
 
 
1942
        return JS_FALSE;
 
1943
    }
 
1944
 
 
1945
    g_assert(priv->gtype == G_OBJECT_TYPE(priv->gobj));
 
1946
 
 
1947
    if (expected_type != G_TYPE_NONE)
 
1948
        result = g_type_is_a (priv->gtype, expected_type);
 
1949
    else
 
1950
        result = JS_TRUE;
 
1951
 
 
1952
    if (!result && throw) {
 
1953
        if (priv->info) {
 
1954
            gjs_throw_custom(context, "TypeError",
 
1955
                             "Object is of type %s.%s - cannot convert to %s",
 
1956
                             g_base_info_get_namespace((GIBaseInfo*) priv->info),
 
1957
                             g_base_info_get_name((GIBaseInfo*) priv->info),
 
1958
                             g_type_name(expected_type));
 
1959
        } else {
 
1960
            gjs_throw_custom(context, "TypeError",
 
1961
                             "Object is of type %s - cannot convert to %s",
 
1962
                             g_type_name(priv->gtype),
 
1963
                             g_type_name(expected_type));
 
1964
        }
 
1965
    }
 
1966
 
 
1967
    return result;
 
1968
}
 
1969
 
 
1970
 
1906
1971
static void
1907
1972
find_vfunc_info (JSContext *context,
1908
1973
                 GType implementor_gtype,
2001
2066
                        "function", &function))
2002
2067
        return JS_FALSE;
2003
2068
 
 
2069
    if (!do_base_typecheck(cx, object, JS_TRUE))
 
2070
        return JS_FALSE;
 
2071
 
2004
2072
    priv = priv_from_js(cx, object);
2005
2073
    gtype = priv->gtype;
2006
2074
    info = priv->info;
2193
2261
    JSObject *parent, *constructor;
2194
2262
    GType instance_type, parent_type;
2195
2263
    GTypeQuery query;
 
2264
    GTypeModule *type_module;
2196
2265
    ObjectInstance *parent_priv;
2197
2266
    GTypeInfo type_info = {
2198
2267
        0, /* class_size */
2220
2289
    if (!parent)
2221
2290
        return JS_FALSE;
2222
2291
 
 
2292
    if (!do_base_typecheck(cx, parent, JS_TRUE))
 
2293
        return JS_FALSE;
 
2294
 
2223
2295
    parent_priv = priv_from_js(cx, parent);
2224
2296
 
2225
2297
    if (!parent_priv)
2231
2303
    type_info.class_size = query.class_size;
2232
2304
    type_info.instance_size = query.instance_size;
2233
2305
 
2234
 
    instance_type = g_type_register_static(parent_type,
2235
 
                                           name,
2236
 
                                           &type_info,
2237
 
                                           0);
 
2306
    type_module = G_TYPE_MODULE (gjs_type_module_get());
 
2307
    instance_type = g_type_module_register_type(type_module,
 
2308
                                                parent_type,
 
2309
                                                name,
 
2310
                                                &type_info,
 
2311
                                                0);
2238
2312
 
2239
2313
    g_free(name);
2240
2314
 
2268
2342
                        "gtype", &iface_jsobj))
2269
2343
        return JS_FALSE;
2270
2344
 
 
2345
    if (!do_base_typecheck(cx, object, JS_TRUE))
 
2346
        return JS_FALSE;
 
2347
 
2271
2348
    priv = priv_from_js(cx, object);
2272
2349
 
2273
2350
    g_type_add_interface_static(priv->gtype,
2287
2364
    JSObject *pspec_js;
2288
2365
    GParamSpec *pspec;
2289
2366
    ObjectInstance *priv;
2290
 
    GObjectClass *oclass;
2291
2367
 
2292
2368
    if (argc != 2)
2293
2369
        return JS_FALSE;
2299
2375
    obj = JSVAL_TO_OBJECT(argv[0]);
2300
2376
    pspec_js = JSVAL_TO_OBJECT(argv[1]);
2301
2377
 
 
2378
    if (!do_base_typecheck(cx, obj, JS_TRUE))
 
2379
        return JS_FALSE;
 
2380
    if (!gjs_typecheck_param(cx, pspec_js, G_TYPE_NONE, JS_TRUE))
 
2381
        return JS_FALSE;
 
2382
 
2302
2383
    priv = priv_from_js(cx, obj);
2303
2384
    pspec = gjs_g_param_from_param(cx, pspec_js);
2304
2385
 
2305
2386
    g_param_spec_set_qdata(pspec, gjs_is_custom_property_quark(), GINT_TO_POINTER(1));
2306
2387
 
2307
 
    oclass = g_type_class_ref(priv->gtype);
2308
 
    g_object_class_install_property(oclass, PROP_JS_HANDLED, pspec);
2309
 
    g_type_class_unref(oclass);
 
2388
    g_object_class_install_property(G_OBJECT_CLASS (priv->klass), PROP_JS_HANDLED, pspec);
2310
2389
 
2311
2390
    JS_SET_RVAL(cx, vp, JSVAL_VOID);
2312
2391
    return JS_TRUE;
2338
2417
    }
2339
2418
 
2340
2419
    obj = JSVAL_TO_OBJECT(argv[0]);
 
2420
    if (!do_base_typecheck(cx, obj, JS_TRUE)) {
 
2421
        ret = JS_FALSE;
 
2422
        goto out;
 
2423
    }
 
2424
 
2341
2425
    priv = priv_from_js(cx, obj);
2342
2426
 
2343
2427
    /* we only support standard accumulators for now */