Coverage Report

Created: 2026-10-31 07:05

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/moddable/xs/sources/xsMapSet.c
Line
Count
Source
1
/*
2
 * Copyright (c) 2016-2026  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
#ifndef mxTableMinLength
41
2.16M
  #define mxTableMinLength (1)
42
#endif
43
#ifndef mxTableMaxLength
44
4.18M
  #define mxTableMaxLength (1024 * 1024)
45
#endif
46
24.4M
#define mxTableThreshold(LENGTH) (((LENGTH) >> 1) + ((LENGTH) >> 2))
47
48
static txSlot* fxCheckMapInstance(txMachine* the, txSlot* slot, txBoolean mutable);
49
static txSlot* fxCheckMapKey(txMachine* the);
50
static txSlot* fxNewMapIteratorInstance(txMachine* the, txSlot* iterable, txInteger kind);
51
52
static txSlot* fxCheckSetInstance(txMachine* the, txSlot* slot, txBoolean mutable);
53
static txSlot* fxCheckSetValue(txMachine* the);
54
55
static void fxNewSetResult(txMachine* the, txSlot* table, txSlot* list, txSlot** tableAddress, txSlot** listAddress);
56
static txSlot* fxCheckSetRecord(txMachine* the, txInteger* otherSize, txSlot** otherHas, txSlot** otherKeys);
57
static txBoolean fxSetRecordHas(txMachine* the, txSlot* other, txSlot* otherHas, txSlot* value);
58
static void fxSetRecordKeys(txMachine* the, txSlot* other, txSlot* otherKeys, txSlot** iterator, txSlot** next, txSlot** value);
59
60
static txSlot* fxNewSetIteratorInstance(txMachine* the, txSlot* iterable, txInteger kind);
61
62
static txSlot* fxCanonicalizeKeyedCollectionKey(txSlot* key);
63
static void fxClearEntries(txMachine* the, txSlot* table, txSlot* list, txBoolean paired);
64
static txBoolean fxDeleteEntry(txMachine* the, txSlot* table, txSlot* list, txSlot* slot, txBoolean paired, txBoolean fit); 
65
static txSlot* fxGetEntry(txMachine* the, txSlot* table, txSlot* slot);
66
static void fxPurgeEntries(txMachine* the, txSlot* list);
67
static void fxResizeEntries(txMachine* the, txSlot* table, txSlot* list);
68
static void fxSetEntry(txMachine* the, txSlot* table, txSlot* list, txSlot* slot, txSlot* pair, txBoolean get); 
69
static txBoolean fxTestEntry(txMachine* the, txSlot* a, txSlot* b);
70
71
static txSlot* fxCanBeHeldWeakly(txMachine* the, txSlot* slot);
72
73
static txSlot* fxCheckWeakMapInstance(txMachine* the, txSlot* slot, txBoolean mutable);
74
static txSlot* fxCheckWeakMapKey(txMachine* the, txBoolean mutable);
75
76
static txSlot* fxCheckWeakSetInstance(txMachine* the, txSlot* slot, txBoolean mutable);
77
static txSlot* fxCheckWeakSetValue(txMachine* the, txBoolean mutable);
78
79
static txBoolean fxDeleteWeakEntry(txMachine* the, txSlot* link, txSlot* slot); 
80
static txSlot* fxGetWeakEntry(txMachine* the, txSlot* link, txSlot* slot);
81
static void fxSetWeakEntry(txMachine* the, txSlot* link, txSlot* slot, txSlot* pair, txBoolean get); 
82
83
static void fxKeepDuringJobs(txMachine* the, txSlot* target);
84
static txSlot* fxNewWeakRefInstance(txMachine* the);
85
86
static void fx_FinalizationRegistryCleanup(txMachine* the, txSlot* registry, txSlot* callback);
87
88
void fxBuildMapSet(txMachine* the)
89
41.0k
{
90
41.0k
  txSlot* slot;
91
41.0k
  txSlot* property;
92
  
93
  /* MAP */
94
41.0k
  mxPush(mxObjectPrototype);
95
41.0k
  slot = fxLastProperty(the, fxNewObjectInstance(the));
96
41.0k
  slot = fxNextHostAccessorProperty(the, slot, mxCallback(fx_Map_prototype_size), C_NULL, mxID(_size), XS_DONT_ENUM_FLAG);
97
41.0k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Map_prototype_clear), 0, mxID(_clear), XS_DONT_ENUM_FLAG);
98
41.0k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Map_prototype_delete), 1, mxID(_delete), XS_DONT_ENUM_FLAG);
99
41.0k
  property = slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Map_prototype_entries), 0, mxID(_entries), XS_DONT_ENUM_FLAG);
100
41.0k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Map_prototype_forEach), 1, mxID(_forEach), XS_DONT_ENUM_FLAG);
101
41.0k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Map_prototype_get), 1, mxID(_get), XS_DONT_ENUM_FLAG);
102
41.0k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Map_prototype_has), 1, mxID(_has), XS_DONT_ENUM_FLAG);
103
41.0k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Map_prototype_keys), 0, mxID(_keys), XS_DONT_ENUM_FLAG);
104
41.0k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Map_prototype_set), 2, mxID(_set), XS_DONT_ENUM_FLAG);
105
41.0k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Map_prototype_values), 0, mxID(_values), XS_DONT_ENUM_FLAG);
106
41.0k
  slot = fxNextSlotProperty(the, slot, property, mxID(_Symbol_iterator), XS_DONT_ENUM_FLAG);
107
41.0k
  slot = fxNextStringXProperty(the, slot, "Map", mxID(_Symbol_toStringTag), XS_DONT_ENUM_FLAG | XS_DONT_SET_FLAG);
108
41.0k
#if mxECMAScript2026
109
41.0k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Map_prototype_getOrInsert), 2, mxID(_getOrInsert), XS_DONT_ENUM_FLAG);
110
41.0k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Map_prototype_getOrInsertComputed), 2, mxID(_getOrInsertComputed), XS_DONT_ENUM_FLAG);
111
41.0k
#endif
112
41.0k
  mxMapPrototype = *the->stack;
113
41.0k
  slot = fxBuildHostConstructor(the, mxCallback(fx_Map), 0, mxID(_Map));
114
41.0k
  mxMapConstructor = *the->stack;
115
41.0k
  slot = fxLastProperty(the, slot);
116
41.0k
  slot = fxNextHostAccessorProperty(the, slot, mxCallback(fx_species_get), C_NULL, mxID(_Symbol_species), XS_DONT_ENUM_FLAG);
117
41.0k
#if mxECMAScript2024
118
41.0k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Map_groupBy), 2, mxID(_groupBy), XS_DONT_ENUM_FLAG);
119
41.0k
#endif
120
41.0k
  mxPop();
121
  
122
41.0k
  mxPush(mxIteratorPrototype);
123
41.0k
  slot = fxLastProperty(the, fxNewObjectInstance(the));
124
41.0k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_MapIterator_prototype_next), 0, mxID(_next), XS_NO_FLAG);
125
41.0k
  slot = fxNextStringXProperty(the, slot, "Map Iterator", mxID(_Symbol_toStringTag), XS_DONT_ENUM_FLAG | XS_DONT_SET_FLAG);
126
41.0k
  mxPull(mxMapIteratorPrototype);
127
  
128
  /* SET */
129
41.0k
  mxPush(mxObjectPrototype);
130
41.0k
  slot = fxLastProperty(the, fxNewObjectInstance(the));
131
41.0k
  slot = fxNextHostAccessorProperty(the, slot, mxCallback(fx_Set_prototype_size), C_NULL, mxID(_size), XS_DONT_ENUM_FLAG);
132
41.0k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Set_prototype_add), 1, mxID(_add), XS_DONT_ENUM_FLAG);
133
41.0k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Set_prototype_clear), 0, mxID(_clear), XS_DONT_ENUM_FLAG);
134
41.0k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Set_prototype_delete), 1, mxID(_delete), XS_DONT_ENUM_FLAG);
135
41.0k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Set_prototype_entries), 0, mxID(_entries), XS_DONT_ENUM_FLAG);
136
41.0k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Set_prototype_forEach), 1, mxID(_forEach), XS_DONT_ENUM_FLAG);
137
41.0k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Set_prototype_has), 1, mxID(_has), XS_DONT_ENUM_FLAG);
138
41.0k
  property = slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Set_prototype_values), 0, mxID(_values), XS_DONT_ENUM_FLAG);
139
41.0k
  slot = fxNextSlotProperty(the, slot, property, mxID(_keys), XS_DONT_ENUM_FLAG);
140
41.0k
  slot = fxNextSlotProperty(the, slot, property, mxID(_Symbol_iterator), XS_DONT_ENUM_FLAG);
141
41.0k
  slot = fxNextStringXProperty(the, slot, "Set", mxID(_Symbol_toStringTag), XS_DONT_ENUM_FLAG | XS_DONT_SET_FLAG);
142
41.0k
#if mxECMAScript2025
143
41.0k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Set_prototype_difference), 1, mxID(_difference), XS_DONT_ENUM_FLAG);
144
41.0k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Set_prototype_intersection), 1, mxID(_intersection), XS_DONT_ENUM_FLAG);
145
41.0k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Set_prototype_isDisjointFrom), 1, mxID(_isDisjointFrom), XS_DONT_ENUM_FLAG);
146
41.0k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Set_prototype_isSubsetOf), 1, mxID(_isSubsetOf), XS_DONT_ENUM_FLAG);
147
41.0k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Set_prototype_isSupersetOf), 1, mxID(_isSupersetOf), XS_DONT_ENUM_FLAG);
148
41.0k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Set_prototype_symmetricDifference), 1, mxID(_symmetricDifference), XS_DONT_ENUM_FLAG);
149
41.0k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Set_prototype_union), 1, mxID(_union), XS_DONT_ENUM_FLAG);
150
41.0k
#endif
151
41.0k
  mxSetPrototype = *the->stack;
152
41.0k
  slot = fxBuildHostConstructor(the, mxCallback(fx_Set), 0, mxID(_Set));
153
41.0k
  mxSetConstructor = *the->stack;
154
41.0k
  slot = fxLastProperty(the, slot);
155
41.0k
  slot = fxNextHostAccessorProperty(the, slot, mxCallback(fx_species_get), C_NULL, mxID(_Symbol_species), XS_DONT_ENUM_FLAG);
156
41.0k
  mxPop();
157
  
158
41.0k
  mxPush(mxIteratorPrototype);
159
41.0k
  slot = fxLastProperty(the, fxNewObjectInstance(the));
160
41.0k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_SetIterator_prototype_next), 0, mxID(_next), XS_NO_FLAG);
161
41.0k
  slot = fxNextStringXProperty(the, slot, "Set Iterator", mxID(_Symbol_toStringTag), XS_DONT_ENUM_FLAG | XS_DONT_SET_FLAG);
162
41.0k
  mxPull(mxSetIteratorPrototype);
163
164
  /* WEAK MAP */
165
41.0k
  mxPush(mxObjectPrototype);
166
41.0k
  slot = fxLastProperty(the, fxNewObjectInstance(the));
167
41.0k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_WeakMap_prototype_delete), 1, mxID(_delete), XS_DONT_ENUM_FLAG);
168
41.0k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_WeakMap_prototype_get), 1, mxID(_get), XS_DONT_ENUM_FLAG);
169
41.0k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_WeakMap_prototype_has), 1, mxID(_has), XS_DONT_ENUM_FLAG);
170
41.0k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_WeakMap_prototype_set), 2, mxID(_set), XS_DONT_ENUM_FLAG);
171
41.0k
  slot = fxNextStringXProperty(the, slot, "WeakMap", mxID(_Symbol_toStringTag), XS_DONT_ENUM_FLAG | XS_DONT_SET_FLAG);
172
41.0k
#if mxECMAScript2026
173
41.0k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_WeakMap_prototype_getOrInsert), 2, mxID(_getOrInsert), XS_DONT_ENUM_FLAG);
174
41.0k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_WeakMap_prototype_getOrInsertComputed), 2, mxID(_getOrInsertComputed), XS_DONT_ENUM_FLAG);
175
41.0k
#endif
176
41.0k
  mxWeakMapPrototype = *the->stack;
177
41.0k
  slot = fxBuildHostConstructor(the, mxCallback(fx_WeakMap), 0, mxID(_WeakMap));
178
41.0k
  mxWeakMapConstructor = *the->stack;
179
41.0k
  mxPop();
180
  
181
  /* WEAK SET */
182
41.0k
  mxPush(mxObjectPrototype);
183
41.0k
  slot = fxLastProperty(the, fxNewObjectInstance(the));
184
41.0k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_WeakSet_prototype_add), 1, mxID(_add), XS_DONT_ENUM_FLAG);
185
41.0k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_WeakSet_prototype_delete), 1, mxID(_delete), XS_DONT_ENUM_FLAG);
186
41.0k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_WeakSet_prototype_has), 1, mxID(_has), XS_DONT_ENUM_FLAG);
187
41.0k
  slot = fxNextStringXProperty(the, slot, "WeakSet", mxID(_Symbol_toStringTag), XS_DONT_ENUM_FLAG | XS_DONT_SET_FLAG);
188
41.0k
  mxWeakSetPrototype = *the->stack;
189
41.0k
  slot = fxBuildHostConstructor(the, mxCallback(fx_WeakSet), 0, mxID(_WeakSet));
190
41.0k
  mxWeakSetConstructor = *the->stack;
191
41.0k
  mxPop();
192
  
193
  /* WEAK REF */
194
41.0k
  mxPush(mxObjectPrototype);
195
41.0k
  slot = fxLastProperty(the, fxNewObjectInstance(the));
196
41.0k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_WeakRef_prototype_deref), 0, mxID(_deref), XS_DONT_ENUM_FLAG);
197
41.0k
  slot = fxNextStringXProperty(the, slot, "WeakRef", mxID(_Symbol_toStringTag), XS_DONT_ENUM_FLAG | XS_DONT_SET_FLAG);
198
41.0k
  mxWeakRefPrototype = *the->stack;
199
41.0k
  slot = fxBuildHostConstructor(the, mxCallback(fx_WeakRef), 1, mxID(_WeakRef));
200
41.0k
  mxWeakRefConstructor = *the->stack;
201
41.0k
  mxPop();
202
  
203
  /* FINALIZATION REGISTRY */
204
41.0k
  mxPush(mxObjectPrototype);
205
41.0k
  slot = fxLastProperty(the, fxNewObjectInstance(the));
206
//  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_FinalizationRegistry_prototype_cleanupSome), 0, mxID(_cleanupSome), XS_DONT_ENUM_FLAG);
207
41.0k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_FinalizationRegistry_prototype_register), 2, mxID(_register), XS_DONT_ENUM_FLAG);
208
41.0k
  slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_FinalizationRegistry_prototype_unregister), 1, mxID(_unregister), XS_DONT_ENUM_FLAG);
209
41.0k
  slot = fxNextStringXProperty(the, slot, "FinalizationRegistry", mxID(_Symbol_toStringTag), XS_DONT_ENUM_FLAG | XS_DONT_SET_FLAG);
210
41.0k
  mxFinalizationRegistryPrototype = *the->stack;
211
41.0k
  slot = fxBuildHostConstructor(the, mxCallback(fx_FinalizationRegistry), 1, mxID(_FinalizationRegistry));
212
41.0k
  mxFinalizationRegistryConstructor = *the->stack;
213
41.0k
  mxPop();
214
41.0k
}
215
216
txSlot* fxCheckMapInstance(txMachine* the, txSlot* slot, txBoolean mutable)
217
13.9k
{
218
13.9k
  if (slot->kind == XS_REFERENCE_KIND) {
219
13.5k
    txSlot* instance = slot->value.reference;
220
13.5k
    if (((slot = instance->next)) && (slot->flag & XS_INTERNAL_FLAG) && (slot->kind == XS_MAP_KIND) && (instance != mxMapPrototype.value.reference)) {
221
11.4k
      if (mutable && (slot->flag & XS_DONT_SET_FLAG))
222
0
        mxTypeError("this: read-only Map instance");
223
11.4k
      return instance;
224
11.4k
    }
225
13.5k
  }
226
13.9k
  mxTypeError("this: not a Map instance");
227
0
  return C_NULL;
228
13.9k
}
229
230
txSlot* fxCheckMapKey(txMachine* the)
231
11.0k
{
232
11.0k
  if (mxArgc > 0) {
233
11.0k
    txSlot* slot = mxArgv(0);
234
11.0k
    return slot;
235
11.0k
  }
236
17
  return &mxUndefined;
237
11.0k
}
238
239
txSlot* fxNewMapInstance(txMachine* the)
240
333k
{
241
333k
  txSlot* map;
242
333k
  txSlot* table;
243
333k
  txSlot* list;
244
333k
  txSlot* size;
245
333k
  txSlot** address;
246
333k
  map = fxNewSlot(the);
247
333k
  map->kind = XS_INSTANCE_KIND;
248
333k
  map->value.instance.garbage = C_NULL;
249
333k
  map->value.instance.prototype = the->stack->value.reference;
250
333k
  the->stack->kind = XS_REFERENCE_KIND;
251
333k
  the->stack->value.reference = map;
252
333k
  table = map->next = fxNewSlot(the);
253
333k
  list = table->next = fxNewSlot(the);
254
333k
  size = list->next = fxNewSlot(the);
255
333k
  address = (txSlot**)fxNewChunk(the, mxTableMinLength * sizeof(txSlot*));
256
333k
  c_memset(address, 0, mxTableMinLength * sizeof(txSlot*));
257
  /* TABLE */
258
333k
  table->flag = XS_INTERNAL_FLAG;
259
333k
  table->kind = XS_MAP_KIND;
260
333k
  table->value.table.address = address;
261
333k
  table->value.table.length = mxTableMinLength;
262
  /* LIST */
263
333k
  list->flag = XS_INTERNAL_FLAG;
264
333k
  list->kind = XS_LIST_KIND;
265
333k
  list->value.list.first = C_NULL;
266
333k
  list->value.list.last = C_NULL;
267
  /* SIZE */
268
333k
  size->flag = XS_INTERNAL_FLAG;
269
333k
  size->kind = XS_INTEGER_KIND;
270
333k
  size->value.integer = 0;
271
333k
  return map;
272
333k
}
273
274
void fx_Map(txMachine* the)
275
327k
{
276
327k
  txSlot *function, *iterable, *iterator, *next, *value;
277
327k
  if (!mxHasTarget)
278
38
    mxTypeError("call: Map");
279
327k
  mxPushSlot(mxTarget);
280
327k
  fxGetPrototypeFromConstructor(the, &mxMapPrototype);
281
327k
  fxNewMapInstance(the);
282
327k
  mxPullSlot(mxResult);
283
327k
  if (mxArgc < 1)
284
325k
    return;
285
2.15k
  iterable = mxArgv(0);
286
2.15k
  if ((iterable->kind == XS_UNDEFINED_KIND) || (iterable->kind == XS_NULL_KIND))
287
147
    return;
288
2.00k
  mxPushSlot(mxResult);
289
2.00k
  mxGetID(mxID(_set)); 
290
2.00k
  function = the->stack;  
291
2.00k
  if (!fxIsCallable(the, function))  
292
1
    mxTypeError("result.set: not a function");
293
2.00k
  mxTemporary(iterator);
294
2.00k
  mxTemporary(next);
295
2.00k
  fxGetIterator(the, iterable, iterator, next, 0);  
296
2.00k
  mxTemporary(value);
297
7.77k
  while (fxIteratorNext(the, iterator, next, value)) {
298
5.80k
    mxTry(the) {
299
5.80k
      if (value->kind != XS_REFERENCE_KIND)
300
18
        mxTypeError("item: not an object");
301
5.78k
      mxPushSlot(mxResult);
302
5.78k
      mxPushSlot(function);
303
5.78k
      mxCall();
304
5.78k
      mxPushSlot(value);
305
5.78k
      mxGetIndex(0);
306
5.78k
      mxPushSlot(value);
307
5.78k
      mxGetIndex(1);
308
5.78k
      mxRunCount(2);
309
5.78k
      mxPop();
310
5.78k
    }
311
5.78k
    mxCatch(the) {
312
18
      fxIteratorReturn(the, iterator, 1);
313
18
      fxJump(the);
314
18
    }
315
5.80k
  }
316
2.00k
}
317
318
static void fx_Map_groupByAux(txMachine* the)
319
37.9k
{
320
37.9k
  txSlot* instance = mxResult->value.reference;
321
37.9k
  txSlot* table = instance->next;
322
37.9k
  txSlot* list = table->next;
323
37.9k
  txSlot* key = the->stack;
324
37.9k
  txSlot* entry = fxGetEntry(the, table, key);
325
37.9k
  if (entry)
326
417
    mxPushSlot(entry->next);
327
37.5k
  else {
328
37.5k
    mxPush(mxArrayPrototype);
329
37.5k
    fxNewArrayInstance(the);
330
37.5k
    fxSetEntry(the, table, list, key, the->stack, 0);
331
37.5k
  }
332
37.9k
}
333
334
void fx_Map_groupBy(txMachine* the)
335
5.79k
{
336
5.79k
  txSlot *instance, *key, *value;
337
5.79k
  mxPush(mxMapPrototype);
338
5.79k
  instance = fxNewMapInstance(the);
339
5.79k
  mxPullSlot(mxResult);
340
5.79k
  fxGroupBy(the, fx_Map_groupByAux);
341
5.79k
  key = instance->next->next->value.list.first;
342
43.3k
  while (key) {
343
37.5k
    value = key->next;
344
37.5k
    fxCacheArray(the, value->value.reference);
345
37.5k
    key = value->next;
346
37.5k
  }
347
5.79k
}
348
349
void fx_Map_prototype_clear(txMachine* the)
350
583
{
351
583
  txSlot* instance = fxCheckMapInstance(the, mxThis, XS_MUTABLE);
352
583
  txSlot* table = instance->next;
353
583
  txSlot* list = table->next;
354
583
  fxClearEntries(the, table, list, 1);
355
583
}
356
357
void fx_Map_prototype_delete(txMachine* the)
358
322
{
359
322
  txSlot* instance = fxCheckMapInstance(the, mxThis, XS_MUTABLE);
360
322
  txSlot* table = instance->next;
361
322
  txSlot* list = table->next;
362
322
  txSlot* key = fxCheckMapKey(the);
363
322
  mxResult->value.boolean = fxDeleteEntry(the, table, list, key, 1, 1);
364
322
  mxResult->kind = XS_BOOLEAN_KIND;
365
322
}
366
367
void fx_Map_prototype_entries(txMachine* the)
368
323
{
369
323
  fxCheckMapInstance(the, mxThis, XS_IMMUTABLE);
370
323
  fxNewMapIteratorInstance(the, mxThis, 2);
371
323
}
372
373
void fx_Map_prototype_forEach(txMachine* the)
374
57
{
375
57
  txSlot* instance = fxCheckMapInstance(the, mxThis, XS_IMMUTABLE);
376
57
  txSlot* table = instance->next;
377
57
  txSlot* list = table->next;
378
57
  txSlot* function = fxArgToCallback(the, 0);
379
57
  txSlot* key;
380
57
  txSlot* value;
381
57
  mxPushList();
382
57
  key = the->stack->value.list.first = list->value.list.first;
383
82
  while (key) {
384
25
    value = key->next;
385
25
    if (!(key->flag & XS_DONT_ENUM_FLAG)) {
386
      /* THIS */
387
25
      if (mxArgc > 1)
388
10
        mxPushSlot(mxArgv(1));
389
15
      else
390
15
        mxPushUndefined();
391
      /* FUNCTION */
392
25
      mxPushSlot(function);
393
25
      mxCall();
394
      /* ARGUMENTS */
395
25
      mxPushSlot(value);
396
25
      mxPushSlot(key);
397
25
      mxPushSlot(mxThis);
398
25
      mxRunCount(3);
399
25
      mxPop();
400
25
    }
401
25
    key = the->stack->value.list.first = value->next;
402
25
  }
403
57
  mxPop();
404
57
}
405
406
void fx_Map_prototype_get(txMachine* the)
407
1.38k
{
408
1.38k
  txSlot* instance = fxCheckMapInstance(the, mxThis, XS_IMMUTABLE);
409
1.38k
  txSlot* table = instance->next;
410
1.38k
  txSlot* key = fxCheckMapKey(the);
411
1.38k
  txSlot* result = fxGetEntry(the, table, key);
412
1.38k
  if (result) {
413
1.27k
    txSlot* value = result->next;
414
1.27k
    mxResult->kind = value->kind;
415
1.27k
    mxResult->value = value->value;
416
1.27k
  }
417
1.38k
}
418
419
void fx_Map_prototype_getOrInsert(txMachine* the)
420
2.22k
{
421
2.22k
  txSlot* instance = fxCheckMapInstance(the, mxThis, XS_MUTABLE);
422
2.22k
  txSlot* table = instance->next;
423
2.22k
  txSlot* list = table->next;
424
2.22k
  txSlot* key = fxCheckMapKey(the);
425
2.22k
  *mxResult = (mxArgc > 1) ? *mxArgv(1) : mxUndefined;
426
2.22k
  fxSetEntry(the, table, list, key, mxResult, 1);
427
2.22k
}
428
429
void fx_Map_prototype_getOrInsertComputed(txMachine* the)
430
75
{
431
75
  txSlot* instance = fxCheckMapInstance(the, mxThis, XS_MUTABLE);
432
75
  txSlot* table = instance->next;
433
75
  txSlot* list = table->next;
434
75
  txSlot* function = fxArgToCallback(the, 1);
435
75
  txSlot* key = fxCheckMapKey(the);
436
75
  txSlot* result = fxGetEntry(the, table, key);
437
75
  if (result) {
438
18
    txSlot* value = result->next;
439
18
    mxResult->kind = value->kind;
440
18
    mxResult->value = value->value;
441
18
    return;
442
18
  }
443
57
  mxPushUndefined();
444
57
  mxPushSlot(function);
445
57
  mxCall();
446
57
  mxPushSlot(key);
447
57
  mxRunCount(1);
448
57
  mxPullSlot(mxResult);
449
57
  fxSetEntry(the, table, list, key, mxResult, 0);
450
57
}
451
452
void fx_Map_prototype_has(txMachine* the)
453
306
{
454
306
  txSlot* instance = fxCheckMapInstance(the, mxThis, XS_IMMUTABLE);
455
306
  txSlot* table = instance->next;
456
306
  txSlot* key = fxCheckMapKey(the);
457
306
  txSlot* result = fxGetEntry(the, table, key);
458
306
  mxResult->kind = XS_BOOLEAN_KIND;
459
306
  mxResult->value.boolean = (result) ? 1 : 0;
460
306
}
461
462
void fx_Map_prototype_keys(txMachine* the)
463
34
{
464
34
  fxCheckMapInstance(the, mxThis, XS_IMMUTABLE);
465
34
  fxNewMapIteratorInstance(the, mxThis, 0);
466
34
}
467
468
void fx_Map_prototype_set(txMachine* the)
469
8.32k
{
470
8.32k
  txSlot* instance = fxCheckMapInstance(the, mxThis, XS_MUTABLE);
471
8.32k
  txSlot* table = instance->next;
472
8.32k
  txSlot* list = table->next;
473
8.32k
  txSlot* key = fxCheckMapKey(the);
474
8.32k
  fxSetEntry(the, table, list, key, (mxArgc > 1) ? mxArgv(1) : &mxUndefined, 0);
475
8.32k
  *mxResult = *mxThis;
476
8.32k
}
477
478
void fx_Map_prototype_size(txMachine* the)
479
239
{
480
239
  txSlot* instance = fxCheckMapInstance(the, mxThis, XS_IMMUTABLE);
481
239
  txSlot* table = instance->next;
482
239
  txSlot* list = table->next;
483
239
  mxResult->kind = XS_INTEGER_KIND;
484
239
  mxResult->value.integer = list->next->value.integer;
485
239
}
486
487
void fx_Map_prototype_values(txMachine* the)
488
75
{
489
75
  fxCheckMapInstance(the, mxThis, XS_IMMUTABLE);
490
75
  fxNewMapIteratorInstance(the, mxThis, 1);
491
75
}
492
493
txSlot* fxNewMapIteratorInstance(txMachine* the, txSlot* iterable, txInteger kind) 
494
76
{
495
76
  txSlot* instance;
496
76
  txSlot* property;
497
76
  mxPush(mxMapIteratorPrototype);
498
76
  instance = fxNewIteratorInstance(the, iterable, mxID(_Map));
499
76
  property = fxLastProperty(the, instance);
500
76
  property->kind = XS_LIST_KIND;
501
76
  property->value.list.first = C_NULL;
502
76
  property->value.list.last = C_NULL;
503
76
  property = fxNextIntegerProperty(the, property, kind, XS_NO_ID, XS_INTERNAL_FLAG);
504
76
  mxPullSlot(mxResult);
505
76
  return instance;
506
76
}
507
508
void fx_MapIterator_prototype_next(txMachine* the)
509
59
{
510
59
  txSlot* iterator = fxCheckIteratorInstance(the, mxThis, mxID(_Map));
511
59
  txSlot* result = iterator->next;
512
59
  txSlot* iterable = result->next;
513
59
  mxResult->kind = result->kind;
514
59
  mxResult->value = result->value;
515
59
  result = fxCheckIteratorResult(the, result);
516
59
  if (result->next->value.boolean == 0) {
517
53
    txSlot* list = iterable->next;
518
53
    txInteger kind = list->next->value.integer;
519
53
    txSlot* key = list->value.list.first;
520
53
    if (key)
521
18
      key = key->next->next;
522
35
    else
523
35
      key = iterable->value.reference->next->next->value.list.first;
524
56
    while (key && (key->flag & XS_DONT_ENUM_FLAG))
525
3
      key = key->next->next;
526
53
    if (key) {
527
35
      txSlot* value = key->next;
528
35
      if (kind == 2) {
529
15
        mxPushSlot(key);
530
15
        mxPushSlot(value);
531
15
        fxConstructArrayEntry(the, result);
532
15
      }
533
20
      else if (kind == 1) {
534
6
        result->kind = value->kind;
535
6
        result->value = value->value;
536
6
      }
537
14
      else {
538
14
        result->kind = key->kind;
539
14
        result->value = key->value;
540
14
      }
541
35
      list->value.list.first = key;
542
35
    }
543
18
    else {
544
18
      result->kind = XS_UNDEFINED_KIND;
545
18
      result->next->value.boolean = 1;
546
18
      list->value.list.first = C_NULL;
547
18
    }
548
53
  }
549
59
}
550
551
txSlot* fxCheckSetInstance(txMachine* the, txSlot* slot, txBoolean mutable)
552
25.1M
{
553
25.1M
  if (slot->kind == XS_REFERENCE_KIND) {
554
25.1M
    txSlot* instance = slot->value.reference;
555
25.1M
    if (((slot = instance->next)) && (slot->flag & XS_INTERNAL_FLAG) && (slot->kind == XS_SET_KIND) && (instance != mxSetPrototype.value.reference)) {
556
25.1M
      if (mutable && (slot->flag & XS_DONT_SET_FLAG))
557
0
        mxTypeError("this: read-only Set instance");
558
25.1M
      return instance;
559
25.1M
    }
560
25.1M
  }
561
25.1M
  mxTypeError("this: not a Set instance");
562
0
  return C_NULL;
563
25.1M
}
564
565
txSlot* fxCheckSetValue(txMachine* the)
566
24.7M
{
567
24.7M
  if (mxArgc > 0) {
568
24.7M
    txSlot* slot = mxArgv(0);
569
24.7M
    return slot;
570
24.7M
  }
571
7
  return &mxUndefined;
572
24.7M
}
573
574
txSlot* fxCheckSetRecord(txMachine* the, txInteger* otherSize, txSlot** otherHas, txSlot** otherKeys)
575
168k
{
576
168k
  txSlot* other;
577
168k
  txNumber size;
578
168k
  if (mxArgc < 1)
579
1
    mxTypeError("other is no object");
580
168k
  other = mxArgv(0);  
581
168k
  if (!mxIsReference(other))
582
41
    mxTypeError("other is no object");
583
    
584
168k
  mxPushSlot(other);
585
168k
  mxGetID(mxID(_size));  
586
168k
  size = fxToNumber(the, the->stack);
587
168k
  if (c_isnan(size))
588
10
    mxTypeError("other.size is NaN");
589
168k
  size = c_trunc(size);
590
168k
  if (size < 0)
591
1
    mxRangeError("other.size < 0");
592
168k
  if (size > 0x7FFFFFFF)
593
0
    size = 0x7FFFFFFF;
594
168k
  if (otherSize)
595
168k
    *otherSize = (txInteger)size;
596
168k
  mxPop();
597
  
598
168k
  mxPushSlot(other);
599
168k
  mxGetID(mxID(_has));
600
168k
  if (!fxIsCallable(the, the->stack))
601
10
    mxTypeError("other.has is no function");
602
168k
  if (otherHas)
603
166k
    *otherHas = the->stack;
604
      
605
168k
  mxPushSlot(other);
606
168k
  mxGetID(mxID(_keys));
607
168k
  if (!fxIsCallable(the, the->stack))
608
10
    mxTypeError("other.keys is no function");
609
168k
  if (otherKeys)
610
64.8k
    *otherKeys = the->stack;
611
    
612
168k
  return other;
613
168k
}
614
615
txSlot* fxNewSetInstance(txMachine* the, txInteger tableLength)
616
1.10M
{
617
1.10M
  txSlot* set;
618
1.10M
  txSlot* table;
619
1.10M
  txSlot* list;
620
1.10M
  txSlot* size;
621
1.10M
  txSlot** address;
622
1.10M
  set = fxNewSlot(the);
623
1.10M
  set->kind = XS_INSTANCE_KIND;
624
1.10M
  set->value.instance.garbage = C_NULL;
625
1.10M
  set->value.instance.prototype = the->stack->value.reference;
626
1.10M
  the->stack->kind = XS_REFERENCE_KIND;
627
1.10M
  the->stack->value.reference = set;
628
1.10M
  table = set->next = fxNewSlot(the);
629
1.10M
  list = table->next = fxNewSlot(the);
630
1.10M
  size = list->next = fxNewSlot(the);
631
1.10M
  address = (txSlot**)fxNewChunk(the, tableLength * sizeof(txSlot*));
632
1.10M
  c_memset(address, 0, tableLength * sizeof(txSlot*));
633
  /* TABLE */
634
1.10M
  table->flag = XS_INTERNAL_FLAG;
635
1.10M
  table->kind = XS_SET_KIND;
636
1.10M
  table->value.table.address = address;
637
1.10M
  table->value.table.length = tableLength;
638
  /* LIST */
639
1.10M
  list->flag = XS_INTERNAL_FLAG;
640
1.10M
  list->kind = XS_LIST_KIND;
641
1.10M
  list->value.list.first = C_NULL;
642
1.10M
  list->value.list.last = C_NULL;
643
  /* SIZE */
644
1.10M
  size->flag = XS_INTERNAL_FLAG;
645
1.10M
  size->kind = XS_INTEGER_KIND;
646
1.10M
  size->value.integer = 0;
647
1.10M
  return set;
648
1.10M
}
649
650
void fxNewSetResult(txMachine* the, txSlot* table, txSlot* list, txSlot** tableAddress, txSlot** listAddress)
651
62.2k
{
652
62.2k
  txSlot *resultInstance, *resultTable, *resultList, *value;
653
62.2k
  mxPush(mxSetPrototype);
654
62.2k
  resultInstance = fxNewSetInstance(the, (list) ? table->value.table.length : mxTableMinLength);
655
62.2k
  mxPullSlot(mxResult);
656
62.2k
  resultTable = resultInstance->next;
657
62.2k
  resultList = resultTable->next;
658
62.2k
  if (list) {
659
62.2k
    value = list->value.list.first;
660
327k
    while (value) {
661
264k
      fxSetEntry(the, resultTable, resultList, value, C_NULL, 0);
662
264k
      value = value->next;
663
264k
    }  
664
62.2k
  }  
665
62.2k
  *tableAddress = resultTable;
666
62.2k
  *listAddress = resultList;
667
62.2k
}
668
669
txBoolean fxSetRecordHas(txMachine* the, txSlot* other, txSlot* otherHas, txSlot* value)
670
288k
{
671
288k
  txBoolean result;
672
288k
  mxPushSlot(other);
673
288k
  mxPushSlot(otherHas);
674
288k
  mxCall();
675
288k
  mxPushSlot(value);
676
288k
  mxRunCount(1);
677
288k
  result = fxToBoolean(the, the->stack);
678
288k
  mxPop();
679
288k
  return result;
680
288k
}
681
682
void fxSetRecordKeys(txMachine* the, txSlot* other, txSlot* otherKeys, txSlot** iterator, txSlot** next, txSlot** value)
683
18.6k
{
684
18.6k
  mxPushSlot(other);
685
18.6k
  mxPushSlot(otherKeys);
686
18.6k
  mxCall();
687
18.6k
  mxRunCount(0);
688
18.6k
  *iterator = the->stack;
689
18.6k
  mxDub();
690
18.6k
  mxGetID(mxID(_next));
691
18.6k
  *next = the->stack;
692
18.6k
  mxPushUndefined();
693
18.6k
  *value = the->stack;
694
18.6k
}
695
696
void fx_Set(txMachine* the)
697
1.04M
{
698
1.04M
  txSlot *function, *iterable, *iterator, *next, *value;
699
1.04M
  if (!mxHasTarget)
700
845
    mxTypeError("call: Set");
701
1.04M
  mxPushSlot(mxTarget);
702
1.04M
  fxGetPrototypeFromConstructor(the, &mxSetPrototype);
703
1.04M
  fxNewSetInstance(the, mxTableMinLength);
704
1.04M
  mxPullSlot(mxResult);
705
1.04M
  if (mxArgc < 1)
706
1.73k
    return;
707
1.04M
  iterable = mxArgv(0);
708
1.04M
  if ((iterable->kind == XS_UNDEFINED_KIND) || (iterable->kind == XS_NULL_KIND))
709
13.6k
    return;
710
1.02M
  mxPushSlot(mxResult);
711
1.02M
  mxGetID(mxID(_add)); 
712
1.02M
  function = the->stack;  
713
1.02M
  if (!fxIsCallable(the, function))  
714
3
    mxTypeError("result.add: not a function");
715
1.02M
  mxTemporary(iterator);
716
1.02M
  mxTemporary(next);
717
1.02M
  fxGetIterator(the, iterable, iterator, next, 0);  
718
1.02M
  mxTemporary(value);
719
25.1M
  while (fxIteratorNext(the, iterator, next, value)) {
720
24.1M
    mxTry(the) {
721
24.1M
      mxPushSlot(mxResult);
722
24.1M
      mxPushSlot(function);
723
24.1M
      mxCall();
724
24.1M
      mxPushSlot(value);
725
24.1M
      mxRunCount(1);
726
24.1M
      mxPop();
727
24.1M
    }
728
24.1M
    mxCatch(the) {
729
1
      fxIteratorReturn(the, iterator, 1);
730
1
      fxJump(the);
731
1
    }
732
24.1M
  }
733
1.02M
}
734
735
void fx_Set_prototype_add(txMachine* the)
736
24.2M
{
737
24.2M
  txSlot* instance = fxCheckSetInstance(the, mxThis, XS_MUTABLE);
738
24.2M
  txSlot* table = instance->next;
739
24.2M
  txSlot* list = table->next;
740
24.2M
  txSlot* value = fxCheckSetValue(the);
741
24.2M
  fxSetEntry(the, table, list, value, C_NULL, 0);
742
24.2M
  *mxResult = *mxThis;
743
24.2M
}
744
745
void fx_Set_prototype_clear(txMachine* the)
746
65.0k
{
747
65.0k
  txSlot* instance = fxCheckSetInstance(the, mxThis, XS_MUTABLE);
748
65.0k
  txSlot* table = instance->next;
749
65.0k
  txSlot* list = table->next;
750
65.0k
  fxClearEntries(the, table, list, 0);
751
65.0k
}
752
753
void fx_Set_prototype_delete(txMachine* the)
754
143k
{
755
143k
  txSlot* instance = fxCheckSetInstance(the, mxThis, XS_MUTABLE);
756
143k
  txSlot* table = instance->next;
757
143k
  txSlot* list = table->next;
758
143k
  txSlot* value = fxCheckSetValue(the);
759
143k
  mxResult->value.boolean = fxDeleteEntry(the, table, list, value, 0, 1);
760
143k
  mxResult->kind = XS_BOOLEAN_KIND;
761
143k
}
762
763
void fx_Set_prototype_difference(txMachine* the)
764
62.2k
{
765
62.2k
  txSlot* stack = the->stack;
766
62.2k
  txSlot* instance = fxCheckSetInstance(the, mxThis, XS_IMMUTABLE);
767
62.2k
  txSlot* table = instance->next;
768
62.2k
  txSlot* list = table->next;
769
62.2k
  txInteger size = list->next->value.integer, otherSize;
770
62.2k
  txSlot *otherHas, *otherKeys, *resultTable, *resultList, *iterator, *next, *value;
771
62.2k
  txSlot *other = fxCheckSetRecord(the, &otherSize, &otherHas, &otherKeys);
772
62.2k
  fxNewSetResult(the, table, list, &resultTable, &resultList);
773
62.2k
  if (size <= otherSize) {
774
46.1k
    mxPushList();
775
46.1k
    value = the->stack->value.list.first = list->value.list.first;
776
230k
    while (value) {
777
184k
      if (!(value->flag & XS_DONT_ENUM_FLAG)) {
778
184k
        if (fxSetRecordHas(the, other, otherHas, value))
779
184k
          fxDeleteEntry(the, resultTable, resultList, value, 0, 0);
780
184k
      }
781
184k
      value = the->stack->value.list.first = value->next;
782
184k
    }
783
46.1k
  }
784
16.0k
  else {
785
16.0k
    fxSetRecordKeys(the, other, otherKeys, &iterator, &next, &value);
786
408k
    while (fxIteratorNext(the, iterator, next, value)) {
787
392k
      fxCanonicalizeKeyedCollectionKey(value);
788
392k
      fxDeleteEntry(the, resultTable, resultList, value, 0, 0);
789
392k
    }
790
16.0k
  }
791
62.2k
  fxResizeEntries(the, resultTable, resultList);
792
62.2k
  fxPurgeEntries(the, resultList);
793
62.2k
  the->stack = stack;
794
62.2k
}
795
796
void fx_Set_prototype_entries(txMachine* the)
797
33
{
798
33
  fxCheckSetInstance(the, mxThis, XS_IMMUTABLE);
799
33
  fxNewSetIteratorInstance(the, mxThis, 2);
800
33
}
801
802
void fx_Set_prototype_forEach(txMachine* the)
803
24
{
804
24
  txSlot* instance = fxCheckSetInstance(the, mxThis, XS_IMMUTABLE);
805
24
  txSlot* table = instance->next;
806
24
  txSlot* list = table->next;
807
24
  txSlot* function = fxArgToCallback(the, 0);
808
24
  txSlot* value;
809
24
  mxPushList();
810
24
  value = the->stack->value.list.first = list->value.list.first;
811
42
  while (value) {
812
18
    if (!(value->flag & XS_DONT_ENUM_FLAG)) {
813
      /* THIS */
814
18
      if (mxArgc > 1)
815
3
        mxPushSlot(mxArgv(1));
816
15
      else
817
15
        mxPushUndefined();
818
      /* FUNCTION */
819
18
      mxPushSlot(function);
820
18
      mxCall();
821
      /* ARGUMENTS */
822
18
      mxPushSlot(value);
823
18
      mxPushSlot(value);
824
18
      mxPushSlot(mxThis);
825
18
      mxRunCount(3);
826
18
      mxPop();
827
18
    }
828
18
    value = the->stack->value.list.first = value->next;
829
18
  }
830
24
  mxPop();
831
24
}
832
833
void fx_Set_prototype_has(txMachine* the)
834
288k
{
835
288k
  txSlot* instance = fxCheckSetInstance(the, mxThis, XS_IMMUTABLE);
836
288k
  txSlot* table = instance->next;
837
288k
  txSlot* value = fxCheckSetValue(the);
838
288k
  txSlot* result = fxGetEntry(the, table, value);
839
288k
  mxResult->kind = XS_BOOLEAN_KIND;
840
288k
  mxResult->value.boolean = (result) ? 1 : 0;
841
288k
}
842
843
void fx_Set_prototype_intersection(txMachine* the)
844
171
{
845
171
  txSlot* stack = the->stack;
846
171
  txSlot* instance = fxCheckSetInstance(the, mxThis, XS_IMMUTABLE);
847
171
  txSlot* table = instance->next;
848
171
  txSlot* list = table->next;
849
171
  txInteger size = list->next->value.integer, otherSize;
850
171
  txSlot *otherHas, *otherKeys, *resultTable, *resultList, *iterator, *next, *value;
851
171
  txSlot *other = fxCheckSetRecord(the, &otherSize, &otherHas, &otherKeys);
852
171
  fxNewSetResult(the, table, C_NULL, &resultTable, &resultList);
853
171
  if (size <= otherSize) {
854
16
    mxPushList();
855
16
    value = the->stack->value.list.first = list->value.list.first;
856
42
    while (value) {
857
26
      if (!(value->flag & XS_DONT_ENUM_FLAG)) {
858
26
        if (fxSetRecordHas(the, other, otherHas, value))
859
15
          fxSetEntry(the, resultTable, resultList, value, C_NULL, 0);
860
26
      }
861
26
      value = the->stack->value.list.first = value->next;
862
26
    }
863
16
  }
864
155
  else {
865
155
    fxSetRecordKeys(the, other, otherKeys, &iterator, &next, &value);
866
179k
    while (fxIteratorNext(the, iterator, next, value)) {
867
179k
      fxCanonicalizeKeyedCollectionKey(value);
868
179k
      if (fxGetEntry(the, table, value))
869
3
        fxSetEntry(the, resultTable, resultList, value, C_NULL, 0);
870
179k
    }
871
155
  }
872
171
  the->stack = stack;
873
171
}
874
875
void fx_Set_prototype_isDisjointFrom(txMachine* the)
876
123
{
877
123
  txSlot* stack = the->stack;
878
123
  txSlot* instance = fxCheckSetInstance(the, mxThis, XS_IMMUTABLE);
879
123
  txSlot* table = instance->next;
880
123
  txSlot* list = table->next;
881
123
  txInteger size = list->next->value.integer, otherSize;
882
123
  txSlot *otherHas, *otherKeys, *iterator, *next, *value;
883
123
  txSlot *other = fxCheckSetRecord(the, &otherSize, &otherHas, &otherKeys);
884
123
  mxResult->value.boolean = 0;
885
123
  mxResult->kind = XS_BOOLEAN_KIND;
886
123
  if (size <= otherSize) {
887
24
    mxPushList();
888
24
    value = the->stack->value.list.first = list->value.list.first;
889
38
    while (value) {
890
26
      if (!(value->flag & XS_DONT_ENUM_FLAG)) {
891
26
        if (fxSetRecordHas(the, other, otherHas, value))
892
12
          goto bail;
893
26
      }
894
14
      value = the->stack->value.list.first = value->next;
895
14
    }
896
24
  }
897
99
  else {
898
99
    fxSetRecordKeys(the, other, otherKeys, &iterator, &next, &value);
899
470k
    while (fxIteratorNext(the, iterator, next, value)) {
900
470k
      if (fxGetEntry(the, table, value)) {
901
7
        fxIteratorReturn(the, iterator, 0);
902
7
        goto bail;
903
7
      }
904
470k
    }
905
99
  }
906
104
  mxResult->value.boolean = 1;
907
104
bail:
908
47
  the->stack = stack;
909
47
}
910
911
void fx_Set_prototype_isSubsetOf(txMachine* the)
912
103k
{
913
103k
  txSlot* stack = the->stack;
914
103k
  txSlot* instance = fxCheckSetInstance(the, mxThis, XS_IMMUTABLE);
915
103k
  txSlot* table = instance->next;
916
103k
  txSlot* list = table->next;
917
103k
  txInteger size = list->next->value.integer, otherSize;
918
103k
  txSlot *otherHas, *value;
919
103k
  txSlot *other = fxCheckSetRecord(the, &otherSize, &otherHas, C_NULL);
920
103k
  mxResult->value.boolean = 0;
921
103k
  mxResult->kind = XS_BOOLEAN_KIND;
922
103k
  if (size > otherSize)
923
1
    goto bail;
924
103k
  mxPushList();
925
103k
  value = the->stack->value.list.first = list->value.list.first;
926
207k
  while (value) {
927
103k
    if (!(value->flag & XS_DONT_ENUM_FLAG)) {
928
103k
      if (!fxSetRecordHas(the, other, otherHas, value))
929
6
        goto bail;
930
103k
    }
931
103k
    value = the->stack->value.list.first = value->next;
932
103k
  }
933
103k
  mxResult->value.boolean = 1;
934
103k
bail:
935
103k
  the->stack = stack;
936
103k
}
937
938
void fx_Set_prototype_isSupersetOf(txMachine* the)
939
2.62k
{
940
2.62k
  txSlot* stack = the->stack;
941
2.62k
  txSlot* instance = fxCheckSetInstance(the, mxThis, XS_IMMUTABLE);
942
2.62k
  txSlot* table = instance->next;
943
2.62k
  txSlot* list = table->next;
944
2.62k
  txInteger size = list->next->value.integer, otherSize;
945
2.62k
  txSlot *otherKeys, *iterator, *next, *value;
946
2.62k
  txSlot *other = fxCheckSetRecord(the, &otherSize, C_NULL, &otherKeys);
947
2.62k
  mxResult->value.boolean = 0;
948
2.62k
  mxResult->kind = XS_BOOLEAN_KIND;
949
2.62k
  if (size < otherSize)
950
4
    goto bail;
951
2.62k
  fxSetRecordKeys(the, other, otherKeys, &iterator, &next, &value);
952
111k
  while (fxIteratorNext(the, iterator, next, value)) {
953
109k
    if (!fxGetEntry(the, table, value)) {
954
5
      fxIteratorReturn(the, iterator, 0);
955
5
      goto bail;
956
5
    }
957
109k
  }
958
2.61k
  mxResult->value.boolean = 1;
959
2.61k
bail:
960
17
  the->stack = stack;
961
17
}
962
963
void fx_Set_prototype_size(txMachine* the)
964
166k
{
965
166k
  txSlot* instance = fxCheckSetInstance(the, mxThis, XS_IMMUTABLE);
966
166k
  txSlot* table = instance->next;
967
166k
  txSlot* list = table->next;
968
166k
  mxResult->kind = XS_INTEGER_KIND;
969
166k
  mxResult->value.integer = list->next->value.integer;
970
166k
}
971
972
void fx_Set_prototype_symmetricDifference(txMachine* the)
973
83
{
974
83
  txSlot* stack = the->stack;
975
83
  txSlot* instance = fxCheckSetInstance(the, mxThis, XS_IMMUTABLE);
976
83
  txSlot* table = instance->next;
977
83
  txSlot* list = table->next;
978
83
  txSlot *otherKeys, *resultTable, *resultList, *iterator, *next, *value;
979
83
  txSlot *other = fxCheckSetRecord(the, C_NULL, C_NULL, &otherKeys);
980
83
  fxNewSetResult(the, table, list, &resultTable, &resultList);
981
83
  fxSetRecordKeys(the, other, otherKeys, &iterator, &next, &value);
982
34.5k
  while (fxIteratorNext(the, iterator, next, value)) {
983
34.4k
    fxCanonicalizeKeyedCollectionKey(value);
984
34.4k
    if (fxGetEntry(the, table, value))
985
35
      fxDeleteEntry(the, resultTable, resultList, value, 0, 0);
986
34.4k
    else
987
34.4k
      fxSetEntry(the, resultTable, resultList, value, C_NULL, 0);
988
34.4k
  }
989
83
  fxResizeEntries(the, resultTable, resultList);
990
83
  fxPurgeEntries(the, resultList);
991
83
  the->stack = stack;
992
83
}
993
994
void fx_Set_prototype_union(txMachine* the)
995
108
{
996
108
  txSlot* stack = the->stack;
997
108
  txSlot* instance = fxCheckSetInstance(the, mxThis, XS_IMMUTABLE);
998
108
  txSlot* table = instance->next;
999
108
  txSlot* list = table->next;
1000
108
  txSlot *otherKeys, *resultTable, *resultList, *iterator, *next, *value;
1001
108
  txSlot *other = fxCheckSetRecord(the, C_NULL, C_NULL, &otherKeys);
1002
108
  fxNewSetResult(the, table, list, &resultTable, &resultList);
1003
108
  fxSetRecordKeys(the, other, otherKeys, &iterator, &next, &value);
1004
37.5k
  while (fxIteratorNext(the, iterator, next, value)) {
1005
37.4k
    fxCanonicalizeKeyedCollectionKey(value);
1006
37.4k
    fxSetEntry(the, resultTable, resultList, value, C_NULL, 0);
1007
37.4k
  }
1008
108
  the->stack = stack;
1009
108
}
1010
1011
void fx_Set_prototype_values(txMachine* the)
1012
82.0k
{
1013
82.0k
  fxCheckSetInstance(the, mxThis, XS_IMMUTABLE);
1014
82.0k
  fxNewSetIteratorInstance(the, mxThis, 1);
1015
82.0k
}
1016
1017
txSlot* fxNewSetIteratorInstance(txMachine* the, txSlot* iterable, txInteger kind) 
1018
78.3k
{
1019
78.3k
  txSlot* instance;
1020
78.3k
  txSlot* property;
1021
78.3k
  mxPush(mxSetIteratorPrototype);
1022
78.3k
  instance = fxNewIteratorInstance(the, iterable, mxID(_Set));
1023
78.3k
  property = fxLastProperty(the, instance);
1024
78.3k
  property->kind = XS_LIST_KIND;
1025
78.3k
  property->value.list.first = C_NULL;
1026
78.3k
  property->value.list.last = C_NULL;
1027
78.3k
  property = fxNextIntegerProperty(the, property, kind, XS_NO_ID, XS_INTERNAL_FLAG);
1028
78.3k
  mxPullSlot(mxResult);
1029
78.3k
  return instance;
1030
78.3k
}
1031
1032
void fx_SetIterator_prototype_next(txMachine* the)
1033
174k
{
1034
174k
  txSlot* iterator = fxCheckIteratorInstance(the, mxThis, mxID(_Set));
1035
174k
  txSlot* result = iterator->next;
1036
174k
  txSlot* iterable = result->next;
1037
174k
  mxResult->kind = result->kind;
1038
174k
  mxResult->value = result->value;
1039
174k
  result = fxCheckIteratorResult(the, result);
1040
174k
  if (result->next->value.boolean == 0) {
1041
174k
    txSlot* list = iterable->next;
1042
174k
    txInteger kind = list->next->value.integer;
1043
174k
    txSlot* value = list->value.list.first;
1044
174k
    if (value)
1045
96.2k
      value = value->next;
1046
78.2k
    else
1047
78.2k
      value = iterable->value.reference->next->next->value.list.first;
1048
174k
    while (value && (value->flag & XS_DONT_ENUM_FLAG))
1049
0
      value = value->next;
1050
174k
    if (value) {
1051
96.2k
      if (kind == 2) {
1052
6
        mxPushSlot(value);
1053
6
        mxPushSlot(value);
1054
6
        fxConstructArrayEntry(the, result);
1055
6
      }
1056
96.2k
      else {
1057
96.2k
        result->kind = value->kind;
1058
96.2k
        result->value = value->value;
1059
96.2k
      }
1060
96.2k
      list->value.list.first = value;
1061
96.2k
    }
1062
78.2k
    else {
1063
78.2k
      result->kind = XS_UNDEFINED_KIND;
1064
78.2k
      result->next->value.boolean = 1;
1065
78.2k
      list->value.list.first = C_NULL;
1066
78.2k
    }
1067
174k
  }
1068
174k
}
1069
1070
txSlot* fxCanonicalizeKeyedCollectionKey(txSlot* key)
1071
643k
{
1072
643k
  if ((key->kind == XS_NUMBER_KIND) && (key->value.number == 0)) { \
1073
10
    key->kind = XS_INTEGER_KIND; \
1074
10
    key->value.integer = 0; \
1075
10
  }
1076
643k
  return key;
1077
643k
}
1078
1079
void fxClearEntries(txMachine* the, txSlot* table, txSlot* list, txBoolean paired)
1080
34
{
1081
34
  txSlot* slot = list->value.list.first;
1082
127
  while (slot) {
1083
93
    slot->flag = XS_DONT_ENUM_FLAG;
1084
93
    slot->kind = XS_UNDEFINED_KIND;
1085
93
    slot = slot->next;
1086
93
  }
1087
34
  c_memset(table->value.table.address, 0, table->value.table.length * sizeof(txSlot*));
1088
34
  list->next->value.integer = 0;
1089
34
  fxResizeEntries(the, table, list);
1090
34
  fxPurgeEntries(the, list);
1091
34
}
1092
1093
txBoolean fxDeleteEntry(txMachine* the, txSlot* table, txSlot* list, txSlot* key, txBoolean paired, txBoolean fit) 
1094
721k
{
1095
721k
  txSlot* info = list->next;
1096
721k
  txU4 sum = fxSumEntry(the, key);
1097
721k
  txU4 index = sum & (table->value.table.length - 1);
1098
721k
  txSlot** address = &(table->value.table.address[index]);
1099
721k
  txSlot* entry;
1100
721k
  txSlot* first;
1101
721k
  txSlot* last;
1102
801k
  while ((entry = *address)) {
1103
384k
    if (entry->value.entry.sum == sum) {
1104
352k
      first = entry->value.entry.slot;
1105
352k
      if (fxTestEntry(the, first, key)) {
1106
304k
        *address = entry->next;
1107
304k
        entry->next = C_NULL;
1108
304k
        first->flag = XS_DONT_ENUM_FLAG;
1109
304k
        first->kind = XS_UNDEFINED_KIND;
1110
304k
        if (paired) {
1111
77
          last = first->next;
1112
77
          last->flag = XS_DONT_ENUM_FLAG;
1113
77
          last->kind = XS_UNDEFINED_KIND;
1114
77
        }
1115
304k
        info->value.integer--;
1116
304k
        if (fit) {
1117
71.9k
          fxResizeEntries(the, table, list);
1118
71.9k
          fxPurgeEntries(the, list);
1119
71.9k
        }
1120
304k
        return 1;
1121
304k
      }
1122
352k
    }
1123
80.2k
    address = &entry->next;
1124
80.2k
  }
1125
416k
  return 0;
1126
721k
}
1127
1128
txSlot* fxGetEntry(txMachine* the, txSlot* table, txSlot* slot) 
1129
1.12M
{
1130
1.12M
  txU4 sum = fxSumEntry(the, slot);
1131
1.12M
  txU4 index = sum & (table->value.table.length - 1);
1132
1.12M
  txSlot* entry = table->value.table.address[index];
1133
1.12M
  txSlot* result;
1134
2.11M
  while (entry) {
1135
1.39M
    if (entry->value.entry.sum == sum) {
1136
895k
      result = entry->value.entry.slot;
1137
895k
      if (fxTestEntry(the, result, slot))
1138
399k
        return result;
1139
895k
    }
1140
997k
    entry = entry->next;
1141
997k
  }
1142
721k
  return C_NULL;
1143
1.12M
}
1144
1145
void fxPurgeEntries(txMachine* the, txSlot* list) 
1146
134k
{
1147
134k
  txSlot* former = C_NULL;
1148
134k
  txSlot** address = &(list->value.list.first);
1149
134k
  txSlot* slot;
1150
758k
  while ((slot = *address)) {
1151
624k
    if (slot->flag & XS_DONT_ENUM_FLAG) {
1152
304k
      *address = slot->next;
1153
304k
    }
1154
319k
    else {
1155
319k
      former = slot;
1156
319k
      address = &slot->next;
1157
319k
    }
1158
624k
  }
1159
134k
  list->value.list.last = former;
1160
134k
}
1161
1162
void fxResizeEntries(txMachine* the, txSlot* table, txSlot* list) 
1163
12.2M
{
1164
12.2M
  txSize size = list->next->value.integer;
1165
12.2M
  txSize formerLength = table->value.table.length;
1166
12.2M
  txSize currentLength = formerLength;
1167
12.2M
  txSize high = mxTableThreshold(formerLength);
1168
12.2M
  txSize low = mxTableThreshold(formerLength) >> 1;
1169
12.2M
  if (high < size) {
1170
4.18M
    currentLength = formerLength << 1;
1171
4.18M
    if (currentLength > mxTableMaxLength)
1172
0
      currentLength = mxTableMaxLength;
1173
4.18M
  }
1174
8.04M
  else if (low >= size) {
1175
124k
    currentLength = formerLength >> 1;
1176
124k
    if (currentLength < mxTableMinLength)
1177
21
      currentLength = mxTableMinLength;
1178
124k
  }
1179
12.2M
  if (formerLength != currentLength) {
1180
4.31M
    txSlot** currentAddress = (txSlot**)fxNewChunk(the, currentLength * sizeof(txSlot*));
1181
4.31M
    if (currentAddress) {
1182
4.31M
      txSlot** formerAddress = table->value.table.address;
1183
1184
4.31M
            txSize currentMask = currentLength - 1;
1185
4.31M
      c_memset(currentAddress, 0, currentLength * sizeof(txSlot*));
1186
26.2M
      while (formerLength) {
1187
21.9M
        txSlot* entry;
1188
40.7M
        while ((entry = *formerAddress)) {
1189
18.7M
          txU4 index = entry->value.entry.sum & currentMask;
1190
18.7M
           *formerAddress = entry->next;
1191
18.7M
          entry->next = currentAddress[index];
1192
18.7M
          currentAddress[index] = entry;
1193
18.7M
        }
1194
21.9M
        formerLength--;
1195
21.9M
        formerAddress++;
1196
21.9M
      }
1197
4.31M
      table->value.table.address = currentAddress;
1198
4.31M
      table->value.table.length = currentLength;
1199
4.31M
    }
1200
4.31M
  }
1201
//  {
1202
//    txSize holes = 0;
1203
//    txSize collisions = 0;
1204
//    txSize i =  0;
1205
//    while (i < table->value.table.length) {
1206
//      txSize j = 0;
1207
//      txSlot* entry = table->value.table.address[i];
1208
//      while (entry) {
1209
//        j++;
1210
//        entry = entry->next;
1211
//      }
1212
//      if (j == 0)
1213
//        holes++;
1214
//      else if (collisions < j)
1215
//        collisions = j;
1216
//      i++;
1217
//    }
1218
//    fprintf(stderr, "# size %d capacity %d holes %d collisions <%d\n", size, table->value.table.length, holes, collisions);
1219
//  }
1220
12.2M
}
1221
1222
void fxSetEntry(txMachine* the, txSlot* table, txSlot* list, txSlot* key, txSlot* pair, txBoolean get) 
1223
24.6M
{
1224
24.6M
  txU4 sum = fxSumEntry(the, key);
1225
24.6M
  txU4 index = sum & (table->value.table.length - 1);
1226
24.6M
  txSlot** address = &(table->value.table.address[index]);
1227
24.6M
  txSlot* entry = *address;
1228
24.6M
  txSlot* first;
1229
24.6M
  txSlot* last;
1230
31.9M
  while (entry) {
1231
19.8M
    if (entry->value.entry.sum == sum) {
1232
12.6M
      first = entry->value.entry.slot;
1233
12.6M
      if (fxTestEntry(the, first, key)) {
1234
12.5M
        if (pair) {
1235
2.56k
          last = first->next;
1236
2.56k
          if (get) {
1237
2.12k
            pair->kind = last->kind;
1238
2.12k
            pair->value = last->value;
1239
2.12k
          }
1240
445
          else {
1241
445
            last->kind = pair->kind;
1242
445
            last->value = pair->value;
1243
445
          }
1244
2.56k
        }
1245
12.5M
        return;
1246
12.5M
      }
1247
12.6M
    }
1248
7.27M
    entry = entry->next;
1249
7.27M
  }
1250
12.0M
  first = fxNewSlot(the);
1251
12.0M
  first->kind = key->kind;
1252
12.0M
  first->value = key->value;
1253
12.0M
  mxPushClosure(first);
1254
12.0M
  if (pair) {
1255
44.1k
    first->next = last = fxNewSlot(the);
1256
44.1k
    last->kind = pair->kind;
1257
44.1k
    last->value = pair->value;
1258
44.1k
    mxPushClosure(last);
1259
44.1k
  }
1260
12.0M
  entry = fxNewSlot(the);
1261
12.0M
  address = &(table->value.table.address[index]);
1262
12.0M
  entry->next = *address;
1263
12.0M
  entry->kind = XS_ENTRY_KIND;
1264
12.0M
  entry->value.entry.slot = first;
1265
12.0M
  entry->value.entry.sum = sum;
1266
12.0M
  *address = entry;
1267
12.0M
  if (list->value.list.last)
1268
11.0M
    list->value.list.last->next = first;
1269
1.05M
  else
1270
1.05M
    list->value.list.first = first;
1271
12.0M
  if (pair)
1272
44.1k
    list->value.list.last = last;
1273
12.0M
  else
1274
12.0M
    list->value.list.last = first;
1275
12.0M
  if (pair)
1276
44.1k
    mxPop();
1277
12.0M
  mxPop();
1278
12.0M
  list->next->value.integer++;
1279
12.0M
  fxResizeEntries(the, table, list);
1280
12.0M
}
1281
1282
txU4 fxSumEntry(txMachine* the, txSlot* slot) 
1283
26.4M
{
1284
26.4M
  txU1 kind = slot->kind;
1285
26.4M
  txU8 sum;
1286
  
1287
26.4M
  if ((XS_STRING_KIND == kind) || (XS_STRING_X_KIND == kind)) {
1288
    // Dan Bernstein: http://www.cse.yorku.ca/~oz/hash.html
1289
24.3M
    txU1 *string = (txU1*)slot->value.string;
1290
24.3M
    sum = 5381;
1291
69.2M
    while ((kind = c_read8(string++))) {
1292
44.9M
      sum = ((sum << 5) + sum) + kind;
1293
44.9M
    }
1294
24.3M
  }
1295
2.19M
  else {
1296
2.19M
    if (XS_REFERENCE_KIND == kind) {
1297
77.1k
      sum = (txU8)(((uintptr_t)slot->value.reference) / sizeof(txSlot));
1298
77.1k
    }
1299
2.11M
    else if (XS_INTEGER_KIND == kind) {
1300
5.27k
      fxToNumber(the, slot);
1301
5.27k
      sum = *((txU8*)&slot->value.number);
1302
5.27k
    }
1303
2.10M
    else if (XS_NUMBER_KIND == kind) {
1304
576k
      if (slot->value.number == 0) {
1305
85.6k
        sum = slot->value.number = 0;
1306
85.6k
      }
1307
491k
      else {
1308
491k
        if (c_isnan(slot->value.number)) {
1309
113k
        #if mxCanonicalNaN
1310
113k
          slot->value.number = *gxCanonicalNaN64;
1311
        #else       
1312
          slot->value.number = C_NAN;
1313
        #endif
1314
113k
        }
1315
491k
        sum = *((txU8*)&slot->value.number);
1316
491k
      }
1317
576k
    }
1318
1.53M
    else if ((XS_BIGINT_KIND == kind) || (XS_BIGINT_X_KIND == kind)) {
1319
190
      sum = gxTypeBigInt.toBigUint64(the, slot);
1320
190
    }
1321
1.53M
    else if (XS_SYMBOL_KIND == kind) {
1322
75
      sum = slot->value.symbol;
1323
75
    }
1324
1.53M
    else if (XS_BOOLEAN_KIND == kind) {
1325
173k
      sum = slot->value.boolean;
1326
173k
    }
1327
1.35M
    else {
1328
1.35M
      sum = 0;
1329
1.35M
    }
1330
    // Thomas Wang: http://web.archive.org/web/20071223173210/http://www.concentric.net/~Ttwang/tech/inthash.htm
1331
2.19M
    sum = (~sum) + (sum << 18); // sum = (sum << 18) - sum - 1;
1332
2.19M
    sum = sum ^ (sum >> 31);
1333
2.19M
    sum = sum * 21; // sum = (sum + (sum << 2)) + (sum << 4);
1334
2.19M
    sum = sum ^ (sum >> 11);
1335
2.19M
    sum = sum + (sum << 6);
1336
2.19M
    sum = sum ^ (sum >> 22);
1337
2.19M
  }
1338
26.4M
  sum &= 0xFFFFFFFF;
1339
26.4M
  return (txU4)sum;
1340
26.4M
}
1341
1342
txBoolean fxTestEntry(txMachine* the, txSlot* a, txSlot* b)
1343
13.9M
{ 
1344
13.9M
  txBoolean result = 0;
1345
13.9M
  if (a->kind == b->kind) {
1346
13.2M
    if ((XS_UNDEFINED_KIND == a->kind) || (XS_NULL_KIND == a->kind))
1347
394k
      result = 1;
1348
12.8M
    else if (XS_BOOLEAN_KIND == a->kind)
1349
108k
      result = a->value.boolean == b->value.boolean;
1350
12.7M
    else if (XS_INTEGER_KIND == a->kind)
1351
0
      result = a->value.integer == b->value.integer;
1352
12.7M
        else if (XS_NUMBER_KIND == a->kind)
1353
306k
      result = ((c_isnan(a->value.number) && c_isnan(b->value.number)) || (a->value.number == b->value.number));
1354
12.4M
    else if ((XS_STRING_KIND == a->kind) || (XS_STRING_X_KIND == a->kind))
1355
12.4M
      result = c_strcmp(a->value.string, b->value.string) == 0;
1356
1.61k
    else if (XS_SYMBOL_KIND == a->kind)
1357
30
      result = a->value.symbol == b->value.symbol;
1358
1.58k
    else if ((XS_BIGINT_KIND == a->kind) || (XS_BIGINT_X_KIND == a->kind))
1359
117
      result = gxTypeBigInt.compare(the, 0, 1, 0, a, b);
1360
1.46k
    else if (XS_REFERENCE_KIND == a->kind)
1361
1.46k
      result = a->value.reference == b->value.reference;
1362
13.2M
  }
1363
687k
  else if ((XS_INTEGER_KIND == a->kind) && (XS_NUMBER_KIND == b->kind))
1364
0
    result = (!c_isnan(b->value.number)) && ((txNumber)(a->value.integer) == b->value.number);
1365
687k
  else if ((XS_NUMBER_KIND == a->kind) && (XS_INTEGER_KIND == b->kind))
1366
0
    result = (!c_isnan(a->value.number)) && (a->value.number == (txNumber)(b->value.integer));
1367
687k
  else if ((XS_STRING_KIND == a->kind) && (XS_STRING_X_KIND == b->kind))
1368
0
    result = c_strcmp(a->value.string, b->value.string) == 0;
1369
687k
  else if ((XS_STRING_X_KIND == a->kind) && (XS_STRING_KIND == b->kind))
1370
0
    result = c_strcmp(a->value.string, b->value.string) == 0;
1371
687k
  else if ((XS_BIGINT_KIND == a->kind) && (XS_BIGINT_X_KIND == b->kind))
1372
0
    result = gxTypeBigInt.compare(the, 0, 1, 0, a, b);
1373
687k
  else if ((XS_BIGINT_X_KIND == a->kind) && (XS_BIGINT_KIND == b->kind))
1374
0
    result = gxTypeBigInt.compare(the, 0, 1, 0, a, b);
1375
13.9M
  return result;
1376
13.9M
}
1377
1378
txSlot* fxCanBeHeldWeakly(txMachine* the, txSlot* slot)
1379
1.97M
{
1380
1.97M
  if (slot->kind == XS_REFERENCE_KIND)
1381
1.84M
    slot = slot->value.reference;
1382
130k
#if mxKeysGarbageCollection
1383
130k
  else if (slot->kind == XS_SYMBOL_KIND) {
1384
123k
    slot = fxGetKey(the, slot->value.symbol);
1385
123k
    if (slot->kind == XS_REFERENCE_KIND)
1386
123k
      slot = slot->value.reference;
1387
20
    else
1388
20
      slot = C_NULL;
1389
123k
  }
1390
7.80k
#endif
1391
7.80k
  else
1392
7.80k
    slot = C_NULL;
1393
1.97M
  return slot;
1394
1.97M
}
1395
1396
txSlot* fxCheckWeakMapInstance(txMachine* the, txSlot* slot, txBoolean mutable)
1397
171k
{
1398
171k
  if (slot->kind == XS_REFERENCE_KIND) {
1399
169k
    txSlot* instance = slot->value.reference;
1400
169k
    if (((slot = instance->next)) && (slot->flag & XS_INTERNAL_FLAG) && (slot->kind == XS_WEAK_MAP_KIND) && (instance != mxWeakMapPrototype.value.reference)) {
1401
167k
      if (mutable && (slot->flag &  XS_DONT_SET_FLAG))
1402
0
        mxTypeError("this: read-only WeakMap instance");
1403
167k
      return instance;
1404
167k
    }
1405
169k
  }
1406
171k
  mxTypeError("this: not a WeakMap instance");
1407
0
  return C_NULL;
1408
171k
}
1409
1410
txSlot* fxCheckWeakMapKey(txMachine* the, txBoolean mutable)
1411
167k
{
1412
167k
  if (mxArgc > 0) {
1413
167k
    txSlot* slot = fxCanBeHeldWeakly(the, mxArgv(0));
1414
167k
    if (slot && mutable && (slot->flag & XS_MARK_FLAG))
1415
0
      mxTypeError("key: read-only");
1416
167k
    return slot;
1417
167k
  }
1418
8
  return C_NULL;
1419
167k
}
1420
1421
txSlot* fxNewWeakMapInstance(txMachine* the)
1422
213k
{
1423
213k
  txSlot* map;
1424
213k
  txSlot* list;
1425
213k
  map = fxNewSlot(the);
1426
213k
  map->kind = XS_INSTANCE_KIND;
1427
213k
  map->value.instance.garbage = C_NULL;
1428
213k
  map->value.instance.prototype = the->stack->value.reference;
1429
213k
  the->stack->kind = XS_REFERENCE_KIND;
1430
213k
  the->stack->value.reference = map;
1431
  /* LIST */
1432
213k
  list = map->next = fxNewSlot(the);
1433
213k
  list->flag = XS_INTERNAL_FLAG;
1434
213k
  list->kind = XS_WEAK_MAP_KIND;
1435
213k
  list->value.weakList.first = C_NULL;
1436
213k
  list->value.weakList.link = the->firstWeakListLink;
1437
213k
  the->firstWeakListLink = list;
1438
213k
  return map;
1439
213k
}
1440
1441
void fx_WeakMap(txMachine* the)
1442
214k
{
1443
214k
  txSlot *function, *iterable, *iterator, *next, *value;
1444
214k
  if (!mxHasTarget)
1445
1.29k
    mxTypeError("call: WeakMap");
1446
213k
  mxPushSlot(mxTarget);
1447
213k
  fxGetPrototypeFromConstructor(the, &mxWeakMapPrototype);
1448
213k
  fxNewWeakMapInstance(the);
1449
213k
  mxPullSlot(mxResult);
1450
213k
  if (mxArgc < 1)
1451
102k
    return;
1452
111k
  iterable = mxArgv(0);
1453
111k
  if ((iterable->kind == XS_UNDEFINED_KIND) || (iterable->kind == XS_NULL_KIND))
1454
3.71k
    return;
1455
107k
  mxPushSlot(mxResult);
1456
107k
  mxGetID(mxID(_set)); 
1457
107k
  function = the->stack;  
1458
107k
  if (!fxIsCallable(the, function))  
1459
3
    mxTypeError("result.set: not a function");
1460
107k
  mxTemporary(iterator);
1461
107k
  mxTemporary(next);
1462
107k
  fxGetIterator(the, iterable, iterator, next, 0);  
1463
107k
  mxTemporary(value);
1464
214k
  while (fxIteratorNext(the, iterator, next, value)) {
1465
107k
    mxTry(the) {
1466
107k
      mxPushSlot(mxResult);
1467
107k
      mxPushSlot(function);
1468
107k
      mxCall();
1469
107k
      if (value->kind != XS_REFERENCE_KIND)
1470
10
        mxTypeError("item: not an object");
1471
107k
      mxPushSlot(value);
1472
107k
      mxGetIndex(0);
1473
107k
      mxPushSlot(value);
1474
107k
      mxGetIndex(1);
1475
107k
      mxRunCount(2);
1476
107k
      mxPop();
1477
107k
    }
1478
107k
    mxCatch(the) {
1479
12
      fxIteratorReturn(the, iterator, 1);
1480
12
      fxJump(the);
1481
12
    }
1482
107k
  }
1483
107k
}
1484
1485
void fx_WeakMap_prototype_delete(txMachine* the)
1486
89
{
1487
89
  txSlot* instance = fxCheckWeakMapInstance(the, mxThis, XS_MUTABLE);
1488
89
  txSlot* key = fxCheckWeakMapKey(the, XS_MUTABLE);
1489
89
  mxResult->value.boolean = (key) ? fxDeleteWeakEntry(the, instance->next, key) : 0;
1490
89
  mxResult->kind = XS_BOOLEAN_KIND;
1491
89
}
1492
1493
void fx_WeakMap_prototype_get(txMachine* the)
1494
15.7k
{
1495
15.7k
  txSlot* instance = fxCheckWeakMapInstance(the, mxThis, XS_IMMUTABLE);
1496
15.7k
  txSlot* key = fxCheckWeakMapKey(the, XS_IMMUTABLE);
1497
15.7k
  txSlot* result = (key) ? fxGetWeakEntry(the, instance->next, key) : C_NULL;
1498
15.7k
  if (result) {
1499
5.24k
    txSlot* value = result->value.weakEntry.value;
1500
5.24k
    mxResult->kind = value->kind;
1501
5.24k
    mxResult->value = value->value;
1502
5.24k
  }
1503
15.7k
}
1504
1505
void fx_WeakMap_prototype_getOrInsert(txMachine* the)
1506
140
{
1507
140
  txSlot* instance = fxCheckWeakMapInstance(the, mxThis, XS_MUTABLE);
1508
140
  txSlot* key = fxCheckWeakMapKey(the, XS_MUTABLE);
1509
140
  if (!key)
1510
34
    mxTypeError("key: not an object");
1511
106
  *mxResult = (mxArgc > 1) ? *mxArgv(1) : mxUndefined;
1512
106
  fxSetWeakEntry(the, instance->next, key, mxResult, 1);
1513
106
}
1514
1515
void fx_WeakMap_prototype_getOrInsertComputed(txMachine* the)
1516
261
{
1517
261
  txSlot* instance = fxCheckWeakMapInstance(the, mxThis, XS_MUTABLE);
1518
261
  txSlot* key = fxCheckWeakMapKey(the, XS_MUTABLE);
1519
261
  txSlot* function;
1520
261
  txSlot* result;
1521
261
  if (!key)
1522
75
    mxTypeError("key: not an object");
1523
186
  function = fxArgToCallback(the, 1);
1524
186
  result = fxGetWeakEntry(the, instance->next, key);
1525
186
  if (result) {
1526
63
    txSlot* value = result->value.weakEntry.value;
1527
63
    mxResult->kind = value->kind;
1528
63
    mxResult->value = value->value;
1529
63
    return;
1530
63
  }
1531
123
  mxPushUndefined();
1532
123
  mxPushSlot(function);
1533
123
  mxCall();
1534
123
  if (key->next && (key->next->flag & XS_INTERNAL_FLAG) && (key->next->kind == XS_SYMBOL_KIND))
1535
12
    mxPushSlot(key->next);
1536
111
  else
1537
111
    mxPushReference(key);
1538
123
  mxRunCount(1);
1539
123
  mxPullSlot(mxResult);
1540
123
  fxSetWeakEntry(the, instance->next, key, mxResult, 0);
1541
123
}
1542
1543
void fx_WeakMap_prototype_has(txMachine* the)
1544
98
{
1545
98
  txSlot* instance = fxCheckWeakMapInstance(the, mxThis, XS_IMMUTABLE);
1546
98
  txSlot* key = fxCheckWeakMapKey(the, XS_IMMUTABLE);
1547
98
  txSlot* result = (key) ? fxGetWeakEntry(the, instance->next, key) : C_NULL;
1548
98
  mxResult->kind = XS_BOOLEAN_KIND;
1549
98
  mxResult->value.boolean = (result) ? 1 : 0;
1550
98
}
1551
1552
void fx_WeakMap_prototype_set(txMachine* the)
1553
154k
{
1554
154k
  txSlot* instance = fxCheckWeakMapInstance(the, mxThis, XS_MUTABLE);
1555
154k
  txSlot* key = fxCheckWeakMapKey(the, XS_MUTABLE);
1556
154k
  if (!key)
1557
14
    mxTypeError("key: not an object");
1558
154k
  fxSetWeakEntry(the, instance->next, key, (mxArgc > 1) ? mxArgv(1) : &mxUndefined, 0);
1559
154k
  *mxResult = *mxThis;
1560
154k
}
1561
1562
txSlot* fxCheckWeakSetInstance(txMachine* the, txSlot* slot, txBoolean mutable)
1563
1.58M
{
1564
1.58M
  if (slot->kind == XS_REFERENCE_KIND) {
1565
1.58M
    txSlot* instance = slot->value.reference;
1566
1.58M
    if (((slot = instance->next)) && (slot->flag & XS_INTERNAL_FLAG) && (slot->kind == XS_WEAK_SET_KIND) && (instance != mxWeakSetPrototype.value.reference)) {
1567
1.58M
      if (mutable && (slot->flag & XS_DONT_SET_FLAG))
1568
0
        mxTypeError("this: read-only WeakSet instance");
1569
1.58M
      return instance;
1570
1.58M
    }
1571
1.58M
  }
1572
1.58M
  mxTypeError("this: not a WeakSet instance");
1573
0
  return C_NULL;
1574
1.58M
}
1575
1576
txSlot* fxCheckWeakSetValue(txMachine* the, txBoolean mutable)
1577
1.58M
{
1578
1.58M
  if (mxArgc > 0) {
1579
1.58M
    txSlot* slot = fxCanBeHeldWeakly(the, mxArgv(0));
1580
1.58M
    if (slot && mutable && (slot->flag & XS_MARK_FLAG))
1581
0
      mxTypeError("value: read-only");
1582
1.58M
    return slot;
1583
1.58M
  }
1584
7
  return C_NULL;
1585
1.58M
}
1586
1587
txSlot* fxNewWeakSetInstance(txMachine* the)
1588
181k
{
1589
181k
  txSlot* set;
1590
181k
  txSlot* list;
1591
181k
  set = fxNewSlot(the);
1592
181k
  set->kind = XS_INSTANCE_KIND;
1593
181k
  set->value.instance.garbage = C_NULL;
1594
181k
  set->value.instance.prototype = the->stack->value.reference;
1595
181k
  the->stack->kind = XS_REFERENCE_KIND;
1596
181k
  the->stack->value.reference = set;
1597
  /* LIST */
1598
181k
  list = set->next = fxNewSlot(the);
1599
181k
  list->flag = XS_INTERNAL_FLAG;
1600
181k
  list->kind = XS_WEAK_SET_KIND;
1601
181k
  list->value.weakList.first = C_NULL;
1602
181k
  list->value.weakList.link = the->firstWeakListLink;
1603
181k
  the->firstWeakListLink = list;
1604
181k
  return set;
1605
181k
}
1606
1607
void fx_WeakSet(txMachine* the)
1608
182k
{
1609
182k
  txSlot *function, *iterable, *iterator, *next, *value;
1610
182k
  if (!mxHasTarget)
1611
985
    mxTypeError("call: WeakSet");
1612
181k
  mxPushSlot(mxTarget);
1613
181k
  fxGetPrototypeFromConstructor(the, &mxWeakSetPrototype);
1614
181k
  fxNewWeakSetInstance(the);
1615
181k
  mxPullSlot(mxResult);
1616
181k
  if (mxArgc < 1)
1617
181k
    return;
1618
44
  iterable = mxArgv(0);
1619
44
  if ((iterable->kind == XS_UNDEFINED_KIND) || (iterable->kind == XS_NULL_KIND))
1620
6
    return;
1621
38
  mxPushSlot(mxResult);
1622
38
  mxGetID(mxID(_add)); 
1623
38
  function = the->stack;  
1624
38
  if (!fxIsCallable(the, function))  
1625
1
    mxTypeError("result.add: not a function");
1626
37
  mxTemporary(iterator);
1627
37
  mxTemporary(next);
1628
37
  fxGetIterator(the, iterable, iterator, next, 0);  
1629
37
  mxTemporary(value);
1630
73
  while (fxIteratorNext(the, iterator, next, value)) {
1631
37
    mxTry(the) {
1632
37
      mxPushSlot(mxResult);
1633
37
      mxPushSlot(function);
1634
37
      mxCall();
1635
37
      mxPushSlot(value);
1636
37
      mxRunCount(1);
1637
37
      mxPop();
1638
37
    }
1639
37
    mxCatch(the) {
1640
1
      fxIteratorReturn(the, iterator, 1);
1641
1
      fxJump(the);
1642
1
    }
1643
37
  }
1644
37
}
1645
1646
void fx_WeakSet_prototype_add(txMachine* the)
1647
312k
{
1648
312k
  txSlot* instance = fxCheckWeakSetInstance(the, mxThis, XS_MUTABLE);
1649
312k
  txSlot* value = fxCheckWeakSetValue(the, XS_MUTABLE);
1650
312k
  if (!value)
1651
16
    mxTypeError("value: not an object");
1652
312k
  fxSetWeakEntry(the, instance->next, value, &mxUndefined, 0);
1653
312k
  *mxResult = *mxThis;
1654
312k
}
1655
1656
void fx_WeakSet_prototype_has(txMachine* the)
1657
284
{
1658
284
  txSlot* instance = fxCheckWeakSetInstance(the, mxThis, XS_IMMUTABLE);
1659
284
  txSlot* value = fxCheckWeakSetValue(the, XS_IMMUTABLE);
1660
284
  txSlot* result = (value) ? fxGetWeakEntry(the, instance->next, value) : C_NULL;
1661
284
  mxResult->kind = XS_BOOLEAN_KIND;
1662
284
  mxResult->value.boolean = (result) ? 1 : 0;
1663
284
}
1664
1665
void fx_WeakSet_prototype_delete(txMachine* the)
1666
1.27M
{
1667
1.27M
  txSlot* instance = fxCheckWeakSetInstance(the, mxThis, XS_MUTABLE);
1668
1.27M
  txSlot* value = fxCheckWeakSetValue(the, XS_MUTABLE);
1669
1.27M
  mxResult->value.boolean = (value) ? fxDeleteWeakEntry(the, instance->next, value) : 0;
1670
1.27M
  mxResult->kind = XS_BOOLEAN_KIND;
1671
1.27M
}
1672
1673
txBoolean fxDeleteWeakEntry(txMachine* the, txSlot* list, txSlot* key) 
1674
1.26M
{
1675
1.26M
  txSlot* slot = (key->flag & XS_EXOTIC_FLAG) ? key->next : key;
1676
1.26M
  txSlot** address = &slot->next;
1677
3.49M
  while ((slot = *address)) {
1678
3.10M
    if (!(slot->flag & XS_INTERNAL_FLAG))
1679
594k
      break;
1680
2.50M
    if ((slot->kind == XS_WEAK_ENTRY_KIND) && (slot->value.weakEntry.check == list)) {
1681
272k
      slot->value.weakEntry.value->kind = XS_UNINITIALIZED_KIND;
1682
272k
      *address = slot->next;
1683
272k
      return 1;
1684
272k
    }      
1685
2.23M
    address = &slot->next;
1686
2.23M
  }
1687
993k
  return 0;
1688
1.26M
}
1689
1690
txSlot* fxGetWeakEntry(txMachine* the, txSlot* list, txSlot* key) 
1691
15.6k
{
1692
15.6k
  txSlot* slot = (key->flag & XS_EXOTIC_FLAG) ? key->next : key;
1693
15.6k
    slot = slot->next;
1694
56.3k
  while (slot) {
1695
46.0k
    if (!(slot->flag & XS_INTERNAL_FLAG))
1696
49
      break;
1697
46.0k
    if ((slot->kind == XS_WEAK_ENTRY_KIND) && (slot->value.weakEntry.check == list))
1698
5.36k
      return slot;
1699
40.6k
    slot = slot->next;
1700
40.6k
  }
1701
10.2k
  return C_NULL;
1702
15.6k
}
1703
1704
void fxSetWeakEntry(txMachine* the, txSlot* list, txSlot* key, txSlot* value, txBoolean get) 
1705
464k
{
1706
464k
  txSlot* slot = (key->flag & XS_EXOTIC_FLAG) ? key->next : key;
1707
464k
  txSlot** address = &slot->next;
1708
464k
  txSlot* keyEntry;
1709
464k
  txSlot* listEntry;
1710
464k
  txSlot* closure;
1711
136M
  while ((slot = *address)) {
1712
136M
    if (!(slot->flag & XS_INTERNAL_FLAG))
1713
308k
      break;
1714
136M
    if ((slot->kind == XS_WEAK_ENTRY_KIND) && (slot->value.weakEntry.check == list)) {
1715
39.7k
      slot = slot->value.weakEntry.value;
1716
39.7k
      if (get) {
1717
51
        value->kind = slot->kind;
1718
51
        value->value = slot->value;
1719
51
      }
1720
39.7k
      else {
1721
39.7k
        slot->kind = value->kind;
1722
39.7k
        slot->value = value->value;
1723
39.7k
      }
1724
39.7k
      return;
1725
39.7k
    }      
1726
136M
    address = &slot->next;
1727
136M
  }
1728
  
1729
424k
  keyEntry = fxNewSlot(the);
1730
424k
  mxPushClosure(keyEntry);
1731
424k
  listEntry = fxNewSlot(the);
1732
424k
  mxPushClosure(listEntry);
1733
424k
  closure = fxNewSlot(the);
1734
424k
  closure->kind = value->kind;
1735
424k
  closure->value = value->value;
1736
  
1737
424k
  slot = (key->flag & XS_EXOTIC_FLAG) ? key->next : key;
1738
424k
  address = &slot->next;
1739
136M
  while ((slot = *address)) {
1740
136M
    if (!(slot->flag & XS_INTERNAL_FLAG))
1741
308k
      break;
1742
136M
    address = &slot->next;
1743
136M
  }
1744
  
1745
424k
  keyEntry->next = *address;
1746
424k
  keyEntry->flag = XS_INTERNAL_FLAG;
1747
424k
  keyEntry->kind = XS_WEAK_ENTRY_KIND;
1748
424k
  keyEntry->value.weakEntry.check = list;
1749
424k
  keyEntry->value.weakEntry.value = closure;
1750
424k
  *address = keyEntry;
1751
  
1752
424k
  listEntry->next = list->value.weakList.first;
1753
424k
  listEntry->flag = XS_INTERNAL_FLAG;
1754
424k
  listEntry->kind = XS_WEAK_ENTRY_KIND;
1755
424k
  listEntry->value.weakEntry.check = key;
1756
424k
  listEntry->value.weakEntry.value = closure;
1757
424k
  list->value.weakList.first = listEntry;
1758
  
1759
424k
  mxPop();
1760
424k
  mxPop();
1761
424k
}
1762
1763
void fxKeepDuringJobs(txMachine* the, txSlot* target)
1764
227
{
1765
227
  txSlot* instance = mxDuringJobs.value.reference;
1766
227
  txSlot** address = &(instance->next);
1767
227
  txSlot* slot;
1768
279
  while ((slot = *address)) {
1769
204
    if (slot->value.reference == target)
1770
152
      return;
1771
52
    address = &(slot->next);
1772
52
  }
1773
75
  *address = slot = fxNewSlot(the);
1774
75
  slot->value.reference = target;
1775
75
  slot->kind = XS_REFERENCE_KIND;
1776
75
}
1777
1778
txSlot* fxCheckWeakRefInstance(txMachine* the, txSlot* slot)
1779
61
{
1780
61
  if (slot->kind == XS_REFERENCE_KIND) {
1781
46
    txSlot* instance = slot->value.reference;
1782
46
    if (((slot = instance->next)) && (slot->flag & XS_INTERNAL_FLAG) && (slot->kind == XS_WEAK_REF_KIND))
1783
25
      return instance;
1784
46
  }
1785
61
  mxTypeError("this is no WeakRef instance");
1786
0
  return C_NULL;
1787
61
}
1788
1789
txSlot* fxNewWeakRefInstance(txMachine* the)
1790
202
{
1791
202
  txSlot* slot;
1792
202
  txSlot* instance = fxNewSlot(the);
1793
202
  instance->kind = XS_INSTANCE_KIND;
1794
202
  instance->value.instance.garbage = C_NULL;
1795
202
  instance->value.instance.prototype = the->stack->value.reference;
1796
202
  the->stack->kind = XS_REFERENCE_KIND;
1797
202
  the->stack->value.reference = instance;
1798
202
  slot = instance->next = fxNewSlot(the);
1799
202
  slot->flag = XS_INTERNAL_FLAG;
1800
202
  slot->kind = XS_WEAK_REF_KIND;
1801
202
  slot->value.weakRef.target = C_NULL;
1802
202
  slot->value.weakRef.link = C_NULL;
1803
202
  return instance;
1804
202
}
1805
1806
void fx_WeakRef(txMachine* the)
1807
231
{
1808
231
  txSlot* target;
1809
231
  txSlot* instance;
1810
231
  if (!mxHasTarget)
1811
3
    mxTypeError("call: WeakRef");
1812
228
  if (mxArgc < 1)
1813
6
    mxTypeError("no target");
1814
222
  target = fxCanBeHeldWeakly(the, mxArgv(0));
1815
222
  if (!target)
1816
20
    mxTypeError("target: not an object");
1817
202
  mxPushSlot(mxTarget);
1818
202
  fxGetPrototypeFromConstructor(the, &mxWeakRefPrototype);
1819
202
  instance = fxNewWeakRefInstance(the);
1820
202
  mxPullSlot(mxResult);
1821
202
  fxKeepDuringJobs(the, target);
1822
202
  instance->next->value.weakRef.target = target;
1823
202
}
1824
1825
void fx_WeakRef_prototype_deref(txMachine* the)
1826
61
{
1827
61
  txSlot* instance = fxCheckWeakRefInstance(the, mxThis);
1828
61
  txSlot* target = instance->next->value.weakRef.target;
1829
61
  if (target) {
1830
25
    txSlot* property = target->next;
1831
25
    if (property && (property->flag & XS_INTERNAL_FLAG) && (property->kind == XS_SYMBOL_KIND)) {
1832
6
      mxResult->value.symbol = property->value.symbol;
1833
6
      mxResult->kind = XS_SYMBOL_KIND;
1834
6
    }
1835
19
    else {
1836
19
      mxResult->value.reference = target;
1837
19
      mxResult->kind = XS_REFERENCE_KIND;
1838
19
    }
1839
25
    fxKeepDuringJobs(the, target);
1840
25
  }
1841
61
}
1842
1843
txSlot* fxCheckFinalizationRegistryInstance(txMachine* the, txSlot* slot)
1844
187k
{
1845
187k
  if (slot->kind == XS_REFERENCE_KIND) {
1846
187k
    txSlot* instance = slot->value.reference;
1847
187k
    if (((slot = instance->next)) && (slot->flag & XS_INTERNAL_FLAG) && (slot->kind == XS_CLOSURE_KIND) && (slot->value.closure->kind == XS_FINALIZATION_REGISTRY_KIND)) {
1848
187k
      if (slot->flag & XS_MARK_FLAG)
1849
0
        mxTypeError("this: read-only FinalizationRegistry instance");
1850
187k
      return instance;
1851
187k
    }
1852
187k
  }
1853
187k
  mxTypeError("this: not a FinalizationRegistry instance");
1854
0
  return C_NULL;
1855
187k
}
1856
1857
void fx_FinalizationRegistry(txMachine* the)
1858
115k
{
1859
115k
  txSlot* callback;
1860
115k
  txSlot* instance;
1861
115k
  txSlot* property;
1862
115k
  txSlot* registry;
1863
115k
  txSlot* slot;
1864
115k
  if (!mxHasTarget)
1865
3
    mxTypeError("call: FinalizationRegistry");
1866
115k
  if (mxArgc < 1)
1867
3
    mxTypeError("no callback");
1868
115k
  callback = mxArgv(0);
1869
115k
  if (!fxIsCallable(the, callback))
1870
23
    mxTypeError("callback: not a function");
1871
115k
  mxPushSlot(mxTarget);
1872
115k
  fxGetPrototypeFromConstructor(the, &mxFinalizationRegistryPrototype);
1873
115k
  instance = fxNewSlot(the);
1874
115k
  instance->kind = XS_INSTANCE_KIND;
1875
115k
  instance->value.instance.garbage = C_NULL;
1876
115k
  instance->value.instance.prototype = the->stack->value.reference;
1877
115k
  the->stack->kind = XS_REFERENCE_KIND;
1878
115k
  the->stack->value.reference = instance;
1879
115k
  mxPullSlot(mxResult);
1880
115k
  property = instance->next = fxNewSlot(the);
1881
115k
  property->flag = XS_INTERNAL_FLAG;
1882
115k
  property->kind = XS_CLOSURE_KIND;
1883
115k
  property->value.closure = C_NULL;
1884
115k
  registry = fxNewSlot(the);
1885
115k
  registry->kind = XS_FINALIZATION_REGISTRY_KIND;
1886
115k
  registry->value.finalizationRegistry.callback = C_NULL;
1887
115k
  registry->value.finalizationRegistry.flags = XS_NO_FLAG;
1888
115k
  property->value.closure = registry;
1889
115k
  slot = fxNewSlot(the);
1890
115k
  slot->kind = callback->kind;
1891
115k
  slot->value = callback->value;
1892
115k
  registry->value.finalizationRegistry.callback = slot;
1893
115k
}
1894
1895
1896
#if 0
1897
// removed from FinalizationRegistry specification
1898
void fx_FinalizationRegistry_prototype_cleanupSome(txMachine* the)
1899
{
1900
  txSlot* instance;
1901
  txSlot* registry;
1902
  txSlot* callback = C_NULL;
1903
  txSlot** address;
1904
  txSlot* slot;
1905
  if (!mxIsReference(mxThis))
1906
    mxTypeError("this: not an object");
1907
  instance = fxCheckFinalizationRegistryInstance(the, mxThis);
1908
  registry = instance->next->value.closure;
1909
  if (mxArgc > 0) {
1910
    callback = mxArgv(0);
1911
    if (mxIsUndefined(callback))
1912
      callback = C_NULL;
1913
    else if (!fxIsCallable(the, callback))
1914
      mxTypeError("callback: not a function");
1915
  }
1916
  fx_FinalizationRegistryCleanup(the, registry, callback);
1917
  callback = registry->value.finalizationRegistry.callback;
1918
  if (callback->next == C_NULL) {
1919
    address = &(mxFinalizationRegistries.value.reference->next);
1920
    while ((slot = *address)) {
1921
      if (slot->value.closure == registry) {
1922
        *address = slot->next;
1923
        return;
1924
      }
1925
      address = &(slot->next);
1926
    }
1927
  }
1928
}
1929
#endif
1930
1931
void fx_FinalizationRegistry_prototype_register(txMachine* the)
1932
182k
{
1933
182k
  txSlot* instance;
1934
182k
  txSlot* registry;
1935
182k
  txSlot* target;
1936
182k
  txSlot* token = C_NULL;
1937
182k
  txSlot* callback;
1938
182k
  txSlot** address;
1939
182k
  txSlot* slot;
1940
182k
  if (!mxIsReference(mxThis))
1941
42
    mxTypeError("this: not an object");
1942
182k
  instance = fxCheckFinalizationRegistryInstance(the, mxThis);
1943
182k
  registry = instance->next->value.closure;
1944
182k
  if (mxArgc < 1)
1945
1
    mxTypeError("no target");
1946
182k
  target = fxCanBeHeldWeakly(the, mxArgv(0));
1947
182k
  if (!target)
1948
17
    mxTypeError("target: not an object");
1949
182k
  if (mxArgc > 1) {
1950
182k
    if (fxIsSameValue(the, mxArgv(0), mxArgv(1), 1))
1951
6
      mxTypeError("target and holdings are the same");
1952
182k
  }
1953
182k
  if (mxArgc > 2) {
1954
30.4k
    token = mxArgv(2);
1955
30.4k
    if (mxIsUndefined(token))
1956
13
      token = C_NULL;
1957
30.4k
    else {
1958
30.4k
      token = fxCanBeHeldWeakly(the, token);
1959
30.4k
      if (!token)
1960
15
        mxTypeError("token: not an object");
1961
30.4k
    }
1962
30.4k
  }
1963
182k
  callback = registry->value.finalizationRegistry.callback;
1964
182k
  address = &(callback->next);
1965
244M
  while ((slot = *address))
1966
244M
    address = &(slot->next);
1967
182k
  slot = *address = fxNewSlot(the);
1968
182k
  if (mxArgc > 1) {
1969
182k
    slot->kind = mxArgv(1)->kind;
1970
182k
    slot->value = mxArgv(1)->value;
1971
182k
  }
1972
182k
  slot = slot->next = fxNewSlot(the);
1973
182k
  slot->kind = XS_FINALIZATION_CELL_KIND;
1974
182k
  slot->value.finalizationCell.target = target;
1975
182k
  slot->value.finalizationCell.token = token;
1976
  
1977
182k
  address = &(mxFinalizationRegistries.value.reference->next);
1978
163M
  while ((slot = *address)) {
1979
163M
    if (slot->value.closure == registry)
1980
98.4k
      return;
1981
163M
    address = &(slot->next);
1982
163M
  }
1983
83.9k
  slot = *address = fxNewSlot(the);
1984
83.9k
  slot->kind = XS_CLOSURE_KIND;
1985
83.9k
  slot->value.closure = registry;
1986
83.9k
}  
1987
1988
void fx_FinalizationRegistry_prototype_unregister(txMachine* the)
1989
4.88k
{
1990
4.88k
  txSlot* instance;
1991
4.88k
  txSlot* token;
1992
4.88k
  txSlot* registry;
1993
4.88k
  txSlot* callback;
1994
4.88k
  txSlot** address;
1995
4.88k
  txSlot* slot;
1996
4.88k
  if (!mxIsReference(mxThis))
1997
14
    mxTypeError("this: not an object");
1998
4.87k
  instance = fxCheckFinalizationRegistryInstance(the, mxThis);
1999
4.87k
  if (mxArgc < 1)
2000
0
    mxTypeError("no token");
2001
4.87k
  token = fxCanBeHeldWeakly(the, mxArgv(0));
2002
4.87k
  if (!token)
2003
21
    mxTypeError("token: not an object");
2004
4.85k
  mxResult->kind = XS_BOOLEAN_KIND;
2005
4.85k
  mxResult->value.boolean = 0;
2006
4.85k
  registry = instance->next->value.closure;
2007
4.85k
  callback = registry->value.finalizationRegistry.callback;
2008
4.85k
  address = &(callback->next);
2009
14.8k
  while ((slot = *address)) {
2010
9.99k
    slot = slot->next;
2011
9.99k
    if (slot->value.finalizationCell.token && fxIsSameInstance(the, slot->value.finalizationCell.token, token)) {
2012
45
      *address = slot->next;
2013
45
      mxResult->value.boolean = 1;
2014
45
    }
2015
9.94k
    else
2016
9.94k
      address = &(slot->next);
2017
9.99k
  }
2018
4.85k
  if (callback->next == C_NULL) {
2019
146
    address = &(mxFinalizationRegistries.value.reference->next);
2020
311
    while ((slot = *address)) {
2021
175
      if (slot->value.closure == registry) {
2022
10
        *address = slot->next;
2023
10
        return;
2024
10
      }
2025
165
      address = &(slot->next);
2026
165
    }
2027
146
  }
2028
4.85k
}
2029
2030
void fx_FinalizationRegistryCleanup(txMachine* the, txSlot* registry, txSlot* callback)
2031
77
{
2032
77
  txSlot* slot;
2033
77
  txUnsigned flags;
2034
77
  txSlot** address;
2035
77
  txSlot* value;
2036
2037
77
  if (!(registry->value.finalizationRegistry.flags & XS_FINALIZATION_REGISTRY_CHANGED))
2038
0
    return;
2039
    
2040
77
  slot = registry->value.finalizationRegistry.callback->next;
2041
77
  flags = 0;
2042
83
  while (slot) {
2043
83
    slot = slot->next;
2044
83
    if (slot->value.finalizationCell.target == C_NULL) {
2045
77
      flags = 1;
2046
77
      break;
2047
77
    }
2048
6
    slot = slot->next;
2049
6
  }
2050
77
  if (!flags)
2051
0
    return;
2052
77
  if (!callback)
2053
77
    callback = registry->value.finalizationRegistry.callback;
2054
77
  flags = registry->value.finalizationRegistry.flags;
2055
77
  {
2056
77
    mxTry(the) {
2057
77
      address = &(registry->value.finalizationRegistry.callback->next);
2058
30.3k
      while ((value = *address)) {
2059
30.2k
        slot = value->next;
2060
30.2k
        if (slot->value.finalizationCell.target == C_NULL) {
2061
5.11k
          *address = slot->next;
2062
5.11k
          mxPushUndefined();
2063
5.11k
          mxPushSlot(callback);
2064
5.11k
          mxCall();
2065
5.11k
          mxPushSlot(value);
2066
5.11k
          mxRunCount(1);
2067
5.11k
          mxPop();
2068
5.11k
        }
2069
25.1k
        else
2070
25.1k
          address = &(slot->next);
2071
30.2k
      }
2072
77
      registry->value.finalizationRegistry.flags = flags;
2073
77
    }
2074
77
    mxCatch(the) {
2075
0
      registry->value.finalizationRegistry.flags = flags;
2076
0
      fxJump(the);
2077
0
    }
2078
77
  }
2079
  
2080
77
  slot = registry->value.finalizationRegistry.callback->next;
2081
25.2k
  while (slot) {
2082
25.1k
    slot = slot->next;
2083
25.1k
    if (slot->value.finalizationCell.target == C_NULL)
2084
0
      break;
2085
25.1k
    slot = slot->next;
2086
25.1k
  }
2087
77
  if (!slot)
2088
77
    registry->value.finalizationRegistry.flags &= ~XS_FINALIZATION_REGISTRY_CHANGED;
2089
77
}
2090
2091
void fxCleanupFinalizationRegistries(txMachine* the)
2092
158k
{
2093
158k
  txSlot** address = &(mxFinalizationRegistries.value.reference->next);
2094
158k
  txSlot* closure;
2095
158k
  while ((closure = *address)) {
2096
360
    txSlot* registry = closure->value.closure;
2097
360
    if (registry->value.finalizationRegistry.flags & XS_FINALIZATION_REGISTRY_CHANGED) {
2098
77
      fx_FinalizationRegistryCleanup(the, registry, C_NULL);
2099
77
      address = &(mxFinalizationRegistries.value.reference->next);
2100
77
    }
2101
283
    else
2102
283
      address = &(closure->next);
2103
360
  }
2104
158k
}
2105
2106