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

« back to all changes in this revision

Viewing changes to gjs/jsapi-util.h

  • 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:
55
55
 */
56
56
#define GJS_MODULE_PROP_FLAGS (JSPROP_PERMANENT | JSPROP_ENUMERATE)
57
57
 
58
 
/* priv_from_js_with_typecheck checks that the object is in fact an
59
 
 * instance of the specified class before accessing its private data.
60
 
 * Keep in mind that the function can return JS_TRUE and still fill the
61
 
 * out parameter with NULL if the object is the prototype for a class
62
 
 * without the JSCLASS_CONSTRUCT_PROTOTYPE flag or if it the class simply
63
 
 * does not have any private data.
 
58
/*
 
59
 * Helper methods to access private data:
 
60
 *
 
61
 * do_base_typecheck: checks that object has the right JSClass, and possibly
 
62
 *                    throw a TypeError exception if the check fails
 
63
 * priv_from_js: accesses the object private field; as a debug measure,
 
64
 *               it also checks that the object is of a compatible
 
65
 *               JSClass, but it doesn't raise an exception (it
 
66
 *               wouldn't be of much use, if subsequent code crashes on
 
67
 *               NULL)
 
68
 * priv_from_js_with_typecheck: a convenience function to call
 
69
 *                              do_base_typecheck and priv_from_js
64
70
 */
65
 
#define GJS_DEFINE_PRIV_FROM_JS(type, class) \
66
 
    __attribute__((unused)) static JSBool           \
67
 
    priv_from_js_with_typecheck(JSContext *context,     \
68
 
                                JSObject  *object,  \
69
 
                                type      **out)    \
70
 
    {\
71
 
        if (!out) \
72
 
            return JS_FALSE; \
73
 
        if (!JS_InstanceOf(context, object, &class, NULL)) \
74
 
            return JS_FALSE; \
75
 
        *out = JS_GetInstancePrivate(context, object, &class, NULL); \
76
 
        return JS_TRUE; \
77
 
    }\
78
 
    static type*\
79
 
    priv_from_js(JSContext *context, \
80
 
                 JSObject  *object)  \
81
 
    {\
82
 
        return JS_GetInstancePrivate(context, object, &class, NULL); \
 
71
#define GJS_DEFINE_PRIV_FROM_JS(type, class)                            \
 
72
    __attribute__((unused)) static inline JSBool                        \
 
73
    do_base_typecheck(JSContext *context,                               \
 
74
                      JSObject  *object,                                \
 
75
                      JSBool     throw)                                 \
 
76
    {                                                                   \
 
77
        return gjs_typecheck_static_instance(context, object, &class, throw); \
 
78
    }                                                                   \
 
79
    static inline type*                                                 \
 
80
    priv_from_js(JSContext *context,                                    \
 
81
                 JSObject  *object)                                     \
 
82
    {                                                                   \
 
83
        return JS_GetInstancePrivate(context, object, &class, NULL);    \
 
84
    }                                                                   \
 
85
    __attribute__((unused)) static JSBool                               \
 
86
    priv_from_js_with_typecheck(JSContext *context,                     \
 
87
                                JSObject  *object,                      \
 
88
                                type      **out)                        \
 
89
    {                                                                   \
 
90
        if (!do_base_typecheck(context, object, JS_FALSE))              \
 
91
            return JS_FALSE;                                            \
 
92
        *out = priv_from_js(context, object);                           \
 
93
        return JS_TRUE;                                                 \
83
94
    }
84
95
 
85
 
 
86
 
#define GJS_DEFINE_DYNAMIC_PRIV_FROM_JS(type, class) \
87
 
    __attribute__((unused)) static JSBool\
88
 
    priv_from_js_with_typecheck(JSContext *context, \
89
 
                                JSObject  *object,  \
90
 
                                type      **out)    \
91
 
    {\
92
 
        type *result; \
93
 
        if (!out) \
94
 
            return JS_FALSE; \
95
 
        result = gjs_get_instance_private_dynamic_with_typecheck(context, object, &class, NULL); \
96
 
        if (result == NULL) \
97
 
            return JS_FALSE; \
98
 
        *out = result; \
99
 
        return JS_TRUE; \
100
 
    }\
101
 
    static type*\
102
 
    priv_from_js(JSContext *context, \
103
 
                 JSObject  *object)  \
104
 
    {\
105
 
        return gjs_get_instance_private_dynamic(context, object, &class, NULL); \
 
96
#define GJS_DEFINE_DYNAMIC_PRIV_FROM_JS(type, class)                    \
 
97
    __attribute__((unused)) static inline JSBool                        \
 
98
    do_base_typecheck(JSContext *context,                               \
 
99
                      JSObject  *object,                                \
 
100
                      JSBool     throw)                                 \
 
101
    {                                                                   \
 
102
        return gjs_typecheck_dynamic_instance(context, object, &class, throw); \
 
103
    }                                                                   \
 
104
    static inline type*                                                 \
 
105
    priv_from_js(JSContext *context,                                    \
 
106
                 JSObject  *object)                                     \
 
107
    {                                                                   \
 
108
        return JS_GetPrivate(context, object);                          \
 
109
    }                                                                   \
 
110
    __attribute__((unused)) static JSBool                               \
 
111
    priv_from_js_with_typecheck(JSContext *context,                     \
 
112
                                JSObject  *object,                      \
 
113
                                type      **out)                        \
 
114
    {                                                                   \
 
115
        if (!do_base_typecheck(context, object, JS_FALSE))              \
 
116
            return JS_FALSE;                                            \
 
117
        *out = priv_from_js(context, object);                           \
 
118
        return JS_TRUE;                                                 \
106
119
    }
107
120
 
108
121
/**
227
240
                                              JSFunctionSpec  *static_fs);
228
241
void gjs_throw_constructor_error             (JSContext       *context);
229
242
 
230
 
void* gjs_get_instance_private_dynamic                (JSContext  *context,
231
 
                                                       JSObject   *obj,
232
 
                                                       JSClass    *static_clasp,
233
 
                                                       jsval      *argv);
234
 
void* gjs_get_instance_private_dynamic_with_typecheck (JSContext  *context,
235
 
                                                       JSObject   *obj,
236
 
                                                       JSClass    *static_clasp,
237
 
                                                       jsval      *argv);
 
243
JSBool gjs_typecheck_static_instance          (JSContext  *context,
 
244
                                               JSObject   *obj,
 
245
                                               JSClass    *static_clasp,
 
246
                                               JSBool      _throw);
 
247
JSBool gjs_typecheck_dynamic_instance         (JSContext  *context,
 
248
                                               JSObject   *obj,
 
249
                                               JSClass    *static_clasp,
 
250
                                               JSBool      _throw);
238
251
 
239
252
JSObject*   gjs_construct_object_dynamic     (JSContext       *context,
240
253
                                              JSObject        *proto,
249
262
void        gjs_throw                        (JSContext       *context,
250
263
                                              const char      *format,
251
264
                                              ...)  G_GNUC_PRINTF (2, 3);
 
265
void        gjs_throw_custom                 (JSContext       *context,
 
266
                                              const char      *error_class,
 
267
                                              const char      *format,
 
268
                                              ...)  G_GNUC_PRINTF (3, 4);
252
269
void        gjs_throw_literal                (JSContext       *context,
253
270
                                              const char      *string);
254
271
void        gjs_throw_g_error                (JSContext       *context,