void fxParseJSONObject(txMachine* the, txJSONParser* theParser) { txSlot* anObject; txSlot* at; txSlot* aProperty; fxGetNextJSONToken(the, theParser); mxPush(mxObjectPrototype); anObject = fxNewObjectInstance(the); for (;;) { if (theParser->token == XS_JSON_TOKEN_RIGHT_BRACE) break; if (theParser->token != XS_JSON_TOKEN_STRING) { mxSyntaxError("missing name"); break; } mxPushString(theParser->string->value.string); at = fxAt(the, the->stack); if (theParser->reviver) mxPushString(theParser->string->value.string); fxGetNextJSONToken(the, theParser); if (theParser->token != XS_JSON_TOKEN_COLON) { mxSyntaxError("missing :"); break; } fxGetNextJSONToken(the, theParser); fxParseJSONValue(the, theParser); if (theParser->reviver) { mxPushInteger(2); mxPushReference(anObject); mxPushReference(theParser->reviver); fxCall(the); } if (the->stack->kind != XS_UNDEFINED_KIND) { aProperty = fxSetProperty(the, anObject, at->value.at.id, at->value.at.index, XS_OWN); aProperty->kind = the->stack->kind; aProperty->value = the->stack->value; } the->stack++; the->stack++; if (theParser->token != XS_JSON_TOKEN_COMMA) break; fxGetNextJSONToken(the, theParser); } if (theParser->token != XS_JSON_TOKEN_RIGHT_BRACE) mxSyntaxError("missing }"); fxGetNextJSONToken(the, theParser); }
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); } }
void fxToPrimitive(txMachine* the, txSlot* theSlot, txInteger theHint) { if (theSlot->kind == XS_REFERENCE_KIND) { fxBeginHost(the); if (theHint == XS_NO_HINT) mxPushString(mxDefaultString.value.string); else if (theHint == XS_NUMBER_HINT) mxPushString(mxNumberString.value.string); else mxPushString(mxStringString.value.string); mxPushInteger(1); mxPushSlot(theSlot); fxCallID(the, mxID(_Symbol_toPrimitive)); theSlot->kind = the->stack->kind; theSlot->value = the->stack->value; the->stack++; fxEndHost(the); } }
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 fxParseJSONValue(txMachine* the, txJSONParser* theParser) { switch (theParser->token) { case XS_JSON_TOKEN_FALSE: mxPushBoolean(0); fxGetNextJSONToken(the, theParser); break; case XS_JSON_TOKEN_TRUE: mxPushBoolean(1); fxGetNextJSONToken(the, theParser); break; case XS_JSON_TOKEN_NULL: mxPushNull(); fxGetNextJSONToken(the, theParser); break; case XS_JSON_TOKEN_INTEGER: mxPushInteger(theParser->integer); fxGetNextJSONToken(the, theParser); break; case XS_JSON_TOKEN_NUMBER: mxPushNumber(theParser->number); fxGetNextJSONToken(the, theParser); break; case XS_JSON_TOKEN_STRING: mxPushString(theParser->string->value.string); fxGetNextJSONToken(the, theParser); break; case XS_JSON_TOKEN_LEFT_BRACE: fxParseJSONObject(the, theParser); break; case XS_JSON_TOKEN_LEFT_BRACKET: fxParseJSONArray(the, theParser); break; default: mxPushUndefined(); mxSyntaxError("invalid value"); break; } }