コード例 #1
0
ファイル: JSON.cpp プロジェクト: netroby/lightspark
ASFUNCTIONBODY(JSON,_stringify)
{
	_NR<ASObject> value;
	ARG_UNPACK(value);
	std::vector<ASObject *> path;
	tiny_string filter;
	IFunction* replacer = NULL;
	if (argslen > 1 && !args[1]->is<Null>() && !args[1]->is<Undefined>())
	{
		if (args[1]->is<IFunction>())
		{
			replacer = args[1]->as<IFunction>();
		}
		else if (args[1]->is<Array>())
		{
			filter = " ";
			Array* ar = args[1]->as<Array>();
			for (uint64_t i = 0; i < ar->size(); i++)
			{
				filter += ar->at(i)->toString();
				filter += " ";
			}
		}
		else
			throwError<TypeError>(kJSONInvalidReplacer);
	}

	tiny_string spaces = "";
	if (argslen > 2)
	{
		ASObject* space = args[2];
		spaces = "          ";
		if (space->is<Number>() || space->is<Integer>() || space->is<UInteger>())
		{
			int32_t v = space->toInt();
			if (v < 0) v = 0;
			if (v > 10) v = 10;
			spaces = spaces.substr_bytes(0,v);
		}
		else if (space->is<Boolean>() || space->is<Null>())
		{
			spaces = "";
		}
		else
		{
			if(space->has_toString())
			{
				_R<ASObject> ret = space->call_toString();
				spaces = ret->toString();
			}
			else
				spaces = space->toString();
			if (spaces.numBytes() > 10)
				spaces = spaces.substr_bytes(0,10);
		}
	}
	tiny_string res = value->toJSON(path,replacer,spaces,filter);

	return Class<ASString>::getInstanceS(res);
}
コード例 #2
0
ファイル: Vector.cpp プロジェクト: jobermayr/lightspark
ASFUNCTIONBODY(Vector, every)
{
	Vector* th=static_cast<Vector*>(obj);
	if (argslen < 1)
		throwError<ArgumentError>(kWrongArgumentCountError, "Vector.some", "1", Integer::toString(argslen));
	if (!args[0]->is<IFunction>())
		throwError<TypeError>(kCheckTypeFailedError, args[0]->getClassName(), "Function");
	IFunction* f = static_cast<IFunction*>(args[0]);
	ASObject* params[3];
	ASObject *funcRet;

	for(unsigned int i=0; i < th->size(); i++)
	{
		if (th->vec[i])
		{
			params[0] = th->vec[i];
			th->vec[i]->incRef();
		}
		else
			params[0] = obj->getSystemState()->getNullRef();
		params[1] = abstract_i(obj->getSystemState(),i);
		params[2] = th;
		th->incRef();

		if(argslen==1)
		{
			funcRet=f->call(obj->getSystemState()->getNullRef(), params, 3);
		}
		else
		{
			args[1]->incRef();
			funcRet=f->call(args[1], params, 3);
		}
		if(funcRet)
		{
			if (funcRet->is<Undefined>() || funcRet->is<Null>())
				throwError<TypeError>(kCallOfNonFunctionError, funcRet->toString());
			if(!Boolean_concrete(funcRet))
			{
				return funcRet;
			}
			funcRet->decRef();
		}
	}
	return abstract_b(obj->getSystemState(),true);
}