Debian Codegraph @

js/src/builtin/HandlerFunction-inl.h

raw
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 * vim: set ts=8 sts=2 et sw=2 tw=80:
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 /*
8 * Handler for operations that act on a target object, and possibly upon
9 * an extra value.
10 */
11
12 #ifndef builtin_HandlerFunction_inl_h
13 #define builtin_HandlerFunction_inl_h
14
15 #include <stddef.h> // size_t
16
17 #include "gc/AllocKind.h" // js::gc::AllocKind
18 #include "js/CallArgs.h" // JS::CallArgs
19 #include "js/RootingAPI.h" // JS::Handle, JS::Rooted
20 #include "js/Value.h" // JS::ObjectValue
21 #include "vm/JSContext.h" // JSContext
22 #include "vm/JSFunction.h" // JSFunction, js::Native, js::NewNativeFunction
23 #include "vm/JSObject.h" // JSObject, js::GenericObject
24 #include "vm/StringType.h" // js::PropertyName
25
26 #include "vm/JSContext-inl.h" // JSContext::check
27
28 namespace js {
29
30 // Handler functions are extended functions, that close over a target object and
31 // (optionally) over an extra object, storing those objects in the function's
32 // extended slots.
33 constexpr size_t HandlerFunctionSlot_Target = 0;
34 constexpr size_t HandlerFunctionSlot_Extra = 1;
35
36 static_assert(HandlerFunctionSlot_Extra < FunctionExtended::NUM_EXTENDED_SLOTS,
37 "handler function slots shouldn't exceed available extended "
38 "slots");
39
40 [[nodiscard]] inline JSFunction* NewHandler(JSContext* cx, Native handler,
41 JS::Handle<JSObject*> target) {
42 cx->check(target);
43
44 JS::Handle<PropertyName*> funName = cx->names().empty_;
45 JS::Rooted<JSFunction*> handlerFun(
46 cx, NewNativeFunction(cx, handler, 0, funName,
47 gc::AllocKind::FUNCTION_EXTENDED, GenericObject));
48 if (!handlerFun) {
49 return nullptr;
50 }
51 handlerFun->setExtendedSlot(HandlerFunctionSlot_Target,
52 JS::ObjectValue(*target));
53 return handlerFun;
54 }
55
56 [[nodiscard]] inline JSFunction* NewHandlerWithExtra(
57 JSContext* cx, Native handler, JS::Handle<JSObject*> target,
58 JS::Handle<JSObject*> extra) {
59 cx->check(extra);
60 JSFunction* handlerFun = NewHandler(cx, handler, target);
61 if (handlerFun) {
62 handlerFun->setExtendedSlot(HandlerFunctionSlot_Extra,
63 JS::ObjectValue(*extra));
64 }
65 return handlerFun;
66 }
67
68 [[nodiscard]] inline JSFunction* NewHandlerWithExtraValue(
69 JSContext* cx, Native handler, JS::Handle<JSObject*> target,
70 JS::Handle<JS::Value> extra) {
71 cx->check(extra);
72 JSFunction* handlerFun = NewHandler(cx, handler, target);
73 if (handlerFun) {
74 handlerFun->setExtendedSlot(HandlerFunctionSlot_Extra, extra);
75 }
76 return handlerFun;
77 }
78
79 /**
80 * Within the call of a handler function that "closes over" a target value that
81 * is always a |T*| object (and never a wrapper around one), return that |T*|.
82 */
83 template <class T>
84 [[nodiscard]] inline T* TargetFromHandler(const JS::CallArgs& args) {
85 JSFunction& func = args.callee().as<JSFunction>();
86 return &func.getExtendedSlot(HandlerFunctionSlot_Target).toObject().as<T>();
87 }
88
89 /**
90 * Within the call of a handler function that "closes over" a target value and
91 * an extra value, return that extra value.
92 */
93 [[nodiscard]] inline JS::Value ExtraValueFromHandler(const JS::CallArgs& args) {
94 JSFunction& func = args.callee().as<JSFunction>();
95 return func.getExtendedSlot(HandlerFunctionSlot_Extra);
96 }
97
98 /**
99 * Within the call of a handler function that "closes over" a target value and
100 * an extra value, where that extra value is always a |T*| object (and never a
101 * wrapper around one), return that |T*|.
102 */
103 template <class T>
104 [[nodiscard]] inline T* ExtraFromHandler(const JS::CallArgs& args) {
105 return &ExtraValueFromHandler(args).toObject().as<T>();
106 }
107
108 } // namespace js
109
110 #endif // builtin_HandlerFunction_inl_h