grafthistory: support curl
[elinks/elinks-j605.git] / src / ecmascript / spidermonkey / util.h
blob543d9773c12f35a7f8e6e96c730ee24307baccc3
2 #ifndef EL__ECMASCRIPT_SPIDERMONKEY_UTIL_H
3 #define EL__ECMASCRIPT_SPIDERMONKEY_UTIL_H
5 /* For wild SpiderMonkey installations. */
6 #ifdef CONFIG_BEOS
7 #define XP_BEOS
8 #elif CONFIG_OS2
9 #define XP_OS2
10 #elif CONFIG_RISCOS
11 #error Out of luck, buddy!
12 #elif CONFIG_UNIX
13 #define XP_UNIX
14 #elif CONFIG_WIN32
15 #define XP_WIN
16 #endif
18 #include <jsapi.h>
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 */
36 static inline void
37 string_to_jsval(JSContext *ctx, jsval *vp, unsigned char *string)
39 if (!string) {
40 *vp = JSVAL_NULL;
41 } else {
42 *vp = STRING_TO_JSVAL(JS_NewStringCopyZ(ctx, string));
46 static inline void
47 astring_to_jsval(JSContext *ctx, jsval *vp, unsigned char *string)
49 string_to_jsval(ctx, vp, string);
50 mem_free_if(string);
53 static inline void
54 int_to_jsval(JSContext *ctx, jsval *vp, int number)
56 *vp = INT_TO_JSVAL(number);
59 static inline void
60 object_to_jsval(JSContext *ctx, jsval *vp, JSObject *object)
62 *vp = OBJECT_TO_JSVAL(object);
65 static inline void
66 boolean_to_jsval(JSContext *ctx, jsval *vp, int boolean)
68 *vp = BOOLEAN_TO_JSVAL(boolean);
71 static inline void
72 undef_to_jsval(JSContext *ctx, jsval *vp)
74 *vp = JSVAL_NULL;
78 static inline int
79 jsval_to_boolean(JSContext *ctx, jsval *vp)
81 jsval val;
83 if (JS_ConvertValue(ctx, *vp, JSTYPE_BOOLEAN, &val) == JS_FALSE) {
84 return JS_FALSE;
87 return JSVAL_TO_BOOLEAN(val);
90 static inline unsigned char *
91 jsval_to_string(JSContext *ctx, jsval *vp)
93 jsval val;
95 if (JS_ConvertValue(ctx, *vp, JSTYPE_STRING, &val) == JS_FALSE) {
96 return "";
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 {
111 const char *name;
112 JSNative call;
113 uint8 nargs;
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,
122 JSPropertySpec *ps,
123 const spidermonkeyFunctionSpec *fs,
124 JSPropertySpec *static_ps,
125 const spidermonkeyFunctionSpec *static_fs);
127 #endif