summaryrefslogtreecommitdiffstats
path: root/js/public/Array.h
blob: f019090167defb89b347e36faef66477d84fbfb7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

/* Array-related operations. */

#ifndef js_Array_h
#define js_Array_h

#include <stddef.h>  // size_t
#include <stdint.h>  // uint32_t

#include "jstypes.h"  // JS_PUBLIC_API

#include "js/TypeDecls.h"

namespace JS {

class HandleValueArray;

/**
 * Create an Array from the current realm with the given contents.
 */
extern JS_PUBLIC_API JSObject* NewArrayObject(JSContext* cx,
                                              const HandleValueArray& contents);

/**
 * Create an Array from the current realm with the given length and allocate
 * memory for all its elements.  (The elements nonetheless will not exist as
 * properties on the returned array until values have been assigned to them.)
 */
extern JS_PUBLIC_API JSObject* NewArrayObject(JSContext* cx, size_t length);

/**
 * Determine whether |value| is an Array object or a wrapper around one.  (An
 * ES6 proxy whose target is an Array object, e.g.
 * |var target = [], handler = {}; Proxy.revocable(target, handler).proxy|, is
 * not considered to be an Array.)
 *
 * On success set |*isArray| accordingly and return true; on failure return
 * false.
 */
extern JS_PUBLIC_API bool IsArrayObject(JSContext* cx, Handle<Value> value,
                                        bool* isArray);

/**
 * Determine whether |obj| is an Array object or a wrapper around one.  (An
 * ES6 proxy whose target is an Array object, e.g.
 * |var target = [], handler = {}; Proxy.revocable(target, handler).proxy|, is
 * not considered to be an Array.)
 *
 * On success set |*isArray| accordingly and return true; on failure return
 * false.
 */
extern JS_PUBLIC_API bool IsArrayObject(JSContext* cx, Handle<JSObject*> obj,
                                        bool* isArray);

/**
 * Store |*lengthp = ToLength(obj.length)| and return true on success, else
 * return false.
 *
 * If the length does not fit in |uint32_t|, an exception is reported and false
 * is returned.
 *
 * |ToLength| converts its input to an integer usable to index an
 * array-like object.
 *
 * If |obj| is an Array, this overall operation is the same as getting
 * |obj.length|.
 */
extern JS_PUBLIC_API bool GetArrayLength(JSContext* cx, Handle<JSObject*> obj,
                                         uint32_t* lengthp);

/**
 * Perform |obj.length = length| as if in strict mode code, with a fast path for
 * the case where |obj| is an Array.
 *
 * This operation is exactly and only assigning to a "length" property.  In
 * general, it can invoke an existing "length" setter, throw if the property is
 * non-writable, or do anything else a property-set operation might do.
 */
extern JS_PUBLIC_API bool SetArrayLength(JSContext* cx, Handle<JSObject*> obj,
                                         uint32_t length);

/**
 * The answer to a successful query as to whether an object is an Array per
 * ES6's internal |IsArray| operation (as exposed by |Array.isArray|).
 */
enum class IsArrayAnswer { Array, NotArray, RevokedProxy };

/**
 * ES6 7.2.2.
 *
 * Returns false on failure, otherwise returns true and sets |*isArray|
 * indicating whether the object passes ECMAScript's IsArray test.  This is the
 * same test performed by |Array.isArray|.
 *
 * This is NOT the same as asking whether |obj| is an Array or a wrapper around
 * one.  If |obj| is a proxy created by |Proxy.revocable()| and has been
 * revoked, or if |obj| is a proxy whose target (at any number of hops) is a
 * revoked proxy, this method throws a TypeError and returns false.
 */
extern JS_PUBLIC_API bool IsArray(JSContext* cx, Handle<JSObject*> obj,
                                  bool* isArray);

/**
 * Identical to IsArray above, but the nature of the object (if successfully
 * determined) is communicated via |*answer|.  In particular this method
 * returns true and sets |*answer = IsArrayAnswer::RevokedProxy| when called on
 * a revoked proxy.
 *
 * Most users will want the overload above, not this one.
 */
extern JS_PUBLIC_API bool IsArray(JSContext* cx, Handle<JSObject*> obj,
                                  IsArrayAnswer* answer);

}  // namespace JS

#endif  // js_Array_h