Beispiel #1
0
void fxRunModule(txMachine* the, txString path)
{
	mxPushStringC(path);
	fxRequireModule(the, XS_NO_ID, the->stack);
	the->stack++;
	fxRunLoop(the);
}
Beispiel #2
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);
}
Beispiel #3
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;
    }
}
Beispiel #4
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();
}
Beispiel #5
0
void fxBuildError(txMachine* the)
{
	mxPush(mxGlobal);
	
	mxPush(mxObjectPrototype);
	fxNewObjectInstance(the);
	mxPushStringC("Error");
	fxQueueID(the, fxID(the, "name"), XS_DONT_ENUM_FLAG);
	mxPush(mxEmptyString);
	fxQueueID(the, fxID(the, "message"), XS_DONT_ENUM_FLAG);
	fxNewHostFunction(the, fx_Error_toString, 0);
	fxQueueID(the, fxID(the, "toLocaleString"), XS_DONT_ENUM_FLAG);
	fxNewHostFunction(the, fx_Error_toString, 0);
	fxQueueID(the, fxID(the, "toString"), XS_DONT_ENUM_FLAG);
	fxAliasInstance(the, the->stack);
	mxErrorPrototype = *the->stack;
	fxNewHostConstructor(the, fx_Error, 1);
	the->stack->value.reference->next->next->next->flag |= XS_DONT_SET_FLAG;
	 *(--the->stack) = mxErrorPrototype;
	fxPutID(the, fxID(the, "constructor"), XS_DONT_ENUM_FLAG, XS_DONT_ENUM_FLAG);
	fxQueueID(the, fxID(the, "Error"), XS_DONT_DELETE_FLAG | XS_DONT_ENUM_FLAG | XS_DONT_SET_FLAG);
	
	mxPush(mxErrorPrototype);
	fxNewObjectInstance(the);
	mxPushStringC("EvalError");
	fxQueueID(the, fxID(the, "name"), XS_DONT_ENUM_FLAG);
	mxPush(mxEmptyString);
	fxQueueID(the, fxID(the, "message"), XS_DONT_ENUM_FLAG);
	fxAliasInstance(the, the->stack);
	mxEvalErrorPrototype = *the->stack;
	fxNewHostConstructor(the, fx_EvalError, 1);
	the->stack->value.reference->next->next->next->flag |= XS_DONT_SET_FLAG;
	 *(--the->stack) = mxEvalErrorPrototype;
	fxPutID(the, fxID(the, "constructor"), XS_DONT_ENUM_FLAG, XS_DONT_ENUM_FLAG);
	fxQueueID(the, fxID(the, "EvalError"), XS_DONT_DELETE_FLAG | XS_DONT_ENUM_FLAG | XS_DONT_SET_FLAG);
	
	mxPush(mxErrorPrototype);
	fxNewObjectInstance(the);
	mxPushStringC("RangeError");
	fxQueueID(the, fxID(the, "name"), XS_DONT_ENUM_FLAG);
	mxPush(mxEmptyString);
	fxQueueID(the, fxID(the, "message"), XS_DONT_ENUM_FLAG);
	fxAliasInstance(the, the->stack);
	mxRangeErrorPrototype = *the->stack;
	fxNewHostConstructor(the, fx_RangeError, 1);
	the->stack->value.reference->next->next->next->flag |= XS_DONT_SET_FLAG;
	 *(--the->stack) = mxRangeErrorPrototype;
	fxPutID(the, fxID(the, "constructor"), XS_DONT_ENUM_FLAG, XS_DONT_ENUM_FLAG);
	fxQueueID(the, fxID(the, "RangeError"), XS_DONT_DELETE_FLAG | XS_DONT_ENUM_FLAG | XS_DONT_SET_FLAG);
	
	mxPush(mxErrorPrototype);
	fxNewObjectInstance(the);
	mxPushStringC("ReferenceError");
	fxQueueID(the, fxID(the, "name"), XS_DONT_ENUM_FLAG);
	mxPush(mxEmptyString);
	fxQueueID(the, fxID(the, "message"), XS_DONT_ENUM_FLAG);
	fxAliasInstance(the, the->stack);
	mxReferenceErrorPrototype = *the->stack;
	fxNewHostConstructor(the, fx_ReferenceError, 1);
	the->stack->value.reference->next->next->next->flag |= XS_DONT_SET_FLAG;
	 *(--the->stack) = mxReferenceErrorPrototype;
	fxPutID(the, fxID(the, "constructor"), XS_DONT_ENUM_FLAG, XS_DONT_ENUM_FLAG);
	fxQueueID(the, fxID(the, "ReferenceError"), XS_DONT_DELETE_FLAG | XS_DONT_ENUM_FLAG | XS_DONT_SET_FLAG);

	mxPush(mxErrorPrototype);
	fxNewObjectInstance(the);
	mxPushStringC("SyntaxError");
	fxQueueID(the, fxID(the, "name"), XS_DONT_ENUM_FLAG);
	mxPush(mxEmptyString);
	fxQueueID(the, fxID(the, "message"), XS_DONT_ENUM_FLAG);
	fxAliasInstance(the, the->stack);
	mxSyntaxErrorPrototype = *the->stack;
	fxNewHostConstructor(the, fx_SyntaxError, 1);
	the->stack->value.reference->next->next->next->flag |= XS_DONT_SET_FLAG;
	 *(--the->stack) = mxSyntaxErrorPrototype;
	fxPutID(the, fxID(the, "constructor"), XS_DONT_ENUM_FLAG, XS_DONT_ENUM_FLAG);
	fxQueueID(the, fxID(the, "SyntaxError"), XS_DONT_DELETE_FLAG | XS_DONT_ENUM_FLAG | XS_DONT_SET_FLAG);
	
	mxPush(mxErrorPrototype);
	fxNewObjectInstance(the);
	mxPushStringC("TypeError");
	fxQueueID(the, fxID(the, "name"), XS_DONT_ENUM_FLAG);
	mxPush(mxEmptyString);
	fxQueueID(the, fxID(the, "message"), XS_DONT_ENUM_FLAG);
	fxAliasInstance(the, the->stack);
	mxTypeErrorPrototype = *the->stack;
	fxNewHostConstructor(the, fx_TypeError, 1);
	the->stack->value.reference->next->next->next->flag |= XS_DONT_SET_FLAG;
	 *(--the->stack) = mxTypeErrorPrototype;
	fxPutID(the, fxID(the, "constructor"), XS_DONT_ENUM_FLAG, XS_DONT_ENUM_FLAG);
	fxQueueID(the, fxID(the, "TypeError"), XS_DONT_DELETE_FLAG | XS_DONT_ENUM_FLAG | XS_DONT_SET_FLAG);
	
	mxPush(mxErrorPrototype);
	fxNewObjectInstance(the);
	mxPushStringC("URIError");
	fxQueueID(the, fxID(the, "name"), XS_DONT_ENUM_FLAG);
	mxPush(mxEmptyString);
	fxQueueID(the, fxID(the, "message"), XS_DONT_ENUM_FLAG);
	fxAliasInstance(the, the->stack);
	mxURIErrorPrototype = *the->stack;
	fxNewHostConstructor(the, fx_URIError, 1);
	the->stack->value.reference->next->next->next->flag |= XS_DONT_SET_FLAG;
	 *(--the->stack) = mxURIErrorPrototype;
	fxPutID(the, fxID(the, "constructor"), XS_DONT_ENUM_FLAG, XS_DONT_ENUM_FLAG);
	fxQueueID(the, fxID(the, "URIError"), XS_DONT_DELETE_FLAG | XS_DONT_ENUM_FLAG | XS_DONT_SET_FLAG);

	the->stack++;
}
Beispiel #6
0
txMachine* fxCreateMachine(txCreation* theCreation, void* theArchive, txString theName, void* theContext)
{
	txMachine* the = (txMachine* )c_calloc(sizeof(txMachine), 1);
	if (the) {
		txJump aJump;

		aJump.nextJump = C_NULL;
		aJump.stack = C_NULL;
		aJump.scope = C_NULL;
		aJump.frame = C_NULL;
		aJump.code = C_NULL;
		aJump.flag = 0;
		the->firstJump = &aJump;
		if (c_setjmp(aJump.buffer) == 0) {
			txInteger anIndex;

		#if __FSK_LAYER__
			FskInstrumentedItemNew(the, NULL, &gXSTypeInstrumentation);
		#endif

		#ifdef mxDebug
			the->echoSize = 1 * 1024;
			the->echoBuffer = (txString)c_malloc(the->echoSize);
			if (!the->echoBuffer)
				fxJump(the);
			//fxConnect(the);
			the->connection = mxNoSocket;
			the->name = theName;
			the->sorter = (txSlot**)c_malloc(theCreation->keyCount * sizeof(txSlot*));
			if (!the->sorter)
				fxJump(the);
			the->breakOnExceptionFlag = 1;
		#endif
		#ifdef mxProfile
			the->profileID = 1;
			the->profileBottom = c_malloc(XS_PROFILE_COUNT * sizeof(txProfileRecord));
			if (!the->profileBottom)
				fxJump(the);
			the->profileCurrent = the->profileBottom;
			the->profileTop = the->profileBottom + XS_PROFILE_COUNT;
		#endif

			the->archive = theArchive;
			
			the->dtoa = fxNew_dtoa();
			if (!the->dtoa)
				fxJump(the);
				
			fxAllocate(the, theCreation);

			/* mxGLobal */
			mxPushUndefined();
			/* mxException */
			mxPushUndefined();
			for (anIndex = mxObjectPrototypeStackIndex; anIndex < mxModulePathsStackIndex; anIndex++)
				mxPushUndefined();
			/* mxModulePaths */
			mxPushUndefined();
			/* mxImportingModules */
			fxNewInstance(the);
			/* mxLoadingModules */
			fxNewInstance(the);
			/* mxLoadedModules */
			fxNewInstance(the);
			/* mxResolvingModules */
			fxNewInstance(the);
			/* mxRunningModules */
			fxNewInstance(the);
			/* mxRequiredModules */
			fxNewInstance(the);
			/* mxModules */
			mxPushUndefined();
			/* mxPendingJobs */
			fxNewInstance(the);
			/* mxRunningJobs */
			fxNewInstance(the);
			/* mxFiles */
			mxPushList();
			/* mxBreakpoints */
			mxPushList();
			
			/* mxHosts */
			mxPushUndefined();
			/* mxIDs */
			mxPushUndefined();
			/* mxEmptyCode */
			mxPushUndefined();
			the->stack->value.code = (txByte *)fxNewChunk(the, sizeof(gxNoCode));
			c_memcpy(the->stack->value.code, gxNoCode, sizeof(gxNoCode));
			the->stack->kind = XS_CODE_KIND;	
			/* mxEmptyString */
			mxPushStringC("");
			/* mxBooleanString */
			mxPushStringC("boolean");
			/* mxDefaultString */
			mxPushStringC("default");
			/* mxFunctionString */
			mxPushStringC("function");
			/* mxNumberString */
			mxPushStringC("number");
			/* mxObjectString */
			mxPushStringC("object");
			/* mxStringString */
			mxPushStringC("string");
			/* mxSymbolString */
			mxPushStringC("symbol");
			/* mxUndefinedString */
			mxPushStringC("undefined");
			for (anIndex = mxGetArgumentFunctionStackIndex; anIndex < mxStackIndexCount; anIndex++) 
				mxPushUndefined();
			
			fxBuildKeys(the);
			fxBuildGlobal(the);
			fxBuildObject(the);
			fxBuildFunction(the);
			fxBuildGenerator(the);
			fxBuildArray(the);
			fxBuildString(the);
			fxBuildBoolean(the);
			fxBuildNumber(the);
			fxBuildDate(the);
			fxBuildMath(the);
			fxBuildRegExp(the);
			fxBuildError(the);
			fxBuildJSON(the);
			fxBuildDataView(the);
			fxBuildPromise(the);
			fxBuildSymbol(the);
			fxBuildProxy(the);
			fxBuildMapSet(the);
			fxBuildModule(the);
			fxBuildHost(the);
			
			mxPush(mxSetPrototype);
			fxNewSetInstance(the);
			mxPull(mxModulePaths);
			
			mxPush(mxObjectPrototype);
			fxNewWeakSetInstance(the);
			mxPull(mxModules);
			
            the->collectFlag = XS_COLLECTING_FLAG;
			
            /*{
				int c = 32;
				while (--c)
					fxCollectGarbage(the);
			}*/
			the->context = theContext;

		#ifdef mxDebug
			if (fxGetAutomatic(the))
				fxLogin(the);
		#endif

			the->firstJump = C_NULL;
		}
		else {
		#if __FSK_LAYER__
			FskInstrumentedItemDispose(the);
		#endif
			fxFree(the);
			c_free(the);
			the = NULL;
		}
	}
	return the;
}