Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Last modification | View Log | Download | RSS feed

  1. /*
  2.  * Copyright 2012 Vincent Sanders <vince@netsurf-browser.org>
  3.  *
  4.  * This file is part of NetSurf, http://www.netsurf-browser.org/
  5.  *
  6.  * NetSurf is free software; you can redistribute it and/or modify
  7.  * it under the terms of the GNU General Public License as published by
  8.  * the Free Software Foundation; version 2 of the License.
  9.  *
  10.  * NetSurf is distributed in the hope that it will be useful,
  11.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.  * GNU General Public License for more details.
  14.  *
  15.  * You should have received a copy of the GNU General Public License
  16.  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  17.  */
  18.  
  19. /** \file
  20.  * spidermonkey jsapi compatability glue.
  21.  */
  22.  
  23. #ifndef _NETSURF_JAVASCRIPT_JSAPI_H_
  24. #define _NETSURF_JAVASCRIPT_JSAPI_H_
  25.  
  26. #ifdef WITH_MOZJS
  27. #include "js/jsapi.h"
  28. #else
  29. #include "mozjs/jsapi.h"
  30. #endif
  31.  
  32. #if JS_VERSION < 180
  33.  
  34. /************************** Spidermonkey 1.7.0 **************************/
  35.  
  36. #include <string.h>
  37.  
  38. #ifndef JSVERSION_LATEST
  39. #define JSVERSION_LATEST JS_VERSION
  40. #endif
  41.  
  42. #ifndef JSOPTION_JIT
  43. #define JSOPTION_JIT 0
  44. #endif
  45.  
  46. /* *CAUTION* these native function macros introduce and use jsapi_this
  47.  * and jsapi_rval variables, native function code should not conflict
  48.  * with these
  49.  */
  50.  
  51. /* native function definition with five parameters */
  52. #define JSAPI_FUNC(name, cx, argc, vp) \
  53.         jsapi_func_##name(cx, JSObject *jsapi_this, argc, vp, jsval *jsapi_rval)
  54.  
  55. /* native function return value - No macro available */
  56. #define JSAPI_FUNC_RVAL(cx, vp) (jsapi_rval)
  57.  
  58. /* native function return value setter - No macro available  */
  59. #define JSAPI_FUNC_SET_RVAL(cx, vp, v) (*jsapi_rval = (v))
  60.  
  61. /* arguments */
  62. #define JSAPI_FUNC_ARGV(cx, vp) (vp)
  63.  
  64. /* check if a jsval is an object */
  65. #define JSAPI_JSVAL_IS_OBJECT(v) JSVAL_IS_OBJECT(v)
  66.  
  67. /* native function specifier with five parameters and no JS_FS macro */
  68. #define JSAPI_FS(name, nargs, flags) \
  69.         { #name, jsapi_func_##name, nargs, flags, 0 }
  70.  
  71. /* native function specifier list end */
  72. #define JSAPI_FS_END { NULL, NULL, 0, 0, 0 }
  73.  
  74.  
  75.  
  76.  
  77. /* native proprty definition */
  78. #define JSAPI_PROP(name, cx, obj, vp) \
  79.         jsapi_property_##name(cx, obj, jsval jsapi_id, vp)
  80. #define JSAPI_STRICTPROP JSAPI_PROP
  81.  
  82. /* native property return value */
  83. #define JSAPI_PROP_RVAL(cx, vp) (*(vp))
  84.  
  85. /* native property getter return value */
  86. #define JSAPI_PROP_SET_RVAL(cx, vp, v) (*(vp) = (v))
  87.  
  88. /* native property ID value as a jsval */
  89. #define JSAPI_PROP_IDVAL(cx, vp) (*(vp) = jsapi_id)
  90.  
  91. /* native property specifier */
  92. #define JSAPI_PS(name, fnname, tinyid, flags)                           \
  93.         { name , tinyid , flags , jsapi_property_##fnname##_get , jsapi_property_##fnname##_set }
  94.  
  95. /* native property specifier with no setter */
  96. #define JSAPI_PS_RO(name, fnname, tinyid, flags)                                \
  97.         { name , tinyid , flags | JSPROP_READONLY, jsapi_property_##fnname##_get , NULL }
  98.  
  99. /* native property specifier list end */
  100. #define JSAPI_PS_END { NULL, 0, 0, NULL, NULL }
  101.  
  102. #define JS_StrictPropertyStub JS_PropertyStub
  103.  
  104.  
  105.  
  106.  
  107. /* The object instance in a native call */
  108. /* "this" JSObject getter */
  109. JSObject * js_ComputeThis(JSContext *cx, JSObject *thisp, void *argv);
  110. #define JSAPI_THIS_OBJECT(cx, vp) \
  111.         js_ComputeThis(cx, JSVAL_TO_OBJECT(vp[-1]), vp)
  112.  
  113. static inline JSObject *
  114. JS_NewCompartmentAndGlobalObject(JSContext *cx,
  115.                                  JSClass *jsclass,
  116.                                  JSPrincipals *principals)
  117. {
  118.         JSObject *global;
  119.         global = JS_NewObject(cx, jsclass, NULL, NULL);
  120.         if (global == NULL) {
  121.                 return NULL;
  122.         }
  123.         return global;
  124. }
  125.  
  126.  
  127. #define JSString_to_char(injsstring, outchar, outlen)   \
  128.         outchar = JS_GetStringBytes(injsstring);                \
  129.         outlen = strlen(outchar)
  130.  
  131. /* string type cast */
  132. #define JSAPI_STRING_TO_JSVAL(str) ((str == NULL)?JSVAL_NULL:STRING_TO_JSVAL(str))
  133.  
  134. #define JSAPI_CLASS_NO_INTERNAL_MEMBERS NULL
  135.  
  136. /* Garbage Collector */
  137.  
  138. /* macros for GC marking */
  139. #define JSAPI_JSCLASS_MARK_IS_TRACE 0
  140.  
  141. #define JSAPI_JSCLASS_MARKOP(x) (x)
  142.  
  143. #define JSAPI_MARKOP(name) uint32_t name(JSContext *cx, JSObject *obj, void *arg)
  144.  
  145. #define JSAPI_MARKCX cx
  146.  
  147. #define JSAPI_GCMARK(thing) JS_MarkGCThing(cx, thing, "object", arg)
  148.  
  149. /* Macros for manipulating GC root */
  150.  
  151. #define JSAPI_ADD_OBJECT_ROOT(cx, obj) JS_AddRoot(cx, obj)
  152. #define JSAPI_REMOVE_OBJECT_ROOT(cx, obj) JS_RemoveRoot(cx, obj)
  153.  
  154. #define JSAPI_ADD_VALUE_ROOT(cx, obj) JS_AddRoot(cx, obj)
  155. #define JSAPI_REMOVE_VALUE_ROOT(cx, obj) JS_RemoveRoot(cx, obj)
  156.  
  157. #elif JS_VERSION == 180
  158.  
  159. /************************** Spidermonkey 1.8.0 **************************/
  160.  
  161. #include <string.h>
  162.  
  163. /* *CAUTION* these macros introduce and use jsapi_this and jsapi_rval
  164.  * parameters, native function code should not conflict with these
  165.  */
  166.  
  167. /* five parameter jsapi native call */
  168. #define JSAPI_FUNC(name, cx, argc, vp) \
  169.         jsapi_func_##name(cx, JSObject *jsapi_this, argc, vp, jsval *jsapi_rval)
  170.  
  171. /* five parameter function descriptor */
  172. #define JSAPI_FS(name, nargs, flags) \
  173.         JS_FS(#name, jsapi_func_##name, nargs, flags, 0)
  174.  
  175. /* function descriptor end */
  176. #define JSAPI_FS_END JS_FS_END
  177.  
  178. /* return value */
  179. #define JSAPI_RVAL(cx, vp) JS_RVAL(cx, jsapi_rval)
  180.  
  181. /* return value setter */
  182. #define JSAPI_FUNC_SET_RVAL(cx, vp, v) JS_SET_RVAL(cx, jsapi_rval, v)
  183.  
  184. /* arguments */
  185. #define JSAPI_FUNC_ARGV(cx, vp) (vp)
  186.  
  187. /* check if a jsval is an object */
  188. #define JSAPI_JSVAL_IS_OBJECT(v) JSVAL_IS_OBJECT(v)
  189.  
  190. /* The object instance in a native call */
  191. #define JSAPI_THIS_OBJECT(cx,vp) jsapi_this
  192.  
  193.  
  194.  
  195. /* proprty native calls */
  196. #define JSAPI_PROP(name, cx, obj, vp) \
  197.         jsapi_property_##name(cx, obj, jsval jsapi_id, vp)
  198. #define JSAPI_STRICTPROP JSAPI_PROP
  199.  
  200. /* native property return value */
  201. #define JSAPI_PROP_RVAL JS_RVAL
  202.  
  203. /* native property return value setter */
  204. #define JSAPI_PROP_SET_RVAL JS_SET_RVAL
  205.  
  206. /* native property ID value as a jsval */
  207. #define JSAPI_PROP_IDVAL(cx, vp) (*(vp) = jsapi_id)
  208.  
  209. /* property specifier */
  210. #define JSAPI_PS(name, fnname, tinyid, flags)                           \
  211.         { name , tinyid , flags , jsapi_property_##fnname##_get , jsapi_property_##fnname##_set }
  212.  
  213. #define JSAPI_PS_RO(name, fnname, tinyid, flags)                                \
  214.         { name , tinyid , flags | JSPROP_READONLY, jsapi_property_##fnname##_get , NULL }
  215.  
  216. #define JSAPI_PS_END { NULL, 0, 0, NULL, NULL }
  217.  
  218.  
  219.  
  220.  
  221. static inline JSObject *
  222. JS_NewCompartmentAndGlobalObject(JSContext *cx,
  223.                                  JSClass *jsclass,
  224.                                  JSPrincipals *principals)
  225. {
  226.         JSObject *global;
  227.         global = JS_NewObject(cx, jsclass, NULL, NULL);
  228.         if (global == NULL) {
  229.                 return NULL;
  230.         }
  231.         return global;
  232. }
  233.  
  234. #define JS_StrictPropertyStub JS_PropertyStub
  235.  
  236. #define JSString_to_char(injsstring, outchar, outlen)   \
  237.         outchar = JS_GetStringBytes(injsstring);                \
  238.         outlen = strlen(outchar)
  239.  
  240. /* string type cast */
  241. #define JSAPI_STRING_TO_JSVAL(str) ((str == NULL)?JSVAL_NULL:STRING_TO_JSVAL(str))
  242.  
  243. #define JSAPI_CLASS_NO_INTERNAL_MEMBERS NULL
  244.  
  245. /* Garbage Collector */
  246.  
  247. /* GC marking */
  248. #ifdef JSCLASS_MARK_IS_TRACE
  249. /* mark function pointer requires casting */
  250. #define JSAPI_JSCLASS_MARK_IS_TRACE JSCLASS_MARK_IS_TRACE
  251. #define JSAPI_JSCLASS_MARKOP(x) ((JSMarkOp)x)
  252. #else
  253. /* mark function pointer does not require casting */
  254. #define JSAPI_JSCLASS_MARK_IS_TRACE 0
  255. #define JSAPI_JSCLASS_MARKOP(x) (x)
  256. #endif
  257.  
  258. #define JSAPI_MARKOP(name) JSBool name(JSTracer *trc, JSObject *obj)
  259.  
  260. #define JSAPI_MARKCX trc->context
  261.  
  262. #define JSAPI_GCMARK(thing) JS_CallTracer(trc, thing, JSTRACE_OBJECT);
  263.  
  264. /* Macros for manipulating GC root */
  265. #define JSAPI_ADD_OBJECT_ROOT(cx, obj) JS_AddRoot(cx, obj)
  266. #define JSAPI_REMOVE_OBJECT_ROOT(cx, obj) JS_RemoveRoot(cx, obj)
  267.  
  268. #define JSAPI_ADD_VALUE_ROOT(cx, obj) JS_AddRoot(cx, obj)
  269. #define JSAPI_REMOVE_VALUE_ROOT(cx, obj) JS_RemoveRoot(cx, obj)
  270.  
  271.  
  272. #else /* #if JS_VERSION == 180 */
  273.  
  274. /************************** Spidermonkey 1.8.5 **************************/
  275.  
  276. /* three parameter jsapi native call */
  277. #define JSAPI_FUNC(name, cx, argc, vp) jsapi_func_##name(cx, argc, vp)
  278.  
  279. /* three parameter function descriptor */
  280. #define JSAPI_FS(name, nargs, flags) \
  281.         JS_FS(#name, jsapi_func_##name, nargs, flags)
  282.  
  283. /* function descriptor end */
  284. #define JSAPI_FS_END JS_FS_END
  285.  
  286. /* return value */
  287. #define JSAPI_RVAL JS_RVAL
  288.  
  289. /* return value setter */
  290. #define JSAPI_FUNC_SET_RVAL JS_SET_RVAL
  291.  
  292. /* arguments */
  293. #define JSAPI_FUNC_ARGV(cx, vp) JS_ARGV(cx,vp)
  294.  
  295. /* check if a jsval is an object */
  296. #define JSAPI_JSVAL_IS_OBJECT(v) JSVAL_IS_OBJECT(v)
  297. /* The docuemntation says this is obsolete and should be
  298.  * ((JSVAL_IS_NULL(v)) || (JSVAL_IS_PRIMITIVE(v)))
  299.  * which doesnt work
  300.  */
  301.  
  302.  
  303. /* The object instance in a native call */
  304. #define JSAPI_THIS_OBJECT(cx,vp) JS_THIS_OBJECT(cx,vp)
  305.  
  306. /* proprty native calls */
  307. #define JSAPI_PROP(name, cx, obj, vp) \
  308.         jsapi_property_##name(cx, obj, jsid jsapi_id, vp)
  309. #define JSAPI_STRICTPROP(name, cx, obj, vp) \
  310.         jsapi_property_##name(cx, obj, jsid jsapi_id, JSBool strict, vp)
  311.  
  312. /* native property return value */
  313. #define JSAPI_PROP_RVAL JS_RVAL
  314.  
  315. /* native property getter return value */
  316. #define JSAPI_PROP_SET_RVAL JS_SET_RVAL
  317.  
  318. /* native property ID value as a jsval */
  319. #define JSAPI_PROP_IDVAL(cx, vp) JS_IdToValue(cx, jsapi_id, vp)
  320.  
  321. /* property specifier */
  322. #define JSAPI_PS(name, fnname, tinyid, flags) {                 \
  323.                 name,                                           \
  324.                 tinyid,                                         \
  325.                 flags,                                          \
  326.                 jsapi_property_##fnname##_get,                  \
  327.                 jsapi_property_##fnname##_set                   \
  328.         }
  329.  
  330. #define JSAPI_PS_RO(name, fnname, tinyid, flags) {              \
  331.                 name,                                           \
  332.                 tinyid,                                         \
  333.                 flags | JSPROP_READONLY,                        \
  334.                 jsapi_property_##fnname##_get,                  \
  335.                 NULL                                            \
  336.         }
  337.  
  338. #define JSAPI_PS_END { NULL, 0, 0, NULL, NULL }
  339.  
  340.  
  341. #define JSString_to_char(injsstring, outchar, outlen)           \
  342.         outlen = JS_GetStringLength(injsstring);                \
  343.         outchar = alloca(sizeof(char)*(outlen+1));              \
  344.         JS_EncodeStringToBuffer(injsstring, outchar, outlen);   \
  345.         outchar[outlen] = '\0'
  346.  
  347. /* string type cast */
  348. #define JSAPI_STRING_TO_JSVAL(str) ((str == NULL)?JSVAL_NULL:STRING_TO_JSVAL(str))
  349.  
  350. #define JSAPI_CLASS_NO_INTERNAL_MEMBERS JSCLASS_NO_INTERNAL_MEMBERS
  351.  
  352. /* GC marking */
  353. #ifdef JSCLASS_MARK_IS_TRACE
  354. /* mark requires casting */
  355. #define JSAPI_JSCLASS_MARK_IS_TRACE JSCLASS_MARK_IS_TRACE
  356. #define JSAPI_JSCLASS_MARKOP(x) ((JSMarkOp)x)
  357. #else
  358. /* mark does not require casting */
  359. #define JSAPI_JSCLASS_MARK_IS_TRACE 0
  360. #define JSAPI_JSCLASS_MARKOP(x) (x)
  361. #endif
  362.  
  363. #define JSAPI_MARKOP(name) JSBool name(JSTracer *trc, JSObject *obj)
  364.  
  365. #define JSAPI_MARKCX trc->context
  366.  
  367. #define JSAPI_GCMARK(thing) JS_CallTracer(trc, thing, JSTRACE_OBJECT);
  368.  
  369. /* Macros for manipulating GC root */
  370. #define JSAPI_ADD_OBJECT_ROOT(cx, obj) JS_AddObjectRoot(cx, obj)
  371. #define JSAPI_REMOVE_OBJECT_ROOT(cx, obj) JS_RemoveObjectRoot(cx, obj)
  372.  
  373. #define JSAPI_ADD_VALUE_ROOT(cx, val) JS_AddValueRoot(cx, val)
  374. #define JSAPI_REMOVE_VALUE_ROOT(cx, val) JS_RemoveValueRoot(cx, val)
  375.  
  376. #endif
  377.  
  378. #define JSLOG(args...) LOG((args))
  379. #ifdef ENABLE_VERBOSE_JS_DEBUG
  380. #define JSDBG(args...) LOG((args))
  381. #else
  382. #define JSDBG(args...)
  383. #endif
  384.  
  385. #endif
  386.