Coverage Report

Created: 2025-01-28 06:38

/src/hermes/lib/VM/JSLib/JSLibInternal.h
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright (c) Meta Platforms, Inc. and affiliates.
3
 *
4
 * This source code is licensed under the MIT license found in the
5
 * LICENSE file in the root directory of this source tree.
6
 */
7
8
#ifndef HERMES_VM_JSLIB_JSLIBINTERNAL_H
9
#define HERMES_VM_JSLIB_JSLIBINTERNAL_H
10
11
#include "hermes/Support/ScopeChain.h"
12
#include "hermes/VM/Callable.h"
13
#include "hermes/VM/JSDate.h"
14
#include "hermes/VM/JSError.h"
15
#include "hermes/VM/JSMapImpl.h"
16
#include "hermes/VM/JSNativeFunctions.h"
17
#include "hermes/VM/JSProxy.h"
18
#include "hermes/VM/JSRegExp.h"
19
#include "hermes/VM/JSWeakRef.h"
20
21
namespace hermes {
22
namespace vm {
23
24
/// This function declares a new system constructor (the likes of 'Object' and
25
/// 'Array') with a specified object to be used as the 'prototype' property.
26
/// - First, it creates a new NativeFunction object for the constructor, with
27
///   Function.prototype as the internal prototype.
28
/// - Initializes the native function's 'prototype' property with the supplied
29
///   prototype object.
30
/// - Then, it sets the supplied prototype object's 'constructor' property to
31
///   the created NativeFunction.
32
/// - Lastly, it initializes the requested global property with the
33
///   NativeFunction.
34
///
35
/// \param name the name of the global property to initialize (e.g. 'Object')
36
/// \param nativeFunctionPtr the native function implementing the constructor.
37
/// \param prototypeObjectHandle the object instance to set in the 'prototype'
38
///   property of the constructor.
39
/// \param paramCount the number of declared constructor parameters
40
/// The second version takes an additional parameter:
41
/// \param constructorProtoObjectHandle the prototype to set for the
42
///   constructor function
43
/// \return the created constructor function.
44
Handle<NativeConstructor> defineSystemConstructor(
45
    Runtime &runtime,
46
    SymbolID name,
47
    NativeFunctionPtr nativeFunctionPtr,
48
    Handle<JSObject> prototypeObjectHandle,
49
    Handle<JSObject> constructorProtoObjectHandle,
50
    unsigned paramCount,
51
    NativeConstructor::CreatorFunction *creator,
52
    CellKind targetKind);
53
54
Handle<NativeConstructor> defineSystemConstructor(
55
    Runtime &runtime,
56
    SymbolID name,
57
    NativeFunctionPtr nativeFunctionPtr,
58
    Handle<JSObject> prototypeObjectHandle,
59
    unsigned paramCount,
60
    NativeConstructor::CreatorFunction *creator,
61
    CellKind targetKind);
62
63
template <class NativeClass>
64
Handle<NativeConstructor> defineSystemConstructor(
65
    Runtime &runtime,
66
    SymbolID name,
67
    NativeFunctionPtr nativeFunctionPtr,
68
    Handle<JSObject> prototypeObjectHandle,
69
    unsigned paramCount,
70
3.04k
    CellKind targetKind) {
71
3.04k
  return defineSystemConstructor(
72
3.04k
      runtime,
73
3.04k
      name,
74
3.04k
      nativeFunctionPtr,
75
3.04k
      prototypeObjectHandle,
76
3.04k
      paramCount,
77
3.04k
      NativeConstructor::creatorFunction<NativeClass>,
78
3.04k
      targetKind);
79
3.04k
}
hermes::vm::Handle<hermes::vm::NativeConstructor> hermes::vm::defineSystemConstructor<hermes::vm::JSArray>(hermes::vm::Runtime&, hermes::vm::SymbolID, hermes::vm::CallResult<hermes::vm::HermesValue, (hermes::vm::detail::CallResultSpecialize)2> (*)(void*, hermes::vm::Runtime&, hermes::vm::NativeArgs), hermes::vm::Handle<hermes::vm::JSObject>, unsigned int, hermes::vm::CellKind)
Line
Count
Source
70
160
    CellKind targetKind) {
71
160
  return defineSystemConstructor(
72
160
      runtime,
73
160
      name,
74
160
      nativeFunctionPtr,
75
160
      prototypeObjectHandle,
76
160
      paramCount,
77
160
      NativeConstructor::creatorFunction<NativeClass>,
78
160
      targetKind);
79
160
}
hermes::vm::Handle<hermes::vm::NativeConstructor> hermes::vm::defineSystemConstructor<hermes::vm::JSArrayBuffer>(hermes::vm::Runtime&, hermes::vm::SymbolID, hermes::vm::CallResult<hermes::vm::HermesValue, (hermes::vm::detail::CallResultSpecialize)2> (*)(void*, hermes::vm::Runtime&, hermes::vm::NativeArgs), hermes::vm::Handle<hermes::vm::JSObject>, unsigned int, hermes::vm::CellKind)
Line
Count
Source
70
160
    CellKind targetKind) {
71
160
  return defineSystemConstructor(
72
160
      runtime,
73
160
      name,
74
160
      nativeFunctionPtr,
75
160
      prototypeObjectHandle,
76
160
      paramCount,
77
160
      NativeConstructor::creatorFunction<NativeClass>,
78
160
      targetKind);
79
160
}
hermes::vm::Handle<hermes::vm::NativeConstructor> hermes::vm::defineSystemConstructor<hermes::vm::JSBigInt>(hermes::vm::Runtime&, hermes::vm::SymbolID, hermes::vm::CallResult<hermes::vm::HermesValue, (hermes::vm::detail::CallResultSpecialize)2> (*)(void*, hermes::vm::Runtime&, hermes::vm::NativeArgs), hermes::vm::Handle<hermes::vm::JSObject>, unsigned int, hermes::vm::CellKind)
Line
Count
Source
70
160
    CellKind targetKind) {
71
160
  return defineSystemConstructor(
72
160
      runtime,
73
160
      name,
74
160
      nativeFunctionPtr,
75
160
      prototypeObjectHandle,
76
160
      paramCount,
77
160
      NativeConstructor::creatorFunction<NativeClass>,
78
160
      targetKind);
79
160
}
hermes::vm::Handle<hermes::vm::NativeConstructor> hermes::vm::defineSystemConstructor<hermes::vm::JSDataView>(hermes::vm::Runtime&, hermes::vm::SymbolID, hermes::vm::CallResult<hermes::vm::HermesValue, (hermes::vm::detail::CallResultSpecialize)2> (*)(void*, hermes::vm::Runtime&, hermes::vm::NativeArgs), hermes::vm::Handle<hermes::vm::JSObject>, unsigned int, hermes::vm::CellKind)
Line
Count
Source
70
160
    CellKind targetKind) {
71
160
  return defineSystemConstructor(
72
160
      runtime,
73
160
      name,
74
160
      nativeFunctionPtr,
75
160
      prototypeObjectHandle,
76
160
      paramCount,
77
160
      NativeConstructor::creatorFunction<NativeClass>,
78
160
      targetKind);
79
160
}
hermes::vm::Handle<hermes::vm::NativeConstructor> hermes::vm::defineSystemConstructor<hermes::vm::JSError>(hermes::vm::Runtime&, hermes::vm::SymbolID, hermes::vm::CallResult<hermes::vm::HermesValue, (hermes::vm::detail::CallResultSpecialize)2> (*)(void*, hermes::vm::Runtime&, hermes::vm::NativeArgs), hermes::vm::Handle<hermes::vm::JSObject>, unsigned int, hermes::vm::CellKind)
Line
Count
Source
70
160
    CellKind targetKind) {
71
160
  return defineSystemConstructor(
72
160
      runtime,
73
160
      name,
74
160
      nativeFunctionPtr,
75
160
      prototypeObjectHandle,
76
160
      paramCount,
77
160
      NativeConstructor::creatorFunction<NativeClass>,
78
160
      targetKind);
79
160
}
hermes::vm::Handle<hermes::vm::NativeConstructor> hermes::vm::defineSystemConstructor<hermes::vm::JSMapImpl<(hermes::vm::CellKind)48> >(hermes::vm::Runtime&, hermes::vm::SymbolID, hermes::vm::CallResult<hermes::vm::HermesValue, (hermes::vm::detail::CallResultSpecialize)2> (*)(void*, hermes::vm::Runtime&, hermes::vm::NativeArgs), hermes::vm::Handle<hermes::vm::JSObject>, unsigned int, hermes::vm::CellKind)
Line
Count
Source
70
160
    CellKind targetKind) {
71
160
  return defineSystemConstructor(
72
160
      runtime,
73
160
      name,
74
160
      nativeFunctionPtr,
75
160
      prototypeObjectHandle,
76
160
      paramCount,
77
160
      NativeConstructor::creatorFunction<NativeClass>,
78
160
      targetKind);
79
160
}
hermes::vm::Handle<hermes::vm::NativeConstructor> hermes::vm::defineSystemConstructor<hermes::vm::JSObject>(hermes::vm::Runtime&, hermes::vm::SymbolID, hermes::vm::CallResult<hermes::vm::HermesValue, (hermes::vm::detail::CallResultSpecialize)2> (*)(void*, hermes::vm::Runtime&, hermes::vm::NativeArgs), hermes::vm::Handle<hermes::vm::JSObject>, unsigned int, hermes::vm::CellKind)
Line
Count
Source
70
320
    CellKind targetKind) {
71
320
  return defineSystemConstructor(
72
320
      runtime,
73
320
      name,
74
320
      nativeFunctionPtr,
75
320
      prototypeObjectHandle,
76
320
      paramCount,
77
320
      NativeConstructor::creatorFunction<NativeClass>,
78
320
      targetKind);
79
320
}
hermes::vm::Handle<hermes::vm::NativeConstructor> hermes::vm::defineSystemConstructor<hermes::vm::JSProxy>(hermes::vm::Runtime&, hermes::vm::SymbolID, hermes::vm::CallResult<hermes::vm::HermesValue, (hermes::vm::detail::CallResultSpecialize)2> (*)(void*, hermes::vm::Runtime&, hermes::vm::NativeArgs), hermes::vm::Handle<hermes::vm::JSObject>, unsigned int, hermes::vm::CellKind)
Line
Count
Source
70
160
    CellKind targetKind) {
71
160
  return defineSystemConstructor(
72
160
      runtime,
73
160
      name,
74
160
      nativeFunctionPtr,
75
160
      prototypeObjectHandle,
76
160
      paramCount,
77
160
      NativeConstructor::creatorFunction<NativeClass>,
78
160
      targetKind);
79
160
}
hermes::vm::Handle<hermes::vm::NativeConstructor> hermes::vm::defineSystemConstructor<hermes::vm::JSMapImpl<(hermes::vm::CellKind)47> >(hermes::vm::Runtime&, hermes::vm::SymbolID, hermes::vm::CallResult<hermes::vm::HermesValue, (hermes::vm::detail::CallResultSpecialize)2> (*)(void*, hermes::vm::Runtime&, hermes::vm::NativeArgs), hermes::vm::Handle<hermes::vm::JSObject>, unsigned int, hermes::vm::CellKind)
Line
Count
Source
70
160
    CellKind targetKind) {
71
160
  return defineSystemConstructor(
72
160
      runtime,
73
160
      name,
74
160
      nativeFunctionPtr,
75
160
      prototypeObjectHandle,
76
160
      paramCount,
77
160
      NativeConstructor::creatorFunction<NativeClass>,
78
160
      targetKind);
79
160
}
hermes::vm::Handle<hermes::vm::NativeConstructor> hermes::vm::defineSystemConstructor<hermes::vm::JSString>(hermes::vm::Runtime&, hermes::vm::SymbolID, hermes::vm::CallResult<hermes::vm::HermesValue, (hermes::vm::detail::CallResultSpecialize)2> (*)(void*, hermes::vm::Runtime&, hermes::vm::NativeArgs), hermes::vm::Handle<hermes::vm::JSObject>, unsigned int, hermes::vm::CellKind)
Line
Count
Source
70
160
    CellKind targetKind) {
71
160
  return defineSystemConstructor(
72
160
      runtime,
73
160
      name,
74
160
      nativeFunctionPtr,
75
160
      prototypeObjectHandle,
76
160
      paramCount,
77
160
      NativeConstructor::creatorFunction<NativeClass>,
78
160
      targetKind);
79
160
}
hermes::vm::Handle<hermes::vm::NativeConstructor> hermes::vm::defineSystemConstructor<hermes::vm::JSFunction>(hermes::vm::Runtime&, hermes::vm::SymbolID, hermes::vm::CallResult<hermes::vm::HermesValue, (hermes::vm::detail::CallResultSpecialize)2> (*)(void*, hermes::vm::Runtime&, hermes::vm::NativeArgs), hermes::vm::Handle<hermes::vm::JSObject>, unsigned int, hermes::vm::CellKind)
Line
Count
Source
70
160
    CellKind targetKind) {
71
160
  return defineSystemConstructor(
72
160
      runtime,
73
160
      name,
74
160
      nativeFunctionPtr,
75
160
      prototypeObjectHandle,
76
160
      paramCount,
77
160
      NativeConstructor::creatorFunction<NativeClass>,
78
160
      targetKind);
79
160
}
hermes::vm::Handle<hermes::vm::NativeConstructor> hermes::vm::defineSystemConstructor<hermes::vm::JSNumber>(hermes::vm::Runtime&, hermes::vm::SymbolID, hermes::vm::CallResult<hermes::vm::HermesValue, (hermes::vm::detail::CallResultSpecialize)2> (*)(void*, hermes::vm::Runtime&, hermes::vm::NativeArgs), hermes::vm::Handle<hermes::vm::JSObject>, unsigned int, hermes::vm::CellKind)
Line
Count
Source
70
160
    CellKind targetKind) {
71
160
  return defineSystemConstructor(
72
160
      runtime,
73
160
      name,
74
160
      nativeFunctionPtr,
75
160
      prototypeObjectHandle,
76
160
      paramCount,
77
160
      NativeConstructor::creatorFunction<NativeClass>,
78
160
      targetKind);
79
160
}
hermes::vm::Handle<hermes::vm::NativeConstructor> hermes::vm::defineSystemConstructor<hermes::vm::JSBoolean>(hermes::vm::Runtime&, hermes::vm::SymbolID, hermes::vm::CallResult<hermes::vm::HermesValue, (hermes::vm::detail::CallResultSpecialize)2> (*)(void*, hermes::vm::Runtime&, hermes::vm::NativeArgs), hermes::vm::Handle<hermes::vm::JSObject>, unsigned int, hermes::vm::CellKind)
Line
Count
Source
70
160
    CellKind targetKind) {
71
160
  return defineSystemConstructor(
72
160
      runtime,
73
160
      name,
74
160
      nativeFunctionPtr,
75
160
      prototypeObjectHandle,
76
160
      paramCount,
77
160
      NativeConstructor::creatorFunction<NativeClass>,
78
160
      targetKind);
79
160
}
hermes::vm::Handle<hermes::vm::NativeConstructor> hermes::vm::defineSystemConstructor<hermes::vm::JSRegExp>(hermes::vm::Runtime&, hermes::vm::SymbolID, hermes::vm::CallResult<hermes::vm::HermesValue, (hermes::vm::detail::CallResultSpecialize)2> (*)(void*, hermes::vm::Runtime&, hermes::vm::NativeArgs), hermes::vm::Handle<hermes::vm::JSObject>, unsigned int, hermes::vm::CellKind)
Line
Count
Source
70
160
    CellKind targetKind) {
71
160
  return defineSystemConstructor(
72
160
      runtime,
73
160
      name,
74
160
      nativeFunctionPtr,
75
160
      prototypeObjectHandle,
76
160
      paramCount,
77
160
      NativeConstructor::creatorFunction<NativeClass>,
78
160
      targetKind);
79
160
}
hermes::vm::Handle<hermes::vm::NativeConstructor> hermes::vm::defineSystemConstructor<hermes::vm::JSSymbol>(hermes::vm::Runtime&, hermes::vm::SymbolID, hermes::vm::CallResult<hermes::vm::HermesValue, (hermes::vm::detail::CallResultSpecialize)2> (*)(void*, hermes::vm::Runtime&, hermes::vm::NativeArgs), hermes::vm::Handle<hermes::vm::JSObject>, unsigned int, hermes::vm::CellKind)
Line
Count
Source
70
160
    CellKind targetKind) {
71
160
  return defineSystemConstructor(
72
160
      runtime,
73
160
      name,
74
160
      nativeFunctionPtr,
75
160
      prototypeObjectHandle,
76
160
      paramCount,
77
160
      NativeConstructor::creatorFunction<NativeClass>,
78
160
      targetKind);
79
160
}
hermes::vm::Handle<hermes::vm::NativeConstructor> hermes::vm::defineSystemConstructor<hermes::vm::JSDate>(hermes::vm::Runtime&, hermes::vm::SymbolID, hermes::vm::CallResult<hermes::vm::HermesValue, (hermes::vm::detail::CallResultSpecialize)2> (*)(void*, hermes::vm::Runtime&, hermes::vm::NativeArgs), hermes::vm::Handle<hermes::vm::JSObject>, unsigned int, hermes::vm::CellKind)
Line
Count
Source
70
160
    CellKind targetKind) {
71
160
  return defineSystemConstructor(
72
160
      runtime,
73
160
      name,
74
160
      nativeFunctionPtr,
75
160
      prototypeObjectHandle,
76
160
      paramCount,
77
160
      NativeConstructor::creatorFunction<NativeClass>,
78
160
      targetKind);
79
160
}
hermes::vm::Handle<hermes::vm::NativeConstructor> hermes::vm::defineSystemConstructor<hermes::vm::JSWeakMapImpl<(hermes::vm::CellKind)51> >(hermes::vm::Runtime&, hermes::vm::SymbolID, hermes::vm::CallResult<hermes::vm::HermesValue, (hermes::vm::detail::CallResultSpecialize)2> (*)(void*, hermes::vm::Runtime&, hermes::vm::NativeArgs), hermes::vm::Handle<hermes::vm::JSObject>, unsigned int, hermes::vm::CellKind)
Line
Count
Source
70
160
    CellKind targetKind) {
71
160
  return defineSystemConstructor(
72
160
      runtime,
73
160
      name,
74
160
      nativeFunctionPtr,
75
160
      prototypeObjectHandle,
76
160
      paramCount,
77
160
      NativeConstructor::creatorFunction<NativeClass>,
78
160
      targetKind);
79
160
}
Unexecuted instantiation: hermes::vm::Handle<hermes::vm::NativeConstructor> hermes::vm::defineSystemConstructor<hermes::vm::JSWeakRef>(hermes::vm::Runtime&, hermes::vm::SymbolID, hermes::vm::CallResult<hermes::vm::HermesValue, (hermes::vm::detail::CallResultSpecialize)2> (*)(void*, hermes::vm::Runtime&, hermes::vm::NativeArgs), hermes::vm::Handle<hermes::vm::JSObject>, unsigned int, hermes::vm::CellKind)
hermes::vm::Handle<hermes::vm::NativeConstructor> hermes::vm::defineSystemConstructor<hermes::vm::JSWeakMapImpl<(hermes::vm::CellKind)52> >(hermes::vm::Runtime&, hermes::vm::SymbolID, hermes::vm::CallResult<hermes::vm::HermesValue, (hermes::vm::detail::CallResultSpecialize)2> (*)(void*, hermes::vm::Runtime&, hermes::vm::NativeArgs), hermes::vm::Handle<hermes::vm::JSObject>, unsigned int, hermes::vm::CellKind)
Line
Count
Source
70
160
    CellKind targetKind) {
71
160
  return defineSystemConstructor(
72
160
      runtime,
73
160
      name,
74
160
      nativeFunctionPtr,
75
160
      prototypeObjectHandle,
76
160
      paramCount,
77
160
      NativeConstructor::creatorFunction<NativeClass>,
78
160
      targetKind);
79
160
}
80
81
/// Define a method in an object instance.
82
/// Currently, it's only used to define global %HermesInternal object in
83
/// createHermesInternalObject(), with different flags, i.e. writable = 0 and
84
/// configurable = 0.
85
/// \param objectHandle the instance where the method is defined.
86
/// \param propertyName the key in objectHandle to insert the method at.
87
/// \param methodName the value of the function.name property.
88
/// \param context the context to pass to the native function.
89
/// \param nativeFunctionPtr the native function implementing the method.
90
/// \param paramCount the number of declared method parameters
91
/// \param dpf the flags to set on the newly defined property.
92
/// \return the new NativeFunction.
93
CallResult<HermesValue> defineMethod(
94
    Runtime &runtime,
95
    Handle<JSObject> objectHandle,
96
    SymbolID propertyName,
97
    SymbolID methodName,
98
    void *context,
99
    NativeFunctionPtr nativeFunctionPtr,
100
    unsigned paramCount,
101
    DefinePropertyFlags dpf);
102
103
/// Define a method in an object instance.
104
/// Currently, it's only used to define global %HermesInternal object in
105
/// createHermesInternalObject(), with different flags, i.e. writable = 0 and
106
/// configurable = 0.
107
/// \param objectHandle the instance where the method is defined.
108
/// \param name the key in objectHandle to insert the method at.
109
/// \param context the context to pass to the native function.
110
/// \param nativeFunctionPtr the native function implementing the method.
111
/// \param paramCount the number of declared method parameters
112
/// \param dpf the flags to set on the newly defined property.
113
/// \return the new NativeFunction.
114
inline CallResult<HermesValue> defineMethod(
115
    Runtime &runtime,
116
    Handle<JSObject> objectHandle,
117
    SymbolID name,
118
    void *context,
119
    NativeFunctionPtr nativeFunctionPtr,
120
    unsigned paramCount,
121
56.1k
    DefinePropertyFlags dpf) {
122
56.1k
  return defineMethod(
123
56.1k
      runtime,
124
56.1k
      objectHandle,
125
56.1k
      name,
126
56.1k
      name,
127
56.1k
      context,
128
56.1k
      nativeFunctionPtr,
129
56.1k
      paramCount,
130
56.1k
      dpf);
131
56.1k
}
132
133
/// Define a method in an object instance.
134
/// \param objectHandle the instance where the method is defined.
135
/// \param name the name of the method.
136
/// \param context the context to pass to the native function.
137
/// \param nativeFunctionPtr the native function implementing the method.
138
/// \param paramCount the number of declared method parameters
139
void defineMethod(
140
    Runtime &runtime,
141
    Handle<JSObject> objectHandle,
142
    SymbolID name,
143
    void *context,
144
    NativeFunctionPtr nativeFunctionPtr,
145
    unsigned paramCount);
146
147
/// Define an accessor in an object instance.
148
/// \param objectHandle the instance where the accessor is defined.
149
/// \param propertyName the key in the object at which to define the accessor.
150
/// \param methodName the name of the function.
151
/// \param context the context to pass to the native functions.
152
/// \param getterFunc the native function implementing the getter.
153
/// \param setterFunc the native function implementing the setter.
154
void defineAccessor(
155
    Runtime &runtime,
156
    Handle<JSObject> objectHandle,
157
    SymbolID propertyName,
158
    SymbolID methodName,
159
    void *context,
160
    NativeFunctionPtr getterFunc,
161
    NativeFunctionPtr setterFunc,
162
    bool enumerable,
163
    bool configurable);
164
165
/// Define an accessor in an object instance.
166
/// \param objectHandle the instance where the accessor is defined.
167
/// \param name the name of the accessor.
168
/// \param context the context to pass to the native functions.
169
/// \param getterFunc the native function implementing the getter.
170
/// \param setterFunc the native function implementing the setter.
171
inline void defineAccessor(
172
    Runtime &runtime,
173
    Handle<JSObject> objectHandle,
174
    SymbolID name,
175
    void *context,
176
    NativeFunctionPtr getterFunc,
177
    NativeFunctionPtr setterFunc,
178
    bool enumerable,
179
6.88k
    bool configurable) {
180
6.88k
  defineAccessor(
181
6.88k
      runtime,
182
6.88k
      objectHandle,
183
6.88k
      name,
184
6.88k
      name,
185
6.88k
      context,
186
6.88k
      getterFunc,
187
6.88k
      setterFunc,
188
6.88k
      enumerable,
189
6.88k
      configurable);
190
6.88k
}
191
192
/// Define a property in an object instance.
193
/// The property is writable, configurable, but not enumerable.
194
/// \param objectHandle the instance where the property is defined.
195
/// \param name the name of the property.
196
/// \param value the value to set to the property.
197
void defineProperty(
198
    Runtime &runtime,
199
    Handle<JSObject> objectHandle,
200
    SymbolID name,
201
    Handle<> value);
202
203
/// Define a property in an object instance.
204
/// \param objectHandle the instance where the property is defined.
205
/// \param name the name of the property.
206
/// \param value the value to set to the property.
207
/// \param dpf the flags to set on the newly defined property.
208
void defineProperty(
209
    Runtime &runtime,
210
    Handle<JSObject> objectHandle,
211
    SymbolID name,
212
    Handle<> value,
213
    DefinePropertyFlags dpf);
214
215
/// Call the IteratorClose operation following an exception being thrown.
216
/// \pre runtime.thrownValue_ must be populated with a thrown value.
217
/// \return ExecutionStatus::EXCEPTION
218
ExecutionStatus iteratorCloseAndRethrow(
219
    Runtime &runtime,
220
    Handle<JSObject> iterator);
221
222
/// Create and initialize the global Object constructor. Populate the methods
223
/// of Object and Object.prototype.
224
/// \return the global Object constructor.
225
Handle<JSObject> createObjectConstructor(Runtime &runtime);
226
227
/// Built-in Object.prototype.toString.
228
CallResult<HermesValue> directObjectPrototypeToString(
229
    Runtime &runtime,
230
    Handle<> arg);
231
232
/// Create and initialize the global Error constructor, as well as all
233
/// the native error constructors. Populate the instance and prototype methods.
234
#define ALL_ERROR_TYPE(name) \
235
  Handle<JSObject> create##name##Constructor(Runtime &runtime);
236
#include "hermes/FrontEndDefs/NativeErrorTypes.def"
237
238
/// Populate the internal CallSite.prototype.
239
void populateCallSitePrototype(Runtime &runtime);
240
241
/// Create and initialize the global String constructor. Populate the methods
242
/// of String and String.prototype.
243
/// \return the global String constructor.
244
Handle<JSObject> createStringConstructor(Runtime &runtime);
245
246
/// Create and initialize the global BigInt constructor. Populate the methods
247
/// of BigInt and BigInt.prototype.
248
/// \return the global BigInt constructor.
249
Handle<JSObject> createBigIntConstructor(Runtime &runtime);
250
251
/// Create and initialize the global Function constructor. Populate the methods
252
/// of Function and Function.prototype.
253
/// \return the global Function constructor.
254
Handle<JSObject> createFunctionConstructor(Runtime &runtime);
255
256
/// Create and initialize the global Number constructor. Populate the methods
257
/// of Number and Number.prototype.
258
/// \return the global Number constructor.
259
Handle<JSObject> createNumberConstructor(Runtime &runtime);
260
261
/// Create and initialize the global Boolean constructor. Populate the methods
262
/// of Boolean and Boolean.prototype.
263
/// \return the global Boolean constructor.
264
Handle<JSObject> createBooleanConstructor(Runtime &runtime);
265
266
/// Create and initialize the global Date constructor. Populate the methods
267
/// of Date and Date.prototype.
268
/// \return the global Date constructor.
269
Handle<JSObject> createDateConstructor(Runtime &runtime);
270
271
/// Create and initialize the global Math object, populating its value
272
/// and function properties.
273
Handle<JSObject> createMathObject(Runtime &runtime);
274
275
/// Create and initialize the global Proxy constructor, populating its methods.
276
/// \return the global Proxy constructor.
277
Handle<JSObject> createProxyConstructor(Runtime &runtime);
278
279
// Forward declaration.
280
class JSLibFlags;
281
282
/// Create and initialize the global %HermesInternal object, populating its
283
/// value and function properties.
284
Handle<JSObject> createHermesInternalObject(
285
    Runtime &runtime,
286
    const JSLibFlags &jsLibFlags);
287
288
#ifdef HERMES_ENABLE_DEBUGGER
289
290
/// Create and initialize the global %DebuggerInternal object, populating its
291
/// value and function properties.
292
Handle<JSObject> createDebuggerInternalObject(Runtime &runtime);
293
294
#endif // HERMES_ENABLE_DEBUGGER
295
296
/// Create and initialize the global JSON object, populating its value
297
/// and function properties.
298
Handle<JSObject> createJSONObject(Runtime &runtime);
299
300
/// Create and initialize the global Reflect object, populating its value
301
/// and function properties.
302
Handle<JSObject> createReflectObject(Runtime &runtime);
303
304
/// Create and initialize the global RegExp constructor. Populate the methods
305
/// of RegExp and RegExp.prototype.
306
/// \return the global RegExp constructor.
307
Handle<JSObject> createRegExpConstructor(Runtime &runtime);
308
309
/// ES6.0 21.2.3.2.3 Runtime Semantics: RegExpCreate ( P, F )
310
/// Creates a new RegExp with provided pattern \p P, and flags \p F.
311
CallResult<Handle<JSRegExp>>
312
regExpCreate(Runtime &runtime, Handle<> P, Handle<> F);
313
314
/// ES6.0 21.2.5.2.1
315
/// Implemented in RegExp.cpp
316
CallResult<HermesValue>
317
regExpExec(Runtime &runtime, Handle<JSObject> R, Handle<StringPrimitive> S);
318
319
/// Runs the RegExp.prototype.exec() function (ES5.1 15.10.6.2)
320
/// with a this value of \p regexp, with the argument \p S.
321
/// \return a new array as the result, null pointer if there were no matches.
322
CallResult<Handle<JSArray>> directRegExpExec(
323
    Handle<JSRegExp> regexp,
324
    Runtime &runtime,
325
    Handle<StringPrimitive> S);
326
327
/// ES6.0 21.1.3.14.1
328
/// Implemented in RegExp.cpp
329
/// Transforms a replacement string by substituting $ replacement strings.
330
/// \p captures can be a null pointer.
331
CallResult<HermesValue> getSubstitution(
332
    Runtime &runtime,
333
    Handle<StringPrimitive> matched,
334
    Handle<StringPrimitive> str,
335
    uint32_t position,
336
    Handle<ArrayStorageSmall> captures,
337
    Handle<JSObject> namedCaptures,
338
    Handle<StringPrimitive> replacement);
339
340
/// Main logic for String.prototype.split and RegExp.prototype[Symbol.split].
341
/// Returns an array of splitted strings.
342
CallResult<HermesValue> splitInternal(
343
    Runtime &runtime,
344
    Handle<> string,
345
    Handle<> limit,
346
    Handle<> separator);
347
348
/// Set the lastIndex property of \p regexp to \p shv.
349
inline ExecutionStatus
350
0
setLastIndex(Handle<JSObject> regexp, Runtime &runtime, SmallHermesValue shv) {
351
0
  return runtime.putNamedThrowOnError(
352
0
      regexp, PropCacheID::RegExpLastIndex, shv);
353
0
}
354
355
/// Set the lastIndex property of \p regexp to \p value.
356
inline ExecutionStatus
357
0
setLastIndex(Handle<JSObject> regexp, Runtime &runtime, double value) {
358
0
  auto shv = SmallHermesValue::encodeNumberValue(value, runtime);
359
0
  return setLastIndex(regexp, runtime, shv);
360
0
}
361
362
/// ES6.0 21.2.5.2.3
363
/// If \p unicode is set and the character at \p index in \S is the start of a
364
/// surrogate pair, \return index + 2. Otherwise \return index + 1.
365
/// Note that this function does not allocate.
366
uint64_t
367
advanceStringIndex(const StringPrimitive *S, uint64_t index, bool unicode);
368
369
/// Create and initialize the global Array constructor. Populate the methods
370
/// of Array and Array.prototype.
371
/// \return the global Array constructor.
372
Handle<JSObject> createArrayConstructor(Runtime &runtime);
373
374
Handle<JSObject> createArrayBufferConstructor(Runtime &runtime);
375
376
Handle<JSObject> createDataViewConstructor(Runtime &runtime);
377
378
Handle<JSObject> createTypedArrayBaseConstructor(Runtime &runtime);
379
380
#define TYPED_ARRAY(name, type) \
381
  Handle<JSObject> create##name##ArrayConstructor(Runtime &runtime);
382
#include "hermes/VM/TypedArrays.def"
383
#undef TYPED_ARRAY
384
385
/// Create and initialize the global Set constructor. Populate the methods
386
/// of Set.prototype.
387
Handle<JSObject> createSetConstructor(Runtime &runtime);
388
389
/// Create SetIterator.prototype and populate methods.
390
Handle<JSObject> createSetIteratorPrototype(Runtime &runtime);
391
392
/// Create and initialize the global Map constructor. Populate the methods
393
/// of Map.prototype.
394
Handle<JSObject> createMapConstructor(Runtime &runtime);
395
396
/// Create MapIterator.prototype and populate methods.
397
Handle<JSObject> createMapIteratorPrototype(Runtime &runtime);
398
399
/// Create the WeakMap constructor and populate methods.
400
Handle<JSObject> createWeakMapConstructor(Runtime &runtime);
401
402
/// Create the WeakSet constructor and populate methods.
403
Handle<JSObject> createWeakSetConstructor(Runtime &runtime);
404
405
/// Create the WeakRef constructor and populate methods.
406
Handle<JSObject> createWeakRefConstructor(Runtime &runtime);
407
408
/// Create the Symbol constructor and populate methods.
409
Handle<JSObject> createSymbolConstructor(Runtime &runtime);
410
411
/// Create the GeneratorFunction constructor and populate methods.
412
Handle<JSObject> createGeneratorFunctionConstructor(Runtime &runtime);
413
414
/// Create the AsyncFunction constructor and populate methods.
415
Handle<JSObject> createAsyncFunctionConstructor(Runtime &runtime);
416
417
/// Create the TextEncoder constructor and populate methods.
418
Handle<JSObject> createTextEncoderConstructor(Runtime &runtime);
419
420
/// Create the IteratorPrototype.
421
void populateIteratorPrototype(Runtime &runtime);
422
423
/// Create the ArrayIterator prototype.
424
void populateArrayIteratorPrototype(Runtime &runtime);
425
426
/// Create the StringIterator prototype.
427
void populateStringIteratorPrototype(Runtime &runtime);
428
429
/// Create the RegExpStringIterator prototype.
430
void populateRegExpStringIteratorPrototype(Runtime &runtime);
431
432
/// Create the %GeneratorPrototype%.
433
void populateGeneratorPrototype(Runtime &runtime);
434
435
/// ES19.2.1.1.1. CreateDynamicFunction ( constructor, newTarget, kind, args )
436
enum class DynamicFunctionKind { Normal, Generator, Async };
437
438
/// Create a new function given arguments and a body.
439
/// \param isGeneratorFunction when true, make a generator with "function*".
440
CallResult<HermesValue> createDynamicFunction(
441
    Runtime &runtime,
442
    NativeArgs args,
443
    DynamicFunctionKind kind);
444
445
/// A direct passthrough to call eval() on \p str.
446
CallResult<HermesValue> directEval(
447
    Runtime &runtime,
448
    Handle<StringPrimitive> str,
449
    const ScopeChain &scopeChain,
450
    bool isStrict,
451
    bool singleFunction);
452
453
/// ES10 23.1.1.2 AddEntriesFromIterable
454
/// Calls a callback with each pair of [key, value] from an iterable.
455
/// \param target the object to which to add the entries
456
/// \param iterable iterable which contains pairs of [key, value].
457
///     Must not be undefined or null.
458
/// \param adder the callback for actually adding properties, with signature:
459
///     ExecutionStatus adder(Runtime &runtime, Handle<> key, Handle<> value);
460
template <typename AdderCB>
461
CallResult<HermesValue> addEntriesFromIterable(
462
    Runtime &runtime,
463
    Handle<JSObject> target,
464
    Handle<> iterable,
465
0
    AdderCB adder) {
466
0
  GCScope gcScope{runtime};
467
  // 2. Assert: iterable is present, and is neither undefined nor null.
468
0
  assert(
469
0
      !iterable->isUndefined() && !iterable->isNull() &&
470
0
      "iterable cannot be undefined or null");
471
  // 3. Let iteratorRecord be ? GetIterator(iterable).
472
0
  CallResult<IteratorRecord> iterRes = getIterator(runtime, iterable);
473
0
  if (LLVM_UNLIKELY(iterRes == ExecutionStatus::EXCEPTION)) {
474
0
    return ExecutionStatus::EXCEPTION;
475
0
  }
476
0
  auto iteratorRecord = *iterRes;
477
478
0
  MutableHandle<JSObject> nextItem{runtime};
479
0
  MutableHandle<> key{runtime};
480
0
  MutableHandle<> value{runtime};
481
0
  Handle<> zero = HandleRootOwner::getZeroValue();
482
0
  Handle<> one = HandleRootOwner::getOneValue();
483
0
  auto marker = gcScope.createMarker();
484
485
  // 4. Repeat,
486
0
  for (;; gcScope.flushToMarker(marker)) {
487
    // a. Let next be ? IteratorStep(iteratorRecord).
488
0
    auto nextRes = iteratorStep(runtime, iteratorRecord);
489
0
    if (LLVM_UNLIKELY(nextRes == ExecutionStatus::EXCEPTION)) {
490
0
      return ExecutionStatus::EXCEPTION;
491
0
    }
492
0
    if (!*nextRes) {
493
      // b. If next is false, return target.
494
0
      return target.getHermesValue();
495
0
    }
496
    // c. Let nextItem be ? IteratorValue(next).
497
0
    nextItem = vmcast<JSObject>(nextRes->getHermesValue());
498
0
    auto nextItemRes = JSObject::getNamed_RJS(
499
0
        nextItem, runtime, Predefined::getSymbolID(Predefined::value));
500
0
    if (LLVM_UNLIKELY(nextItemRes == ExecutionStatus::EXCEPTION)) {
501
0
      return ExecutionStatus::EXCEPTION;
502
0
    }
503
0
    if (!vmisa<JSObject>(nextItemRes->get())) {
504
      // d. If Type(nextItem) is not Object, then
505
      // i.     Let error be ThrowCompletion(a newly created TypeError object).
506
      // ii.     Return ? IteratorClose(iteratorRecord, error).
507
0
      (void)runtime.raiseTypeError("Iterator value must be an object");
508
0
      return iteratorCloseAndRethrow(runtime, iteratorRecord.iterator);
509
0
    }
510
0
    nextItem = PseudoHandle<JSObject>::vmcast(std::move(*nextItemRes));
511
512
    // e. Let k be Get(nextItem, "0").
513
0
    auto keyRes = JSObject::getComputed_RJS(nextItem, runtime, zero);
514
0
    if (LLVM_UNLIKELY(keyRes == ExecutionStatus::EXCEPTION)) {
515
      // f. If k is an abrupt completion,
516
      //    return ? IteratorClose(iteratorRecord, k).
517
0
      return iteratorCloseAndRethrow(runtime, iteratorRecord.iterator);
518
0
    }
519
0
    key = std::move(*keyRes);
520
521
    // g. Let v be Get(nextItem, "1").
522
0
    auto valueRes = JSObject::getComputed_RJS(nextItem, runtime, one);
523
0
    if (LLVM_UNLIKELY(valueRes == ExecutionStatus::EXCEPTION)) {
524
      // h. If v is an abrupt completion,
525
      //    return ? IteratorClose(iteratorRecord, v).
526
0
      return iteratorCloseAndRethrow(runtime, iteratorRecord.iterator);
527
0
    }
528
0
    value = std::move(*valueRes);
529
530
    // i. Let status be Call(adder, target, « k.[[Value]], v.[[Value]] »).
531
0
    if (LLVM_UNLIKELY(
532
0
            adder(runtime, key, value) == ExecutionStatus::EXCEPTION)) {
533
      // j. If status is an abrupt completion,
534
      //    return ? IteratorClose(iteratorRecord, status).
535
0
      return iteratorCloseAndRethrow(runtime, iteratorRecord.iterator);
536
0
    }
537
0
  }
538
539
0
  llvm_unreachable(
540
0
      "loop must terminate with 'return' when iteration is complete");
541
0
}
Unexecuted instantiation: Map.cpp:hermes::vm::CallResult<hermes::vm::HermesValue, (hermes::vm::detail::CallResultSpecialize)2> hermes::vm::addEntriesFromIterable<hermes::vm::mapConstructor(void*, hermes::vm::Runtime&, hermes::vm::NativeArgs)::$_0>(hermes::vm::Runtime&, hermes::vm::Handle<hermes::vm::JSObject>, hermes::vm::Handle<hermes::vm::HermesValue>, hermes::vm::mapConstructor(void*, hermes::vm::Runtime&, hermes::vm::NativeArgs)::$_0)
Unexecuted instantiation: Object.cpp:hermes::vm::CallResult<hermes::vm::HermesValue, (hermes::vm::detail::CallResultSpecialize)2> hermes::vm::addEntriesFromIterable<hermes::vm::objectFromEntries(void*, hermes::vm::Runtime&, hermes::vm::NativeArgs)::$_0>(hermes::vm::Runtime&, hermes::vm::Handle<hermes::vm::JSObject>, hermes::vm::Handle<hermes::vm::HermesValue>, hermes::vm::objectFromEntries(void*, hermes::vm::Runtime&, hermes::vm::NativeArgs)::$_0)
542
543
#ifdef HERMES_ENABLE_IR_INSTRUMENTATION
544
/// Default no-op IR instrumentation hooks (__instrument).
545
Handle<JSObject> createInstrumentObject(Runtime &runtime);
546
#endif
547
548
} // namespace vm
549
550
#ifdef HERMES_ENABLE_INTL
551
namespace intl {
552
553
// TODO T65916424: Consider how we can move this somewhere more modular.
554
vm::Handle<vm::JSObject> createIntlObject(vm::Runtime &runtime);
555
556
} // namespace intl
557
#endif
558
559
} // namespace hermes
560
561
#endif // HERMES_VM_JSLIB_JSLIBINTERNAL_H