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