Esempio n. 1
0
void fx_Function(txMachine* the)
{
    txInteger c, i;
    txStringStream stream;

    c = mxArgc;
    i = 0;
    mxPushStringC("(function anonymous(");
    while (c > 1) {
        fxToString(the, mxArgv(i));
        fxConcatString(the, the->stack, mxArgv(i));
        if (c > 2)
            fxConcatStringC(the, the->stack, ", ");
        c--;
        i++;
    }
    fxConcatStringC(the, the->stack, "){");
    if (c > 0) {
        fxToString(the, mxArgv(i));
        fxConcatString(the, the->stack, mxArgv(i));
    }
    fxConcatStringC(the, the->stack, "})");
    stream.slot = the->stack;
    stream.offset = 0;
    stream.size = c_strlen(the->stack->value.string);
    fxRunScript(the, fxParseScript(the, &stream, fxStringGetter, mxProgramFlag), C_NULL, C_NULL, C_NULL, C_NULL);
    if (mxTarget->kind == XS_UNDEFINED_KIND)
        mxPullSlot(mxResult);
    else {
        txSlot* from = fxGetInstance(the, the->stack++);
        txSlot* to = fxGetInstance(the, mxThis);
        txSlot* fromProperty;
        txSlot* toProperty;
        to->next->value.code = from->next->value.code;
        fromProperty = mxFunctionInstancePrototype(from);
        toProperty = mxFunctionInstancePrototype(to);
        *toProperty = *fromProperty;
        fromProperty = mxFunctionInstanceInfo(from);
        toProperty = mxFunctionInstanceInfo(to);
        *toProperty = *fromProperty;
    }
}
Esempio n. 2
0
void fxCatenate(txMachine* the)
{
	txInteger aCount, anIndex;
	
	aCount = the->stack->value.integer;
	the->stack++;
	fxToString(the, the->stack);
	for (anIndex = 1; anIndex < aCount; anIndex++) {
		fxToString(the, the->stack + anIndex);
		fxConcatString(the, the->stack, the->stack + anIndex);
	}
	aCount--;
	(the->stack + aCount)->value = (the->stack)->value;
	the->stack += aCount;
}
Esempio n. 3
0
void fx_Function_prototype_bind(txMachine* the)
{
    txSlot* instance = fxToInstance(the, mxThis);
    txSize length;
    txSlot* slot;
    txID id;
    txSlot* arguments;
    txSlot* argument;
    txSize c = mxArgc, i;

    if (!fxIsFunction(the, instance))
        mxTypeError("this is no Function instance");

    if (fxHasOwnProperty(the, instance, mxID(_length))) {
        mxPushSlot(mxThis);
        fxGetID(the, mxID(_length));
        length = fxToInteger(the, the->stack++);
        if (c > 1)
            length -= c - 1;
        if (length < 0)
            length = 0;
        mxPop();
    }
    else
        length = 0;

    mxPushSlot(mxThis);
    fxGetID(the, mxID(_name));
    mxPushStringC("bound ");
    fxConcatString(the, the->stack, the->stack + 1);
    slot = fxNewName(the, the->stack);
    id = slot->ID;
    mxPop();
    mxPop();

    mxPushReference(instance->value.instance.prototype);
    instance = fxNewFunctionInstance(the, id);
    mxPullSlot(mxResult);

    slot = mxFunctionInstanceCode(instance);
    slot->kind = XS_CALLBACK_KIND;
    slot->value.callback.address = fx_Function_prototype_bound;
    slot->value.callback.IDs = (txID*)mxIDs.value.code;

    slot = mxFunctionInstanceInfo(instance);
    slot->value.info.length = (txID)length;

    slot = fxLastProperty(the, instance);
    slot = fxNextSlotProperty(the, slot, mxThis, mxID(_boundFunction), XS_GET_ONLY);
    if (c > 0)
        slot = fxNextSlotProperty(the, slot, mxArgv(0), mxID(_boundThis), XS_GET_ONLY);
    else
        slot = fxNextUndefinedProperty(the, slot, mxID(_boundThis), XS_GET_ONLY);

    mxPush(mxArrayPrototype);
    arguments = fxNewArrayInstance(the);
    argument = arguments->next;
    for (i = 1; i < c; i++) {
        argument->next = fxNewSlot(the);
        argument = argument->next;
        argument->kind = mxArgv(i)->kind;
        argument->value = mxArgv(i)->value;
    }
    arguments->next->value.array.length = mxArgc - 1;
    fxCacheArray(the, arguments);
    slot = fxNextSlotProperty(the, slot, the->stack, mxID(_boundArguments), XS_GET_ONLY);
    mxPop();
}
Esempio n. 4
0
void fx_Function(txMachine* the)
{	
	txSlot* aFunction;
	txInteger aCount, anIndex, aFileIndex, aLineIndex;
	txFlag aFlag;
	txSlot* aSlot;
	txStringStream aStream;
	txByte* aCode;
	txSlot* aProperty;
	
	if (mxResult->kind == XS_UNDEFINED_KIND) {
		mxPush(mxFunctionPrototype);
		aFunction = fxNewFunctionInstance(the);
		*mxResult = *(the->stack++);
	}
	else
		aFunction = fxGetOwnInstance(the, mxResult);
	mxCheckFunction(aFunction);

	aCount = mxArgc;
	anIndex = 0;
	aFileIndex = aCount - 2;
	aLineIndex = aCount - 1;
	aFlag = XS_NO_FLAG;
	if ((aCount >= 4) && (mxArgv(aLineIndex)->kind == XS_INTEGER_KIND)) {
		aCount -= 2;
		aFlag |= XS_DEBUG_FLAG;
	}
	aSlot = fxGetInstance(the, mxThis);
	if (aSlot && (aSlot->flag & XS_SANDBOX_FLAG))
		aFlag |= XS_SANDBOX_FLAG;
	else
		aFlag |= the->frame->next->flag & XS_SANDBOX_FLAG;
	
	mxZeroSlot(--the->stack);
	fxCopyStringC(the, the->stack, "(");
	while (aCount > 1) {
		fxToString(the, mxArgv(anIndex));
		fxConcatString(the, the->stack, mxArgv(anIndex));
		if (aCount > 2)
			fxConcatStringC(the, the->stack, ", ");
		aCount--;
		anIndex++;
	}
	fxConcatStringC(the, the->stack, "){");
	if (aCount > 0) {
		fxToString(the, mxArgv(anIndex));
		fxConcatString(the, the->stack, mxArgv(anIndex));
	}
	fxConcatStringC(the, the->stack, "}");
		
	aStream.slot = the->stack;
	aStream.offset = 0;
	aStream.size = c_strlen(the->stack->value.string);
#ifdef mxDebug
	if (aFlag & XS_DEBUG_FLAG) 
		aCode = fxParseScript(the, &aStream, fxStringGetter, 
				fxNewFile(the, mxArgv(aFileIndex)), mxArgv(aLineIndex)->value.integer, 
				aFlag, C_NULL);
	else
#endif
		aCode = fxParseScript(the, &aStream, fxStringGetter, C_NULL, 0, 
				aFlag, C_NULL);
	
	aFunction->next->value.code = aCode;
	
	aProperty = fxSetProperty(the, aFunction, the->bodyID, C_NULL);
	aProperty->flag = XS_DONT_DELETE_FLAG | XS_DONT_ENUM_FLAG | XS_DONT_SET_FLAG;
	aProperty->value.string = mxArgv(anIndex)->value.string;
	aProperty->kind = mxArgv(anIndex)->kind;
	the->stack++;
}