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