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