2 #ifndef EL__ECMASCRIPT_SPIDERMONKEY_UTIL_H
3 #define EL__ECMASCRIPT_SPIDERMONKEY_UTIL_H
5 /* For wild SpiderMonkey installations. */
11 #error Out of luck, buddy!
19 #include "util/memory.h"
20 #include "util/string.h"
22 static void string_to_jsval(JSContext
*ctx
, jsval
*vp
, unsigned char *string
);
23 static void astring_to_jsval(JSContext
*ctx
, jsval
*vp
, unsigned char *string
);
24 static void int_to_jsval(JSContext
*ctx
, jsval
*vp
, int number
);
25 static void object_to_jsval(JSContext
*ctx
, jsval
*vp
, JSObject
*object
);
26 static void boolean_to_jsval(JSContext
*ctx
, jsval
*vp
, int boolean
);
27 static void undef_to_jsval(JSContext
*ctx
, jsval
*vp
);
29 static int jsval_to_boolean(JSContext
*ctx
, jsval
*vp
);
30 static unsigned char *jsval_to_string(JSContext
*ctx
, jsval
*vp
);
34 /** Inline functions */
37 string_to_jsval(JSContext
*ctx
, jsval
*vp
, unsigned char *string
)
42 *vp
= STRING_TO_JSVAL(JS_NewStringCopyZ(ctx
, string
));
47 astring_to_jsval(JSContext
*ctx
, jsval
*vp
, unsigned char *string
)
49 string_to_jsval(ctx
, vp
, string
);
54 int_to_jsval(JSContext
*ctx
, jsval
*vp
, int number
)
56 *vp
= INT_TO_JSVAL(number
);
60 object_to_jsval(JSContext
*ctx
, jsval
*vp
, JSObject
*object
)
62 *vp
= OBJECT_TO_JSVAL(object
);
66 boolean_to_jsval(JSContext
*ctx
, jsval
*vp
, int boolean
)
68 *vp
= BOOLEAN_TO_JSVAL(boolean
);
72 undef_to_jsval(JSContext
*ctx
, jsval
*vp
)
79 jsval_to_boolean(JSContext
*ctx
, jsval
*vp
)
83 if (JS_ConvertValue(ctx
, *vp
, JSTYPE_BOOLEAN
, &val
) == JS_FALSE
) {
87 return JSVAL_TO_BOOLEAN(val
);
90 static inline unsigned char *
91 jsval_to_string(JSContext
*ctx
, jsval
*vp
)
95 if (JS_ConvertValue(ctx
, *vp
, JSTYPE_STRING
, &val
) == JS_FALSE
) {
99 return empty_string_or_(JS_GetStringBytes(JS_ValueToString(ctx
, val
)));
102 /** An ELinks-specific replacement for JSFunctionSpec.
104 * Bug 1016: In SpiderMonkey 1.7 bundled with XULRunner 1.8, jsapi.h
105 * defines JSFunctionSpec in different ways depending on whether
106 * MOZILLA_1_8_BRANCH is defined, and there is no obvious way for
107 * ELinks to check whether MOZILLA_1_8_BRANCH was defined when the
108 * library was built. Avoid the unstable JSFunctionSpec definitions
109 * and use this ELinks-specific structure instead. */
110 typedef struct spidermonkeyFunctionSpec
{
114 /* ELinks does not use "flags" and "extra" so omit them here. */
115 } spidermonkeyFunctionSpec
;
117 JSBool
spidermonkey_DefineFunctions(JSContext
*cx
, JSObject
*obj
,
118 const spidermonkeyFunctionSpec
*fs
);
119 JSObject
*spidermonkey_InitClass(JSContext
*cx
, JSObject
*obj
,
120 JSObject
*parent_proto
, JSClass
*clasp
,
121 JSNative constructor
, uintN nargs
,
123 const spidermonkeyFunctionSpec
*fs
,
124 JSPropertySpec
*static_ps
,
125 const spidermonkeyFunctionSpec
*static_fs
);