mozjs_sys 0.140.10-2

System crate for the Mozilla SpiderMonkey JavaScript engine.
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
 * vim: set ts=8 sts=4 et sw=4 tw=99:
 * 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/. */

#include "jsapi.h"

#include "js/ArrayBuffer.h"
#include "js/ArrayBufferMaybeShared.h"
#include "js/BigInt.h"
#include "js/BuildId.h"
#include "js/ColumnNumber.h"
#include "js/CompilationAndEvaluation.h"
#include "js/ContextOptions.h"
#include "js/Conversions.h"
#include "js/Date.h"
#include "js/EnvironmentChain.h"
#include "js/Equality.h"
#include "js/ForOfIterator.h"
#include "js/Id.h"
#include "js/Initialization.h"
#include "js/JSON.h"
#include "js/MemoryMetrics.h"
#include "js/Modules.h"
#include "js/Object.h"
#include "js/Promise.h"
#include "js/PropertySpec.h"
#include "js/Proxy.h"
#include "js/Realm.h"
#include "js/RegExp.h"
#include "js/SavedFrameAPI.h"
#include "js/ScalarType.h"
#include "js/SharedArrayBuffer.h"
#include "js/SourceText.h"
#include "js/String.h"
#include "js/StructuredClone.h"
#include "js/Symbol.h"
#include "js/UniquePtr.h"
#include "js/Utility.h"
#include "js/Warnings.h"
#include "js/WasmModule.h"
#include "js/experimental/JSStencil.h"
#include "js/experimental/JitInfo.h"
#include "js/experimental/TypedData.h"
#include "js/friend/DOMProxy.h"
#include "js/friend/ErrorMessages.h"
#include "js/friend/WindowProxy.h"
#include "js/shadow/Object.h"
#include "js/shadow/Shape.h"
#include "jsfriendapi.h"

namespace glue {

// Reexport some functions that are marked inline.

bool JS_Init() { return ::JS_Init(); }

JS::RealmOptions* JS_NewRealmOptions() {
  JS::RealmOptions* result = new JS::RealmOptions;
  return result;
}

void DeleteRealmOptions(JS::RealmOptions* options) { delete options; }

JS::OwningCompileOptions* JS_NewOwningCompileOptions(JSContext* cx) {
  JS::OwningCompileOptions* result = new JS::OwningCompileOptions(cx);
  return result;
}

void DeleteOwningCompileOptions(JS::OwningCompileOptions* opts) { delete opts; }

JS::shadow::Zone* JS_AsShadowZone(JS::Zone* zone) {
  return JS::shadow::Zone::from(zone);
}

// Currently Unused, see jsimpls.rs (JS::CallArgs::from_vp)
JS::CallArgs JS_CallArgsFromVp(unsigned argc, JS::Value* vp) {
  return JS::CallArgsFromVp(argc, vp);
}

void JS_StackCapture_AllFrames(JS::StackCapture* capture) {
  JS::StackCapture all = JS::StackCapture(JS::AllFrames());
  // Since Rust can't provide a meaningful initial value for the
  // pointer, it is uninitialized memory. This means we must
  // overwrite its value, rather than perform an assignment
  // which could invoke a destructor on uninitialized memory.
  memcpy(capture, &all, sizeof(JS::StackCapture));
}

void JS_StackCapture_MaxFrames(uint32_t max, JS::StackCapture* capture) {
  JS::StackCapture maxFrames = JS::StackCapture(JS::MaxFrames(max));
  memcpy(capture, &maxFrames, sizeof(JS::StackCapture));
}

void JS_StackCapture_FirstSubsumedFrame(JSContext* cx,
                                        bool ignoreSelfHostedFrames,
                                        JS::StackCapture* capture) {
  JS::StackCapture subsumed =
      JS::StackCapture(JS::FirstSubsumedFrame(cx, ignoreSelfHostedFrames));
  memcpy(capture, &subsumed, sizeof(JS::StackCapture));
}

size_t GetLinearStringLength(JSLinearString* s) {
  return JS::GetLinearStringLength(s);
}

uint16_t GetLinearStringCharAt(JSLinearString* s, size_t idx) {
  return JS::GetLinearStringCharAt(s, idx);
}

JSLinearString* AtomToLinearString(JSAtom* atom) {
  return JS::AtomToLinearString(atom);
}

// Wrappers around UniquePtr functions

/**
 * Create a new ArrayBuffer with the given contents. The contents must not be
 * modified by any other code, internal or external.
 *
 * !!! IMPORTANT !!!
 * If and only if an ArrayBuffer is successfully created and returned,
 * ownership of |contents| is transferred to the new ArrayBuffer.
 *
 * When the ArrayBuffer is ready to be disposed of, `freeFunc(contents,
 * freeUserData)` will be called to release the ArrayBuffer's reference on the
 * contents.
 *
 * `freeFunc()` must not call any JSAPI functions that could cause a garbage
 * collection.
 *
 * The caller must keep the buffer alive until `freeFunc()` is called, or, if
 * `freeFunc` is null, until the JSRuntime is destroyed.
 *
 * The caller must not access the buffer on other threads. The JS engine will
 * not allow the buffer to be transferred to other threads. If you try to
 * transfer an external ArrayBuffer to another thread, the data is copied to a
 * new malloc buffer. `freeFunc()` must be threadsafe, and may be called from
 * any thread.
 *
 * This allows ArrayBuffers to be used with embedder objects that use reference
 * counting, for example. In that case the caller is responsible
 * for incrementing the reference count before passing the contents to this
 * function. This also allows using non-reference-counted contents that must be
 * freed with some function other than free().
 */
JSObject* NewExternalArrayBuffer(JSContext* cx, size_t nbytes, void* contents,
                                 JS::BufferContentsFreeFunc freeFunc,
                                 void* freeUserData) {
  js::UniquePtr<void, JS::BufferContentsDeleter> dataPtr{
      contents, {freeFunc, freeUserData}};
  return NewExternalArrayBuffer(cx, nbytes, std::move(dataPtr));
}

JSObject* NewArrayBufferWithContents(JSContext* cx, size_t nbytes,
                                     void* contents) {
  js::UniquePtr<void, JS::FreePolicy> dataPtr{contents};
  return JS::NewArrayBufferWithContents(cx, nbytes, std::move(dataPtr));
}

// Reexport some methods

bool JS_ForOfIteratorInit(
    JS::ForOfIterator* iterator, JS::HandleValue iterable,
    JS::ForOfIterator::NonIterableBehavior nonIterableBehavior) {
  return iterator->init(iterable, nonIterableBehavior);
}

bool JS_ForOfIteratorNext(JS::ForOfIterator* iterator,
                          JS::MutableHandleValue val, bool* done) {
  return iterator->next(val, done);
}

// These functions are only intended for use in testing,
// to make sure that the Rust implementation of JS::Value
// agrees with the C++ implementation.

void JS_ValueSetBoolean(JS::Value* value, bool x) { value->setBoolean(x); }

bool JS_ValueIsBoolean(const JS::Value* value) { return value->isBoolean(); }

bool JS_ValueToBoolean(const JS::Value* value) { return value->toBoolean(); }

void JS_ValueSetDouble(JS::Value* value, double x) { value->setDouble(x); }

bool JS_ValueIsDouble(const JS::Value* value) { return value->isDouble(); }

double JS_ValueToDouble(const JS::Value* value) { return value->toDouble(); }

void JS_ValueSetInt32(JS::Value* value, int32_t x) { value->setInt32(x); }

bool JS_ValueIsInt32(const JS::Value* value) { return value->isInt32(); }

int32_t JS_ValueToInt32(const JS::Value* value) { return value->toInt32(); }

bool JS_ValueIsNumber(const JS::Value* value) { return value->isNumber(); }

double JS_ValueToNumber(const JS::Value* value) { return value->toNumber(); }

void JS_ValueSetNull(JS::Value* value) { value->setNull(); }

bool JS_ValueIsNull(const JS::Value* value) { return value->isNull(); }

bool JS_ValueIsUndefined(const JS::Value* value) {
  return value->isUndefined();
}

// These types are using maybe so we manually unwrap them in these wrappers

bool FromPropertyDescriptor(JSContext* cx,
                            JS::Handle<JS::PropertyDescriptor> desc_,
                            JS::MutableHandleValue vp) {
  return JS::FromPropertyDescriptor(
      cx,
      JS::Rooted<mozilla::Maybe<JS::PropertyDescriptor>>(
          cx, mozilla::ToMaybe(&desc_)),
      vp);
}

bool JS_GetPropertyDescriptor(JSContext* cx, JS::Handle<JSObject*> obj,
                              const char* name,
                              JS::MutableHandle<JS::PropertyDescriptor> desc,
                              JS::MutableHandle<JSObject*> holder,
                              bool* isNone) {
  JS::Rooted<mozilla::Maybe<JS::PropertyDescriptor>> mpd(cx);
  bool result = JS_GetPropertyDescriptor(cx, obj, name, &mpd, holder);
  *isNone = mpd.isNothing();
  if (!*isNone) {
    desc.set(*mpd);
  }
  return result;
}

bool JS_GetOwnPropertyDescriptorById(
    JSContext* cx, JS::HandleObject obj, JS::HandleId id,
    JS::MutableHandle<JS::PropertyDescriptor> desc, bool* isNone) {
  JS::Rooted<mozilla::Maybe<JS::PropertyDescriptor>> mpd(cx);
  bool result = JS_GetOwnPropertyDescriptorById(cx, obj, id, &mpd);
  *isNone = mpd.isNothing();
  if (!*isNone) {
    desc.set(*mpd);
  }
  return result;
}

bool JS_GetOwnPropertyDescriptor(JSContext* cx, JS::HandleObject obj,
                                 const char* name,
                                 JS::MutableHandle<JS::PropertyDescriptor> desc,
                                 bool* isNone) {
  JS::Rooted<mozilla::Maybe<JS::PropertyDescriptor>> mpd(cx);
  bool result = JS_GetOwnPropertyDescriptor(cx, obj, name, &mpd);
  *isNone = mpd.isNothing();
  if (!*isNone) {
    desc.set(*mpd);
  }
  return result;
}

bool JS_GetOwnUCPropertyDescriptor(
    JSContext* cx, JS::HandleObject obj, const char16_t* name, size_t namelen,
    JS::MutableHandle<JS::PropertyDescriptor> desc, bool* isNone) {
  JS::Rooted<mozilla::Maybe<JS::PropertyDescriptor>> mpd(cx);
  bool result = JS_GetOwnUCPropertyDescriptor(cx, obj, name, namelen, &mpd);
  *isNone = mpd.isNothing();
  if (!*isNone) {
    desc.set(*mpd);
  }
  return result;
}

bool JS_GetPropertyDescriptorById(
    JSContext* cx, JS::HandleObject obj, JS::HandleId id,
    JS::MutableHandle<JS::PropertyDescriptor> desc,
    JS::MutableHandleObject holder, bool* isNone) {
  JS::Rooted<mozilla::Maybe<JS::PropertyDescriptor>> mpd(cx);
  bool result = JS_GetPropertyDescriptorById(cx, obj, id, &mpd, holder);
  *isNone = mpd.isNothing();
  if (!*isNone) {
    desc.set(*mpd);
  }
  return result;
}

bool JS_GetUCPropertyDescriptor(JSContext* cx, JS::HandleObject obj,
                                const char16_t* name, size_t namelen,
                                JS::MutableHandle<JS::PropertyDescriptor> desc,
                                JS::MutableHandleObject holder, bool* isNone) {
  JS::Rooted<mozilla::Maybe<JS::PropertyDescriptor>> mpd(cx);
  bool result =
      JS_GetUCPropertyDescriptor(cx, obj, name, namelen, &mpd, holder);
  *isNone = mpd.isNothing();
  if (!*isNone) {
    desc.set(*mpd);
  }
  return result;
}

bool SetPropertyIgnoringNamedGetter(
    JSContext* cx, JS::HandleObject obj, JS::HandleId id, JS::HandleValue v,
    JS::HandleValue receiver, const JS::Handle<JS::PropertyDescriptor>* ownDesc,
    JS::ObjectOpResult& result) {
  return js::SetPropertyIgnoringNamedGetter(
      cx, obj, id, v, receiver,
      JS::Rooted<mozilla::Maybe<JS::PropertyDescriptor>>(
          cx, mozilla::ToMaybe(ownDesc)),
      result);
}

bool CreateError(JSContext* cx, JSExnType type, JS::HandleObject stack,
                 JS::HandleString fileName, uint32_t lineNumber,
                 uint32_t columnNumber, JSErrorReport* report,
                 JS::HandleString message, JS::HandleValue cause,
                 JS::MutableHandleValue rval) {
  return JS::CreateError(
      cx, type, stack, fileName, lineNumber,
      JS::ColumnNumberOneOrigin(columnNumber), report, message,
      JS::Rooted<mozilla::Maybe<JS::Value>>(cx, mozilla::ToMaybe(&cause)),
      rval);
}

JSExnType GetErrorType(const JS::Value& val) {
  auto type = JS_GetErrorType(val);
  if (type.isNothing()) {
    return JSEXN_ERROR_LIMIT;
  }
  return *type;
}

void GetExceptionCause(JSObject* exc, JS::MutableHandleValue dest) {
  auto cause = JS::GetExceptionCause(exc);
  if (cause.isNothing()) {
    dest.setNull();
  } else {
    dest.set(*cause);
  }
}

JS::EnvironmentChain* NewEnvironmentChain(
    JSContext* cx, JS::SupportUnscopables supportUnscopables) {
  return new JS::EnvironmentChain(cx, supportUnscopables);
}

void DeleteEnvironmentChain(JS::EnvironmentChain* chain) { delete chain; }

bool AppendToEnvironmentChain(JS::EnvironmentChain* chain, JSObject* obj) {
  return chain->append(obj);
}

}  // namespace glue

// There's a couple of classes from pre-57 releases of SM that bindgen can't
// deal with. https://github.com/rust-lang-nursery/rust-bindgen/issues/851
// https://bugzilla.mozilla.org/show_bug.cgi?id=1277338
// https://rust-lang-nursery.github.io/rust-bindgen/replacing-types.html

/**
 * <div rustbindgen replaces="JS::CallArgs"></div>
 */

class MOZ_STACK_CLASS CallArgsReplacement {
 protected:
  JS::Value* argv_;
  unsigned argc_;
  bool constructing_ : 1;
  bool ignoresReturnValue_ : 1;
#ifdef JS_DEBUG
  JS::detail::IncludeUsedRval wantUsedRval_;
#endif
};

/**
 * <div rustbindgen replaces="JSJitMethodCallArgs"></div>
 */

class JSJitMethodCallArgsReplacement {
 private:
  JS::Value* argv_;
  unsigned argc_;
  bool constructing_ : 1;
  bool ignoresReturnValue_ : 1;
#ifdef JS_DEBUG
  JS::detail::NoUsedRval wantUsedRval_;
#endif
};

/// <div rustbindgen replaces="JS::MutableHandleIdVector"></div>
struct MutableHandleIdVector_Simple {
  void* ptr;
};
static_assert(sizeof(JS::MutableHandleIdVector) ==
                  sizeof(MutableHandleIdVector_Simple),
              "wrong handle size");

/// <div rustbindgen replaces="JS::HandleObjectVector"></div>
struct HandleObjectVector_Simple {
  void* ptr;
};

/// <div rustbindgen replaces="JS::MutableHandleObjectVector"></div>
struct MutableHandleObjectVector_Simple {
  void* ptr;
};