@ugo-studio/jspp 0.2.8 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/analysis/typeAnalyzer.js +42 -27
- package/dist/core/codegen/class-handlers.js +6 -6
- package/dist/core/codegen/control-flow-handlers.js +4 -4
- package/dist/core/codegen/declaration-handlers.js +21 -3
- package/dist/core/codegen/destructuring-handlers.js +187 -0
- package/dist/core/codegen/expression-handlers.js +7 -0
- package/dist/core/codegen/function-handlers.js +58 -36
- package/dist/core/codegen/helpers.js +288 -52
- package/dist/core/codegen/index.js +7 -4
- package/dist/core/codegen/statement-handlers.js +43 -23
- package/package.json +1 -1
- package/scripts/precompile-headers.ts +13 -5
- package/src/prelude/any_value.hpp +362 -361
- package/src/prelude/any_value_access.hpp +170 -170
- package/src/prelude/any_value_defines.hpp +189 -189
- package/src/prelude/any_value_helpers.hpp +374 -365
- package/src/prelude/library/array.hpp +185 -185
- package/src/prelude/library/console.hpp +111 -111
- package/src/prelude/library/error.hpp +112 -112
- package/src/prelude/library/function.hpp +10 -10
- package/src/prelude/library/math.hpp +307 -307
- package/src/prelude/library/object.hpp +275 -275
- package/src/prelude/library/performance.hpp +1 -1
- package/src/prelude/library/process.hpp +39 -39
- package/src/prelude/library/promise.hpp +123 -123
- package/src/prelude/library/symbol.hpp +52 -52
- package/src/prelude/library/timer.hpp +91 -91
- package/src/prelude/types.hpp +178 -178
- package/src/prelude/utils/access.hpp +411 -393
- package/src/prelude/utils/operators.hpp +336 -329
- package/src/prelude/values/array.hpp +0 -1
- package/src/prelude/values/async_iterator.hpp +83 -81
- package/src/prelude/values/function.hpp +82 -82
- package/src/prelude/values/helpers/array.hpp +198 -208
- package/src/prelude/values/helpers/async_iterator.hpp +275 -271
- package/src/prelude/values/helpers/function.hpp +108 -108
- package/src/prelude/values/helpers/iterator.hpp +144 -107
- package/src/prelude/values/helpers/promise.hpp +253 -253
- package/src/prelude/values/helpers/string.hpp +37 -47
- package/src/prelude/values/iterator.hpp +32 -5
- package/src/prelude/values/promise.hpp +72 -72
- package/src/prelude/values/prototypes/array.hpp +54 -42
- package/src/prelude/values/prototypes/iterator.hpp +201 -74
- package/src/prelude/values/prototypes/promise.hpp +196 -196
- package/src/prelude/values/prototypes/string.hpp +564 -542
- package/src/prelude/values/string.hpp +25 -26
|
@@ -1,276 +1,276 @@
|
|
|
1
|
-
#pragma once
|
|
2
|
-
|
|
3
|
-
#include "types.hpp"
|
|
4
|
-
#include "any_value.hpp"
|
|
5
|
-
#include "utils/access.hpp"
|
|
6
|
-
#include "exception.hpp"
|
|
7
|
-
|
|
8
|
-
// Define Object constructor
|
|
9
|
-
inline auto Object = jspp::AnyValue::make_class([](
|
|
10
|
-
{
|
|
11
|
-
if (args.empty() || args[0].is_undefined() || args[0].is_null()) {
|
|
12
|
-
return jspp::AnyValue::make_object({});
|
|
13
|
-
}
|
|
14
|
-
// Return argument if it is an object
|
|
15
|
-
if (args[0].is_object() || args[0].is_array() || args[0].is_function() || args[0].is_promise() || args[0].is_iterator()) {
|
|
16
|
-
return args[0];
|
|
17
|
-
}
|
|
18
|
-
// TODO: Wrapper objects for primitives
|
|
19
|
-
return jspp::AnyValue::make_object({}); }, "Object");
|
|
20
|
-
|
|
21
|
-
struct ObjectInit
|
|
22
|
-
{
|
|
23
|
-
ObjectInit()
|
|
24
|
-
{
|
|
25
|
-
// Object.keys(obj)
|
|
26
|
-
Object.define_data_property("keys", jspp::AnyValue::make_function([](
|
|
27
|
-
{
|
|
28
|
-
if (args.empty()) throw jspp::Exception::make_exception("Object.keys called on non-object", "TypeError");
|
|
29
|
-
auto obj = args[0];
|
|
30
|
-
if (obj.is_null() || obj.is_undefined()) throw jspp::Exception::make_exception("Object.keys called on null or undefined", "TypeError");
|
|
31
|
-
|
|
32
|
-
auto keys = jspp::Access::get_object_keys(obj);
|
|
33
|
-
std::vector<jspp::AnyValue> keyValues;
|
|
34
|
-
for(const auto& k : keys) {
|
|
35
|
-
keyValues.push_back(jspp::AnyValue::make_string(k));
|
|
36
|
-
}
|
|
37
|
-
return jspp::AnyValue::make_array(std::move(keyValues)); }, "keys"));
|
|
38
|
-
|
|
39
|
-
// Object.values(obj)
|
|
40
|
-
Object.define_data_property("values", jspp::AnyValue::make_function([](
|
|
41
|
-
{
|
|
42
|
-
if (args.empty()) throw jspp::Exception::make_exception("Object.values called on non-object", "TypeError");
|
|
43
|
-
auto obj = args[0];
|
|
44
|
-
if (obj.is_null() || obj.is_undefined()) throw jspp::Exception::make_exception("Object.values called on null or undefined", "TypeError");
|
|
45
|
-
|
|
46
|
-
auto keys = jspp::Access::get_object_keys(obj);
|
|
47
|
-
std::vector<jspp::AnyValue> values;
|
|
48
|
-
for(const auto& k : keys) {
|
|
49
|
-
values.push_back(obj.get_property_with_receiver(k, obj));
|
|
50
|
-
}
|
|
51
|
-
return jspp::AnyValue::make_array(std::move(values)); }, "values"));
|
|
52
|
-
|
|
53
|
-
// Object.entries(obj)
|
|
54
|
-
Object.define_data_property("entries", jspp::AnyValue::make_function([](
|
|
55
|
-
{
|
|
56
|
-
if (args.empty()) throw jspp::Exception::make_exception("Object.entries called on non-object", "TypeError");
|
|
57
|
-
auto obj = args[0];
|
|
58
|
-
if (obj.is_null() || obj.is_undefined()) throw jspp::Exception::make_exception("Object.entries called on null or undefined", "TypeError");
|
|
59
|
-
|
|
60
|
-
auto keys = jspp::Access::get_object_keys(obj);
|
|
61
|
-
std::vector<jspp::AnyValue> entries;
|
|
62
|
-
for(const auto& k : keys) {
|
|
63
|
-
std::vector<jspp::AnyValue> entry;
|
|
64
|
-
entry.push_back(jspp::AnyValue::make_string(k));
|
|
65
|
-
entry.push_back(obj.get_property_with_receiver(k, obj));
|
|
66
|
-
entries.push_back(jspp::AnyValue::make_array(std::move(entry)));
|
|
67
|
-
}
|
|
68
|
-
return jspp::AnyValue::make_array(std::move(entries)); }, "entries"));
|
|
69
|
-
|
|
70
|
-
// Object.assign(target, ...sources)
|
|
71
|
-
Object.define_data_property("assign", jspp::AnyValue::make_function([](
|
|
72
|
-
{
|
|
73
|
-
if (args.empty()) throw jspp::Exception::make_exception("Cannot convert undefined or null to object", "TypeError");
|
|
74
|
-
auto target = args[0];
|
|
75
|
-
if (target.is_null() || target.is_undefined()) throw jspp::Exception::make_exception("Cannot convert undefined or null to object", "TypeError");
|
|
76
|
-
|
|
77
|
-
for (size_t i = 1; i < args.size(); ++i) {
|
|
78
|
-
auto source = args[i];
|
|
79
|
-
if (source.is_null() || source.is_undefined()) continue;
|
|
80
|
-
|
|
81
|
-
auto keys = jspp::Access::get_object_keys(source);
|
|
82
|
-
for(const auto& k : keys) {
|
|
83
|
-
auto val = source.get_property_with_receiver(k, source);
|
|
84
|
-
target.set_own_property(k, val);
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
return target; }, "assign"));
|
|
88
|
-
|
|
89
|
-
// Object.is(value1, value2)
|
|
90
|
-
Object.define_data_property("is", jspp::AnyValue::make_function([](
|
|
91
|
-
{
|
|
92
|
-
jspp::AnyValue v1 = args.size() > 0 ? args[0] : jspp::Constants::UNDEFINED;
|
|
93
|
-
jspp::AnyValue v2 = args.size() > 1 ? args[1] : jspp::Constants::UNDEFINED;
|
|
94
|
-
|
|
95
|
-
if (v1.is_number() && v2.is_number()) {
|
|
96
|
-
double d1 = v1.as_double();
|
|
97
|
-
double d2 = v2.as_double();
|
|
98
|
-
if (std::isnan(d1) && std::isnan(d2)) return jspp::Constants::TRUE;
|
|
99
|
-
if (d1 == 0 && d2 == 0) {
|
|
100
|
-
return jspp::AnyValue::make_boolean(std::signbit(d1) == std::signbit(d2));
|
|
101
|
-
}
|
|
102
|
-
return jspp::AnyValue::make_boolean(d1 == d2);
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
return jspp::is_strictly_equal_to(v1, v2); }, "is"));
|
|
106
|
-
|
|
107
|
-
// Object.getPrototypeOf(obj)
|
|
108
|
-
Object.define_data_property("getPrototypeOf", jspp::AnyValue::make_function([](
|
|
109
|
-
{
|
|
110
|
-
if (args.empty()) throw jspp::Exception::make_exception("Object.getPrototypeOf called on non-object", "TypeError");
|
|
111
|
-
auto obj = args[0];
|
|
112
|
-
|
|
113
|
-
if (obj.is_object()) {
|
|
114
|
-
return obj.as_object()->proto;
|
|
115
|
-
}
|
|
116
|
-
if (obj.is_array()) {
|
|
117
|
-
return obj.as_array()->proto;
|
|
118
|
-
}
|
|
119
|
-
if (obj.is_function()) {
|
|
120
|
-
return obj.as_function()->proto;
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
return jspp::Constants::Null; }, "getPrototypeOf"));
|
|
124
|
-
|
|
125
|
-
// Object.setPrototypeOf(obj, proto)
|
|
126
|
-
Object.define_data_property("setPrototypeOf", jspp::AnyValue::make_function([](
|
|
127
|
-
{
|
|
128
|
-
if (args.size() < 2) throw jspp::Exception::make_exception("Object.setPrototypeOf requires at least 2 arguments", "TypeError");
|
|
129
|
-
auto obj = args[0];
|
|
130
|
-
auto proto = args[1];
|
|
131
|
-
|
|
132
|
-
if (!proto.is_object() && !proto.is_null()) {
|
|
133
|
-
throw jspp::Exception::make_exception("Object prototype may only be an Object or null", "TypeError");
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
if (obj.is_object()) {
|
|
137
|
-
obj.as_object()->proto = proto;
|
|
138
|
-
} else if (obj.is_array()) {
|
|
139
|
-
obj.as_array()->proto = proto;
|
|
140
|
-
} else if (obj.is_function()) {
|
|
141
|
-
obj.as_function()->proto = proto;
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
return obj; }, "setPrototypeOf"));
|
|
145
|
-
|
|
146
|
-
// Object.create(proto, [propertiesObject])
|
|
147
|
-
Object.define_data_property("create", jspp::AnyValue::make_function([](
|
|
148
|
-
{
|
|
149
|
-
if (args.empty()) throw jspp::Exception::make_exception("Object prototype may only be an Object or null", "TypeError");
|
|
150
|
-
auto proto = args[0];
|
|
151
|
-
if (!proto.is_object() && !proto.is_null()) {
|
|
152
|
-
throw jspp::Exception::make_exception("Object prototype may only be an Object or null", "TypeError");
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
auto newObj = jspp::AnyValue::make_object_with_proto({}, proto);
|
|
156
|
-
|
|
157
|
-
if (args.size() > 1 && !args[1].is_undefined()) {
|
|
158
|
-
// Object.defineProperties(newObj, propertiesObject)
|
|
159
|
-
// TODO: implement defineProperties logic if needed.
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
return newObj; }, "create"));
|
|
163
|
-
|
|
164
|
-
// Object.defineProperty(obj, prop, descriptor)
|
|
165
|
-
Object.define_data_property("defineProperty", jspp::AnyValue::make_function([](
|
|
166
|
-
{
|
|
167
|
-
if (args.size() < 3) throw jspp::Exception::make_exception("Object.defineProperty requires 3 arguments", "TypeError");
|
|
168
|
-
auto obj = args[0];
|
|
169
|
-
if (!obj.is_object() && !obj.is_array() && !obj.is_function()) throw jspp::Exception::make_exception("Object.defineProperty called on non-object", "TypeError");
|
|
170
|
-
|
|
171
|
-
std::string prop = args[1].to_std_string();
|
|
172
|
-
auto descObj = args[2];
|
|
173
|
-
|
|
174
|
-
bool enumerable = false;
|
|
175
|
-
bool configurable = false;
|
|
176
|
-
bool writable = false;
|
|
177
|
-
|
|
178
|
-
if (descObj.has_property("enumerable")) enumerable = jspp::is_truthy(descObj.get_own_property("enumerable"));
|
|
179
|
-
if (descObj.has_property("configurable")) configurable = jspp::is_truthy(descObj.get_own_property("configurable"));
|
|
180
|
-
if (descObj.has_property("writable")) writable = jspp::is_truthy(descObj.get_own_property("writable"));
|
|
181
|
-
|
|
182
|
-
bool hasValue = descObj.has_property("value");
|
|
183
|
-
bool hasGet = descObj.has_property("get");
|
|
184
|
-
bool hasSet = descObj.has_property("set");
|
|
185
|
-
|
|
186
|
-
if (hasValue && (hasGet || hasSet)) {
|
|
187
|
-
throw jspp::Exception::make_exception("Invalid property descriptor. Cannot both specify accessors and a value or writable attribute", "TypeError");
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
if (hasValue) {
|
|
191
|
-
auto value = descObj.get_own_property("value");
|
|
192
|
-
obj.define_data_property(prop, value, writable, enumerable, configurable);
|
|
193
|
-
} else {
|
|
194
|
-
jspp::AnyValue getter = jspp::Constants::UNDEFINED;
|
|
195
|
-
jspp::AnyValue setter = jspp::Constants::UNDEFINED;
|
|
196
|
-
|
|
197
|
-
if (hasGet) getter = descObj.get_own_property("get");
|
|
198
|
-
if (hasSet) setter = descObj.get_own_property("set");
|
|
199
|
-
|
|
200
|
-
if (!getter.is_undefined() && !getter.is_function()) throw jspp::Exception::make_exception("Getter must be a function: " + getter.to_std_string(), "TypeError");
|
|
201
|
-
if (!setter.is_undefined() && !setter.is_function()) throw jspp::Exception::make_exception("Setter must be a function", "TypeError");
|
|
202
|
-
|
|
203
|
-
if (obj.is_object()) {
|
|
204
|
-
auto o_ptr = obj.as_object();
|
|
205
|
-
std::optional<std::function<jspp::AnyValue(
|
|
206
|
-
std::optional<std::function<jspp::AnyValue(
|
|
207
|
-
|
|
208
|
-
if (getter.is_function()) {
|
|
209
|
-
getFunc = [getter](
|
|
210
|
-
return getter.call(thisVal, args);
|
|
211
|
-
};
|
|
212
|
-
}
|
|
213
|
-
if (setter.is_function()) {
|
|
214
|
-
setFunc = [setter](
|
|
215
|
-
return setter.call(thisVal, args);
|
|
216
|
-
};
|
|
217
|
-
}
|
|
218
|
-
|
|
219
|
-
auto desc = jspp::AnyValue::make_accessor_descriptor(getFunc, setFunc, enumerable, configurable);
|
|
220
|
-
auto offset = o_ptr->shape->get_offset(prop);
|
|
221
|
-
if (offset.has_value()) {
|
|
222
|
-
o_ptr->storage[offset.value()] = desc;
|
|
223
|
-
} else {
|
|
224
|
-
o_ptr->shape = o_ptr->shape->transition(prop);
|
|
225
|
-
o_ptr->storage.push_back(desc);
|
|
226
|
-
}
|
|
227
|
-
}
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
return obj; }, "defineProperty"));
|
|
231
|
-
|
|
232
|
-
// Object.hasOwn(obj, prop)
|
|
233
|
-
Object.define_data_property("hasOwn", jspp::AnyValue::make_function([](
|
|
234
|
-
{
|
|
235
|
-
if (args.empty()) throw jspp::Exception::make_exception("Object.hasOwn called on non-object", "TypeError");
|
|
236
|
-
auto obj = args[0];
|
|
237
|
-
if (obj.is_null() || obj.is_undefined()) throw jspp::Exception::make_exception("Object.hasOwn called on null or undefined", "TypeError");
|
|
238
|
-
std::string prop = args.size() > 1 ? args[1].to_std_string() : "undefined";
|
|
239
|
-
|
|
240
|
-
if (obj.is_object()) return jspp::AnyValue::make_boolean(obj.as_object()->shape->get_offset(prop).has_value());
|
|
241
|
-
if (obj.is_function()) return jspp::AnyValue::make_boolean(obj.as_function()->props.count(prop));
|
|
242
|
-
if (obj.is_array()) {
|
|
243
|
-
if (prop == "length") return jspp::Constants::TRUE;
|
|
244
|
-
if (jspp::JsArray::is_array_index(prop)) {
|
|
245
|
-
uint32_t idx = static_cast<uint32_t>(std::stoull(prop));
|
|
246
|
-
auto arr = obj.as_array();
|
|
247
|
-
if (idx < arr->dense.size() && !(arr->dense[idx].is_uninitialized())) return jspp::Constants::TRUE;
|
|
248
|
-
if (arr->sparse.count(idx)) return jspp::Constants::TRUE;
|
|
249
|
-
return jspp::Constants::FALSE;
|
|
250
|
-
}
|
|
251
|
-
return jspp::AnyValue::make_boolean(obj.as_array()->props.count(prop));
|
|
252
|
-
}
|
|
253
|
-
|
|
254
|
-
return jspp::Constants::FALSE; }, "hasOwn"));
|
|
255
|
-
|
|
256
|
-
// Object.prototype.hasOwnProperty
|
|
257
|
-
auto proto = Object.get_own_property("prototype");
|
|
258
|
-
proto.define_data_property("hasOwnProperty", jspp::AnyValue::make_function([](
|
|
259
|
-
{
|
|
260
|
-
std::string prop = args.size() > 0 ? args[0].to_std_string() : "undefined";
|
|
261
|
-
if (thisVal.is_object()) return jspp::AnyValue::make_boolean(thisVal.as_object()->shape->get_offset(prop).has_value());
|
|
262
|
-
if (thisVal.is_function()) return jspp::AnyValue::make_boolean(thisVal.as_function()->props.count(prop));
|
|
263
|
-
if (thisVal.is_array()) {
|
|
264
|
-
if (prop == "length") return jspp::Constants::TRUE;
|
|
265
|
-
if (jspp::JsArray::is_array_index(prop)) {
|
|
266
|
-
uint32_t idx = static_cast<uint32_t>(std::stoull(prop));
|
|
267
|
-
auto arr = thisVal.as_array();
|
|
268
|
-
if (idx < arr->dense.size() && !(arr->dense[idx].is_uninitialized())) return jspp::Constants::TRUE;
|
|
269
|
-
if (arr->sparse.count(idx)) return jspp::Constants::TRUE;
|
|
270
|
-
return jspp::Constants::FALSE;
|
|
271
|
-
}
|
|
272
|
-
return jspp::AnyValue::make_boolean(thisVal.as_array()->props.count(prop));
|
|
273
|
-
}
|
|
274
|
-
return jspp::Constants::FALSE; }, "hasOwnProperty"));
|
|
275
|
-
}
|
|
1
|
+
#pragma once
|
|
2
|
+
|
|
3
|
+
#include "types.hpp"
|
|
4
|
+
#include "any_value.hpp"
|
|
5
|
+
#include "utils/access.hpp"
|
|
6
|
+
#include "exception.hpp"
|
|
7
|
+
|
|
8
|
+
// Define Object constructor
|
|
9
|
+
inline auto Object = jspp::AnyValue::make_class([](jspp::AnyValue thisVal, std::span<const jspp::AnyValue> args) -> jspp::AnyValue
|
|
10
|
+
{
|
|
11
|
+
if (args.empty() || args[0].is_undefined() || args[0].is_null()) {
|
|
12
|
+
return jspp::AnyValue::make_object({});
|
|
13
|
+
}
|
|
14
|
+
// Return argument if it is an object
|
|
15
|
+
if (args[0].is_object() || args[0].is_array() || args[0].is_function() || args[0].is_promise() || args[0].is_iterator()) {
|
|
16
|
+
return args[0];
|
|
17
|
+
}
|
|
18
|
+
// TODO: Wrapper objects for primitives
|
|
19
|
+
return jspp::AnyValue::make_object({}); }, "Object");
|
|
20
|
+
|
|
21
|
+
struct ObjectInit
|
|
22
|
+
{
|
|
23
|
+
ObjectInit()
|
|
24
|
+
{
|
|
25
|
+
// Object.keys(obj)
|
|
26
|
+
Object.define_data_property("keys", jspp::AnyValue::make_function([](jspp::AnyValue, std::span<const jspp::AnyValue> args) -> jspp::AnyValue
|
|
27
|
+
{
|
|
28
|
+
if (args.empty()) throw jspp::Exception::make_exception("Object.keys called on non-object", "TypeError");
|
|
29
|
+
auto obj = args[0];
|
|
30
|
+
if (obj.is_null() || obj.is_undefined()) throw jspp::Exception::make_exception("Object.keys called on null or undefined", "TypeError");
|
|
31
|
+
|
|
32
|
+
auto keys = jspp::Access::get_object_keys(obj);
|
|
33
|
+
std::vector<jspp::AnyValue> keyValues;
|
|
34
|
+
for(const auto& k : keys) {
|
|
35
|
+
keyValues.push_back(jspp::AnyValue::make_string(k));
|
|
36
|
+
}
|
|
37
|
+
return jspp::AnyValue::make_array(std::move(keyValues)); }, "keys"));
|
|
38
|
+
|
|
39
|
+
// Object.values(obj)
|
|
40
|
+
Object.define_data_property("values", jspp::AnyValue::make_function([](jspp::AnyValue, std::span<const jspp::AnyValue> args) -> jspp::AnyValue
|
|
41
|
+
{
|
|
42
|
+
if (args.empty()) throw jspp::Exception::make_exception("Object.values called on non-object", "TypeError");
|
|
43
|
+
auto obj = args[0];
|
|
44
|
+
if (obj.is_null() || obj.is_undefined()) throw jspp::Exception::make_exception("Object.values called on null or undefined", "TypeError");
|
|
45
|
+
|
|
46
|
+
auto keys = jspp::Access::get_object_keys(obj);
|
|
47
|
+
std::vector<jspp::AnyValue> values;
|
|
48
|
+
for(const auto& k : keys) {
|
|
49
|
+
values.push_back(obj.get_property_with_receiver(k, obj));
|
|
50
|
+
}
|
|
51
|
+
return jspp::AnyValue::make_array(std::move(values)); }, "values"));
|
|
52
|
+
|
|
53
|
+
// Object.entries(obj)
|
|
54
|
+
Object.define_data_property("entries", jspp::AnyValue::make_function([](jspp::AnyValue, std::span<const jspp::AnyValue> args) -> jspp::AnyValue
|
|
55
|
+
{
|
|
56
|
+
if (args.empty()) throw jspp::Exception::make_exception("Object.entries called on non-object", "TypeError");
|
|
57
|
+
auto obj = args[0];
|
|
58
|
+
if (obj.is_null() || obj.is_undefined()) throw jspp::Exception::make_exception("Object.entries called on null or undefined", "TypeError");
|
|
59
|
+
|
|
60
|
+
auto keys = jspp::Access::get_object_keys(obj);
|
|
61
|
+
std::vector<jspp::AnyValue> entries;
|
|
62
|
+
for(const auto& k : keys) {
|
|
63
|
+
std::vector<jspp::AnyValue> entry;
|
|
64
|
+
entry.push_back(jspp::AnyValue::make_string(k));
|
|
65
|
+
entry.push_back(obj.get_property_with_receiver(k, obj));
|
|
66
|
+
entries.push_back(jspp::AnyValue::make_array(std::move(entry)));
|
|
67
|
+
}
|
|
68
|
+
return jspp::AnyValue::make_array(std::move(entries)); }, "entries"));
|
|
69
|
+
|
|
70
|
+
// Object.assign(target, ...sources)
|
|
71
|
+
Object.define_data_property("assign", jspp::AnyValue::make_function([](jspp::AnyValue, std::span<const jspp::AnyValue> args) -> jspp::AnyValue
|
|
72
|
+
{
|
|
73
|
+
if (args.empty()) throw jspp::Exception::make_exception("Cannot convert undefined or null to object", "TypeError");
|
|
74
|
+
auto target = args[0];
|
|
75
|
+
if (target.is_null() || target.is_undefined()) throw jspp::Exception::make_exception("Cannot convert undefined or null to object", "TypeError");
|
|
76
|
+
|
|
77
|
+
for (size_t i = 1; i < args.size(); ++i) {
|
|
78
|
+
auto source = args[i];
|
|
79
|
+
if (source.is_null() || source.is_undefined()) continue;
|
|
80
|
+
|
|
81
|
+
auto keys = jspp::Access::get_object_keys(source);
|
|
82
|
+
for(const auto& k : keys) {
|
|
83
|
+
auto val = source.get_property_with_receiver(k, source);
|
|
84
|
+
target.set_own_property(k, val);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
return target; }, "assign"));
|
|
88
|
+
|
|
89
|
+
// Object.is(value1, value2)
|
|
90
|
+
Object.define_data_property("is", jspp::AnyValue::make_function([](jspp::AnyValue, std::span<const jspp::AnyValue> args) -> jspp::AnyValue
|
|
91
|
+
{
|
|
92
|
+
jspp::AnyValue v1 = args.size() > 0 ? args[0] : jspp::Constants::UNDEFINED;
|
|
93
|
+
jspp::AnyValue v2 = args.size() > 1 ? args[1] : jspp::Constants::UNDEFINED;
|
|
94
|
+
|
|
95
|
+
if (v1.is_number() && v2.is_number()) {
|
|
96
|
+
double d1 = v1.as_double();
|
|
97
|
+
double d2 = v2.as_double();
|
|
98
|
+
if (std::isnan(d1) && std::isnan(d2)) return jspp::Constants::TRUE;
|
|
99
|
+
if (d1 == 0 && d2 == 0) {
|
|
100
|
+
return jspp::AnyValue::make_boolean(std::signbit(d1) == std::signbit(d2));
|
|
101
|
+
}
|
|
102
|
+
return jspp::AnyValue::make_boolean(d1 == d2);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
return jspp::is_strictly_equal_to(v1, v2); }, "is"));
|
|
106
|
+
|
|
107
|
+
// Object.getPrototypeOf(obj)
|
|
108
|
+
Object.define_data_property("getPrototypeOf", jspp::AnyValue::make_function([](jspp::AnyValue, std::span<const jspp::AnyValue> args) -> jspp::AnyValue
|
|
109
|
+
{
|
|
110
|
+
if (args.empty()) throw jspp::Exception::make_exception("Object.getPrototypeOf called on non-object", "TypeError");
|
|
111
|
+
auto obj = args[0];
|
|
112
|
+
|
|
113
|
+
if (obj.is_object()) {
|
|
114
|
+
return obj.as_object()->proto;
|
|
115
|
+
}
|
|
116
|
+
if (obj.is_array()) {
|
|
117
|
+
return obj.as_array()->proto;
|
|
118
|
+
}
|
|
119
|
+
if (obj.is_function()) {
|
|
120
|
+
return obj.as_function()->proto;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
return jspp::Constants::Null; }, "getPrototypeOf"));
|
|
124
|
+
|
|
125
|
+
// Object.setPrototypeOf(obj, proto)
|
|
126
|
+
Object.define_data_property("setPrototypeOf", jspp::AnyValue::make_function([](jspp::AnyValue, std::span<const jspp::AnyValue> args) -> jspp::AnyValue
|
|
127
|
+
{
|
|
128
|
+
if (args.size() < 2) throw jspp::Exception::make_exception("Object.setPrototypeOf requires at least 2 arguments", "TypeError");
|
|
129
|
+
auto obj = args[0];
|
|
130
|
+
auto proto = args[1];
|
|
131
|
+
|
|
132
|
+
if (!proto.is_object() && !proto.is_null()) {
|
|
133
|
+
throw jspp::Exception::make_exception("Object prototype may only be an Object or null", "TypeError");
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
if (obj.is_object()) {
|
|
137
|
+
obj.as_object()->proto = proto;
|
|
138
|
+
} else if (obj.is_array()) {
|
|
139
|
+
obj.as_array()->proto = proto;
|
|
140
|
+
} else if (obj.is_function()) {
|
|
141
|
+
obj.as_function()->proto = proto;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
return obj; }, "setPrototypeOf"));
|
|
145
|
+
|
|
146
|
+
// Object.create(proto, [propertiesObject])
|
|
147
|
+
Object.define_data_property("create", jspp::AnyValue::make_function([](jspp::AnyValue, std::span<const jspp::AnyValue> args) -> jspp::AnyValue
|
|
148
|
+
{
|
|
149
|
+
if (args.empty()) throw jspp::Exception::make_exception("Object prototype may only be an Object or null", "TypeError");
|
|
150
|
+
auto proto = args[0];
|
|
151
|
+
if (!proto.is_object() && !proto.is_null()) {
|
|
152
|
+
throw jspp::Exception::make_exception("Object prototype may only be an Object or null", "TypeError");
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
auto newObj = jspp::AnyValue::make_object_with_proto({}, proto);
|
|
156
|
+
|
|
157
|
+
if (args.size() > 1 && !args[1].is_undefined()) {
|
|
158
|
+
// Object.defineProperties(newObj, propertiesObject)
|
|
159
|
+
// TODO: implement defineProperties logic if needed.
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
return newObj; }, "create"));
|
|
163
|
+
|
|
164
|
+
// Object.defineProperty(obj, prop, descriptor)
|
|
165
|
+
Object.define_data_property("defineProperty", jspp::AnyValue::make_function([](jspp::AnyValue, std::span<const jspp::AnyValue> args) -> jspp::AnyValue
|
|
166
|
+
{
|
|
167
|
+
if (args.size() < 3) throw jspp::Exception::make_exception("Object.defineProperty requires 3 arguments", "TypeError");
|
|
168
|
+
auto obj = args[0];
|
|
169
|
+
if (!obj.is_object() && !obj.is_array() && !obj.is_function()) throw jspp::Exception::make_exception("Object.defineProperty called on non-object", "TypeError");
|
|
170
|
+
|
|
171
|
+
std::string prop = args[1].to_std_string();
|
|
172
|
+
auto descObj = args[2];
|
|
173
|
+
|
|
174
|
+
bool enumerable = false;
|
|
175
|
+
bool configurable = false;
|
|
176
|
+
bool writable = false;
|
|
177
|
+
|
|
178
|
+
if (descObj.has_property("enumerable")) enumerable = jspp::is_truthy(descObj.get_own_property("enumerable"));
|
|
179
|
+
if (descObj.has_property("configurable")) configurable = jspp::is_truthy(descObj.get_own_property("configurable"));
|
|
180
|
+
if (descObj.has_property("writable")) writable = jspp::is_truthy(descObj.get_own_property("writable"));
|
|
181
|
+
|
|
182
|
+
bool hasValue = descObj.has_property("value");
|
|
183
|
+
bool hasGet = descObj.has_property("get");
|
|
184
|
+
bool hasSet = descObj.has_property("set");
|
|
185
|
+
|
|
186
|
+
if (hasValue && (hasGet || hasSet)) {
|
|
187
|
+
throw jspp::Exception::make_exception("Invalid property descriptor. Cannot both specify accessors and a value or writable attribute", "TypeError");
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
if (hasValue) {
|
|
191
|
+
auto value = descObj.get_own_property("value");
|
|
192
|
+
obj.define_data_property(prop, value, writable, enumerable, configurable);
|
|
193
|
+
} else {
|
|
194
|
+
jspp::AnyValue getter = jspp::Constants::UNDEFINED;
|
|
195
|
+
jspp::AnyValue setter = jspp::Constants::UNDEFINED;
|
|
196
|
+
|
|
197
|
+
if (hasGet) getter = descObj.get_own_property("get");
|
|
198
|
+
if (hasSet) setter = descObj.get_own_property("set");
|
|
199
|
+
|
|
200
|
+
if (!getter.is_undefined() && !getter.is_function()) throw jspp::Exception::make_exception("Getter must be a function: " + getter.to_std_string(), "TypeError");
|
|
201
|
+
if (!setter.is_undefined() && !setter.is_function()) throw jspp::Exception::make_exception("Setter must be a function", "TypeError");
|
|
202
|
+
|
|
203
|
+
if (obj.is_object()) {
|
|
204
|
+
auto o_ptr = obj.as_object();
|
|
205
|
+
std::optional<std::function<jspp::AnyValue(jspp::AnyValue, std::span<const jspp::AnyValue>)>> getFunc;
|
|
206
|
+
std::optional<std::function<jspp::AnyValue(jspp::AnyValue, std::span<const jspp::AnyValue>)>> setFunc;
|
|
207
|
+
|
|
208
|
+
if (getter.is_function()) {
|
|
209
|
+
getFunc = [getter](jspp::AnyValue thisVal, std::span<const jspp::AnyValue> args) -> jspp::AnyValue {
|
|
210
|
+
return getter.call(thisVal, args);
|
|
211
|
+
};
|
|
212
|
+
}
|
|
213
|
+
if (setter.is_function()) {
|
|
214
|
+
setFunc = [setter](jspp::AnyValue thisVal, std::span<const jspp::AnyValue> args) -> jspp::AnyValue {
|
|
215
|
+
return setter.call(thisVal, args);
|
|
216
|
+
};
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
auto desc = jspp::AnyValue::make_accessor_descriptor(getFunc, setFunc, enumerable, configurable);
|
|
220
|
+
auto offset = o_ptr->shape->get_offset(prop);
|
|
221
|
+
if (offset.has_value()) {
|
|
222
|
+
o_ptr->storage[offset.value()] = desc;
|
|
223
|
+
} else {
|
|
224
|
+
o_ptr->shape = o_ptr->shape->transition(prop);
|
|
225
|
+
o_ptr->storage.push_back(desc);
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
return obj; }, "defineProperty"));
|
|
231
|
+
|
|
232
|
+
// Object.hasOwn(obj, prop)
|
|
233
|
+
Object.define_data_property("hasOwn", jspp::AnyValue::make_function([](jspp::AnyValue, std::span<const jspp::AnyValue> args) -> jspp::AnyValue
|
|
234
|
+
{
|
|
235
|
+
if (args.empty()) throw jspp::Exception::make_exception("Object.hasOwn called on non-object", "TypeError");
|
|
236
|
+
auto obj = args[0];
|
|
237
|
+
if (obj.is_null() || obj.is_undefined()) throw jspp::Exception::make_exception("Object.hasOwn called on null or undefined", "TypeError");
|
|
238
|
+
std::string prop = args.size() > 1 ? args[1].to_std_string() : "undefined";
|
|
239
|
+
|
|
240
|
+
if (obj.is_object()) return jspp::AnyValue::make_boolean(obj.as_object()->shape->get_offset(prop).has_value());
|
|
241
|
+
if (obj.is_function()) return jspp::AnyValue::make_boolean(obj.as_function()->props.count(prop));
|
|
242
|
+
if (obj.is_array()) {
|
|
243
|
+
if (prop == "length") return jspp::Constants::TRUE;
|
|
244
|
+
if (jspp::JsArray::is_array_index(prop)) {
|
|
245
|
+
uint32_t idx = static_cast<uint32_t>(std::stoull(prop));
|
|
246
|
+
auto arr = obj.as_array();
|
|
247
|
+
if (idx < arr->dense.size() && !(arr->dense[idx].is_uninitialized())) return jspp::Constants::TRUE;
|
|
248
|
+
if (arr->sparse.count(idx)) return jspp::Constants::TRUE;
|
|
249
|
+
return jspp::Constants::FALSE;
|
|
250
|
+
}
|
|
251
|
+
return jspp::AnyValue::make_boolean(obj.as_array()->props.count(prop));
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
return jspp::Constants::FALSE; }, "hasOwn"));
|
|
255
|
+
|
|
256
|
+
// Object.prototype.hasOwnProperty
|
|
257
|
+
auto proto = Object.get_own_property("prototype");
|
|
258
|
+
proto.define_data_property("hasOwnProperty", jspp::AnyValue::make_function([](jspp::AnyValue thisVal, std::span<const jspp::AnyValue> args) -> jspp::AnyValue
|
|
259
|
+
{
|
|
260
|
+
std::string prop = args.size() > 0 ? args[0].to_std_string() : "undefined";
|
|
261
|
+
if (thisVal.is_object()) return jspp::AnyValue::make_boolean(thisVal.as_object()->shape->get_offset(prop).has_value());
|
|
262
|
+
if (thisVal.is_function()) return jspp::AnyValue::make_boolean(thisVal.as_function()->props.count(prop));
|
|
263
|
+
if (thisVal.is_array()) {
|
|
264
|
+
if (prop == "length") return jspp::Constants::TRUE;
|
|
265
|
+
if (jspp::JsArray::is_array_index(prop)) {
|
|
266
|
+
uint32_t idx = static_cast<uint32_t>(std::stoull(prop));
|
|
267
|
+
auto arr = thisVal.as_array();
|
|
268
|
+
if (idx < arr->dense.size() && !(arr->dense[idx].is_uninitialized())) return jspp::Constants::TRUE;
|
|
269
|
+
if (arr->sparse.count(idx)) return jspp::Constants::TRUE;
|
|
270
|
+
return jspp::Constants::FALSE;
|
|
271
|
+
}
|
|
272
|
+
return jspp::AnyValue::make_boolean(thisVal.as_array()->props.count(prop));
|
|
273
|
+
}
|
|
274
|
+
return jspp::Constants::FALSE; }, "hasOwnProperty"));
|
|
275
|
+
}
|
|
276
276
|
} objectInit;
|
|
@@ -13,7 +13,7 @@ inline auto performance = jspp::AnyValue::make_object({
|
|
|
13
13
|
// [C++14 Feature] Generalized Lambda Capture
|
|
14
14
|
// We initialize 'startTime' RIGHT HERE inside the [].
|
|
15
15
|
// It acts like a private variable stored inside this specific function.
|
|
16
|
-
[startTime = std::chrono::steady_clock::now()](
|
|
16
|
+
[startTime = std::chrono::steady_clock::now()](jspp::AnyValue, std::span<const jspp::AnyValue>)
|
|
17
17
|
{
|
|
18
18
|
// We calculate the diff against the captured startTime
|
|
19
19
|
std::chrono::duration<double, std::milli> duration =
|