1 
2 #ifndef EL__ECMASCRIPT_SPIDERMONKEY_UTIL_H
3 #define EL__ECMASCRIPT_SPIDERMONKEY_UTIL_H
4 
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
17 
18 #include <jsapi.h>
19 #include "util/memory.h"
20 #include "util/string.h"
21 
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);
28 
29 static int jsval_to_boolean(JSContext *ctx, jsval *vp);
30 static unsigned char *jsval_to_string(JSContext *ctx, jsval *vp);
31 
32 
33 
34 /** Inline functions */
35 
36 static inline void
string_to_jsval(JSContext * ctx,jsval * vp,unsigned char * string)37 string_to_jsval(JSContext *ctx, jsval *vp, unsigned char *string)
38 {
39 	if (!string) {
40 		*vp = JSVAL_NULL;
41 	} else {
42 		*vp = STRING_TO_JSVAL(JS_NewStringCopyZ(ctx, string));
43 	}
44 }
45 
46 static inline void
astring_to_jsval(JSContext * ctx,jsval * vp,unsigned char * string)47 astring_to_jsval(JSContext *ctx, jsval *vp, unsigned char *string)
48 {
49 	string_to_jsval(ctx, vp, string);
50 	mem_free_if(string);
51 }
52 
53 static inline void
int_to_jsval(JSContext * ctx,jsval * vp,int number)54 int_to_jsval(JSContext *ctx, jsval *vp, int number)
55 {
56 	*vp = INT_TO_JSVAL(number);
57 }
58 
59 static inline void
object_to_jsval(JSContext * ctx,jsval * vp,JSObject * object)60 object_to_jsval(JSContext *ctx, jsval *vp, JSObject *object)
61 {
62 	*vp = OBJECT_TO_JSVAL(object);
63 }
64 
65 static inline void
boolean_to_jsval(JSContext * ctx,jsval * vp,int boolean)66 boolean_to_jsval(JSContext *ctx, jsval *vp, int boolean)
67 {
68 	*vp = BOOLEAN_TO_JSVAL(boolean);
69 }
70 
71 static inline void
undef_to_jsval(JSContext * ctx,jsval * vp)72 undef_to_jsval(JSContext *ctx, jsval *vp)
73 {
74 	*vp = JSVAL_NULL;
75 }
76 
77 
78 static inline int
jsval_to_boolean(JSContext * ctx,jsval * vp)79 jsval_to_boolean(JSContext *ctx, jsval *vp)
80 {
81 	jsval val;
82 
83 	if (JS_ConvertValue(ctx, *vp, JSTYPE_BOOLEAN, &val) == JS_FALSE) {
84 		return JS_FALSE;
85 	}
86 
87 	return JSVAL_TO_BOOLEAN(val);
88 }
89 
90 static inline unsigned char *
jsval_to_string(JSContext * ctx,jsval * vp)91 jsval_to_string(JSContext *ctx, jsval *vp)
92 {
93 	jsval val;
94 
95 	if (JS_ConvertValue(ctx, *vp, JSTYPE_STRING, &val) == JS_FALSE) {
96 		return "";
97 	}
98 
99 	return empty_string_or_(JS_GetStringBytes(JS_ValueToString(ctx, val)));
100 }
101 
102 /** An ELinks-specific replacement for JSFunctionSpec.
103  *
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;
116 
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);
126 
127 #endif
128