CallNonGenericMethod.h 4.4 KB

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