PageRenderTime 65ms CodeModel.GetById 30ms RepoModel.GetById 1ms app.codeStats 0ms

/src/spidermonkey/object.cpp

https://github.com/the-kenny/flusspferd
C++ | 391 lines | 305 code | 62 blank | 24 comment | 57 complexity | 3312825658ad375697b4b334241d23bb MD5 | raw file
  1. // vim:ts=2:sw=2:expandtab:autoindent:filetype=cpp:
  2. /*
  3. Copyright (c) 2008, 2009 Aristid Breitkreuz, Ash Berlin, Ruediger Sonderfeld
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in
  11. all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. THE SOFTWARE.
  19. */
  20. #include "flusspferd/object.hpp"
  21. #include "flusspferd/property_iterator.hpp"
  22. #include "flusspferd/function.hpp"
  23. #include "flusspferd/exception.hpp"
  24. #include "flusspferd/local_root_scope.hpp"
  25. #include "flusspferd/root.hpp"
  26. #include "flusspferd/arguments.hpp"
  27. #include "flusspferd/string.hpp"
  28. #include "flusspferd/native_object_base.hpp"
  29. #include "flusspferd/spidermonkey/init.hpp"
  30. #include "flusspferd/spidermonkey/value.hpp"
  31. #include "flusspferd/spidermonkey/object.hpp"
  32. #include <cassert>
  33. #include <js/jsapi.h>
  34. #if JS_VERSION < 180
  35. #include <js/jsobj.h>
  36. #endif
  37. using namespace flusspferd;
  38. object::object() : Impl::object_impl(0) { }
  39. object::~object() { }
  40. void object::seal(bool deep) {
  41. if (is_null())
  42. throw exception("Could not seal object (object is null)");
  43. if (!JS_SealObject(Impl::current_context(), get(), deep))
  44. throw exception("Could not seal object");
  45. }
  46. object object::parent() {
  47. if (is_null())
  48. throw exception("Could not get object parent (object is null)");
  49. return Impl::wrap_object(JS_GetParent(Impl::current_context(), get()));
  50. }
  51. object object::prototype() {
  52. if (is_null())
  53. throw exception("Could not get object prototype (object is null)");
  54. return Impl::wrap_object(JS_GetPrototype(Impl::current_context(), get()));
  55. }
  56. void object::set_parent(object const &o) {
  57. if (is_null())
  58. throw exception("Could not set parent (object is null)");
  59. if (!JS_SetParent(Impl::current_context(), get(), o.get_const()))
  60. throw exception("Could not set object parent");
  61. }
  62. void object::set_prototype(object const &o) {
  63. if (is_null())
  64. throw exception("Could not set object prototype (object is null)");
  65. if (!JS_SetPrototype(Impl::current_context(), get(), o.get_const()))
  66. throw exception("Could not set object prototype");
  67. }
  68. void object::set_property(char const *name, value const &v_) {
  69. if (is_null())
  70. throw exception("Could not set property (object is null)");
  71. value v = v_;
  72. if (!JS_SetProperty(Impl::current_context(), get(), name,
  73. Impl::get_jsvalp(v)))
  74. throw exception("Could not set property");
  75. }
  76. void object::set_property(std::string const &name, value const &v) {
  77. set_property(name.c_str(), v);
  78. }
  79. void object::set_property(value const &id, value const &v_) {
  80. if (is_null())
  81. throw exception("Could not set property (object is null)");
  82. local_root_scope scope;
  83. value v = v_;
  84. string name = id.to_string();
  85. if (!JS_SetUCProperty(Impl::current_context(), get(),
  86. name.data(), name.length(),
  87. Impl::get_jsvalp(v)))
  88. throw exception("Could not set property");
  89. }
  90. value object::get_property(char const *name) const {
  91. if (is_null())
  92. throw exception("Could not get property (object is null)");
  93. value result;
  94. if (!JS_GetProperty(Impl::current_context(), get_const(),
  95. name, Impl::get_jsvalp(result)))
  96. throw exception("Could not get property");
  97. return result;
  98. }
  99. value object::get_property(std::string const &name) const {
  100. return get_property(name.c_str());
  101. }
  102. value object::get_property(value const &id) const {
  103. if (is_null())
  104. throw exception("Could not get property (object is null)");
  105. value result;
  106. local_root_scope scope;
  107. string name = id.to_string();
  108. if (!JS_GetUCProperty(Impl::current_context(), get_const(),
  109. name.data(), name.length(),
  110. Impl::get_jsvalp(result)))
  111. throw exception("Could not get property");
  112. return result;
  113. }
  114. bool object::has_property(char const *name) const {
  115. if (is_null())
  116. throw exception("Could not check property (object is null)");
  117. JSBool foundp;
  118. if (!JS_HasProperty(Impl::current_context(), get_const(), name,
  119. &foundp))
  120. throw exception("Could not check property");
  121. return foundp;
  122. }
  123. bool object::has_property(std::string const &name) const {
  124. return has_property(name.c_str());
  125. }
  126. bool object::has_property(value const &id) const {
  127. if (is_null())
  128. throw exception("Could not check property (object is null)");
  129. local_root_scope scope;
  130. string name = id.to_string();
  131. JSBool foundp;
  132. if (!JS_HasUCProperty(Impl::current_context(), get_const(),
  133. name.data(), name.length(),
  134. &foundp))
  135. throw exception("Could not check property");
  136. return foundp;
  137. }
  138. bool object::has_own_property(char const *name_) const {
  139. local_root_scope scope;
  140. string name(name_);
  141. return has_own_property(name);
  142. }
  143. bool object::has_own_property(std::string const &name_) const {
  144. local_root_scope scope;
  145. string name(name_);
  146. return has_own_property(name);
  147. }
  148. bool object::has_own_property(value const &id) const {
  149. if (is_null())
  150. throw exception("Could not check property (object is null)");
  151. JSBool has;
  152. #if JS_VERSION >= 180
  153. string name = id.to_string();
  154. if (!JS_AlreadyHasOwnUCProperty(Impl::current_context(), get_const(),
  155. name.data(), name.length(), &has))
  156. #else
  157. JSObject *obj = get_const();
  158. jsval argv[] = { Impl::get_jsval(id) };
  159. jsval vp;
  160. JSBool ret = js_HasOwnPropertyHelper(Impl::current_context(), obj,
  161. obj->map->ops->lookupProperty, 1, argv,
  162. &vp);
  163. has = JSVAL_TO_BOOLEAN(vp);
  164. if (!ret)
  165. #endif
  166. {
  167. throw exception("Unable to check for own property");
  168. }
  169. return has;
  170. }
  171. void object::delete_property(char const *name) {
  172. if (is_null())
  173. throw exception("Could not delete property (object is null)");
  174. if (!JS_DeleteProperty(Impl::current_context(), get(), name))
  175. throw exception("Could not delete property");
  176. }
  177. void object::delete_property(std::string const &name) {
  178. delete_property(name.c_str());
  179. }
  180. void object::delete_property(value const &id) {
  181. if (is_null())
  182. throw exception("Could not delete property (object is null)");
  183. local_root_scope scope;
  184. string name = id.to_string();
  185. jsval dummy;
  186. if (!JS_DeleteUCProperty2(Impl::current_context(), get(),
  187. name.data(), name.length(), &dummy))
  188. throw exception("Could not delete property");
  189. }
  190. property_iterator object::begin() const {
  191. if (is_null())
  192. throw exception("Could not create iterator for null object");
  193. return property_iterator(*this);
  194. }
  195. property_iterator object::end() const {
  196. if (is_null())
  197. throw exception("Could not create iterator for null object");
  198. return property_iterator();
  199. }
  200. void object::define_property(
  201. string const &name,
  202. value const &init_value,
  203. property_attributes const attrs)
  204. {
  205. if (is_null())
  206. throw exception("Could not define property (object is null)");
  207. root_string name_r(name);
  208. unsigned flags = attrs.flags;
  209. root_value v;
  210. v = init_value;
  211. function getter;
  212. if (attrs.getter) getter = attrs.getter.get();
  213. function setter;
  214. if (attrs.setter) setter = attrs.setter.get();
  215. JSObject *getter_o = Impl::get_object(getter);
  216. JSObject *setter_o = Impl::get_object(setter);
  217. unsigned sm_flags = 0;
  218. if (~flags & dont_enumerate) sm_flags |= JSPROP_ENUMERATE;
  219. if (flags & read_only_property) sm_flags |= JSPROP_READONLY;
  220. if (flags & permanent_property) sm_flags |= JSPROP_PERMANENT;
  221. if (flags & shared_property) sm_flags |= JSPROP_SHARED;
  222. if (getter_o) sm_flags |= JSPROP_GETTER;
  223. if (setter_o) sm_flags |= JSPROP_SETTER;
  224. if (!JS_DefineUCProperty(Impl::current_context(),
  225. get_const(),
  226. name.data(), name.length(),
  227. Impl::get_jsval(v),
  228. *(JSPropertyOp*) &getter_o,
  229. *(JSPropertyOp*) &setter_o,
  230. sm_flags))
  231. throw exception("Could not define property");
  232. }
  233. bool object::is_null() const {
  234. return !get_const();
  235. }
  236. value object::apply(object const &fn, arguments const &arg_) {
  237. if (is_null())
  238. throw exception("Could not apply function (object is null)");
  239. value fnv(fn);
  240. root_value result((value()));
  241. arguments arg(arg_);
  242. JSBool status = JS_CallFunctionValue(
  243. Impl::current_context(),
  244. get(),
  245. Impl::get_jsval(fnv),
  246. arg.size(),
  247. Impl::get_arguments(arg),
  248. Impl::get_jsvalp(result));
  249. if (!status)
  250. throw exception("Could not call function");
  251. return result;
  252. }
  253. value object::call(char const *fn, arguments const &arg_) {
  254. if (is_null())
  255. throw exception("Could not call function (object is null)");
  256. root_value result((value()));
  257. arguments arg(arg_);
  258. JSBool status = JS_CallFunctionName(
  259. Impl::current_context(),
  260. get(),
  261. fn,
  262. arg.size(),
  263. Impl::get_arguments(arg),
  264. Impl::get_jsvalp(result));
  265. if (!status)
  266. throw exception("Could not call function");
  267. return result;
  268. }
  269. value object::call(std::string const &fn, arguments const &arg) {
  270. return call(fn.c_str(), arg);
  271. }
  272. value object::call(object o, arguments const &arg) {
  273. return o.apply(*this, arg);
  274. }
  275. value object::call(arguments const &arg) {
  276. return call(global(), arg);
  277. }
  278. bool object::is_array() const {
  279. if (is_null())
  280. return false;
  281. return JS_IsArrayObject(Impl::current_context(), get_const());
  282. }
  283. bool object::get_property_attributes(
  284. string const &name, property_attributes &attrs)
  285. {
  286. if (is_null())
  287. throw exception("Could not get property attributes (object is null)");
  288. flusspferd::root_string name_r(name);
  289. JSBool found;
  290. void *getter_op, *setter_op;
  291. uintN sm_flags;
  292. attrs.flags = no_property_flag;
  293. attrs.getter = boost::none;
  294. attrs.setter = boost::none;
  295. JSBool success = JS_GetUCPropertyAttrsGetterAndSetter(
  296. Impl::current_context(), get_const(), name.data(), name.length(),
  297. &sm_flags, &found,
  298. (JSPropertyOp*)&getter_op, (JSPropertyOp*)&setter_op);
  299. if (!success)
  300. throw exception("Could not query property attributes");
  301. if (!found)
  302. return false;
  303. if (~sm_flags & JSPROP_ENUMERATE)
  304. attrs.flags = attrs.flags | dont_enumerate;
  305. if (sm_flags & JSPROP_PERMANENT)
  306. attrs.flags = attrs.flags | permanent_property;
  307. if (sm_flags & JSPROP_READONLY)
  308. attrs.flags = attrs.flags | read_only_property;
  309. if (sm_flags & JSPROP_SHARED)
  310. attrs.flags = attrs.flags | shared_property;
  311. if (getter_op) {
  312. if (sm_flags & JSPROP_GETTER) {
  313. attrs.getter = Impl::wrap_object((JSObject*)getter_op);
  314. } else {
  315. // What do i set attrs.getter to here....?
  316. }
  317. }
  318. if (setter_op) {
  319. if (sm_flags & JSPROP_SETTER) {
  320. attrs.setter = Impl::wrap_object((JSObject*)setter_op);
  321. } else {
  322. // What do i set attrs.setter to here....?
  323. }
  324. }
  325. return true;
  326. }