/src/moddable/xs/sources/xsObject.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright (c) 2016-2017 Moddable Tech, Inc. |
3 | | * |
4 | | * This file is part of the Moddable SDK Runtime. |
5 | | * |
6 | | * The Moddable SDK Runtime is free software: you can redistribute it and/or modify |
7 | | * it under the terms of the GNU Lesser General Public License as published by |
8 | | * the Free Software Foundation, either version 3 of the License, or |
9 | | * (at your option) any later version. |
10 | | * |
11 | | * The Moddable SDK Runtime is distributed in the hope that it will be useful, |
12 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 | | * GNU Lesser General Public License for more details. |
15 | | * |
16 | | * You should have received a copy of the GNU Lesser General Public License |
17 | | * along with the Moddable SDK Runtime. If not, see <http://www.gnu.org/licenses/>. |
18 | | * |
19 | | * This file incorporates work covered by the following copyright and |
20 | | * permission notice: |
21 | | * |
22 | | * Copyright (C) 2010-2016 Marvell International Ltd. |
23 | | * Copyright (C) 2002-2010 Kinoma, Inc. |
24 | | * |
25 | | * Licensed under the Apache License, Version 2.0 (the "License"); |
26 | | * you may not use this file except in compliance with the License. |
27 | | * You may obtain a copy of the License at |
28 | | * |
29 | | * http://www.apache.org/licenses/LICENSE-2.0 |
30 | | * |
31 | | * Unless required by applicable law or agreed to in writing, software |
32 | | * distributed under the License is distributed on an "AS IS" BASIS, |
33 | | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
34 | | * See the License for the specific language governing permissions and |
35 | | * limitations under the License. |
36 | | */ |
37 | | |
38 | | #include "xsAll.h" |
39 | | |
40 | | void fxBuildObject(txMachine* the) |
41 | 25.3k | { |
42 | 25.3k | txSlot* slot; |
43 | 25.3k | fxNewHostFunction(the, mxCallback(fx_Object_assign), 2, XS_NO_ID, XS_NO_ID); |
44 | 25.3k | mxAssignObjectFunction = *the->stack; |
45 | 25.3k | mxPop(); |
46 | 25.3k | fxNewHostFunction(the, mxCallback(fx_Object_copy), 2, XS_NO_ID, XS_NO_ID); |
47 | 25.3k | mxCopyObjectFunction = *the->stack; |
48 | 25.3k | mxPop(); |
49 | 25.3k | fxNewHostFunction(the, mxCallback(fxOrdinaryToPrimitive), 2, XS_NO_ID, XS_NO_ID); |
50 | 25.3k | mxOrdinaryToPrimitiveFunction = *the->stack; |
51 | 25.3k | mxPop(); |
52 | | |
53 | 25.3k | mxPush(mxObjectPrototype); |
54 | 25.3k | slot = fxLastProperty(the, the->stack->value.reference); |
55 | 25.3k | slot = fxNextHostAccessorProperty(the, slot, mxCallback(fx_Object_prototype___proto__get), mxCallback(fx_Object_prototype___proto__set), mxID(___proto__), XS_DONT_ENUM_FLAG | XS_DONT_SET_FLAG); |
56 | 25.3k | slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Object_prototype_hasOwnProperty), 1, mxID(_hasOwnProperty), XS_DONT_ENUM_FLAG); |
57 | 25.3k | slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Object_prototype___defineGetter__), 2, mxID(___defineGetter__), XS_DONT_ENUM_FLAG); |
58 | 25.3k | slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Object_prototype___defineSetter__), 2, mxID(___defineSetter__), XS_DONT_ENUM_FLAG); |
59 | 25.3k | slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Object_prototype___lookupGetter__), 1, mxID(___lookupGetter__), XS_DONT_ENUM_FLAG); |
60 | 25.3k | slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Object_prototype___lookupSetter__), 1, mxID(___lookupSetter__), XS_DONT_ENUM_FLAG); |
61 | 25.3k | slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Object_prototype_isPrototypeOf), 1, mxID(_isPrototypeOf), XS_DONT_ENUM_FLAG); |
62 | 25.3k | slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Object_prototype_propertyIsEnumerable), 1, mxID(_propertyIsEnumerable), XS_DONT_ENUM_FLAG); |
63 | 25.3k | slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Object_prototype_toLocaleString), 0, mxID(_toLocaleString), XS_DONT_ENUM_FLAG); |
64 | 25.3k | slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Object_prototype_toString), 0, mxID(_toString), XS_DONT_ENUM_FLAG); |
65 | 25.3k | slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Object_prototype_valueOf), 0, mxID(_valueOf), XS_DONT_ENUM_FLAG); |
66 | 25.3k | slot = fxBuildHostConstructor(the, mxCallback(fx_Object), 1, mxID(_Object)); |
67 | 25.3k | mxObjectConstructor = *the->stack; |
68 | 25.3k | slot = fxLastProperty(the, slot); |
69 | 25.3k | slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Object_assign), 2, mxID(_assign), XS_DONT_ENUM_FLAG); |
70 | 25.3k | slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Object_create), 2, mxID(_create), XS_DONT_ENUM_FLAG); |
71 | 25.3k | slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Object_defineProperties), 2, mxID(_defineProperties), XS_DONT_ENUM_FLAG); |
72 | 25.3k | slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Object_defineProperty), 3, mxID(_defineProperty), XS_DONT_ENUM_FLAG); |
73 | 25.3k | slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Object_entries), 1, mxID(_entries), XS_DONT_ENUM_FLAG); |
74 | 25.3k | slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Object_freeze), 1, mxID(_freeze), XS_DONT_ENUM_FLAG); |
75 | 25.3k | slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Object_fromEntries), 1, mxID(_fromEntries), XS_DONT_ENUM_FLAG); |
76 | 25.3k | slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Object_getOwnPropertyDescriptor), 2, mxID(_getOwnPropertyDescriptor), XS_DONT_ENUM_FLAG); |
77 | 25.3k | slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Object_getOwnPropertyDescriptors), 1, mxID(_getOwnPropertyDescriptors), XS_DONT_ENUM_FLAG); |
78 | 25.3k | slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Object_getOwnPropertyNames), 1, mxID(_getOwnPropertyNames), XS_DONT_ENUM_FLAG); |
79 | 25.3k | slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Object_getOwnPropertySymbols), 1, mxID(_getOwnPropertySymbols), XS_DONT_ENUM_FLAG); |
80 | 25.3k | slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Object_getPrototypeOf), 1, mxID(_getPrototypeOf), XS_DONT_ENUM_FLAG); |
81 | 25.3k | #if mxECMAScript2024 |
82 | 25.3k | slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Object_groupBy), 2, mxID(_groupBy), XS_DONT_ENUM_FLAG); |
83 | 25.3k | #endif |
84 | 25.3k | slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Object_hasOwn), 2, mxID(_hasOwn), XS_DONT_ENUM_FLAG); |
85 | 25.3k | slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Object_is), 2, mxID(_is), XS_DONT_ENUM_FLAG); |
86 | 25.3k | slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Object_isExtensible), 1, mxID(_isExtensible), XS_DONT_ENUM_FLAG); |
87 | 25.3k | slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Object_isFrozen), 1, mxID(_isFrozen), XS_DONT_ENUM_FLAG); |
88 | 25.3k | slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Object_isSealed), 1, mxID(_isSealed), XS_DONT_ENUM_FLAG); |
89 | 25.3k | slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Object_keys), 1, mxID(_keys), XS_DONT_ENUM_FLAG); |
90 | 25.3k | slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Object_preventExtensions), 1, mxID(_preventExtensions), XS_DONT_ENUM_FLAG); |
91 | 25.3k | slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Object_seal), 1, mxID(_seal), XS_DONT_ENUM_FLAG); |
92 | 25.3k | slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Object_setPrototypeOf), 2, mxID(_setPrototypeOf), XS_DONT_ENUM_FLAG); |
93 | 25.3k | slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Object_values), 1, mxID(_values), XS_DONT_ENUM_FLAG); |
94 | 25.3k | mxPop(); |
95 | 25.3k | } |
96 | | |
97 | | |
98 | | txSlot* fxNewObjectInstance(txMachine* the) |
99 | 60.8M | { |
100 | 60.8M | txSlot* instance; |
101 | 60.8M | instance = fxNewSlot(the); |
102 | 60.8M | instance->kind = XS_INSTANCE_KIND; |
103 | 60.8M | instance->value.instance.garbage = C_NULL; |
104 | 60.8M | instance->value.instance.prototype = the->stack->value.reference; |
105 | 60.8M | the->stack->value.reference = instance; |
106 | 60.8M | the->stack->kind = XS_REFERENCE_KIND; |
107 | 60.8M | return instance; |
108 | 60.8M | } |
109 | | |
110 | | void fx_Object(txMachine* the) |
111 | 145k | { |
112 | 145k | if (!mxIsUndefined(mxTarget) && !fxIsSameSlot(the, mxTarget, mxFunction)) { |
113 | 4 | mxPushSlot(mxTarget); |
114 | 4 | fxGetPrototypeFromConstructor(the, &mxObjectPrototype); |
115 | 4 | fxNewObjectInstance(the); |
116 | 4 | mxPullSlot(mxResult); |
117 | 4 | return; |
118 | 4 | } |
119 | 145k | if ((mxArgc == 0) || (mxArgv(0)->kind == XS_UNDEFINED_KIND) || (mxArgv(0)->kind == XS_NULL_KIND)) { |
120 | 106k | mxPush(mxObjectPrototype); |
121 | 106k | fxNewObjectInstance(the); |
122 | 106k | mxPullSlot(mxResult); |
123 | 106k | return; |
124 | 106k | } |
125 | 38.4k | *mxResult = *mxArgv(0); |
126 | 38.4k | fxToInstance(the, mxResult); |
127 | 38.4k | } |
128 | | |
129 | | void fx_Object_prototype___proto__get(txMachine* the) |
130 | 1.28M | { |
131 | 1.28M | if ((mxThis->kind == XS_UNDEFINED_KIND) || (mxThis->kind == XS_NULL_KIND)) |
132 | 3 | mxTypeError("invalid this"); |
133 | 1.28M | mxBehaviorGetPrototype(the, fxToInstance(the, mxThis), mxResult); |
134 | 1.28M | } |
135 | | |
136 | | void fx_Object_prototype___proto__set(txMachine* the) |
137 | 539k | { |
138 | 539k | txSlot* instance; |
139 | 539k | if ((mxThis->kind == XS_UNDEFINED_KIND) || (mxThis->kind == XS_NULL_KIND)) |
140 | 3 | mxTypeError("invalid this"); |
141 | 539k | if ((mxArgc < 1) || ((mxArgv(0)->kind != XS_NULL_KIND) && (mxArgv(0)->kind != XS_REFERENCE_KIND))) |
142 | 382k | return; |
143 | 157k | if (mxThis->kind == XS_REFERENCE_KIND) { |
144 | 157k | instance = mxThis->value.reference; |
145 | 157k | if (!mxBehaviorSetPrototype(the, instance, mxArgv(0))) |
146 | 187 | mxTypeError("invalid prototype"); |
147 | 157k | } |
148 | 157k | } |
149 | | |
150 | | void fx_Object_prototype___defineGetter__(txMachine* the) |
151 | 12 | { |
152 | 12 | txSlot* instance = fxToInstance(the, mxThis); |
153 | 12 | txSlot* at; |
154 | 12 | txSlot* slot; |
155 | 12 | if ((mxArgc < 2) || (!fxIsCallable(the, mxArgv(1)))) |
156 | 9 | mxTypeError("invalid getter"); |
157 | 3 | at = fxAt(the, mxArgv(0)); |
158 | 3 | mxPushUndefined(); |
159 | 3 | slot = the->stack; |
160 | 3 | slot->value.accessor.getter = fxToInstance(the, mxArgv(1)); |
161 | 3 | slot->value.accessor.setter = C_NULL; |
162 | 3 | slot->kind = XS_ACCESSOR_KIND; |
163 | 3 | slot->flag = XS_NO_FLAG; |
164 | 3 | if(!mxBehaviorDefineOwnProperty(the, instance, at->value.at.id, at->value.at.index, slot, XS_GETTER_FLAG | XS_DONT_DELETE_FLAG| XS_DONT_ENUM_FLAG)) |
165 | 2 | mxTypeError("invalid descriptor"); |
166 | 1 | mxPop(); |
167 | 1 | } |
168 | | |
169 | | void fx_Object_prototype___defineSetter__(txMachine* the) |
170 | 17 | { |
171 | 17 | txSlot* instance = fxToInstance(the, mxThis); |
172 | 17 | txSlot* at; |
173 | 17 | txSlot* slot; |
174 | 17 | if ((mxArgc < 2) || (!fxIsCallable(the, mxArgv(1)))) |
175 | 9 | mxTypeError("invalid setter"); |
176 | 8 | at = fxAt(the, mxArgv(0)); |
177 | 8 | mxPushUndefined(); |
178 | 8 | slot = the->stack; |
179 | 8 | slot->value.accessor.getter = C_NULL; |
180 | 8 | slot->value.accessor.setter = fxToInstance(the, mxArgv(1)); |
181 | 8 | slot->kind = XS_ACCESSOR_KIND; |
182 | 8 | slot->flag = XS_NO_FLAG; |
183 | 8 | if(!mxBehaviorDefineOwnProperty(the, instance, at->value.at.id, at->value.at.index, slot, XS_SETTER_FLAG | XS_DONT_DELETE_FLAG| XS_DONT_ENUM_FLAG)) |
184 | 1 | mxTypeError("invalid descriptor"); |
185 | 7 | mxPop(); |
186 | 7 | } |
187 | | |
188 | | void fx_Object_prototype___lookupGetter__(txMachine* the) |
189 | 155 | { |
190 | 155 | txSlot* instance = fxToInstance(the, mxThis); |
191 | 155 | txSlot* at; |
192 | 155 | txSlot* slot; |
193 | 155 | if (mxArgc < 1) |
194 | 0 | mxPushUndefined(); |
195 | 155 | else |
196 | 155 | mxPushSlot(mxArgv(0)); |
197 | 155 | at = fxAt(the, the->stack); |
198 | 155 | mxPushUndefined(); |
199 | 155 | slot = the->stack; |
200 | 440 | again: |
201 | 440 | if (mxBehaviorGetOwnProperty(the, instance, at->value.at.id, at->value.at.index, slot)) { |
202 | 21 | if (slot->kind == XS_ACCESSOR_KIND) { |
203 | 17 | if (slot->value.accessor.getter) { |
204 | 16 | mxResult->kind = XS_REFERENCE_KIND; |
205 | 16 | mxResult->value.reference = slot->value.accessor.getter; |
206 | 16 | } |
207 | 17 | } |
208 | 21 | mxPop(); |
209 | 21 | return; |
210 | 21 | } |
211 | 419 | if (mxBehaviorGetPrototype(the, instance, slot)) { |
212 | 285 | instance = slot->value.reference; |
213 | 285 | goto again; |
214 | 285 | } |
215 | 134 | mxPop(); |
216 | 134 | mxPop(); |
217 | 134 | } |
218 | | |
219 | | void fx_Object_prototype___lookupSetter__(txMachine* the) |
220 | 9 | { |
221 | 9 | txSlot* instance = fxToInstance(the, mxThis); |
222 | 9 | txSlot* at; |
223 | 9 | txSlot* slot; |
224 | 9 | if (mxArgc < 1) |
225 | 0 | mxPushUndefined(); |
226 | 9 | else |
227 | 9 | mxPushSlot(mxArgv(0)); |
228 | 9 | at = fxAt(the, the->stack); |
229 | 9 | mxPushUndefined(); |
230 | 9 | slot = the->stack; |
231 | 17 | again: |
232 | 17 | if (mxBehaviorGetOwnProperty(the, instance, at->value.at.id, at->value.at.index, slot)) { |
233 | 4 | if (slot->kind == XS_ACCESSOR_KIND) { |
234 | 2 | if (slot->value.accessor.setter) { |
235 | 1 | mxResult->kind = XS_REFERENCE_KIND; |
236 | 1 | mxResult->value.reference = slot->value.accessor.setter; |
237 | 1 | } |
238 | 2 | } |
239 | 4 | mxPop(); |
240 | 4 | return; |
241 | 4 | } |
242 | 13 | if (mxBehaviorGetPrototype(the, instance, slot)) { |
243 | 10 | instance = slot->value.reference; |
244 | 10 | goto again; |
245 | 10 | } |
246 | 3 | mxPop(); |
247 | 3 | mxPop(); |
248 | 3 | } |
249 | | |
250 | | void fx_Object_prototype_hasOwnProperty(txMachine* the) |
251 | 161 | { |
252 | 161 | txSlot* at; |
253 | 161 | txSlot* instance; |
254 | 161 | if (mxArgc < 1) |
255 | 1 | mxPushUndefined(); |
256 | 160 | else |
257 | 160 | mxPushSlot(mxArgv(0)); |
258 | 161 | at = fxAt(the, the->stack); |
259 | 161 | instance = fxToInstance(the, mxThis); |
260 | 161 | mxPushUndefined(); |
261 | 161 | mxResult->value.boolean = mxBehaviorGetOwnProperty(the, instance, at->value.at.id, at->value.at.index, the->stack); |
262 | 161 | mxResult->kind = XS_BOOLEAN_KIND; |
263 | 161 | mxPop(); |
264 | 161 | mxPop(); |
265 | 161 | } |
266 | | |
267 | | void fx_Object_prototype_isPrototypeOf(txMachine* the) |
268 | 293 | { |
269 | 293 | mxResult->kind = XS_BOOLEAN_KIND; |
270 | 293 | mxResult->value.boolean = 0; |
271 | 293 | if (mxArgc > 0) { |
272 | 276 | txSlot* slot = mxArgv(0); |
273 | 276 | if (slot->kind == XS_REFERENCE_KIND) { |
274 | 67 | txSlot* prototype = fxToInstance(the, mxThis); |
275 | 80 | while (mxBehaviorGetPrototype(the, slot->value.reference, slot)) { |
276 | 74 | if (prototype == slot->value.reference) { |
277 | 61 | mxResult->value.boolean = 1; |
278 | 61 | break; |
279 | 61 | } |
280 | 74 | } |
281 | 67 | } |
282 | 276 | } |
283 | 293 | } |
284 | | |
285 | | void fx_Object_prototype_propertyIsEnumerable(txMachine* the) |
286 | 365 | { |
287 | 365 | txSlot* at; |
288 | 365 | txSlot* instance; |
289 | 365 | if (mxArgc < 1) |
290 | 0 | mxPushUndefined(); |
291 | 365 | else |
292 | 365 | mxPushSlot(mxArgv(0)); |
293 | 365 | at = fxAt(the, the->stack); |
294 | 365 | instance = fxToInstance(the, mxThis); |
295 | 365 | mxPushUndefined(); |
296 | 365 | mxResult->value.boolean = mxBehaviorGetOwnProperty(the, instance, at->value.at.id, at->value.at.index, the->stack) && ((the->stack->flag & XS_DONT_ENUM_FLAG) == 0); |
297 | 365 | mxResult->kind = XS_BOOLEAN_KIND; |
298 | 365 | mxPop(); |
299 | 365 | mxPop(); |
300 | 365 | } |
301 | | |
302 | | void fx_Object_prototype_toLocaleString(txMachine* the) |
303 | 21 | { |
304 | 21 | mxPushSlot(mxThis); |
305 | 21 | mxDub(); |
306 | 21 | mxGetID(mxID(_toString)); |
307 | 21 | mxCall(); |
308 | 21 | mxRunCount(0); |
309 | 21 | mxPullSlot(mxResult); |
310 | 21 | } |
311 | | |
312 | | void fx_Object_prototype_toString(txMachine* the) |
313 | 494k | { |
314 | 494k | txString tag = C_NULL; |
315 | 494k | txSlot* instance = C_NULL; |
316 | 494k | txSlot* slot; |
317 | 494k | txSlot* target; |
318 | 494k | switch (mxThis->kind) { |
319 | 119k | case XS_UNDEFINED_KIND: |
320 | 119k | tag = "Undefined"; |
321 | 119k | break; |
322 | 3 | case XS_NULL_KIND: |
323 | 3 | tag = "Null"; |
324 | 3 | break; |
325 | 20 | case XS_BOOLEAN_KIND: |
326 | 20 | instance = mxBooleanPrototype.value.reference; |
327 | 20 | tag = "Boolean"; |
328 | 20 | break; |
329 | 10 | case XS_INTEGER_KIND: |
330 | 11 | case XS_NUMBER_KIND: |
331 | 11 | instance = mxNumberPrototype.value.reference; |
332 | 11 | tag = "Number"; |
333 | 11 | break; |
334 | 8 | case XS_STRING_KIND: |
335 | 8 | case XS_STRING_X_KIND: |
336 | 8 | instance = mxStringPrototype.value.reference; |
337 | 8 | tag = "String"; |
338 | 8 | break; |
339 | 6 | case XS_SYMBOL_KIND: |
340 | 6 | instance = mxSymbolPrototype.value.reference; |
341 | 6 | tag = "Object"; |
342 | 6 | break; |
343 | 10 | case XS_BIGINT_KIND: |
344 | 10 | case XS_BIGINT_X_KIND: |
345 | 10 | instance = mxBigIntPrototype.value.reference; |
346 | 10 | tag = "Object"; |
347 | 10 | break; |
348 | 375k | case XS_REFERENCE_KIND: |
349 | 375k | instance = mxThis->value.reference; |
350 | 375k | if (fxIsArray(the, instance)) |
351 | 32 | tag = "Array"; |
352 | 375k | else { |
353 | 375k | slot = instance->next; |
354 | 375k | if (slot) { |
355 | 300k | if ((slot->ID == XS_ARGUMENTS_SLOPPY_BEHAVIOR) || (slot->ID == XS_ARGUMENTS_STRICT_BEHAVIOR)) |
356 | 18.1k | tag = "Arguments"; |
357 | 282k | else if (slot->flag & XS_INTERNAL_FLAG) { |
358 | 194k | switch (slot->kind) { |
359 | 34 | case XS_BOOLEAN_KIND: |
360 | 34 | tag = "Boolean"; |
361 | 34 | break; |
362 | 1 | case XS_CALLBACK_KIND: |
363 | 1 | case XS_CALLBACK_X_KIND: |
364 | 8 | case XS_CODE_KIND: |
365 | 9 | case XS_CODE_X_KIND: |
366 | 9 | tag = "Function"; |
367 | 9 | break; |
368 | 30 | case XS_DATE_KIND: |
369 | 30 | tag = "Date"; |
370 | 30 | break; |
371 | 13 | case XS_ERROR_KIND: |
372 | 13 | tag = "Error"; |
373 | 13 | break; |
374 | 59.6k | case XS_GLOBAL_KIND: |
375 | 59.6k | tag = "global"; |
376 | 59.6k | break; |
377 | 20 | case XS_NUMBER_KIND: |
378 | 20 | tag = "Number"; |
379 | 20 | break; |
380 | 7 | case XS_REGEXP_KIND: |
381 | 7 | tag = "RegExp"; |
382 | 7 | break; |
383 | 16 | case XS_STRING_KIND: |
384 | 16 | case XS_STRING_X_KIND: |
385 | 16 | tag = "String"; |
386 | 16 | break; |
387 | 10 | case XS_BIGINT_KIND: |
388 | 10 | case XS_BIGINT_X_KIND: |
389 | 10 | tag = "Object"; |
390 | 10 | break; |
391 | 230 | case XS_PROXY_KIND: |
392 | 434 | while ((target = slot->value.proxy.target)) { |
393 | 434 | slot = target->next; |
394 | 434 | if (!slot || (slot->kind != XS_PROXY_KIND)) |
395 | 230 | break; |
396 | 434 | } |
397 | 230 | if (mxIsFunction(target)) { |
398 | 24 | instance = target; |
399 | 24 | tag = "Function"; |
400 | 24 | } |
401 | 206 | else |
402 | 206 | tag = "Object"; |
403 | 230 | break; |
404 | 134k | default: |
405 | 134k | tag = "Object"; |
406 | 134k | break; |
407 | 194k | } |
408 | 194k | } |
409 | 87.5k | else |
410 | 87.5k | tag = "Object"; |
411 | 300k | } |
412 | 74.8k | else |
413 | 74.8k | tag = "Object"; |
414 | 375k | } |
415 | 375k | break; |
416 | | #if mxHostFunctionPrimitive |
417 | | case XS_HOST_FUNCTION_KIND: |
418 | | instance = mxFunctionPrototype.value.reference; |
419 | | tag = "Function"; |
420 | | break; |
421 | | #endif |
422 | 375k | default: |
423 | 0 | tag = "Object"; |
424 | 0 | break; |
425 | 494k | } |
426 | 494k | fxStringX(the, mxResult, "[object "); |
427 | 494k | if (instance) { |
428 | 375k | mxPushReference(instance); |
429 | 375k | mxGetID(mxID(_Symbol_toStringTag)); |
430 | 375k | if ((the->stack->kind == XS_STRING_KIND) || (the->stack->kind == XS_STRING_X_KIND)) |
431 | 210k | fxConcatString(the, mxResult, the->stack); |
432 | 165k | else |
433 | 165k | fxConcatStringC(the, mxResult, tag); |
434 | 375k | } |
435 | 119k | else |
436 | 119k | fxConcatStringC(the, mxResult, tag); |
437 | 494k | fxConcatStringC(the, mxResult, "]"); |
438 | 494k | } |
439 | | |
440 | | void fx_Object_prototype_valueOf(txMachine* the) |
441 | 4.02M | { |
442 | 4.02M | fxToInstance(the, mxThis); |
443 | 4.02M | *mxResult = *mxThis; |
444 | 4.02M | } |
445 | | |
446 | | void fx_Object_assign(txMachine* the) |
447 | 127 | { |
448 | 127 | txSlot* target; |
449 | 127 | txInteger c, i; |
450 | 127 | txSlot* instance; |
451 | 127 | txSlot* at; |
452 | 127 | txSlot* property; |
453 | 127 | if ((mxArgc < 1) || (mxArgv(0)->kind == XS_UNDEFINED_KIND) || (mxArgv(0)->kind == XS_NULL_KIND)) |
454 | 2 | mxTypeError("invalid target"); |
455 | 125 | fxToInstance(the, mxArgv(0)); |
456 | 125 | target = mxArgv(0); |
457 | 125 | c = mxArgc; |
458 | 293 | for (i = 1; i < c; i++) { |
459 | 168 | if ((mxArgv(i)->kind == XS_UNDEFINED_KIND) || (mxArgv(i)->kind == XS_NULL_KIND)) |
460 | 11 | continue; |
461 | 157 | instance = fxToInstance(the, mxArgv(i)); |
462 | 157 | at = fxNewInstance(the); |
463 | 157 | mxBehaviorOwnKeys(the, instance, XS_EACH_NAME_FLAG | XS_EACH_SYMBOL_FLAG, at); |
464 | 157 | mxPushUndefined(); |
465 | 157 | property = the->stack; |
466 | 439 | while ((at = at->next)) { |
467 | 282 | if (mxBehaviorGetOwnProperty(the, instance, at->value.at.id, at->value.at.index, property) && !(property->flag & XS_DONT_ENUM_FLAG)) { |
468 | 255 | mxPushReference(instance); |
469 | 255 | mxGetAll(at->value.at.id, at->value.at.index); |
470 | 255 | mxPushSlot(target); |
471 | 255 | mxSetAll(at->value.at.id, at->value.at.index); |
472 | 255 | mxPop(); |
473 | 255 | } |
474 | 282 | } |
475 | 157 | mxPop(); |
476 | 157 | mxPop(); |
477 | 157 | } |
478 | 125 | *mxResult = *target; |
479 | 125 | } |
480 | | |
481 | | void fx_Object_copy(txMachine* the) |
482 | 6.11k | { |
483 | 6.11k | txInteger c = mxArgc, i; |
484 | 6.11k | txSlot* target; |
485 | 6.11k | txSlot* source; |
486 | 6.11k | txSlot* at; |
487 | 6.11k | txSlot* property; |
488 | 6.11k | if ((c < 1) || (mxArgv(0)->kind == XS_UNDEFINED_KIND) || (mxArgv(0)->kind == XS_NULL_KIND)) |
489 | 0 | mxTypeError("invalid object"); |
490 | 6.11k | target = fxToInstance(the, mxArgv(0)); |
491 | 6.11k | *mxResult = *mxArgv(0); |
492 | 6.11k | if ((c < 2) || (mxArgv(1)->kind == XS_UNDEFINED_KIND) || (mxArgv(1)->kind == XS_NULL_KIND)) |
493 | 124 | return; |
494 | 5.99k | source = fxToInstance(the, mxArgv(1)); |
495 | 5.99k | at = fxNewInstance(the); |
496 | 5.99k | mxBehaviorOwnKeys(the, source, XS_EACH_NAME_FLAG | XS_EACH_SYMBOL_FLAG, at); |
497 | 5.99k | mxPushUndefined(); |
498 | 5.99k | property = the->stack; |
499 | 6.95M | while ((at = at->next)) { |
500 | 6.95M | for (i = 2; i < c; i++) { |
501 | 178 | txSlot* exclude = mxArgv(i); |
502 | 178 | if ((exclude->value.at.id == at->value.at.id) && (exclude->value.at.index == at->value.at.index)) |
503 | 2 | break; |
504 | 178 | } |
505 | 6.95M | if (i == c) { |
506 | 6.95M | if (mxBehaviorGetOwnProperty(the, source, at->value.at.id, at->value.at.index, property) && !(property->flag & XS_DONT_ENUM_FLAG)) { |
507 | 6.94M | mxPushReference(source); |
508 | 6.94M | mxGetAll(at->value.at.id, at->value.at.index); |
509 | 6.94M | the->stack->flag = 0; |
510 | 6.94M | mxBehaviorDefineOwnProperty(the, target, at->value.at.id, at->value.at.index, the->stack, XS_GET_ONLY); |
511 | 6.94M | mxPop(); |
512 | 6.94M | } |
513 | 6.95M | } |
514 | 6.95M | mxCheckMetering(); |
515 | 6.95M | } |
516 | 5.99k | mxPop(); // property |
517 | 5.99k | mxPop(); // at |
518 | 5.99k | } |
519 | | |
520 | | void fx_Object_create(txMachine* the) |
521 | 5.04k | { |
522 | 5.04k | txSlot* instance; |
523 | 5.04k | txSlot* properties; |
524 | 5.04k | txSlot* at; |
525 | 5.04k | txSlot* property; |
526 | 5.04k | txFlag mask; |
527 | 5.04k | if ((mxArgc < 1) || ((mxArgv(0)->kind != XS_NULL_KIND) && (mxArgv(0)->kind != XS_REFERENCE_KIND))) |
528 | 2 | mxTypeError("invalid prototype"); |
529 | 5.04k | if (mxArgv(0)->kind == XS_NULL_KIND) |
530 | 16 | fxNewInstance(the); |
531 | 5.03k | else { |
532 | 5.03k | mxPushSlot(mxArgv(0)); |
533 | 5.03k | fxNewHostInstance(the); |
534 | 5.03k | } |
535 | 5.04k | mxPullSlot(mxResult); |
536 | 5.04k | instance = fxGetInstance(the, mxResult); |
537 | 5.04k | if ((mxArgc > 1) && (mxArgv(1)->kind != XS_UNDEFINED_KIND)) { |
538 | 73 | properties = fxToInstance(the, mxArgv(1)); |
539 | 73 | at = fxNewInstance(the); |
540 | 73 | mxBehaviorOwnKeys(the, properties, XS_EACH_NAME_FLAG | XS_EACH_SYMBOL_FLAG, at); |
541 | 73 | mxPushUndefined(); |
542 | 73 | property = the->stack; |
543 | 260 | while ((at = at->next)) { |
544 | 187 | if (mxBehaviorGetOwnProperty(the, properties, at->value.at.id, at->value.at.index, property) && !(property->flag & XS_DONT_ENUM_FLAG)) { |
545 | 120 | mxPushReference(properties); |
546 | 120 | mxGetAll(at->value.at.id, at->value.at.index); |
547 | 120 | mask = fxDescriptorToSlot(the, the->stack); |
548 | 120 | if (!mxBehaviorDefineOwnProperty(the, instance, at->value.at.id, at->value.at.index, the->stack, mask)) |
549 | 0 | mxTypeError("invalid descriptor"); |
550 | 120 | mxPop(); |
551 | 120 | } |
552 | 187 | } |
553 | 73 | mxPop(); |
554 | 73 | mxPop(); |
555 | 73 | } |
556 | 5.04k | } |
557 | | |
558 | | void fx_Object_defineProperties(txMachine* the) |
559 | 65 | { |
560 | 65 | txSlot* instance; |
561 | 65 | txSlot* properties; |
562 | 65 | txSlot* at; |
563 | 65 | txSlot* property; |
564 | 65 | txFlag mask; |
565 | 65 | if ((mxArgc < 1) || (mxArgv(0)->kind != XS_REFERENCE_KIND)) |
566 | 2 | mxTypeError("invalid object"); |
567 | 63 | if ((mxArgc < 2) || (mxArgv(1)->kind == XS_UNDEFINED_KIND)) |
568 | 2 | mxTypeError("invalid properties"); |
569 | 61 | instance = fxGetInstance(the, mxArgv(0)); |
570 | 61 | properties = fxToInstance(the, mxArgv(1)); |
571 | 61 | at = fxNewInstance(the); |
572 | 61 | mxBehaviorOwnKeys(the, properties, XS_EACH_NAME_FLAG | XS_EACH_SYMBOL_FLAG, at); |
573 | 61 | mxPushUndefined(); |
574 | 61 | property = the->stack; |
575 | 268 | while ((at = at->next)) { |
576 | 213 | if (mxBehaviorGetOwnProperty(the, properties, at->value.at.id, at->value.at.index, property) && !(property->flag & XS_DONT_ENUM_FLAG)) { |
577 | 65 | mxPushReference(properties); |
578 | 65 | mxGetAll(at->value.at.id, at->value.at.index); |
579 | 65 | mask = fxDescriptorToSlot(the, the->stack); |
580 | 65 | if (!mxBehaviorDefineOwnProperty(the, instance, at->value.at.id, at->value.at.index, the->stack, mask)) |
581 | 6 | mxTypeError("invalid descriptor"); |
582 | 59 | mxPop(); |
583 | 59 | } |
584 | 213 | } |
585 | 55 | mxPop(); |
586 | 55 | mxPop(); |
587 | 55 | *mxResult = *mxArgv(0); |
588 | 55 | } |
589 | | |
590 | | void fx_Object_defineProperty(txMachine* the) |
591 | 208k | { |
592 | 208k | txSlot* at; |
593 | 208k | txFlag mask; |
594 | 208k | if ((mxArgc < 1) || (mxArgv(0)->kind != XS_REFERENCE_KIND)) |
595 | 1.80k | mxTypeError("invalid object"); |
596 | 207k | if (mxArgc < 2) |
597 | 1 | mxTypeError("invalid key"); |
598 | 207k | at = fxAt(the, mxArgv(1)); |
599 | 207k | if ((mxArgc < 3) || (mxArgv(2)->kind != XS_REFERENCE_KIND)) |
600 | 1.26k | mxTypeError("invalid descriptor"); |
601 | 205k | mask = fxDescriptorToSlot(the, mxArgv(2)); |
602 | 205k | if (!mxBehaviorDefineOwnProperty(the, mxArgv(0)->value.reference, at->value.at.id, at->value.at.index, mxArgv(2), mask)) |
603 | 602 | mxTypeError("invalid descriptor"); |
604 | 205k | *mxResult = *mxArgv(0); |
605 | 205k | } |
606 | | |
607 | | void fx_Object_entries(txMachine* the) |
608 | 33 | { |
609 | 33 | txSlot* instance; |
610 | 33 | txSlot* result; |
611 | 33 | txSlot* array; |
612 | 33 | txSlot* property; |
613 | 33 | txSlot** address; |
614 | 33 | txSlot* item; |
615 | 33 | if ((mxArgc < 1) || (mxArgv(0)->kind == XS_UNDEFINED_KIND) || (mxArgv(0)->kind == XS_NULL_KIND)) |
616 | 2 | mxTypeError("invalid object"); |
617 | 31 | instance = fxToInstance(the, mxArgv(0)); |
618 | 31 | mxPush(mxArrayPrototype); |
619 | 31 | result = fxNewArrayInstance(the); |
620 | 31 | mxPullSlot(mxResult); |
621 | 31 | array = result->next; |
622 | 31 | mxBehaviorOwnKeys(the, instance, XS_EACH_NAME_FLAG, array); |
623 | 31 | mxPushUndefined(); |
624 | 31 | property = the->stack; |
625 | 31 | address = &array->next; |
626 | 224 | while ((item = *address)) { |
627 | 193 | if (mxBehaviorGetOwnProperty(the, instance, item->value.at.id, item->value.at.index, property) && !(property->flag & XS_DONT_ENUM_FLAG)) { |
628 | 130 | array->value.array.length++; |
629 | 130 | mxPushUndefined(); |
630 | 130 | fxKeyAt(the, item->value.at.id, item->value.at.index, the->stack); |
631 | 130 | mxPushReference(instance); |
632 | 130 | mxGetAll(item->value.at.id, item->value.at.index); |
633 | 130 | fxConstructArrayEntry(the, item); |
634 | 130 | address = &(item->next); |
635 | 130 | } |
636 | 63 | else |
637 | 63 | *address = item->next; |
638 | 193 | } |
639 | 31 | mxPop(); |
640 | 31 | fxCacheArray(the, result); |
641 | 31 | } |
642 | | |
643 | | void fx_Object_freeze(txMachine* the) |
644 | 97 | { |
645 | 97 | if (mxArgc > 0) { |
646 | 96 | txSlot* slot = mxArgv(0); |
647 | 96 | if (slot->kind == XS_REFERENCE_KIND) { |
648 | 89 | txSlot* instance = slot->value.reference; |
649 | 89 | txBoolean deep = 0; |
650 | 89 | txSlot* at; |
651 | 89 | txSlot* property; |
652 | 89 | if (!mxBehaviorPreventExtensions(the, instance)) |
653 | 1 | mxTypeError("extensible object"); |
654 | 88 | if ((mxArgc > 1) && fxToBoolean(the, mxArgv(1))) |
655 | 13 | deep = 1; |
656 | 88 | at = fxNewInstance(the); |
657 | 88 | mxBehaviorOwnKeys(the, instance, XS_EACH_NAME_FLAG | XS_EACH_SYMBOL_FLAG, at); |
658 | 88 | mxPushUndefined(); |
659 | 88 | property = the->stack; |
660 | 412 | while ((at = at->next)) { |
661 | 325 | if (mxBehaviorGetOwnProperty(the, instance, at->value.at.id, at->value.at.index, property)) { |
662 | 324 | txFlag mask = XS_DONT_DELETE_FLAG; |
663 | 324 | property->flag |= XS_DONT_DELETE_FLAG; |
664 | 324 | if (property->kind != XS_ACCESSOR_KIND) { |
665 | 304 | mask |= XS_DONT_SET_FLAG; |
666 | 304 | property->flag |= XS_DONT_SET_FLAG; |
667 | 304 | } |
668 | 324 | property->kind = XS_UNINITIALIZED_KIND; |
669 | 324 | if (!mxBehaviorDefineOwnProperty(the, instance, at->value.at.id, at->value.at.index, property, mask)) { |
670 | 1 | if (!deep) |
671 | 1 | mxTypeError("cannot configure property"); |
672 | 1 | } |
673 | 324 | } |
674 | 325 | } |
675 | 87 | mxPop(); |
676 | 87 | if (deep) { |
677 | 13 | at = the->stack->value.reference; |
678 | 13 | mxPushUndefined(); |
679 | 13 | property = the->stack; |
680 | 48 | while ((at = at->next)) { |
681 | 35 | if (mxBehaviorGetOwnProperty(the, instance, at->value.at.id, at->value.at.index, property)) { |
682 | 35 | if (property->kind == XS_REFERENCE_KIND) { |
683 | 9 | mxPushSlot(mxThis); |
684 | 9 | mxDub(); |
685 | 9 | mxGetID(mxID(_isFrozen)); |
686 | 9 | mxCall(); |
687 | 9 | mxPushSlot(property); |
688 | 9 | mxRunCount(1); |
689 | 9 | if (!fxRunTest(the)) { |
690 | 7 | mxPushSlot(mxThis); |
691 | 7 | mxPushSlot(mxFunction); |
692 | 7 | mxCall(); |
693 | 7 | mxPushSlot(property); |
694 | 7 | mxPushBoolean(1); |
695 | 7 | mxRunCount(2); |
696 | 7 | mxPop(); |
697 | 7 | } |
698 | 9 | } |
699 | 26 | else if (property->kind == XS_ACCESSOR_KIND) { |
700 | 1 | if (property->value.accessor.getter) { |
701 | 1 | mxPushSlot(mxThis); |
702 | 1 | mxDub(); |
703 | 1 | mxGetID(mxID(_isFrozen)); |
704 | 1 | mxCall(); |
705 | 1 | mxPushReference(property->value.accessor.getter); |
706 | 1 | mxRunCount(1); |
707 | 1 | if (!fxRunTest(the)) { |
708 | 1 | mxPushSlot(mxThis); |
709 | 1 | mxPushSlot(mxFunction); |
710 | 1 | mxCall(); |
711 | 1 | mxPushReference(property->value.accessor.getter); |
712 | 1 | mxPushBoolean(1); |
713 | 1 | mxRunCount(2); |
714 | 1 | mxPop(); |
715 | 1 | } |
716 | 1 | } |
717 | 1 | if (property->value.accessor.setter) { |
718 | 1 | mxPushSlot(mxThis); |
719 | 1 | mxDub(); |
720 | 1 | mxGetID(mxID(_isFrozen)); |
721 | 1 | mxCall(); |
722 | 1 | mxPushReference(property->value.accessor.setter); |
723 | 1 | mxRunCount(1); |
724 | 1 | if (!fxRunTest(the)) { |
725 | 1 | mxPushSlot(mxThis); |
726 | 1 | mxPushSlot(mxFunction); |
727 | 1 | mxCall(); |
728 | 1 | mxPushReference(property->value.accessor.setter); |
729 | 1 | mxPushBoolean(1); |
730 | 1 | mxRunCount(2); |
731 | 1 | mxPop(); |
732 | 1 | } |
733 | 1 | } |
734 | 1 | } |
735 | 35 | } |
736 | 35 | } |
737 | 13 | mxPop(); |
738 | 13 | } |
739 | 87 | mxPop(); |
740 | 87 | } |
741 | 94 | *mxResult = *mxArgv(0); |
742 | 94 | } |
743 | 97 | } |
744 | | |
745 | | void fx_Object_fromEntries(txMachine* the) |
746 | 240 | { |
747 | 240 | txSlot *instance, *iterator, *next, *value, *at; |
748 | 240 | if ((mxArgc < 1) || (mxArgv(0)->kind == XS_UNDEFINED_KIND) || (mxArgv(0)->kind == XS_NULL_KIND)) |
749 | 2 | mxTypeError("invalid iterable"); |
750 | 238 | mxPush(mxObjectPrototype); |
751 | 238 | instance = fxNewObjectInstance(the); |
752 | 238 | mxPullSlot(mxResult); |
753 | 238 | mxTemporary(iterator); |
754 | 238 | mxTemporary(next); |
755 | 238 | fxGetIterator(the, mxArgv(0), iterator, next, 0); |
756 | 238 | mxTemporary(value); |
757 | 37.3k | while (fxIteratorNext(the, iterator, next, value)) { |
758 | 37.1k | mxTry(the) { |
759 | 37.1k | if (value->kind != XS_REFERENCE_KIND) |
760 | 2 | mxTypeError("item: not an object"); |
761 | 37.1k | mxPushSlot(value); |
762 | 37.1k | mxGetIndex(0); |
763 | 37.1k | mxPushSlot(value); |
764 | 37.1k | mxGetIndex(1); |
765 | 37.1k | at = fxAt(the, the->stack + 1); |
766 | 37.1k | mxBehaviorDefineOwnProperty(the, instance, at->value.at.id, at->value.at.index, the->stack, XS_GET_ONLY); |
767 | 37.1k | mxPop(); |
768 | 37.1k | mxPop(); |
769 | 37.1k | } |
770 | 37.1k | mxCatch(the) { |
771 | 3 | fxIteratorReturn(the, iterator, 1); |
772 | 3 | fxJump(the); |
773 | 3 | } |
774 | 37.1k | } |
775 | 233 | mxPop(); |
776 | 233 | } |
777 | | |
778 | | void fx_Object_getOwnPropertyDescriptor(txMachine* the) |
779 | 654 | { |
780 | 654 | txSlot* instance; |
781 | 654 | txSlot* at; |
782 | 654 | if ((mxArgc < 1) || (mxArgv(0)->kind == XS_UNDEFINED_KIND) || (mxArgv(0)->kind == XS_NULL_KIND)) |
783 | 12 | mxTypeError("invalid object"); |
784 | 642 | instance = fxToInstance(the, mxArgv(0)); |
785 | 642 | if (mxArgc < 2) |
786 | 260 | mxPushUndefined(); |
787 | 382 | else |
788 | 382 | mxPushSlot(mxArgv(1)); |
789 | 642 | at = fxAt(the, the->stack); |
790 | 642 | mxPushUndefined(); |
791 | 642 | if (mxBehaviorGetOwnProperty(the, instance, at->value.at.id, at->value.at.index, the->stack)) { |
792 | 364 | fxDescribeProperty(the, the->stack, XS_GET_ONLY); |
793 | 364 | mxPullSlot(mxResult); |
794 | 364 | } |
795 | 642 | mxPop(); |
796 | 642 | mxPop(); |
797 | 642 | } |
798 | | |
799 | | void fx_Object_getOwnPropertyDescriptors(txMachine* the) |
800 | 20 | { |
801 | 20 | txSlot* instance; |
802 | 20 | txSlot* result; |
803 | 20 | txSlot* at; |
804 | 20 | txSlot* property; |
805 | 20 | if ((mxArgc < 1) || (mxArgv(0)->kind == XS_UNDEFINED_KIND) || (mxArgv(0)->kind == XS_NULL_KIND)) |
806 | 3 | mxTypeError("invalid object"); |
807 | 17 | instance = fxToInstance(the, mxArgv(0)); |
808 | 17 | mxPush(mxObjectPrototype); |
809 | 17 | result = fxNewObjectInstance(the); |
810 | 17 | mxPullSlot(mxResult); |
811 | 17 | at = fxNewInstance(the); |
812 | 17 | mxBehaviorOwnKeys(the, instance, XS_EACH_NAME_FLAG | XS_EACH_SYMBOL_FLAG, at); |
813 | 17 | mxPushUndefined(); |
814 | 17 | property = the->stack; |
815 | 33 | while ((at = at->next)) { |
816 | 16 | if (mxBehaviorGetOwnProperty(the, instance, at->value.at.id, at->value.at.index, property)) { |
817 | 12 | fxDescribeProperty(the, property, XS_GET_ONLY); |
818 | 12 | mxBehaviorDefineOwnProperty(the, result, at->value.at.id, at->value.at.index, the->stack, XS_GET_ONLY); |
819 | 12 | mxPop(); |
820 | 12 | } |
821 | 16 | } |
822 | 17 | mxPop(); |
823 | 17 | } |
824 | | |
825 | | void fx_Object_getOwnPropertyNames(txMachine* the) |
826 | 75 | { |
827 | 75 | txSlot* instance; |
828 | 75 | txSlot* result; |
829 | 75 | txSlot* array; |
830 | 75 | txSlot* item; |
831 | 75 | if ((mxArgc < 1) || (mxArgv(0)->kind == XS_UNDEFINED_KIND) || (mxArgv(0)->kind == XS_NULL_KIND)) |
832 | 3 | mxTypeError("invalid object"); |
833 | 72 | instance = fxToInstance(the, mxArgv(0)); |
834 | 72 | mxPush(mxArrayPrototype); |
835 | 72 | result = fxNewArrayInstance(the); |
836 | 72 | mxPullSlot(mxResult); |
837 | 72 | array = result->next; |
838 | 72 | mxBehaviorOwnKeys(the, instance, XS_EACH_NAME_FLAG, array); |
839 | 72 | item = array; |
840 | 701 | while ((item = item->next)) { |
841 | 629 | array->value.array.length++; |
842 | 629 | fxKeyAt(the, item->value.at.id, item->value.at.index, item); |
843 | 629 | } |
844 | 72 | fxCacheArray(the, result); |
845 | 72 | } |
846 | | |
847 | | void fx_Object_getOwnPropertySymbols(txMachine* the) |
848 | 27 | { |
849 | 27 | txSlot* instance; |
850 | 27 | txSlot* result; |
851 | 27 | txSlot* array; |
852 | 27 | txSlot* item; |
853 | 27 | if ((mxArgc < 1) || (mxArgv(0)->kind == XS_UNDEFINED_KIND) || (mxArgv(0)->kind == XS_NULL_KIND)) |
854 | 3 | mxTypeError("invalid object"); |
855 | 24 | instance = fxToInstance(the, mxArgv(0)); |
856 | 24 | mxPush(mxArrayPrototype); |
857 | 24 | result = fxNewArrayInstance(the); |
858 | 24 | mxPullSlot(mxResult); |
859 | 24 | array = result->next; |
860 | 24 | mxBehaviorOwnKeys(the, instance, XS_EACH_SYMBOL_FLAG, array); |
861 | 24 | item = array; |
862 | 33 | while ((item = item->next)) { |
863 | 9 | array->value.array.length++; |
864 | 9 | fxKeyAt(the, item->value.at.id, item->value.at.index, item); |
865 | 9 | } |
866 | 24 | fxCacheArray(the, result); |
867 | 24 | } |
868 | | |
869 | | void fx_Object_getPrototypeOf(txMachine* the) |
870 | 304 | { |
871 | 304 | if ((mxArgc < 1) || (mxArgv(0)->kind == XS_UNDEFINED_KIND) || (mxArgv(0)->kind == XS_NULL_KIND)) |
872 | 3 | mxTypeError("invalid object"); |
873 | 301 | mxBehaviorGetPrototype(the, fxToInstance(the, mxArgv(0)), mxResult); |
874 | 301 | } |
875 | | |
876 | | static void fx_Object_groupByAux(txMachine* the) |
877 | 24 | { |
878 | 24 | txSlot* instance = mxResult->value.reference; |
879 | 24 | txSlot* at = the->stack; |
880 | 24 | fxAt(the, at); |
881 | 24 | mxPushUndefined(); |
882 | 24 | if (!mxBehaviorGetOwnProperty(the, instance, at->value.at.id, at->value.at.index, the->stack)) { |
883 | 14 | mxPop(); |
884 | 14 | mxPush(mxArrayPrototype); |
885 | 14 | fxNewArrayInstance(the); |
886 | 14 | if (!mxBehaviorDefineOwnProperty(the, instance, at->value.at.id, at->value.at.index, the->stack, XS_GET_ONLY)) |
887 | 0 | mxTypeError("invalid descriptor"); |
888 | 14 | } |
889 | 24 | } |
890 | | |
891 | | void fx_Object_groupBy(txMachine* the) |
892 | 20 | { |
893 | 20 | txSlot *instance, *at, *property; |
894 | 20 | instance = fxNewInstance(the); |
895 | 20 | mxPullSlot(mxResult); |
896 | 20 | fxGroupBy(the, fx_Object_groupByAux); |
897 | 20 | at = fxNewInstance(the); |
898 | 20 | mxBehaviorOwnKeys(the, instance, XS_EACH_NAME_FLAG | XS_EACH_SYMBOL_FLAG, at); |
899 | 20 | mxPushUndefined(); |
900 | 20 | property = the->stack; |
901 | 34 | while ((at = at->next)) { |
902 | 14 | if (mxBehaviorGetOwnProperty(the, instance, at->value.at.id, at->value.at.index, property)) { |
903 | 14 | fxCacheArray(the, property->value.reference); |
904 | 14 | } |
905 | 14 | } |
906 | 20 | mxPop(); |
907 | 20 | mxPop(); |
908 | 20 | } |
909 | | |
910 | | void fx_Object_hasOwn(txMachine* the) |
911 | 11 | { |
912 | 11 | txSlot* instance; |
913 | 11 | txSlot* at; |
914 | 11 | if ((mxArgc < 1) || (mxArgv(0)->kind == XS_UNDEFINED_KIND) || (mxArgv(0)->kind == XS_NULL_KIND)) |
915 | 3 | mxTypeError("invalid object"); |
916 | 8 | instance = fxToInstance(the, mxArgv(0)); |
917 | 8 | if (mxArgc < 2) |
918 | 2 | mxPushUndefined(); |
919 | 6 | else |
920 | 6 | mxPushSlot(mxArgv(1)); |
921 | 8 | at = fxAt(the, the->stack); |
922 | 8 | mxPushUndefined(); |
923 | 8 | mxResult->value.boolean = mxBehaviorGetOwnProperty(the, instance, at->value.at.id, at->value.at.index, the->stack); |
924 | 8 | mxResult->kind = XS_BOOLEAN_KIND; |
925 | 8 | mxPop(); |
926 | 8 | mxPop(); |
927 | 8 | } |
928 | | |
929 | | void fx_Object_is(txMachine* the) |
930 | 393k | { |
931 | 393k | if (mxArgc > 0) |
932 | 393k | mxPushSlot(mxArgv(0)); |
933 | 314 | else |
934 | 314 | mxPushUndefined(); |
935 | 393k | if (mxArgc > 1) |
936 | 309k | mxPushSlot(mxArgv(1)); |
937 | 84.6k | else |
938 | 84.6k | mxPushUndefined(); |
939 | 393k | mxResult->value.boolean = fxIsSameValue(the, the->stack + 1, the->stack, 0); |
940 | 393k | mxResult->kind = XS_BOOLEAN_KIND; |
941 | 393k | the->stack += 2; |
942 | 393k | } |
943 | | |
944 | | void fx_Object_isExtensible(txMachine* the) |
945 | 62 | { |
946 | 62 | mxResult->kind = XS_BOOLEAN_KIND; |
947 | 62 | mxResult->value.boolean = 0; |
948 | 62 | if (mxArgc > 0) { |
949 | 61 | txSlot* slot = mxArgv(0); |
950 | 61 | if (slot->kind == XS_REFERENCE_KIND) { |
951 | 59 | slot = slot->value.reference; |
952 | 59 | mxResult->value.boolean = mxBehaviorIsExtensible(the, slot); |
953 | 59 | } |
954 | 61 | } |
955 | 62 | } |
956 | | |
957 | | void fx_Object_isFrozen(txMachine* the) |
958 | 62 | { |
959 | 62 | mxResult->kind = XS_BOOLEAN_KIND; |
960 | 62 | mxResult->value.boolean = 1; |
961 | 62 | if (mxArgc > 0) { |
962 | 62 | txSlot* slot = mxArgv(0); |
963 | 62 | if (slot->kind == XS_REFERENCE_KIND) { |
964 | 57 | txSlot* instance = slot->value.reference; |
965 | 57 | mxResult->value.boolean = mxBehaviorIsExtensible(the, instance) ? 0 : 1; |
966 | 57 | if (mxResult->value.boolean) { |
967 | 38 | txSlot* at; |
968 | 38 | txSlot* property; |
969 | 38 | at = fxNewInstance(the); |
970 | 38 | mxBehaviorOwnKeys(the, instance, XS_EACH_NAME_FLAG | XS_EACH_SYMBOL_FLAG, at); |
971 | 38 | mxPushUndefined(); |
972 | 38 | property = the->stack; |
973 | 179 | while ((at = at->next)) { |
974 | 141 | if (mxBehaviorGetOwnProperty(the, instance, at->value.at.id, at->value.at.index, property)) { |
975 | 140 | if (property->kind != XS_ACCESSOR_KIND) |
976 | 139 | if (!(property->flag & XS_DONT_SET_FLAG)) |
977 | 15 | mxResult->value.boolean = 0; |
978 | 140 | if (!(property->flag & XS_DONT_DELETE_FLAG)) |
979 | 14 | mxResult->value.boolean = 0; |
980 | 140 | } |
981 | 141 | } |
982 | 38 | mxPop(); |
983 | 38 | mxPop(); |
984 | 38 | } |
985 | 57 | } |
986 | 62 | } |
987 | 62 | } |
988 | | |
989 | | void fx_Object_isSealed(txMachine* the) |
990 | 30 | { |
991 | 30 | mxResult->kind = XS_BOOLEAN_KIND; |
992 | 30 | mxResult->value.boolean = 1; |
993 | 30 | if (mxArgc > 0) { |
994 | 29 | txSlot* slot = mxArgv(0); |
995 | 29 | if (slot->kind == XS_REFERENCE_KIND) { |
996 | 28 | txSlot* instance = slot->value.reference; |
997 | 28 | mxResult->value.boolean = mxBehaviorIsExtensible(the, instance) ? 0 : 1; |
998 | 28 | if (mxResult->value.boolean) { |
999 | 24 | txSlot* at; |
1000 | 24 | txSlot* property; |
1001 | 24 | at = fxNewInstance(the); |
1002 | 24 | mxBehaviorOwnKeys(the, instance, XS_EACH_NAME_FLAG | XS_EACH_SYMBOL_FLAG, at); |
1003 | 24 | mxPushUndefined(); |
1004 | 24 | property = the->stack; |
1005 | 651 | while ((at = at->next)) { |
1006 | 627 | if (mxBehaviorGetOwnProperty(the, instance, at->value.at.id, at->value.at.index, property)) { |
1007 | 626 | if (!(property->flag & XS_DONT_DELETE_FLAG)) |
1008 | 0 | mxResult->value.boolean = 0; |
1009 | 626 | } |
1010 | 627 | } |
1011 | 24 | mxPop(); |
1012 | 24 | mxPop(); |
1013 | 24 | } |
1014 | 28 | } |
1015 | 29 | } |
1016 | 30 | } |
1017 | | |
1018 | | void fx_Object_keys(txMachine* the) |
1019 | 74 | { |
1020 | 74 | txSlot* instance; |
1021 | 74 | txSlot* result; |
1022 | 74 | txSlot* array; |
1023 | 74 | txSlot* property; |
1024 | 74 | txSlot** address; |
1025 | 74 | txSlot* item; |
1026 | 74 | if ((mxArgc < 1) || (mxArgv(0)->kind == XS_UNDEFINED_KIND) || (mxArgv(0)->kind == XS_NULL_KIND)) |
1027 | 3 | mxTypeError("invalid object"); |
1028 | 71 | instance = fxToInstance(the, mxArgv(0)); |
1029 | 71 | mxPush(mxArrayPrototype); |
1030 | 71 | result = fxNewArrayInstance(the); |
1031 | 71 | mxPullSlot(mxResult); |
1032 | 71 | array = result->next; |
1033 | 71 | mxBehaviorOwnKeys(the, instance, XS_EACH_NAME_FLAG, array); |
1034 | 71 | mxPushUndefined(); |
1035 | 71 | property = the->stack; |
1036 | 71 | address = &array->next; |
1037 | 515 | while ((item = *address)) { |
1038 | 444 | if (mxBehaviorGetOwnProperty(the, instance, item->value.at.id, item->value.at.index, property) && !(property->flag & XS_DONT_ENUM_FLAG)) { |
1039 | 388 | array->value.array.length++; |
1040 | 388 | fxKeyAt(the, item->value.at.id, item->value.at.index, item); |
1041 | 388 | address = &(item->next); |
1042 | 388 | } |
1043 | 56 | else |
1044 | 56 | *address = item->next; |
1045 | 444 | } |
1046 | 71 | mxPop(); |
1047 | 71 | fxCacheArray(the, result); |
1048 | 71 | } |
1049 | | |
1050 | | void fx_Object_preventExtensions(txMachine* the) |
1051 | 91 | { |
1052 | 91 | if (mxArgc > 0) { |
1053 | 85 | txSlot* slot = mxArgv(0); |
1054 | 85 | if (slot->kind == XS_REFERENCE_KIND) { |
1055 | 83 | slot = slot->value.reference; |
1056 | 83 | if (!mxBehaviorPreventExtensions(the, slot)) |
1057 | 2 | mxTypeError("extensible object"); |
1058 | 83 | } |
1059 | 83 | *mxResult = *mxArgv(0); |
1060 | 83 | } |
1061 | 91 | } |
1062 | | |
1063 | | void fx_Object_seal(txMachine* the) |
1064 | 319 | { |
1065 | 319 | if (mxArgc > 0) { |
1066 | 149 | txSlot* slot = mxArgv(0); |
1067 | 149 | if (slot->kind == XS_REFERENCE_KIND) { |
1068 | 83 | txSlot* instance = slot->value.reference; |
1069 | 83 | txSlot* at; |
1070 | 83 | txSlot* property; |
1071 | 83 | if (!mxBehaviorPreventExtensions(the, instance)) |
1072 | 1 | mxTypeError("extensible object"); |
1073 | 82 | at = fxNewInstance(the); |
1074 | 82 | mxBehaviorOwnKeys(the, instance, XS_EACH_NAME_FLAG | XS_EACH_SYMBOL_FLAG, at); |
1075 | 82 | mxPushUndefined(); |
1076 | 82 | property = the->stack; |
1077 | 478 | while ((at = at->next)) { |
1078 | 397 | if (mxBehaviorGetOwnProperty(the, instance, at->value.at.id, at->value.at.index, property)) { |
1079 | 397 | txFlag mask = XS_DONT_DELETE_FLAG; |
1080 | 397 | property->flag |= XS_DONT_DELETE_FLAG; |
1081 | 397 | property->kind = XS_UNINITIALIZED_KIND; |
1082 | 397 | if (!mxBehaviorDefineOwnProperty(the, instance, at->value.at.id, at->value.at.index, property, mask)) |
1083 | 1 | mxTypeError("cannot configure property"); |
1084 | 397 | } |
1085 | 397 | } |
1086 | 81 | mxPop(); |
1087 | 81 | mxPop(); |
1088 | 81 | } |
1089 | 147 | *mxResult = *mxArgv(0); |
1090 | 147 | } |
1091 | 319 | } |
1092 | | |
1093 | | void fx_Object_setPrototypeOf(txMachine* the) |
1094 | 62 | { |
1095 | 62 | txSlot* instance; |
1096 | 62 | if ((mxArgc < 1) || (mxArgv(0)->kind == XS_UNDEFINED_KIND) || (mxArgv(0)->kind == XS_NULL_KIND)) |
1097 | 4 | mxTypeError("invalid object"); |
1098 | 58 | if ((mxArgc < 2) || ((mxArgv(1)->kind != XS_NULL_KIND) && (mxArgv(1)->kind != XS_REFERENCE_KIND))) |
1099 | 17 | mxTypeError("invalid prototype"); |
1100 | 41 | if (mxArgv(0)->kind == XS_REFERENCE_KIND) { |
1101 | 34 | instance = mxArgv(0)->value.reference; |
1102 | 34 | if (!mxBehaviorSetPrototype(the, instance, mxArgv(1))) |
1103 | 13 | mxTypeError("invalid prototype"); |
1104 | 34 | } |
1105 | 28 | *mxResult = *mxArgv(0); |
1106 | 28 | } |
1107 | | |
1108 | | void fx_Object_values(txMachine* the) |
1109 | 113k | { |
1110 | 113k | txSlot* instance; |
1111 | 113k | txSlot* result; |
1112 | 113k | txSlot* array; |
1113 | 113k | txSlot* property; |
1114 | 113k | txSlot** address; |
1115 | 113k | txSlot* item; |
1116 | 113k | if ((mxArgc < 1) || (mxArgv(0)->kind == XS_UNDEFINED_KIND) || (mxArgv(0)->kind == XS_NULL_KIND)) |
1117 | 3 | mxTypeError("invalid object"); |
1118 | 113k | instance = fxToInstance(the, mxArgv(0)); |
1119 | 113k | mxPush(mxArrayPrototype); |
1120 | 113k | result = fxNewArrayInstance(the); |
1121 | 113k | mxPullSlot(mxResult); |
1122 | 113k | array = result->next; |
1123 | 113k | mxBehaviorOwnKeys(the, instance, XS_EACH_NAME_FLAG, array); |
1124 | 113k | mxPushUndefined(); |
1125 | 113k | property = the->stack; |
1126 | 113k | address = &array->next; |
1127 | 3.30M | while ((item = *address)) { |
1128 | 3.19M | if (mxBehaviorGetOwnProperty(the, instance, item->value.at.id, item->value.at.index, property) && !(property->flag & XS_DONT_ENUM_FLAG)) { |
1129 | 3.08M | array->value.array.length++; |
1130 | 3.08M | mxPushReference(instance); |
1131 | 3.08M | mxGetAll(item->value.at.id, item->value.at.index); |
1132 | 3.08M | mxPullSlot(item); |
1133 | 3.08M | address = &(item->next); |
1134 | 3.08M | } |
1135 | 113k | else |
1136 | 113k | *address = item->next; |
1137 | 3.19M | } |
1138 | 113k | mxPop(); |
1139 | 113k | fxCacheArray(the, result); |
1140 | 113k | } |