Exemplo n.º 1
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++;
		}
	}
}
Exemplo n.º 2
0
void fx_Number_prototype_toString(txMachine* the)
{
	char buffer[256];
	txInteger radix;
	txSlot* slot = fxCheckNumber(the, mxThis);
	if (!slot) mxTypeError("this is no number");
	if (mxArgc > 0) {
		radix = fxToInteger(the, mxArgv(0));
		if (radix) {
			if ((radix < 2) || (36 < radix))
				mxRangeError("invalid radix");
		}
		else	
			radix = 10;
	}
	else	
		radix = 10;
	mxResult->kind = slot->kind;
	mxResult->value = slot->value;
	if (radix == 10)
		fxToString(the, mxResult);
	else {
		static char gxDigits[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
		txString string = buffer + sizeof(buffer);
		txNumber value;
		txBoolean minus;
		value = mxResult->value.number;
		switch (c_fpclassify(value)) {
		case C_FP_INFINITE:
			if (value < 0)
				fxCopyStringC(the, mxResult, "-Infinity");
			else
				fxCopyStringC(the, mxResult, "Infinity");
			break;
		case C_FP_NAN:
			fxCopyStringC(the, mxResult, "NaN");
			break;
		case C_FP_ZERO:
			fxCopyStringC(the, mxResult, "0");
			break;
		default:
			*(--string) = 0;
			if (value < 0) {
				minus = 1;
				value = -value;
			} 
			else
				minus = 0;
			do {
				*(--string) = gxDigits[(txInteger)c_fmod(value, radix)];
				value = value / radix;
			} while (value >= 1);
			if (minus)
				*(--string) = '-';
			fxCopyStringC(the, mxResult, string);
		}
	}
}
Exemplo n.º 3
0
txSlot* fxSetArrayProperty(txMachine* the, txSlot* array, txInteger index) 
{
	txSlot* result;
	if (index >= XS_MAX_INDEX)		
		mxRangeError("invalid index");
	if (array->value.array.length <= index)
		fxSetArrayLength(the, array, index + 1);
	result = array->value.array.address + index;
	result->ID = XS_NO_ID;
	return result;
}
Exemplo n.º 4
0
txString fxToStringBuffer(txMachine* the, txSlot* theSlot, txString theBuffer, txSize theSize)
{
	char* aString;
	txSize aSize;

	aString = fxToString(the, theSlot);
	aSize = c_strlen(aString) + 1;
	if (aSize > theSize)
		mxRangeError("Cannot buffer string");
	c_memcpy(theBuffer, aString, aSize);
	return theBuffer;
}