CallArgs.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  2. * vim: set ts=8 sts=4 et sw=4 tw=99:
  3. * This Source Code Form is subject to the terms of the Mozilla Public
  4. * License, v. 2.0. If a copy of the MPL was not distributed with this
  5. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  6. /*
  7. * Helper classes encapsulating access to the callee, |this| value, arguments,
  8. * and argument count for a call/construct operation.
  9. *
  10. * JS::CallArgs encapsulates access to a JSNative's un-abstracted
  11. * |unsigned argc, Value* vp| arguments. The principal way to create a
  12. * JS::CallArgs is using JS::CallArgsFromVp:
  13. *
  14. * // If provided no arguments or a non-numeric first argument, return zero.
  15. * // Otherwise return |this| exactly as given, without boxing.
  16. * static bool
  17. * Func(JSContext* cx, unsigned argc, JS::Value* vp)
  18. * {
  19. * JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
  20. *
  21. * // Guard against no arguments or a non-numeric arg0.
  22. * if (args.length() == 0 || !args[0].isNumber()) {
  23. * args.rval().setInt32(0);
  24. * return true;
  25. * }
  26. *
  27. * // Access to the callee must occur before accessing/setting
  28. * // the return value.
  29. * JSObject& callee = args.callee();
  30. * args.rval().setObject(callee);
  31. *
  32. * // callee() and calleev() will now assert.
  33. *
  34. * // It's always fine to access thisv().
  35. * HandleValue thisv = args.thisv();
  36. * args.rval().set(thisv);
  37. *
  38. * // As the return value was last set to |this|, returns |this|.
  39. * return true;
  40. * }
  41. *
  42. * CallArgs is exposed publicly and used internally. Not all parts of its
  43. * public interface are meant to be used by embedders! See inline comments to
  44. * for details.
  45. *
  46. * It's possible (albeit deprecated) to manually index into |vp| to access the
  47. * callee, |this|, and arguments of a function, and to set its return value.
  48. * It's also possible to use the supported API of JS_CALLEE, JS_THIS, JS_ARGV,
  49. * JS_RVAL, and JS_SET_RVAL to the same ends.
  50. *
  51. * But neither API has the error-handling or moving-GC correctness of CallArgs.
  52. * New code should use CallArgs instead whenever possible.
  53. *
  54. * The eventual plan is to change JSNative to take |const CallArgs&| directly,
  55. * for automatic assertion of correct use and to make calling functions more
  56. * efficient. Embedders should start internally switching away from using
  57. * |argc| and |vp| directly, except to create a |CallArgs|. Then, when an
  58. * eventual release making that change occurs, porting efforts will require
  59. * changing methods' signatures but won't require invasive changes to the
  60. * methods' implementations, potentially under time pressure.
  61. */
  62. #ifndef js_CallArgs_h
  63. #define js_CallArgs_h
  64. #include "mozilla/Assertions.h"
  65. #include "mozilla/Attributes.h"
  66. #include "mozilla/TypeTraits.h"
  67. #include "jstypes.h"
  68. #include "js/RootingAPI.h"
  69. #include "js/Value.h"
  70. /* Typedef for native functions called by the JS VM. */
  71. typedef bool
  72. (* JSNative)(JSContext* cx, unsigned argc, JS::Value* vp);
  73. namespace JS {
  74. extern JS_PUBLIC_DATA(const HandleValue) UndefinedHandleValue;
  75. namespace detail {
  76. /*
  77. * Compute |this| for the |vp| inside a JSNative, either boxing primitives or
  78. * replacing with the global object as necessary.
  79. */
  80. extern JS_PUBLIC_API(Value)
  81. ComputeThis(JSContext* cx, JS::Value* vp);
  82. #ifdef JS_DEBUG
  83. extern JS_PUBLIC_API(void)
  84. CheckIsValidConstructible(const Value& v);
  85. #endif
  86. class MOZ_STACK_CLASS IncludeUsedRval
  87. {
  88. protected:
  89. #ifdef JS_DEBUG
  90. mutable bool usedRval_;
  91. void setUsedRval() const { usedRval_ = true; }
  92. void clearUsedRval() const { usedRval_ = false; }
  93. void assertUnusedRval() const { MOZ_ASSERT(!usedRval_); }
  94. #else
  95. void setUsedRval() const {}
  96. void clearUsedRval() const {}
  97. void assertUnusedRval() const {}
  98. #endif
  99. };
  100. class MOZ_STACK_CLASS NoUsedRval
  101. {
  102. protected:
  103. void setUsedRval() const {}
  104. void clearUsedRval() const {}
  105. void assertUnusedRval() const {}
  106. };
  107. template<class WantUsedRval>
  108. class MOZ_STACK_CLASS CallArgsBase : public WantUsedRval
  109. {
  110. static_assert(mozilla::IsSame<WantUsedRval, IncludeUsedRval>::value ||
  111. mozilla::IsSame<WantUsedRval, NoUsedRval>::value,
  112. "WantUsedRval can only be IncludeUsedRval or NoUsedRval");
  113. protected:
  114. Value* argv_;
  115. unsigned argc_;
  116. bool constructing_;
  117. public:
  118. // CALLEE ACCESS
  119. /*
  120. * Returns the function being called, as a value. Must not be called after
  121. * rval() has been used!
  122. */
  123. HandleValue calleev() const {
  124. this->assertUnusedRval();
  125. return HandleValue::fromMarkedLocation(&argv_[-2]);
  126. }
  127. /*
  128. * Returns the function being called, as an object. Must not be called
  129. * after rval() has been used!
  130. */
  131. JSObject& callee() const {
  132. return calleev().toObject();
  133. }
  134. // CALLING/CONSTRUCTING-DIFFERENTIATIONS
  135. bool isConstructing() const {
  136. if (!argv_[-1].isMagic())
  137. return false;
  138. #ifdef JS_DEBUG
  139. if (!this->usedRval_)
  140. CheckIsValidConstructible(calleev());
  141. #endif
  142. return true;
  143. }
  144. MutableHandleValue newTarget() const {
  145. MOZ_ASSERT(constructing_);
  146. return MutableHandleValue::fromMarkedLocation(&this->argv_[argc_]);
  147. }
  148. /*
  149. * Returns the |this| value passed to the function. This method must not
  150. * be called when the function is being called as a constructor via |new|.
  151. * The value may or may not be an object: it is the individual function's
  152. * responsibility to box the value if needed.
  153. */
  154. HandleValue thisv() const {
  155. // Some internal code uses thisv() in constructing cases, so don't do
  156. // this yet.
  157. // MOZ_ASSERT(!argv_[-1].isMagic(JS_IS_CONSTRUCTING));
  158. return HandleValue::fromMarkedLocation(&argv_[-1]);
  159. }
  160. Value computeThis(JSContext* cx) const {
  161. if (thisv().isObject())
  162. return thisv();
  163. return ComputeThis(cx, base());
  164. }
  165. // ARGUMENTS
  166. /* Returns the number of arguments. */
  167. unsigned length() const { return argc_; }
  168. /* Returns the i-th zero-indexed argument. */
  169. MutableHandleValue operator[](unsigned i) const {
  170. MOZ_ASSERT(i < argc_);
  171. return MutableHandleValue::fromMarkedLocation(&this->argv_[i]);
  172. }
  173. /*
  174. * Returns the i-th zero-indexed argument, or |undefined| if there's no
  175. * such argument.
  176. */
  177. HandleValue get(unsigned i) const {
  178. return i < length()
  179. ? HandleValue::fromMarkedLocation(&this->argv_[i])
  180. : UndefinedHandleValue;
  181. }
  182. /*
  183. * Returns true if the i-th zero-indexed argument is present and is not
  184. * |undefined|.
  185. */
  186. bool hasDefined(unsigned i) const {
  187. return i < argc_ && !this->argv_[i].isUndefined();
  188. }
  189. // RETURN VALUE
  190. /*
  191. * Returns the currently-set return value. The initial contents of this
  192. * value are unspecified. Once this method has been called, callee() and
  193. * calleev() can no longer be used. (If you're compiling against a debug
  194. * build of SpiderMonkey, these methods will assert to aid debugging.)
  195. *
  196. * If the method you're implementing succeeds by returning true, you *must*
  197. * set this. (SpiderMonkey doesn't currently assert this, but it will do
  198. * so eventually.) You don't need to use or change this if your method
  199. * fails.
  200. */
  201. MutableHandleValue rval() const {
  202. this->setUsedRval();
  203. return MutableHandleValue::fromMarkedLocation(&argv_[-2]);
  204. }
  205. public:
  206. // These methods are publicly exposed, but they are *not* to be used when
  207. // implementing a JSNative method and encapsulating access to |vp| within
  208. // it. You probably don't want to use these!
  209. void setCallee(const Value& aCalleev) const {
  210. this->clearUsedRval();
  211. argv_[-2] = aCalleev;
  212. }
  213. void setThis(const Value& aThisv) const {
  214. argv_[-1] = aThisv;
  215. }
  216. MutableHandleValue mutableThisv() const {
  217. return MutableHandleValue::fromMarkedLocation(&argv_[-1]);
  218. }
  219. public:
  220. // These methods are publicly exposed, but we're unsure of the interfaces
  221. // (because they're hackish and drop assertions). Avoid using these if you
  222. // can.
  223. Value* array() const { return argv_; }
  224. Value* end() const { return argv_ + argc_ + constructing_; }
  225. public:
  226. // These methods are only intended for internal use. Embedders shouldn't
  227. // use them!
  228. Value* base() const { return argv_ - 2; }
  229. Value* spAfterCall() const {
  230. this->setUsedRval();
  231. return argv_ - 1;
  232. }
  233. };
  234. } // namespace detail
  235. class MOZ_STACK_CLASS CallArgs : public detail::CallArgsBase<detail::IncludeUsedRval>
  236. {
  237. private:
  238. friend CallArgs CallArgsFromVp(unsigned argc, Value* vp);
  239. friend CallArgs CallArgsFromSp(unsigned stackSlots, Value* sp, bool constructing);
  240. static CallArgs create(unsigned argc, Value* argv, bool constructing) {
  241. CallArgs args;
  242. args.clearUsedRval();
  243. args.argv_ = argv;
  244. args.argc_ = argc;
  245. args.constructing_ = constructing;
  246. #ifdef DEBUG
  247. for (unsigned i = 0; i < argc; ++i)
  248. MOZ_ASSERT_IF(argv[i].isMarkable(), !GCThingIsMarkedGray(GCCellPtr(argv[i])));
  249. #endif
  250. return args;
  251. }
  252. public:
  253. /*
  254. * Returns true if there are at least |required| arguments passed in. If
  255. * false, it reports an error message on the context.
  256. */
  257. bool requireAtLeast(JSContext* cx, const char* fnname, unsigned required) const;
  258. };
  259. MOZ_ALWAYS_INLINE CallArgs
  260. CallArgsFromVp(unsigned argc, Value* vp)
  261. {
  262. return CallArgs::create(argc, vp + 2, vp[1].isMagic(JS_IS_CONSTRUCTING));
  263. }
  264. // This method is only intended for internal use in SpiderMonkey. We may
  265. // eventually move it to an internal header. Embedders should use
  266. // JS::CallArgsFromVp!
  267. MOZ_ALWAYS_INLINE CallArgs
  268. CallArgsFromSp(unsigned stackSlots, Value* sp, bool constructing = false)
  269. {
  270. return CallArgs::create(stackSlots - constructing, sp - stackSlots, constructing);
  271. }
  272. } // namespace JS
  273. /*
  274. * Macros to hide interpreter stack layout details from a JSNative using its
  275. * JS::Value* vp parameter. DO NOT USE THESE! Instead use JS::CallArgs and
  276. * friends, above. These macros will be removed when we change JSNative to
  277. * take a const JS::CallArgs&.
  278. */
  279. /*
  280. * Return |this| if |this| is an object. Otherwise, return the global object
  281. * if |this| is null or undefined, and finally return a boxed version of any
  282. * other primitive.
  283. *
  284. * Note: if this method returns null, an error has occurred and must be
  285. * propagated or caught.
  286. */
  287. MOZ_ALWAYS_INLINE JS::Value
  288. JS_THIS(JSContext* cx, JS::Value* vp)
  289. {
  290. return vp[1].isPrimitive() ? JS::detail::ComputeThis(cx, vp) : vp[1];
  291. }
  292. /*
  293. * A note on JS_THIS_OBJECT: no equivalent method is part of the CallArgs
  294. * interface, and we're unlikely to add one (functions shouldn't be implicitly
  295. * exposing the global object to arbitrary callers). Continue using |vp|
  296. * directly for this case, but be aware this API will eventually be replaced
  297. * with a function that operates directly upon |args.thisv()|.
  298. */
  299. #define JS_THIS_OBJECT(cx,vp) (JS_THIS(cx,vp).toObjectOrNull())
  300. /*
  301. * |this| is passed to functions in ES5 without change. Functions themselves
  302. * do any post-processing they desire to box |this|, compute the global object,
  303. * &c. This macro retrieves a function's unboxed |this| value.
  304. *
  305. * This macro must not be used in conjunction with JS_THIS or JS_THIS_OBJECT,
  306. * or vice versa. Either use the provided this value with this macro, or
  307. * compute the boxed |this| value using those. JS_THIS_VALUE must not be used
  308. * if the function is being called as a constructor.
  309. *
  310. * But: DO NOT USE THIS! Instead use JS::CallArgs::thisv(), above.
  311. *
  312. */
  313. #define JS_THIS_VALUE(cx,vp) ((vp)[1])
  314. #endif /* js_CallArgs_h */