/src/moddable/xs/sources/xsProperty.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 | | txSlot* fxLastProperty(txMachine* the, txSlot* slot) |
41 | 84.2M | { |
42 | 84.2M | txSlot* property; |
43 | 269M | while ((property = slot->next)) |
44 | 185M | slot = property; |
45 | 84.2M | return slot; |
46 | 84.2M | } |
47 | | |
48 | | #ifndef mxLink |
49 | | txSlot* fxNextHostAccessorProperty(txMachine* the, txSlot* property, txCallback get, txCallback set, txID id, txFlag flag) |
50 | 1.45M | { |
51 | 1.45M | txSlot *getter = NULL, *setter = NULL, *home = the->stack, *slot; |
52 | 1.45M | if (get) { |
53 | 1.45M | getter = fxBuildHostFunction(the, get, 0, XS_NO_ID); |
54 | 1.45M | slot = mxFunctionInstanceHome(getter); |
55 | 1.45M | slot->value.home.object = home->value.reference; |
56 | 1.45M | fxRenameFunction(the, getter, id, 0, XS_NO_ID, "get "); |
57 | 1.45M | } |
58 | 1.45M | if (set) { |
59 | 121k | setter = fxBuildHostFunction(the, set, 1, XS_NO_ID); |
60 | 121k | slot = mxFunctionInstanceHome(setter); |
61 | 121k | slot->value.home.object = home->value.reference; |
62 | 121k | fxRenameFunction(the, setter, id, 0, XS_NO_ID, "set "); |
63 | 121k | } |
64 | 1.45M | property = property->next = fxNewSlot(the); |
65 | 1.45M | property->flag = flag; |
66 | 1.45M | property->ID = id; |
67 | 1.45M | property->kind = XS_ACCESSOR_KIND; |
68 | 1.45M | property->value.accessor.getter = getter; |
69 | 1.45M | property->value.accessor.setter = setter; |
70 | 1.45M | if (set) |
71 | 121k | mxPop(); |
72 | 1.45M | if (get) |
73 | 1.45M | mxPop(); |
74 | 1.45M | return property; |
75 | 1.45M | } |
76 | | |
77 | | txSlot* fxNextHostFunctionProperty(txMachine* the, txSlot* property, txCallback call, txInteger length, txID id, txFlag flag) |
78 | 13.8M | { |
79 | 13.8M | txSlot *function, *home = the->stack, *slot; |
80 | 13.8M | function = fxNewHostFunction(the, call, length, id, XS_NO_ID); |
81 | 13.8M | slot = mxFunctionInstanceHome(function); |
82 | 13.8M | slot->value.home.object = home->value.reference; |
83 | 13.8M | property = property->next = fxNewSlot(the); |
84 | 13.8M | property->flag = flag; |
85 | 13.8M | property->ID = id; |
86 | 13.8M | property->kind = the->stack->kind; |
87 | 13.8M | property->value = the->stack->value; |
88 | 13.8M | mxPop(); |
89 | 13.8M | return property; |
90 | 13.8M | } |
91 | | #endif |
92 | | |
93 | | txSlot* fxNextUndefinedProperty(txMachine* the, txSlot* property, txID id, txFlag flag) |
94 | 2.99M | { |
95 | 2.99M | property = property->next = fxNewSlot(the); |
96 | 2.99M | property->flag = flag; |
97 | 2.99M | property->ID = id; |
98 | 2.99M | property->kind = XS_UNDEFINED_KIND; |
99 | 2.99M | return property; |
100 | 2.99M | } |
101 | | |
102 | | txSlot* fxNextNullProperty(txMachine* the, txSlot* property, txID id, txFlag flag) |
103 | 2.41M | { |
104 | 2.41M | property = property->next = fxNewSlot(the); |
105 | 2.41M | property->flag = flag; |
106 | 2.41M | property->ID = id; |
107 | 2.41M | property->kind = XS_NULL_KIND; |
108 | 2.41M | return property; |
109 | 2.41M | } |
110 | | |
111 | | txSlot* fxNextBooleanProperty(txMachine* the, txSlot* property, txBoolean boolean, txID id, txFlag flag) |
112 | 3.36M | { |
113 | 3.36M | property = property->next = fxNewSlot(the); |
114 | 3.36M | property->flag = flag; |
115 | 3.36M | property->ID = id; |
116 | 3.36M | property->kind = XS_BOOLEAN_KIND; |
117 | 3.36M | property->value.boolean = boolean; |
118 | 3.36M | return property; |
119 | 3.36M | } |
120 | | |
121 | | txSlot* fxNextIntegerProperty(txMachine* the, txSlot* property, txInteger integer, txID id, txFlag flag) |
122 | 33.2M | { |
123 | 33.2M | property = property->next = fxNewSlot(the); |
124 | 33.2M | property->flag = flag; |
125 | 33.2M | property->ID = id; |
126 | 33.2M | property->kind = XS_INTEGER_KIND; |
127 | 33.2M | property->value.integer = integer; |
128 | 33.2M | return property; |
129 | 33.2M | } |
130 | | |
131 | | txSlot* fxNextNumberProperty(txMachine* the, txSlot* property, txNumber number, txID id, txFlag flag) |
132 | 13.9M | { |
133 | 13.9M | property = property->next = fxNewSlot(the); |
134 | 13.9M | property->flag = flag; |
135 | 13.9M | property->ID = id; |
136 | 13.9M | property->kind = XS_NUMBER_KIND; |
137 | 13.9M | property->value.number = number; |
138 | 13.9M | return property; |
139 | 13.9M | } |
140 | | |
141 | | txSlot* fxNextReferenceProperty(txMachine* the, txSlot* property, txSlot* slot, txID id, txFlag flag) |
142 | 450k | { |
143 | 450k | property = property->next = fxNewSlot(the); |
144 | 450k | property->flag = flag; |
145 | 450k | property->ID = id; |
146 | 450k | property->kind = XS_REFERENCE_KIND; |
147 | 450k | property->value.reference = slot; |
148 | 450k | return property; |
149 | 450k | } |
150 | | |
151 | | txSlot* fxNextSlotProperty(txMachine* the, txSlot* property, txSlot* slot, txID id, txFlag flag) |
152 | 94.5M | { |
153 | 94.5M | property = property->next = fxNewSlot(the); |
154 | 94.5M | property->flag = flag; |
155 | 94.5M | property->ID = id; |
156 | 94.5M | property->kind = slot->kind; |
157 | 94.5M | property->value = slot->value; |
158 | 94.5M | return property; |
159 | 94.5M | } |
160 | | |
161 | | txSlot* fxNextStringProperty(txMachine* the, txSlot* property, txString string, txID id, txFlag flag) |
162 | 2.15M | { |
163 | 2.15M | property = property->next = fxNewSlot(the); |
164 | 2.15M | property->flag = flag; |
165 | 2.15M | property->ID = id; |
166 | 2.15M | fxCopyStringC(the, property, string); |
167 | 2.15M | return property; |
168 | 2.15M | } |
169 | | |
170 | | txSlot* fxNextStringXProperty(txMachine* the, txSlot* property, txString string, txID id, txFlag flag) |
171 | 2.03M | { |
172 | 2.03M | property = property->next = fxNewSlot(the); |
173 | 2.03M | property->flag = flag; |
174 | 2.03M | property->ID = id; |
175 | 2.03M | #ifdef mxSnapshot |
176 | 2.03M | fxCopyStringC(the, property, string); |
177 | | #else |
178 | | property->kind = XS_STRING_X_KIND; |
179 | | property->value.string = string; |
180 | | #endif |
181 | 2.03M | return property; |
182 | 2.03M | } |
183 | | |
184 | | |
185 | | txSlot* fxNextSymbolProperty(txMachine* the, txSlot* property, txID symbol, txID id, txFlag flag) |
186 | 1.24M | { |
187 | 1.24M | property = property->next = fxNewSlot(the); |
188 | 1.24M | property->flag = flag; |
189 | 1.24M | property->ID = id; |
190 | 1.24M | property->kind = XS_SYMBOL_KIND; |
191 | 1.24M | property->value.symbol = symbol; |
192 | 1.24M | return property; |
193 | 1.24M | } |
194 | | |
195 | | txSlot* fxNextTypeDispatchProperty(txMachine* the, txSlot* property, txTypeDispatch* dispatch, txTypeAtomics* atomics, txID id, txFlag flag) |
196 | 564k | { |
197 | 564k | property = property->next = fxNewSlot(the); |
198 | 564k | property->flag = flag; |
199 | 564k | property->ID = id; |
200 | 564k | property->kind = XS_TYPED_ARRAY_KIND; |
201 | 564k | property->value.typedArray.dispatch = dispatch; |
202 | 564k | property->value.typedArray.atomics = atomics; |
203 | 564k | return property; |
204 | 564k | } |
205 | | |
206 | | txSlot* fxQueueKey(txMachine* the, txID id, txIndex index, txSlot* keys) |
207 | 64.4M | { |
208 | 64.4M | keys = keys->next = fxNewSlot(the); |
209 | 64.4M | keys->kind = XS_AT_KIND; |
210 | 64.4M | keys->value.at.id = id; |
211 | 64.4M | keys->value.at.index = index; |
212 | 64.4M | return keys; |
213 | 64.4M | } |
214 | | |
215 | | txSlot* fxQueueIDKeys(txMachine* the, txSlot* first, txFlag flag, txSlot* keys) |
216 | 2.20M | { |
217 | 2.20M | if (flag & XS_EACH_NAME_FLAG) { |
218 | 2.20M | txSlot* property = first; |
219 | 14.5M | while (property) { |
220 | 12.3M | if (!(property->flag & XS_INTERNAL_FLAG) && fxIsKeyName(the, property->ID)) |
221 | 11.6M | keys = fxQueueKey(the, property->ID, 0, keys); |
222 | 12.3M | property = property->next; |
223 | 12.3M | } |
224 | 2.20M | } |
225 | 2.20M | if (flag & XS_EACH_SYMBOL_FLAG) { |
226 | 162k | txSlot* property = first; |
227 | 287k | while (property) { |
228 | 124k | if (!(property->flag & XS_INTERNAL_FLAG) && fxIsKeySymbol(the, property->ID)) |
229 | 3.57k | keys = fxQueueKey(the, property->ID, 0, keys); |
230 | 124k | property = property->next; |
231 | 124k | } |
232 | 162k | } |
233 | 2.20M | return keys; |
234 | 2.20M | } |
235 | | |
236 | | // INDEX |
237 | | |
238 | | static txSize fxSizeToCapacity(txMachine* the, txSize size) |
239 | 1.44M | { |
240 | 1.44M | return fxAddChunkSizes(the, size, size / 3); |
241 | 1.44M | } |
242 | | |
243 | | txBoolean fxDeleteIndexProperty(txMachine* the, txSlot* array, txIndex index) |
244 | 4.32M | { |
245 | 4.32M | txSlot* address = array->value.array.address; |
246 | 4.32M | if (address) { |
247 | 4.23M | txIndex length = array->value.array.length; |
248 | 4.23M | txIndex size = (((txChunk*)(((txByte*)address) - sizeof(txChunk)))->size) / sizeof(txSlot); |
249 | 4.23M | txSlot* result = address; |
250 | 4.23M | txSlot* limit = result + size; |
251 | 4.23M | if (length == size) |
252 | 1.74M | result = address + index; |
253 | 2.48M | else { |
254 | 2.48M | txIndex at; |
255 | 7.12M | while (result < limit) { |
256 | 4.71M | at = *((txIndex*)result); |
257 | 4.71M | if (at == index) |
258 | 988 | break; |
259 | 4.71M | if (at > index) |
260 | 72.4k | return 1; |
261 | 4.63M | result++; |
262 | 4.63M | } |
263 | 2.48M | } |
264 | 4.16M | if (result < limit) { |
265 | 1.04k | if (result->flag & XS_DONT_DELETE_FLAG) |
266 | 5 | return 0; |
267 | 1.04k | index = (txIndex)(result - address); |
268 | 1.04k | size--; |
269 | 1.04k | if (size > 0) { |
270 | 880 | txSlot* chunk = (txSlot*)fxNewChunk(the, size * sizeof(txSlot)); |
271 | 880 | address = array->value.array.address; |
272 | 880 | if (index > 0) |
273 | 748 | c_memcpy(chunk, address, index * sizeof(txSlot)); |
274 | 880 | if (index < size) |
275 | 379 | c_memcpy(chunk + index, address + index + 1, (size - index) * sizeof(txSlot)); |
276 | 880 | array->value.array.address = chunk; |
277 | 880 | } |
278 | 164 | else |
279 | 164 | array->value.array.address = C_NULL; |
280 | 1.04k | } |
281 | 4.16M | } |
282 | 4.25M | return 1; |
283 | 4.32M | } |
284 | | |
285 | | txSlot* fxGetIndexProperty(txMachine* the, txSlot* array, txIndex index) |
286 | 201M | { |
287 | 201M | txSlot* address = array->value.array.address; |
288 | 201M | if (address) { |
289 | 156M | txIndex length = array->value.array.length; |
290 | 156M | txIndex size = (((txChunk*)(((txByte*)address) - sizeof(txChunk)))->size) / sizeof(txSlot); |
291 | 156M | if (length == size) { |
292 | 124M | if (index < length) |
293 | 95.9M | return address + index; |
294 | 124M | } |
295 | 32.4M | else { |
296 | 32.4M | txSlot* result = address; |
297 | 32.4M | txSlot* limit = result + size; |
298 | 32.4M | txIndex at; |
299 | 566M | while (result < limit) { |
300 | 538M | at = *((txIndex*)result); |
301 | 538M | if (at == index) |
302 | 4.21M | return result; |
303 | 534M | result++; |
304 | 534M | } |
305 | 32.4M | } |
306 | 156M | } |
307 | 101M | return C_NULL; |
308 | 201M | } |
309 | | |
310 | | txIndex fxGetIndexSize(txMachine* the, txSlot* array) |
311 | 0 | { |
312 | 0 | txSlot* address = array->value.array.address; |
313 | 0 | if (address) |
314 | 0 | return (((txChunk*)(((txByte*)address) - sizeof(txChunk)))->size) / sizeof(txSlot); |
315 | 0 | return 0; |
316 | 0 | } |
317 | | |
318 | | txSlot* fxQueueIndexKeys(txMachine* the, txSlot* array, txFlag flag, txSlot* keys) |
319 | 182k | { |
320 | 182k | if (flag & XS_EACH_NAME_FLAG) { |
321 | 182k | if (array->value.array.address) { |
322 | 140k | txSize offset = 0; |
323 | 140k | txSize size = (((txChunk*)(((txByte*)array->value.array.address) - sizeof(txChunk)))->size) / sizeof(txSlot); |
324 | 35.9M | while (offset < size) { |
325 | 35.8M | txSlot* slot = array->value.array.address + offset; |
326 | 35.8M | txIndex index = *((txIndex*)slot); |
327 | 35.8M | keys = fxQueueKey(the, 0, index, keys); |
328 | 35.8M | offset++; |
329 | 35.8M | } |
330 | 140k | } |
331 | 182k | } |
332 | 182k | return keys; |
333 | 182k | } |
334 | | |
335 | | txSlot* fxSetIndexProperty(txMachine* the, txSlot* instance, txSlot* array, txIndex index) |
336 | 23.0M | { |
337 | 23.0M | txSlot* address = array->value.array.address; |
338 | 23.0M | txSlot* chunk = address; |
339 | 23.0M | txIndex length = array->value.array.length; |
340 | 23.0M | txIndex current; |
341 | 23.0M | txSize size; |
342 | 23.0M | txSlot* result; |
343 | 23.0M | txSlot* limit; |
344 | 23.0M | txIndex at; |
345 | 23.0M | if (address) { |
346 | 16.5M | current = (((txChunk*)(((txByte*)chunk) - sizeof(txChunk)))->size) / sizeof(txSlot); |
347 | 16.5M | if (length == current) { |
348 | 11.2M | if (index < length) |
349 | 0 | return address + index; |
350 | 11.2M | if (instance->flag & XS_DONT_PATCH_FLAG) |
351 | 0 | return C_NULL; |
352 | 11.2M | if (array->flag & XS_DONT_SET_FLAG) |
353 | 1 | return C_NULL; |
354 | 11.2M | current++; |
355 | 11.2M | size = fxMultiplyChunkSizes(the, current, sizeof(txSlot)); |
356 | 11.2M | chunk = (txSlot*)fxRenewChunk(the, address, size); |
357 | 11.2M | if (!chunk) { |
358 | 3.28M | #ifndef mxNoArrayOverallocation |
359 | 3.28M | if (array->ID == XS_ARRAY_BEHAVIOR) { |
360 | 1.44M | txSize capacity = fxSizeToCapacity(the, size); |
361 | 1.44M | chunk = (txSlot*)fxNewGrowableChunk(the, size, capacity); |
362 | 1.44M | } |
363 | 1.84M | else |
364 | 1.84M | #endif |
365 | 1.84M | chunk = (txSlot*)fxNewChunk(the, size); |
366 | 3.28M | address = array->value.array.address; |
367 | 3.28M | c_memcpy(chunk, address, length * sizeof(txSlot)); |
368 | 3.28M | } |
369 | 11.2M | result = chunk + length; |
370 | 11.2M | } |
371 | 5.27M | else { |
372 | 5.27M | result = address; |
373 | 5.27M | limit = result + current; |
374 | 177M | while (result < limit) { |
375 | 174M | at = *((txIndex*)result); |
376 | 174M | if (at == index) |
377 | 0 | return result; |
378 | 174M | if (at > index) |
379 | 1.70M | break; |
380 | 172M | result++; |
381 | 172M | } |
382 | 5.27M | if (instance->flag & XS_DONT_PATCH_FLAG) |
383 | 0 | return C_NULL; |
384 | 5.27M | if ((array->flag & XS_DONT_SET_FLAG) && (index >= length)) |
385 | 0 | return C_NULL; |
386 | 5.27M | at = mxPtrDiff(result - address); |
387 | 5.27M | current++; |
388 | 5.27M | size = fxMultiplyChunkSizes(the, current, sizeof(txSlot)); |
389 | 5.27M | chunk = (txSlot*)fxNewChunk(the, size); |
390 | 5.27M | address = array->value.array.address; |
391 | 5.27M | result = address + at; |
392 | 5.27M | limit = address + current - 1; |
393 | 5.27M | if (result > address) |
394 | 5.07M | c_memcpy(chunk, address, (result - address) * sizeof(txSlot)); |
395 | 5.27M | if (result < limit) |
396 | 1.70M | c_memcpy(chunk + (result - address) + 1, result, (limit - result) * sizeof(txSlot)); |
397 | 5.27M | result = chunk + (result - address); |
398 | 5.27M | } |
399 | 16.5M | } |
400 | 6.47M | else { |
401 | 6.47M | if (instance->flag & XS_DONT_PATCH_FLAG) |
402 | 0 | return C_NULL; |
403 | 6.47M | if ((array->flag & XS_DONT_SET_FLAG) && (index >= length)) |
404 | 1 | return C_NULL; |
405 | 6.47M | current = 1; |
406 | 6.47M | chunk = (txSlot*)fxNewChunk(the, sizeof(txSlot)); |
407 | 6.47M | result = chunk; |
408 | 6.47M | } |
409 | 23.0M | result->next = C_NULL; |
410 | 23.0M | result->ID = XS_NO_ID; |
411 | 23.0M | result->flag = XS_NO_FLAG; |
412 | 23.0M | result->kind = XS_UNDEFINED_KIND; |
413 | 23.0M | *((txIndex*)result) = index; |
414 | 23.0M | array->value.array.address = chunk; |
415 | 23.0M | if (index >= length) { |
416 | 12.9M | array->value.array.length = index + 1; |
417 | 12.9M | } |
418 | 23.0M | return result; |
419 | 23.0M | } |
420 | | |
421 | | void fxSetIndexSize(txMachine* the, txSlot* array, txIndex target, txBoolean growable) |
422 | 9.08M | { |
423 | 9.08M | txSlot* address = array->value.array.address; |
424 | 9.08M | txSlot* chunk = C_NULL; |
425 | 9.08M | txIndex current = (address) ? (((txChunk*)(((txByte*)address) - sizeof(txChunk)))->size) / sizeof(txSlot) : 0; |
426 | 9.08M | txSize size; |
427 | 9.08M | if (current != target) { |
428 | 1.51M | if (array->flag & XS_DONT_SET_FLAG) |
429 | 0 | mxTypeError("set length: not writable"); |
430 | 1.51M | size = fxMultiplyChunkSizes(the, target, sizeof(txSlot)); |
431 | 1.51M | if (address) { |
432 | 460k | if (target) { |
433 | 455k | chunk = (txSlot*)fxRenewChunk(the, address, size); |
434 | 455k | if (!chunk) { |
435 | 1.19k | #ifndef mxNoArrayOverallocation |
436 | 1.19k | if (growable) { |
437 | 1.19k | txSize capacity = fxSizeToCapacity(the, size); |
438 | 1.19k | chunk = (txSlot*)fxNewGrowableChunk(the, size, capacity); |
439 | 1.19k | } |
440 | 5 | else |
441 | 5 | #endif |
442 | 5 | chunk = (txSlot*)fxNewChunk(the, size); |
443 | 1.19k | address = array->value.array.address; |
444 | 1.19k | if (current < target) |
445 | 1.19k | c_memcpy(chunk, address, current * sizeof(txSlot)); |
446 | 0 | else |
447 | 0 | c_memcpy(chunk, address, size); |
448 | 1.19k | } |
449 | 455k | } |
450 | 460k | } |
451 | 1.05M | else { |
452 | 1.05M | #ifndef mxNoArrayOverallocation |
453 | 1.05M | if (growable) { |
454 | 515 | txSize capacity = fxSizeToCapacity(the, size); |
455 | 515 | chunk = (txSlot*)fxNewGrowableChunk(the, size, capacity); |
456 | 515 | } |
457 | 1.05M | else |
458 | 1.05M | #endif |
459 | 1.05M | chunk = (txSlot*)fxNewChunk(the, size); |
460 | 1.05M | } |
461 | 1.51M | if (current < target) |
462 | 1.49M | c_memset(chunk + current, 0, (target - current) * sizeof(txSlot)); |
463 | 1.51M | array->value.array.length = target; |
464 | 1.51M | array->value.array.address = chunk; |
465 | 1.51M | } |
466 | 9.08M | } |
467 | | |
468 | | txBoolean fxDefinePrivateProperty(txMachine* the, txSlot* instance, txSlot* check, txID id, txSlot* slot, txFlag mask) |
469 | 31.9k | { |
470 | 31.9k | txSlot** address; |
471 | 31.9k | txSlot* property; |
472 | 31.9k | mxCheck(the, instance->kind == XS_INSTANCE_KIND); |
473 | | #if mxAliasInstance |
474 | | if (instance->ID) { |
475 | | txSlot* alias = the->aliasArray[instance->ID]; |
476 | | if (alias) |
477 | | instance = alias; |
478 | | else |
479 | | instance = fxAliasInstance(the, instance); |
480 | | } |
481 | | if (instance->flag & XS_MARK_FLAG) |
482 | | return 0; |
483 | | #else |
484 | 31.9k | if (instance->flag & XS_DONT_MARSHALL_FLAG) |
485 | 1 | return 0; |
486 | 31.9k | #endif |
487 | 31.9k | if (mxIsModule(instance)) { |
488 | 0 | instance = mxModuleInstanceExports(instance)->value.reference; |
489 | 0 | } |
490 | 31.9k | address = &(instance->next); |
491 | 39.2k | while ((property = *address)) { |
492 | 16.4k | if (!(property->flag & XS_INTERNAL_FLAG)) { |
493 | 5.46k | property = C_NULL; |
494 | 5.46k | break; |
495 | 5.46k | } |
496 | 10.9k | if (property->kind == XS_PRIVATE_KIND) { |
497 | 3.65k | if (property->value.private.check == check) { |
498 | 3.65k | break; |
499 | 3.65k | } |
500 | 3.65k | } |
501 | 7.31k | address = &(property->next); |
502 | 7.31k | } |
503 | 31.9k | if (!property) { |
504 | 28.2k | property = fxNewSlot(the); |
505 | 28.2k | property->next = *address; |
506 | 28.2k | property->flag = XS_INTERNAL_FLAG; |
507 | 28.2k | property->kind = XS_PRIVATE_KIND; |
508 | 28.2k | property->value.private.check = check; |
509 | 28.2k | property->value.private.first = C_NULL; |
510 | 28.2k | *address = property; |
511 | 28.2k | } |
512 | 31.9k | address = &(property->value.private.first); |
513 | 35.5k | while ((property = *address)) { |
514 | 3.66k | if (property->ID == id) |
515 | 0 | break; |
516 | 3.66k | address = &(property->next); |
517 | 3.66k | } |
518 | 31.9k | if (mask & XS_ACCESSOR_FLAG) { |
519 | 2 | if (property) { |
520 | 0 | if (property->kind != XS_ACCESSOR_KIND) |
521 | 0 | return 0; |
522 | 0 | } |
523 | 2 | else { |
524 | 2 | *address = property = fxNewSlot(the); |
525 | 2 | property->ID = id; |
526 | 2 | property->kind = XS_ACCESSOR_KIND; |
527 | 2 | property->value.accessor.getter = C_NULL; |
528 | 2 | property->value.accessor.setter = C_NULL; |
529 | 2 | } |
530 | 2 | if (mask & XS_GETTER_FLAG) { |
531 | 2 | txSlot* function = slot->value.accessor.getter; |
532 | 2 | if (property->value.accessor.getter) |
533 | 0 | return 0; |
534 | 2 | property->value.accessor.getter = function; |
535 | 2 | if (mxIsFunction(function)) { |
536 | 2 | if (((mask & XS_METHOD_FLAG) && (function->flag & XS_MARK_FLAG) == 0)) { |
537 | 2 | txSlot* home = mxFunctionInstanceHome(function); |
538 | 2 | home->value.home.object = instance; |
539 | 2 | } |
540 | 2 | if ((mask & XS_NAME_FLAG) && ((function->flag & XS_MARK_FLAG) == 0)) |
541 | 2 | fxRenameFunction(the, function, id, 0, mxID(_get), "get "); |
542 | 2 | } |
543 | 2 | } |
544 | 0 | else { |
545 | 0 | txSlot* function = slot->value.accessor.setter; |
546 | 0 | if (property->value.accessor.setter) |
547 | 0 | return 0; |
548 | 0 | property->value.accessor.setter = function; |
549 | 0 | if (mxIsFunction(function)) { |
550 | 0 | if (((mask & XS_METHOD_FLAG) && (function->flag & XS_MARK_FLAG) == 0)) { |
551 | 0 | txSlot* home = mxFunctionInstanceHome(function); |
552 | 0 | home->value.home.object = instance; |
553 | 0 | } |
554 | 0 | if ((mask & XS_NAME_FLAG) && ((function->flag & XS_MARK_FLAG) == 0)) |
555 | 0 | fxRenameFunction(the, function, id, 0, mxID(_set), "set "); |
556 | 0 | } |
557 | 0 | } |
558 | 2 | } |
559 | 31.9k | else { |
560 | 31.9k | if (property) |
561 | 0 | return 0; |
562 | 31.9k | *address = property = fxNewSlot(the); |
563 | 31.9k | property->flag = (mask & XS_METHOD_FLAG) ? XS_DONT_SET_FLAG : XS_NO_FLAG; |
564 | 31.9k | property->ID = id; |
565 | 31.9k | property->kind = slot->kind; |
566 | 31.9k | property->value = slot->value; |
567 | 31.9k | if (property->kind == XS_REFERENCE_KIND) { |
568 | 19 | txSlot* function = slot->value.reference; |
569 | 19 | if (mxIsFunction(function)) { |
570 | 15 | if ((mask & XS_METHOD_FLAG) && ((function->flag & XS_MARK_FLAG) == 0)) { |
571 | 14 | txSlot* home = mxFunctionInstanceHome(function); |
572 | 14 | home->value.home.object = instance; |
573 | 14 | } |
574 | 15 | if ((mask & XS_NAME_FLAG) && ((function->flag & XS_MARK_FLAG) == 0)) |
575 | 15 | fxRenameFunction(the, function, id, 0, mxID(_value), C_NULL); |
576 | 15 | } |
577 | 19 | } |
578 | 31.9k | } |
579 | 31.9k | return 1; |
580 | 31.9k | } |
581 | | |
582 | | txSlot* fxGetPrivateProperty(txMachine* the, txSlot* instance, txSlot* check, txID id) |
583 | 9.04k | { |
584 | 9.04k | txSlot* result; |
585 | 9.04k | mxCheck(the, instance->kind == XS_INSTANCE_KIND); |
586 | | #if mxAliasInstance |
587 | | if (instance->ID) { |
588 | | txSlot* alias = the->aliasArray[instance->ID]; |
589 | | if (alias) |
590 | | instance = alias; |
591 | | } |
592 | | #endif |
593 | 9.04k | if (mxIsModule(instance)) { |
594 | 0 | instance = mxModuleInstanceExports(instance)->value.reference; |
595 | 0 | } |
596 | 9.04k | result = instance->next; |
597 | 9.06k | while (result) { |
598 | 9.06k | if (result->kind == XS_PRIVATE_KIND) { |
599 | 9.04k | if (result->value.private.check == check) |
600 | 9.04k | break; |
601 | 9.04k | } |
602 | 21 | result = result->next; |
603 | 21 | } |
604 | 9.04k | if (result) { |
605 | 9.04k | result = result->value.private.first; |
606 | 9.04k | while (result) { |
607 | 9.04k | if (result->ID == id) |
608 | 9.04k | break; |
609 | 4 | result = result->next; |
610 | 4 | } |
611 | 9.04k | } |
612 | 9.04k | if (result) { |
613 | 9.04k | if ((result->kind == XS_ACCESSOR_KIND) && (result->value.accessor.getter == C_NULL)) |
614 | 0 | result = C_NULL; |
615 | 9.04k | } |
616 | 9.04k | return result; |
617 | 9.04k | } |
618 | | |
619 | | txSlot* fxSetPrivateProperty(txMachine* the, txSlot* instance, txSlot* check, txID id) |
620 | 15 | { |
621 | 15 | txSlot* result; |
622 | 15 | mxCheck(the, instance->kind == XS_INSTANCE_KIND); |
623 | | #if mxAliasInstance |
624 | | if (instance->ID) { |
625 | | txSlot* alias = the->aliasArray[instance->ID]; |
626 | | if (alias) |
627 | | instance = alias; |
628 | | else |
629 | | instance = fxAliasInstance(the, instance); |
630 | | } |
631 | | #endif |
632 | 15 | if (mxIsModule(instance)) { |
633 | 0 | instance = mxModuleInstanceExports(instance)->value.reference; |
634 | 0 | } |
635 | 15 | result = instance->next; |
636 | 23 | while (result) { |
637 | 21 | if (result->kind == XS_PRIVATE_KIND) { |
638 | 13 | if (result->value.private.check == check) |
639 | 13 | break; |
640 | 13 | } |
641 | 8 | result = result->next; |
642 | 8 | } |
643 | 15 | if (result) { |
644 | 13 | result = result->value.private.first; |
645 | 15 | while (result) { |
646 | 15 | if (result->ID == id) |
647 | 13 | break; |
648 | 2 | result = result->next; |
649 | 2 | } |
650 | 13 | } |
651 | 15 | if (result) { |
652 | 13 | if ((result->kind == XS_ACCESSOR_KIND) && (result->value.accessor.setter == C_NULL)) |
653 | 0 | result = C_NULL; |
654 | 13 | } |
655 | 15 | return result; |
656 | 15 | } |
657 | | |
658 | | void fxGroupBy(txMachine* the, txCallback aux) |
659 | 3.21k | { |
660 | 3.21k | txSlot *function, *iterable, *iterator, *next, *value, *slot; |
661 | 3.21k | txNumber k = 0; |
662 | 3.21k | if ((mxArgc < 1) || mxIsUndefined(mxArgv(0)) || mxIsUndefined(mxArgv(1))) |
663 | 1.06k | mxTypeError("items: not an object"); |
664 | 2.15k | if ((mxArgc < 2) || (!fxIsCallable(the, mxArgv(1)))) |
665 | 2.11k | mxTypeError("callback: not a function"); |
666 | 39 | function = mxArgv(1); |
667 | 39 | iterable = mxArgv(0); |
668 | 39 | mxTemporary(iterator); |
669 | 39 | mxTemporary(next); |
670 | 39 | fxGetIterator(the, iterable, iterator, next, 0); |
671 | 39 | mxTemporary(value); |
672 | 34.5k | while (fxIteratorNext(the, iterator, next, value)) { |
673 | 34.5k | mxTry(the) { |
674 | 34.5k | if (k > C_MAX_SAFE_INTEGER) |
675 | 0 | mxTypeError("unsafe integer"); |
676 | 34.5k | mxPushUndefined(); |
677 | 34.5k | mxPushSlot(function); |
678 | 34.5k | mxCall(); |
679 | 34.5k | mxPushSlot(value); |
680 | 34.5k | mxPushNumber(k); |
681 | 34.5k | mxRunCount(2); |
682 | 34.5k | aux(the); |
683 | 34.5k | slot = the->stack->value.reference->next; |
684 | 34.5k | slot->value.array.length++; |
685 | 70.6k | while (slot->next) |
686 | 36.1k | slot = slot->next; |
687 | 34.5k | slot->next = fxDuplicateSlot(the, value); |
688 | 34.5k | mxPop(); |
689 | 34.5k | mxPop(); |
690 | 34.5k | k++; |
691 | 34.5k | } |
692 | 34.5k | mxCatch(the) { |
693 | 1 | fxIteratorReturn(the, iterator, 1); |
694 | 1 | fxJump(the); |
695 | 1 | } |
696 | 34.5k | } |
697 | 38 | mxPop(); |
698 | 38 | mxPop(); |
699 | 38 | mxPop(); |
700 | 38 | } |
701 | | |
702 | | |
703 | | |
704 | | |
705 | | |
706 | | |