Beispiel #1
0
int JSON::parseNumber(const tiny_string &jsonstring, int pos, ASObject** parent, const multiname& key)
{
	int len = jsonstring.numBytes();
	tiny_string res;
	bool done = false;
	while (!done && pos < len)
	{
		char c = jsonstring.charAt(pos);
		switch(c)
		{
			case '0':
			case '1':
			case '2':
			case '3':
			case '4':
			case '5':
			case '6':
			case '7':
			case '8':
			case '9':
			case '-':
			case '+':
			case '.':
			case 'E':
			case 'e':
				res += c;
				pos++;
				break;
			default:
				done = true;
				break;
		}
	}
	ASString* numstr = Class<ASString>::getInstanceS(res);
	number_t num = numstr->toNumber();

	if (std::isnan(num))
		throwError<SyntaxError>(kJSONInvalidParseInput);

	if (*parent == NULL)
		*parent = Class<Number>::getInstanceS(num);
	else 
	{
		(*parent)->setVariableByMultiname(key,Class<Number>::getInstanceS(num),ASObject::CONST_NOT_ALLOWED);
	}
	return pos;
}
Beispiel #2
0
bool Vector::isValidMultiname(const multiname& name, uint32_t& index)
{
	//First of all the multiname has to contain the null namespace
	//As the namespace vector is sorted, we check only the first one
	assert_and_throw(name.ns.size()!=0);
	if(name.ns[0].name!="")
		return false;

	index=0;
	switch(name.name_type)
	{
		//We try to convert this to an index, otherwise bail out
		case multiname::NAME_STRING:
        {
			if(name.name_s.empty())
                return false;
            ASString* s = Class<ASString>::getInstanceS(name.name_s);
            number_t n = s->toNumber();
            delete s;
            if(!Number::isInteger(n) || n<0)
                return false;
            index = n;
            break;
        }
		//This is already an int, so its good enough
		case multiname::NAME_INT:
			if(name.name_i < 0)
				throw Class<RangeError>::getInstanceS("Error #1125");
			index=name.name_i;
			break;
		case multiname::NAME_NUMBER:
			if(!Number::isInteger(name.name_d) || name.name_d < 0)
				throw Class<RangeError>::getInstanceS("Error #1125");
			index = name.name_d;
			break;
		case multiname::NAME_OBJECT:
			//TODO: should be use toPrimitive here?
			return false;
		default:
			throw UnsupportedException("Vector::isValidMultiname not completely implemented");
	}
	return true;
}