CallNonGenericMethod.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  2. * This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #ifndef js_CallNonGenericMethod_h
  6. #define js_CallNonGenericMethod_h
  7. #include "jstypes.h"
  8. #include "js/CallArgs.h"
  9. namespace JS {
  10. // Returns true if |v| is considered an acceptable this-value.
  11. typedef bool (*IsAcceptableThis)(HandleValue v);
  12. // Implements the guts of a method; guaranteed to be provided an acceptable
  13. // this-value, as determined by a corresponding IsAcceptableThis method.
  14. typedef bool (*NativeImpl)(JSContext* cx, const CallArgs& args);
  15. namespace detail {
  16. // DON'T CALL THIS DIRECTLY. It's for use only by CallNonGenericMethod!
  17. extern JS_PUBLIC_API(bool)
  18. CallMethodIfWrapped(JSContext* cx, IsAcceptableThis test, NativeImpl impl, const CallArgs& args);
  19. } // namespace detail
  20. // Methods usually act upon |this| objects only from a single global object and
  21. // compartment. Sometimes, however, a method must act upon |this| values from
  22. // multiple global objects or compartments. In such cases the |this| value a
  23. // method might see will be wrapped, such that various access to the object --
  24. // to its class, its private data, its reserved slots, and so on -- will not
  25. // work properly without entering that object's compartment. This method
  26. // implements a solution to this problem.
  27. //
  28. // To implement a method that accepts |this| values from multiple compartments,
  29. // define two functions. The first function matches the IsAcceptableThis type
  30. // and indicates whether the provided value is an acceptable |this| for the
  31. // method; it must be a pure function only of its argument.
  32. //
  33. // static const JSClass AnswerClass = { ... };
  34. //
  35. // static bool
  36. // IsAnswerObject(const Value& v)
  37. // {
  38. // if (!v.isObject())
  39. // return false;
  40. // return JS_GetClass(&v.toObject()) == &AnswerClass;
  41. // }
  42. //
  43. // The second function implements the NativeImpl signature and defines the
  44. // behavior of the method when it is provided an acceptable |this| value.
  45. // Aside from some typing niceties -- see the CallArgs interface for details --
  46. // its interface is the same as that of JSNative.
  47. //
  48. // static bool
  49. // answer_getAnswer_impl(JSContext* cx, JS::CallArgs args)
  50. // {
  51. // args.rval().setInt32(42);
  52. // return true;
  53. // }
  54. //
  55. // The implementation function is guaranteed to be called *only* with a |this|
  56. // value which is considered acceptable.
  57. //
  58. // Now to implement the actual method, write a JSNative that calls the method
  59. // declared below, passing the appropriate template and runtime arguments.
  60. //
  61. // static bool
  62. // answer_getAnswer(JSContext* cx, unsigned argc, JS::Value* vp)
  63. // {
  64. // JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
  65. // return JS::CallNonGenericMethod<IsAnswerObject, answer_getAnswer_impl>(cx, args);
  66. // }
  67. //
  68. // Note that, because they are used as template arguments, the predicate
  69. // and implementation functions must have external linkage. (This is
  70. // unfortunate, but GCC wasn't inlining things as one would hope when we
  71. // passed them as function arguments.)
  72. //
  73. // JS::CallNonGenericMethod will test whether |args.thisv()| is acceptable. If
  74. // it is, it will call the provided implementation function, which will return
  75. // a value and indicate success. If it is not, it will attempt to unwrap
  76. // |this| and call the implementation function on the unwrapped |this|. If
  77. // that succeeds, all well and good. If it doesn't succeed, a TypeError will
  78. // be thrown.
  79. //
  80. // Note: JS::CallNonGenericMethod will only work correctly if it's called in
  81. // tail position in a JSNative. Do not call it from any other place.
  82. //
  83. template<IsAcceptableThis Test, NativeImpl Impl>
  84. MOZ_ALWAYS_INLINE bool
  85. CallNonGenericMethod(JSContext* cx, const CallArgs& args)
  86. {
  87. HandleValue thisv = args.thisv();
  88. if (Test(thisv))
  89. return Impl(cx, args);
  90. return detail::CallMethodIfWrapped(cx, Test, Impl, args);
  91. }
  92. MOZ_ALWAYS_INLINE bool
  93. CallNonGenericMethod(JSContext* cx, IsAcceptableThis Test, NativeImpl Impl, const CallArgs& args)
  94. {
  95. HandleValue thisv = args.thisv();
  96. if (Test(thisv))
  97. return Impl(cx, args);
  98. return detail::CallMethodIfWrapped(cx, Test, Impl, args);
  99. }
  100. } // namespace JS
  101. #endif /* js_CallNonGenericMethod_h */