1 /* Better compatibility across versions of SpiderMonkey. */
2 
3 #ifdef HAVE_CONFIG_H
4 #include "config.h"
5 #endif
6 
7 #include "elinks.h"
8 
9 #include "ecmascript/spidermonkey/util.h"
10 
11 /** An ELinks-specific replacement for JS_DefineFunctions().
12  *
13  * @relates spidermonkeyFunctionSpec */
14 JSBool
spidermonkey_DefineFunctions(JSContext * cx,JSObject * obj,const spidermonkeyFunctionSpec * fs)15 spidermonkey_DefineFunctions(JSContext *cx, JSObject *obj,
16 			     const spidermonkeyFunctionSpec *fs)
17 {
18 	for (; fs->name; fs++) {
19 		if (!JS_DefineFunction(cx, obj, fs->name, fs->call,
20 				       fs->nargs, 0))
21 			return JS_FALSE;
22 	}
23 	return JS_TRUE;
24 }
25 
26 /** An ELinks-specific replacement for JS_InitClass().
27  *
28  * @relates spidermonkeyFunctionSpec */
29 JSObject *
spidermonkey_InitClass(JSContext * cx,JSObject * obj,JSObject * parent_proto,JSClass * clasp,JSNative constructor,uintN nargs,JSPropertySpec * ps,const spidermonkeyFunctionSpec * fs,JSPropertySpec * static_ps,const spidermonkeyFunctionSpec * static_fs)30 spidermonkey_InitClass(JSContext *cx, JSObject *obj,
31 		       JSObject *parent_proto, JSClass *clasp,
32 		       JSNative constructor, uintN nargs,
33 		       JSPropertySpec *ps,
34 		       const spidermonkeyFunctionSpec *fs,
35 		       JSPropertySpec *static_ps,
36 		       const spidermonkeyFunctionSpec *static_fs)
37 {
38 	JSObject *proto = JS_InitClass(cx, obj, parent_proto, clasp,
39 				       constructor, nargs,
40 				       ps, NULL, static_ps, NULL);
41 
42 	if (proto == NULL)
43 		return NULL;
44 
45 	if (fs) {
46 		if (!spidermonkey_DefineFunctions(cx, proto, fs))
47 			return NULL;
48 	}
49 
50 	if (static_fs) {
51 		JSObject *cons_obj = JS_GetConstructor(cx, proto);
52 
53 		if (cons_obj == NULL)
54 			return NULL;
55 		if (!spidermonkey_DefineFunctions(cx, cons_obj, static_fs))
56 			return NULL;
57 	}
58 
59 	return proto;
60 }
61