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