void fxSerializeJSON(txMachine* the, txJSONSerializer* theSerializer) { txSlot* aSlot; txInteger aFlag; aSlot = fxGetInstance(the, mxThis); theSerializer->offset = 0; theSerializer->size = 1024; theSerializer->buffer = c_malloc(1024); if (!theSerializer->buffer) mxUnknownError("out of memory"); if (mxArgc > 1) { aSlot = mxArgv(1); if (mxIsReference(aSlot)) { aSlot = fxGetInstance(the, aSlot); if (mxIsFunction(aSlot)) theSerializer->replacer = mxArgv(1); else if (mxIsArray(aSlot)) mxSyntaxError("not yet implememented"); } } if (mxArgc > 2) { aSlot = mxArgv(2); if (mxIsReference(aSlot)) { aSlot = fxGetInstance(the, aSlot); if (mxIsNumber(aSlot) || mxIsString(aSlot)) aSlot = aSlot->next; } if ((aSlot->kind == XS_INTEGER_KIND) || (aSlot->kind == XS_NUMBER_KIND)) { txInteger aCount = fxToInteger(the, aSlot), anIndex; if (aCount < 0) aCount = 0; else if (aCount > 10) aCount = 10; for (anIndex = 0; anIndex < aCount; anIndex++) theSerializer->indent[anIndex] = ' '; } else if (mxIsStringPrimitive(aSlot)) c_strncpy((char *)theSerializer->indent, aSlot->value.string, 10); } theSerializer->stack = the->stack; mxPush(mxObjectPrototype); fxNewObjectInstance(the); aFlag = 0; if (mxArgc > 0) mxPushSlot(mxArgv(0)); else mxPushUndefined(); mxPush(mxEmptyString); fxSerializeJSONProperty(the, theSerializer, &aFlag); the->stack++; }
void fxSerializeJSONChar(txMachine* the, txJSONSerializer* theSerializer, char c) { //fprintf(stderr, "%c", c); if (theSerializer->offset == theSerializer->size) { char* aBuffer; theSerializer->size += 1024; aBuffer = c_realloc(theSerializer->buffer, theSerializer->size); if (!aBuffer) mxUnknownError("out of memory"); theSerializer->buffer = aBuffer; } theSerializer->buffer[theSerializer->offset] = c; theSerializer->offset++; }
void fxSerializeJSONChars(txMachine* the, txJSONSerializer* theSerializer, char* s) { //fprintf(stderr, "%s", s); txSize aSize = c_strlen(s); if ((theSerializer->offset + aSize) >= theSerializer->size) { char* aBuffer; theSerializer->size += ((aSize / 1024) + 1) * 1024; aBuffer = c_realloc(theSerializer->buffer, theSerializer->size); if (!aBuffer) mxUnknownError("out of memory"); theSerializer->buffer = aBuffer; } c_memcpy(theSerializer->buffer + theSerializer->offset, s, aSize); theSerializer->offset += aSize; }
void fx_JSON_parse(txMachine* the) { volatile txJSONParser* aParser = C_NULL; txSlot* slot; mxTry(the) { if (mxArgc < 1) mxSyntaxError("no buffer"); aParser = c_malloc(sizeof(txJSONParser)); if (NULL == aParser) mxUnknownError("out of memory"); c_memset((txJSONParser*)aParser, 0, sizeof(txJSONParser)); if (mxArgc > 1) { slot = mxArgv(1); if (slot->kind == XS_REFERENCE_KIND) { slot = slot->value.reference; if (slot->next && ((slot->next->kind == XS_CODE_KIND) || (slot->next->kind == XS_CODE_X_KIND) || (slot->next->kind == XS_CALLBACK_KIND))) aParser->reviver = slot; } } slot = mxArgv(0); if (slot->kind == XS_REFERENCE_KIND) { slot = slot->value.reference->next; if (slot && (slot->flag & XS_INTERNAL_FLAG) && (slot->kind == XS_HOST_KIND)) { aParser->data = slot->value.host.data; aParser->offset = 0; mxPushSlot(mxArgv(0)); fxGetID(the, mxID(_length)); aParser->size = fxToInteger(the, the->stack++); } } if (!aParser->data) { fxToString(the, mxArgv(0)); aParser->slot = mxArgv(0); aParser->offset = 0; aParser->size = c_strlen(aParser->slot->value.string); } fxParseJSON(the, (txJSONParser*)aParser); mxPullSlot(mxResult); c_free((txJSONParser*)aParser); // @@ reviver } mxCatch(the) { if (aParser) c_free((txJSONParser*)aParser); fxJump(the); } }
void fx_Symbol_for(txMachine* the) { txString string; txU1* p; txU4 sum; txU4 modulo; txSlot* result; txID index; if (mxArgc < 1) mxSyntaxError("no key parameter"); string = fxToString(the, mxArgv(0)); p = (txU1*)string; sum = 0; while(*p != 0) { sum = (sum << 1) + *p++; } sum &= 0x7FFFFFFF; modulo = sum % the->symbolModulo; result = the->symbolTable[modulo]; while (result != C_NULL) { if (result->value.key.sum == sum) if (c_strcmp(result->value.key.string, string) == 0) break; result = result->next; } if (result == C_NULL) { index = the->keyIndex; if (index == the->keyCount) mxUnknownError("not enough IDs"); result = fxNewSlot(the); result->next = the->symbolTable[modulo]; result->kind = XS_KEY_KIND; result->ID = 0x8000 | index; result->value.key.string = mxArgv(0)->value.string; result->value.key.sum = sum; the->keyArray[index] = result; the->keyIndex++; the->symbolTable[modulo] = result; } mxResult->kind = XS_SYMBOL_KIND; mxResult->value.ID = result->ID; }
txSlot* fxNewNameC(txMachine* the, txString theString) { txU1* string; txU4 sum; txU4 modulo; txSlot* result; txID index; string = (txU1*)theString; sum = 0; while(*string != 0) { sum = (sum << 1) + *string++; } sum &= 0x7FFFFFFF; modulo = sum % the->nameModulo; result = the->nameTable[modulo]; while (result != C_NULL) { if (result->value.key.sum == sum) if (c_strcmp(result->value.key.string, theString) == 0) break; result = result->next; } if (result == C_NULL) { index = the->keyIndex; if (index == the->keyCount) mxUnknownError("not enough IDs"); result = fxNewSlot(the); result->next = the->nameTable[modulo]; result->flag = XS_DONT_ENUM_FLAG; result->kind = XS_KEY_KIND; result->ID = 0x8000 | index; result->value.key.string = C_NULL; result->value.key.sum = sum; the->keyArray[index] = result; the->keyIndex++; the->nameTable[modulo] = result; result->value.key.string = (txString)fxNewChunk(the, c_strlen(theString) + 1); c_strcpy(result->value.key.string, theString); } return result; }
void fx_Symbol(txMachine* the) { txID id = the->keyIndex; txSlot* description; if (mxTarget->kind != XS_UNDEFINED_KIND) mxTypeError("new Symbol"); if (id == the->keyCount) mxUnknownError("not enough IDs"); if (mxArgc > 0) { fxToString(the, mxArgv(0)); description = fxNewSlot(the); description->kind = XS_STRING_KIND; description->value.string = mxArgv(0)->value.string; } else description = C_NULL; the->keyArray[id] = description; the->keyIndex++; mxResult->kind = XS_SYMBOL_KIND; mxResult->value.ID = 0x8000 | id; }