@agoric/xsnap 0.14.3-u14.0 → 0.14.3-u16.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (137) hide show
  1. package/README.md +3 -3
  2. package/api.js +4 -2
  3. package/build.env +1 -1
  4. package/moddable/modules/data/base64/base64.js +28 -0
  5. package/moddable/modules/data/base64/manifest.json +11 -0
  6. package/moddable/modules/data/base64/modBase64.c +188 -0
  7. package/moddable/modules/data/binaryMessage/BinaryMessage.js +106 -0
  8. package/moddable/modules/data/crc/crc.c +205 -0
  9. package/moddable/modules/data/crc/crc.js +36 -0
  10. package/moddable/modules/data/crc/manifest.json +8 -0
  11. package/moddable/modules/data/hex/hex.js +28 -0
  12. package/moddable/modules/data/hex/manifest.json +11 -0
  13. package/moddable/modules/data/hex/modHex.c +139 -0
  14. package/moddable/modules/data/logical/logical.js +32 -0
  15. package/moddable/modules/data/logical/modLogical.c +98 -0
  16. package/moddable/modules/data/qrcode/manifest.json +9 -0
  17. package/moddable/modules/data/qrcode/qrcode.c +93 -0
  18. package/moddable/modules/data/qrcode/qrcode.js +23 -0
  19. package/moddable/modules/data/qrcode/qrcodegen.c +1025 -0
  20. package/moddable/modules/data/qrcode/qrcodegen.h +267 -0
  21. package/moddable/modules/data/text/decoder/manifest.json +8 -0
  22. package/moddable/modules/data/text/decoder/textdecoder.c +480 -0
  23. package/moddable/modules/data/text/decoder/textdecoder.js +27 -0
  24. package/moddable/modules/data/text/encoder/manifest.json +8 -0
  25. package/moddable/modules/data/text/encoder/textencoder.c +232 -0
  26. package/moddable/modules/data/text/encoder/textencoder.js +24 -0
  27. package/moddable/modules/data/tinyint/tinyint.c +150 -0
  28. package/moddable/modules/data/tinyint/tinyint.js +53 -0
  29. package/moddable/modules/data/url/manifest.json +17 -0
  30. package/moddable/modules/data/url/url.c +1959 -0
  31. package/moddable/modules/data/url/url.js +210 -0
  32. package/moddable/modules/data/wavreader/manifest.json +8 -0
  33. package/moddable/modules/data/wavreader/wavreader.js +128 -0
  34. package/moddable/modules/data/zlib/deflate.c +161 -0
  35. package/moddable/modules/data/zlib/deflate.js +63 -0
  36. package/moddable/modules/data/zlib/inflate.c +145 -0
  37. package/moddable/modules/data/zlib/inflate.js +66 -0
  38. package/moddable/modules/data/zlib/manifest_deflate.json +9 -0
  39. package/moddable/modules/data/zlib/manifest_inflate.json +9 -0
  40. package/moddable/modules/data/zlib/miniz.c +4924 -0
  41. package/moddable/xs/includes/xs.d.ts +73 -0
  42. package/moddable/xs/includes/xs.h +1533 -0
  43. package/moddable/xs/includes/xsmc.h +206 -0
  44. package/moddable/xs/makefiles/lin/makefile +33 -0
  45. package/moddable/xs/makefiles/lin/xsc.mk +118 -0
  46. package/moddable/xs/makefiles/lin/xsid.mk +90 -0
  47. package/moddable/xs/makefiles/lin/xsl.mk +168 -0
  48. package/moddable/xs/makefiles/lin/xst.mk +201 -0
  49. package/moddable/xs/makefiles/mac/makefile +33 -0
  50. package/moddable/xs/makefiles/mac/xsc.mk +130 -0
  51. package/moddable/xs/makefiles/mac/xsid.mk +102 -0
  52. package/moddable/xs/makefiles/mac/xsl.mk +177 -0
  53. package/moddable/xs/makefiles/mac/xst.mk +203 -0
  54. package/moddable/xs/makefiles/mac/xst_no_asan.txt +52 -0
  55. package/moddable/xs/makefiles/win/build.bat +26 -0
  56. package/moddable/xs/makefiles/win/xsc.mak +142 -0
  57. package/moddable/xs/makefiles/win/xsid.mak +113 -0
  58. package/moddable/xs/makefiles/win/xsl.mak +186 -0
  59. package/moddable/xs/makefiles/win/xst.mak +195 -0
  60. package/moddable/xs/platforms/lin_xs.h +99 -0
  61. package/moddable/xs/platforms/mac_xs.h +97 -0
  62. package/moddable/xs/platforms/wasm_xs.h +79 -0
  63. package/moddable/xs/platforms/win_xs.h +104 -0
  64. package/moddable/xs/platforms/xsHost.h +63 -0
  65. package/moddable/xs/platforms/xsPlatform.h +618 -0
  66. package/moddable/xs/sources/xsAPI.c +2555 -0
  67. package/moddable/xs/sources/xsAll.c +294 -0
  68. package/moddable/xs/sources/xsAll.h +2741 -0
  69. package/moddable/xs/sources/xsArguments.c +222 -0
  70. package/moddable/xs/sources/xsArray.c +2657 -0
  71. package/moddable/xs/sources/xsAtomics.c +844 -0
  72. package/moddable/xs/sources/xsBigInt.c +1859 -0
  73. package/moddable/xs/sources/xsBoolean.c +109 -0
  74. package/moddable/xs/sources/xsCode.c +4493 -0
  75. package/moddable/xs/sources/xsCommon.c +1710 -0
  76. package/moddable/xs/sources/xsCommon.h +1142 -0
  77. package/moddable/xs/sources/xsDataView.c +2890 -0
  78. package/moddable/xs/sources/xsDate.c +1541 -0
  79. package/moddable/xs/sources/xsDebug.c +2710 -0
  80. package/moddable/xs/sources/xsDefaults.c +134 -0
  81. package/moddable/xs/sources/xsError.c +353 -0
  82. package/moddable/xs/sources/xsFunction.c +776 -0
  83. package/moddable/xs/sources/xsGenerator.c +865 -0
  84. package/moddable/xs/sources/xsGlobal.c +839 -0
  85. package/moddable/xs/sources/xsJSON.c +1091 -0
  86. package/moddable/xs/sources/xsLexical.c +1969 -0
  87. package/moddable/xs/sources/xsLockdown.c +933 -0
  88. package/moddable/xs/sources/xsMapSet.c +1649 -0
  89. package/moddable/xs/sources/xsMarshall.c +1020 -0
  90. package/moddable/xs/sources/xsMath.c +624 -0
  91. package/moddable/xs/sources/xsMemory.c +1941 -0
  92. package/moddable/xs/sources/xsModule.c +3101 -0
  93. package/moddable/xs/sources/xsNumber.c +560 -0
  94. package/moddable/xs/sources/xsObject.c +1102 -0
  95. package/moddable/xs/sources/xsPlatforms.c +480 -0
  96. package/moddable/xs/sources/xsProfile.c +577 -0
  97. package/moddable/xs/sources/xsPromise.c +1199 -0
  98. package/moddable/xs/sources/xsProperty.c +636 -0
  99. package/moddable/xs/sources/xsProxy.c +1014 -0
  100. package/moddable/xs/sources/xsRegExp.c +1168 -0
  101. package/moddable/xs/sources/xsRun.c +4889 -0
  102. package/moddable/xs/sources/xsScope.c +1293 -0
  103. package/moddable/xs/sources/xsScript.c +288 -0
  104. package/moddable/xs/sources/xsScript.h +1186 -0
  105. package/moddable/xs/sources/xsSnapshot.c +2161 -0
  106. package/moddable/xs/sources/xsSnapshot.h +51 -0
  107. package/moddable/xs/sources/xsSourceMap.c +218 -0
  108. package/moddable/xs/sources/xsString.c +3332 -0
  109. package/moddable/xs/sources/xsSymbol.c +503 -0
  110. package/moddable/xs/sources/xsSyntaxical.c +4193 -0
  111. package/moddable/xs/sources/xsTree.c +1893 -0
  112. package/moddable/xs/sources/xsType.c +1488 -0
  113. package/moddable/xs/sources/xsdtoa.c +6672 -0
  114. package/moddable/xs/sources/xsmc.c +340 -0
  115. package/moddable/xs/sources/xsre.c +7578 -0
  116. package/package.json +37 -20
  117. package/scripts/get_xsnap_version.sh +14 -0
  118. package/scripts/test-package.sh +21 -0
  119. package/src/avaAssertXS.js +6 -2
  120. package/src/avaHandler.cjs +2 -5
  121. package/src/avaXS.js +7 -8
  122. package/src/build.js +161 -28
  123. package/src/replay.js +0 -3
  124. package/src/xsnap.js +105 -91
  125. package/src/xsrepl.js +2 -3
  126. package/xsnap-native/xsnap/makefiles/lin/makefile +10 -0
  127. package/xsnap-native/xsnap/makefiles/lin/xsnap-worker.mk +156 -0
  128. package/xsnap-native/xsnap/makefiles/lin/xsnap.mk +144 -0
  129. package/xsnap-native/xsnap/makefiles/mac/makefile +10 -0
  130. package/xsnap-native/xsnap/makefiles/mac/xsnap-worker.mk +165 -0
  131. package/xsnap-native/xsnap/makefiles/mac/xsnap.mk +153 -0
  132. package/xsnap-native/xsnap/sources/xsnap-worker.c +1008 -0
  133. package/xsnap-native/xsnap/sources/xsnap.c +717 -0
  134. package/xsnap-native/xsnap/sources/xsnap.h +142 -0
  135. package/xsnap-native/xsnap/sources/xsnapPlatform.c +1501 -0
  136. package/xsnap-native/xsnap/sources/xsnapPlatform.h +105 -0
  137. package/CHANGELOG.md +0 -654
@@ -0,0 +1,3101 @@
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
+ #ifndef mxReport
41
+ #define mxReport 0
42
+ #endif
43
+
44
+ static void fxCompleteModule(txMachine* the, txSlot* module, txSlot* exception);
45
+ static void fxDuplicateModuleTransfers(txMachine* the, txSlot* srcModule, txSlot* dstModule);
46
+
47
+ static void fxExecuteModules(txMachine* the, txSlot* queue);
48
+ static txID fxExecuteModulesFrom(txMachine* the, txSlot* queue, txSlot* module, txSlot** exception);
49
+
50
+ static txSlot* fxGetModule(txMachine* the, txSlot* realm, txID moduleID);
51
+
52
+ static void fxLinkCircularities(txMachine* the, txSlot* module, txSlot* circularities, txSlot* exports);
53
+ static void fxLinkExports(txMachine* the, txSlot* module);
54
+ static void fxLinkLocals(txMachine* the, txSlot* module);
55
+ static void fxLinkModules(txMachine* the, txSlot* queue);
56
+ static void fxLinkNamespace(txMachine* the, txSlot* module, txSlot* transfer);
57
+ static void fxLinkTransfer(txMachine* the, txSlot* module, txID importID, txSlot* transfer);
58
+
59
+ static void fxLoadModules(txMachine* the, txSlot* queue);
60
+ static void fxLoadModulesFrom(txMachine* the, txSlot* queue, txSlot* module, txBoolean now);
61
+
62
+ static txBoolean fxMapModule(txMachine* the, txSlot* realm, txID moduleID, txSlot* module, txSlot* queue, txSlot* result);
63
+ static void fxMapModuleDescriptor(txMachine* the, txSlot* realm, txID moduleID, txSlot* module, txSlot* queue, txSlot* result, txSlot* descriptor);
64
+ static void fxNewModule(txMachine* the, txSlot* realm, txID moduleID, txSlot* module);
65
+ static void fxOrderModule(txMachine* the, txSlot* queue, txSlot* order, txSlot* module);
66
+ static void fxOverrideModule(txMachine* the, txSlot* queue, txSlot* result, txSlot* module, txSlot* record);
67
+ static txBoolean fxQueueModule(txMachine* the, txSlot* queue, txSlot* module);
68
+
69
+ static txID fxResolveSpecifier(txMachine* the, txSlot* realm, txID moduleID, txSlot* name);
70
+
71
+ static void fxRunImportFulfilled(txMachine* the, txSlot* module, txSlot* with);
72
+ static void fxRunImportRejected(txMachine* the, txSlot* module, txSlot* with);
73
+
74
+ static void fxRunImportNow(txMachine* the, txSlot* realm, txID id);
75
+
76
+ static txBoolean fxModuleDefineOwnProperty(txMachine* the, txSlot* instance, txID id, txIndex index, txSlot* slot, txFlag mask);
77
+ static txBoolean fxModuleDeleteProperty(txMachine* the, txSlot* instance, txID id, txIndex index);
78
+ static txSlot* fxModuleFindProperty(txMachine* the, txSlot* instance, txID id, txIndex index);
79
+ static txBoolean fxModuleGetOwnProperty(txMachine* the, txSlot* instance, txID id, txIndex index, txSlot* slot);
80
+ static txSlot* fxModuleGetProperty(txMachine* the, txSlot* instance, txID id, txIndex index, txFlag flag);
81
+ static txBoolean fxModuleGetPropertyValue(txMachine* the, txSlot* instance, txID id, txIndex index, txSlot* receiver, txSlot* value);
82
+ static txBoolean fxModuleGetPrototype(txMachine* the, txSlot* instance, txSlot* result);
83
+ static txBoolean fxModuleHasProperty(txMachine* the, txSlot* instance, txID id, txIndex index);
84
+ static txBoolean fxModuleIsExtensible(txMachine* the, txSlot* instance);
85
+ static void fxModuleOwnKeys(txMachine* the, txSlot* instance, txFlag flag, txSlot* keys);
86
+ static int fxModuleOwnKeysCompare(const void* p, const void* q);
87
+ static txBoolean fxModulePreventExtensions(txMachine* the, txSlot* instance);
88
+ static txSlot* fxModuleSetProperty(txMachine* the, txSlot* instance, txID id, txIndex index, txFlag flag);
89
+ static txBoolean fxModuleSetPropertyValue(txMachine* the, txSlot* instance, txID id, txIndex index, txSlot* value, txSlot* receiver);
90
+ static txBoolean fxModuleSetPrototype(txMachine* the, txSlot* instance, txSlot* prototype);
91
+
92
+ static txSlot* fxCheckCompartmentInstance(txMachine* the, txSlot* slot);
93
+ static void fxPrepareCompartmentFunction(txMachine* the, txSlot* program, txSlot* instance);
94
+
95
+ static txSlot* fxCheckModuleSourceInstance(txMachine* the, txSlot* slot);
96
+ static txSlot* fxNewModuleSourceInstance(txMachine* the);
97
+
98
+ const txBehavior ICACHE_FLASH_ATTR gxModuleBehavior = {
99
+ fxModuleGetProperty,
100
+ fxModuleSetProperty,
101
+ fxOrdinaryCall,
102
+ fxOrdinaryConstruct,
103
+ fxModuleDefineOwnProperty,
104
+ fxModuleDeleteProperty,
105
+ fxModuleGetOwnProperty,
106
+ fxModuleGetPropertyValue,
107
+ fxModuleGetPrototype,
108
+ fxModuleHasProperty,
109
+ fxModuleIsExtensible,
110
+ fxModuleOwnKeys,
111
+ fxModulePreventExtensions,
112
+ fxModuleSetPropertyValue,
113
+ fxModuleSetPrototype,
114
+ };
115
+
116
+ enum {
117
+ XS_MODULE_STATUS_NEW = 0,
118
+ XS_MODULE_STATUS_LOADING,
119
+ XS_MODULE_STATUS_LOADED,
120
+ XS_MODULE_STATUS_LINKING,
121
+ XS_MODULE_STATUS_LINKED,
122
+ XS_MODULE_STATUS_EXECUTING,
123
+ XS_MODULE_STATUS_EXECUTED,
124
+ XS_MODULE_STATUS_ERROR,
125
+ };
126
+
127
+
128
+ #define mxIsModule(THE_SLOT) \
129
+ (((THE_SLOT)->next) && ((THE_SLOT)->next->flag & XS_INTERNAL_FLAG) && ((THE_SLOT)->next->kind == XS_MODULE_KIND))
130
+ #define mxIsModuleSource(THE_SLOT) \
131
+ (((THE_SLOT)->next) && ((THE_SLOT)->next->flag & XS_INTERNAL_FLAG) && ((THE_SLOT)->next->kind == XS_MODULE_SOURCE_KIND))
132
+
133
+ #define mxModuleInstanceStatus(MODULE) ((MODULE)->next->next->ID)
134
+ #define mxModuleStatus(MODULE) mxModuleInstanceStatus((MODULE)->value.reference)
135
+
136
+ #if mxReport
137
+ #define mxModuleName(ID) c_strrchr(fxGetKeyName(the, (ID)), '/')
138
+ #define mxReportModuleQueue(LABEL) fxReportModuleQueue(the, queue, LABEL)
139
+ static void fxReportModuleQueue(txMachine* the, txSlot* queue, txString label)
140
+ {
141
+ txSlot* module = queue->next;
142
+ fprintf(stderr, "%s %p", label, queue);
143
+ while (module) {
144
+ fprintf(stderr, " '%s' %p %d", fxGetKeyName(the, module->ID), module->value.reference, mxModuleStatus(module));
145
+ module = module->next;
146
+ }
147
+ fprintf(stderr, "\n");
148
+ }
149
+ #else
150
+ #define mxReportModuleQueue(LABEL)
151
+ #endif
152
+
153
+ static void fxPushKeyString(txMachine* the, txID id)
154
+ {
155
+ txSlot* key = fxGetKey(the, id);
156
+ if (key->kind == XS_KEY_X_KIND)
157
+ mxPushStringX(key->value.key.string);
158
+ else
159
+ mxPushString(key->value.key.string);
160
+ }
161
+
162
+ void fxBuildModule(txMachine* the)
163
+ {
164
+ txSlot* slot;
165
+
166
+ fxNewHostFunction(the, mxCallback(fxModuleGetter), 0, XS_NO_ID, XS_NO_ID);
167
+ mxPushUndefined();
168
+ the->stack->flag = XS_DONT_DELETE_FLAG;
169
+ the->stack->kind = XS_ACCESSOR_KIND;
170
+ the->stack->value.accessor.getter = (the->stack + 1)->value.reference;
171
+ the->stack->value.accessor.setter = C_NULL;
172
+ mxPull(mxModuleAccessor);
173
+ the->stack += 1;
174
+
175
+ mxPush(mxObjectPrototype);
176
+ slot = fxLastProperty(the, fxNewObjectInstance(the));
177
+ slot = fxNextStringXProperty(the, slot, "Module", mxID(_Symbol_toStringTag), XS_GET_ONLY);
178
+ mxModulePrototype = *the->stack;
179
+ mxPop();
180
+
181
+ mxPush(mxObjectPrototype);
182
+ fxNewObjectInstance(the);
183
+ mxTransferPrototype = *the->stack;
184
+ mxPop();
185
+
186
+ mxPush(mxObjectPrototype);
187
+ slot = fxLastProperty(the, fxNewObjectInstance(the));
188
+ slot = fxNextHostAccessorProperty(the, slot, mxCallback(fx_Compartment_prototype_get_globalThis), C_NULL, mxID(_globalThis), XS_DONT_ENUM_FLAG);
189
+ slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Compartment_prototype_evaluate), 1, mxID(_evaluate), XS_DONT_ENUM_FLAG);
190
+ slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Compartment_prototype_import), 1, mxID(_import), XS_DONT_ENUM_FLAG);
191
+ slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Compartment_prototype_importNow), 1, mxID(_importNow), XS_DONT_ENUM_FLAG);
192
+ // slot = fxNextHostFunctionProperty(the, slot, mxCallback(fx_Compartment_prototype_module), 1, mxID(_module), XS_DONT_ENUM_FLAG);
193
+ slot = fxNextStringXProperty(the, slot, "Compartment", mxID(_Symbol_toStringTag), XS_DONT_ENUM_FLAG | XS_DONT_SET_FLAG);
194
+ mxCompartmentPrototype = *the->stack;
195
+ slot = fxBuildHostConstructor(the, mxCallback(fx_Compartment), 1, mxID(_Compartment));
196
+ mxCompartmentConstructor = *the->stack;
197
+ mxPop();
198
+
199
+ mxPush(mxObjectPrototype);
200
+ slot = fxLastProperty(the, fxNewObjectInstance(the));
201
+ slot = fxNextHostAccessorProperty(the, slot, mxCallback(fx_ModuleSource_prototype_get_bindings), C_NULL, mxID(_bindings), XS_DONT_ENUM_FLAG);
202
+ slot = fxNextHostAccessorProperty(the, slot, mxCallback(fx_ModuleSource_prototype_get_needsImport), C_NULL, mxID(_needsImport), XS_DONT_ENUM_FLAG);
203
+ slot = fxNextHostAccessorProperty(the, slot, mxCallback(fx_ModuleSource_prototype_get_needsImportMeta), C_NULL, mxID(_needsImportMeta), XS_DONT_ENUM_FLAG);
204
+ slot = fxNextStringXProperty(the, slot, "ModuleSource", mxID(_Symbol_toStringTag), XS_DONT_ENUM_FLAG | XS_DONT_SET_FLAG);
205
+ mxModuleSourcePrototype = *the->stack;
206
+ slot = fxBuildHostConstructor(the, mxCallback(fx_ModuleSource), 1, mxID(_ModuleSource));
207
+ mxModuleSourceConstructor = *the->stack;
208
+ mxPop();
209
+ }
210
+
211
+ void fxCompleteModule(txMachine* the, txSlot* module, txSlot* exception)
212
+ {
213
+ if (exception) {
214
+ txSlot* exports = mxModuleInstanceExports(module);
215
+ txSlot* meta = mxModuleInstanceMeta(module);
216
+ if (exports->kind == XS_REFERENCE_KIND) {
217
+ exports->value.reference->next = C_NULL;
218
+ }
219
+ else {
220
+ fxNewInstance(the);
221
+ mxPullSlot(exports);
222
+ }
223
+ meta->value = exception->value;
224
+ meta->kind = exception->kind;
225
+ mxModuleInstanceStatus(module) = XS_MODULE_STATUS_ERROR;
226
+ }
227
+ else {
228
+ mxModuleInstanceStatus(module) = XS_MODULE_STATUS_EXECUTED;
229
+ }
230
+ }
231
+
232
+ void fxDuplicateModuleTransfers(txMachine* the, txSlot* srcModule, txSlot* dstModule)
233
+ {
234
+ txSlot* srcSlot;
235
+ txSlot* dstSlot;
236
+ txSlot* transfer;
237
+ txSlot* aliases;
238
+ txSlot* function;
239
+ srcSlot = mxModuleInternal(srcModule);
240
+ dstSlot = mxModuleInternal(dstModule);
241
+ dstSlot->flag |= (srcSlot->flag & (XS_IMPORT_FLAG | XS_IMPORT_META_FLAG));
242
+ srcSlot = mxModuleTransfers(srcModule);
243
+ dstSlot = mxModuleTransfers(dstModule);
244
+ fxDuplicateInstance(the, srcSlot->value.reference);
245
+ mxPullSlot(dstSlot);
246
+ transfer = dstSlot->value.reference->next;
247
+ while (transfer) {
248
+ fxDuplicateInstance(the, transfer->value.reference);
249
+ mxPullSlot(transfer);
250
+ aliases = mxTransferAliases(transfer);
251
+ if (aliases->kind == XS_REFERENCE_KIND) {
252
+ fxDuplicateInstance(the, aliases->value.reference);
253
+ mxPullSlot(aliases);
254
+ }
255
+ transfer = transfer->next;
256
+ }
257
+ srcSlot = mxModuleInitialize(srcModule);
258
+ dstSlot = mxModuleInitialize(dstModule);
259
+ if (srcSlot->kind == XS_REFERENCE_KIND) {
260
+ function = fxDuplicateInstance(the, srcSlot->value.reference);
261
+ mxPullSlot(dstSlot);
262
+ mxFunctionInstanceHome(function)->value.home.module = dstModule->value.reference;
263
+ }
264
+ else {
265
+ dstSlot->kind = srcSlot->kind;
266
+ dstSlot->value = srcSlot->value;
267
+ }
268
+ srcSlot = mxModuleExecute(srcModule);
269
+ dstSlot = mxModuleExecute(dstModule);
270
+ function = fxDuplicateInstance(the, srcSlot->value.reference);
271
+ mxPullSlot(dstSlot);
272
+ mxFunctionInstanceHome(function)->value.home.module = dstModule->value.reference;
273
+ }
274
+
275
+ void fxExecuteModules(txMachine* the, txSlot* queue)
276
+ {
277
+ txBoolean done = 1;
278
+ txSlot* module = queue->next;
279
+ mxReportModuleQueue("INIT");
280
+ while (module) {
281
+ txID status = mxModuleStatus(module);
282
+ if (status == XS_MODULE_STATUS_LINKED) {
283
+ txSlot* exception;
284
+ txID fromStatus = fxExecuteModulesFrom(the, queue, module, &exception);
285
+ if (fromStatus == XS_MODULE_STATUS_ERROR) {
286
+ fxCompleteModule(the, module->value.reference, exception);
287
+ fxRunImportRejected(the, module->value.reference, module->value.reference);
288
+ }
289
+ else if (fromStatus == XS_MODULE_STATUS_LINKED) {
290
+ mxModuleStatus(module) = XS_MODULE_STATUS_EXECUTING;
291
+ mxPushSlot(mxModuleHosts(module));
292
+ mxPull(mxHosts);
293
+ {
294
+ mxTry(the) {
295
+ txSlot* result;
296
+ mxPushUndefined();
297
+ mxPushSlot(mxModuleExecute(module));
298
+ mxCall();
299
+ mxRunCount(0);
300
+ result = the->stack;
301
+ if (mxIsReference(result) && mxIsPromise(result->value.reference)) {
302
+ txSlot* function;
303
+ txSlot* home;
304
+ txSlot* resolveExecuteFunction;
305
+ txSlot* rejectExecuteFunction;
306
+
307
+ function = fxNewHostFunction(the, fxExecuteModulesFulfilled, 1, XS_NO_ID, mxExecuteModulesFulfilledProfileID);
308
+ home = mxFunctionInstanceHome(function);
309
+ home->value.home.object = queue;
310
+ home->value.home.module = module->value.reference;
311
+ resolveExecuteFunction = the->stack;
312
+
313
+ function = fxNewHostFunction(the, fxExecuteModulesRejected, 1, XS_NO_ID, mxExecuteModulesRejectedProfileID);
314
+ home = mxFunctionInstanceHome(function);
315
+ home->value.home.object = queue;
316
+ home->value.home.module = module->value.reference;
317
+ rejectExecuteFunction = the->stack;
318
+
319
+ fxPromiseThen(the, result->value.reference, resolveExecuteFunction, rejectExecuteFunction, C_NULL, C_NULL);
320
+
321
+ mxPop();
322
+ mxPop();
323
+ mxPop();
324
+ done = 0;
325
+ }
326
+ else {
327
+ mxPop();
328
+ fxCompleteModule(the, module->value.reference, C_NULL);
329
+ fxRunImportFulfilled(the, module->value.reference, module->value.reference);
330
+ }
331
+ }
332
+ mxCatch(the) {
333
+ mxPush(mxException);
334
+ mxException = mxUndefined;
335
+ fxCompleteModule(the, module->value.reference, the->stack);
336
+ fxRunImportRejected(the, module->value.reference, module->value.reference);
337
+ mxPop();
338
+ }
339
+ }
340
+ mxPushUndefined();
341
+ mxPull(mxHosts);
342
+ }
343
+ else
344
+ done = 0;
345
+ }
346
+ else if (status < XS_MODULE_STATUS_EXECUTED)
347
+ done = 0;
348
+ module = module->next;
349
+ }
350
+ if (done) {
351
+ queue->next = C_NULL;
352
+ mxReportModuleQueue("DONE");
353
+ }
354
+ }
355
+
356
+ txID fxExecuteModulesFrom(txMachine* the, txSlot* queue, txSlot* module, txSlot** exception)
357
+ {
358
+ txSlot* fromModules = mxModuleTransfers(module)->value.reference;
359
+ txSlot* fromModule;
360
+ if (mxModuleTransfers(module)->kind == XS_NULL_KIND)
361
+ return XS_MODULE_STATUS_LINKED;
362
+ fromModule = fromModules->next;
363
+ while (fromModule) {
364
+ if (mxModuleStatus(fromModule) == XS_MODULE_STATUS_ERROR) {
365
+ *exception = mxModuleMeta(fromModule);
366
+ return XS_MODULE_STATUS_ERROR;
367
+ }
368
+ fromModule = fromModule->next;
369
+ }
370
+ fromModule = fromModules->next;
371
+ while (fromModule) {
372
+ if (mxModuleStatus(fromModule) == XS_MODULE_STATUS_EXECUTING) {
373
+ return XS_MODULE_STATUS_EXECUTING;
374
+ }
375
+ fromModule = fromModule->next;
376
+ }
377
+ fromModule = fromModules->next;
378
+ while (fromModule) {
379
+ if (mxModuleStatus(fromModule) == XS_MODULE_STATUS_LINKED) {
380
+ txSlot* waitingModule = queue->next;
381
+ while (waitingModule) {
382
+ if (module->value.reference == waitingModule->value.reference)
383
+ break;
384
+ if (fromModule->value.reference == waitingModule->value.reference)
385
+ return XS_MODULE_STATUS_EXECUTING;
386
+ waitingModule = waitingModule->next;
387
+ }
388
+ }
389
+ fromModule = fromModule->next;
390
+ }
391
+ return XS_MODULE_STATUS_LINKED;
392
+ }
393
+
394
+ void fxExecuteModulesFulfilled(txMachine* the)
395
+ {
396
+ txSlot* home = mxFunctionInstanceHome(mxFunction->value.reference);
397
+ txSlot* queue = home->value.home.object;
398
+ txSlot* module = home->value.home.module;
399
+ fxCompleteModule(the, module, C_NULL);
400
+ fxRunImportFulfilled(the, module, module);
401
+ fxExecuteModules(the, queue);
402
+ }
403
+
404
+ void fxExecuteModulesRejected(txMachine* the)
405
+ {
406
+ txSlot* home = mxFunctionInstanceHome(mxFunction->value.reference);
407
+ txSlot* queue = home->value.home.object;
408
+ txSlot* module = home->value.home.module;
409
+ fxCompleteModule(the, module, mxArgv(0));
410
+ fxRunImportRejected(the, module, module);
411
+ fxExecuteModules(the, queue);
412
+ }
413
+
414
+ void fxExecuteVirtualModuleSource(txMachine* the)
415
+ {
416
+ txSlot* instance = mxFunction->value.reference;
417
+ txSlot* function = fxLastProperty(the, instance);
418
+ txSlot* home = mxFunctionInstanceHome(instance);
419
+ txSlot* module = home->value.home.module;
420
+ txSlot* internal = mxModuleInstanceInternal(module);
421
+ txSlot* meta = mxModuleInstanceMeta(module);
422
+ txSlot* closures = mxFunctionInstanceCode(instance)->value.callback.closures;
423
+ txSlot* property;
424
+ if (mxIsUndefined(function))
425
+ return;
426
+ closures->flag |= XS_DONT_PATCH_FLAG;
427
+ closures->value.instance.prototype = C_NULL;
428
+ property = closures->next->next;
429
+ while (property) {
430
+ property->flag |= XS_DONT_DELETE_FLAG;
431
+ property = property->next;
432
+ }
433
+ /* THIS */
434
+ mxPushReference(home->value.home.object);
435
+ /* FUNCTION */
436
+ mxPushSlot(function);
437
+ mxCall();
438
+ /* ARGUMENTS */
439
+ mxPushReference(closures);
440
+ if (internal->flag & XS_IMPORT_FLAG) {
441
+ function = fxNewHostFunction(the, fxExecuteVirtualModuleSourceImport, 1, XS_NO_ID, mxExecuteVirtualModuleSourceImportProfileID);
442
+ mxFunctionInstanceHome(function)->value.home.module = module;
443
+ }
444
+ else
445
+ mxPushUndefined();
446
+ if (internal->flag & XS_IMPORT_META_FLAG)
447
+ mxPushSlot(meta);
448
+ else
449
+ mxPushUndefined();
450
+ mxRunCount(3);
451
+ mxPullSlot(mxResult);
452
+ }
453
+
454
+ txSlot* fxGetModule(txMachine* the, txSlot* realm, txID moduleID)
455
+ {
456
+ txSlot* result = mxBehaviorGetProperty(the, mxOwnModules(realm)->value.reference, moduleID, 0, XS_ANY);
457
+ return result;
458
+ }
459
+
460
+ void fxLinkCircularities(txMachine* the, txSlot* module, txSlot* circularities, txSlot* exports)
461
+ {
462
+ txSlot* circularitiesInstance;
463
+ txSlot* circularity;
464
+ txSlot* transfers;
465
+ txSlot* exportsInstance;
466
+ txSlot* export;
467
+ txSlot* transfer;
468
+ txSlot* aliases;
469
+ txSlot* alias;
470
+ txSlot* local;
471
+ txSlot* circularitiesCopy;
472
+ txSlot* circularityCopy;
473
+ txSlot* from;
474
+ txSlot* stars;
475
+ txSlot* star;
476
+ circularitiesInstance = circularities->value.reference;
477
+ circularity = circularitiesInstance->next;
478
+ while (circularity) {
479
+ if (circularity->value.reference == module->value.reference)
480
+ return;
481
+ circularity = circularity->next;
482
+ }
483
+ circularity = fxDuplicateSlot(the, module);
484
+ circularity->next = circularitiesInstance->next;
485
+ circularitiesInstance->next = circularity;
486
+ exportsInstance = exports->value.reference;
487
+ export = exportsInstance;
488
+ transfers = mxModuleExports(module);
489
+ if (transfers->kind == XS_REFERENCE_KIND) {
490
+ transfer = transfers->value.reference->next;
491
+ while (transfer) {
492
+ export = export->next = fxNewSlot(the);
493
+ export->ID = transfer->ID;
494
+ export->kind = transfer->kind;
495
+ export->value = transfer->value;
496
+ transfer = transfer->next;
497
+ }
498
+ return;
499
+ }
500
+ transfers = mxModuleTransfers(module);
501
+ if (transfers->kind == XS_REFERENCE_KIND) {
502
+ transfer = transfers->value.reference->next;
503
+ while (transfer) {
504
+ aliases = mxTransferAliases(transfer);
505
+ if (aliases->kind == XS_REFERENCE_KIND) {
506
+ alias = aliases->value.reference->next;
507
+ while (alias) {
508
+ export = export->next = fxNewSlot(the);
509
+ export->ID = alias->value.symbol;
510
+ export->kind = transfer->kind;
511
+ export->value = transfer->value;
512
+ alias = alias->next;
513
+ }
514
+ }
515
+ transfer = transfer->next;
516
+ }
517
+ transfer = transfers->value.reference->next;
518
+ while (transfer) {
519
+ local = mxTransferLocal(transfer);
520
+ aliases = mxTransferAliases(transfer);
521
+ if ((local->kind == XS_NULL_KIND) && (aliases->kind == XS_NULL_KIND)) {
522
+ from = mxTransferFrom(transfer);
523
+ fxNewInstance(the);
524
+ circularitiesCopy = the->stack;
525
+ circularity = circularitiesInstance->next;
526
+ circularityCopy = circularitiesCopy->value.reference;
527
+ while (circularity) {
528
+ circularityCopy = circularityCopy->next = fxDuplicateSlot(the, circularity);
529
+ circularity = circularity->next;
530
+ }
531
+ fxNewInstance(the);
532
+ stars = the->stack;
533
+ fxLinkCircularities(the, from, circularitiesCopy, stars);
534
+ star = stars->value.reference->next;
535
+ while (star) {
536
+ if (star->ID != mxID(_default)) {
537
+ txSlot* ambiguous = mxBehaviorGetProperty(the, exportsInstance, star->ID, 0, XS_OWN);
538
+ if (ambiguous) {
539
+ txSlot* ambiguousModule;
540
+ txSlot* starModule;
541
+ if (ambiguous->kind == XS_EXPORT_KIND)
542
+ ambiguousModule = ambiguous->value.export.module;
543
+ else
544
+ ambiguousModule = mxTransferClosure(ambiguous)->value.export.module;
545
+ if (star->kind == XS_EXPORT_KIND)
546
+ starModule = star->value.export.module;
547
+ else
548
+ starModule = mxTransferClosure(star)->value.export.module;
549
+ if (ambiguousModule != starModule) {
550
+ ambiguous->kind = XS_EXPORT_KIND;
551
+ ambiguous->value.export.closure = C_NULL;
552
+ ambiguous->value.export.module = C_NULL;
553
+ }
554
+ }
555
+ else {
556
+ export = export->next = fxNewSlot(the);
557
+ export->ID = star->ID;
558
+ export->kind = star->kind;
559
+ export->value = star->value;
560
+ }
561
+ }
562
+ star = star->next;
563
+ }
564
+ mxPop();
565
+ mxPop();
566
+ }
567
+ transfer = transfer->next;
568
+ }
569
+ }
570
+ }
571
+
572
+ void fxLinkExports(txMachine* the, txSlot* module)
573
+ {
574
+ txSlot* exports;
575
+ txSlot* transfer;
576
+ txSlot* from;
577
+ txSlot* import;
578
+ txSlot* closure;
579
+
580
+ exports = mxModuleExports(module)->value.reference;
581
+ exports->flag |= XS_DONT_PATCH_FLAG;
582
+ transfer = exports->next;
583
+ while (transfer) {
584
+ if (transfer->kind != XS_EXPORT_KIND) {
585
+ from = mxTransferFrom(transfer);
586
+ if (from->kind != XS_NULL_KIND) {
587
+ import = mxTransferImport(transfer);
588
+ if (import->kind != XS_NULL_KIND)
589
+ fxLinkTransfer(the, from, import->value.symbol, transfer);
590
+ else
591
+ fxLinkNamespace(the, from, transfer);
592
+ }
593
+ closure = mxTransferClosure(transfer);
594
+ transfer->kind = closure->kind;
595
+ transfer->value = closure->value;
596
+ }
597
+ transfer->flag |= XS_DONT_DELETE_FLAG | XS_DONT_SET_FLAG;
598
+ transfer = transfer->next;
599
+ }
600
+ }
601
+
602
+ void fxLinkLocals(txMachine* the, txSlot* module)
603
+ {
604
+ txSlot* transfer;
605
+ txSlot* local;
606
+ txSlot* from;
607
+ txSlot* import;
608
+ txSlot* closure;
609
+
610
+ transfer = mxModuleTransfers(module)->value.reference->next;
611
+ while (transfer) {
612
+ local = mxTransferLocal(transfer);
613
+ if (local->kind != XS_NULL_KIND) {
614
+ from = mxTransferFrom(transfer);
615
+ if (from->kind != XS_NULL_KIND) {
616
+ import = mxTransferImport(transfer);
617
+ if (import->kind != XS_NULL_KIND)
618
+ fxLinkTransfer(the, from, import->value.symbol, transfer);
619
+ else
620
+ fxLinkNamespace(the, from, transfer);
621
+ closure = mxTransferClosure(transfer);
622
+ closure->flag |= XS_DONT_SET_FLAG;
623
+ }
624
+ }
625
+ transfer = transfer->next;
626
+ }
627
+ }
628
+
629
+ void fxLinkModules(txMachine* the, txSlot* queue)
630
+ {
631
+ txSlot* module;
632
+ txSlot* order;
633
+ txSlot* transfer;
634
+ txSlot* local;
635
+ txSlot* from;
636
+ txSlot* import;
637
+ txSlot* closure;
638
+ txSlot* property;
639
+ txSlot* circularities;
640
+ txSlot* exports;
641
+ txSlot* realm;
642
+ txSlot* closures;
643
+ txSlot* export;
644
+
645
+ order = fxNewInstance(the);
646
+ while ((module = queue->next))
647
+ fxOrderModule(the, queue, order, module);
648
+ queue->next = order->next;
649
+ order->next = C_NULL;
650
+ mxPop();
651
+
652
+ mxReportModuleQueue("LINK");
653
+ module = queue->next;
654
+ while (module) {
655
+ if (mxModuleStatus(module) == XS_MODULE_STATUS_LINKING) {
656
+ transfer = mxModuleTransfers(module)->value.reference->next;
657
+ while (transfer) {
658
+ local = mxTransferLocal(transfer);
659
+ from = mxTransferFrom(transfer);
660
+ import = mxTransferImport(transfer);
661
+ if (local->kind != XS_NULL_KIND) {
662
+ if ((from->kind == XS_NULL_KIND) || (import->kind == XS_NULL_KIND)) {
663
+ closure = mxTransferClosure(transfer);
664
+ closure->value.export.closure = fxNewSlot(the);
665
+ closure->value.export.closure->kind = XS_UNINITIALIZED_KIND;
666
+ closure->value.export.module = module->value.reference;
667
+ closure->kind = XS_EXPORT_KIND;
668
+ }
669
+ }
670
+ else {
671
+ if ((from->kind != XS_NULL_KIND) && (import->kind == XS_NULL_KIND)) {
672
+ closure = mxTransferClosure(transfer);
673
+ closure->value.export.closure = fxNewSlot(the);
674
+ closure->value.export.closure->kind = XS_UNINITIALIZED_KIND;
675
+ closure->value.export.module = module->value.reference;
676
+ closure->kind = XS_EXPORT_KIND;
677
+ }
678
+ }
679
+ transfer = transfer->next;
680
+ }
681
+ }
682
+ module = module->next;
683
+ }
684
+
685
+ module = queue->next;
686
+ while (module) {
687
+ if (mxModuleStatus(module) == XS_MODULE_STATUS_LINKING) {
688
+ fxNewInstance(the);
689
+ circularities = the->stack;
690
+ fxNewInstance(the);
691
+ exports = the->stack;
692
+ fxLinkCircularities(the, module, circularities, exports);
693
+ mxModuleExports(module)->kind = XS_REFERENCE_KIND;
694
+ mxModuleExports(module)->value.reference = exports->value.reference;
695
+ mxPop();
696
+ mxPop();
697
+ }
698
+ module = module->next;
699
+ }
700
+
701
+ module = queue->next;
702
+ while (module) {
703
+ if (mxModuleStatus(module) == XS_MODULE_STATUS_LINKING)
704
+ fxLinkExports(the, module);
705
+ module = module->next;
706
+ }
707
+ module = queue->next;
708
+ while (module) {
709
+ if (mxModuleStatus(module) == XS_MODULE_STATUS_LINKING)
710
+ fxLinkLocals(the, module);
711
+ module = module->next;
712
+ }
713
+
714
+ module = queue->next;
715
+ while (module) {
716
+ if (mxModuleStatus(module) == XS_MODULE_STATUS_LINKING) {
717
+ txInteger count = 0;
718
+ mxPushUndefined();
719
+ realm = mxModuleInternal(module)->value.module.realm;
720
+ closures = fxNewEnvironmentInstance(the, mxRealmClosures(realm));
721
+ closure = closures->next;
722
+ transfer = mxModuleTransfers(module)->value.reference->next;
723
+ while (transfer) {
724
+ local = mxTransferLocal(transfer);
725
+ if (local->kind != XS_NULL_KIND) {
726
+ export = mxTransferClosure(transfer);
727
+ mxCheck(the, export->kind == XS_EXPORT_KIND);
728
+ closure = closure->next = fxNewSlot(the);
729
+ closure->ID = local->value.symbol;
730
+ closure->flag = export->flag;
731
+ closure->kind = XS_CLOSURE_KIND;
732
+ closure->value.closure = export->value.export.closure;
733
+ }
734
+ from = mxTransferFrom(transfer);
735
+ if (from->kind != XS_NULL_KIND)
736
+ count++;
737
+ transfer = transfer->next;
738
+ }
739
+ property = mxModuleInitialize(module);
740
+ if (property->kind == XS_REFERENCE_KIND) {
741
+ property = property->value.reference->next;
742
+ if ((property->kind == XS_CODE_KIND) || (property->kind == XS_CODE_X_KIND))
743
+ property->value.code.closures = closures;
744
+ else if (property->kind == XS_CALLBACK_KIND)
745
+ property->value.callback.closures = closures;
746
+ }
747
+ property = mxModuleExecute(module);
748
+ if (property->kind == XS_REFERENCE_KIND) {
749
+ property = property->value.reference->next;
750
+ if ((property->kind == XS_CODE_KIND) || (property->kind == XS_CODE_X_KIND))
751
+ property->value.code.closures = closures;
752
+ else if (property->kind == XS_CALLBACK_KIND)
753
+ property->value.callback.closures = closures;
754
+ }
755
+ mxPop();
756
+ if (count > 0) {
757
+ txSlot* instance = fxNewInstance(the);
758
+ transfer = mxModuleTransfers(module)->value.reference->next;
759
+ while (transfer) {
760
+ from = mxTransferFrom(transfer);
761
+ if (from->kind != XS_NULL_KIND) {
762
+ txSlot** address = &(instance->next);
763
+ txSlot* slot;
764
+ while ((slot = *address)) {
765
+ if (slot->value.reference == from->value.reference)
766
+ break;
767
+ address = &slot->next;
768
+ }
769
+ if (!slot)
770
+ slot = *address = fxDuplicateSlot(the, from);
771
+ }
772
+ transfer = transfer->next;
773
+ }
774
+ mxModuleTransfers(module)->value.reference = instance;
775
+ mxPop();
776
+ }
777
+ else
778
+ mxModuleTransfers(module)->kind = XS_NULL_KIND;
779
+ }
780
+ module = module->next;
781
+ }
782
+
783
+ module = queue->next;
784
+ while (module) {
785
+ if (mxModuleStatus(module) == XS_MODULE_STATUS_LINKING) {
786
+ mxModuleStatus(module) = XS_MODULE_STATUS_LINKED;
787
+ property = mxModuleInitialize(module);
788
+ if (property->kind == XS_REFERENCE_KIND) {
789
+ mxPushSlot(mxModuleHosts(module));
790
+ mxPull(mxHosts);
791
+ mxPushUndefined();
792
+ mxPushSlot(property);
793
+ mxCall();
794
+ mxRunCount(0);
795
+ mxPop();
796
+ mxPushUndefined();
797
+ mxPull(mxHosts);
798
+ }
799
+ }
800
+ module = module->next;
801
+ }
802
+ }
803
+
804
+ void fxLinkNamespace(txMachine* the, txSlot* module, txSlot* transfer)
805
+ {
806
+ txSlot* export = mxTransferClosure(transfer);
807
+ mxCheck(the, export->kind == XS_EXPORT_KIND);
808
+ export->value.export.closure = fxDuplicateSlot(the, module);
809
+ export->value.export.closure->ID = XS_NO_ID;
810
+ }
811
+
812
+ void fxLinkTransfer(txMachine* the, txSlot* module, txID importID, txSlot* transfer)
813
+ {
814
+ txSlot* export;
815
+ txSlot* exportClosure;
816
+ txSlot* transferClosure;
817
+ txSlot* from;
818
+ txSlot* import;
819
+
820
+ export = mxBehaviorGetProperty(the, mxModuleExports(module)->value.reference, importID, 0, XS_OWN);
821
+ if (export) {
822
+ if (export->kind == XS_EXPORT_KIND) {
823
+ if (export->value.export.closure == C_NULL) {
824
+ txString path = C_NULL;
825
+ txInteger line = 0;
826
+ #ifdef mxDebug
827
+ txSlot* slot = mxTransferClosure(transfer)->next;
828
+ if (slot) {
829
+ path = slot->value.string;
830
+ line = slot->next->value.integer;
831
+ }
832
+ #endif
833
+ fxIDToString(the, importID, the->nameBuffer, sizeof(the->nameBuffer));
834
+ fxThrowMessage(the, path, line, XS_SYNTAX_ERROR, "import %s ambiguous", the->nameBuffer);
835
+ }
836
+ transferClosure = mxTransferClosure(transfer);
837
+ transferClosure->value = export->value;
838
+ transferClosure->kind = export->kind;
839
+ }
840
+ else {
841
+ exportClosure = mxTransferClosure(export);
842
+ if (exportClosure->kind != XS_NULL_KIND) {
843
+ transferClosure = mxTransferClosure(transfer);
844
+ transferClosure->value = exportClosure->value;
845
+ transferClosure->kind = exportClosure->kind;
846
+ }
847
+ else {
848
+ from = mxTransferFrom(export);
849
+ import = mxTransferImport(export);
850
+ if (((from->value.reference == module->value.reference) && (import->value.symbol == importID))
851
+ || ((from->value.reference == mxTransferFrom(transfer)->value.reference) && (import->value.symbol == mxTransferImport(transfer)->value.symbol))) {
852
+ txString path = C_NULL;
853
+ txInteger line = 0;
854
+ #ifdef mxDebug
855
+ txSlot* slot = mxTransferClosure(transfer)->next;
856
+ if (slot) {
857
+ path = slot->value.string;
858
+ line = slot->next->value.integer;
859
+ }
860
+ #endif
861
+ fxIDToString(the, importID, the->nameBuffer, sizeof(the->nameBuffer));
862
+ fxThrowMessage(the, path, line, XS_SYNTAX_ERROR, "import %s circular", the->nameBuffer);
863
+ }
864
+ fxCheckCStack(the);
865
+ fxLinkTransfer(the, from, import->value.symbol, transfer);
866
+ }
867
+ }
868
+ }
869
+ else {
870
+ txString path = C_NULL;
871
+ txInteger line = 0;
872
+ #ifdef mxDebug
873
+ txSlot* slot = mxTransferClosure(transfer)->next;
874
+ if (slot) {
875
+ path = slot->value.string;
876
+ line = slot->next->value.integer;
877
+ }
878
+ #endif
879
+ fxIDToString(the, importID, the->nameBuffer, sizeof(the->nameBuffer));
880
+ fxThrowMessage(the, path, line, XS_SYNTAX_ERROR, "import %s not found", the->nameBuffer);
881
+ }
882
+ }
883
+
884
+ void fxLoadModules(txMachine* the, txSlot* queue)
885
+ {
886
+ txSlot* module = queue->next;
887
+ txBoolean done = 1;
888
+ if (queue->flag & XS_LEVEL_FLAG)
889
+ return;
890
+ queue->flag |= XS_LEVEL_FLAG;
891
+ mxReportModuleQueue("LOAD");
892
+ while (module) {
893
+ if (mxModuleStatus(module) == XS_MODULE_STATUS_NEW) {
894
+ txSlot* loader = mxModuleLoader(module);
895
+ txID moduleID = loader->value.module.id;
896
+ txSlot* realm = loader->value.module.realm;
897
+ mxTry(the) {
898
+ if (fxMapModule(the, realm, moduleID, module, queue, C_NULL)) {
899
+ module = queue;
900
+ done = 0;
901
+ }
902
+ else {
903
+ txSlot* loadHook = mxLoadHook(realm);
904
+ mxModuleStatus(module) = XS_MODULE_STATUS_LOADING;
905
+ if (mxIsUndefined(loadHook)) {
906
+ if (realm != mxModuleInstanceInternal(mxProgram.value.reference)->value.module.realm)
907
+ mxTypeError("no loadHook");
908
+ fxLoadModule(the, module, moduleID);
909
+ if (mxModuleExecute(module)->kind == XS_NULL_KIND)
910
+ mxTypeError("no module");
911
+ mxModuleStatus(module) = XS_MODULE_STATUS_LOADED;
912
+ module = queue;
913
+ done = 1;
914
+ mxReportModuleQueue("LOAD");
915
+ }
916
+ else {
917
+ txSlot* promise;
918
+ txSlot* function;
919
+ txSlot* home;
920
+ txSlot* resolveLoadFunction;
921
+ txSlot* rejectLoadFunction;
922
+ done = 0;
923
+ mxModuleStatus(module) = XS_MODULE_STATUS_LOADING;
924
+
925
+ mxPushUndefined();
926
+ mxPushSlot(loadHook);
927
+ mxCall();
928
+ fxPushKeyString(the, moduleID);
929
+ mxRunCount(1);
930
+ if (!mxIsReference(the->stack))
931
+ mxTypeError("loadHook returned no object");
932
+ promise = the->stack->value.reference;
933
+ if (!mxIsPromise(promise))
934
+ mxTypeError("loadHook returned no promise");
935
+
936
+ function = fxNewHostFunction(the, fxLoadModulesFulfilled, 1, XS_NO_ID, mxLoadModulesFulfilledProfileID);
937
+ home = mxFunctionInstanceHome(function);
938
+ home->value.home.object = queue;
939
+ home->value.home.module = module->value.reference;
940
+ resolveLoadFunction = the->stack;
941
+
942
+ function = fxNewHostFunction(the, fxLoadModulesRejected, 1, XS_NO_ID, mxLoadModulesRejectedProfileID);
943
+ home = mxFunctionInstanceHome(function);
944
+ home->value.home.object = queue;
945
+ home->value.home.module = module->value.reference;
946
+ rejectLoadFunction = the->stack;
947
+
948
+ fxPromiseThen(the, promise, resolveLoadFunction, rejectLoadFunction, C_NULL, C_NULL);
949
+
950
+ mxPop();
951
+ mxPop();
952
+ mxPop();
953
+ }
954
+ }
955
+ }
956
+ mxCatch(the) {
957
+ mxPush(mxException);
958
+ mxException = mxUndefined;
959
+ fxCompleteModule(the, module->value.reference, the->stack);
960
+ fxRunImportRejected(the, module->value.reference, module->value.reference);
961
+ }
962
+ }
963
+ else if (mxModuleStatus(module) == XS_MODULE_STATUS_LOADING) {
964
+ done = 0;
965
+ }
966
+ else if (mxModuleStatus(module) == XS_MODULE_STATUS_LOADED) {
967
+ mxTry(the) {
968
+ fxLoadModulesFrom(the, queue, module->value.reference, 0);
969
+ mxModuleStatus(module) = XS_MODULE_STATUS_LINKING;
970
+ module = queue;
971
+ done = 1;
972
+ }
973
+ mxCatch(the) {
974
+ mxPush(mxException);
975
+ mxException = mxUndefined;
976
+ fxCompleteModule(the, module->value.reference, the->stack);
977
+ fxRunImportRejected(the, module->value.reference, module->value.reference);
978
+ }
979
+ }
980
+ module = module->next;
981
+ }
982
+ queue->flag &= ~XS_LEVEL_FLAG;
983
+ if (done) {
984
+ mxTry(the) {
985
+ fxLinkModules(the, queue);
986
+ }
987
+ mxCatch(the) {
988
+ mxPush(mxException);
989
+ mxException = mxUndefined;
990
+ module = queue->next;
991
+ while (module) {
992
+ if (mxModuleStatus(module) == XS_MODULE_STATUS_LINKING) {
993
+ fxCompleteModule(the, module->value.reference, the->stack);
994
+ fxRunImportRejected(the, module->value.reference, module->value.reference);
995
+ }
996
+ module = module->next;
997
+ }
998
+ mxPop();
999
+ }
1000
+ fxExecuteModules(the, queue);
1001
+ }
1002
+ }
1003
+
1004
+ void fxLoadModulesFrom(txMachine* the, txSlot* queue, txSlot* module, txBoolean now)
1005
+ {
1006
+ txSlot* internal = mxModuleInstanceInternal(module);
1007
+ txID moduleID = internal->value.module.id;
1008
+ txSlot* realm = internal->value.module.realm;
1009
+ txSlot* transfer = mxModuleInstanceTransfers(module)->value.reference->next;
1010
+ while (transfer) {
1011
+ txSlot* from = mxTransferFrom(transfer);
1012
+ if (from->kind != XS_NULL_KIND) {
1013
+ txID importModuleID = fxResolveSpecifier(the, realm, moduleID, from);
1014
+ txSlot* importModule = fxGetModule(the, realm, importModuleID);
1015
+ txID status;
1016
+ if (!importModule) {
1017
+ importModule = mxBehaviorSetProperty(the, mxOwnModules(realm)->value.reference, importModuleID, 0, XS_OWN);
1018
+ fxNewModule(the, realm, importModuleID, importModule);
1019
+ }
1020
+ from->kind = XS_REFERENCE_KIND;
1021
+ from->value.reference = importModule->value.reference;
1022
+ status = mxModuleStatus(importModule);
1023
+ if ((status == XS_MODULE_STATUS_NEW) || (status == XS_MODULE_STATUS_LOADED)) {
1024
+ fxQueueModule(the, queue, importModule);
1025
+ }
1026
+ else if (now) {
1027
+ if (status == XS_MODULE_STATUS_LINKING) {
1028
+ txSlot* slot = queue->next;
1029
+ while (slot) {
1030
+ if (slot->value.reference == importModule->value.reference)
1031
+ break;
1032
+ slot= slot->next;
1033
+ }
1034
+ if (!slot)
1035
+ mxTypeError("async module");
1036
+ }
1037
+ else if (status == XS_MODULE_STATUS_ERROR) {
1038
+ mxPushSlot(mxModuleInstanceMeta(module));
1039
+ mxPull(mxException);
1040
+ fxJump(the);
1041
+ }
1042
+ else if (status != XS_MODULE_STATUS_EXECUTED) {
1043
+ mxTypeError("async module");
1044
+ }
1045
+ }
1046
+ }
1047
+ transfer = transfer->next;
1048
+ }
1049
+ }
1050
+
1051
+ void fxLoadModulesFulfilled(txMachine* the)
1052
+ {
1053
+ txSlot* home = mxFunctionInstanceHome(mxFunction->value.reference);
1054
+ txSlot* queue = home->value.home.object;
1055
+ txSlot* module = home->value.home.module;
1056
+ txSlot* internal = mxModuleInstanceInternal(module);
1057
+ txSlot* realm = internal->value.module.realm;
1058
+ txID moduleID = internal->value.module.id;
1059
+ mxTry(the) {
1060
+ mxPushReference(module);
1061
+ fxMapModuleDescriptor(the, realm, moduleID, the->stack, queue, C_NULL, mxArgv(0));
1062
+ mxPop(); // module
1063
+ }
1064
+ mxCatch(the) {
1065
+ mxPush(mxException);
1066
+ mxException = mxUndefined;
1067
+ fxCompleteModule(the, module, the->stack);
1068
+ fxRunImportRejected(the, module, module);
1069
+ mxPop();
1070
+ }
1071
+ fxLoadModules(the, queue);
1072
+ }
1073
+
1074
+ void fxLoadModulesRejected(txMachine* the)
1075
+ {
1076
+ txSlot* home = mxFunctionInstanceHome(mxFunction->value.reference);
1077
+ txSlot* queue = home->value.home.object;
1078
+ txSlot* module = home->value.home.module;
1079
+ fxCompleteModule(the, module, mxArgv(0));
1080
+ fxRunImportRejected(the, module, module);
1081
+ fxLoadModules(the, queue);
1082
+ }
1083
+
1084
+ void fxLoadVirtualModuleNamespace(txMachine* the, txSlot* object, txSlot* module)
1085
+ {
1086
+ txSlot* exports = fxNewInstance(the);
1087
+ txSlot* export = exports;
1088
+ txSlot* at;
1089
+ txSlot* property;
1090
+ at = fxNewInstance(the);
1091
+ mxBehaviorOwnKeys(the, object, XS_EACH_NAME_FLAG, at);
1092
+ mxTemporary(property);
1093
+ while ((at = at->next)) {
1094
+ if (at->value.at.id != XS_NO_ID) {
1095
+ if (mxBehaviorGetOwnProperty(the, object, at->value.at.id, at->value.at.index, property) && !(property->flag & XS_DONT_ENUM_FLAG)) {
1096
+ mxPushReference(object);
1097
+ mxGetAll(at->value.at.id, at->value.at.index);
1098
+ export = export->next = fxNewSlot(the);
1099
+ export->ID = at->value.at.id;
1100
+ export->value.export.closure = fxNewSlot(the);
1101
+ export->value.export.closure->kind = the->stack->kind;
1102
+ export->value.export.closure->value = the->stack->value;
1103
+ export->value.export.module = module;
1104
+ export->kind = XS_EXPORT_KIND;
1105
+ mxPop();
1106
+ }
1107
+ }
1108
+ }
1109
+ mxPop();
1110
+ mxPop();
1111
+ mxPullSlot(mxModuleInstanceExports(module));
1112
+ }
1113
+
1114
+ void fxLoadVirtualModuleSource(txMachine* the, txSlot* record, txSlot* instance)
1115
+ {
1116
+ txSlot* slot;
1117
+ txSlot* function;
1118
+ txSlot* property;
1119
+ txSlot* transfers;
1120
+ txSlot* transfer;
1121
+
1122
+ mxPushSlot(record);
1123
+ mxGetID(fxID(the, "execute"));
1124
+ slot = the->stack;
1125
+ if (!mxIsUndefined(slot)) {
1126
+ if (!fxIsCallable(the, slot))
1127
+ mxTypeError("execute is no function");
1128
+ }
1129
+ function = fxNewHostFunction(the, fxExecuteVirtualModuleSource, 0, XS_NO_ID, mxExecuteVirtualModuleSourceProfileID);
1130
+ property = mxFunctionInstanceHome(function);
1131
+ property->value.home.object = fxToInstance(the, record);
1132
+ property->value.home.module = instance;
1133
+ property = fxLastProperty(the, function);
1134
+ property = fxNextSlotProperty(the, property, slot, XS_NO_ID, XS_INTERNAL_FLAG);
1135
+ mxPullSlot(mxModuleInstanceExecute(instance));
1136
+ mxPop(); // initialize
1137
+
1138
+ mxPush(mxObjectPrototype);
1139
+ transfers = fxNewObjectInstance(the);
1140
+ transfer = fxLastProperty(the, transfers);
1141
+
1142
+ mxPushSlot(record);
1143
+ mxGetID(mxID(_bindings));
1144
+ if (!mxIsUndefined(the->stack)) {
1145
+ txSlot* array;
1146
+ txInteger length, index;
1147
+
1148
+ array = the->stack;
1149
+ mxPushSlot(array);
1150
+ mxGetID(mxID(_length));
1151
+ length = fxToInteger(the, the->stack);
1152
+ mxPop();
1153
+
1154
+ for (index = 0; index < length; index++) {
1155
+ txSlot* item;
1156
+ txSlot* temporary;
1157
+ txInteger from = 0;
1158
+ txInteger export = 0;
1159
+ txInteger import = 0;
1160
+ txID nameID, asID;
1161
+ txSlot* specifier = C_NULL;
1162
+ txSlot* former;
1163
+
1164
+ mxPushSlot(array);
1165
+ mxGetIndex(index);
1166
+ item = the->stack;
1167
+
1168
+ mxTemporary(temporary);
1169
+
1170
+ mxPushSlot(item);
1171
+ mxGetID(mxID(_from));
1172
+ if (!mxIsUndefined(the->stack)) {
1173
+ from++;
1174
+ fxToString(the, the->stack);
1175
+ mxPullSlot(temporary);
1176
+ specifier = temporary;
1177
+ }
1178
+ else
1179
+ mxPop();
1180
+
1181
+ mxPushSlot(item);
1182
+ mxGetID(fxID(the, "exportAllFrom"));
1183
+ if (!mxIsUndefined(the->stack)) {
1184
+ from++;
1185
+ export++;
1186
+ fxToString(the, the->stack);
1187
+ mxPullSlot(temporary);
1188
+ specifier = temporary;
1189
+ nameID = XS_NO_ID;
1190
+ }
1191
+ else
1192
+ mxPop();
1193
+
1194
+ mxPushSlot(item);
1195
+ mxGetID(fxID(the, "importAllFrom"));
1196
+ if (!mxIsUndefined(the->stack)) {
1197
+ from++;
1198
+ import++;
1199
+ fxToString(the, the->stack);
1200
+ mxPullSlot(temporary);
1201
+ specifier = temporary;
1202
+ nameID = XS_NO_ID;
1203
+ }
1204
+ else
1205
+ mxPop();
1206
+
1207
+ mxPushSlot(item);
1208
+ mxGetID(mxID(_export));
1209
+ if (!mxIsUndefined(the->stack)) {
1210
+ export++;
1211
+ fxToString(the, the->stack);
1212
+ nameID = fxNewName(the, the->stack);
1213
+ }
1214
+ mxPop();
1215
+
1216
+ mxPushSlot(item);
1217
+ mxGetID(mxID(_import));
1218
+ if (!mxIsUndefined(the->stack)) {
1219
+ import++;
1220
+ fxToString(the, the->stack);
1221
+ nameID = fxNewName(the, the->stack);
1222
+ }
1223
+ mxPop();
1224
+
1225
+ mxPushSlot(item);
1226
+ mxGetID(mxID(_as));
1227
+ if (!mxIsUndefined(the->stack)) {
1228
+ fxToString(the, the->stack);
1229
+ asID = fxNewName(the, the->stack);
1230
+ }
1231
+ else
1232
+ asID = nameID;
1233
+ mxPop();
1234
+
1235
+ if (from > 1)
1236
+ mxSyntaxError("too many from");
1237
+ else if (export > 1)
1238
+ mxSyntaxError("too many export");
1239
+ else if (import > 1)
1240
+ mxSyntaxError("too many import");
1241
+ else if (export && import)
1242
+ mxSyntaxError("export and import");
1243
+ else if (!export && !import)
1244
+ mxSyntaxError("neither export nor import");
1245
+
1246
+ if (export) {
1247
+ if (asID != XS_NO_ID) {
1248
+ former = transfers->next;
1249
+ while (former) {
1250
+ txSlot* aliases = mxTransferAliases(former);
1251
+ if (!mxIsNull(aliases)) {
1252
+ txSlot* alias = aliases->value.reference->next;
1253
+ while (alias) {
1254
+ if (alias->value.symbol == asID) {
1255
+ fxIDToString(the, asID, the->nameBuffer, sizeof(the->nameBuffer));
1256
+ mxSyntaxError("duplicate export %s", the->nameBuffer);
1257
+ }
1258
+ alias = alias->next;
1259
+ }
1260
+ }
1261
+ former = former->next;
1262
+ }
1263
+ if (specifier) {
1264
+ former = transfers->next;
1265
+ while (former) {
1266
+ txSlot* aliases = mxTransferAliases(former);
1267
+ if (!mxIsNull(aliases)) {
1268
+ txSlot* local = mxTransferLocal(former);
1269
+ if (mxIsNull(local)) {
1270
+ txSlot* from = mxTransferFrom(former);
1271
+ if (!mxIsNull(from) && !c_strcmp(from->value.string, specifier->value.string)) {
1272
+ txSlot* import = mxTransferImport(former);
1273
+ if (mxIsNull(import)) {
1274
+ if (nameID == XS_NO_ID)
1275
+ break;
1276
+ }
1277
+ else {
1278
+ if (nameID == import->value.symbol)
1279
+ break;
1280
+ }
1281
+ }
1282
+ }
1283
+ }
1284
+ former = former->next;
1285
+ }
1286
+ }
1287
+ else {
1288
+ former = transfers->next;
1289
+ while (former) {
1290
+ txSlot* local = mxTransferLocal(former);
1291
+ if ((local->kind == XS_SYMBOL_KIND) && (local->value.symbol == nameID))
1292
+ break;
1293
+ former = former->next;
1294
+ }
1295
+ }
1296
+ if (former) {
1297
+ txSlot* aliases = mxTransferAliases(former);
1298
+ txSlot* alias;
1299
+ if (mxIsNull(aliases)) {
1300
+ mxPush(mxObjectPrototype);
1301
+ alias = fxLastProperty(the, fxNewObjectInstance(the));
1302
+ mxPullSlot(mxTransferAliases(former));
1303
+ }
1304
+ else {
1305
+ alias = fxLastProperty(the, aliases->value.reference);
1306
+ }
1307
+ fxNextSymbolProperty(the, alias, asID, XS_NO_ID, XS_DONT_ENUM_FLAG);
1308
+ }
1309
+ else if (specifier) {
1310
+ mxPushNull();
1311
+ mxPushSlot(specifier);
1312
+ if ((nameID != XS_NO_ID) || (asID != XS_NO_ID)) {
1313
+ if (nameID != XS_NO_ID)
1314
+ mxPushSymbol(nameID);
1315
+ else
1316
+ mxPushNull();
1317
+ if (asID != XS_NO_ID)
1318
+ mxPushSymbol(asID);
1319
+ else
1320
+ mxPushSymbol(nameID);
1321
+ mxPushInteger(4);
1322
+ }
1323
+ else {
1324
+ mxPushNull();
1325
+ mxPushInteger(3);
1326
+ }
1327
+ fxPrepareTransfer(the);
1328
+ transfer = fxNextSlotProperty(the, transfer, the->stack, XS_NO_ID, XS_DONT_ENUM_FLAG);
1329
+ mxPop(); // transfer
1330
+ }
1331
+ else {
1332
+ mxPushSymbol(nameID);
1333
+ mxPushNull();
1334
+ mxPushNull();
1335
+ mxPushSymbol(asID);
1336
+ mxPushInteger(4);
1337
+ fxPrepareTransfer(the);
1338
+ transfer = fxNextSlotProperty(the, transfer, the->stack, XS_NO_ID, XS_DONT_ENUM_FLAG);
1339
+ mxPop(); // transfer
1340
+ }
1341
+ }
1342
+ else if (specifier) {
1343
+ former = transfers->next;
1344
+ while (former) {
1345
+ txSlot* from = mxTransferFrom(former);
1346
+ if (!mxIsNull(from) && !c_strcmp(from->value.string, specifier->value.string)) {
1347
+ txSlot* local = mxTransferLocal(former);
1348
+ if (mxIsNull(local)) {
1349
+ txSlot* import = mxTransferImport(former);
1350
+ if (mxIsNull(import)) {
1351
+ txSlot* aliases = mxTransferAliases(former);
1352
+ if (mxIsNull(aliases)) {
1353
+ mxSyntaxError("duplicate export *");
1354
+ }
1355
+ }
1356
+ }
1357
+ }
1358
+ former = former->next;
1359
+ }
1360
+ mxPushNull();
1361
+ mxPushSlot(specifier);
1362
+ mxPushNull();
1363
+ mxPushInteger(3);
1364
+ fxPrepareTransfer(the);
1365
+ transfer = fxNextSlotProperty(the, transfer, the->stack, XS_NO_ID, XS_DONT_ENUM_FLAG);
1366
+ mxPop(); // transfer
1367
+ }
1368
+ else
1369
+ mxSyntaxError("invalid export *");
1370
+ }
1371
+ else {
1372
+ if (!specifier) {
1373
+ if (asID == XS_NO_ID)
1374
+ mxSyntaxError("invalid import *");
1375
+ else {
1376
+ fxIDToString(the, asID, the->nameBuffer, sizeof(the->nameBuffer));
1377
+ mxSyntaxError("invalid import %s", the->nameBuffer);
1378
+ }
1379
+ }
1380
+ else if (asID == XS_NO_ID)
1381
+ mxSyntaxError("invalid import * from %s", specifier->value.string);
1382
+
1383
+ former = transfers->next;
1384
+ while (former) {
1385
+ txSlot* local = mxTransferLocal(former);
1386
+ if ((local->kind == XS_SYMBOL_KIND) && (local->value.symbol == asID)) {
1387
+ txSlot* from = mxTransferFrom(former);
1388
+ if (!mxIsNull(from)) {
1389
+ fxIDToString(the, asID, the->nameBuffer, sizeof(the->nameBuffer));
1390
+ mxSyntaxError("duplicate import %s", the->nameBuffer);
1391
+ }
1392
+ break;
1393
+ }
1394
+ former = former->next;
1395
+ }
1396
+ if (former) {
1397
+ txSlot* from = mxTransferFrom(former);
1398
+ mxPushSlot(specifier);
1399
+ mxPullSlot(from);
1400
+ if (nameID != XS_NO_ID) {
1401
+ txSlot* import = mxTransferImport(former);
1402
+ import->kind = XS_SYMBOL_KIND;
1403
+ import->value.symbol = nameID;
1404
+ }
1405
+ }
1406
+ else {
1407
+ mxPushSymbol(asID);
1408
+ mxPushSlot(specifier);
1409
+ if (nameID != XS_NO_ID)
1410
+ mxPushSymbol(nameID);
1411
+ else
1412
+ mxPushNull();
1413
+ mxPushInteger(3);
1414
+ fxPrepareTransfer(the);
1415
+ transfer = fxNextSlotProperty(the, transfer, the->stack, XS_NO_ID, XS_DONT_ENUM_FLAG);
1416
+ mxPop(); // transfer
1417
+ }
1418
+ }
1419
+
1420
+ mxPop(); // temporary
1421
+ mxPop(); // item
1422
+ }
1423
+ }
1424
+ mxPop(); // bindings
1425
+
1426
+ #if mxReport
1427
+ {
1428
+ fxIDToString(the, mxModuleInstanceInternal(instance)->value.module.id, the->nameBuffer, sizeof(the->nameBuffer));
1429
+ fprintf(stderr, "### %s\n", the->nameBuffer);
1430
+ txSlot* transfer = the->stack->value.reference->next;
1431
+ while (transfer) {
1432
+ txSlot* local = mxTransferLocal(transfer);
1433
+ txSlot* from = mxTransferFrom(transfer);
1434
+ txSlot* import = mxTransferImport(transfer);
1435
+ txSlot* aliases = mxTransferAliases(transfer);
1436
+ if (local->kind != XS_NULL_KIND) {
1437
+ fxIDToString(the, local->value.symbol, the->nameBuffer, sizeof(the->nameBuffer));
1438
+ fprintf(stderr, " local %s", the->nameBuffer);
1439
+ }
1440
+ if (from->kind != XS_NULL_KIND) {
1441
+ fprintf(stderr, " from %s", from->value.string);
1442
+ }
1443
+ if (import->kind != XS_NULL_KIND) {
1444
+ fxIDToString(the, import->value.symbol, the->nameBuffer, sizeof(the->nameBuffer));
1445
+ fprintf(stderr, " import %s", the->nameBuffer);
1446
+ }
1447
+ if (!mxIsNull(aliases)) {
1448
+ txSlot* alias = aliases->value.reference->next;
1449
+ while (alias) {
1450
+ fxIDToString(the, alias->value.symbol, the->nameBuffer, sizeof(the->nameBuffer));
1451
+ fprintf(stderr, " alias %s", the->nameBuffer);
1452
+ alias = alias->next;
1453
+ }
1454
+ }
1455
+ fprintf(stderr, "\n");
1456
+ transfer = transfer->next;
1457
+ }
1458
+ }
1459
+ #endif
1460
+
1461
+ mxPullSlot(mxModuleInstanceTransfers(instance));
1462
+
1463
+ mxPushSlot(record);
1464
+ mxGetID(mxID(_needsImport));
1465
+ if (!mxIsUndefined(the->stack)) {
1466
+ if (fxToBoolean(the, the->stack)) {
1467
+ txSlot* internal = mxModuleInstanceInternal(instance);
1468
+ internal->flag |= XS_IMPORT_FLAG;
1469
+ }
1470
+ }
1471
+ mxPop(); // needsImport
1472
+
1473
+ mxPushSlot(record);
1474
+ mxGetID(mxID(_needsImportMeta));
1475
+ if (!mxIsUndefined(the->stack)) {
1476
+ if (fxToBoolean(the, the->stack)) {
1477
+ txSlot* internal = mxModuleInstanceInternal(instance);
1478
+ internal->flag |= XS_IMPORT_META_FLAG;
1479
+ }
1480
+ }
1481
+ mxPop(); // _needsImportMeta
1482
+ }
1483
+
1484
+ txBoolean fxMapModule(txMachine* the, txSlot* realm, txID moduleID, txSlot* module, txSlot* queue, txSlot* result)
1485
+ {
1486
+ txSlot* moduleMap = mxModuleMap(realm);
1487
+ txSlot* descriptor = moduleMap->value.reference->next;
1488
+ while (descriptor) {
1489
+ if (descriptor->ID == moduleID) {
1490
+ fxMapModuleDescriptor(the, realm, moduleID, module, queue, result,descriptor);
1491
+ return 1;
1492
+ }
1493
+ descriptor = descriptor->next;
1494
+ }
1495
+ return 0;
1496
+ }
1497
+
1498
+ void fxMapModuleDescriptor(txMachine* the, txSlot* realm, txID moduleID, txSlot* module, txSlot* queue, txSlot* result, txSlot* descriptor)
1499
+ {
1500
+ txSlot* property;
1501
+ if (!mxIsReference(descriptor))
1502
+ mxTypeError("descriptor is no object");
1503
+
1504
+ mxPushSlot(descriptor);
1505
+ mxGetID(fxID(the, "namespace"));
1506
+ property = the->stack;
1507
+ if (!mxIsUndefined(property)) {
1508
+ if ((property->kind == XS_STRING_KIND) || (property->kind == XS_STRING_X_KIND)) {
1509
+ txSlot* aliasRealm;
1510
+ txSlot* aliasOwn;
1511
+ txID aliasModuleID;
1512
+ txSlot* aliasModule;
1513
+ mxPushSlot(descriptor);
1514
+ mxGetID(fxID(the, "compartment"));
1515
+ if (!mxIsUndefined(the->stack)) {
1516
+ txSlot* program = fxCheckCompartmentInstance(the, the->stack);
1517
+ aliasRealm = mxModuleInstanceInternal(program)->value.module.realm;
1518
+ }
1519
+ else {
1520
+ aliasRealm = mxRealmParent(realm)->value.reference;
1521
+ }
1522
+ aliasOwn = mxOwnModules(aliasRealm)->value.reference;
1523
+ mxPop();
1524
+ aliasModuleID = fxResolveSpecifier(the, aliasRealm, XS_NO_ID, property);
1525
+ // if ((aliasRealm == realm) && (aliasModuleID == moduleID))
1526
+ // mxTypeError("descriptor.specifier is circular");
1527
+
1528
+ aliasModule = mxBehaviorGetProperty(the, aliasOwn, aliasModuleID, 0, XS_ANY);
1529
+ if (aliasModule) {
1530
+ property = aliasModule;
1531
+ }
1532
+ else {
1533
+ property = mxBehaviorSetProperty(the, aliasOwn, aliasModuleID, 0, XS_OWN);
1534
+ fxNewModule(the, aliasRealm, aliasModuleID, property);
1535
+ }
1536
+ goto namespace;
1537
+ }
1538
+ else {
1539
+ txSlot* instance = module->value.reference;
1540
+ if (!mxIsReference(property))
1541
+ mxTypeError("descriptor.namespace is no object");
1542
+ if (mxIsModule(property->value.reference))
1543
+ goto namespace;
1544
+ fxLoadVirtualModuleNamespace(the, property->value.reference, instance);
1545
+ txID status = mxModuleInstanceStatus(instance);
1546
+ if ((status == XS_MODULE_STATUS_NEW) || (status == XS_MODULE_STATUS_LOADING)) {
1547
+ mxModuleInstanceStatus(instance) = XS_MODULE_STATUS_EXECUTED;
1548
+ if (result) {
1549
+
1550
+ }
1551
+ else {
1552
+ fxCompleteModule(the, instance, C_NULL);
1553
+ fxRunImportFulfilled(the, instance, instance);
1554
+ }
1555
+ }
1556
+ goto done;
1557
+ }
1558
+
1559
+ namespace:
1560
+ if (module->kind == XS_UNDEFINED_KIND) {
1561
+ module->kind = property->kind;
1562
+ module->value = property->value;
1563
+ }
1564
+ else {
1565
+ txID status = mxModuleStatus(module);
1566
+ mxCheck(the, (status == XS_MODULE_STATUS_NEW) || (status == XS_MODULE_STATUS_LOADING));
1567
+ mxPushSlot(module);
1568
+ fxOverrideModule(the, queue, result, module->value.reference, property->value.reference);
1569
+ status = mxModuleStatus(property);
1570
+ if (result) {
1571
+ if (status == XS_MODULE_STATUS_ERROR) {
1572
+ mxPushSlot(mxModuleMeta(property));
1573
+ mxPull(mxException);
1574
+ fxJump(the);
1575
+ }
1576
+ else if (status == XS_MODULE_STATUS_EXECUTED) {
1577
+ }
1578
+ else {
1579
+ fxQueueModule(the, queue, property);
1580
+ }
1581
+ }
1582
+ else {
1583
+ if (status == XS_MODULE_STATUS_ERROR) {
1584
+ fxCompleteModule(the, module->value.reference, C_NULL);
1585
+ fxRunImportRejected(the, module->value.reference, property->value.reference);
1586
+ }
1587
+ else if (status == XS_MODULE_STATUS_EXECUTED) {
1588
+ fxCompleteModule(the, module->value.reference, C_NULL);
1589
+ fxRunImportFulfilled(the, module->value.reference, property->value.reference);
1590
+ }
1591
+ else {
1592
+ txSlot* srcSlot = mxModuleFulfill(module);
1593
+ txSlot* dstSlot = fxLastProperty(the, property->value.reference);
1594
+ while (srcSlot) {
1595
+ dstSlot = fxNextSlotProperty(the, dstSlot, srcSlot, XS_NO_ID, XS_NO_FLAG);
1596
+ srcSlot = srcSlot->next;
1597
+ }
1598
+ fxQueueModule(the, queue, property);
1599
+ }
1600
+ }
1601
+ mxPop();
1602
+ }
1603
+ goto done;
1604
+ }
1605
+ mxPop(); // property;
1606
+
1607
+ if (module->kind == XS_UNDEFINED_KIND)
1608
+ fxNewModule(the, realm, moduleID, module);
1609
+ else {
1610
+ txID status = mxModuleStatus(module);
1611
+ mxCheck(the, (status == XS_MODULE_STATUS_NEW) || (status == XS_MODULE_STATUS_LOADING));
1612
+ }
1613
+
1614
+ mxPushSlot(descriptor);
1615
+ mxGetID(fxID(the, "archive"));
1616
+ property = the->stack;
1617
+ if (!mxIsUndefined(property)) {
1618
+ void* archive = fxGetHostData(the, property);
1619
+ txString path;
1620
+ void* code;
1621
+ size_t size;
1622
+ txScript script;
1623
+ mxPushSlot(descriptor);
1624
+ mxGetID(fxID(the, "path"));
1625
+ property = the->stack;
1626
+ path = fxToString(the, property);
1627
+ code = fxGetArchiveCode(the, archive, path, &size);
1628
+ if (code == C_NULL)
1629
+ mxURIError("module not found: %s", path);
1630
+ mxPop();
1631
+ script.callback = NULL;
1632
+ script.symbolsBuffer = NULL;
1633
+ script.symbolsSize = 0;
1634
+ script.codeBuffer = code;
1635
+ script.codeSize = (txSize)size;
1636
+ script.hostsBuffer = NULL;
1637
+ script.hostsSize = 0;
1638
+ script.path = path;
1639
+ script.version[0] = XS_MAJOR_VERSION;
1640
+ script.version[1] = XS_MINOR_VERSION;
1641
+ script.version[2] = XS_PATCH_VERSION;
1642
+ script.version[3] = 0;
1643
+ fxRunScript(the, &script, module, C_NULL, C_NULL, C_NULL, module->value.reference);
1644
+
1645
+ // mxPushClosure(module);
1646
+ mxPop();
1647
+ // mxPop(); // path
1648
+ if (mxModuleExecute(module)->kind == XS_NULL_KIND)
1649
+ mxTypeError("no module");
1650
+ goto importMeta;
1651
+ }
1652
+ mxPop(); // property;
1653
+
1654
+ mxPushSlot(descriptor);
1655
+ mxGetID(fxID(the, "source"));
1656
+ property = the->stack;
1657
+ if (!mxIsUndefined(property)) {
1658
+ if ((property->kind == XS_STRING_KIND) || (property->kind == XS_STRING_X_KIND)) {
1659
+ txSlot* loader = mxModuleLoader(module);
1660
+ loader->value.module.realm = mxRealmParent(realm)->value.reference;
1661
+ if (property->kind == XS_STRING_X_KIND)
1662
+ loader->value.module.id = fxNewNameX(the, property->value.string);
1663
+ else
1664
+ loader->value.module.id = fxNewName(the, property);
1665
+ mxModuleStatus(module) = XS_MODULE_STATUS_NEW;
1666
+ goto done;
1667
+ }
1668
+ if (!mxIsReference(property))
1669
+ mxTypeError("descriptor.source is no object");
1670
+ if (mxIsModuleSource(property->value.reference))
1671
+ fxDuplicateModuleTransfers(the, property, module);
1672
+ else
1673
+ // mxTypeError("descriptor.source is object");
1674
+ fxLoadVirtualModuleSource(the, property, module->value.reference);
1675
+ goto importMeta;
1676
+ }
1677
+ mxPop(); // property
1678
+
1679
+ mxTypeError("invalid descriptor");
1680
+
1681
+ importMeta:
1682
+ mxPop(); // property
1683
+ mxModuleStatus(module) = XS_MODULE_STATUS_LOADED;
1684
+
1685
+ mxPushSlot(descriptor);
1686
+ mxGetID(fxID(the, "importMeta"));
1687
+ property = the->stack;
1688
+ if (!mxIsUndefined(property)) {
1689
+ if (!mxIsReference(property))
1690
+ mxTypeError("descriptor.importMeta is no object");
1691
+ txSlot* meta = mxModuleMeta(module);
1692
+ meta->value.reference->flag &= ~XS_DONT_PATCH_FLAG;
1693
+ mxPushUndefined();
1694
+ mxPush(mxAssignObjectFunction);
1695
+ mxCall();
1696
+ mxPushSlot(meta);
1697
+ mxPushSlot(property);
1698
+ mxRunCount(2);
1699
+ mxPop();
1700
+ meta->value.reference->flag |= XS_DONT_PATCH_FLAG;
1701
+ }
1702
+ mxPop(); // property
1703
+
1704
+ mxPushSlot(descriptor);
1705
+ mxGetID(fxID(the, "specifier"));
1706
+ property = the->stack;
1707
+ if (!mxIsUndefined(property)) {
1708
+ txSlot* internal = mxModuleInternal(module);
1709
+ fxToString(the, property);
1710
+ if (property->kind == XS_STRING_X_KIND)
1711
+ internal->value.module.id = fxNewNameX(the, property->value.string);
1712
+ else
1713
+ internal->value.module.id = fxNewName(the, property);
1714
+ }
1715
+
1716
+ goto done;
1717
+
1718
+ done:
1719
+ mxPop(); // property
1720
+ }
1721
+
1722
+
1723
+ void fxNewModule(txMachine* the, txSlot* realm, txID moduleID, txSlot* module)
1724
+ {
1725
+ txSlot* slot;
1726
+ txSlot* meta;
1727
+
1728
+ mxPush(mxModulePrototype);
1729
+ slot = fxNewObjectInstance(the);
1730
+ slot->flag |= XS_EXOTIC_FLAG | XS_DONT_PATCH_FLAG;
1731
+ /* HOST */
1732
+ slot = slot->next = fxNewSlot(the);
1733
+ slot->ID = XS_MODULE_BEHAVIOR;
1734
+ slot->flag = XS_INTERNAL_FLAG;
1735
+ slot->kind = XS_MODULE_KIND;
1736
+ slot->value.module.realm = realm;
1737
+ slot->value.module.id = moduleID;
1738
+ /* EXPORTS */
1739
+ slot = fxNextNullProperty(the, slot, XS_NO_ID, XS_INTERNAL_FLAG);
1740
+ /* META */
1741
+ meta = fxNewInstance(the);
1742
+ meta->flag |= XS_DONT_PATCH_FLAG;
1743
+ slot = fxNextReferenceProperty(the, slot, meta, XS_NO_ID, XS_INTERNAL_FLAG);
1744
+ mxPop();
1745
+ /* TRANSFERS */
1746
+ slot = fxNextNullProperty(the, slot, XS_NO_ID, XS_INTERNAL_FLAG);
1747
+ /* INITIALIZE */
1748
+ slot = fxNextNullProperty(the, slot, XS_NO_ID, XS_INTERNAL_FLAG);
1749
+ /* FUNCTION */
1750
+ slot = fxNextNullProperty(the, slot, XS_NO_ID, XS_INTERNAL_FLAG);
1751
+ /* HOSTS */
1752
+ slot = fxNextNullProperty(the, slot, XS_NO_ID, XS_INTERNAL_FLAG);
1753
+ /* LOADER */
1754
+ slot = fxNextNullProperty(the, slot, XS_NO_ID, XS_INTERNAL_FLAG);
1755
+ slot->kind = XS_MODULE_KIND;
1756
+ slot->value.module.realm = realm;
1757
+ slot->value.module.id = moduleID;
1758
+ module->kind = the->stack->kind;
1759
+ module->value = the->stack->value;
1760
+ mxPop();
1761
+ #if mxInstrument
1762
+ the->loadedModulesCount++;
1763
+ #endif
1764
+ }
1765
+
1766
+ void fxOrderModule(txMachine* the, txSlot* queue, txSlot* order, txSlot* module)
1767
+ {
1768
+ txSlot** fromAddress = &(queue->next);
1769
+ txSlot** toAddress = &(order->next);
1770
+ txSlot* from;
1771
+ txSlot* to;
1772
+ while ((from = *fromAddress)) {
1773
+ if (from == module) {
1774
+ *fromAddress = module->next;
1775
+ module->next = C_NULL;
1776
+ break;
1777
+ }
1778
+ fromAddress = &(from->next);
1779
+ }
1780
+ if (mxModuleStatus(module) == XS_MODULE_STATUS_LINKING) {
1781
+ txSlot* transfer = mxModuleTransfers(module)->value.reference->next;
1782
+ while (transfer) {
1783
+ from = mxTransferFrom(transfer);
1784
+ if (from->kind != XS_NULL_KIND) {
1785
+ to = queue->next;
1786
+ while (to) {
1787
+ if (to->value.reference == from->value.reference)
1788
+ break;
1789
+ to = to->next;
1790
+ }
1791
+ if (to)
1792
+ fxOrderModule(the, queue, order, to);
1793
+ }
1794
+ transfer = transfer->next;
1795
+ }
1796
+ }
1797
+ while ((to = *toAddress)) {
1798
+ if (to->value.reference == module->value.reference)
1799
+ return;
1800
+ toAddress = &(to->next);
1801
+ }
1802
+ *toAddress = module;
1803
+ }
1804
+
1805
+ void fxOverrideModule(txMachine* the, txSlot* queue, txSlot* result, txSlot* module, txSlot* record)
1806
+ {
1807
+ txSlot* realm = mxModuleInstanceInternal(module)->value.module.realm;
1808
+ txSlot** address = &(queue->next);
1809
+ txSlot* slot;
1810
+ while ((slot = *address)) {
1811
+ if (slot->value.reference == module) {
1812
+ // slot->value.reference = record;
1813
+ *address = slot->next;
1814
+ }
1815
+ else {
1816
+ if (mxModuleStatus(slot) == XS_MODULE_STATUS_LINKING) {
1817
+ txSlot* transfer = mxModuleTransfers(slot)->value.reference->next;
1818
+ while (transfer) {
1819
+ txSlot* from = mxTransferFrom(transfer);
1820
+ if ((from->kind == XS_REFERENCE_KIND) && (from->value.reference == module))
1821
+ from->value.reference = record;
1822
+ transfer = transfer->next;
1823
+ }
1824
+ }
1825
+ }
1826
+
1827
+ address = &(slot->next);
1828
+ }
1829
+ slot = mxOwnModules(realm)->value.reference->next;
1830
+ while (slot) {
1831
+ if (slot->value.reference == module) {
1832
+ slot->value.reference = record;
1833
+ }
1834
+ slot = slot->next;
1835
+ }
1836
+ if (result) {
1837
+ if (result->value.reference == module) {
1838
+ result->value.reference = record;
1839
+ }
1840
+ }
1841
+ }
1842
+
1843
+ void fxPrepareModule(txMachine* the, txFlag flag)
1844
+ {
1845
+ txSlot* module = mxFunctionInstanceHome(mxFunction->value.reference)->value.home.module;
1846
+ txInteger c = the->stack->value.integer, i;
1847
+ txSlot* argument = the->stack + c;
1848
+ txSlot* result = the->stack + c;
1849
+ txSlot* slot;
1850
+ txSlot* property;
1851
+ property = mxModuleInstanceInternal(module);
1852
+ property->flag |= flag;
1853
+ slot = argument--;
1854
+ property = mxModuleInstanceInitialize(module);
1855
+ property->kind = slot->kind;
1856
+ property->value = slot->value;
1857
+ slot = argument--;
1858
+ property = mxModuleInstanceExecute(module);
1859
+ property->kind = slot->kind;
1860
+ property->value = slot->value;
1861
+ mxFunctionInstanceCode(slot->value.reference)->ID = mxModuleInstanceInternal(module)->value.module.id;
1862
+ slot = &mxHosts;
1863
+ property = mxModuleInstanceHosts(module);
1864
+ property->kind = slot->kind;
1865
+ property->value = slot->value;
1866
+ mxPush(mxObjectPrototype);
1867
+ slot = fxLastProperty(the, fxNewObjectInstance(the));
1868
+ for (i = 2; i < c; i++)
1869
+ slot = fxNextSlotProperty(the, slot, argument--, XS_NO_ID, XS_DONT_ENUM_FLAG);
1870
+ property = mxModuleInstanceTransfers(module);
1871
+ mxPullSlot(property);
1872
+ result->kind = XS_REFERENCE_KIND;
1873
+ result->value.reference = module;
1874
+ the->stack = result;
1875
+ }
1876
+
1877
+ void fxPrepareTransfer(txMachine* the)
1878
+ {
1879
+ txInteger c = the->stack->value.integer, i;
1880
+ txSlot* argument = the->stack + c;
1881
+ txSlot* result = the->stack + c;
1882
+ txSlot* property;
1883
+ txSlot* slot;
1884
+ mxPush(mxTransferPrototype);
1885
+ property = fxNewObjectInstance(the);
1886
+ property = fxNextSlotProperty(the, property, argument--, mxID(_local), XS_DONT_ENUM_FLAG);
1887
+ property = fxNextSlotProperty(the, property, argument--, mxID(_from), XS_DONT_ENUM_FLAG);
1888
+ property = fxNextSlotProperty(the, property, argument--, mxID(_import), XS_DONT_ENUM_FLAG);
1889
+ if (c > 3) {
1890
+ mxPush(mxObjectPrototype);
1891
+ slot = fxLastProperty(the, fxNewObjectInstance(the));
1892
+ for (i = 3; i < c; i++)
1893
+ slot = fxNextSlotProperty(the, slot, argument--, XS_NO_ID, XS_DONT_ENUM_FLAG);
1894
+ property = fxNextSlotProperty(the, property, the->stack, mxID(_aliases), XS_DONT_ENUM_FLAG);
1895
+ mxPop();
1896
+ }
1897
+ else {
1898
+ property = fxNextNullProperty(the, property, mxID(_aliases), XS_DONT_ENUM_FLAG);
1899
+ }
1900
+ property = fxNextNullProperty(the, property, mxID(_closure), XS_DONT_ENUM_FLAG);
1901
+ result->kind = the->stack->kind;
1902
+ result->value = the->stack->value;
1903
+ the->stack = result;
1904
+
1905
+ // #ifdef mxDebug
1906
+ // slot = the->frame->next;
1907
+ // if (slot) {
1908
+ // slot = slot - 1;
1909
+ // if (slot->next) {
1910
+ // property = fxNextSlotProperty(the, property, slot->next, XS_NO_ID, XS_DONT_ENUM_FLAG);
1911
+ // property = fxNextIntegerProperty(the, property, slot->ID, XS_NO_ID, XS_DONT_ENUM_FLAG);
1912
+ // }
1913
+ // }
1914
+ // #endif
1915
+ }
1916
+
1917
+ txBoolean fxQueueModule(txMachine* the, txSlot* queue, txSlot* module)
1918
+ {
1919
+ txSlot** address = &(queue->next);
1920
+ txSlot* slot;
1921
+ while ((slot = *address)) {
1922
+ if (slot->value.reference == module->value.reference)
1923
+ return 0;
1924
+ address = &(slot->next);
1925
+ }
1926
+ slot = *address = fxNewSlot(the);
1927
+ slot->ID = mxModuleInternal(module)->value.module.id; //??
1928
+ slot->kind = XS_REFERENCE_KIND;
1929
+ slot->value.reference = module->value.reference;
1930
+ return 1;
1931
+ }
1932
+
1933
+ void fxResolveModule(txMachine* the, txSlot* module, txID moduleID, txScript* script, void* data, txDestructor destructor)
1934
+ {
1935
+ if (script->codeBuffer) {
1936
+ txSlot* key = fxGetKey(the, moduleID);
1937
+ if (key) {
1938
+ txSlot* meta = mxModuleMeta(module);
1939
+ txSlot* slot = fxLastProperty(the, meta->value.reference);
1940
+ slot = slot->next = fxNewSlot(the);
1941
+ slot->value.string = key->value.key.string;
1942
+ if (key->kind == XS_KEY_KIND)
1943
+ slot->kind = XS_STRING_KIND;
1944
+ else
1945
+ slot->kind = XS_STRING_X_KIND;
1946
+ slot->ID = mxID(_uri);
1947
+ slot->flag |= XS_DONT_DELETE_FLAG | XS_DONT_SET_FLAG;
1948
+ }
1949
+ // mxPushClosure(module);
1950
+ fxRunScript(the, script, module, C_NULL, C_NULL, C_NULL, module->value.reference);
1951
+ mxPop();
1952
+ }
1953
+ }
1954
+
1955
+ txID fxResolveSpecifier(txMachine* the, txSlot* realm, txID moduleID, txSlot* name)
1956
+ {
1957
+ if (moduleID == XS_NO_ID) {
1958
+ if (realm == mxModuleInstanceInternal(mxProgram.value.reference)->value.module.realm) {
1959
+ moduleID = fxFindModule(the, realm, moduleID, name);
1960
+ if (moduleID == XS_NO_ID) {
1961
+ fxToStringBuffer(the, name, the->nameBuffer, sizeof(the->nameBuffer));
1962
+ mxReferenceError("module \"%s\" not found", the->nameBuffer);
1963
+ }
1964
+ }
1965
+ else {
1966
+ if (name->kind == XS_STRING_X_KIND)
1967
+ moduleID = fxNewNameX(the, name->value.string);
1968
+ else
1969
+ moduleID = fxNewName(the, name);
1970
+ }
1971
+ }
1972
+ else {
1973
+ txSlot* resolveHook = mxResolveHook(realm);
1974
+ while (mxIsUndefined(resolveHook)) {
1975
+ txSlot* parent = mxRealmParent(realm);
1976
+ if (mxIsReference(parent)) {
1977
+ realm = parent->value.reference;
1978
+ resolveHook = mxResolveHook(realm);
1979
+ }
1980
+ else
1981
+ break;
1982
+ }
1983
+ if (mxIsUndefined(resolveHook)) {
1984
+ mxCheck(the, realm == mxModuleInstanceInternal(mxProgram.value.reference)->value.module.realm);
1985
+ moduleID = fxFindModule(the, realm, moduleID, name);
1986
+ if (moduleID == XS_NO_ID) {
1987
+ fxToStringBuffer(the, name, the->nameBuffer, sizeof(the->nameBuffer));
1988
+ mxReferenceError("module \"%s\" not found", the->nameBuffer);
1989
+ }
1990
+ }
1991
+ else {
1992
+ mxPushUndefined();
1993
+ mxPushSlot(resolveHook);
1994
+ mxCall();
1995
+ mxPushSlot(name);
1996
+ fxPushKeyString(the, moduleID);
1997
+ mxRunCount(2);
1998
+ fxToString(the, the->stack);
1999
+ moduleID = fxNewName(the, the->stack);
2000
+ mxPop();
2001
+ }
2002
+ }
2003
+ name->kind = XS_SYMBOL_KIND;
2004
+ name->value.symbol = moduleID;
2005
+ return moduleID;
2006
+ }
2007
+
2008
+ void fxRunImport(txMachine* the, txSlot* realm, txID moduleID)
2009
+ {
2010
+ txSlot* stack = the->stack;
2011
+ txSlot* promise;
2012
+ txSlot* fulfillFunction;
2013
+ txSlot* rejectFunction;
2014
+ txSlot* module;
2015
+ txID status;
2016
+ txSlot* slot;
2017
+
2018
+ fxBeginHost(the);
2019
+ mxPush(mxPromisePrototype);
2020
+ promise = fxNewPromiseInstance(the);
2021
+ mxPromiseStatus(promise)->value.integer = mxPendingStatus;
2022
+ fxPushPromiseFunctions(the, promise);
2023
+ fulfillFunction = the->stack + 1;
2024
+ rejectFunction = the->stack;
2025
+ {
2026
+ mxTry(the) {
2027
+ fxToString(the, stack);
2028
+ moduleID = fxResolveSpecifier(the, realm, moduleID, stack);
2029
+ module = fxGetModule(the, realm, moduleID);
2030
+ if (!module) {
2031
+ module = mxBehaviorSetProperty(the, mxOwnModules(realm)->value.reference, moduleID, 0, XS_OWN);
2032
+ fxNewModule(the, realm, moduleID, module);
2033
+ }
2034
+ status = mxModuleStatus(module);
2035
+ if (status == XS_MODULE_STATUS_ERROR) {
2036
+ /* THIS */
2037
+ mxPushUndefined();
2038
+ /* FUNCTION */
2039
+ mxPushSlot(rejectFunction);
2040
+ mxCall();
2041
+ /* ARGUMENTS */
2042
+ mxPushSlot(mxModuleMeta(module));
2043
+ mxRunCount(1);
2044
+ }
2045
+ else if (status == XS_MODULE_STATUS_EXECUTED) {
2046
+ /* THIS */
2047
+ mxPushUndefined();
2048
+ /* FUNCTION */
2049
+ mxPushSlot(fulfillFunction);
2050
+ mxCall();
2051
+ /* ARGUMENTS */
2052
+ mxPushSlot(module);
2053
+ mxRunCount(1);
2054
+ }
2055
+ else {
2056
+ txSlot* queue = mxModuleQueue.value.reference;
2057
+ slot = fxLastProperty(the, module->value.reference);
2058
+ slot = fxNextSlotProperty(the, slot, fulfillFunction, XS_NO_ID, XS_NO_FLAG);
2059
+ slot = fxNextSlotProperty(the, slot, rejectFunction, XS_NO_ID, XS_NO_FLAG);
2060
+ // if ((status == XS_MODULE_STATUS_NEW) || (status == XS_MODULE_STATUS_LOADED)) {
2061
+ // txSlot* queue = fxNewInstance(the);
2062
+ if (fxQueueModule(the, queue, module))
2063
+ fxLoadModules(the, queue);
2064
+ // }
2065
+ }
2066
+ }
2067
+ mxCatch(the) {
2068
+ fxRejectException(the, rejectFunction);
2069
+ }
2070
+ }
2071
+ fxEndHost(the);
2072
+ stack->value.reference = promise;
2073
+ stack->kind = XS_REFERENCE_KIND;
2074
+ the->stack = stack;
2075
+ }
2076
+
2077
+ void fxRunImportFulfilled(txMachine* the, txSlot* module, txSlot* with)
2078
+ {
2079
+ if (mxModuleInstanceMeta(module)->next) {
2080
+ txSlot* stack = the->stack;
2081
+ txSlot* slot = mxModuleInstanceFulfill(module);
2082
+ while (slot) {
2083
+ mxPushSlot(slot);
2084
+ slot = slot->next;
2085
+ slot = slot->next;
2086
+ }
2087
+ mxModuleInstanceMeta(module)->next = C_NULL;
2088
+ slot = stack;
2089
+ while (slot > the->stack) {
2090
+ slot--;
2091
+ /* THIS */
2092
+ mxPushUndefined();
2093
+ /* FUNCTION */
2094
+ mxPushSlot(slot);
2095
+ mxCall();
2096
+ /* ARGUMENTS */
2097
+ mxPushReference(with);
2098
+ mxRunCount(1);
2099
+ mxPop();
2100
+ }
2101
+ the->stack = stack;
2102
+ }
2103
+ }
2104
+
2105
+ void fxRunImportRejected(txMachine* the, txSlot* module, txSlot* with)
2106
+ {
2107
+ if (mxModuleInstanceMeta(module)->next) {
2108
+ txSlot* stack = the->stack;
2109
+ txSlot* slot = mxModuleInstanceFulfill(module);
2110
+ txSlot* exception;
2111
+ while (slot) {
2112
+ slot = slot->next;
2113
+ mxPushSlot(slot);
2114
+ slot = slot->next;
2115
+ }
2116
+ mxModuleInstanceMeta(module)->next = C_NULL;
2117
+ exception = mxModuleInstanceMeta(with);
2118
+ slot = stack;
2119
+ while (slot > the->stack) {
2120
+ slot--;
2121
+ /* THIS */
2122
+ mxPushUndefined();
2123
+ /* FUNCTION */
2124
+ mxPushSlot(slot);
2125
+ mxCall();
2126
+ /* ARGUMENTS */
2127
+ mxPushSlot(exception);
2128
+ mxRunCount(1);
2129
+ mxPop();
2130
+ }
2131
+ the->stack = stack;
2132
+ }
2133
+ }
2134
+
2135
+ /* NOW */
2136
+
2137
+ void fxAwaitImport(txMachine* the, txBoolean defaultFlag)
2138
+ {
2139
+ txSlot* stack = the->stack;
2140
+ txSlot* realm = mxModuleInstanceInternal(mxProgram.value.reference)->value.module.realm;
2141
+ txID defaultID;
2142
+ txSlot* export;
2143
+ mxTry(the) {
2144
+ fxToString(the, stack);
2145
+ if (defaultFlag & XS_IMPORT_PREFLIGHT) {
2146
+ txID moduleID = fxFindModule(the, realm, XS_NO_ID, stack);
2147
+ stack->kind = XS_BOOLEAN_KIND;
2148
+ stack->value.boolean = moduleID != XS_NO_ID;
2149
+ }
2150
+ else {
2151
+ if (defaultFlag & XS_IMPORT_ASYNC) {
2152
+ gxDefaults.runImport(the, realm, XS_NO_ID);
2153
+ }
2154
+ else {
2155
+ fxRunImportNow(the, realm, XS_NO_ID);
2156
+ if (defaultFlag & XS_IMPORT_DEFAULT) {
2157
+ defaultID = mxID(_default);
2158
+ export = mxModuleExports(stack)->value.reference->next;
2159
+ while (export) {
2160
+ mxCheck(the, export->kind == XS_EXPORT_KIND);
2161
+ if (export->ID == defaultID) {
2162
+ stack->kind = export->value.export.closure->kind;
2163
+ stack->value = export->value.export.closure->value;
2164
+ break;
2165
+ }
2166
+ export = export->next;
2167
+ }
2168
+ }
2169
+ }
2170
+ }
2171
+ }
2172
+ mxCatch(the) {
2173
+ fxJump(the);
2174
+ }
2175
+ the->stack = stack;
2176
+ }
2177
+
2178
+ void fxRunImportNow(txMachine* the, txSlot* realm, txID moduleID)
2179
+ {
2180
+ txSlot* stack = the->stack;
2181
+ txSlot* module;
2182
+ txID status;
2183
+ txSlot* slot;
2184
+ fxToString(the, stack);
2185
+ moduleID = fxResolveSpecifier(the, realm, moduleID, stack);
2186
+ module = fxGetModule(the, realm, moduleID);
2187
+ if (!module) {
2188
+ module = mxBehaviorSetProperty(the, mxOwnModules(realm)->value.reference, moduleID, 0, XS_OWN);
2189
+ fxNewModule(the, realm, moduleID, module);
2190
+ }
2191
+ status = mxModuleStatus(module);
2192
+ if ((status == XS_MODULE_STATUS_NEW) || (status == XS_MODULE_STATUS_LOADED)) {
2193
+ txSlot* result = stack;
2194
+ txSlot* queue = fxNewInstance(the);
2195
+ mxTry(the) {
2196
+ txBoolean done = 1;
2197
+ result->kind = module->kind;
2198
+ result->value = module->value;
2199
+
2200
+ slot = mxBehaviorSetProperty(the, queue, moduleID, 0, XS_OWN);;
2201
+ slot->flag |= XS_BASE_FLAG;
2202
+ slot->kind = XS_REFERENCE_KIND;
2203
+ slot->value.reference = module->value.reference;
2204
+
2205
+ mxReportModuleQueue("LOAD");
2206
+ module = queue->next;
2207
+ while (module) {
2208
+ if (mxModuleStatus(module) == XS_MODULE_STATUS_NEW) {
2209
+ txSlot* loader = mxModuleLoader(module);
2210
+ txID moduleID = loader->value.module.id;
2211
+ txSlot* realm = loader->value.module.realm;
2212
+ if (!fxMapModule(the, realm, moduleID, module, queue, result)) {
2213
+ txSlot* loadNowHook = mxLoadNowHook(realm);
2214
+ if (mxIsUndefined(loadNowHook)) {
2215
+ if (realm != mxModuleInstanceInternal(mxProgram.value.reference)->value.module.realm)
2216
+ mxTypeError("no loadNowHook");
2217
+ fxLoadModule(the, module, moduleID);
2218
+ if (mxModuleExecute(module)->kind == XS_NULL_KIND)
2219
+ mxTypeError("no module");
2220
+ mxModuleStatus(module) = XS_MODULE_STATUS_LOADED;
2221
+ }
2222
+ else {
2223
+ txSlot* descriptor;
2224
+ txSlot* internal = mxModuleInternal(module);
2225
+ moduleID = internal->value.module.id;
2226
+ realm = internal->value.module.realm;
2227
+ done = 0;
2228
+ mxModuleStatus(module) = XS_MODULE_STATUS_LOADING;
2229
+ mxPushUndefined();
2230
+ mxPushSlot(loadNowHook);
2231
+ mxCall();
2232
+ fxPushKeyString(the, moduleID);
2233
+ mxRunCount(1);
2234
+ descriptor = the->stack;
2235
+ fxMapModuleDescriptor(the, realm, moduleID, module, queue, result, descriptor);
2236
+ mxPop(); // descriptor
2237
+ }
2238
+ }
2239
+ module = queue;
2240
+ done = 1;
2241
+ mxReportModuleQueue("LOAD");
2242
+ }
2243
+ else if (mxModuleStatus(module) == XS_MODULE_STATUS_LOADING) {
2244
+ done = 0;
2245
+ }
2246
+ else if (mxModuleStatus(module) == XS_MODULE_STATUS_LOADED) {
2247
+ fxLoadModulesFrom(the, queue, module->value.reference, 1);
2248
+ mxModuleStatus(module) = XS_MODULE_STATUS_LINKING;
2249
+ module = queue;
2250
+ done = 1;
2251
+ }
2252
+ module = module->next;
2253
+ }
2254
+ if (!done)
2255
+ mxTypeError("async queue");
2256
+ fxLinkModules(the, queue);
2257
+ mxReportModuleQueue("INIT");
2258
+ module = queue->next;
2259
+ while (module) {
2260
+ if (mxModuleStatus(module) == XS_MODULE_STATUS_LINKED) {
2261
+ mxModuleStatus(module) = XS_MODULE_STATUS_EXECUTING;
2262
+ if (mxModuleExecute(module)->value.reference->value.instance.prototype == mxAsyncFunctionPrototype.value.reference)
2263
+ mxTypeError("async module");
2264
+ mxPushSlot(mxModuleHosts(module));
2265
+ mxPull(mxHosts);
2266
+ mxPushUndefined();
2267
+ mxPushSlot(mxModuleExecute(module));
2268
+ mxCall();
2269
+ mxRunCount(0);
2270
+ mxPop();
2271
+ fxCompleteModule(the, module->value.reference, C_NULL);
2272
+ mxModuleMeta(module)->next = C_NULL;
2273
+ mxPushUndefined();
2274
+ mxPull(mxHosts);
2275
+ mxReportModuleQueue("INIT");
2276
+ }
2277
+ module = module->next;
2278
+ }
2279
+ mxPop();
2280
+ }
2281
+ mxCatch(the) {
2282
+ module = queue->next;
2283
+ while (module) {
2284
+ if (mxModuleStatus(module) < XS_MODULE_STATUS_EXECUTED) {
2285
+ fxCompleteModule(the, module->value.reference, &mxException);
2286
+ mxModuleMeta(module)->next = C_NULL;
2287
+ }
2288
+ module = module->next;
2289
+ }
2290
+ mxPushUndefined();
2291
+ mxPull(mxHosts);
2292
+ fxJump(the);
2293
+ }
2294
+ }
2295
+ else if (status == XS_MODULE_STATUS_EXECUTED) {
2296
+ stack->kind = module->kind;
2297
+ stack->value = module->value;
2298
+ }
2299
+ else if (status == XS_MODULE_STATUS_ERROR) {
2300
+ mxPushSlot(mxModuleMeta(module));
2301
+ mxPull(mxException);
2302
+ fxJump(the);
2303
+ }
2304
+ else {
2305
+ mxTypeError("async module");
2306
+ }
2307
+ the->stack = stack;
2308
+ }
2309
+
2310
+ /* BEHAVIOR */
2311
+
2312
+ void fxModuleGetter(txMachine* the) {
2313
+ mxReferenceError("not initialized yet");
2314
+ }
2315
+
2316
+ txBoolean fxModuleDefineOwnProperty(txMachine* the, txSlot* instance, txID id, txIndex index, txSlot* slot, txFlag mask)
2317
+ {
2318
+ txSlot* property = fxModuleFindProperty(the, instance, id, index);
2319
+ if (property) {
2320
+ if ((mask & XS_DONT_DELETE_FLAG) && !(slot->flag & XS_DONT_DELETE_FLAG))
2321
+ return 0;
2322
+ if ((mask & XS_DONT_ENUM_FLAG) && ((property->flag & XS_DONT_ENUM_FLAG) != (slot->flag & XS_DONT_ENUM_FLAG)))
2323
+ return 0;
2324
+ if (mask & XS_ACCESSOR_FLAG) {
2325
+ if (property->kind != XS_ACCESSOR_KIND)
2326
+ return 0;
2327
+ if (mask & XS_GETTER_FLAG) {
2328
+ if (property->value.accessor.getter != slot->value.accessor.getter)
2329
+ return 0;
2330
+ }
2331
+ if (mask & XS_SETTER_FLAG) {
2332
+ if (property->value.accessor.setter != slot->value.accessor.setter)
2333
+ return 0;
2334
+ }
2335
+ return 1;
2336
+ }
2337
+ if ((mask & XS_DONT_SET_FLAG) && ((property->flag & XS_DONT_SET_FLAG) != (slot->flag & XS_DONT_SET_FLAG)))
2338
+ return 0;
2339
+ if (property->kind == XS_ACCESSOR_KIND)
2340
+ return 0;
2341
+ if ((slot->kind != XS_UNINITIALIZED_KIND) && !fxIsSameValue(the, property, slot, 0))
2342
+ return 0;
2343
+ return 1;
2344
+ }
2345
+ return 0;
2346
+ }
2347
+
2348
+ txBoolean fxModuleDeleteProperty(txMachine* the, txSlot* instance, txID id, txIndex index)
2349
+ {
2350
+ if (fxIsKeySymbol(the, id)) {
2351
+ return (id == mxID(_Symbol_toStringTag)) ? 0 : 1;
2352
+ }
2353
+ // if (mxModuleInstanceStatus(instance) < XS_MODULE_STATUS_LINKED) {
2354
+ // mxReferenceError("module not initialized yet");
2355
+ // }
2356
+ // else
2357
+ {
2358
+ txSlot* exports = mxModuleInstanceExports(instance);
2359
+ if (mxIsReference(exports)) {
2360
+ txSlot* property = exports->value.reference->next;
2361
+ while (property) {
2362
+ if (property->ID == id)
2363
+ return 0;
2364
+ property = property->next;
2365
+ }
2366
+ }
2367
+ }
2368
+ return 1;
2369
+ }
2370
+
2371
+ txSlot* fxModuleFindProperty(txMachine* the, txSlot* instance, txID id, txIndex index)
2372
+ {
2373
+ if (fxIsKeySymbol(the, id)) {
2374
+ if (id == mxID(_Symbol_toStringTag))
2375
+ return mxModulePrototype.value.reference->next;
2376
+ }
2377
+ // if (mxModuleInstanceStatus(instance) < XS_MODULE_STATUS_LINKED) {
2378
+ // mxReferenceError("module not initialized yet");
2379
+ // }
2380
+ // else
2381
+ {
2382
+ txSlot* exports = mxModuleInstanceExports(instance);
2383
+ if (mxIsReference(exports)) {
2384
+ txSlot* property = exports->value.reference->next;
2385
+ while (property) {
2386
+ if (property->ID == id) {
2387
+ property = property->value.export.closure;
2388
+ if (property && (property->kind == XS_UNINITIALIZED_KIND)) {
2389
+ fxIDToString(the, id, the->nameBuffer, sizeof(the->nameBuffer));
2390
+ mxReferenceError("get %s: not initialized yet", the->nameBuffer);
2391
+ }
2392
+ return property;
2393
+ }
2394
+ property = property->next;
2395
+ }
2396
+ }
2397
+ }
2398
+ return C_NULL;
2399
+ }
2400
+
2401
+ txBoolean fxModuleGetOwnProperty(txMachine* the, txSlot* instance, txID id, txIndex index, txSlot* slot)
2402
+ {
2403
+ txSlot* property = fxModuleFindProperty(the, instance, id, index);
2404
+ if (property) {
2405
+ slot->flag = (id == mxID(_Symbol_toStringTag)) ? property->flag : XS_DONT_DELETE_FLAG;
2406
+ slot->kind = property->kind;
2407
+ slot->value = property->value;
2408
+ return 1;
2409
+ }
2410
+ slot->flag = XS_NO_FLAG;
2411
+ slot->kind = XS_UNDEFINED_KIND;
2412
+ return 0;
2413
+ }
2414
+
2415
+ txSlot* fxModuleGetProperty(txMachine* the, txSlot* instance, txID id, txIndex index, txFlag flag)
2416
+ {
2417
+ if (fxIsKeySymbol(the, id)) {
2418
+ if (id == mxID(_Symbol_toStringTag))
2419
+ return mxModulePrototype.value.reference->next;
2420
+ }
2421
+ // if (mxModuleInstanceStatus(instance) < XS_MODULE_STATUS_LINKED) {
2422
+ // return &mxModuleAccessor;
2423
+ // }
2424
+ // else
2425
+ {
2426
+ txSlot* exports = mxModuleInstanceExports(instance);
2427
+ if (mxIsReference(exports)) {
2428
+ txSlot* property = exports->value.reference->next;
2429
+ while (property) {
2430
+ if (property->ID == id) {
2431
+ property = property->value.export.closure;
2432
+ if (property && (property->kind == XS_UNINITIALIZED_KIND)) {
2433
+ return &mxModuleAccessor;
2434
+ }
2435
+ return property;
2436
+ }
2437
+ property = property->next;
2438
+ }
2439
+ }
2440
+ }
2441
+ return C_NULL;
2442
+ }
2443
+
2444
+ txBoolean fxModuleGetPropertyValue(txMachine* the, txSlot* instance, txID id, txIndex index, txSlot* receiver, txSlot* value)
2445
+ {
2446
+ txSlot* property = fxModuleFindProperty(the, instance, id, index);
2447
+ if (property) {
2448
+ value->kind = property->kind;
2449
+ value->value = property->value;
2450
+ return 1;
2451
+ }
2452
+ value->kind = XS_UNDEFINED_KIND;
2453
+ return 0;
2454
+ }
2455
+
2456
+ txBoolean fxModuleGetPrototype(txMachine* the, txSlot* instance, txSlot* result)
2457
+ {
2458
+ result->kind = XS_NULL_KIND;
2459
+ return 0;
2460
+ }
2461
+
2462
+ txBoolean fxModuleHasProperty(txMachine* the, txSlot* instance, txID id, txIndex index)
2463
+ {
2464
+ if (fxIsKeySymbol(the, id)) {
2465
+ return (id == mxID(_Symbol_toStringTag)) ? 1 : 0;
2466
+ }
2467
+ // if (mxModuleInstanceStatus(instance) < XS_MODULE_STATUS_LINKED) {
2468
+ // mxReferenceError("module not initialized yet");
2469
+ // }
2470
+ // else
2471
+ {
2472
+ txSlot* exports = mxModuleInstanceExports(instance);
2473
+ if (mxIsReference(exports)) {
2474
+ txSlot* property = exports->value.reference->next;
2475
+ while (property) {
2476
+ if (property->ID == id) {
2477
+ property = property->value.export.closure;
2478
+ return property ? 1 : 0;
2479
+ }
2480
+ property = property->next;
2481
+ }
2482
+ }
2483
+ }
2484
+ return 0;
2485
+ }
2486
+
2487
+ txBoolean fxModuleIsExtensible(txMachine* the, txSlot* instance)
2488
+ {
2489
+ return 0;
2490
+ }
2491
+
2492
+ void fxModuleOwnKeys(txMachine* the, txSlot* instance, txFlag flag, txSlot* keys)
2493
+ {
2494
+ if (flag & XS_EACH_NAME_FLAG) {
2495
+ // if (mxModuleInstanceStatus(instance) < XS_MODULE_STATUS_LINKED) {
2496
+ // mxReferenceError("module not initialized yet");
2497
+ // }
2498
+ // else
2499
+ {
2500
+ txSlot* exports = mxModuleInstanceExports(instance);
2501
+ if (mxIsReference(exports)) {
2502
+ txSlot* property = exports->value.reference->next;
2503
+ txSlot* stack = the->stack;
2504
+ while (property) {
2505
+ if (property->value.export.closure) {
2506
+ txSlot* key = fxGetKey(the, property->ID);
2507
+ mxPushSlot(key);
2508
+ the->stack->ID = key->ID;
2509
+ }
2510
+ property = property->next;
2511
+ }
2512
+ c_qsort(the->stack, stack - the->stack, sizeof(txSlot), fxModuleOwnKeysCompare);
2513
+ while (the->stack < stack) {
2514
+ keys = fxQueueKey(the, the->stack->ID, 0, keys);
2515
+ mxPop();
2516
+ }
2517
+ }
2518
+ }
2519
+ }
2520
+ if (flag & XS_EACH_SYMBOL_FLAG) {
2521
+ txSlot* property = mxModulePrototype.value.reference->next;
2522
+ keys = fxQueueKey(the, property->ID, 0, keys);
2523
+ }
2524
+ }
2525
+
2526
+ int fxModuleOwnKeysCompare(const void* p, const void* q)
2527
+ {
2528
+ return c_strcmp(((txSlot*)p)->value.key.string, ((txSlot*)q)->value.key.string);
2529
+ }
2530
+
2531
+ txBoolean fxModulePreventExtensions(txMachine* the, txSlot* instance)
2532
+ {
2533
+ return 1;
2534
+ }
2535
+
2536
+ txSlot* fxModuleSetProperty(txMachine* the, txSlot* instance, txID id, txIndex index, txFlag flag)
2537
+ {
2538
+ return C_NULL;
2539
+ }
2540
+
2541
+ txBoolean fxModuleSetPropertyValue(txMachine* the, txSlot* instance, txID id, txIndex index, txSlot* value, txSlot* receiver)
2542
+ {
2543
+ return 0;
2544
+ }
2545
+
2546
+ txBoolean fxModuleSetPrototype(txMachine* the, txSlot* instance, txSlot* prototype)
2547
+ {
2548
+ return (prototype->kind == XS_NULL_KIND) ? 1 : 0;
2549
+ }
2550
+
2551
+ /* COMPARTMENT */
2552
+
2553
+ txSlot* fxCheckCompartmentInstance(txMachine* the, txSlot* slot)
2554
+ {
2555
+ if (slot->kind == XS_REFERENCE_KIND) {
2556
+ txSlot* instance = slot->value.reference;
2557
+ if (((slot = instance->next)) && (slot->flag & XS_INTERNAL_FLAG) && (slot->kind == XS_PROGRAM_KIND)) {
2558
+ return instance;
2559
+ }
2560
+ }
2561
+ mxTypeError("this is no Compartment instance");
2562
+ return C_NULL;
2563
+ }
2564
+
2565
+ void fxPrepareCompartmentFunction(txMachine* the, txSlot* program, txSlot* instance)
2566
+ {
2567
+ txSlot* property = mxFunctionInstanceHome(instance);
2568
+ property->value.home.module = program;
2569
+ if (mxCompartmentGlobal.kind != XS_UNDEFINED_KIND) {
2570
+ instance->flag |= XS_DONT_PATCH_FLAG;
2571
+ property = property->next;
2572
+ while (property) {
2573
+ if (!(property->flag & XS_INTERNAL_FLAG))
2574
+ property->flag |= XS_DONT_DELETE_FLAG | XS_DONT_SET_FLAG;
2575
+ property = property->next;
2576
+ }
2577
+ }
2578
+ }
2579
+
2580
+ void fx_Compartment(txMachine* the)
2581
+ {
2582
+ txSlot* module = mxFunctionInstanceHome(mxFunction->value.reference)->value.home.module;
2583
+ txSlot* program = C_NULL;
2584
+ txSlot* parent;
2585
+ txSlot* realm;
2586
+ txSlot* global = C_NULL;
2587
+ txSlot* closures = C_NULL;
2588
+ txSlot* slot;
2589
+ txSlot* target;
2590
+ txSlot* own;
2591
+ txInteger id;
2592
+
2593
+ if (!module) module = mxProgram.value.reference;
2594
+ mxTry(the) {
2595
+ if (mxIsUndefined(mxTarget))
2596
+ mxTypeError("call Compartment");
2597
+
2598
+ mxPushSlot(mxTarget);
2599
+ fxGetPrototypeFromConstructor(the, &mxCompartmentPrototype);
2600
+ program = fxNewProgramInstance(the);
2601
+ mxPullSlot(mxResult);
2602
+
2603
+ // PARENT
2604
+ parent = mxModuleInstanceInternal(module)->value.module.realm;
2605
+ mxPushReference(parent);
2606
+
2607
+ // GLOBALS
2608
+ if (the->sharedMachine == C_NULL) {
2609
+ txSlot* instance;
2610
+ txSlot* property;
2611
+ mxPush(mxObjectPrototype);
2612
+ #ifdef mxLink
2613
+ global = fxNewObjectInstance(the);
2614
+ #else
2615
+ global = fxNewGlobalInstance(the);
2616
+ #endif
2617
+ slot = fxLastProperty(the, global);
2618
+ if (mxCompartmentGlobal.kind == XS_UNDEFINED_KIND) {
2619
+ for (id = XS_SYMBOL_ID_COUNT; id < _Infinity; id++)
2620
+ slot = fxNextSlotProperty(the, slot, &the->stackPrototypes[-1 - id], mxID(id), XS_DONT_ENUM_FLAG);
2621
+ for (; id < _Compartment; id++)
2622
+ slot = fxNextSlotProperty(the, slot, &the->stackPrototypes[-1 - id], mxID(id), XS_GET_ONLY);
2623
+ }
2624
+ else {
2625
+ txSlot* item = mxCompartmentGlobal.value.reference->next;
2626
+ for (id = XS_SYMBOL_ID_COUNT; id < _Infinity; id++) {
2627
+ mxPushSlot(item->value.array.address + id);
2628
+ slot = fxNextSlotProperty(the, slot, the->stack, mxID(id), XS_DONT_ENUM_FLAG);
2629
+ mxPop();
2630
+ }
2631
+ for (; id < _Compartment; id++) {
2632
+ mxPushSlot(item->value.array.address + id);
2633
+ slot = fxNextSlotProperty(the, slot, the->stack, mxID(id), XS_GET_ONLY);
2634
+ mxPop();
2635
+ }
2636
+ }
2637
+
2638
+ instance = fxBuildHostFunction(the, mxCallback(fx_Compartment), 1, mxID(_Compartment));
2639
+ instance->flag |= XS_CAN_CONSTRUCT_FLAG;
2640
+ property = fxLastProperty(the, instance);
2641
+ fxNextSlotProperty(the, property, &mxCompartmentPrototype, mxID(_prototype), XS_GET_ONLY);
2642
+ fxPrepareCompartmentFunction(the, program, instance);
2643
+ slot = fxNextSlotProperty(the, slot, the->stack, mxID(_Compartment), XS_DONT_ENUM_FLAG);
2644
+ mxPop();
2645
+
2646
+ instance = fxBuildHostFunction(the, mxCallback(fx_Function), 1, mxID(_Function));
2647
+ instance->flag |= XS_CAN_CONSTRUCT_FLAG;
2648
+ property = fxLastProperty(the, instance);
2649
+ fxNextSlotProperty(the, property, &mxFunctionPrototype, mxID(_prototype), XS_GET_ONLY);
2650
+ fxPrepareCompartmentFunction(the, program, instance);
2651
+ slot = fxNextSlotProperty(the, slot, the->stack, mxID(_Function), XS_DONT_ENUM_FLAG);
2652
+ mxPop();
2653
+
2654
+ instance = fxBuildHostFunction(the, mxCallback(fx_eval), 1, mxID(_eval));
2655
+ fxPrepareCompartmentFunction(the, program, instance);
2656
+ slot = fxNextSlotProperty(the, slot, the->stack, mxID(_eval), XS_DONT_ENUM_FLAG);
2657
+ mxPop();
2658
+ }
2659
+ else {
2660
+ mxPush(mxCompartmentGlobal);
2661
+ global = fxNewObjectInstance(the);
2662
+ slot = fxLastProperty(the, global);
2663
+ id = _Compartment;
2664
+ for (; id < XS_INTRINSICS_COUNT; id++) {
2665
+ txSlot* instance = fxDuplicateInstance(the, the->stackPrototypes[-1 - id].value.reference);
2666
+ mxFunctionInstanceHome(instance)->value.home.module = program;
2667
+ slot = fxNextSlotProperty(the, slot, the->stack, mxID(id), XS_DONT_ENUM_FLAG);
2668
+ mxPop();
2669
+ }
2670
+ }
2671
+ slot = fxNextSlotProperty(the, slot, the->stack, mxID(_global), XS_DONT_ENUM_FLAG);
2672
+ slot = fxNextSlotProperty(the, slot, the->stack, mxID(_globalThis), XS_DONT_ENUM_FLAG);
2673
+
2674
+ if (mxArgc > 0) {
2675
+ if (!mxIsReference(mxArgv(0)))
2676
+ mxTypeError("invalid options");
2677
+
2678
+ mxPushSlot(mxArgv(0));
2679
+ if (mxHasID(fxID(the, "globals"))) {
2680
+ mxPushSlot(mxArgv(0));
2681
+ mxGetID(fxID(the, "globals"));
2682
+ slot = the->stack;
2683
+ if (!mxIsReference(slot))
2684
+ mxTypeError("options.globals is no object");
2685
+ mxPushUndefined();
2686
+ mxPush(mxAssignObjectFunction);
2687
+ mxCall();
2688
+ mxPushReference(global);
2689
+ mxPushSlot(slot);
2690
+ mxRunCount(2);
2691
+ mxPop();
2692
+ mxPop(); // globals
2693
+ }
2694
+
2695
+ target = fxNewInstance(the);
2696
+ own = fxNewInstance(the);
2697
+ mxPushSlot(mxArgv(0));
2698
+ if (mxHasID(fxID(the, "modules"))) {
2699
+ txSlot* source;
2700
+ txSlot* at;
2701
+ txSlot* temporary;
2702
+ mxPushSlot(mxArgv(0));
2703
+ mxGetID(fxID(the, "modules"));
2704
+ if (!mxIsReference(the->stack))
2705
+ mxTypeError("options.modules is no object");
2706
+ source = the->stack->value.reference;
2707
+ at = fxNewInstance(the);
2708
+ mxBehaviorOwnKeys(the, source, XS_EACH_NAME_FLAG, at);
2709
+ mxTemporary(temporary);
2710
+ while ((at = at->next)) {
2711
+ if (mxBehaviorGetOwnProperty(the, source, at->value.at.id, at->value.at.index, temporary) && !(temporary->flag & XS_DONT_ENUM_FLAG)) {
2712
+ txSlot* descriptor;
2713
+ txSlot* property;
2714
+ mxPushReference(source);
2715
+ mxGetAll(at->value.at.id, at->value.at.index);
2716
+ descriptor = the->stack;
2717
+ property = mxBehaviorSetProperty(the, target, at->value.at.id, at->value.at.index, XS_OWN);
2718
+ property->kind = descriptor->kind;
2719
+ property->value = descriptor->value;
2720
+ mxPop(); // descriptor
2721
+ }
2722
+ }
2723
+ mxPop(); // temporary
2724
+ mxPop(); // at
2725
+ mxPop(); // modules
2726
+ }
2727
+
2728
+ mxPushUndefined();
2729
+ closures = fxNewEnvironmentInstance(the, C_NULL);
2730
+ mxPushSlot(mxArgv(0));
2731
+ if (mxHasID(fxID(the, "globalLexicals"))) {
2732
+ txSlot* target = fxLastProperty(the, closures);
2733
+ txSlot* source;
2734
+ txSlot* at;
2735
+ txSlot* property;
2736
+ mxPushSlot(mxArgv(0));
2737
+ mxGetID(fxID(the, "globalLexicals"));
2738
+ if (!mxIsReference(the->stack))
2739
+ mxTypeError("options.globalLexicals is no object");
2740
+ source = the->stack->value.reference;
2741
+ at = fxNewInstance(the);
2742
+ mxBehaviorOwnKeys(the, source, XS_EACH_NAME_FLAG, at);
2743
+ mxTemporary(property);
2744
+ while ((at = at->next)) {
2745
+ if ((at->value.at.id != XS_NO_ID) && mxBehaviorGetOwnProperty(the, source, at->value.at.id, at->value.at.index, property) && !(property->flag & XS_DONT_ENUM_FLAG)) {
2746
+ mxPushReference(source);
2747
+ mxGetAll(at->value.at.id, at->value.at.index);
2748
+ target = target->next = fxNewSlot(the);
2749
+ target->value.closure = fxNewSlot(the);;
2750
+ target->kind = XS_CLOSURE_KIND;
2751
+ target->ID = at->value.at.id;
2752
+ if (property->kind == XS_ACCESSOR_KIND)
2753
+ target->value.closure->flag = property->value.accessor.setter ? XS_NO_FLAG : XS_DONT_SET_FLAG;
2754
+ else
2755
+ target->value.closure->flag = property->flag & XS_DONT_SET_FLAG;
2756
+ target->value.closure->kind = the->stack->kind;
2757
+ target->value.closure->value = the->stack->value;
2758
+ mxPop();
2759
+ }
2760
+ }
2761
+ mxPop(); // property
2762
+ mxPop(); // at
2763
+ mxPop(); // globalLexicals
2764
+ }
2765
+
2766
+ mxPushSlot(mxArgv(0));
2767
+ if (mxHasID(fxID(the, "resolveHook"))) {
2768
+ mxPushSlot(mxArgv(0));
2769
+ mxGetID(fxID(the, "resolveHook"));
2770
+ if (!fxIsCallable(the, the->stack))
2771
+ mxTypeError("options.resolveHook is no function");
2772
+ }
2773
+ else
2774
+ mxPushUndefined();
2775
+
2776
+ mxPushUndefined(); // moduleMapHook;
2777
+
2778
+ mxPushSlot(mxArgv(0));
2779
+ if (mxHasID(fxID(the, "loadHook"))) {
2780
+ mxPushSlot(mxArgv(0));
2781
+ mxGetID(fxID(the, "loadHook"));
2782
+ if (!fxIsCallable(the, the->stack))
2783
+ mxTypeError("options.loadHook is no function");
2784
+ }
2785
+ else
2786
+ mxPushUndefined();
2787
+
2788
+ mxPushSlot(mxArgv(0));
2789
+ if (mxHasID(fxID(the, "loadNowHook"))) {
2790
+ mxPushSlot(mxArgv(0));
2791
+ mxGetID(fxID(the, "loadNowHook"));
2792
+ if (!fxIsCallable(the, the->stack))
2793
+ mxTypeError("options.loadNowHook is no function");
2794
+ }
2795
+ else
2796
+ mxPushUndefined();
2797
+
2798
+ mxPushUndefined(); // importMetaHook;
2799
+ }
2800
+ else {
2801
+ target = fxNewInstance(the);
2802
+ own = fxNewInstance(the);
2803
+
2804
+ mxPushUndefined();
2805
+ closures = fxNewEnvironmentInstance(the, C_NULL);
2806
+
2807
+ mxPushUndefined();
2808
+ mxPushUndefined();
2809
+ mxPushUndefined();
2810
+ mxPushUndefined();
2811
+ mxPushUndefined();
2812
+ }
2813
+
2814
+ realm = fxNewRealmInstance(the);
2815
+ mxModuleInstanceInternal(program)->value.module.realm = realm;
2816
+
2817
+ slot = own->next;
2818
+ while (slot) {
2819
+ txSlot* internal = mxModuleInternal(slot);
2820
+ if (internal->value.module.realm == C_NULL) {
2821
+ if (!(internal->flag & XS_MARK_FLAG))
2822
+ internal->value.module.realm = realm;
2823
+ }
2824
+ slot = slot->next;
2825
+ }
2826
+
2827
+
2828
+ mxPop();
2829
+ }
2830
+ mxCatch(the) {
2831
+ fxJump(the);
2832
+ }
2833
+ }
2834
+
2835
+ void fx_Compartment_prototype_get_globalThis(txMachine* the)
2836
+ {
2837
+ txSlot* program = fxCheckCompartmentInstance(the, mxThis);
2838
+ txSlot* realm = mxModuleInstanceInternal(program)->value.module.realm;
2839
+ txSlot* global = mxRealmGlobal(realm);
2840
+ mxResult->kind = global->kind;
2841
+ mxResult->value = global->value;
2842
+ }
2843
+
2844
+ void fx_Compartment_prototype_evaluate(txMachine* the)
2845
+ {
2846
+ txSlot* program = fxCheckCompartmentInstance(the, mxThis);
2847
+ txSlot* realm = mxModuleInstanceInternal(program)->value.module.realm;
2848
+ if (mxArgc > 0) {
2849
+ txStringStream stream;
2850
+ stream.slot = mxArgv(0);
2851
+ stream.offset = 0;
2852
+ stream.size = mxStringLength(fxToString(the, mxArgv(0)));
2853
+ fxRunScript(the, fxParseScript(the, &stream, fxStringGetter, mxStrictFlag | mxProgramFlag | mxDebugFlag), mxRealmGlobal(realm), C_NULL, mxRealmClosures(realm)->value.reference, C_NULL, program);
2854
+ mxPullSlot(mxResult);
2855
+ }
2856
+ }
2857
+
2858
+ void fx_Compartment_prototype_import(txMachine* the)
2859
+ {
2860
+ txSlot* program = fxCheckCompartmentInstance(the, mxThis);
2861
+ txSlot* realm = mxModuleInstanceInternal(program)->value.module.realm;
2862
+ if (mxArgc > 0)
2863
+ mxPushSlot(mxArgv(0));
2864
+ else
2865
+ mxPushUndefined();
2866
+ gxDefaults.runImport(the, realm, XS_NO_ID);
2867
+ mxPullSlot(mxResult);
2868
+ }
2869
+
2870
+ void fx_Compartment_prototype_importNow(txMachine* the)
2871
+ {
2872
+ txSlot* program = fxCheckCompartmentInstance(the, mxThis);
2873
+ txSlot* realm = mxModuleInstanceInternal(program)->value.module.realm;
2874
+ if (mxArgc > 0)
2875
+ mxPushSlot(mxArgv(0));
2876
+ else
2877
+ mxPushUndefined();
2878
+ fxRunImportNow(the, realm, XS_NO_ID);
2879
+ mxPullSlot(mxResult);
2880
+ }
2881
+
2882
+ txSlot* fxCheckModuleSourceInstance(txMachine* the, txSlot* slot)
2883
+ {
2884
+ if (slot->kind == XS_REFERENCE_KIND) {
2885
+ txSlot* instance = slot->value.reference;
2886
+ if (((slot = instance->next)) && (slot->flag & XS_INTERNAL_FLAG) && (slot->kind == XS_MODULE_SOURCE_KIND)) {
2887
+ return instance;
2888
+ }
2889
+ }
2890
+ mxTypeError("this is no ModuleSource instance");
2891
+ return C_NULL;
2892
+ }
2893
+
2894
+ txSlot* fxNewModuleSourceInstance(txMachine* the)
2895
+ {
2896
+ txSlot* instance;
2897
+ txSlot* slot;
2898
+ txSlot* meta;
2899
+ instance = fxNewSlot(the);
2900
+ instance->kind = XS_INSTANCE_KIND;
2901
+ instance->value.instance.garbage = C_NULL;
2902
+ instance->value.instance.prototype = the->stack->value.reference;
2903
+ the->stack->kind = XS_REFERENCE_KIND;
2904
+ the->stack->value.reference = instance;
2905
+ /* HOST */
2906
+ slot = instance->next = fxNewSlot(the);
2907
+ slot->flag = XS_INTERNAL_FLAG;
2908
+ slot->kind = XS_MODULE_SOURCE_KIND;
2909
+ slot->value.module.realm = C_NULL;
2910
+ slot->value.module.id = XS_NO_ID;
2911
+ /* EXPORTS */
2912
+ slot = fxNextNullProperty(the, slot, XS_NO_ID, XS_INTERNAL_FLAG);
2913
+ /* META */
2914
+ meta = fxNewInstance(the);
2915
+ slot = fxNextReferenceProperty(the, slot, meta, XS_NO_ID, XS_INTERNAL_FLAG);
2916
+ mxPop();
2917
+ /* TRANSFERS */
2918
+ slot = fxNextNullProperty(the, slot, XS_NO_ID, XS_INTERNAL_FLAG);
2919
+ /* INITIALIZE */
2920
+ slot = fxNextNullProperty(the, slot, XS_NO_ID, XS_INTERNAL_FLAG);
2921
+ /* FUNCTION */
2922
+ slot = fxNextNullProperty(the, slot, XS_NO_ID, XS_INTERNAL_FLAG);
2923
+ /* HOSTS */
2924
+ slot = fxNextNullProperty(the, slot, XS_NO_ID, XS_INTERNAL_FLAG);
2925
+ /* LOADER */
2926
+ slot = fxNextNullProperty(the, slot, XS_NO_ID, XS_INTERNAL_FLAG);
2927
+ return instance;
2928
+ }
2929
+
2930
+ void fxExecuteVirtualModuleSourceImport(txMachine* the)
2931
+ {
2932
+ txSlot* instance = mxFunction->value.reference;
2933
+ txSlot* home = mxFunctionInstanceHome(instance);
2934
+ txSlot* module = home->value.home.module;
2935
+ txSlot* internal = mxModuleInstanceInternal(module);
2936
+ mxPushSlot(mxArgv(0));
2937
+ fxRunImport(the, internal->value.module.realm, internal->value.module.id);
2938
+ mxPullSlot(mxResult);
2939
+ }
2940
+
2941
+ void fx_ModuleSource(txMachine* the)
2942
+ {
2943
+ txSlot* instance;
2944
+ txSlot* slot;
2945
+ txStringStream stream;
2946
+ txScript* script;
2947
+ if (mxIsUndefined(mxTarget))
2948
+ mxTypeError("call: ModuleSource");
2949
+ mxPushSlot(mxTarget);
2950
+ fxGetPrototypeFromConstructor(the, &mxModuleSourcePrototype);
2951
+ instance = fxNewModuleSourceInstance(the);
2952
+ mxPullSlot(mxResult);
2953
+ if (mxArgc == 0)
2954
+ mxPushUndefined();
2955
+ else
2956
+ mxPushSlot(mxArgv(0));
2957
+ slot = the->stack;
2958
+ stream.slot = slot;
2959
+ stream.offset = 0;
2960
+ stream.size = mxStringLength(fxToString(the, slot));
2961
+ script = fxParseScript(the, &stream, fxStringGetter, mxDebugFlag);
2962
+ // mxPushClosure(mxResult);
2963
+ fxRunScript(the, script, mxResult, C_NULL, C_NULL, C_NULL, instance);
2964
+ mxPop();
2965
+ if (mxModuleInstanceExecute(instance)->kind == XS_NULL_KIND)
2966
+ mxTypeError("no module");
2967
+ mxPop();
2968
+ mxPop();
2969
+ }
2970
+
2971
+ void fx_ModuleSource_prototype_get_bindings(txMachine* the)
2972
+ {
2973
+ txSlot* record = fxCheckModuleSourceInstance(the, mxThis);
2974
+ txSlot* resultInstance;
2975
+ txSlot* resultArray;
2976
+ txSlot* resultItem;
2977
+ txSlot* transfer;
2978
+
2979
+ mxPush(mxArrayPrototype);
2980
+ resultInstance = fxNewArrayInstance(the);
2981
+ mxPullSlot(mxResult);
2982
+ resultArray = resultInstance->next;
2983
+ resultItem = resultArray;
2984
+
2985
+ transfer = mxModuleInstanceTransfers(record)->value.reference->next;
2986
+ while (transfer) {
2987
+ txSlot* local = mxTransferLocal(transfer);
2988
+ txSlot* from = mxTransferFrom(transfer);
2989
+ txSlot* import = mxTransferImport(transfer);
2990
+ txSlot* aliases = mxTransferAliases(transfer);
2991
+
2992
+ if (local->kind != XS_NULL_KIND) {
2993
+ if (from->kind != XS_NULL_KIND) {
2994
+ txSlot* slot = fxNewObject(the);
2995
+ if (import->kind == XS_NULL_KIND) {
2996
+ mxPushString(from->value.string);
2997
+ slot = fxNextSlotProperty(the, slot, the->stack, fxID(the, "importAllFrom"), XS_NO_FLAG);
2998
+ mxPop();
2999
+ fxPushKeyString(the, local->value.symbol);
3000
+ slot = fxNextSlotProperty(the, slot, the->stack, mxID(_as), XS_NO_FLAG);
3001
+ mxPop();
3002
+ }
3003
+ else {
3004
+ fxPushKeyString(the, import->value.symbol);
3005
+ slot = fxNextSlotProperty(the, slot, the->stack, mxID(_import), XS_NO_FLAG);
3006
+ mxPop();
3007
+ if (local->value.symbol != import->value.symbol) {
3008
+ fxPushKeyString(the, local->value.symbol);
3009
+ slot = fxNextSlotProperty(the, slot, the->stack, mxID(_as), XS_NO_FLAG);
3010
+ mxPop();
3011
+ }
3012
+ mxPushString(from->value.string);
3013
+ slot = fxNextSlotProperty(the, slot, the->stack, mxID(_from), XS_NO_FLAG);
3014
+ mxPop();
3015
+ }
3016
+ resultItem = resultItem->next = fxNewSlot(the);
3017
+ mxPullSlot(resultItem);
3018
+ resultArray->value.array.length++;
3019
+ }
3020
+ if (aliases->kind == XS_REFERENCE_KIND) {
3021
+ txSlot* alias = aliases->value.reference->next;
3022
+ while (alias) {
3023
+ txSlot* slot = fxNewObject(the);
3024
+ fxPushKeyString(the, local->value.symbol);
3025
+ slot = fxNextSlotProperty(the, slot, the->stack, mxID(_export), XS_NO_FLAG);
3026
+ mxPop();
3027
+ if (local->value.symbol != alias->value.symbol) {
3028
+ fxPushKeyString(the, alias->value.symbol);
3029
+ slot = fxNextSlotProperty(the, slot, the->stack, mxID(_as), XS_NO_FLAG);
3030
+ mxPop();
3031
+ }
3032
+ resultItem = resultItem->next = fxNewSlot(the);
3033
+ mxPullSlot(resultItem);
3034
+ resultArray->value.array.length++;
3035
+ alias = alias->next;
3036
+ }
3037
+ }
3038
+ }
3039
+ else if (aliases->kind == XS_REFERENCE_KIND) {
3040
+ txSlot* alias = aliases->value.reference->next;
3041
+ while (alias) {
3042
+ txSlot* slot = fxNewObject(the);
3043
+ if (from->kind != XS_NULL_KIND) {
3044
+ if (import->kind == XS_NULL_KIND) {
3045
+ mxPushString(from->value.string);
3046
+ slot = fxNextSlotProperty(the, slot, the->stack, fxID(the, "exportAllFrom"), XS_NO_FLAG);
3047
+ mxPop();
3048
+ fxPushKeyString(the, alias->value.symbol);
3049
+ slot = fxNextSlotProperty(the, slot, the->stack, mxID(_as), XS_NO_FLAG);
3050
+ mxPop();
3051
+ }
3052
+ else {
3053
+ fxPushKeyString(the, import->value.symbol);
3054
+ slot = fxNextSlotProperty(the, slot, the->stack, mxID(_export), XS_NO_FLAG);
3055
+ mxPop();
3056
+ if (alias->value.symbol != import->value.symbol) {
3057
+ fxPushKeyString(the, alias->value.symbol);
3058
+ slot = fxNextSlotProperty(the, slot, the->stack, mxID(_as), XS_NO_FLAG);
3059
+ mxPop();
3060
+ }
3061
+ mxPushString(from->value.string);
3062
+ slot = fxNextSlotProperty(the, slot, the->stack, mxID(_from), XS_NO_FLAG);
3063
+ mxPop();
3064
+ }
3065
+ resultItem = resultItem->next = fxNewSlot(the);
3066
+ mxPullSlot(resultItem);
3067
+ resultArray->value.array.length++;
3068
+ }
3069
+ alias = alias->next;
3070
+ }
3071
+ }
3072
+ else if ((from->kind != XS_NULL_KIND) && (import->kind == XS_NULL_KIND)) {
3073
+ txSlot* slot = fxNewObject(the);
3074
+ mxPushString(from->value.string);
3075
+ slot = fxNextSlotProperty(the, slot, the->stack, fxID(the, "exportAllFrom"), XS_NO_FLAG);
3076
+ mxPop();
3077
+ resultItem = resultItem->next = fxNewSlot(the);
3078
+ mxPullSlot(resultItem);
3079
+ resultArray->value.array.length++;
3080
+ }
3081
+ transfer = transfer->next;
3082
+ }
3083
+
3084
+ fxCacheArray(the, resultInstance);
3085
+ }
3086
+
3087
+ void fx_ModuleSource_prototype_get_needsImport(txMachine* the)
3088
+ {
3089
+ txSlot* record = fxCheckModuleSourceInstance(the, mxThis);
3090
+ txFlag flag = mxModuleInstanceInternal(record)->flag;
3091
+ mxResult->value.boolean = (flag & XS_IMPORT_FLAG) ? 1 : 0;
3092
+ mxResult->kind = XS_BOOLEAN_KIND;
3093
+ }
3094
+
3095
+ void fx_ModuleSource_prototype_get_needsImportMeta(txMachine* the)
3096
+ {
3097
+ txSlot* record = fxCheckModuleSourceInstance(the, mxThis);
3098
+ txFlag flag = mxModuleInstanceInternal(record)->flag;
3099
+ mxResult->value.boolean = (flag & XS_IMPORT_META_FLAG) ? 1 : 0;
3100
+ mxResult->kind = XS_BOOLEAN_KIND;
3101
+ }