void fxLoadModule(txMachine* the, txID moduleID) { txArchive* archive = the->archive; txSlot* key = fxGetKey(the, moduleID); char buffer[PATH_MAX]; txString path = buffer; txString dot; txString* extension; txLoader* loader; c_strcpy(path, key->value.key.string); if (archive) { if (!c_strncmp(path, archive->base, archive->baseLength)) { txInteger c = archive->scriptCount, i; txScript* script = archive->scripts; path += archive->baseLength; for (i = 0; i < c; i++) { if (!c_strcmp(path, script->path)) { fxResolveModule(the, moduleID, script, fxLoadLibrary(the, key->value.key.string, C_NULL), fxUnloadLibrary); return; } script++; } } } dot = c_strrchr(path, '.'); for (extension = gxExtensions, loader = gxLoaders; *extension; extension++, loader++) { if (!c_strcmp(dot, *extension)) { (**loader)(the, buffer, moduleID); return; } } }
void fxSerializeJSONOwnProperty(txMachine* the, txSlot* theContext, txID id, txIndex index, txSlot* theProperty) { txJSONSerializer* theSerializer = theContext->value.regexp.code; txInteger* theFlag = theContext->value.regexp.offsets; if (id) { txSlot* key = fxGetKey(the, id); if (key && (key->flag & XS_DONT_ENUM_FLAG)) { if (key->kind == XS_KEY_KIND) { mxPushSlot(theProperty); mxPushString(key->value.key.string); fxSerializeJSONProperty(the, theSerializer, theFlag); } else { mxPushSlot(theProperty); mxPushStringX(key->value.key.string); fxSerializeJSONProperty(the, theSerializer, theFlag); } } } else { mxPushSlot(theProperty); mxPushInteger((txInteger)index); fxSerializeJSONProperty(the, theSerializer, theFlag); } }
txString fxName(txMachine* the, txID theID) { txSlot* aKey = fxGetKey(the, theID); if (aKey) return aKey->value.key.string; return C_NULL; }
void fxRunForIn(txMachine* the) { txSlot* limit = the->stack; txSlot* slot = fxToInstance(the, limit); while (slot) { fxEachInstanceProperty(the, slot, XS_EACH_ENUMERABLE_FLAG | XS_EACH_STRING_FLAG, fxRunForInProperty, limit, slot); slot = fxGetParent(the, slot); } slot = the->stack; while (slot < limit) { txInteger id = slot->value.integer; if (id < 0) { txSlot* key = fxGetKey(the, (txID)id); if (key && (key->flag & XS_DONT_ENUM_FLAG)) { if (key->kind == XS_KEY_KIND) { slot->kind = XS_STRING_KIND; slot->value.string = key->value.key.string; } else { slot->kind = XS_STRING_X_KIND; slot->value.string = key->value.key.string; } } else { slot->kind = XS_SYMBOL_KIND; slot->value.ID = (txID)id; } } slot++; } limit->kind = XS_NULL_KIND; }
void fxSymbolToString(txMachine* the, txSlot* slot) { txSlot* key = fxGetKey(the, slot->value.ID); fxCopyStringC(the, slot, "Symbol("); if (key) fxConcatStringC(the, slot, key->value.string); fxConcatStringC(the, slot, ")"); }
void fx_Symbol_keyFor(txMachine* the) { txSlot* slot; txSlot* key; if (mxArgc < 1) mxSyntaxError("no sym parameter"); slot = fxCheckSymbol(the, mxArgv(0)); if (!slot) mxTypeError("sym is no symbol"); key = fxGetKey(the, slot->value.ID); if (key && ((key->flag & XS_DONT_ENUM_FLAG) == 0)) { mxResult->kind = XS_STRING_KIND; mxResult->value.string = key->value.key.string; } }
void fxModuleURL(txMachine* the) { txID id = fxCurrentModuleID(the); if (id != XS_NO_ID) { txSlot* key = fxGetKey(the, id); if (key->kind == XS_KEY_KIND) mxPushString(key->value.key.string); else mxPushStringX(key->value.key.string); } else { mxPushUndefined(); } }
void fxLoadModule(txMachine* the, txID moduleID) { txArchive* archive = the->archive; txSlot* key = fxGetKey(the, moduleID); txString path = NULL; char buffer[PATH_MAX]; txString dot = NULL; txString* extension; txLoader* loader; KprURLToPath(key->value.key.string, &path); c_strcpy(buffer, path); c_free(path); path = buffer; if (archive) { if (!c_strncmp(path, archive->base, archive->baseLength)) { txInteger c = archive->scriptCount, i; txScript* script = archive->scripts; path += archive->baseLength; #if mxWindows { char* separator = path; while (*separator) { if (*separator == '/') *separator = mxSeparator; separator++; } } #endif for (i = 0; i < c; i++) { if (!c_strcmp(path, script->path)) { fxResolveModule(the, moduleID, script, C_NULL, C_NULL); return; } script++; } } } dot = FskStrRChr(path, '.'); for (extension = gxExtensions, loader = gxLoaders; *extension; extension++, loader++) { if (!FskStrCompare(dot, *extension)) { (**loader)(the, buffer, moduleID); break; } } }
void fx_Function_prototype_get_name(txMachine* the) { txSlot* instance = fxCheckFunctionInstance(the, mxThis); txSlot* info = mxFunctionInstanceInfo(instance); txSlot* key = fxGetKey(the, info->value.info.name); if (key) { if (key->kind == XS_KEY_KIND) { mxResult->kind = XS_STRING_KIND; mxResult->value.string = key->value.key.string; } else if (key->kind == XS_KEY_X_KIND) { mxResult->kind = XS_STRING_X_KIND; mxResult->value.string = key->value.key.string; } else { fxCopyStringC(the, mxResult, "["); fxConcatStringC(the, mxResult, key->value.string); fxConcatStringC(the, mxResult, "]"); } return; } *mxResult = mxEmptyString; }
void fxIDToSlot(txMachine* the, txInteger id, txSlot* slot) { if (id < 0) { txSlot* key = fxGetKey(the, (txID)id); if (key && (key->flag & XS_DONT_ENUM_FLAG)) { if (key->kind == XS_KEY_KIND) { slot->kind = XS_STRING_KIND; slot->value.string = key->value.key.string; } else { slot->kind = XS_STRING_X_KIND; slot->value.string = key->value.key.string; } } else { slot->kind = XS_SYMBOL_KIND; slot->value.ID = (txID)id; } } else { char buffer[16]; fxCopyStringC(the, slot, fxIntegerToString(the->dtoa, id, buffer, sizeof(buffer))); } }
void fxKeyAt(txMachine* the, txID id, txIndex index, txSlot* slot) { if (id) { txSlot* key = fxGetKey(the, id); if (key && (key->flag & XS_DONT_ENUM_FLAG)) { if (key->kind == XS_KEY_KIND) { slot->kind = XS_STRING_KIND; slot->value.string = key->value.key.string; } else{ slot->kind = XS_STRING_X_KIND; slot->value.string = key->value.key.string; } } else { slot->kind = XS_SYMBOL_KIND; slot->value.symbol = id; } } else { char buffer[16]; fxCopyStringC(the, slot, fxNumberToString(the->dtoa, index, buffer, sizeof(buffer), 0, 0)); } }
txID fxFindModule(txMachine* the, txID moduleID, txSlot* slot) { char name[PATH_MAX]; char base[PATH_MAX]; txBoolean absolute, relative, search; txSlot *key, *iterator, *result; txString dot, slash; txID id; fxToStringBuffer(the, slot, name, sizeof(name)); if ((!c_strncmp(name, "./", 2)) || (!c_strncmp(name, "../", 3))) { absolute = 0; relative = (moduleID == XS_NO_ID) ? 0 : 1; search = 0; } else if ((!c_strncmp(name, "/", 1))) { absolute = 1; relative = 0; search = 0; } else { absolute = 0; relative = (moduleID == XS_NO_ID) ? 0 : 1; search = 1; } slash = c_strrchr(name, '/'); if (!slash) slash = name; dot = c_strrchr(slash, '.'); if (!dot) dot = name + c_strlen(name); if (absolute) { if (fxFindURI(the, "", name, dot, &id)) return id; } if (relative) { key = fxGetKey(the, moduleID); c_strcpy(base, key->value.key.string); if (fxFindURI(the, base, name, dot, &id)) return id; } if (search) { mxCallID(&mxModulePaths, mxID(_Symbol_iterator), 0); iterator = the->stack; for (;;) { mxCallID(iterator, mxID(_next), 0); result = the->stack; mxGetID(result, mxID(_done)); if (fxToBoolean(the, the->stack)) break; the->stack++; mxGetID(result, mxID(_value)); fxToStringBuffer(the, the->stack++, base, sizeof(base)); if (fxFindURI(the, base, name, dot, &id)) return id; } } mxReferenceError("module \"%s\" not found", name); return XS_NO_ID; }
void fxEachOwnProperty(txMachine* the, txSlot* theInstance, txFlag theFlag, txStep theStep, txSlot* theContext) { txSlot* aProperty; txIndex aCount; txIndex anIndex; txFlag propertyFlag = theFlag & XS_DONT_ENUM_FLAG; if (!(theFlag & XS_DONT_ENUM_STRING_FLAG)) { aProperty = theInstance->next; if (theInstance->flag & XS_VALUE_FLAG) { if (aProperty->kind == XS_STAR_KIND) { aProperty = aProperty->next; while (aProperty) { if (!(aProperty->flag & propertyFlag)) { if (aProperty->ID != XS_NO_ID) (*theStep)(the, theContext, aProperty->ID, aProperty->value.closure); } aProperty = aProperty->next; } return; } else if ((aProperty->kind == XS_STRING_KIND) || (aProperty->kind == XS_STRING_X_KIND)) { aCount = fxUnicodeLength(aProperty->value.string); for (anIndex = 0; anIndex < aCount; anIndex++) { (*theStep)(the, theContext, anIndex, &mxEmptyString); } } } while (aProperty) { if (!(aProperty->flag & propertyFlag)) { if (aProperty->ID == XS_NO_ID) { if (aProperty->kind == XS_ARRAY_KIND) { aCount = aProperty->value.array.length; for (anIndex = 0; anIndex < aCount; anIndex++) { if ((aProperty->value.array.address + anIndex)->ID) (*theStep)(the, theContext, anIndex, aProperty->value.array.address + anIndex); } break; } } } aProperty = aProperty->next; } aProperty = theInstance->next; while (aProperty) { if (!(aProperty->flag & propertyFlag)) { if (aProperty->ID < XS_NO_ID) { txSlot* key = fxGetKey(the, aProperty->ID); if (key && (key->flag & XS_DONT_ENUM_FLAG)) { (*theStep)(the, theContext, aProperty->ID, aProperty); } } } aProperty = aProperty->next; } } if (!(theFlag & XS_DONT_ENUM_SYMBOL_FLAG)) { aProperty = theInstance->next; while (aProperty) { if (!(aProperty->flag & propertyFlag)) { if (aProperty->ID < XS_NO_ID) { txSlot* key = fxGetKey(the, aProperty->ID); if (!key || !(key->flag & XS_DONT_ENUM_FLAG)) { (*theStep)(the, theContext, aProperty->ID, aProperty); } } } aProperty = aProperty->next; } } }