Source code

Revision control

Other Tools

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
/* JavaScript API. */
8
9
#ifndef jsapi_h
10
#define jsapi_h
11
12
#include "mozilla/AlreadyAddRefed.h"
13
#include "mozilla/FloatingPoint.h"
14
#include "mozilla/MemoryReporting.h"
15
#include "mozilla/Range.h"
16
#include "mozilla/RangedPtr.h"
17
#include "mozilla/RefPtr.h"
18
#include "mozilla/Utf8.h"
19
#include "mozilla/Variant.h"
20
21
#include <stdarg.h>
22
#include <stddef.h>
23
#include <stdint.h>
24
#include <stdio.h>
25
26
#include "jspubtd.h"
27
28
#include "js/AllocPolicy.h"
29
#include "js/CallArgs.h"
30
#include "js/CharacterEncoding.h"
31
#include "js/Class.h"
32
#include "js/CompileOptions.h"
33
#include "js/ErrorReport.h"
34
#include "js/GCVector.h"
35
#include "js/HashTable.h"
36
#include "js/Id.h"
37
#include "js/MemoryFunctions.h"
38
#include "js/OffThreadScriptCompilation.h"
39
#include "js/Principals.h"
40
#include "js/PropertyDescriptor.h"
41
#include "js/PropertySpec.h"
42
#include "js/Realm.h"
43
#include "js/RealmOptions.h"
44
#include "js/RefCounted.h"
45
#include "js/RootingAPI.h"
46
#include "js/TracingAPI.h"
47
#include "js/Transcoding.h"
48
#include "js/UniquePtr.h"
49
#include "js/Utility.h"
50
#include "js/Value.h"
51
#include "js/Vector.h"
52
53
/************************************************************************/
54
55
struct JSFunctionSpec;
56
struct JSPropertySpec;
57
58
namespace JS {
59
60
template <typename UnitT>
61
class SourceText;
62
63
class TwoByteChars;
64
65
/** AutoValueArray roots an internal fixed-size array of Values. */
66
template <size_t N>
67
class MOZ_RAII AutoValueArray : public AutoGCRooter {
68
const size_t length_;
69
Value elements_[N];
70
71
public:
72
explicit AutoValueArray(JSContext* cx MOZ_GUARD_OBJECT_NOTIFIER_PARAM)
73
: AutoGCRooter(cx, AutoGCRooter::Tag::ValueArray), length_(N) {
74
MOZ_GUARD_OBJECT_NOTIFIER_INIT;
75
}
76
77
unsigned length() const { return length_; }
78
const Value* begin() const { return elements_; }
79
Value* begin() { return elements_; }
80
81
HandleValue operator[](unsigned i) const {
82
MOZ_ASSERT(i < N);
83
return HandleValue::fromMarkedLocation(&elements_[i]);
84
}
85
MutableHandleValue operator[](unsigned i) {
86
MOZ_ASSERT(i < N);
87
return MutableHandleValue::fromMarkedLocation(&elements_[i]);
88
}
89
90
MOZ_DECL_USE_GUARD_OBJECT_NOTIFIER
91
};
92
93
using ValueVector = JS::GCVector<JS::Value>;
94
using IdVector = JS::GCVector<jsid>;
95
using ScriptVector = JS::GCVector<JSScript*>;
96
using StringVector = JS::GCVector<JSString*>;
97
98
/**
99
* Custom rooting behavior for internal and external clients.
100
*/
101
class MOZ_RAII JS_PUBLIC_API CustomAutoRooter : private AutoGCRooter {
102
public:
103
template <typename CX>
104
explicit CustomAutoRooter(const CX& cx MOZ_GUARD_OBJECT_NOTIFIER_PARAM)
105
: AutoGCRooter(cx, AutoGCRooter::Tag::Custom) {
106
MOZ_GUARD_OBJECT_NOTIFIER_INIT;
107
}
108
109
friend void AutoGCRooter::trace(JSTracer* trc);
110
111
protected:
112
virtual ~CustomAutoRooter() {}
113
114
/** Supplied by derived class to trace roots. */
115
virtual void trace(JSTracer* trc) = 0;
116
117
private:
118
MOZ_DECL_USE_GUARD_OBJECT_NOTIFIER
119
};
120
121
/** A handle to an array of rooted values. */
122
class HandleValueArray {
123
const size_t length_;
124
const Value* const elements_;
125
126
HandleValueArray(size_t len, const Value* elements)
127
: length_(len), elements_(elements) {}
128
129
public:
130
explicit HandleValueArray(HandleValue value)
131
: length_(1), elements_(value.address()) {}
132
133
MOZ_IMPLICIT HandleValueArray(const RootedValueVector& values)
134
: length_(values.length()), elements_(values.begin()) {}
135
136
template <size_t N>
137
MOZ_IMPLICIT HandleValueArray(const AutoValueArray<N>& values)
138
: length_(N), elements_(values.begin()) {}
139
140
/** CallArgs must already be rooted somewhere up the stack. */
141
MOZ_IMPLICIT HandleValueArray(const JS::CallArgs& args)
142
: length_(args.length()), elements_(args.array()) {}
143
144
/** Use with care! Only call this if the data is guaranteed to be marked. */
145
static HandleValueArray fromMarkedLocation(size_t len,
146
const Value* elements) {
147
return HandleValueArray(len, elements);
148
}
149
150
static HandleValueArray subarray(const HandleValueArray& values,
151
size_t startIndex, size_t len) {
152
MOZ_ASSERT(startIndex + len <= values.length());
153
return HandleValueArray(len, values.begin() + startIndex);
154
}
155
156
static HandleValueArray empty() { return HandleValueArray(0, nullptr); }
157
158
size_t length() const { return length_; }
159
const Value* begin() const { return elements_; }
160
161
HandleValue operator[](size_t i) const {
162
MOZ_ASSERT(i < length_);
163
return HandleValue::fromMarkedLocation(&elements_[i]);
164
}
165
};
166
167
} /* namespace JS */
168
169
/* Callbacks and their arguments. */
170
171
/************************************************************************/
172
173
typedef bool (*JSInterruptCallback)(JSContext* cx);
174
175
/**
176
* Callback used to ask the embedding for the cross compartment wrapper handler
177
* that implements the desired prolicy for this kind of object in the
178
* destination compartment. |obj| is the object to be wrapped. If |existing| is
179
* non-nullptr, it will point to an existing wrapper object that should be
180
* re-used if possible. |existing| is guaranteed to be a cross-compartment
181
* wrapper with a lazily-defined prototype and the correct global. It is
182
* guaranteed not to wrap a function.
183
*/
184
typedef JSObject* (*JSWrapObjectCallback)(JSContext* cx,
185
JS::HandleObject existing,
186
JS::HandleObject obj);
187
188
/**
189
* Callback used by the wrap hook to ask the embedding to prepare an object
190
* for wrapping in a context. This might include unwrapping other wrappers
191
* or even finding a more suitable object for the new compartment. If |origObj|
192
* is non-null, then it is the original object we are going to swap into during
193
* a transplant.
194
*/
195
typedef void (*JSPreWrapCallback)(JSContext* cx, JS::HandleObject scope,
196
JS::HandleObject origObj,
197
JS::HandleObject obj,
198
JS::HandleObject objectPassedToWrap,
199
JS::MutableHandleObject retObj);
200
201
struct JSWrapObjectCallbacks {
202
JSWrapObjectCallback wrap;
203
JSPreWrapCallback preWrap;
204
};
205
206
typedef void (*JSDestroyCompartmentCallback)(JSFreeOp* fop,
207
JS::Compartment* compartment);
208
209
typedef size_t (*JSSizeOfIncludingThisCompartmentCallback)(
210
mozilla::MallocSizeOf mallocSizeOf, JS::Compartment* compartment);
211
212
/**
213
* Callback used by memory reporting to ask the embedder how much memory an
214
* external string is keeping alive. The embedder is expected to return a value
215
* that corresponds to the size of the allocation that will be released by the
216
* JSStringFinalizer passed to JS_NewExternalString for this string.
217
*
218
* Implementations of this callback MUST NOT do anything that can cause GC.
219
*/
220
using JSExternalStringSizeofCallback =
221
size_t (*)(JSString* str, mozilla::MallocSizeOf mallocSizeOf);
222
223
/**
224
* Callback used to intercept JavaScript errors.
225
*/
226
struct JSErrorInterceptor {
227
/**
228
* This method is called whenever an error has been raised from JS code.
229
*
230
* This method MUST be infallible.
231
*/
232
virtual void interceptError(JSContext* cx, JS::HandleValue error) = 0;
233
};
234
235
/************************************************************************/
236
237
static MOZ_ALWAYS_INLINE JS::Value JS_NumberValue(double d) {
238
int32_t i;
239
d = JS::CanonicalizeNaN(d);
240
if (mozilla::NumberIsInt32(d, &i)) {
241
return JS::Int32Value(i);
242
}
243
return JS::DoubleValue(d);
244
}
245
246
/************************************************************************/
247
248
JS_PUBLIC_API bool JS_StringHasBeenPinned(JSContext* cx, JSString* str);
249
250
/************************************************************************/
251
252
/** Microseconds since the epoch, midnight, January 1, 1970 UTC. */
253
extern JS_PUBLIC_API int64_t JS_Now(void);
254
255
/** Don't want to export data, so provide accessors for non-inline Values. */
256
extern JS_PUBLIC_API JS::Value JS_GetEmptyStringValue(JSContext* cx);
257
258
extern JS_PUBLIC_API JSString* JS_GetEmptyString(JSContext* cx);
259
260
extern JS_PUBLIC_API bool JS_ValueToObject(JSContext* cx, JS::HandleValue v,
261
JS::MutableHandleObject objp);
262
263
extern JS_PUBLIC_API JSFunction* JS_ValueToFunction(JSContext* cx,
264
JS::HandleValue v);
265
266
extern JS_PUBLIC_API JSFunction* JS_ValueToConstructor(JSContext* cx,
267
JS::HandleValue v);
268
269
extern JS_PUBLIC_API JSString* JS_ValueToSource(JSContext* cx,
270
JS::Handle<JS::Value> v);
271
272
extern JS_PUBLIC_API bool JS_DoubleIsInt32(double d, int32_t* ip);
273
274
extern JS_PUBLIC_API JSType JS_TypeOfValue(JSContext* cx,
275
JS::Handle<JS::Value> v);
276
277
namespace JS {
278
279
extern JS_PUBLIC_API const char* InformalValueTypeName(const JS::Value& v);
280
281
} /* namespace JS */
282
283
/** True iff fun is the global eval function. */
284
extern JS_PUBLIC_API bool JS_IsBuiltinEvalFunction(JSFunction* fun);
285
286
/** True iff fun is the Function constructor. */
287
extern JS_PUBLIC_API bool JS_IsBuiltinFunctionConstructor(JSFunction* fun);
288
289
/************************************************************************/
290
291
// [SMDOC] Data Structures (JSContext, JSRuntime, Realm, Compartment, Zone)
292
//
293
// SpiderMonkey uses some data structures that behave a lot like Russian dolls:
294
// runtimes contain zones, zones contain compartments, compartments contain
295
// realms. Each layer has its own purpose.
296
//
297
// Realm
298
// -----
299
// Data associated with a global object. In the browser each frame has its
300
// own global/realm.
301
//
302
// Compartment
303
// -----------
304
// Security membrane; when an object from compartment A is used in compartment
305
// B, a cross-compartment wrapper (a kind of proxy) is used. In the browser each
306
// compartment currently contains one global/realm, but we want to change that
307
// so each compartment contains multiple same-origin realms (bug 1357862).
308
//
309
// Zone
310
// ----
311
// A Zone is a group of compartments that share GC resources (arenas, strings,
312
// etc) for memory usage and performance reasons. Zone is the GC unit: the GC
313
// can operate on one or more zones at a time. The browser uses roughly one zone
314
// per tab.
315
//
316
// Context
317
// -------
318
// JSContext represents a thread: there must be exactly one JSContext for each
319
// thread running JS/Wasm. Internally, helper threads have their own JSContext.
320
//
321
// Runtime
322
// -------
323
// JSRuntime is very similar to JSContext: each runtime belongs to one context
324
// (thread), but helper threads don't have their own runtimes (they're shared by
325
// all runtimes in the process and use the runtime of the task they're
326
// executing).
327
328
/*
329
* Locking, contexts, and memory allocation.
330
*
331
* It is important that SpiderMonkey be initialized, and the first context
332
* be created, in a single-threaded fashion. Otherwise the behavior of the
333
* library is undefined.
334
* See:
336
*/
337
338
// Create a new context (and runtime) for this thread.
339
extern JS_PUBLIC_API JSContext* JS_NewContext(
340
uint32_t maxbytes, uint32_t maxNurseryBytes = JS::DefaultNurseryBytes,
341
JSRuntime* parentRuntime = nullptr);
342
343
// The methods below for controlling the active context in a cooperatively
344
// multithreaded runtime are not threadsafe, and the caller must ensure they
345
// are called serially if there is a chance for contention between threads.
346
347
// Called from the active context for a runtime, yield execution so that
348
// this context is no longer active and can no longer use the API.
349
extern JS_PUBLIC_API void JS_YieldCooperativeContext(JSContext* cx);
350
351
// Called from a context whose runtime has no active context, this thread
352
// becomes the active context for that runtime and may use the API.
353
extern JS_PUBLIC_API void JS_ResumeCooperativeContext(JSContext* cx);
354
355
// Create a new context on this thread for cooperative multithreading in the
356
// same runtime as siblingContext. Called on a runtime (as indicated by
357
// siblingContet) which has no active context, on success the new context will
358
// become the runtime's active context.
359
extern JS_PUBLIC_API JSContext* JS_NewCooperativeContext(
360
JSContext* siblingContext);
361
362
// Destroy a context allocated with JS_NewContext or JS_NewCooperativeContext.
363
// The context must be the current active context in the runtime, and after
364
// this call the runtime will have no active context.
365
extern JS_PUBLIC_API void JS_DestroyContext(JSContext* cx);
366
367
JS_PUBLIC_API void* JS_GetContextPrivate(JSContext* cx);
368
369
JS_PUBLIC_API void JS_SetContextPrivate(JSContext* cx, void* data);
370
371
extern JS_PUBLIC_API JSRuntime* JS_GetParentRuntime(JSContext* cx);
372
373
extern JS_PUBLIC_API JSRuntime* JS_GetRuntime(JSContext* cx);
374
375
extern JS_PUBLIC_API void JS_SetFutexCanWait(JSContext* cx);
376
377
namespace js {
378
379
void AssertHeapIsIdle();
380
381
} /* namespace js */
382
383
namespace JS {
384
385
/**
386
* Initialize the runtime's self-hosted code. Embeddings should call this
387
* exactly once per runtime/context, before the first JS_NewGlobalObject
388
* call.
389
*/
390
JS_PUBLIC_API bool InitSelfHostedCode(JSContext* cx);
391
392
/**
393
* Asserts (in debug and release builds) that `obj` belongs to the current
394
* thread's context.
395
*/
396
JS_PUBLIC_API void AssertObjectBelongsToCurrentThread(JSObject* obj);
397
398
} /* namespace JS */
399
400
/**
401
* Set callback to send tasks to XPCOM thread pools
402
*/
403
JS_PUBLIC_API void SetHelperThreadTaskCallback(
404
void (*callback)(js::RunnableTask*));
405
406
extern JS_PUBLIC_API const char* JS_GetImplementationVersion(void);
407
408
extern JS_PUBLIC_API void JS_SetDestroyCompartmentCallback(
409
JSContext* cx, JSDestroyCompartmentCallback callback);
410
411
extern JS_PUBLIC_API void JS_SetSizeOfIncludingThisCompartmentCallback(
412
JSContext* cx, JSSizeOfIncludingThisCompartmentCallback callback);
413
414
extern JS_PUBLIC_API void JS_SetWrapObjectCallbacks(
415
JSContext* cx, const JSWrapObjectCallbacks* callbacks);
416
417
extern JS_PUBLIC_API void JS_SetExternalStringSizeofCallback(
418
JSContext* cx, JSExternalStringSizeofCallback callback);
419
420
#if defined(NIGHTLY_BUILD)
421
422
// Set a callback that will be called whenever an error
423
// is thrown in this runtime. This is designed as a mechanism
424
// for logging errors. Note that the VM makes no attempt to sanitize
425
// the contents of the error (so it may contain private data)
426
// or to sort out among errors (so it may not be the error you
427
// are interested in or for the component in which you are
428
// interested).
429
//
430
// If the callback sets a new error, this new error
431
// will replace the original error.
432
//
433
// May be `nullptr`.
434
extern JS_PUBLIC_API void JS_SetErrorInterceptorCallback(
435
JSRuntime*, JSErrorInterceptor* callback);
436
437
extern JS_PUBLIC_API JSErrorInterceptor* JS_GetErrorInterceptorCallback(
438
JSRuntime*);
439
440
// Examine a value to determine if it is one of the built-in Error types.
441
// If so, return the error type.
442
extern JS_PUBLIC_API mozilla::Maybe<JSExnType> JS_GetErrorType(
443
const JS::Value& val);
444
445
#endif // defined(NIGHTLY_BUILD)
446
447
extern JS_PUBLIC_API void JS_SetCompartmentPrivate(JS::Compartment* compartment,
448
void* data);
449
450
extern JS_PUBLIC_API void* JS_GetCompartmentPrivate(
451
JS::Compartment* compartment);
452
453
extern JS_PUBLIC_API void JS_SetZoneUserData(JS::Zone* zone, void* data);
454
455
extern JS_PUBLIC_API void* JS_GetZoneUserData(JS::Zone* zone);
456
457
extern JS_PUBLIC_API bool JS_WrapObject(JSContext* cx,
458
JS::MutableHandleObject objp);
459
460
extern JS_PUBLIC_API bool JS_WrapValue(JSContext* cx,
461
JS::MutableHandleValue vp);
462
463
extern JS_PUBLIC_API JSObject* JS_TransplantObject(JSContext* cx,
464
JS::HandleObject origobj,
465
JS::HandleObject target);
466
467
extern JS_PUBLIC_API bool JS_RefreshCrossCompartmentWrappers(
468
JSContext* cx, JS::Handle<JSObject*> obj);
469
470
/*
471
* At any time, a JSContext has a current (possibly-nullptr) realm.
472
* Realms are described in:
473
*
474
* developer.mozilla.org/en-US/docs/SpiderMonkey/SpiderMonkey_compartments
475
*
476
* The current realm of a context may be changed. The preferred way to do
477
* this is with JSAutoRealm:
478
*
479
* void foo(JSContext* cx, JSObject* obj) {
480
* // in some realm 'r'
481
* {
482
* JSAutoRealm ar(cx, obj); // constructor enters
483
* // in the realm of 'obj'
484
* } // destructor leaves
485
* // back in realm 'r'
486
* }
487
*
488
* The object passed to JSAutoRealm must *not* be a cross-compartment wrapper,
489
* because CCWs are not associated with a single realm.
490
*
491
* For more complicated uses that don't neatly fit in a C++ stack frame, the
492
* realm can be entered and left using separate function calls:
493
*
494
* void foo(JSContext* cx, JSObject* obj) {
495
* // in 'oldRealm'
496
* JS::Realm* oldRealm = JS::EnterRealm(cx, obj);
497
* // in the realm of 'obj'
498
* JS::LeaveRealm(cx, oldRealm);
499
* // back in 'oldRealm'
500
* }
501
*
502
* Note: these calls must still execute in a LIFO manner w.r.t all other
503
* enter/leave calls on the context. Furthermore, only the return value of a
504
* JS::EnterRealm call may be passed as the 'oldRealm' argument of
505
* the corresponding JS::LeaveRealm call.
506
*
507
* Entering a realm roots the realm and its global object for the lifetime of
508
* the JSAutoRealm.
509
*/
510
511
class MOZ_RAII JS_PUBLIC_API JSAutoRealm {
512
JSContext* cx_;
513
JS::Realm* oldRealm_;
514
515
public:
516
JSAutoRealm(JSContext* cx, JSObject* target MOZ_GUARD_OBJECT_NOTIFIER_PARAM);
517
JSAutoRealm(JSContext* cx, JSScript* target MOZ_GUARD_OBJECT_NOTIFIER_PARAM);
518
~JSAutoRealm();
519
520
MOZ_DECL_USE_GUARD_OBJECT_NOTIFIER
521
};
522
523
class MOZ_RAII JS_PUBLIC_API JSAutoNullableRealm {
524
JSContext* cx_;
525
JS::Realm* oldRealm_;
526
527
public:
528
explicit JSAutoNullableRealm(
529
JSContext* cx, JSObject* targetOrNull MOZ_GUARD_OBJECT_NOTIFIER_PARAM);
530
~JSAutoNullableRealm();
531
532
MOZ_DECL_USE_GUARD_OBJECT_NOTIFIER
533
};
534
535
namespace JS {
536
537
/** NB: This API is infallible; a nullptr return value does not indicate error.
538
*
539
* |target| must not be a cross-compartment wrapper because CCWs are not
540
* associated with a single realm.
541
*
542
* Entering a realm roots the realm and its global object until the matching
543
* JS::LeaveRealm() call.
544
*/
545
extern JS_PUBLIC_API JS::Realm* EnterRealm(JSContext* cx, JSObject* target);
546
547
extern JS_PUBLIC_API void LeaveRealm(JSContext* cx, JS::Realm* oldRealm);
548
549
using IterateRealmCallback = void (*)(JSContext* cx, void* data,
550
Handle<Realm*> realm);
551
552
/**
553
* This function calls |realmCallback| on every realm. Beware that there is no
554
* guarantee that the realm will survive after the callback returns. Also,
555
* barriers are disabled via the TraceSession.
556
*/
557
extern JS_PUBLIC_API void IterateRealms(JSContext* cx, void* data,
558
IterateRealmCallback realmCallback);
559
560
/**
561
* Like IterateRealms, but only call the callback for realms using |principals|.
562
*/
563
extern JS_PUBLIC_API void IterateRealmsWithPrincipals(
564
JSContext* cx, JSPrincipals* principals, void* data,
565
IterateRealmCallback realmCallback);
566
567
/**
568
* Like IterateRealms, but only iterates realms in |compartment|.
569
*/
570
extern JS_PUBLIC_API void IterateRealmsInCompartment(
571
JSContext* cx, JS::Compartment* compartment, void* data,
572
IterateRealmCallback realmCallback);
573
574
} // namespace JS
575
576
/**
577
* An enum that JSIterateCompartmentCallback can return to indicate
578
* whether to keep iterating.
579
*/
580
namespace JS {
581
enum class CompartmentIterResult { KeepGoing, Stop };
582
} // namespace JS
583
584
typedef JS::CompartmentIterResult (*JSIterateCompartmentCallback)(
585
JSContext* cx, void* data, JS::Compartment* compartment);
586
587
/**
588
* This function calls |compartmentCallback| on every compartment until either
589
* all compartments have been iterated or CompartmentIterResult::Stop is
590
* returned. Beware that there is no guarantee that the compartment will survive
591
* after the callback returns. Also, barriers are disabled via the TraceSession.
592
*/
593
extern JS_PUBLIC_API void JS_IterateCompartments(
594
JSContext* cx, void* data,
595
JSIterateCompartmentCallback compartmentCallback);
596
597
/**
598
* This function calls |compartmentCallback| on every compartment in the given
599
* zone until either all compartments have been iterated or
600
* CompartmentIterResult::Stop is returned. Beware that there is no guarantee
601
* that the compartment will survive after the callback returns. Also, barriers
602
* are disabled via the TraceSession.
603
*/
604
extern JS_PUBLIC_API void JS_IterateCompartmentsInZone(
605
JSContext* cx, JS::Zone* zone, void* data,
606
JSIterateCompartmentCallback compartmentCallback);
607
608
/**
609
* Mark a jsid after entering a new compartment. Different zones separately
610
* mark the ids in a runtime, and this must be used any time an id is obtained
611
* from one compartment and then used in another compartment, unless the two
612
* compartments are guaranteed to be in the same zone.
613
*/
614
extern JS_PUBLIC_API void JS_MarkCrossZoneId(JSContext* cx, jsid id);
615
616
/**
617
* If value stores a jsid (an atomized string or symbol), mark that id as for
618
* JS_MarkCrossZoneId.
619
*/
620
extern JS_PUBLIC_API void JS_MarkCrossZoneIdValue(JSContext* cx,
621
const JS::Value& value);
622
623
/**
624
* Resolve id, which must contain either a string or an int, to a standard
625
* class name in obj if possible, defining the class's constructor and/or
626
* prototype and storing true in *resolved. If id does not name a standard
627
* class or a top-level property induced by initializing a standard class,
628
* store false in *resolved and just return true. Return false on error,
629
* as usual for bool result-typed API entry points.
630
*
631
* This API can be called directly from a global object class's resolve op,
632
* to define standard classes lazily. The class should either have an enumerate
633
* hook that calls JS_EnumerateStandardClasses, or a newEnumerate hook that
634
* calls JS_NewEnumerateStandardClasses. newEnumerate is preferred because it's
635
* faster (does not define all standard classes).
636
*/
637
extern JS_PUBLIC_API bool JS_ResolveStandardClass(JSContext* cx,
638
JS::HandleObject obj,
639
JS::HandleId id,
640
bool* resolved);
641
642
extern JS_PUBLIC_API bool JS_MayResolveStandardClass(const JSAtomState& names,
643
jsid id,
644
JSObject* maybeObj);
645
646
extern JS_PUBLIC_API bool JS_EnumerateStandardClasses(JSContext* cx,
647
JS::HandleObject obj);
648
649
/**
650
* Fill "properties" with a list of standard class names that have not yet been
651
* resolved on "obj". This can be used as (part of) a newEnumerate class hook
652
* on a global. Already-resolved things are excluded because they might have
653
* been deleted by script after being resolved and enumeration considers
654
* already-defined properties anyway.
655
*/
656
extern JS_PUBLIC_API bool JS_NewEnumerateStandardClasses(
657
JSContext* cx, JS::HandleObject obj, JS::MutableHandleIdVector properties,
658
bool enumerableOnly);
659
660
/**
661
* Fill "properties" with a list of standard class names. This can be used for
662
* proxies that want to define behavior that looks like enumerating a global
663
* without touching the global itself.
664
*/
665
extern JS_PUBLIC_API bool JS_NewEnumerateStandardClassesIncludingResolved(
666
JSContext* cx, JS::HandleObject obj, JS::MutableHandleIdVector properties,
667
bool enumerableOnly);
668
669
extern JS_PUBLIC_API bool JS_GetClassObject(JSContext* cx, JSProtoKey key,
670
JS::MutableHandle<JSObject*> objp);
671
672
extern JS_PUBLIC_API bool JS_GetClassPrototype(
673
JSContext* cx, JSProtoKey key, JS::MutableHandle<JSObject*> objp);
674
675
namespace JS {
676
677
/*
678
* Determine if the given object is an instance/prototype/constructor for a
679
* standard class. If so, return the associated JSProtoKey. If not, return
680
* JSProto_Null.
681
*/
682
683
extern JS_PUBLIC_API JSProtoKey IdentifyStandardInstance(JSObject* obj);
684
685
extern JS_PUBLIC_API JSProtoKey IdentifyStandardPrototype(JSObject* obj);
686
687
extern JS_PUBLIC_API JSProtoKey
688
IdentifyStandardInstanceOrPrototype(JSObject* obj);
689
690
extern JS_PUBLIC_API JSProtoKey IdentifyStandardConstructor(JSObject* obj);
691
692
extern JS_PUBLIC_API void ProtoKeyToId(JSContext* cx, JSProtoKey key,
693
JS::MutableHandleId idp);
694
695
} /* namespace JS */
696
697
extern JS_PUBLIC_API JSProtoKey JS_IdToProtoKey(JSContext* cx, JS::HandleId id);
698
699
extern JS_PUBLIC_API bool JS_IsGlobalObject(JSObject* obj);
700
701
extern JS_PUBLIC_API JSObject* JS_GlobalLexicalEnvironment(JSObject* obj);
702
703
extern JS_PUBLIC_API bool JS_HasExtensibleLexicalEnvironment(JSObject* obj);
704
705
extern JS_PUBLIC_API JSObject* JS_ExtensibleLexicalEnvironment(JSObject* obj);
706
707
namespace JS {
708
709
/**
710
* Get the current realm's global. Returns nullptr if no realm has been
711
* entered.
712
*/
713
extern JS_PUBLIC_API JSObject* CurrentGlobalOrNull(JSContext* cx);
714
715
/**
716
* Get the global object associated with an object's realm. The object must not
717
* be a cross-compartment wrapper (because CCWs are shared by all realms in the
718
* compartment).
719
*/
720
extern JS_PUBLIC_API JSObject* GetNonCCWObjectGlobal(JSObject* obj);
721
722
} // namespace JS
723
724
/**
725
* Add 'Reflect.parse', a SpiderMonkey extension, to the Reflect object on the
726
* given global.
727
*/
728
extern JS_PUBLIC_API bool JS_InitReflectParse(JSContext* cx,
729
JS::HandleObject global);
730
731
/**
732
* Add various profiling-related functions as properties of the given object.
733
* Defined in builtin/Profilers.cpp.
734
*/
735
extern JS_PUBLIC_API bool JS_DefineProfilingFunctions(JSContext* cx,
736
JS::HandleObject obj);
737
738
/* Defined in vm/Debugger.cpp. */
739
extern JS_PUBLIC_API bool JS_DefineDebuggerObject(JSContext* cx,
740
JS::HandleObject obj);
741
742
namespace JS {
743
744
/**
745
* Tell JS engine whether Profile Timeline Recording is enabled or not.
746
* If Profile Timeline Recording is enabled, data shown there like stack won't
747
* be optimized out.
748
* This is global state and not associated with specific runtime or context.
749
*/
750
extern JS_PUBLIC_API void SetProfileTimelineRecordingEnabled(bool enabled);
751
752
extern JS_PUBLIC_API bool IsProfileTimelineRecordingEnabled();
753
754
} // namespace JS
755
756
#ifdef JS_HAS_CTYPES
757
/**
758
* Initialize the 'ctypes' object on a global variable 'obj'. The 'ctypes'
759
* object will be sealed.
760
*/
761
extern JS_PUBLIC_API bool JS_InitCTypesClass(JSContext* cx,
762
JS::HandleObject global);
763
764
/**
765
* Convert a unicode string 'source' of length 'slen' to the platform native
766
* charset, returning a null-terminated string allocated with JS_malloc. On
767
* failure, this function should report an error.
768
*/
769
typedef char* (*JSCTypesUnicodeToNativeFun)(JSContext* cx,
770
const char16_t* source,
771
size_t slen);
772
773
/**
774
* Set of function pointers that ctypes can use for various internal functions.
775
* See JS_SetCTypesCallbacks below. Providing nullptr for a function is safe,
776
* and will result in the applicable ctypes functionality not being available.
777
*/
778
struct JSCTypesCallbacks {
779
JSCTypesUnicodeToNativeFun unicodeToNative;
780
};
781
782
/**
783
* Set the callbacks on the provided 'ctypesObj' object. 'callbacks' should be a
784
* pointer to static data that exists for the lifetime of 'ctypesObj', but it
785
* may safely be altered after calling this function and without having
786
* to call this function again.
787
*/
788
extern JS_PUBLIC_API void JS_SetCTypesCallbacks(
789
JSObject* ctypesObj, const JSCTypesCallbacks* callbacks);
790
#endif
791
792
/*
793
* A replacement for MallocAllocPolicy that allocates in the JS heap and adds no
794
* extra behaviours.
795
*
796
* This is currently used for allocating source buffers for parsing. Since these
797
* are temporary and will not be freed by GC, the memory is not tracked by the
798
* usual accounting.
799
*/
800
class JS_PUBLIC_API JSMallocAllocPolicy : public js::AllocPolicyBase {
801
public:
802
void reportAllocOverflow() const {}
803
804
MOZ_MUST_USE bool checkSimulatedOOM() const { return true; }
805
};
806
807
/**
808
* Set the size of the native stack that should not be exceed. To disable
809
* stack size checking pass 0.
810
*
811
* SpiderMonkey allows for a distinction between system code (such as GCs, which
812
* may incidentally be triggered by script but are not strictly performed on
813
* behalf of such script), trusted script (as determined by
814
* JS_SetTrustedPrincipals), and untrusted script. Each kind of code may have a
815
* different stack quota, allowing embedders to keep higher-priority machinery
816
* running in the face of scripted stack exhaustion by something else.
817
*
818
* The stack quotas for each kind of code should be monotonically descending,
819
* and may be specified with this function. If 0 is passed for a given kind
820
* of code, it defaults to the value of the next-highest-priority kind.
821
*
822
* This function may only be called immediately after the runtime is initialized
823
* and before any code is executed and/or interrupts requested.
824
*/
825
extern JS_PUBLIC_API void JS_SetNativeStackQuota(
826
JSContext* cx, size_t systemCodeStackSize,
827
size_t trustedScriptStackSize = 0, size_t untrustedScriptStackSize = 0);
828
829
/************************************************************************/
830
831
extern JS_PUBLIC_API bool JS_ValueToId(JSContext* cx, JS::HandleValue v,
832
JS::MutableHandleId idp);
833
834
extern JS_PUBLIC_API bool JS_StringToId(JSContext* cx, JS::HandleString s,
835
JS::MutableHandleId idp);
836
837
extern JS_PUBLIC_API bool JS_IdToValue(JSContext* cx, jsid id,
838
JS::MutableHandle<JS::Value> vp);
839
840
namespace JS {
841
842
/**
843
* Convert obj to a primitive value. On success, store the result in vp and
844
* return true.
845
*
846
* The hint argument must be JSTYPE_STRING, JSTYPE_NUMBER, or
847
* JSTYPE_UNDEFINED (no hint).
848
*
849
* Implements: ES6 7.1.1 ToPrimitive(input, [PreferredType]).
850
*/
851
extern JS_PUBLIC_API bool ToPrimitive(JSContext* cx, JS::HandleObject obj,
852
JSType hint, JS::MutableHandleValue vp);
853
854
/**
855
* If args.get(0) is one of the strings "string", "number", or "default", set
856
* result to JSTYPE_STRING, JSTYPE_NUMBER, or JSTYPE_UNDEFINED accordingly and
857
* return true. Otherwise, return false with a TypeError pending.
858
*
859
* This can be useful in implementing a @@toPrimitive method.
860
*/
861
extern JS_PUBLIC_API bool GetFirstArgumentAsTypeHint(JSContext* cx,
862
CallArgs args,
863
JSType* result);
864
865
} /* namespace JS */
866
867
template <typename T>
868
struct JSConstScalarSpec {
869
const char* name;
870
T val;
871
};
872
873
using JSConstDoubleSpec = JSConstScalarSpec<double>;
874
using JSConstIntegerSpec = JSConstScalarSpec<int32_t>;
875
876
extern JS_PUBLIC_API JSObject* JS_InitClass(
877
JSContext* cx, JS::HandleObject obj, JS::HandleObject parent_proto,
878
const JSClass* clasp, JSNative constructor, unsigned nargs,
879
const JSPropertySpec* ps, const JSFunctionSpec* fs,
880
const JSPropertySpec* static_ps, const JSFunctionSpec* static_fs);
881
882
/**
883
* Set up ctor.prototype = proto and proto.constructor = ctor with the
884
* right property flags.
885
*/
886
extern JS_PUBLIC_API bool JS_LinkConstructorAndPrototype(
887
JSContext* cx, JS::Handle<JSObject*> ctor, JS::Handle<JSObject*> proto);
888
889
extern JS_PUBLIC_API const JSClass* JS_GetClass(JSObject* obj);
890
891
extern JS_PUBLIC_API bool JS_InstanceOf(JSContext* cx,
892
JS::Handle<JSObject*> obj,
893
const JSClass* clasp,
894
JS::CallArgs* args);
895
896
extern JS_PUBLIC_API bool JS_HasInstance(JSContext* cx,
897
JS::Handle<JSObject*> obj,
898
JS::Handle<JS::Value> v, bool* bp);
899
900
namespace JS {
901
902
// Implementation of
904
// you're looking for the equivalent of "instanceof", you want JS_HasInstance,
905
// not this function.
906
extern JS_PUBLIC_API bool OrdinaryHasInstance(JSContext* cx,
907
HandleObject objArg,
908
HandleValue v, bool* bp);
909
910
// Implementation of
912
// This is almost identical to JS_HasInstance, except the latter may call a
913
// custom hasInstance class op instead of InstanceofOperator.
914
extern JS_PUBLIC_API bool InstanceofOperator(JSContext* cx, HandleObject obj,
915
HandleValue v, bool* bp);
916
917
} // namespace JS
918
919
extern JS_PUBLIC_API void* JS_GetPrivate(JSObject* obj);
920
921
extern JS_PUBLIC_API void JS_SetPrivate(JSObject* obj, void* data);
922
923
extern JS_PUBLIC_API void JS_InitPrivate(JSObject* obj, void* data,
924
size_t nbytes, JS::MemoryUse use);
925
926
extern JS_PUBLIC_API void* JS_GetInstancePrivate(JSContext* cx,
927
JS::Handle<JSObject*> obj,
928
const JSClass* clasp,
929
JS::CallArgs* args);
930
931
extern JS_PUBLIC_API JSObject* JS_GetConstructor(JSContext* cx,
932
JS::Handle<JSObject*> proto);
933
934
namespace JS {
935
936
/**
937
* During global creation, we fire notifications to callbacks registered
938
* via the Debugger API. These callbacks are arbitrary script, and can touch
939
* the global in arbitrary ways. When that happens, the global should not be
940
* in a half-baked state. But this creates a problem for consumers that need
941
* to set slots on the global to put it in a consistent state.
942
*
943
* This API provides a way for consumers to set slots atomically (immediately
944
* after the global is created), before any debugger hooks are fired. It's
945
* unfortunately on the clunky side, but that's the way the cookie crumbles.
946
*
947
* If callers have no additional state on the global to set up, they may pass
948
* |FireOnNewGlobalHook| to JS_NewGlobalObject, which causes that function to
949
* fire the hook as its final act before returning. Otherwise, callers should
950
* pass |DontFireOnNewGlobalHook|, which means that they are responsible for
951
* invoking JS_FireOnNewGlobalObject upon successfully creating the global. If
952
* an error occurs and the operation aborts, callers should skip firing the
953
* hook. But otherwise, callers must take care to fire the hook exactly once
954
* before compiling any script in the global's scope (we have assertions in
955
* place to enforce this). This lets us be sure that debugger clients never miss
956
* breakpoints.
957
*/
958
enum OnNewGlobalHookOption { FireOnNewGlobalHook, DontFireOnNewGlobalHook };
959
960
} /* namespace JS */
961
962
extern JS_PUBLIC_API JSObject* JS_NewGlobalObject(
963
JSContext* cx, const JSClass* clasp, JSPrincipals* principals,
964
JS::OnNewGlobalHookOption hookOption, const JS::RealmOptions& options);
965
/**
966
* Spidermonkey does not have a good way of keeping track of what compartments
967
* should be marked on their own. We can mark the roots unconditionally, but
968
* marking GC things only relevant in live compartments is hard. To mitigate
969
* this, we create a static trace hook, installed on each global object, from
970
* which we can be sure the compartment is relevant, and mark it.
971
*
972
* It is still possible to specify custom trace hooks for global object classes.
973
* They can be provided via the RealmOptions passed to JS_NewGlobalObject.
974
*/
975
extern JS_PUBLIC_API void JS_GlobalObjectTraceHook(JSTracer* trc,
976
JSObject* global);
977
978
namespace JS {
979
980
/**
981
* This allows easily constructing a global object without having to deal with
982
* JSClassOps, forgetting to add JS_GlobalObjectTraceHook, or forgetting to call
983
* JS::InitRealmStandardClasses(). Example:
984
*
985
* const JSClass globalClass = { "MyGlobal", JSCLASS_GLOBAL_FLAGS,
986
* &JS::DefaultGlobalClassOps };
987
* JS_NewGlobalObject(cx, &globalClass, ...);
988
*/
989
extern JS_PUBLIC_DATA const JSClassOps DefaultGlobalClassOps;
990
991
} // namespace JS
992
993
extern JS_PUBLIC_API void JS_FireOnNewGlobalObject(JSContext* cx,
994
JS::HandleObject global);
995
996
extern JS_PUBLIC_API JSObject* JS_NewObject(JSContext* cx,
997
const JSClass* clasp);
998
999
extern JS_PUBLIC_API bool JS_IsNative(JSObject* obj);
1000
1001
/**
1002
* Unlike JS_NewObject, JS_NewObjectWithGivenProto does not compute a default
1003
* proto. If proto is nullptr, the JS object will have `null` as [[Prototype]].
1004
*/
1005
extern JS_PUBLIC_API JSObject* JS_NewObjectWithGivenProto(
1006
JSContext* cx, const JSClass* clasp, JS::Handle<JSObject*> proto);
1007
1008
/**
1009
* Creates a new plain object, like `new Object()`, with Object.prototype as
1010
* [[Prototype]].
1011
*/
1012
extern JS_PUBLIC_API JSObject* JS_NewPlainObject(JSContext* cx);
1013
1014
/**
1015
* Freeze obj, and all objects it refers to, recursively. This will not recurse
1016
* through non-extensible objects, on the assumption that those are already
1017
* deep-frozen.
1018
*/
1019
extern JS_PUBLIC_API bool JS_DeepFreezeObject(JSContext* cx,
1020
JS::Handle<JSObject*> obj);
1021
1022
/**
1023
* Freezes an object; see ES5's Object.freeze(obj) method.
1024
*/
1025
extern JS_PUBLIC_API bool JS_FreezeObject(JSContext* cx,
1026
JS::Handle<JSObject*> obj);
1027
1028
/*** Standard internal methods **********************************************
1029
*
1030
* The functions below are the fundamental operations on objects.
1031
*
1032
* ES6 specifies 14 internal methods that define how objects behave. The
1033
* standard is actually quite good on this topic, though you may have to read
1034
* it a few times. See ES6 sections 6.1.7.2 and 6.1.7.3.
1035
*
1036
* When 'obj' is an ordinary object, these functions have boring standard
1037
* behavior as specified by ES6 section 9.1; see the section about internal
1038
* methods in js/src/vm/NativeObject.h.
1039
*
1040
* Proxies override the behavior of internal methods. So when 'obj' is a proxy,
1041
* any one of the functions below could do just about anything. See
1042
* js/public/Proxy.h.
1043
*/
1044
1045
/**
1046
* Get the prototype of obj, storing it in result.
1047
*
1048
* Implements: ES6 [[GetPrototypeOf]] internal method.
1049
*/
1050
extern JS_PUBLIC_API bool JS_GetPrototype(JSContext* cx, JS::HandleObject obj,
1051
JS::MutableHandleObject result);
1052
1053
/**
1054
* If |obj| (underneath any functionally-transparent wrapper proxies) has as
1055
* its [[GetPrototypeOf]] trap the ordinary [[GetPrototypeOf]] behavior defined
1056
* for ordinary objects, set |*isOrdinary = true| and store |obj|'s prototype
1057
* in |result|. Otherwise set |*isOrdinary = false|. In case of error, both
1058
* outparams have unspecified value.
1059
*/
1060
extern JS_PUBLIC_API bool JS_GetPrototypeIfOrdinary(
1061
JSContext* cx, JS::HandleObject obj, bool* isOrdinary,
1062
JS::MutableHandleObject result);
1063
1064
/**
1065
* Change the prototype of obj.
1066
*
1067
* Implements: ES6 [[SetPrototypeOf]] internal method.
1068
*
1069
* In cases where ES6 [[SetPrototypeOf]] returns false without an exception,
1070
* JS_SetPrototype throws a TypeError and returns false.
1071
*
1072
* Performance warning: JS_SetPrototype is very bad for performance. It may
1073
* cause compiled jit-code to be invalidated. It also causes not only obj but
1074
* all other objects in the same "group" as obj to be permanently deoptimized.
1075
* It's better to create the object with the right prototype from the start.
1076
*/
1077
extern JS_PUBLIC_API bool JS_SetPrototype(JSContext* cx, JS::HandleObject obj,
1078
JS::HandleObject proto);
1079
1080
/**
1081
* Determine whether obj is extensible. Extensible objects can have new
1082
* properties defined on them. Inextensible objects can't, and their
1083
* [[Prototype]] slot is fixed as well.
1084
*
1085
* Implements: ES6 [[IsExtensible]] internal method.
1086
*/
1087
extern JS_PUBLIC_API bool JS_IsExtensible(JSContext* cx, JS::HandleObject obj,
1088
bool* extensible);
1089
1090
/**
1091
* Attempt to make |obj| non-extensible.
1092
*
1093
* Not all failures are treated as errors. See the comment on
1094
* JS::ObjectOpResult in js/public/Class.h.
1095
*
1096
* Implements: ES6 [[PreventExtensions]] internal method.
1097
*/
1098
extern JS_PUBLIC_API bool JS_PreventExtensions(JSContext* cx,
1099
JS::HandleObject obj,
1100
JS::ObjectOpResult& result);
1101
1102
/**
1103
* Attempt to make the [[Prototype]] of |obj| immutable, such that any attempt
1104
* to modify it will fail. If an error occurs during the attempt, return false
1105
* (with a pending exception set, depending upon the nature of the error). If
1106
* no error occurs, return true with |*succeeded| set to indicate whether the
1107
* attempt successfully made the [[Prototype]] immutable.
1108
*
1109
* This is a nonstandard internal method.
1110
*/
1111
extern JS_PUBLIC_API bool JS_SetImmutablePrototype(JSContext* cx,
1112
JS::HandleObject obj,
1113
bool* succeeded);
1114
1115
/**
1116
* Get a description of one of obj's own properties. If no such property exists
1117
* on obj, return true with desc.object() set to null.
1118
*
1119
* Implements: ES6 [[GetOwnProperty]] internal method.
1120
*/
1121
extern JS_PUBLIC_API bool JS_GetOwnPropertyDescriptorById(
1122
JSContext* cx, JS::HandleObject obj, JS::HandleId id,
1123
JS::MutableHandle<JS::PropertyDescriptor> desc);
1124
1125
extern JS_PUBLIC_API bool JS_GetOwnPropertyDescriptor(
1126
JSContext* cx, JS::HandleObject obj, const char* name,
1127
JS::MutableHandle<JS::PropertyDescriptor> desc);
1128
1129
extern JS_PUBLIC_API bool JS_GetOwnUCPropertyDescriptor(
1130
JSContext* cx, JS::HandleObject obj, const char16_t* name, size_t namelen,
1131
JS::MutableHandle<JS::PropertyDescriptor> desc);
1132
1133
/**
1134
* Like JS_GetOwnPropertyDescriptorById, but also searches the prototype chain
1135
* if no own property is found directly on obj. The object on which the
1136
* property is found is returned in desc.object(). If the property is not found
1137
* on the prototype chain, this returns true with desc.object() set to null.
1138
*/
1139
extern JS_PUBLIC_API bool JS_GetPropertyDescriptorById(
1140
JSContext* cx, JS::HandleObject obj, JS::HandleId id,
1141
JS::MutableHandle<JS::PropertyDescriptor> desc);
1142
1143
extern JS_PUBLIC_API bool JS_GetPropertyDescriptor(
1144
JSContext* cx, JS::HandleObject obj, const char* name,
1145
JS::MutableHandle<JS::PropertyDescriptor> desc);
1146
1147
extern JS_PUBLIC_API bool JS_GetUCPropertyDescriptor(
1148
JSContext* cx, JS::HandleObject obj, const char16_t* name, size_t namelen,
1149
JS::MutableHandle<JS::PropertyDescriptor> desc);
1150
1151
/**
1152
* Define a property on obj.
1153
*
1154
* This function uses JS::ObjectOpResult to indicate conditions that ES6
1155
* specifies as non-error failures. This is inconvenient at best, so use this
1156
* function only if you are implementing a proxy handler's defineProperty()
1157
* method. For all other purposes, use one of the many DefineProperty functions
1158
* below that throw an exception in all failure cases.
1159
*
1160
* Implements: ES6 [[DefineOwnProperty]] internal method.
1161
*/
1162
extern JS_PUBLIC_API bool JS_DefinePropertyById(
1163
JSContext* cx, JS::HandleObject obj, JS::HandleId id,
1164
JS::Handle<JS::PropertyDescriptor> desc, JS::ObjectOpResult& result);
1165
1166
/**
1167
* Define a property on obj, throwing a TypeError if the attempt fails.
1168
* This is the C++ equivalent of `Object.defineProperty(obj, id, desc)`.
1169
*/
1170
extern JS_PUBLIC_API bool JS_DefinePropertyById(
1171
JSContext* cx, JS::HandleObject obj, JS::HandleId id,
1172
JS::Handle<JS::PropertyDescriptor> desc);
1173
1174
extern JS_PUBLIC_API bool JS_DefinePropertyById(JSContext* cx,
1175
JS::HandleObject obj,
1176
JS::HandleId id,
1177
JS::HandleValue value,
1178
unsigned attrs);
1179
1180
extern JS_PUBLIC_API bool JS_DefinePropertyById(
1181
JSContext* cx, JS::HandleObject obj, JS::HandleId id, JSNative getter,
1182
JSNative setter, unsigned attrs);
1183
1184
extern JS_PUBLIC_API bool JS_DefinePropertyById(
1185
JSContext* cx, JS::HandleObject obj, JS::HandleId id,
1186
JS::HandleObject getter, JS::HandleObject setter, unsigned attrs);
1187
1188
extern JS_PUBLIC_API bool JS_DefinePropertyById(JSContext* cx,
1189
JS::HandleObject obj,
1190
JS::HandleId id,
1191
JS::HandleObject value,
1192
unsigned attrs);
1193
1194
extern JS_PUBLIC_API bool JS_DefinePropertyById(JSContext* cx,
1195
JS::HandleObject obj,
1196
JS::HandleId id,
1197
JS::HandleString value,
1198
unsigned attrs);
1199
1200
extern JS_PUBLIC_API bool JS_DefinePropertyById(JSContext* cx,
1201
JS::HandleObject obj,
1202
JS::HandleId id, int32_t value,
1203
unsigned attrs);
1204
1205
extern JS_PUBLIC_API bool JS_DefinePropertyById(JSContext* cx,
1206
JS::HandleObject obj,
1207
JS::HandleId id, uint32_t value,
1208
unsigned attrs);
1209
1210
extern JS_PUBLIC_API bool JS_DefinePropertyById(JSContext* cx,
1211
JS::HandleObject obj,
1212
JS::HandleId id, double value,
1213
unsigned attrs);
1214
1215
extern JS_PUBLIC_API bool JS_DefineProperty(JSContext* cx, JS::HandleObject obj,
1216
const char* name,
1217
JS::HandleValue value,
1218
unsigned attrs);
1219
1220
extern JS_PUBLIC_API bool JS_DefineProperty(JSContext* cx, JS::HandleObject obj,
1221
const char* name, JSNative getter,
1222
JSNative setter, unsigned attrs);
1223
1224
extern JS_PUBLIC_API bool JS_DefineProperty(JSContext* cx, JS::HandleObject obj,
1225
const char* name,
1226
JS::HandleObject getter,
1227
JS::HandleObject setter,
1228
unsigned attrs);
1229
1230
extern JS_PUBLIC_API bool JS_DefineProperty(JSContext* cx, JS::HandleObject obj,
1231
const char* name,
1232
JS::HandleObject value,
1233
unsigned attrs);
1234
1235
extern JS_PUBLIC_API bool JS_DefineProperty(JSContext* cx, JS::HandleObject obj,
1236
const char* name,
1237
JS::HandleString value,
1238
unsigned attrs);
1239
1240
extern JS_PUBLIC_API bool JS_DefineProperty(JSContext* cx, JS::HandleObject obj,
1241
const char* name, int32_t value,
1242
unsigned attrs);
1243
1244
extern JS_PUBLIC_API bool JS_DefineProperty(JSContext* cx, JS::HandleObject obj,
1245
const char* name, uint32_t value,
1246
unsigned attrs);
1247
1248
extern JS_PUBLIC_API bool JS_DefineProperty(JSContext* cx, JS::HandleObject obj,
1249
const char* name, double value,
1250
unsigned attrs);
1251
1252
extern JS_PUBLIC_API bool JS_DefineUCProperty(
1253
JSContext* cx, JS::HandleObject obj, const char16_t* name, size_t namelen,
1254
JS::Handle<JS::PropertyDescriptor> desc, JS::ObjectOpResult& result);
1255
1256
extern JS_PUBLIC_API bool JS_DefineUCProperty(
1257
JSContext* cx, JS::HandleObject obj, const char16_t* name, size_t namelen,
1258
JS::Handle<JS::PropertyDescriptor> desc);
1259
1260
extern JS_PUBLIC_API bool JS_DefineUCProperty(
1261
JSContext* cx, JS::HandleObject obj, const char16_t* name, size_t namelen,
1262
JS::HandleValue value, unsigned attrs);
1263
1264
extern JS_PUBLIC_API bool JS_DefineUCProperty(
1265
JSContext* cx, JS::HandleObject obj, const char16_t* name, size_t namelen,
1266
JS::HandleObject getter, JS::HandleObject setter, unsigned attrs);
1267
1268
extern JS_PUBLIC_API bool JS_DefineUCProperty(
1269
JSContext* cx, JS::HandleObject obj, const char16_t* name, size_t namelen,
1270
JS::HandleObject value, unsigned attrs);
1271
1272
extern JS_PUBLIC_API bool JS_DefineUCProperty(
1273
JSContext* cx, JS::HandleObject obj, const char16_t* name, size_t namelen,
1274
JS::HandleString value, unsigned attrs);
1275
1276
extern JS_PUBLIC_API bool JS_DefineUCProperty(JSContext* cx,
1277
JS::HandleObject obj,
1278
const char16_t* name,
1279
size_t namelen, int32_t value,
1280
unsigned attrs);
1281
1282
extern JS_PUBLIC_API bool JS_DefineUCProperty(JSContext* cx,
1283
JS::HandleObject obj,
1284
const char16_t* name,
1285
size_t namelen, uint32_t value,
1286
unsigned attrs);
1287
1288
extern JS_PUBLIC_API bool JS_DefineUCProperty(JSContext* cx,
1289
JS::HandleObject obj,
1290
const char16_t* name,
1291
size_t namelen, double value,
1292
unsigned attrs);
1293
1294
extern JS_PUBLIC_API bool JS_DefineElement(JSContext* cx, JS::HandleObject obj,
1295
uint32_t index,
1296
JS::HandleValue value,
1297
unsigned attrs);
1298
1299
extern JS_PUBLIC_API bool JS_DefineElement(JSContext* cx, JS::HandleObject obj,
1300
uint32_t index,
1301
JS::HandleObject getter,
1302
JS::HandleObject setter,
1303
unsigned attrs);
1304
1305
extern JS_PUBLIC_API bool JS_DefineElement(JSContext* cx, JS::HandleObject obj,
1306
uint32_t index,
1307
JS::HandleObject value,
1308
unsigned attrs);
1309
1310
extern JS_PUBLIC_API bool JS_DefineElement(JSContext* cx, JS::HandleObject obj,
1311
uint32_t index,
1312
JS::HandleString value,
1313
unsigned attrs);
1314
1315
extern JS_PUBLIC_API bool JS_DefineElement(JSContext* cx, JS::HandleObject obj,
1316
uint32_t index, int32_t value,
1317
unsigned attrs);
1318
1319
extern JS_PUBLIC_API bool JS_DefineElement(JSContext* cx, JS::HandleObject obj,
1320
uint32_t index, uint32_t value,
1321
unsigned attrs);
1322
1323
extern JS_PUBLIC_API bool JS_DefineElement(JSContext* cx, JS::HandleObject obj,
1324
uint32_t index, double value,
1325
unsigned attrs);
1326
1327
/**
1328
* Compute the expression `id in obj`.
1329
*
1330
* If obj has an own or inherited property obj[id], set *foundp = true and
1331
* return true. If not, set *foundp = false and return true. On error, return
1332
* false with an exception pending.
1333
*
1334
* Implements: ES6 [[Has]] internal method.
1335
*/
1336
extern JS_PUBLIC_API bool JS_HasPropertyById(JSContext* cx,
1337
JS::HandleObject obj,
1338
JS::HandleId id, bool* foundp);
1339
1340
extern JS_PUBLIC_API bool JS_HasProperty(JSContext* cx, JS::HandleObject obj,
1341
const char* name, bool* foundp);
1342
1343
extern JS_PUBLIC_API bool JS_HasUCProperty(JSContext* cx, JS::HandleObject obj,
1344
const char16_t* name, size_t namelen,
1345
bool* vp);
1346
1347
extern JS_PUBLIC_API bool JS_HasElement(JSContext* cx, JS::HandleObject obj,
1348
uint32_t index, bool* foundp);
1349
1350
/**
1351
* Determine whether obj has an own property with the key `id`.
1352
*
1353
* Implements: ES6 7.3.11 HasOwnProperty(O, P).
1354
*/
1355
extern JS_PUBLIC_API bool JS_HasOwnPropertyById(JSContext* cx,
1356
JS::HandleObject obj,
1357
JS::HandleId id, bool* foundp);
1358
1359
extern JS_PUBLIC_API bool JS_HasOwnProperty(JSContext* cx, JS::HandleObject obj,
1360
const char* name, bool* foundp);
1361
1362
/**
1363
* Get the value of the property `obj[id]`, or undefined if no such property
1364
* exists. This is the C++ equivalent of `vp = Reflect.get(obj, id, receiver)`.
1365
*
1366
* Most callers don't need the `receiver` argument. Consider using
1367
* JS_GetProperty instead. (But if you're implementing a proxy handler's set()
1368
* method, it's often correct to call this function and pass the receiver
1369
* through.)
1370
*
1371
* Implements: ES6 [[Get]] internal method.
1372
*/
1373
extern JS_PUBLIC_API bool JS_ForwardGetPropertyTo(JSContext* cx,
1374
JS::HandleObject obj,
1375
JS::HandleId id,
1376
JS::HandleValue receiver,
1377
JS::MutableHandleValue vp);
1378
1379
extern JS_PUBLIC_API bool JS_ForwardGetElementTo(JSContext* cx,
1380
JS::HandleObject obj,
1381
uint32_t index,
1382
JS::HandleObject receiver,
1383
JS::MutableHandleValue vp);
1384
1385
/**
1386
* Get the value of the property `obj[id]`, or undefined if no such property
1387
* exists. The result is stored in vp.
1388
*
1389
* Implements: ES6 7.3.1 Get(O, P).
1390
*/
1391
extern JS_PUBLIC_API bool JS_GetPropertyById(JSContext* cx,
1392
JS::HandleObject obj,
1393
JS::HandleId id,
1394
JS::MutableHandleValue vp);
1395
1396
extern JS_PUBLIC_API bool JS_GetProperty(JSContext* cx, JS::HandleObject obj,
1397
const char* name,
1398
JS::MutableHandleValue vp);
1399
1400
extern JS_PUBLIC_API bool JS_GetUCProperty(JSContext* cx, JS::HandleObject obj,
1401
const char16_t* name, size_t namelen,
1402
JS::MutableHandleValue vp);
1403
1404
extern JS_PUBLIC_API bool JS_GetElement(JSContext* cx, JS::HandleObject obj,
1405
uint32_t index,
1406
JS::MutableHandleValue vp);
1407
1408
/**
1409
* Perform the same property assignment as `Reflect.set(obj, id, v, receiver)`.
1410
*
1411
* This function has a `receiver` argument that most callers don't need.
1412
* Consider using JS_SetProperty instead.
1413
*
1414
* Implements: ES6 [[Set]] internal method.
1415
*/
1416
extern JS_PUBLIC_API bool JS_ForwardSetPropertyTo(
1417
JSContext* cx, JS::HandleObject obj, JS::HandleId id, JS::HandleValue v,
1418
JS::HandleValue receiver, JS::ObjectOpResult& result);
1419
1420
/**
1421
* Perform the assignment `obj[id] = v`.
1422
*
1423
* This function performs non-strict assignment, so if the property is
1424
* read-only, nothing happens and no error is thrown.
1425
*/
1426
extern JS_PUBLIC_API bool JS_SetPropertyById(JSContext* cx,
1427
JS::HandleObject obj,
1428
JS::HandleId id,
1429
JS::HandleValue v);
1430
1431
extern JS_PUBLIC_API bool JS_SetProperty(JSContext* cx, JS::HandleObject obj,
1432
const char* name, JS::HandleValue v);
1433
1434
extern JS_PUBLIC_API bool JS_SetUCProperty(JSContext* cx, JS::HandleObject obj,
1435
const char16_t* name, size_t namelen,
1436
JS::HandleValue v);
1437
1438
extern JS_PUBLIC_API bool JS_SetElement(JSContext* cx, JS::HandleObject obj,
1439
uint32_t index, JS::HandleValue v);
1440
1441
extern JS_PUBLIC_API bool JS_SetElement(JSContext* cx, JS::HandleObject obj,
1442
uint32_t index, JS::HandleObject v);
1443
1444
extern JS_PUBLIC_API bool JS_SetElement(JSContext* cx, JS::HandleObject obj,
1445
uint32_t index, JS::HandleString v);
1446
1447
extern JS_PUBLIC_API bool JS_SetElement(JSContext* cx, JS::HandleObject obj,
1448
uint32_t index, int32_t v);
1449
1450
extern JS_PUBLIC_API bool JS_SetElement(JSContext* cx, JS::HandleObject obj,
1451
uint32_t index, uint32_t v);
1452
1453
extern JS_PUBLIC_API bool JS_SetElement(JSContext* cx, JS::HandleObject obj,
1454
uint32_t index, double v);
1455
1456
/**
1457
* Delete a property. This is the C++ equivalent of
1458
* `result = Reflect.deleteProperty(obj, id)`.
1459
*
1460
* This function has a `result` out parameter that most callers don't need.
1461
* Unless you can pass through an ObjectOpResult provided by your caller, it's
1462
* probably best to use the JS_DeletePropertyById signature with just 3
1463
* arguments.
1464
*
1465
* Implements: ES6 [[Delete]] internal method.
1466
*/
1467
extern JS_PUBLIC_API bool JS_DeletePropertyById(JSContext* cx,
1468
JS::HandleObject obj,
1469
JS::HandleId id,
1470
JS::ObjectOpResult& result);
1471
1472
extern JS_PUBLIC_API bool JS_DeleteProperty(JSContext* cx, JS::HandleObject obj,
1473
const char* name,
1474
JS::ObjectOpResult& result);
1475
1476
extern JS_PUBLIC_API bool JS_DeleteUCProperty(JSContext* cx,
1477
JS::HandleObject obj,
1478
const char16_t* name,
1479
size_t namelen,
1480
JS::ObjectOpResult& result);
1481
1482
extern JS_PUBLIC_API bool JS_DeleteElement(JSContext* cx, JS::HandleObject obj,
1483
uint32_t index,
1484
JS::ObjectOpResult& result);
1485
1486
/**
1487
* Delete a property, ignoring strict failures. This is the C++ equivalent of
1488
* the JS `delete obj[id]` in non-strict mode code.
1489
*/
1490
extern JS_PUBLIC_API bool JS_DeletePropertyById(JSContext* cx,
1491
JS::HandleObject obj, jsid id);
1492
1493
extern JS_PUBLIC_API bool JS_DeleteProperty(JSContext* cx, JS::HandleObject obj,
1494
const char* name);
1495
1496
extern JS_PUBLIC_API bool JS_DeleteElement(JSContext* cx, JS::HandleObject obj,
1497
uint32_t index);
1498
1499
/**
1500
* Get an array of the non-symbol enumerable properties of obj.
1501
* This function is roughly equivalent to:
1502
*
1503
* var result = [];
1504
* for (key in obj) {
1505
* result.push(key);
1506
* }
1507
* return result;
1508
*
1509
* This is the closest thing we currently have to the ES6 [[Enumerate]]
1510
* internal method.
1511
*
1512
* The array of ids returned by JS_Enumerate must be rooted to protect its
1513
* contents from garbage collection. Use JS::Rooted<JS::IdVector>.
1514
*/
1515
extern JS_PUBLIC_API bool JS_Enumerate(JSContext* cx, JS::HandleObject obj,
1516
JS::MutableHandle<JS::IdVector> props);
1517
1518
/**
1519
* Equivalent to `Object.assign(target, src)`: Copies the properties from the
1520
* `src` object (which must not be null) to `target` (which also must not be
1521
* null).
1522
*/
1523
extern JS_PUBLIC_API bool JS_AssignObject(JSContext* cx,
1524
JS::HandleObject target,
1525
JS::HandleObject src);
1526
1527
/*
1528
* API for determining callability and constructability. [[Call]] and
1529
* [[Construct]] are internal methods that aren't present on all objects, so it
1530
* is useful to ask if they are there or not. The standard itself asks these
1531
* questions routinely.
1532
*/
1533
namespace JS {
1534
1535
/**
1536
* Return true if the given object is callable. In ES6 terms, an object is
1537
* callable if it has a [[Call]] internal method.
1538
*
1539
* Implements: ES6 7.2.3 IsCallable(argument).
1540
*
1541
* Functions are callable. A scripted proxy or wrapper is callable if its
1542
* target is callable. Most other objects aren't callable.
1543
*/
1544
extern JS_PUBLIC_API bool IsCallable(JSObject* obj);
1545
1546
/**
1547
* Return true if the given object is a constructor. In ES6 terms, an object is
1548
* a constructor if it has a [[Construct]] internal method. The expression
1549
* `new obj()` throws a TypeError if obj is not a constructor.
1550
*
1551
* Implements: ES6 7.2.4 IsConstructor(argument).
1552
*
1553
* JS functions and classes are constructors. Arrow functions and most builtin
1554
* functions are not. A scripted proxy or wrapper is a constructor if its
1555
* target is a constructor.
1556
*/
1557
extern JS_PUBLIC_API bool IsConstructor(JSObject* obj);
1558
1559
} /* namespace JS */
1560
1561
/**
1562
* Call a function, passing a this-value and arguments. This is the C++
1563
* equivalent of `rval = Reflect.apply(fun, obj, args)`.
1564
*
1565
* Implements: ES6 7.3.12 Call(F, V, [argumentsList]).
1566
* Use this function to invoke the [[Call]] internal method.
1567
*/
1568
extern JS_PUBLIC_API bool JS_CallFunctionValue(JSContext* cx,
1569
JS::HandleObject obj,
1570
JS::HandleValue fval,
1571
const JS::HandleValueArray& args,
1572
JS::MutableHandleValue rval);
1573
1574
extern JS_PUBLIC_API bool JS_CallFunction(JSContext* cx, JS::HandleObject obj,
1575
JS::HandleFunction fun,
1576
const JS::HandleValueArray& args,
1577
JS::MutableHandleValue rval);
1578
1579
/**
1580
* Perform the method call `rval = obj[name](args)`.
1581
*/
1582
extern JS_PUBLIC_API bool JS_CallFunctionName(JSContext* cx,
1583
JS::HandleObject obj,
1584
const char* name,
1585
const JS::HandleValueArray& args,
1586
JS::MutableHandleValue rval);
1587
1588
namespace JS {
1589
1590
static inline bool Call(JSContext* cx, JS::HandleObject thisObj,
1591
JS::HandleFunction fun,
1592
const JS::HandleValueArray& args,
1593
MutableHandleValue rval) {
1594
return !!JS_CallFunction(cx, thisObj, fun, args, rval);
1595
}
1596
1597
static inline bool Call(JSContext* cx, JS::HandleObject thisObj,
1598
JS::HandleValue fun, const JS::HandleValueArray& args,
1599
MutableHandleValue rval) {
1600
return !!JS_CallFunctionValue(cx, thisObj, fun, args, rval);
1601
}
1602
1603
static inline bool Call(JSContext* cx, JS::HandleObject thisObj,
1604
const char* name, const JS::HandleValueArray& args,
1605
MutableHandleValue rval) {
1606
return !!JS_CallFunctionName(cx, thisObj, name, args, rval);
1607
}
1608
1609
extern JS_PUBLIC_API bool Call(JSContext* cx, JS::HandleValue thisv,
1610
JS::HandleValue fun,
1611
const JS::HandleValueArray& args,
1612
MutableHandleValue rval);
1613
1614
static inline bool Call(JSContext* cx, JS::HandleValue thisv,
1615
JS::HandleObject funObj,
1616
const JS::HandleValueArray& args,
1617
MutableHandleValue rval) {
1618
MOZ_ASSERT(funObj);
1619
JS::RootedValue fun(cx, JS::ObjectValue(*funObj));
1620
return Call(cx, thisv, fun, args, rval);
1621
}
1622
1623
/**
1624
* Invoke a constructor. This is the C++ equivalent of
1625
* `rval = Reflect.construct(fun, args, newTarget)`.
1626
*
1627
* JS::Construct() takes a `newTarget` argument that most callers don't need.
1628
* Consider using the four-argument Construct signature instead. (But if you're
1629
* implementing a subclass or a proxy handler's construct() method, this is the
1630
* right function to call.)
1631
*
1632
* Implements: ES6 7.3.13 Construct(F, [argumentsList], [newTarget]).
1633
* Use this function to invoke the [[Construct]] internal method.
1634
*/
1635
extern JS_PUBLIC_API bool Construct(JSContext* cx, JS::HandleValue fun,
1636
HandleObject newTarget,
1637
const JS::HandleValueArray& args,
1638
MutableHandleObject objp);
1639
1640
/**
1641
* Invoke a constructor. This is the C++ equivalent of
1642
* `rval = new fun(...args)`.
1643
*
1644
* Implements: ES6 7.3.13 Construct(F, [argumentsList], [newTarget]), when
1645
* newTarget is omitted.
1646
*/
1647
extern JS_PUBLIC_API bool Construct(JSContext* cx, JS::HandleValue fun,
1648
const JS::HandleValueArray& args,
1649
MutableHandleObject objp);
1650
1651
} /* namespace JS */
1652
1653
/**
1654
* Invoke a constructor, like the JS expression `new ctor(...args)`. Returns
1655
* the new object, or null on error.
1656
*/
1657
extern JS_PUBLIC_API JSObject* JS_New(JSContext* cx, JS::HandleObject ctor,
1658
const JS::HandleValueArray& args);
1659
1660
/*** Other property-defining functions **************************************/
1661
1662
extern JS_PUBLIC_API JSObject* JS_DefineObject(JSContext* cx,
1663
JS::HandleObject obj,
1664
const char* name,
1665
const JSClass* clasp = nullptr,
1666
unsigned attrs = 0);
1667
1668
extern JS_PUBLIC_API bool JS_DefineConstDoubles(JSContext* cx,
1669
JS::HandleObject obj,
1670
const JSConstDoubleSpec* cds);
1671
1672
extern JS_PUBLIC_API bool JS_DefineConstIntegers(JSContext* cx,
1673
JS::HandleObject obj,
1674
const JSConstIntegerSpec* cis);
1675
1676
extern JS_PUBLIC_API bool JS_DefineProperties(JSContext* cx,
1677
JS::HandleObject obj,
1678
const JSPropertySpec* ps);
1679
1680
/* * */
1681
1682
extern JS_PUBLIC_API bool JS_AlreadyHasOwnPropertyById(JSContext* cx,
1683
JS::HandleObject obj,
1684
JS::HandleId id,
1685
bool* foundp);
1686
1687
extern JS_PUBLIC_API bool JS_AlreadyHasOwnProperty(JSContext* cx,
1688
JS::HandleObject obj,
1689
const char* name,
1690
bool* foundp);
1691
1692
extern JS_PUBLIC_API bool JS_AlreadyHasOwnUCProperty(JSContext* cx,
1693
JS::HandleObject obj,
1694
const char16_t* name,
1695
size_t namelen,
1696
bool* foundp);
1697
1698
extern JS_PUBLIC_API bool JS_AlreadyHasOwnElement(JSContext* cx,
1699
JS::HandleObject obj,
1700
uint32_t index, bool* foundp);
1701
1702
extern JS_PUBLIC_API JSObject* JS_NewArrayObject(
1703
JSContext* cx, const JS::HandleValueArray& contents);
1704
1705
extern JS_PUBLIC_API JSObject* JS_NewArrayObject(JSContext* cx, size_t length);
1706
1707
/**
1708
* On success, returns true, setting |*isArray| to true if |value| is an Array
1709
* object or a wrapper around one, or to false if not. Returns false on
1710
* failure.
1711
*
1712
* This method returns true with |*isArray == false| when passed an ES6 proxy
1713
* whose target is an Array, or when passed a revoked proxy.
1714
*/
1715
extern JS_PUBLIC_API bool JS_IsArrayObject(JSContext* cx, JS::HandleValue value,
1716
bool* isArray);
1717
1718
/**
1719
* On success, returns true, setting |*isArray| to true if |obj| is an Array
1720
* object or a wrapper around one, or to false if not. Returns false on
1721
* failure.
1722
*
1723
* This method returns true with |*isArray == false| when passed an ES6 proxy
1724
* whose target is an Array, or when passed a revoked proxy.
1725
*/
1726
extern JS_PUBLIC_API bool JS_IsArrayObject(JSContext* cx, JS::HandleObject obj,
1727
bool* isArray);
1728
1729
extern JS_PUBLIC_API bool JS_GetArrayLength(JSContext* cx,
1730
JS::Handle<JSObject*> obj,
1731
uint32_t* lengthp);
1732
1733
extern JS_PUBLIC_API bool JS_SetArrayLength(JSContext* cx,
1734
JS::Handle<JSObject*> obj,
1735
uint32_t length);
1736
1737
namespace JS {
1738
1739
/**
1740
* On success, returns true, setting |*isMap| to true if |obj| is a Map object
1741
* or a wrapper around one, or to false if not. Returns false on failure.
1742
*
1743
* This method returns true with |*isMap == false| when passed an ES6 proxy
1744
* whose target is a Map, or when passed a revoked proxy.
1745
*/
1746
extern JS_PUBLIC_API bool IsMapObject(JSContext* cx, JS::HandleObject obj,
1747
bool* isMap);
1748
1749
/**
1750
* On success, returns true, setting |*isSet| to true if |obj| is a Set object
1751
* or a wrapper around one, or to false if not. Returns false on failure.
1752
*
1753
* This method returns true with |*isSet == false| when passed an ES6 proxy
1754
* whose target is a Set, or when passed a revoked proxy.
1755
*/
1756
extern JS_PUBLIC_API bool IsSetObject(JSContext* cx, JS::HandleObject obj,
1757
bool* isSet);
1758
1759
} /* namespace JS */
1760
1761
/**
1762
* Assign 'undefined' to all of the object's non-reserved slots. Note: this is
1763
* done for all slots, regardless of the associated property descriptor.
1764
*/
1765
JS_PUBLIC_API void JS_SetAllNonReservedSlotsToUndefined(JS::HandleObject obj);
1766
1767
extern JS_PUBLIC_API JS::Value JS_GetReservedSlot(JSObject* obj,
1768
uint32_t index);
1769
1770
extern JS_PUBLIC_API void JS_SetReservedSlot(JSObject* obj, uint32_t index,
1771
const JS::Value& v);
1772
1773
extern JS_PUBLIC_API void JS_InitReservedSlot(JSObject* obj, uint32_t index,
1774
void* ptr, size_t nbytes,
1775
JS::MemoryUse use);
1776
1777
template <typename T>
1778
void JS_InitReservedSlot(JSObject* obj, uint32_t index, T* ptr,
1779
JS::MemoryUse use) {
1780
JS_InitReservedSlot(obj, index, ptr, sizeof(T), use);
1781
}
1782
1783
extern JS_PUBLIC_API void JS_InitPrivate(JSObject* obj, void* data,
1784
size_t nbytes, JS::MemoryUse use);
1785
1786
/************************************************************************/
1787
1788
/* native that can be called as a ctor */
1789
static constexpr unsigned JSFUN_CONSTRUCTOR = 0x400;
1790
1791
/* | of all the JSFUN_* flags */
1792
static constexpr unsigned JSFUN_FLAGS_MASK = 0x400;
1793
1794
static_assert((JSPROP_FLAGS_MASK & JSFUN_FLAGS_MASK) == 0,
1795
"JSFUN_* flags do not overlap JSPROP_* flags, because bits from "
1796
"the two flag-sets appear in the same flag in some APIs");
1797
1798
/*
1799
* Functions and scripts.
1800
*/
1801
extern JS_PUBLIC_API JSFunction* JS_NewFunction(JSContext* cx, JSNative call,
1802
unsigned nargs, unsigned flags,