-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathresolve.cpp
More file actions
288 lines (232 loc) · 8.65 KB
/
Copy pathresolve.cpp
File metadata and controls
288 lines (232 loc) · 8.65 KB
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
#include <iostream>
#include <limits>
#include <jsapi.h>
#include <jsfriendapi.h>
#include <js/CompilationAndEvaluation.h>
#include <js/Conversions.h>
#include <js/experimental/TypedData.h>
#include <js/friend/ErrorMessages.h>
#include <js/Object.h>
#include <js/Initialization.h>
#include <js/SourceText.h>
#include "boilerplate.h"
namespace zlib {
#include <zlib.h>
}
/* This example illustrates how to set up a class with a custom resolve hook, in
* order to do lazy property resolution.
*
* We'll use the CRC-32 checksum API from zlib as an example. Not because it's
* an incredibly useful API, but zlib is already a dependency of SpiderMonkey,
* so it's likely to be installed anywhere these examples are being compiled.
*
* There will be two properties that can resolve lazily: an `update()` method,
* and a `checksum` property. */
class Crc {
enum Slots { CrcSlot, SlotCount };
unsigned long m_crc;
Crc(void) : m_crc(zlib::crc32(0L, nullptr, 0)) {}
bool updateImpl(JSContext* cx, const JS::CallArgs& args) {
if (!args.requireAtLeast(cx, "update", 1)) return false;
if (!args[0].isObject() || !JS_IsUint8Array(&args[0].toObject())) {
JS_ReportErrorASCII(cx, "argument to update() should be a Uint8Array");
return false;
}
JSObject* buffer = &args[0].toObject();
size_t len = JS_GetTypedArrayLength(buffer);
if (len > std::numeric_limits<unsigned>::max()) {
JS_ReportErrorASCII(cx, "array has too many bytes");
return false;
}
{
bool isSharedMemory;
JS::AutoAssertNoGC nogc;
uint8_t* data = JS_GetUint8ArrayData(buffer, &isSharedMemory, nogc);
m_crc = zlib::crc32(m_crc, data, unsigned(len));
}
args.rval().setUndefined();
return true;
}
bool getChecksumImpl(JSContext* cx, const JS::CallArgs& args) {
args.rval().setNumber(uint32_t(m_crc));
return true;
}
static Crc* getPriv(JSObject* obj) {
return JS::GetMaybePtrFromReservedSlot<Crc>(obj, CrcSlot);
}
static bool constructor(JSContext* cx, unsigned argc, JS::Value* vp) {
JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
if (!args.isConstructing()) {
JS_ReportErrorNumberASCII(cx, js::GetErrorMessage, nullptr,
JSMSG_CANT_CALL_CLASS_CONSTRUCTOR);
return false;
}
JS::RootedObject newObj(cx,
JS_NewObjectForConstructor(cx, &Crc::klass, args));
if (!newObj) return false;
Crc* priv = new Crc();
JS::SetReservedSlot(newObj, CrcSlot, JS::PrivateValue(priv));
args.rval().setObject(*newObj);
return true;
}
static bool update(JSContext* cx, unsigned argc, JS::Value* vp) {
JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
JS::RootedObject thisObj(cx);
if (!args.computeThis(cx, &thisObj)) return false;
if (!JS_InstanceOf(cx, thisObj, &Crc::klass, &args)) return false;
return getPriv(thisObj)->updateImpl(cx, args);
}
static bool getChecksum(JSContext* cx, unsigned argc, JS::Value* vp) {
JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
JS::RootedObject thisObj(cx);
if (!args.computeThis(cx, &thisObj)) return false;
if (!JS_InstanceOf(cx, thisObj, &Crc::klass, &args)) return false;
return getPriv(thisObj)->getChecksumImpl(cx, args);
}
static bool newEnumerate(JSContext* cx, JS::HandleObject obj,
JS::MutableHandleIdVector properties,
bool enumerableOnly) {
jsid idUpdate =
JS::PropertyKey::fromPinnedString(JS_AtomizeAndPinString(cx, "update"));
if (!properties.append(idUpdate)) return false;
jsid idChecksum = JS::PropertyKey::fromPinnedString(
JS_AtomizeAndPinString(cx, "checksum"));
if (!properties.append(idChecksum)) return false;
return true;
}
static bool resolve(JSContext* cx, JS::HandleObject obj, JS::HandleId id,
bool* resolved) {
if (!id.isString()) {
*resolved = false;
return true;
}
JSLinearString* str = id.toLinearString();
if (JS_LinearStringEqualsAscii(str, "update")) {
if (!JS_DefineFunctionById(cx, obj, id, &Crc::update, 1,
JSPROP_ENUMERATE))
return false;
*resolved = true;
return true;
}
if (JS_LinearStringEqualsAscii(str, "checksum")) {
if (!JS_DefinePropertyById(cx, obj, id, &Crc::getChecksum, nullptr,
JSPROP_ENUMERATE))
return false;
*resolved = true;
return true;
}
*resolved = false;
return true;
}
static bool mayResolve(const JSAtomState& names, jsid id,
JSObject* maybeObj) {
if (!id.isString()) return false;
JSLinearString* str = id.toLinearString();
return JS_LinearStringEqualsAscii(str, "update") ||
JS_LinearStringEqualsAscii(str, "checksum");
}
static void finalize(JS::GCContext* gcx, JSObject* obj) {
Crc* priv = getPriv(obj);
if (priv) {
delete priv;
JS::SetReservedSlot(obj, CrcSlot, JS::UndefinedValue());
}
}
// Note that this vtable applies both to the prototype and instances. The
// operations must distinguish between the two.
static constexpr JSClassOps classOps = {
nullptr, // addProperty
nullptr, // deleteProperty
nullptr, // enumerate
&Crc::newEnumerate,
&Crc::resolve,
&Crc::mayResolve,
&Crc::finalize,
nullptr, // call
nullptr, // construct
nullptr, // trace
};
static constexpr JSClass klass = {
"Crc",
JSCLASS_HAS_RESERVED_SLOTS(SlotCount) | JSCLASS_BACKGROUND_FINALIZE,
&Crc::classOps,
};
public:
static bool DefinePrototype(JSContext* cx) {
JS::RootedObject global(cx, JS::CurrentGlobalOrNull(cx));
return JS_InitClass(cx,
global, // the object in which to define the class
nullptr, // the prototype object, Crc.prototype
// (in our case, a plain JS object because
// calling "Crc.prototype.update" does not
// make sense)
nullptr, // the prototype of the parent class (in our
// case, Object.prototype)
Crc::klass.name, // "Crc", the constructor name
&Crc::constructor,
0, // constructor and num. args
// The four nullptrs below are for arrays where you
// would list predefined (not lazy) methods and
// properties, static and non-static
nullptr, nullptr, nullptr, nullptr);
}
};
constexpr JSClassOps Crc::classOps;
constexpr JSClass Crc::klass;
static const char* testProgram = R"js(
const crc = new Crc();
crc.update(new Uint8Array([1, 2, 3, 4, 5]));
crc.checksum;
)js";
/**** BOILERPLATE *************************************************************/
// Below here, the code is very similar to what is found in hello.cpp
static bool ExecuteCodePrintResult(JSContext* cx, const char* code) {
JS::CompileOptions options(cx);
options.setFileAndLine("noname", 1);
JS::SourceText<mozilla::Utf8Unit> source;
if (!source.init(cx, code, strlen(code), JS::SourceOwnership::Borrowed)) {
return false;
}
JS::RootedValue rval(cx);
if (!JS::Evaluate(cx, options, source, &rval)) return false;
JS::RootedString rval_str(cx, JS::ToString(cx, rval));
if (!rval_str) return false;
// The printed value will be a number, so we know it will be an ASCII string
// that we can just print directly.
std::cout << JS_EncodeStringToASCII(cx, rval_str).get() << '\n';
return true;
}
static void die(const char* why) {
std::cerr << "fatal error: " << why;
exit(1);
}
void LogException(JSContext* cx) {
JS::RootedValue exception(cx);
if (!JS_GetPendingException(cx, &exception))
die("Uncatchable exception thrown, out of memory or something");
JS_ClearPendingException(cx);
JS::RootedString exc_str(cx, JS::ToString(cx, exception));
if (!exc_str) die("Exception thrown, could not be converted to string");
std::cout << "Exception thrown: " << JS_EncodeStringToUTF8(cx, exc_str).get()
<< '\n';
}
static bool ResolveExample(JSContext* cx) {
JS::RootedObject global(cx, boilerplate::CreateGlobal(cx));
if (!global) {
return false;
}
JSAutoRealm ar(cx, global);
if (!Crc::DefinePrototype(cx)) {
LogException(cx);
return false;
}
if (!ExecuteCodePrintResult(cx, testProgram)) {
LogException(cx);
return false;
}
return true;
}
int main(int argc, const char* argv[]) {
if (!boilerplate::RunExample(ResolveExample)) return 1;
return 0;
}