PageRenderTime 26ms CodeModel.GetById 28ms RepoModel.GetById 1ms app.codeStats 0ms

/libflusspferd/spidermonkey/native_object_base.cpp

http://github.com/Flusspferd/flusspferd
C++ | 384 lines | 291 code | 68 blank | 25 comment | 31 complexity | 01a7f521d7edead791e43165fa49ccf0 MD5 | raw file
Possible License(s): MIT
  1. // vim:ts=2:sw=2:expandtab:autoindent:filetype=cpp:
  2. /*
  3. The MIT License
  4. Copyright (c) 2008, 2009 Flusspferd contributors (see "CONTRIBUTORS" or
  5. http://flusspferd.org/contributors.txt)
  6. Permission is hereby granted, free of charge, to any person obtaining a copy
  7. of this software and associated documentation files (the "Software"), to deal
  8. in the Software without restriction, including without limitation the rights
  9. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. copies of the Software, and to permit persons to whom the Software is
  11. furnished to do so, subject to the following conditions:
  12. The above copyright notice and this permission notice shall be included in
  13. all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. THE SOFTWARE.
  21. */
  22. #include "flusspferd/native_object_base.hpp"
  23. #include "flusspferd/tracer.hpp"
  24. #include "flusspferd/object.hpp"
  25. #include "flusspferd/string.hpp"
  26. #include "flusspferd/call_context.hpp"
  27. #include "flusspferd/root.hpp"
  28. #include "flusspferd/exception.hpp"
  29. #include "flusspferd/current_context_scope.hpp"
  30. #include "flusspferd/spidermonkey/init.hpp"
  31. #include "flusspferd/spidermonkey/jsid.hpp"
  32. #include <boost/unordered_map.hpp>
  33. #include <boost/variant.hpp>
  34. using namespace flusspferd;
  35. class native_object_base::impl {
  36. public:
  37. static void finalize(JSContext *ctx, JSObject *obj);
  38. static JSBool call_helper(JSContext *, JSObject *, uintN, jsval *, jsval *);
  39. static void trace_op(JSTracer *trc, JSObject *obj);
  40. template<property_mode>
  41. #if defined(JSID_VOID) || defined(JS_USE_JSVAL_JSID_STRUCT_TYPES) // TODO add better check for new jsid/jsvalue API
  42. static JSBool property_op(JSContext *, JSObject *, jsid, jsval *);
  43. #else
  44. static JSBool property_op(JSContext *, JSObject *, jsval, jsval *);
  45. #endif
  46. static JSBool new_resolve(JSContext *, JSObject *, jsval, uintN, JSObject **);
  47. static JSBool new_enumerate(JSContext *cx, JSObject *obj,
  48. JSIterateOp enum_op, jsval *statep, jsid *idp);
  49. public:
  50. static JSClass native_object_class;
  51. static JSClass native_enumerable_object_class;
  52. };
  53. static const unsigned int basic_flags =
  54. JSCLASS_HAS_PRIVATE
  55. | JSCLASS_NEW_RESOLVE
  56. | JSCLASS_MARK_IS_TRACE
  57. ;
  58. #define MARK_TRACE_OP ((JSMarkOp) &native_object_base::impl::trace_op)
  59. JSClass native_object_base::impl::native_object_class = {
  60. "NativeObject",
  61. basic_flags,
  62. &native_object_base::impl::property_op<native_object_base::property_add>,
  63. &native_object_base::impl::property_op<native_object_base::property_delete>,
  64. &native_object_base::impl::property_op<native_object_base::property_get>,
  65. &native_object_base::impl::property_op<native_object_base::property_set>,
  66. JS_EnumerateStub,
  67. (JSResolveOp) &native_object_base::impl::new_resolve,
  68. JS_ConvertStub,
  69. &native_object_base::impl::finalize,
  70. 0,
  71. 0,
  72. &native_object_base::impl::call_helper,
  73. 0,
  74. 0,
  75. 0,
  76. MARK_TRACE_OP,
  77. 0
  78. };
  79. JSClass native_object_base::impl::native_enumerable_object_class = {
  80. "NativeObject",
  81. basic_flags | JSCLASS_NEW_ENUMERATE,
  82. &native_object_base::impl::property_op<native_object_base::property_add>,
  83. &native_object_base::impl::property_op<native_object_base::property_delete>,
  84. &native_object_base::impl::property_op<native_object_base::property_get>,
  85. &native_object_base::impl::property_op<native_object_base::property_set>,
  86. (JSEnumerateOp) &native_object_base::impl::new_enumerate,
  87. (JSResolveOp) &native_object_base::impl::new_resolve,
  88. JS_ConvertStub,
  89. &native_object_base::impl::finalize,
  90. 0,
  91. 0,
  92. &native_object_base::impl::call_helper,
  93. 0,
  94. 0,
  95. 0,
  96. MARK_TRACE_OP,
  97. 0
  98. };
  99. native_object_base::native_object_base(object const &o) : p(new impl) {
  100. load_into(o);
  101. }
  102. native_object_base::~native_object_base() {
  103. if (!is_null()) {
  104. JS_SetPrivate(Impl::current_context(), get(), 0);
  105. }
  106. }
  107. void native_object_base::load_into(object const &o) {
  108. if (!is_null())
  109. throw exception("Cannot load native_object data into more than one object");
  110. object::operator=(o);
  111. if (!is_null()) {
  112. if (!JS_SetPrivate(Impl::current_context(), Impl::get_object(o), this))
  113. throw exception("Could not create native object (private data)");
  114. }
  115. }
  116. bool native_object_base::is_object_native(object const &o_) {
  117. object o = o_;
  118. if (o.is_null())
  119. return false;
  120. JSContext *ctx = Impl::current_context();
  121. JSObject *jso = Impl::get_object(o);
  122. JSClass *classp = JS_GET_CLASS(ctx, jso);
  123. if (!classp || classp->finalize != &native_object_base::impl::finalize)
  124. return false;
  125. void *priv = JS_GetPrivate(ctx, jso);
  126. if (!priv)
  127. return false;
  128. return true;
  129. }
  130. native_object_base &native_object_base::get_native(object const &o_) {
  131. object o = o_;
  132. if (o.is_null())
  133. throw exception("Can not interpret 'null' as native object");
  134. JSContext *ctx = Impl::current_context();
  135. JSObject *jso = Impl::get_object(o);
  136. JSClass *classp = JS_GET_CLASS(ctx, jso);
  137. if (!classp || classp->finalize != &native_object_base::impl::finalize)
  138. throw exception("Object is not native");
  139. void *priv = JS_GetPrivate(ctx, jso);
  140. if (!priv)
  141. throw exception("Object is not native");
  142. return *static_cast<native_object_base*>(priv);
  143. }
  144. object native_object_base::do_create_object(
  145. object const &prototype_, object const &parent)
  146. {
  147. JSContext *ctx = Impl::current_context();
  148. object prototype = prototype_;
  149. JSObject *o = JS_NewObject(
  150. ctx,
  151. &impl::native_object_class,
  152. Impl::get_object(prototype),
  153. Impl::get_object(parent));
  154. if (!o)
  155. throw exception("Could not create native object");
  156. return Impl::wrap_object(o);
  157. }
  158. object native_object_base::do_create_enumerable_object(
  159. object const &prototype_, object const &parent)
  160. {
  161. JSContext *ctx = Impl::current_context();
  162. object prototype = prototype_;
  163. JSObject *o = JS_NewObject(
  164. ctx,
  165. &impl::native_enumerable_object_class,
  166. Impl::get_object(prototype),
  167. Impl::get_object(parent));
  168. if (!o)
  169. throw exception("Could not create native object");
  170. return Impl::wrap_object(o);
  171. }
  172. void native_object_base::impl::finalize(JSContext *ctx, JSObject *obj) {
  173. void *p = JS_GetPrivate(ctx, obj);
  174. if (p) {
  175. current_context_scope scope(Impl::wrap_context(ctx));
  176. delete static_cast<native_object_base*>(p);
  177. }
  178. }
  179. JSBool native_object_base::impl::call_helper(
  180. JSContext *ctx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
  181. {
  182. FLUSSPFERD_CALLBACK_BEGIN {
  183. current_context_scope scope(Impl::wrap_context(ctx));
  184. JSObject *function = JSVAL_TO_OBJECT(argv[-2]);
  185. native_object_base *self = 0;
  186. try {
  187. self = &native_object_base::get_native(Impl::wrap_object(obj));
  188. } catch (exception &) {
  189. self = &native_object_base::get_native(Impl::wrap_object(function));
  190. }
  191. call_context x;
  192. x.self = Impl::wrap_object(obj);
  193. x.self_native = self;
  194. x.arg = Impl::arguments_impl(argc, argv);
  195. x.result.bind(Impl::wrap_jsvalp(rval));
  196. x.function = Impl::wrap_object(function);
  197. self->self_call(x);
  198. } FLUSSPFERD_CALLBACK_END;
  199. }
  200. #if defined(JSID_VOID) || defined(JS_USE_JSVAL_JSID_STRUCT_TYPES) // TODO add better check for new jsid/jsvalue API
  201. template<native_object_base::property_mode mode>
  202. JSBool native_object_base::impl::property_op(
  203. JSContext *ctx, JSObject *obj, jsid id, jsval *vp)
  204. #else
  205. template<native_object_base::property_mode mode>
  206. JSBool native_object_base::impl::property_op(
  207. JSContext *ctx, JSObject *obj, jsval id, jsval *vp)
  208. #endif
  209. {
  210. FLUSSPFERD_CALLBACK_BEGIN {
  211. current_context_scope scope(Impl::wrap_context(ctx));
  212. native_object_base &self =
  213. native_object_base::get_native(Impl::wrap_object(obj));
  214. value data(Impl::wrap_jsvalp(vp));
  215. self.property_op(mode, Impl::wrap_jsid(id), data);
  216. } FLUSSPFERD_CALLBACK_END;
  217. }
  218. JSBool native_object_base::impl::new_resolve(
  219. JSContext *ctx, JSObject *obj, jsval id, uintN sm_flags, JSObject **objp)
  220. {
  221. FLUSSPFERD_CALLBACK_BEGIN {
  222. current_context_scope scope(Impl::wrap_context(ctx));
  223. native_object_base &self =
  224. native_object_base::get_native(Impl::wrap_object(obj));
  225. unsigned flags = 0;
  226. if (sm_flags & JSRESOLVE_QUALIFIED)
  227. flags |= property_qualified;
  228. if (sm_flags & JSRESOLVE_ASSIGNING)
  229. flags |= property_assigning;
  230. if (sm_flags & JSRESOLVE_DETECTING)
  231. flags |= property_detecting;
  232. if (sm_flags & JSRESOLVE_DECLARING)
  233. flags |= property_declaring;
  234. if (sm_flags & JSRESOLVE_CLASSNAME)
  235. flags |= property_classname;
  236. *objp = 0;
  237. if (self.property_resolve(Impl::wrap_jsval(id), flags))
  238. *objp = Impl::get_object(self);
  239. } FLUSSPFERD_CALLBACK_END;
  240. }
  241. JSBool native_object_base::impl::new_enumerate(
  242. JSContext *ctx, JSObject *obj, JSIterateOp enum_op, jsval *statep, jsid *idp)
  243. {
  244. FLUSSPFERD_CALLBACK_BEGIN {
  245. current_context_scope scope(Impl::wrap_context(ctx));
  246. native_object_base &self =
  247. native_object_base::get_native(Impl::wrap_object(obj));
  248. boost::any *iter;
  249. switch (enum_op) {
  250. case JSENUMERATE_INIT:
  251. {
  252. iter = new boost::any;
  253. int num = 0;
  254. *iter = self.enumerate_start(num);
  255. *statep = PRIVATE_TO_JSVAL(iter);
  256. if (idp) {
  257. #if defined(JSID_VOID) || defined(JS_USE_JSVAL_JSID_STRUCT_TYPES) // TODO add better check for new jsid/jsvalue API
  258. *idp = INT_TO_JSID(num);
  259. #else
  260. *idp = INT_TO_JSVAL(num);
  261. #endif
  262. }
  263. return JS_TRUE;
  264. }
  265. case JSENUMERATE_NEXT:
  266. {
  267. iter = (boost::any*)JSVAL_TO_PRIVATE(*statep);
  268. value id;
  269. if (iter->empty() || (id = self.enumerate_next(*iter)).is_undefined())
  270. *statep = JSVAL_NULL;
  271. else {
  272. JS_ValueToId(ctx, Impl::get_jsval(id), idp);
  273. }
  274. return JS_TRUE;
  275. }
  276. case JSENUMERATE_DESTROY:
  277. {
  278. iter = (boost::any*)JSVAL_TO_PRIVATE(*statep);
  279. delete iter;
  280. return JS_TRUE;
  281. }
  282. }
  283. } FLUSSPFERD_CALLBACK_END;
  284. }
  285. void native_object_base::impl::trace_op(
  286. JSTracer *trc, JSObject *obj)
  287. {
  288. current_context_scope scope(Impl::wrap_context(trc->context));
  289. native_object_base &self =
  290. native_object_base::get_native(Impl::wrap_object(obj));
  291. tracer tracer_(trc);
  292. self.trace(tracer_);
  293. }
  294. void native_object_base::property_op(
  295. property_mode, value const &, value &)
  296. {
  297. }
  298. bool native_object_base::property_resolve(value const &, unsigned) {
  299. return false;
  300. }
  301. boost::any native_object_base::enumerate_start(int &)
  302. {
  303. return boost::any();
  304. }
  305. value native_object_base::enumerate_next(boost::any &)
  306. {
  307. return value();
  308. }
  309. void native_object_base::self_call(call_context &) {
  310. throw exception("Object can not be called");
  311. }
  312. void native_object_base::trace(tracer&) {}