summaryrefslogtreecommitdiff
path: root/javascript/jsapi.h
blob: 5b544b8fdb30b4d0eea01a92fe35720f8e5c0cdc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
/*
 * Copyright 2012 Vincent Sanders <vince@netsurf-browser.org>
 *
 * This file is part of NetSurf, http://www.netsurf-browser.org/
 *
 * NetSurf is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2 of the License.
 *
 * NetSurf is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/** \file
 * spidermonkey jsapi compatability glue.
 */

#ifndef _NETSURF_JAVASCRIPT_JSAPI_H_
#define _NETSURF_JAVASCRIPT_JSAPI_H_

/* include the correct header */
#ifdef WITH_MOZJS
#include "js/jsapi.h"
#else
#include "mozjs/jsapi.h"
#endif

/* logging macros */
#define JSLOG(args...) LOG((args))
#ifdef ENABLE_VERBOSE_JS_DEBUG
#define JSDBG(args...) LOG((args))
#else
#define JSDBG(args...)
#endif

#if JS_VERSION < 180

/************************** Spidermonkey 1.7.0 **************************/

#include <string.h>

#ifndef JSVERSION_LATEST
#define JSVERSION_LATEST JS_VERSION
#endif

#ifndef JSOPTION_JIT
#define JSOPTION_JIT 0
#endif

/* *CAUTION* these native function macros introduce and use jsapi_this
 * and jsapi_rval variables, native function code should not conflict
 * with these
 */

/* native function definition with five parameters */
#define JSAPI_FUNC(name, cx, argc, vp) \
	jsapi_func_##name(cx, JSObject *jsapi_this, argc, vp, jsval *jsapi_rval)

/* native function return value - No macro available */
#define JSAPI_FUNC_RVAL(cx, vp) (jsapi_rval)

/* native function return value setter - No macro available  */
#define JSAPI_FUNC_SET_RVAL(cx, vp, v) (*jsapi_rval = (v))

/* arguments */
#define JSAPI_FUNC_ARGV(cx, vp) (vp)

/* check if a jsval is an object */
#define JSAPI_JSVAL_IS_OBJECT(v) JSVAL_IS_OBJECT(v)

/* native function specifier with five parameters and no JS_FS macro */
#define JSAPI_FS(name, nargs, flags) \
	{ #name, jsapi_func_##name, nargs, flags, 0 }

/* native function specifier list end */
#define JSAPI_FS_END { NULL, NULL, 0, 0, 0 }




/* native proprty definition */
#define JSAPI_PROP(name, cx, obj, vp) \
	jsapi_property_##name(cx, obj, jsval jsapi_id, vp)
#define JSAPI_STRICTPROP JSAPI_PROP

/* native property return value */
#define JSAPI_PROP_RVAL(cx, vp) (*(vp))

/* native property getter return value */
#define JSAPI_PROP_SET_RVAL(cx, vp, v) (*(vp) = (v))

/* native property ID value as a jsval */
#define JSAPI_PROP_IDVAL(cx, vp) (*(vp) = jsapi_id)

/* native property specifier */
#define JSAPI_PS(name, fnname, tinyid, flags)				\
	{ name , tinyid , flags , jsapi_property_##fnname##_get , jsapi_property_##fnname##_set }

/* native property specifier with no setter */
#define JSAPI_PS_RO(name, fnname, tinyid, flags)				\
	{ name , tinyid , flags | JSPROP_READONLY, jsapi_property_##fnname##_get , NULL }

/* native property specifier list end */
#define JSAPI_PS_END { NULL, 0, 0, NULL, NULL }

#define JS_StrictPropertyStub JS_PropertyStub




/* The object instance in a native call */
/* "this" JSObject getter */
JSObject * js_ComputeThis(JSContext *cx, JSObject *thisp, void *argv);
#define JSAPI_THIS_OBJECT(cx, vp) \
        js_ComputeThis(cx, JSVAL_TO_OBJECT(vp[-1]), vp)

static inline JSObject *
JS_NewCompartmentAndGlobalObject(JSContext *cx,
				 JSClass *jsclass,
				 JSPrincipals *principals)
{
	JSObject *global;
	global = JS_NewObject(cx, jsclass, NULL, NULL);
	if (global == NULL) {
		return NULL;
	}
	return global;
}


#define JSString_to_char(injsstring, outchar, outlen)	\
	outchar = JS_GetStringBytes(injsstring);		\
	outlen = strlen(outchar)

/* string type cast */
#define JSAPI_STRING_TO_JSVAL(str) ((str == NULL)?JSVAL_NULL:STRING_TO_JSVAL(str))

#define JSAPI_CLASS_NO_INTERNAL_MEMBERS NULL

/* Garbage Collector */

/* macros for GC marking */
#define JSAPI_JSCLASS_MARK_IS_TRACE 0

#define JSAPI_JSCLASS_MARKOP(x) (x)

#define JSAPI_MARKOP(name) uint32_t name(JSContext *cx, JSObject *obj, void *arg)

#define JSAPI_MARKCX cx

#define JSAPI_GCMARK(thing) JS_MarkGCThing(cx, thing, "object", arg)

#define JSAPI_MARKOP_RETURN(value) return value


/* Macros for manipulating GC root */
#define JSAPI_ADD_OBJECT_ROOT(cx, obj) JS_AddRoot(cx, obj)
#define JSAPI_REMOVE_OBJECT_ROOT(cx, obj) JS_RemoveRoot(cx, obj)

#define JSAPI_ADD_VALUE_ROOT(cx, obj) JS_AddRoot(cx, obj)
#define JSAPI_REMOVE_VALUE_ROOT(cx, obj) JS_RemoveRoot(cx, obj)

#elif JS_VERSION == 180

/************************** Spidermonkey 1.8.0 **************************/

#include <string.h>

/* *CAUTION* these macros introduce and use jsapi_this and jsapi_rval
 * parameters, native function code should not conflict with these
 */

/* five parameter jsapi native call */
#define JSAPI_FUNC(name, cx, argc, vp) \
	jsapi_func_##name(cx, JSObject *jsapi_this, argc, vp, jsval *jsapi_rval)

/* five parameter function descriptor */
#define JSAPI_FS(name, nargs, flags) \
	JS_FS(#name, jsapi_func_##name, nargs, flags, 0)

/* function descriptor end */
#define JSAPI_FS_END JS_FS_END

/* return value */
#define JSAPI_RVAL(cx, vp) JS_RVAL(cx, jsapi_rval)

/* return value setter */
#define JSAPI_FUNC_SET_RVAL(cx, vp, v) JS_SET_RVAL(cx, jsapi_rval, v)

/* arguments */
#define JSAPI_FUNC_ARGV(cx, vp) (vp)

/* check if a jsval is an object */
#define JSAPI_JSVAL_IS_OBJECT(v) JSVAL_IS_OBJECT(v)

/* The object instance in a native call */
#define JSAPI_THIS_OBJECT(cx,vp) jsapi_this



/* proprty native calls */
#define JSAPI_PROP(name, cx, obj, vp) \
	jsapi_property_##name(cx, obj, jsval jsapi_id, vp)
#define JSAPI_STRICTPROP JSAPI_PROP

/* native property return value */
#define JSAPI_PROP_RVAL JS_RVAL

/* native property return value setter */
#define JSAPI_PROP_SET_RVAL JS_SET_RVAL

/* native property ID value as a jsval */
#define JSAPI_PROP_IDVAL(cx, vp) (*(vp) = jsapi_id)

/* property specifier */
#define JSAPI_PS(name, fnname, tinyid, flags)				\
	{ name , tinyid , flags , jsapi_property_##fnname##_get , jsapi_property_##fnname##_set }

#define JSAPI_PS_RO(name, fnname, tinyid, flags)				\
	{ name , tinyid , flags | JSPROP_READONLY, jsapi_property_##fnname##_get , NULL }

#define JSAPI_PS_END { NULL, 0, 0, NULL, NULL }




static inline JSObject *
JS_NewCompartmentAndGlobalObject(JSContext *cx,
				 JSClass *jsclass,
				 JSPrincipals *principals)
{
	JSObject *global;
	global = JS_NewObject(cx, jsclass, NULL, NULL);
	if (global == NULL) {
		return NULL;
	}
	return global;
}

#define JS_StrictPropertyStub JS_PropertyStub

#define JSString_to_char(injsstring, outchar, outlen)	\
	outchar = JS_GetStringBytes(injsstring);		\
	outlen = strlen(outchar)

/* string type cast */
#define JSAPI_STRING_TO_JSVAL(str) ((str == NULL)?JSVAL_NULL:STRING_TO_JSVAL(str))

#define JSAPI_CLASS_NO_INTERNAL_MEMBERS NULL

/* Garbage Collector */

/* GC marking */
#ifdef JSCLASS_MARK_IS_TRACE
/* mark function pointer requires casting */
#define JSAPI_JSCLASS_MARK_IS_TRACE JSCLASS_MARK_IS_TRACE
#define JSAPI_JSCLASS_MARKOP(x) ((JSMarkOp)x)
#else
/* mark function pointer does not require casting */
#define JSAPI_JSCLASS_MARK_IS_TRACE 0
#define JSAPI_JSCLASS_MARKOP(x) (x)
#endif

#define JSAPI_MARKOP(name) JSBool name(JSTracer *trc, JSObject *obj)

#define JSAPI_MARKCX trc->context

#define JSAPI_GCMARK(thing) JS_CallTracer(trc, thing, JSTRACE_OBJECT);

#define JSAPI_MARKOP_RETURN(value) return value

/* Macros for manipulating GC root */
#define JSAPI_ADD_OBJECT_ROOT(cx, obj) JS_AddRoot(cx, obj)
#define JSAPI_REMOVE_OBJECT_ROOT(cx, obj) JS_RemoveRoot(cx, obj)

#define JSAPI_ADD_VALUE_ROOT(cx, obj) JS_AddRoot(cx, obj)
#define JSAPI_REMOVE_VALUE_ROOT(cx, obj) JS_RemoveRoot(cx, obj)


#else /* #if JS_VERSION == 180 */

/************************** Spidermonkey 1.8.5 **************************/

/* three parameter jsapi native call */
#define JSAPI_FUNC(name, cx, argc, vp) jsapi_func_##name(cx, argc, vp)

/* three parameter function descriptor */
#define JSAPI_FS(name, nargs, flags) \
	JS_FS(#name, jsapi_func_##name, nargs, flags)

/* function descriptor end */
#define JSAPI_FS_END JS_FS_END

/* return value */
#define JSAPI_RVAL JS_RVAL

/* return value setter */
#define JSAPI_FUNC_SET_RVAL JS_SET_RVAL

/* arguments */
#define JSAPI_FUNC_ARGV(cx, vp) JS_ARGV(cx,vp)

/* check if a jsval is an object */
#define JSAPI_JSVAL_IS_OBJECT(v) JSVAL_IS_OBJECT(v)
/* The docuemntation says this is obsolete and should be
 * ((JSVAL_IS_NULL(v)) || (JSVAL_IS_PRIMITIVE(v)))
 * which doesnt work
 */


/* The object instance in a native call */
#define JSAPI_THIS_OBJECT(cx,vp) JS_THIS_OBJECT(cx,vp)

/* proprty native calls */
#define JSAPI_PROP(name, cx, obj, vp) \
	jsapi_property_##name(cx, obj, jsid jsapi_id, vp)
#define JSAPI_STRICTPROP(name, cx, obj, vp) \
	jsapi_property_##name(cx, obj, jsid jsapi_id, JSBool strict, vp)

/* native property return value */
#define JSAPI_PROP_RVAL JS_RVAL

/* native property getter return value */
#define JSAPI_PROP_SET_RVAL JS_SET_RVAL

/* native property ID value as a jsval */
#define JSAPI_PROP_IDVAL(cx, vp) JS_IdToValue(cx, jsapi_id, vp)

/* property specifier */
#define JSAPI_PS(name, fnname, tinyid, flags) {			\
		name,						\
		tinyid,						\
		flags,						\
		jsapi_property_##fnname##_get,			\
		jsapi_property_##fnname##_set			\
	}

#define JSAPI_PS_RO(name, fnname, tinyid, flags) {		\
		name,						\
		tinyid,						\
		flags | JSPROP_READONLY,			\
		jsapi_property_##fnname##_get,			\
		NULL						\
	}

#define JSAPI_PS_END { NULL, 0, 0, NULL, NULL }


#define JSString_to_char(injsstring, outchar, outlen)		\
	outlen = JS_GetStringLength(injsstring);		\
	outchar = alloca(sizeof(char)*(outlen+1));		\
	JS_EncodeStringToBuffer(injsstring, outchar, outlen);	\
	outchar[outlen] = '\0'

/* string type cast */
#define JSAPI_STRING_TO_JSVAL(str) ((str == NULL)?JSVAL_NULL:STRING_TO_JSVAL(str))

#define JSAPI_CLASS_NO_INTERNAL_MEMBERS JSCLASS_NO_INTERNAL_MEMBERS

/* GC marking */
#ifdef JSCLASS_MARK_IS_TRACE
/* mark function pointer requires casting */
#define JSAPI_JSCLASS_MARK_IS_TRACE JSCLASS_MARK_IS_TRACE
#define JSAPI_JSCLASS_MARKOP(x) ((JSMarkOp)x)
#else
/* mark function pointer does not require casting */
#define JSAPI_JSCLASS_MARK_IS_TRACE 0
#define JSAPI_JSCLASS_MARKOP(x) (x)
#endif

#define JSAPI_MARKOP(name) void name(JSTracer *trc, JSObject *obj)

#define JSAPI_MARKCX trc->context

#define JSAPI_GCMARK(thing) JS_CallTracer(trc, thing, JSTRACE_OBJECT);

#define JSAPI_MARKOP_RETURN(value)


/* Macros for manipulating GC root */
#define JSAPI_ADD_OBJECT_ROOT(cx, obj) JS_AddObjectRoot(cx, obj)
#define JSAPI_REMOVE_OBJECT_ROOT(cx, obj) JS_RemoveObjectRoot(cx, obj)

#define JSAPI_ADD_VALUE_ROOT(cx, val) JS_AddValueRoot(cx, val)
#define JSAPI_REMOVE_VALUE_ROOT(cx, val) JS_RemoveValueRoot(cx, val)

#endif

/************************** **************************/

#endif