summaryrefslogtreecommitdiff
path: root/include/gpac/internal/smjs_api.h
blob: 683dc7e785f6c471b136915d250a13a99e244e6f (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
/*
 *			GPAC - Multimedia Framework C SDK
 *
 *				Authors: Jean Le Feuvre
 *			Copyright (c) Telecom ParisTech 2010
 *					All rights reserved
 *
 *  This file is part of GPAC / Scene Management sub-project
 *
 *  GPAC is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation; either version 2, or (at your option)
 *  any later version.
 *
 *  GPAC 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 Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; see the file COPYING.  If not, write to
 *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 */

#ifndef GPAC_JSAPI
#define GPAC_JSAPI

#include <gpac/setup.h>

/*WIN32 and WinCE config (no configure script)*/
#if defined(WIN32) || defined(_WIN32_WCE) || defined(__SYMBIAN32__)
# ifndef XP_PC
#  define XP_PC
# endif
# ifndef XP_WIN
#  define XP_WIN
# endif
/*WINCE specific config*/
# if defined(_WIN32_WCE)
#  include <windows.h>
# define XP_WINCE
# endif
#endif

//Android config
#ifdef GPAC_ANDROID
# ifndef XP_UNIX
#  define XP_UNIX
# endif
#endif

//iOS config
#ifdef GPAC_IPHONE
# ifndef XP_UNIX
#  define XP_UNIX
# endif
# ifndef IPHONE_OS
#  define IPHONE_OS
# endif
# ifndef DARWIN
#  define DARWIN
# endif
#endif

/*OSX config*/
#if defined(GPAC_CONFIG_DARWIN) && !defined(GPAC_IPHONE)
# define MOZILLA_1_8_BRANCH
# ifndef XP_UNIX
#  define XP_UNIX
# endif
#endif


/*other platforms should be setup through ./configure and have appropriated defines in config.h*/

#if defined(DEBUG) && defined(GPAC_CONFIG_DARWIN)
#undef DEBUG
#endif

#include <jsapi.h>

#ifndef JS_VERSION
#define JS_VERSION 170
#endif


typedef struct
{
	JSClass _class;
	JSObject *_proto;
} GF_JSClass;



/*new APIs*/
#if (JS_VERSION>=185)

#ifdef USE_FFDEV_18
#define USE_FFDEV_17
#endif

#ifdef USE_FFDEV_17
#define USE_FFDEV_16
#endif

#ifdef USE_FFDEV_16
#define USE_FFDEV_15
#endif

#ifdef USE_FFDEV_15
#define JSVAL_IS_OBJECT(__v) JSVAL_IS_OBJECT_OR_NULL_IMPL(JSVAL_TO_IMPL(__v))
#define USE_FFDEV_14
#endif

#ifdef USE_FFDEV_14
#define USE_FFDEV_12

#define JS_FinalizeStub	NULL
#ifdef USE_FFDEV_16
#define JS_NEW_OBJ_FOR_CONS(__c, __classp, __args)	JS_NewObjectForConstructor(__c, &(__classp)->_class, __args)
#else
#define JS_NEW_OBJ_FOR_CONS(__c, __classp, __args)	JS_NewObjectForConstructor(__c, __classp, __args)
#endif

#else
#define JS_NEW_OBJ_FOR_CONS(__c, __classp, __args)	JS_NewObjectForConstructor(__c, __args)
#endif

#ifdef USE_FFDEV_12
typedef unsigned uintN;
typedef uint32_t jsuint;
typedef int jsint;
typedef double jsdouble;
#endif

#define JS_NewDouble(c, v)	v
#define JS_PropertyStub_forSetter JS_StrictPropertyStub

#if defined(USE_FFDEV_17)

#define SMJS_DECL_FUNC_PROP_SET(func_name) JSBool func_name(JSContext *c, JSHandleObject __hobj, JSHandleId __hid, JSBool strict, JSMutableHandleValue __vp)
#define SMJS_FUNC_PROP_SET(func_name) SMJS_DECL_FUNC_PROP_SET(func_name) { JSObject *obj = *(__hobj._); jsid id = *(__hid._); jsval *vp = __vp._;
#define SMJS_FUNC_PROP_SET_NOVP(func_name) SMJS_DECL_FUNC_PROP_SET(func_name) { JSObject *obj = *(__hobj._); jsid id = *(__hid._);

#define SMJS_DECL_FUNC_PROP_GET(func_name) JSBool func_name(JSContext *c, JSHandleObject __hobj, JSHandleId __hid, JSMutableHandleValue __vp)
#define SMJS_FUNC_PROP_GET(func_name) SMJS_DECL_FUNC_PROP_GET( func_name ) { JSObject *obj = *(__hobj._); jsid id = *(__hid._); jsval *vp = __vp._;
#define SMJS_CALL_PROP_STUB() JS_PropertyStub(c, __hobj, __hid, __vp)
#define DECL_FINALIZE(func_name) void func_name(JSFreeOp *fop, JSObject *obj) { void *c = NULL;

#define SMJS_FUNCTION_SPEC(__name, __fun, __argc) JS_FS(__name, __fun, __argc, 0)
#define SMJS_PROPERTY_SPEC(__name, __tinyid, __flags, __getter, __setter) \
    {__name, __tinyid, __flags, JSOP_WRAPPER(__getter), JSOP_WRAPPER(__setter)}


#else

#ifdef USE_FFDEV_15

#define SMJS_DECL_FUNC_PROP_SET(func_name) JSBool func_name(JSContext *c, JSHandleObject __hobj, JSHandleId __hid, JSBool strict, jsval *vp)
#define SMJS_FUNC_PROP_SET(func_name) SMJS_DECL_FUNC_PROP_SET(func_name) { JSObject *obj = *(__hobj._); jsid id = *(__hid._);
#define SMJS_DECL_FUNC_PROP_GET(func_name) JSBool func_name(JSContext *c, JSHandleObject __hobj, JSHandleId __hid, jsval *vp)
#define SMJS_FUNC_PROP_GET(func_name) SMJS_DECL_FUNC_PROP_GET( func_name ) { JSObject *obj = *(__hobj._); jsid id = *(__hid._);
#define SMJS_CALL_PROP_STUB() JS_PropertyStub(c, __hobj, __hid, vp)
#define DECL_FINALIZE(func_name) void func_name(JSFreeOp *fop, JSObject *obj) { void *c = NULL;

#else

#define SMJS_DECL_FUNC_PROP_SET(func_name) JSBool func_name(JSContext *c, JSObject *obj, jsid id, JSBool strict, jsval *vp)
#define SMJS_FUNC_PROP_SET(func_name) SMJS_DECL_FUNC_PROP_SET( func_name) {
#define SMJS_DECL_FUNC_PROP_GET(func_name) JSBool func_name(JSContext *c, JSObject *obj, jsid id, jsval *vp)
#define SMJS_FUNC_PROP_GET(func_name) SMJS_DECL_FUNC_PROP_GET(func_name) {
#define SMJS_CALL_PROP_STUB() JS_PropertyStub(c, obj, id, vp)
#define DECL_FINALIZE(func_name) void func_name(JSContext *c, JSObject *obj) {

#endif

#define SMJS_FUNC_PROP_SET_NOVP	SMJS_FUNC_PROP_SET

#define SMJS_FUNCTION_SPEC(__name, __fun, __argc) {__name, __fun, __argc, 0}
#define SMJS_PROPERTY_SPEC(__name, __tinyid, __flags, __getter, __setter) \
    {__name, __tinyid, __flags, __getter, __setter}

#endif

#define SMJS_FUNCTION(__name) __name(JSContext *c, uintN argc, jsval *argsvp)
#define SMJS_FUNCTION_EXT(__name, __ext) __name(JSContext *c, uintN argc, jsval *argsvp, __ext)
#define SMJS_ARGS	jsval *argv = JS_ARGV(c, argsvp);
#define SMJS_OBJ	JSObject *obj = JS_THIS_OBJECT(c, argsvp);
#define SMJS_SET_RVAL(__rval) JS_SET_RVAL(c, argsvp, __rval)
#define SMJS_GET_RVAL & JS_RVAL(c, argsvp)
#define SMJS_CALL_ARGS	c, argc, argsvp
#define SMJS_DECL_RVAL jsval *rval = & JS_RVAL(c, argsvp);

#define SMJS_CHARS_FROM_STRING(__c, __jsstr)	(char *) JS_EncodeString(__c, __jsstr)
#define SMJS_CHARS(__c, __val)	SMJS_CHARS_FROM_STRING(__c, JSVAL_TO_STRING(__val))
#define SMJS_FREE(__c, __str)	if (__str) JS_free(__c, __str)


#define SMJS_OBJ_CONSTRUCTOR(__classp)	\
	JSObject *obj = JS_NEW_OBJ_FOR_CONS(c, __classp, argsvp);	\
	SMJS_SET_RVAL(OBJECT_TO_JSVAL(obj));\
 
#define JS_GetFunctionName(_v) (JS_GetFunctionId(_v)!=NULL) ? SMJS_CHARS_FROM_STRING(c, JS_GetFunctionId(_v)) : NULL

#define SMJS_ID_IS_STRING	JSID_IS_STRING
#define SMJS_ID_TO_STRING		JSID_TO_STRING
#define SMJS_ID_IS_INT	JSID_IS_INT
#define SMJS_ID_TO_INT		JSID_TO_INT

#ifndef JS_THREADSAFE
#define JS_THREADSAFE
#endif


#ifdef USE_FFDEV_12
#define JS_ClearContextThread(__ctx)
#define JS_SetContextThread(__ctx)
#define SMJS_GET_PRIVATE(__ctx, __obj)	JS_GetPrivate(__obj)
#define SMJS_SET_PRIVATE(__ctx, __obj, __val)	JS_SetPrivate(__obj, __val)
#define SMJS_GET_PARENT(__ctx, __obj)	JS_GetParent(__obj)
#define SMJS_GET_CLASS(__ctx, __obj)	JS_GetClass(__obj)


#ifdef USE_FFDEV_15
#define JS_GET_CLASS(__ctx, __obj) JS_GetClass(* __obj._)
#else
#define JS_GET_CLASS(__ctx, __obj) JS_GetClass(__obj)
#endif

#ifdef USE_FFDEV_16
#define SMJS_CONSTRUCT_OBJECT(__ctx, __class, __parent)	JS_New(__ctx, JS_GetConstructor(__ctx, (__class)->_proto), 0, NULL)
#else
#define SMJS_CONSTRUCT_OBJECT(__ctx, __class, __parent)	JS_ConstructObject(__ctx, &(__class)->_class, __parent)
#endif

#else

#define SMJS_GET_PRIVATE(__ctx, __obj)	JS_GetPrivate(__ctx, __obj)
#define SMJS_SET_PRIVATE(__ctx, __obj, __val)	JS_SetPrivate(__ctx, __obj, __val)
#define SMJS_GET_PARENT(__ctx, __obj)	JS_GetParent(__ctx, __obj)
#define SMJS_GET_CLASS(__ctx, __obj)	JS_GetClass(__ctx, __obj)

#ifdef USE_FFDEV_11
#define JS_ClearContextThread(__ctx)
#define JS_SetContextThread(__ctx)
#define SMJS_CONSTRUCT_OBJECT(__ctx, __class, __parent)	JS_ConstructObject(__ctx, &(__class)->_class, __parent)
#else
#define SMJS_CONSTRUCT_OBJECT(__ctx, __class, __parent)	JS_ConstructObject(__ctx, &(__class)->_class, 0, __parent)
#endif

#endif


#else

/* Windows with extra-libs */

#define SMJS_DECL_FUNC_PROP_SET(func_name) JSBool func_name(JSContext *c, JSObject *obj, jsval id, jsval *vp)
#define SMJS_FUNC_PROP_SET(func_name) SMJS_DECL_FUNC_PROP_SET(func_name) {
#define SMJS_FUNC_PROP_SET_NOVP	SMJS_FUNC_PROP_SET
#define SMJS_DECL_FUNC_PROP_GET(func_name) JSBool func_name(JSContext *c, JSObject *obj, jsval id, jsval *vp)
#define SMJS_FUNC_PROP_GET(func_name) SMJS_DECL_FUNC_PROP_GET( func_name) {
#define DECL_FINALIZE(func_name) void func_name(JSContext *c, JSObject *obj) {

#define SMJS_CALL_PROP_STUB() JS_PropertyStub(c, obj, id, vp)
#define SMJS_PROP_SETTER jsval id
#define SMJS_PROP_GETTER jsval id
#define JS_PropertyStub_forSetter JS_PropertyStub
#define SMJS_FUNCTION_SPEC(__name, __fun, __argc) {__name, __fun, __argc, 0, 0}
#define SMJS_PROPERTY_SPEC(__name, __tinyid, __flags, __getter, __setter) {__name, __tinyid, __flags, __getter, __setter}
#define SMJS_FUNCTION(__name) __name(JSContext *c, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
#define SMJS_FUNCTION_EXT(__name, __ext) __name(JSContext *c, JSObject *obj, uintN argc, jsval *argv, jsval *rval, __ext)
#define SMJS_ARGS
#define SMJS_OBJ
#define SMJS_OBJ_CONSTRUCTOR(__classp)
#define SMJS_GET_RVAL rval
#define SMJS_SET_RVAL(__rval) *rval = __rval
#define SMJS_CALL_ARGS	c, obj, argc, argv, rval
#define SMJS_DECL_RVAL

#define SMJS_CHARS_FROM_STRING(__c, __str)	JS_GetStringBytes(__str)
#define SMJS_CHARS(__c, __val)	JS_GetStringBytes(JSVAL_TO_STRING(__val))
#define SMJS_FREE(__c, __str)

#define SMJS_ID_IS_STRING	JSVAL_IS_STRING
#define SMJS_ID_TO_STRING		JSVAL_TO_STRING
#define SMJS_ID_IS_INT	JSVAL_IS_INT
#define SMJS_ID_TO_INT		JSVAL_TO_INT

#define SMJS_CONSTRUCT_OBJECT(__ctx, __class, __parent)	JS_ConstructObject(__ctx, &(__class)->_class, 0, __parent)
#define SMJS_GET_PRIVATE(__ctx, __obj)	JS_GetPrivate(__ctx, __obj)
#define SMJS_SET_PRIVATE(__ctx, __obj, __val)	JS_SetPrivate(__ctx, __obj, __val)
#define SMJS_GET_PARENT(__ctx, __obj)	JS_GetParent(__ctx, __obj)
#define SMJS_GET_CLASS(__obj)	JS_GetClass(__obj)

#endif


#ifdef __cplusplus
extern "C" {
#endif

#if (JS_VERSION>=185)
#if defined(USE_FFDEV_18)
JSBool gf_sg_js_has_instance(JSContext *cx, JSHandleObject obj, JSMutableHandleValue vp, JSBool *bp);
#elif defined(USE_FFDEV_15)
JSBool gf_sg_js_has_instance(JSContext *c, JSHandleObject obj,const jsval *val, JSBool *vp);
#else
JSBool gf_sg_js_has_instance(JSContext *c, JSObject *obj,const jsval *val, JSBool *vp);
#endif
#else
JSBool gf_sg_js_has_instance(JSContext *c, JSObject *obj, jsval val, JSBool *vp);
#endif

#define JS_SETUP_CLASS(the_class, cname, flag, getp, setp, fin)	\
	memset(&the_class, 0, sizeof(the_class));	\
	the_class._class.name = cname;	\
	the_class._class.flags = flag;	\
	the_class._class.addProperty = JS_PropertyStub;	\
	the_class._class.delProperty = JS_PropertyStub;	\
	the_class._class.getProperty = getp;	\
	the_class._class.setProperty = setp;	\
	the_class._class.enumerate = JS_EnumerateStub;	\
	the_class._class.resolve = JS_ResolveStub;		\
	the_class._class.convert = JS_ConvertStub;		\
	the_class._class.finalize = (JSFinalizeOp) fin;	\
	the_class._class.hasInstance = gf_sg_js_has_instance;


#define JS_MAKE_DOUBLE(__c, __double)	DOUBLE_TO_JSVAL(JS_NewDouble(__c, __double) )


#define GF_JS_InstanceOf(_a, _b, __class, _d) JS_InstanceOf(_a, _b, & (__class)->_class, NULL)

#define GF_JS_InitClass(cx, obj, parent_proto, clasp, constructor, nargs, ps, fs, static_ps, static_fs) \
		(clasp)->_proto = JS_InitClass(cx, obj, parent_proto, &(clasp)->_class, constructor, nargs, ps, fs, static_ps, static_fs);


#define SMJS_GET_NUMBER(val, _d)  {	\
		if (!JSVAL_IS_NUMBER(val) || (val == JS_GetNaNValue(c)) ) { \
			GF_LOG(GF_LOG_ERROR, GF_LOG_SCRIPT, ("[JS] Value is not a number while assigning @ file %s line %d \n", __FILE__, __LINE__));	\
			return JS_FALSE; \
		}	\
		JS_ValueToNumber(c, val, &_d);	\
		if (_d != _d) { \
			GF_LOG(GF_LOG_ERROR, GF_LOG_SCRIPT, ("[JS] Value is not a number while assigning @ file %s line %d \n", __FILE__, __LINE__));	\
			return JS_FALSE; \
		} \
	}	\
 
JSObject *gf_sg_js_global_object(JSContext *cx, GF_JSClass *__class);

#ifdef __cplusplus
}
#endif

#endif //GPAC_JSAPI