Ejemplo n.º 1
0
/*
 *     Copyright (C) 2010-2015 Marvell International Ltd.
 *     Copyright (C) 2002-2010 Kinoma, Inc.
 *
 *     Licensed under the Apache License, Version 2.0 (the "License");
 *     you may not use this file except in compliance with the License.
 *     You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *     Unless required by applicable law or agreed to in writing, software
 *     distributed under the License is distributed on an "AS IS" BASIS,
 *     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *     See the License for the specific language governing permissions and
 *     limitations under the License.
 */
void fxBuildJSON(txMachine* the)
{
	txSlot* xs;
	
	mxPush(mxGlobal);
	mxPush(mxObjectPrototype);
	fxNewObjectInstance(the);
	fxNewHostFunction(the, fx_JSON_parse, 2);
	fxQueueID(the, fxID(the, "parse"), XS_DONT_ENUM_FLAG);
	fxNewHostFunction(the, fx_JSON_stringify, 3);
	fxQueueID(the, fxID(the, "stringify"), XS_DONT_ENUM_FLAG);
	fxQueueID(the, fxID(the, "JSON"), XS_DONT_DELETE_FLAG | XS_DONT_ENUM_FLAG | XS_DONT_SET_FLAG);
	the->stack++;
	
	xs = fxGetProperty(the, mxGlobal.value.reference, fxID(the, "xs"));
	mxPush(*xs);
	
	mxPush(mxObjectPrototype);
	fxNewObjectInstance(the);
	fxNewHostFunction(the, fx_JSON_parse, 1);
	fxQueueID(the, fxID(the, "parse"), XS_DONT_DELETE_FLAG | XS_DONT_ENUM_FLAG | XS_DONT_SET_FLAG);
	fxNewHostFunction(the, fx_JSON_stringify, 1);
	fxQueueID(the, fxID(the, "serialize"), XS_DONT_DELETE_FLAG | XS_DONT_ENUM_FLAG | XS_DONT_SET_FLAG);
	fxQueueID(the, fxID(the, "json"), XS_DONT_DELETE_FLAG | XS_DONT_ENUM_FLAG | XS_DONT_SET_FLAG);
	
	the->stack++;
}
Ejemplo n.º 2
0
void fxBuildFunction(txMachine* the)
{
	mxPush(mxGlobal);
			
	mxPush(mxObjectPrototype);
	fxNewFunctionInstance(the);
	fxNewHostFunction(the, fx_Function_get_length, 0);
	fxQueueID(the, the->lengthID, XS_DONT_DELETE_FLAG | XS_DONT_ENUM_FLAG | XS_DONT_SET_FLAG | XS_GETTER_FLAG);
	fxNewHostFunction(the, fx_Function_get_prototype, 0);
	fxQueueID(the, the->prototypeID, XS_DONT_DELETE_FLAG | XS_DONT_ENUM_FLAG | XS_GETTER_FLAG);
	fxNewHostFunction(the, fx_Function_set_prototype, 1);
	fxQueueID(the, the->prototypeID, XS_DONT_DELETE_FLAG | XS_DONT_ENUM_FLAG | XS_SETTER_FLAG);
	fxNewHostFunction(the, fx_Function_apply, 2);
	fxQueueID(the, fxID(the, "apply"), XS_DONT_ENUM_FLAG);
	fxNewHostFunction(the, fx_Function_bind, 1);
	fxQueueID(the, fxID(the, "bind"), XS_DONT_ENUM_FLAG);
	fxNewHostFunction(the, fx_Function_call, 1);
	fxQueueID(the, fxID(the, "call"), XS_DONT_ENUM_FLAG);
	
	fxAliasInstance(the, the->stack);
	mxFunctionPrototype = *the->stack;
	fxNewHostConstructor(the, fx_Function, 1);
	the->stack->value.reference->next->next->next->flag |= XS_DONT_SET_FLAG;
	 *(--the->stack) = mxFunctionPrototype;
	fxPutID(the, fxID(the, "constructor"), XS_DONT_ENUM_FLAG, XS_DONT_ENUM_FLAG);
	fxQueueID(the, fxID(the, "Function"), XS_DONT_DELETE_FLAG | XS_DONT_ENUM_FLAG | XS_DONT_SET_FLAG);
	
	the->stack++;
}
Ejemplo n.º 3
0
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++;
}
Ejemplo n.º 4
0
void fx_Function_bind(txMachine* the)
{
	txSlot* aFunction;
	txSlot* aProperty;
	txSlot* anArray;
	txSlot* anItem;
	txSize aCount, anIndex;

	aFunction = fxGetInstance(the, mxThis);
	if (!mxIsFunction(aFunction))
		mxDebug0(the, XS_TYPE_ERROR, "this is no Function");
	if (mxArgc < 1)
		mxDebug0(the, XS_SYNTAX_ERROR, "Function.prototype.bind: no this parameter");
		
	aProperty = aFunction->next;
	if (aProperty->kind == XS_CODE_KIND)
		aCount = *(aProperty->value.code + 4);
	else
		aCount = aProperty->value.callback.length;
	aCount -= mxArgc - 1;
	if (aCount < 0)
		aCount = 0;
		
	mxPush(mxFunctionPrototype);
	aFunction = fxNewFunctionInstance(the);
	*mxResult = *(the->stack++);
	aProperty = aFunction->next;
	aProperty->kind = XS_CALLBACK_KIND;
	aProperty->value.callback.address = fx_Function_bound;
	aProperty->value.callback.length = aCount;
	
	aProperty = fxSetProperty(the, aFunction, fxID(the, "boundFunction"), C_NULL);
	aProperty->kind = mxThis->kind;
	aProperty->value = mxThis->value;
	aProperty = fxSetProperty(the, aFunction, fxID(the, "boundThis"), C_NULL);
	aProperty->kind = mxArgv(0)->kind;
	aProperty->value = mxArgv(0)->value;
	aProperty = fxSetProperty(the, aFunction, fxID(the, "boundArguments"), C_NULL);
	mxPush(mxArrayPrototype);
	anArray = fxNewArrayInstance(the);
	aProperty->kind = the->stack->kind;
	aProperty->value = the->stack->value;
	the->stack++;
	anItem = anArray->next;
	for (anIndex = 1; anIndex < mxArgc; anIndex++) {
		anItem->next = fxNewSlot(the);
		anItem = anItem->next;
		anItem->kind = mxArgv(anIndex)->kind;
		anItem->value = mxArgv(anIndex)->value;
	}
	anArray->next->value.array.length = mxArgc - 1;
	fxCacheArray(the, anArray);
}
Ejemplo n.º 5
0
txSlot* fxToInstance(txMachine* the, txSlot* theSlot)
{
	txSlot* anInstance = C_NULL;
	
	switch (theSlot->kind) {
	case XS_UNDEFINED_KIND:
		mxTypeError("cannot coerce undefined to object");
		break;
	case XS_NULL_KIND:
		mxTypeError("cannot coerce null to object");
		break;
	case XS_BOOLEAN_KIND:
		mxPush(mxBooleanPrototype);
		anInstance = fxNewBooleanInstance(the);
		anInstance->next->value.boolean = theSlot->value.boolean;
		mxPullSlot(theSlot);
		break;
	case XS_INTEGER_KIND:
		mxPush(mxNumberPrototype);
		anInstance = fxNewNumberInstance(the);
		anInstance->next->value.number = theSlot->value.integer;
		mxPullSlot(theSlot);
		break;
	case XS_NUMBER_KIND:
		mxPush(mxNumberPrototype);
		anInstance = fxNewNumberInstance(the);
		anInstance->next->value.number = theSlot->value.number;
		mxPullSlot(theSlot);
		break;
	case XS_STRING_KIND:
	case XS_STRING_X_KIND:
		mxPush(mxStringPrototype);
		anInstance = fxNewStringInstance(the);
		anInstance->next->value.string = theSlot->value.string;
		anInstance->next->next->next->value.integer = fxUnicodeLength(theSlot->value.string);
		mxPullSlot(theSlot);
		break;
	case XS_SYMBOL_KIND:
		mxPush(mxSymbolPrototype);
		anInstance = fxNewSymbolInstance(the);
		anInstance->next->value.ID = theSlot->value.ID;
		mxPullSlot(theSlot);
		break;
	case XS_REFERENCE_KIND:
		anInstance = theSlot->value.reference;
		break;
	default:
		mxTypeError("cannot coerce to instance");
		break;
	}
	return anInstance;
}
Ejemplo n.º 6
0
void fxParseJSONArray(txMachine* the, txJSONParser* theParser)
{
	txSlot* anArray;
	txIndex aLength;
	txSlot* anItem;

	fxGetNextJSONToken(the, theParser);
	mxPush(mxArrayPrototype);
	anArray = fxNewArrayInstance(the);
	aLength = 0;
	anItem = fxLastProperty(the, anArray);
	for (;;) {
		if (theParser->token == XS_JSON_TOKEN_RIGHT_BRACKET)
			break;
		fxParseJSONValue(the, theParser);
		aLength++;
		anItem->next = fxNewSlot(the);
		anItem = anItem->next;
		anItem->kind = the->stack->kind;
		anItem->value = the->stack->value;
		the->stack++;
		if (theParser->token != XS_JSON_TOKEN_COMMA)
			break;
		fxGetNextJSONToken(the, theParser);
	}
	anArray->next->value.array.length = aLength;
	fxCacheArray(the, anArray);
	if (theParser->token != XS_JSON_TOKEN_RIGHT_BRACKET)
		mxSyntaxError("missing ]");
	fxGetNextJSONToken(the, theParser);
}
Ejemplo n.º 7
0
void fx_Function_prototype_bound(txMachine* the)
{
    txSlot* boundArguments;
    txInteger c, i;
    txSlot* argument;

    mxPush(*mxFunction);
    fxGetID(the, mxID(_boundArguments));
    boundArguments = fxGetInstance(the, the->stack);
    the->stack++;
    c = boundArguments->next->value.array.length;
    argument = boundArguments->next->value.array.address;
    for (i = 0; i < c; i++) {
        mxPushSlot(argument);
        argument++;
    }
    for (i = 0; i < mxArgc; i++) {
        mxPushSlot(mxArgv(i));
    }
    /* ARGC */
    mxPushInteger(c + mxArgc);
    /* THIS */
    mxPushSlot(mxFunction);
    fxGetID(the, mxID(_boundThis));
    /* FUNCTION */
    mxPushSlot(mxFunction);
    fxGetID(the, mxID(_boundFunction));
    fxCall(the);
    mxPullSlot(mxResult);
}
Ejemplo n.º 8
0
txMachine* fxBeginHost(txMachine* the)
{
	/* ARGC */
	mxPushInteger(0);
	/* THIS */
	mxPush(mxGlobal);
	/* FUNCTION */
	mxPushUndefined();
	/* TARGET */
	mxPushUndefined();
	/* RESULT */
	mxPushUndefined();
	/* FRAME */
	--the->stack;
	the->stack->next = the->frame;
	the->stack->ID = XS_NO_ID;
	the->stack->flag = XS_C_FLAG;
	the->stack->kind = XS_FRAME_KIND;
	the->stack->value.frame.code = the->code;
	the->stack->value.frame.scope = the->scope;
	the->frame = the->stack;
	/* VARC */
	mxPushInteger(0);
	the->stack->next = C_NULL;
	the->stack->ID = XS_NO_ID;
	the->stack->kind = XS_INTEGER_KIND;
	the->scope = the->stack;
	the->code = mxIDs.value.code;
	return the;
}
Ejemplo n.º 9
0
void fxBuildHost(txMachine* the)
{
	txSlot* property;
	txSlot* slot;
	
	fxNewHostAccessorGlobal(the, fx_get_sandbox, C_NULL, fxID(the, "sandbox"), XS_DONT_DELETE_FLAG | XS_DONT_ENUM_FLAG | XS_DONT_SET_FLAG);
	property = fxLastProperty(the, mxObjectPrototype.value.reference);
	property = fxNextHostAccessorProperty(the, property, fx_Object_prototype_get_sandbox, C_NULL, fxID(the, "sandbox"), XS_DONT_DELETE_FLAG | XS_DONT_ENUM_FLAG | XS_DONT_SET_FLAG);

	mxPush(mxObjectPrototype);
	property = fxLastProperty(the, fxNewObjectInstance(the));
	property = fxNextHostFunctionProperty(the, property, fx_xs_execute, 1, fxID(the, "execute"), XS_GET_ONLY);
	property = fxNextHostFunctionProperty(the, property, fx_xs_isInstanceOf, 2, fxID(the, "isInstanceOf"), XS_GET_ONLY);
	property = fxNextHostFunctionProperty(the, property, fx_xs_newInstanceOf, 1, fxID(the, "newInstanceOf"), XS_GET_ONLY);
	property = fxNextHostFunctionProperty(the, property, fx_xs_script, 1, fxID(the, "script"), XS_GET_ONLY);
#ifdef mxProfile
	property = fxNextHostFunctionProperty(the, property, fx_xs_isProfiling, 0, fxID(the, "isProfiling"), XS_GET_ONLY);
	property = fxNextHostFunctionProperty(the, property, fx_xs_getProfilingDirectory, 0, fxID(the, "getProfilingDirectory"), XS_GET_ONLY);
	property = fxNextHostFunctionProperty(the, property, fx_xs_setProfilingDirectory, 1, fxID(the, "setProfilingDirectory"), XS_GET_ONLY);
	property = fxNextHostFunctionProperty(the, property, fx_xs_startProfiling, 0, fxID(the, "startProfiling"), XS_GET_ONLY);
	property = fxNextHostFunctionProperty(the, property, fx_xs_stopProfiling, 0, fxID(the, "stopProfiling"), XS_GET_ONLY);
#endif
#ifdef mxDebug
	mxPush(mxObjectPrototype);
	slot = fxLastProperty(the, fxNewObjectInstance(the));
	slot = fxNextHostFunctionProperty(the, slot, fx_xs_debug_getAddress, 0, fxID(the, "getAddress"), XS_GET_ONLY);
	slot = fxNextHostFunctionProperty(the, slot, fx_xs_debug_setAddress, 1, fxID(the, "setAddress"), XS_GET_ONLY);
	slot = fxNextHostFunctionProperty(the, slot, fx_xs_debug_getAutomatic, 0, fxID(the, "getAutomatic"), XS_GET_ONLY);
	slot = fxNextHostFunctionProperty(the, slot, fx_xs_debug_setAutomatic, 1, fxID(the, "setAutomatic"), XS_GET_ONLY);
	slot = fxNextHostFunctionProperty(the, slot, fx_xs_debug_getBreakOnException, 0, fxID(the, "getBreakOnException"), XS_GET_ONLY);
	slot = fxNextHostFunctionProperty(the, slot, fx_xs_debug_setBreakOnException, 1, fxID(the, "setBreakOnException"), XS_GET_ONLY);
	slot = fxNextHostFunctionProperty(the, slot, fx_xs_debug_getConnected, 0, fxID(the, "getConnected"), XS_GET_ONLY);
	slot = fxNextHostFunctionProperty(the, slot, fx_xs_debug_setConnected, 1, fxID(the, "setConnected"), XS_GET_ONLY);
	slot = fxNextHostFunctionProperty(the, slot, fx_xs_debug_clearAllBreakpoints, 0, fxID(the, "clearAllBreakpoints"), XS_GET_ONLY);
	slot = fxNextHostFunctionProperty(the, slot, fx_xs_debug_clearBreakpoint, 2, fxID(the, "clearBreakpoint"), XS_GET_ONLY);
	slot = fxNextHostFunctionProperty(the, slot, fx_xs_debug_setBreakpoint, 2, fxID(the, "setBreakpoint"), XS_GET_ONLY);
	property = fxNextSlotProperty(the, property, the->stack, fxID(the, "debug"), XS_GET_ONLY);
	the->stack++;
#endif
	slot = fxSetGlobalProperty(the, mxGlobal.value.reference, fxID(the, "xs"), C_NULL);
	slot->flag = XS_GET_ONLY;
	slot->kind = the->stack->kind;
	slot->value = the->stack->value;
	the->stack++;
}
Ejemplo n.º 10
0
void fx_Error(txMachine* the)
{
	if (mxTarget->kind == XS_UNDEFINED_KIND) {
		mxPush(mxErrorPrototype);
		fxNewObjectInstance(the);
		mxPullSlot(mxResult);
	}
	fx_Error_aux(the);
}
Ejemplo n.º 11
0
void fxParseJSON(txMachine* the, txJSONParser* theParser)
{
	mxPush(mxEmptyString);
	theParser->string = the->stack;
	fxGetNextJSONToken(the, theParser);
	fxParseJSONValue(the, theParser);
	if (theParser->token != XS_JSON_TOKEN_EOF)
		mxSyntaxError("missing EOF");
}
Ejemplo n.º 12
0
void fxThrowError(txMachine* the, txError theError)
{
	if ((theError <= XS_NO_ERROR) || (XS_ERROR_COUNT <= theError))
		theError = XS_UNKNOWN_ERROR;
	mxPush(mxErrorPrototypes(theError));	
	fxNewObjectInstance(the);
	mxException = *(the->stack++);
	fxJump(the);
}
Ejemplo n.º 13
0
void fx_URIError(txMachine* the)
{
	if (mxResult->kind == XS_UNDEFINED_KIND) {
		mxPush(mxURIErrorPrototype);
		fxNewObjectInstance(the);
		*mxResult = *(the->stack++);
	}
	fx_Error_aux(the);
}
Ejemplo n.º 14
0
void fx_setTimeoutCallback(CFRunLoopTimerRef timer, void *info)
{
	txTimeoutData* data = info;
	txMachine* the = data->the;
	fxBeginHost(the);
	{
		mxPush(data->argument);
		/* ARGC */
		mxPushInteger(1);
		/* THIS */
		mxPushUndefined();
		/* FUNCTION */
		mxPush(data->function);
		fxCall(the);
		the->stack++;
	}
	fxEndHost(the);
}
Ejemplo n.º 15
0
void fxBuildGlobal(txMachine* the)
{
    static const txHostFunctionBuilder gx_global_builders[] = {
		{ fx_decodeURI, 1, _decodeURI },
		{ fx_decodeURIComponent, 1, _decodeURIComponent },
		{ fx_encodeURI, 1, _encodeURI },
		{ fx_encodeURIComponent, 1, _encodeURIComponent },
		{ fx_escape, 1, _escape },
		{ fx_eval, 1, _eval },
		{ fx_trace, 1, _trace },
		{ fx_unescape, 1, _unescape },
		{ C_NULL, 0, 0 },
    };
    const txHostFunctionBuilder* builder;
	txSlot* slot;
	
	fxNewGlobalInstance(the);
	mxPull(mxGlobal);

	fxNewInstance(the);
	mxPull(mxObjectPrototype);
	
	mxPush(mxObjectPrototype);
	fxNewFunctionInstance(the, XS_NO_ID);
	mxPull(mxFunctionPrototype);
	
	for (builder = gx_global_builders; builder->callback; builder++) {
		fxNewHostFunctionGlobal(the, builder->callback, builder->length, mxID(builder->id), XS_DONT_ENUM_FLAG);
		the->stack++;
	}
	slot = fxSetGlobalProperty(the, mxGlobal.value.reference, mxID(_undefined), C_NULL);
	slot->flag = XS_GET_ONLY;
	
	mxPush(mxObjectPrototype);
	slot = fxNewObjectInstance(the);
	slot = fxNextHostFunctionProperty(the, slot, fx_Iterator_iterator, 0, mxID(_Symbol_iterator), XS_DONT_ENUM_FLAG);
	mxPull(mxIteratorPrototype);
	
	mxPush(mxIteratorPrototype);
	slot = fxLastProperty(the, fxNewObjectInstance(the));
	slot = fxNextHostFunctionProperty(the, slot, fx_Enumerator_next, 0, mxID(_next), XS_DONT_DELETE_FLAG | XS_DONT_ENUM_FLAG);
	fxNewHostConstructor(the, fx_Enumerator, 0, XS_NO_ID);
	mxPull(mxEnumeratorFunction);
}
Ejemplo n.º 16
0
void fxEnumerate(txMachine* the) 
{
	mxPushInteger(0);
	/* SWAP THIS */
	the->scratch = *(the->stack);
	*(the->stack) = *(the->stack + 1);
	*(the->stack + 1) = the->scratch;
	/* FUNCTION */
	mxPush(mxEnumeratorFunction);
	fxCall(the);
}
Ejemplo n.º 17
0
void fxThrowMessage(txMachine* the, txError theError, txString theMessage)
{
	if ((theError <= XS_NO_ERROR) || (XS_ERROR_COUNT <= theError))
		theError = XS_UNKNOWN_ERROR;
	mxPush(mxErrorPrototypes(theError));	
	fxNewObjectInstance(the);
	mxPushStringC(theMessage);
	fxQueueID(the, fxID(the, "message"), XS_DONT_ENUM_FLAG);
	mxException = *(the->stack++);
	fxJump(the);
}
Ejemplo n.º 18
0
void fxBuildNumber(txMachine* the)
{
	mxPush(mxGlobal);

	mxPush(mxObjectPrototype);
	fxNewNumberInstance(the);
	fxNewHostFunction(the, fx_Number_toExponential, 1);
	fxQueueID(the, fxID(the, "toExponential"), XS_DONT_ENUM_FLAG);
	fxNewHostFunction(the, fx_Number_toFixed, 1);
	fxQueueID(the, fxID(the, "toFixed"), XS_DONT_ENUM_FLAG);
	fxNewHostFunction(the, fx_Number_toString, 0);
	fxQueueID(the, fxID(the, "toLocaleString"), XS_DONT_ENUM_FLAG);
	fxNewHostFunction(the, fx_Number_toPrecision, 1);
	fxQueueID(the, fxID(the, "toPrecision"), XS_DONT_ENUM_FLAG);
	fxNewHostFunction(the, fx_Number_toString, 1);
	fxQueueID(the, fxID(the, "toString"), XS_DONT_ENUM_FLAG);
	fxNewHostFunction(the, fx_Number_valueOf, 0);
	fxQueueID(the, fxID(the, "valueOf"), XS_DONT_ENUM_FLAG);
	
	fxAliasInstance(the, the->stack);
	mxNumberPrototype = *the->stack;
	fxNewHostConstructor(the, fx_Number, 1);
	mxPushNumber(C_DBL_MAX);
	fxQueueID(the, fxID(the, "MAX_VALUE"), XS_DONT_DELETE_FLAG | XS_DONT_ENUM_FLAG | XS_DONT_SET_FLAG);
	mxPushNumber(C_DBL_MIN);
	fxQueueID(the, fxID(the, "MIN_VALUE"), XS_DONT_DELETE_FLAG | XS_DONT_ENUM_FLAG | XS_DONT_SET_FLAG);
	mxPushNumber(C_NAN);
	fxQueueID(the, fxID(the, "NaN"), XS_DONT_DELETE_FLAG | XS_DONT_ENUM_FLAG | XS_DONT_SET_FLAG);
	mxPushNumber(-C_INFINITY);
	fxQueueID(the, fxID(the, "NEGATIVE_INFINITY"), XS_DONT_DELETE_FLAG | XS_DONT_ENUM_FLAG | XS_DONT_SET_FLAG);
	mxPushNumber(C_INFINITY);
	fxQueueID(the, fxID(the, "POSITIVE_INFINITY"), XS_DONT_DELETE_FLAG | XS_DONT_ENUM_FLAG | XS_DONT_SET_FLAG);
	//fxAliasInstance(the, the->stack);
	the->stack->value.reference->next->next->next->flag |= XS_DONT_SET_FLAG;
	 *(--the->stack) = mxNumberPrototype;
	fxPutID(the, fxID(the, "constructor"), XS_DONT_ENUM_FLAG, XS_DONT_ENUM_FLAG);
	fxQueueID(the, fxID(the, "Number"), XS_DONT_DELETE_FLAG | XS_DONT_ENUM_FLAG | XS_DONT_SET_FLAG);
		
	the->stack++;
}
Ejemplo n.º 19
0
void fx_Array(txMachine* the)
{
	txIndex count = (txIndex)mxArgc;
	txBoolean flag = 0;
	txSlot* instance;
	txSlot* array;
	txSlot* argument;
	txSlot* slot;
	if (mxTarget->kind == XS_UNDEFINED_KIND) {
		mxPush(mxArrayPrototype);
		instance = fxNewArrayInstance(the);
		mxPullSlot(mxResult);
		array = instance->next;	
	}
	else
		array = fxCoerceToArray(the, mxThis);
	flag = 0;
	if (count == 1) {
		argument = mxArgv(0);
		if (argument->kind == XS_INTEGER_KIND) {
			flag = 1;
			if ((0 <= argument->value.integer) && (argument->value.integer < XS_MAX_INDEX))
				count = (txIndex)argument->value.integer;
			else
				mxRangeError("invalid length");
		}
		else if (mxArgv(0)->kind == XS_NUMBER_KIND) {
			flag = 1;
			if ((0 <= argument->value.number) && (argument->value.number < XS_MAX_INDEX))
				count = (txIndex)argument->value.number;
			else
				mxRangeError("invalid length");
		}
	}
	array->value.array.address = (txSlot *)fxNewChunk(the, count * sizeof(txSlot));
	array->value.array.length = count;
	slot = array->value.array.address;
	if (flag) {
		c_memset(slot, 0, count * sizeof(txSlot));
	}
	else {
		txIndex index = 0;
		while (index < count) {
			txSlot* argument = mxArgv(index);
			slot->ID = XS_NO_ID;
			slot->kind = argument->kind;
			slot->value = argument->value;
			slot++;
			index++;
		}
	}
}
Ejemplo n.º 20
0
void fx_Function_bound(txMachine* the)
{
	txSlot* boundArguments;
	txInteger aCount, anIndex;
	txSlot* aParameter;
	
	mxPush(*mxFunction);
	fxGetID(the, fxID(the, "boundArguments")); 
	boundArguments = fxGetInstance(the, the->stack);
	the->stack++;
	aCount = boundArguments->next->value.array.length;
	aParameter = boundArguments->next->value.array.address;
	for (anIndex = 0; anIndex < aCount; anIndex++) {
		mxZeroSlot(--the->stack);
		the->stack->kind = aParameter->kind;
		the->stack->value = aParameter->value;
		aParameter++;
	}
	for (anIndex = 0; anIndex < mxArgc; anIndex++) {
		mxZeroSlot(--the->stack);
		the->stack->kind = mxArgv(anIndex)->kind;
		the->stack->value = mxArgv(anIndex)->value;
	}
	/* #PARAM */
	mxZeroSlot(--the->stack);
	the->stack->kind = XS_INTEGER_KIND;
	the->stack->value.integer = aCount + mxArgc;
	/* THIS */
	mxPush(*mxFunction);
	fxGetID(the, fxID(the, "boundThis"));
	/* FUNCTION */
	mxPush(*mxFunction);
	fxGetID(the, fxID(the, "boundFunction"));
	/* RESULT */
	mxZeroSlot(--the->stack);
	fxRunID(the, XS_NO_ID);
	*mxResult = *(the->stack++);
}
Ejemplo n.º 21
0
void fxBuildBoolean(txMachine* the)
{
	mxPush(mxGlobal);
	
	mxPush(mxObjectPrototype);
	fxNewBooleanInstance(the);
	fxNewHostFunction(the, fx_Boolean_toString, 0);
	fxQueueID(the, fxID(the, "toLocaleString"), XS_DONT_ENUM_FLAG);
	fxNewHostFunction(the, fx_Boolean_toString, 0);
	fxQueueID(the, fxID(the, "toString"), XS_DONT_ENUM_FLAG);
	fxNewHostFunction(the, fx_Boolean_valueOf, 0);
	fxQueueID(the, fxID(the, "valueOf"), XS_DONT_ENUM_FLAG);

	fxAliasInstance(the, the->stack);
	mxBooleanPrototype = *the->stack;
	fxNewHostConstructor(the, fx_Boolean, 1);
	//fxAliasInstance(the, the->stack);
	the->stack->value.reference->next->next->next->flag |= XS_DONT_SET_FLAG;
	 *(--the->stack) = mxBooleanPrototype;
	fxPutID(the, fxID(the, "constructor"), XS_DONT_ENUM_FLAG, XS_DONT_ENUM_FLAG);
	fxQueueID(the, fxID(the, "Boolean"), XS_DONT_DELETE_FLAG | XS_DONT_ENUM_FLAG | XS_DONT_SET_FLAG);
	
	the->stack++;
}
Ejemplo n.º 22
0
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);
}
Ejemplo n.º 23
0
txSlot* fxNewIteratorInstance(txMachine* the, txSlot* iterable) 
{
	txSlot* instance;
	txSlot* result;
	txSlot* property;
	instance = fxNewObjectInstance(the);
	mxPush(mxObjectPrototype);
	result = fxNewObjectInstance(the);
	property = fxNextUndefinedProperty(the, result, mxID(_value), XS_DONT_DELETE_FLAG | XS_DONT_SET_FLAG);
	property = fxNextBooleanProperty(the, property, 0, mxID(_done), XS_DONT_DELETE_FLAG | XS_DONT_SET_FLAG);
	property = fxNextSlotProperty(the, instance, the->stack, mxID(_result), XS_GET_ONLY);
	property = fxNextSlotProperty(the, property, iterable, mxID(_iterable), XS_GET_ONLY);
	property = fxNextIntegerProperty(the, property, 0, mxID(_index), XS_GET_ONLY);
    the->stack++;
	return instance;
}
Ejemplo n.º 24
0
void fxBuildError(txMachine* the)
{
	txSlot* slot;
	mxPush(mxObjectPrototype);
	slot = fxLastProperty(the, fxNewObjectInstance(the));
	slot = fxNextHostFunctionProperty(the, slot, fx_Error_toString, 0, mxID(_toLocaleString), XS_DONT_ENUM_FLAG);
	slot = fxNextHostFunctionProperty(the, slot, fx_Error_toString, 0, mxID(_toString), XS_DONT_ENUM_FLAG);
	slot = fxNextStringProperty(the, slot, "Error", mxID(_name), XS_DONT_ENUM_FLAG);
	slot = fxNextStringProperty(the, slot, "", mxID(_message), XS_DONT_ENUM_FLAG);
	mxErrorPrototype = *the->stack;
	fxNewHostConstructorGlobal(the, fx_Error, 0, mxID(_Error), XS_GET_ONLY);
	the->stack++;
	mxPush(mxErrorPrototype);
	slot = fxLastProperty(the, fxNewObjectInstance(the));
	slot = fxNextStringProperty(the, slot, "EvalError", mxID(_name), XS_DONT_ENUM_FLAG);
	mxEvalErrorPrototype = *the->stack;
	fxNewHostConstructorGlobal(the, fx_EvalError, 1, mxID(_EvalError), XS_GET_ONLY);
	the->stack++;
	mxPush(mxErrorPrototype);
	slot = fxLastProperty(the, fxNewObjectInstance(the));
	slot = fxNextStringProperty(the, slot, "RangeError", mxID(_name), XS_DONT_ENUM_FLAG);
	mxRangeErrorPrototype = *the->stack;
	fxNewHostConstructorGlobal(the, fx_RangeError, 1, mxID(_RangeError), XS_GET_ONLY);
	the->stack++;
	mxPush(mxErrorPrototype);
	slot = fxLastProperty(the, fxNewObjectInstance(the));
	slot = fxNextStringProperty(the, slot, "ReferenceError", mxID(_name), XS_DONT_ENUM_FLAG);
	mxReferenceErrorPrototype = *the->stack;
	fxNewHostConstructorGlobal(the, fx_ReferenceError, 1, mxID(_ReferenceError), XS_GET_ONLY);
	the->stack++;
	mxPush(mxErrorPrototype);
	slot = fxLastProperty(the, fxNewObjectInstance(the));
	slot = fxNextStringProperty(the, slot, "SyntaxError", mxID(_name), XS_DONT_ENUM_FLAG);
	mxSyntaxErrorPrototype = *the->stack;
	fxNewHostConstructorGlobal(the, fx_SyntaxError, 1, mxID(_SyntaxError), XS_GET_ONLY);
	the->stack++;
	mxPush(mxErrorPrototype);
	slot = fxLastProperty(the, fxNewObjectInstance(the));
	slot = fxNextStringProperty(the, slot, "TypeError", mxID(_name), XS_DONT_ENUM_FLAG);
	mxTypeErrorPrototype = *the->stack;
	fxNewHostConstructorGlobal(the, fx_TypeError, 1, mxID(_TypeError), XS_GET_ONLY);
	the->stack++;
	mxPush(mxErrorPrototype);
	slot = fxLastProperty(the, fxNewObjectInstance(the));
	slot = fxNextStringProperty(the, slot, "URIError", mxID(_name), XS_DONT_ENUM_FLAG);
	mxURIErrorPrototype = *the->stack;
	fxNewHostConstructorGlobal(the, fx_URIError, 1, mxID(_URIError), XS_GET_ONLY);
	the->stack++;
}
Ejemplo n.º 25
0
void fxBuildBoolean(txMachine* the)
{
    static const txHostFunctionBuilder gx_Boolean_prototype_builders[] = {
		{ fx_Boolean_prototype_toString, 0, _toString },
		{ fx_Boolean_prototype_valueOf, 0, _valueOf },
 		{ C_NULL, 0, 0 },
   };
    const txHostFunctionBuilder* builder;
	txSlot* slot;
	mxPush(mxObjectPrototype);
	slot = fxLastProperty(the, fxNewBooleanInstance(the));
	for (builder = gx_Boolean_prototype_builders; builder->callback; builder++)
		slot = fxNextHostFunctionProperty(the, slot, builder->callback, builder->length, mxID(builder->id), XS_DONT_ENUM_FLAG);
	mxBooleanPrototype = *the->stack;
	fxNewHostConstructorGlobal(the, fx_Boolean, 1, mxID(_Boolean), XS_DONT_ENUM_FLAG);
	the->stack++;
}
Ejemplo n.º 26
0
void fxBuildHosts(txMachine* the, txInteger c, txHostFunctionBuilder* builder)
{
	mxPushInteger(c);
	mxPushInteger(1);
	mxPush(mxArrayPrototype);
	fxNewArrayInstance(the);
	fxArrayCacheBegin(the, the->stack);
	while (c) {
		if (builder->length >= 0)
			fxNewHostFunction(the, builder->callback, builder->length, (builder->id >= 0) ? ((txID*)(the->code))[builder->id] : XS_NO_ID);
		else
			fxNewHostObject(the, (txDestructor)builder->callback);
		fxArrayCacheItem(the, the->stack + 1, the->stack);
		the->stack++;
		c--;
		builder++;
	}
	fxArrayCacheEnd(the, the->stack);
}
Ejemplo n.º 27
0
void fxBuildJSON(txMachine* the)
{
    static const txHostFunctionBuilder gx_JSON_builders[] = {
		{ fx_JSON_parse, 2, _parse },
		{ fx_JSON_stringify, 3, _stringify },
		{ C_NULL, 0, 0 },
    };
    const txHostFunctionBuilder* builder;
	txSlot* slot;
	mxPush(mxObjectPrototype);
	slot = fxLastProperty(the, fxNewObjectInstance(the));
	for (builder = gx_JSON_builders; builder->callback; builder++)
		slot = fxNextHostFunctionProperty(the, slot, builder->callback, builder->length, mxID(builder->id), XS_DONT_ENUM_FLAG);
	slot = fxNextStringXProperty(the, slot, "JSON", mxID(_Symbol_toStringTag), XS_DONT_ENUM_FLAG | XS_DONT_SET_FLAG);
	slot = fxSetGlobalProperty(the, mxGlobal.value.reference, mxID(_JSON));
	slot->flag = XS_DONT_ENUM_FLAG;
	slot->kind = the->stack->kind;
	slot->value = the->stack->value;
	the->stack++;
}
Ejemplo n.º 28
0
void fxBuildFunction(txMachine* the)
{
    static const txHostFunctionBuilder gx_Function_prototype_builders[] = {
        { fx_Function_prototype_apply, 2, _apply },
        { fx_Function_prototype_bind, 1, _bind },
        { fx_Function_prototype_call, 1, _call },
        { fx_Function_prototype_hasInstance, 1, _Symbol_hasInstance },
        { C_NULL, 0, 0 },
    };
    const txHostFunctionBuilder* builder;
    txSlot* slot;
    mxPush(mxFunctionPrototype);
    slot = fxLastProperty(the, the->stack->value.reference);
    slot = fxNextHostAccessorProperty(the, slot, fx_Function_prototype_get_length, fx_Function_prototype_set_length, mxID(_length), XS_DONT_ENUM_FLAG | XS_DONT_SET_FLAG);
    slot = fxNextHostAccessorProperty(the, slot, fx_Function_prototype_get_name, fx_Function_prototype_set_name, mxID(_name), XS_DONT_DELETE_FLAG | XS_DONT_ENUM_FLAG | XS_DONT_SET_FLAG);
    slot = fxNextHostAccessorProperty(the, slot, fx_Function_prototype_get_prototype, fx_Function_prototype_set_prototype, mxID(_prototype), XS_DONT_DELETE_FLAG | XS_DONT_ENUM_FLAG);
    for (builder = gx_Function_prototype_builders; builder->callback; builder++)
        slot = fxNextHostFunctionProperty(the, slot, builder->callback, builder->length, mxID(builder->id), XS_DONT_ENUM_FLAG);
    slot = fxNextStringProperty(the, slot, "Function", mxID(_Symbol_toStringTag), XS_DONT_ENUM_FLAG | XS_DONT_SET_FLAG);
    slot = fxNewHostConstructorGlobal(the, fx_Function, 1, mxID(_Function), XS_GET_ONLY);
    the->stack++;
}
Ejemplo n.º 29
0
void fxConstructArrayEntry(txMachine* the, txSlot* entry)
{
	txSlot* value = the->stack;
	txSlot* key = the->stack + 1;
	txSlot* instance;
	txSlot* array;
	txSlot* item;
	mxPush(mxArrayPrototype);
	instance = fxNewArrayInstance(the);
	array = instance->next;
	fxSetArrayLength(the, array, 2);
	item = array->value.array.address;
	item->ID = XS_NO_ID;
	item->kind = key->kind;
	item->value = key->value;
	item++;
	item->ID = XS_NO_ID;
	item->kind = value->kind;
	item->value = value->value;
	entry->kind = the->stack->kind;
	entry->value = the->stack->value;
	the->stack += 3;
}
Ejemplo n.º 30
0
void fxBuildSymbol(txMachine* the)
{
    static const txHostFunctionBuilder gx_Symbol_prototype_builders[] = {
        { fx_Symbol_prototype_toString, 0, _toString },
        { fx_Symbol_prototype_valueOf, 0, _valueOf },
        { C_NULL, 0, 0 },
    };
    static const txHostFunctionBuilder gx_Symbol_builders[] = {
        { fx_Symbol_for, 1, _for },
        { fx_Symbol_keyFor, 1, _keyFor },
        { C_NULL, 0, 0 },
    };
    const txHostFunctionBuilder* builder;
    txSlot* slot;
    mxPush(mxObjectPrototype);
    slot = fxLastProperty(the, fxNewSymbolInstance(the));
    for (builder = gx_Symbol_prototype_builders; builder->callback; builder++)
        slot = fxNextHostFunctionProperty(the, slot, builder->callback, builder->length, mxID(builder->id), XS_DONT_ENUM_FLAG);
    slot = fxNextHostFunctionProperty(the, slot, fx_Symbol_prototype_toPrimitive, 1, mxID(_Symbol_toPrimitive), XS_DONT_ENUM_FLAG | XS_DONT_SET_FLAG);
    slot = fxNextStringProperty(the, slot, "Symbol", mxID(_Symbol_toStringTag), XS_DONT_ENUM_FLAG | XS_DONT_SET_FLAG);
    mxSymbolPrototype = *the->stack;
    slot = fxLastProperty(the, fxNewHostConstructorGlobal(the, fx_Symbol, 0, mxID(_Symbol), XS_DONT_ENUM_FLAG));
    for (builder = gx_Symbol_builders; builder->callback; builder++)
        slot = fxNextHostFunctionProperty(the, slot, builder->callback, builder->length, mxID(builder->id), XS_DONT_ENUM_FLAG);
    slot = fxNextSymbolProperty(the, slot, mxID(_Symbol_hasInstance), mxID(_hasInstance), XS_GET_ONLY);
    slot = fxNextSymbolProperty(the, slot, mxID(_Symbol_isConcatSpreadable), mxID(_isConcatSpreadable), XS_GET_ONLY);
    slot = fxNextSymbolProperty(the, slot, mxID(_Symbol_iterator), mxID(_iterator), XS_GET_ONLY);
    slot = fxNextSymbolProperty(the, slot, mxID(_Symbol_match), mxID(_match), XS_GET_ONLY);
    slot = fxNextSymbolProperty(the, slot, mxID(_Symbol_replace), mxID(_replace), XS_GET_ONLY);
    slot = fxNextSymbolProperty(the, slot, mxID(_Symbol_search), mxID(_search), XS_GET_ONLY);
    slot = fxNextSymbolProperty(the, slot, mxID(_Symbol_species), mxID(_species), XS_GET_ONLY);
    slot = fxNextSymbolProperty(the, slot, mxID(_Symbol_split), mxID(_split), XS_GET_ONLY);
    slot = fxNextSymbolProperty(the, slot, mxID(_Symbol_toPrimitive), mxID(_toPrimitive), XS_GET_ONLY);
    slot = fxNextSymbolProperty(the, slot, mxID(_Symbol_toStringTag), mxID(_toStringTag), XS_GET_ONLY);
    slot = fxNextSymbolProperty(the, slot, mxID(_Symbol_unscopables), mxID(_unscopables), XS_GET_ONLY);
    the->stack++;
}