void VJSGlobalClass::do_ProgressIndicator(VJSParms_callStaticFunction& ioParms, VJSGlobalObject *inContext)
{
	VString sessiontile;
	VString windowtile, userinfo;
	bool caninterrupt = false;
	Real maxvalue = -1;
	VError err = VE_OK;
	
	if (ioParms.IsNumberParam(1))
		ioParms.GetRealParam(1, &maxvalue);
	else
		err = vThrowError(VE_JVSC_WRONG_PARAMETER_TYPE_NUMBER, "1");

	if (err == VE_OK )
	{
		if (ioParms.IsStringParam(2))
			ioParms.GetStringParam(2, sessiontile);
		else
			err = vThrowError(VE_JVSC_WRONG_PARAMETER_TYPE_STRING, "2");
	}

	caninterrupt = ioParms.GetBoolParam( 3, L"Can Interrupt", "Cannot Interrupt");

		
	//VProgressIndicator* progress = inContext->GetRuntimeDelegate()->CreateProgressIndicator( windowtile);

	if (err == VE_OK)
	{
		VProgressIndicator* progress = new VProgressIndicator();
		if (progress != NULL)
		{
			if (ioParms.CountParams() >= 4)
			{
				if (ioParms.IsStringParam(4))
				{
					ioParms.GetStringParam(4, windowtile);
					progress->SetTitle(windowtile);
				}
				else
					vThrowError(VE_JVSC_WRONG_PARAMETER_TYPE_STRING, "4");
			}
			if (ioParms.CountParams() >= 5)
			{
				if (ioParms.IsStringParam(5))
				{
					ioParms.GetStringParam(5, userinfo);
					progress->SetUserInfo(userinfo);
				}
				else
					vThrowError(VE_JVSC_WRONG_PARAMETER_TYPE_STRING, "5");
			}
			progress->BeginSession((sLONG8)maxvalue, sessiontile, caninterrupt);
		}
		ioParms.ReturnValue(VJSProgressIndicator::CreateInstance(ioParms.GetContextRef(), progress));
		
		ReleaseRefCountable( &progress);
	}
	else
		ioParms.ReturnNullValue();	
}	
void VJSTextStream::_Read (VJSParms_callStaticFunction &ioParms, VJSTextStreamState *inStreamState)
{
	if (inStreamState == NULL)

		XBOX::vThrowError(XBOX::VE_JVSC_INVALID_STATE, L"TextStream.read()");

	else if (ioParms.CountParams() == 1 && ioParms.IsStringParam(1)) {

		XBOX::VString	delimiter;

		if (!ioParms.GetStringParam(1, delimiter) || delimiter.GetLength() > 1)

			XBOX::vThrowError(XBOX::VE_JVSC_WRONG_PARAMETER_TYPE_STRING, "1");
			
		else {

			XBOX::VString	result;
			bool			hasBeenFound;

			hasBeenFound = inStreamState->_ReadUntilDelimiter(delimiter, &result);
			inStreamState->fPosition += result.GetLength();
			if (hasBeenFound)

				inStreamState->fPosition++;	// Delimiter is not included.

			ioParms.ReturnString(result);
		
		}

	} else {

		// numberCharacters is the number of actual characters to read, not the number of bytes.
				
		sLONG	numberCharacters;

		numberCharacters = 0;
		if (ioParms.CountParams() >= 1 && (!ioParms.GetLongParam(1, &numberCharacters) || numberCharacters < 0))

			XBOX::vThrowError(XBOX::VE_JVSC_WRONG_PARAMETER_TYPE_NUMBER, "1");
			
		else {

			XBOX::VString	result;
		
			inStreamState->_ReadCharacters(numberCharacters, &result);
			inStreamState->fPosition += result.GetLength();
			ioParms.ReturnString(result);

		}

	}
}
Ejemplo n.º 3
0
void VJSTimer::_ClearTimer (VJSParms_callStaticFunction &ioParms, VJSWorker *inWorker, bool inIsInterval) 
{
	xbox_assert(inWorker != NULL);

	if (!ioParms.CountParams() || !ioParms.IsNumberParam(1))

		return;
		
	sLONG			id;
	XBOX::VJSTimer	*timer;

	ioParms.GetLongParam(1, &id);
	if ((timer = inWorker->GetTimerContext()->LookUpTimer(id)) == NULL)

		return;	// No timer with given ID found.

	if (timer->IsInterval() != inIsInterval)

		return;	// Mismatched call (cannot used clearInterval() to clear a timeout for example).

	// Mark timer as "cleared".

	timer->_Clear();	

	// This will loop the event queue, trying to find the VJSTimerEvent.
	//
	// If the clearTimeout() or clearInterval() is executed inside "itself" (in its callback),
	// this will do nothing. The event is already executing, but as the timer is marked as 
	// "cleared", VJSTimerEvent::Discard() will free it.
	//
	// Otherwise, the loop will find the VJSTimerEvent object, calling its Discard() and thus 
	// freeing the timer.
	
	inWorker->UnscheduleTimer(timer);
}
void VJSGlobalClass::do_XmlToJSON(VJSParms_callStaticFunction& ioParms, VJSGlobalObject*)
{
	VString xml;
	VString json, root;
	bool simpleJSON = false;

	ioParms.GetStringParam( 1, xml);
	if ( ioParms.CountParams() >= 2)
	{
		VString s;
		if (ioParms.IsStringParam(2))
		{
			ioParms.GetStringParam(2, s);
			if(s== "json-bag")
			{
				simpleJSON = true;
				root="bag";
				if(ioParms.CountParams() >= 3)
				{
					if (ioParms.IsStringParam(3))
						ioParms.GetStringParam(3, root);
					else
						vThrowError(VE_JVSC_WRONG_PARAMETER_TYPE_STRING, "3");
				}
			}
		}
		else
			vThrowError(VE_JVSC_WRONG_PARAMETER_TYPE_STRING, "2");
	}

	if (simpleJSON)
	{
		XBOX::VValueBag *bag = new XBOX::VValueBag;

		VError err = LoadBagFromXML( xml, root, *bag);
		if(err == VE_OK)
			bag->GetJSONString(json);
		ReleaseRefCountable( &bag); 			
	}
	else
	{
		VString errorMessage; sLONG lineNumber;
		VError err = VXMLJsonUtility::XMLToJson(xml, json, errorMessage, lineNumber);
	}

	ioParms.ReturnString(json);
}
void VJSGlobalClass::do_JSONToXml(VJSParms_callStaticFunction& ioParms, VJSGlobalObject*)
{
	VString xml;
	VString json, root;
	bool simpleJSON = false;

	ioParms.GetStringParam( 1, json);
	if ( ioParms.CountParams() >= 2)
	{
		VString s;
		if (ioParms.IsStringParam(2))
		{
			ioParms.GetStringParam(2, s);
			if(s== "json-bag")
			{
				simpleJSON = true;
				root="bag";
				if(ioParms.CountParams() >= 3)
				{
					if (ioParms.IsStringParam(3))
						ioParms.GetStringParam(3, root);
					else
						vThrowError(VE_JVSC_WRONG_PARAMETER_TYPE_STRING, "3");
				}
			}
		}
		else
			vThrowError(VE_JVSC_WRONG_PARAMETER_TYPE_STRING, "2");
	}

	if (simpleJSON)
	{
		XBOX::VValueBag *bag = new XBOX::VValueBag;
		bag->FromJSONString(json);
		
		xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
		bag->DumpXML( xml, root, false);
		ReleaseRefCountable( &bag); 			
	}
	else
	{
		VError err = VXMLJsonUtility::JsonToXML(json, xml);
	}

	ioParms.ReturnString(xml);
}
Ejemplo n.º 6
0
void VJSStorageClass::_removeItem (VJSParms_callStaticFunction &ioParms, VJSStorageObject *inStorageObject)
{
	xbox_assert(inStorageObject != NULL);

	XBOX::VString	name;

	if (ioParms.CountParams() && ioParms.IsStringParam(1) && ioParms.GetStringParam(1, name))

		inStorageObject->RemoveKeyValue(name);
}
void VJSGlobalClass::do_trace( VJSParms_callStaticFunction& inParms, VJSGlobalObject*)
{
	size_t count = inParms.CountParams();
	for( size_t i = 1 ; i <= count ; ++i)	
	{
		VString msg;
		bool ok = inParms.GetStringParam( i, msg);
		if (!ok)
			break;
		VDebugMgr::Get()->DebugMsg( msg);
	}
}
Ejemplo n.º 8
0
void VJSStorageClass::_setItem (VJSParms_callStaticFunction &ioParms, VJSStorageObject *inStorageObject)
{
	xbox_assert(inStorageObject != NULL);

	XBOX::VString	name;

	if (ioParms.CountParams() && ioParms.IsStringParam(1) && ioParms.GetStringParam(1, name)) {

		XBOX::VJSValue	value(ioParms.GetContext());
		
		if (ioParms.CountParams() < 2)
		
			value.SetUndefined();

		else

			value = ioParms.GetParamValue(2);

		inStorageObject->SetKeyValue(name, value);

	}	
}
void VJSGlobalClass::do_dateToIso(VJSParms_callStaticFunction& ioParms, VJSGlobalObject*)
{
	VString s;
	VJSValue jsval(ioParms.GetContextRef());
	if (ioParms.CountParams() > 0)
	{
		VTime dd;
		jsval = ioParms.GetParamValue(1);
		jsval.GetTime(dd);
		dd.GetJSONString(s);
	}
	ioParms.ReturnString(s);
}
void VJSGlobalClass::do_Require(VJSParms_callStaticFunction& ioParms, VJSGlobalObject *inGlobalObject)
{
	xbox_assert(inGlobalObject != NULL);

	XBOX::VString	className;

	if (ioParms.CountParams() != 1 || !ioParms.IsStringParam(1) || !ioParms.GetStringParam(1, className))

		vThrowError(VE_JVSC_WRONG_PARAMETER_TYPE_STRING, "1");

	else

		ioParms.ReturnValue(inGlobalObject->Require(ioParms.GetContext(), className));
}
void VJSGlobalClass::do_testMe( VJSParms_callStaticFunction& inParms, VJSGlobalObject*)
{
	VJSObject globalObject( inParms.GetContext().GetGlobalObject());
	
	VString functionName;
	inParms.GetStringParam( 1, functionName);
	
	std::vector<VJSValue> values;
	size_t count = inParms.CountParams();
	for( size_t i = 2 ; i <= count ; ++i)
		values.push_back( inParms.GetParamValue( i));

	VJSValue result( inParms.GetContextRef());
	bool ok = globalObject.CallMemberFunction( functionName, &values, &result, inParms.GetExceptionRefPointer());
	inParms.ReturnValue( result);
}
Ejemplo n.º 12
0
void VJSDirectory::_save(VJSParms_callStaticFunction& ioParms, CUAGDirectory* inDirectory)
{
	VFile* dest = ioParms.RetainFileParam(1);

	if (dest != nil || ioParms.CountParams() == 0)

		ioParms.ReturnBool(inDirectory->Save(dest) == VE_OK);

	else {
		
		XBOX::vThrowError(XBOX::VE_JVSC_WRONG_PARAMETER_TYPE_FILE, "1");
		ioParms.ReturnBool(false);

	}

	QuickReleaseRefCountable(dest);
}
Ejemplo n.º 13
0
void VJSStorageClass::_getItem (VJSParms_callStaticFunction &ioParms, VJSStorageObject *inStorageObject)
{
	xbox_assert(inStorageObject != NULL);

	XBOX::VString	name;

	if (ioParms.CountParams() && ioParms.IsStringParam(1) && ioParms.GetStringParam(1, name)) {

		XBOX::VJSValue	value(ioParms.GetContext());

		inStorageObject->GetKeyValue(name, &value);

		ioParms.ReturnValue(value);

	} else

		ioParms.ReturnNullValue();
}
void VJSStorageClass::_key (VJSParms_callStaticFunction &ioParms, IJSStorageObject *inStorageObject)
{
	xbox_assert(inStorageObject != NULL);

	sLONG	index;

	if (ioParms.CountParams() && ioParms.IsNumberParam(1) && ioParms.GetLongParam(1, &index)) {

		XBOX::VJSValue	value(ioParms.GetContext());

		inStorageObject->GetKeyValue(index, &value);

		ioParms.ReturnValue(value);

	} else

		ioParms.ReturnNullValue();
}
Ejemplo n.º 15
0
void VJSGroup::_removeFrom(VJSParms_callStaticFunction& ioParms, CUAGGroup* inGroup)
{
	VError err = VE_OK;
	sLONG nbparam = ioParms.CountParams();
	for (sLONG i = 1; i <= nbparam /*&& err == VE_OK*/; i++)
	{
		if (ioParms.IsArrayParam(i))
		{
			VJSArray arr(ioParms.GetContext(), nil, false);
			ioParms.GetParamArray(i, arr);
			sLONG nbelem = arr.GetLength();
			for (sLONG j = 0; j < nbelem; ++j)
			{
				VJSValue val(arr.GetValueAt(j));
				if (val.IsString())
				{
					VString s;
					val.GetString(s);
					err = removeGroupFromGroup(ioParms, inGroup, s);
				}
				else /*if (val.IsInstanceOf("Group"))*/
				{
					CUAGGroup* group = val.GetObjectPrivateData<VJSGroup>();
					if (group != nil)
						err = inGroup->RemoveFromGroup(group);
				}
			}
		}
		else if (ioParms.IsStringParam(i))
		{
			VString s;
			ioParms.GetStringParam(i, s);
			err = removeGroupFromGroup(ioParms, inGroup, s);
		}
		else
		{
			CUAGGroup* group = ioParms.GetParamObjectPrivateData<VJSGroup>(i);
			err = inGroup->RemoveFromGroup(group);
		}
	}
}
Ejemplo n.º 16
0
void VJSUser::_putInto(VJSParms_callStaticFunction& ioParms, CUAGUser* inUser)
{
	VError err = VE_OK;
	sLONG nbparam = ioParms.CountParams();
	for (sLONG i = 1; i <= nbparam /*&& err == VE_OK*/; i++)
	{
		if (ioParms.IsArrayParam(i))
		{
			VJSArray arr(ioParms.GetContextRef(), nil, false);
			ioParms.GetParamArray(i, arr);
			sLONG nbelem = arr.GetLength();
			for (sLONG j = 0; j < nbelem; ++j)
			{
				VJSValue val(arr.GetValueAt(j));
				if (val.IsString())
				{
					VString s;
					val.GetString(s);
					err = putUserIntoGroup(ioParms, inUser, s);
				}
				else if (val.IsInstanceOf("Group"))
				{
					CUAGGroup* group = val.GetObjectPrivateData<VJSGroup>();
					err = inUser->PutIntoGroup(group);
				}
			}
		}
		else if (ioParms.IsStringParam(i))
		{
			VString s;
			ioParms.GetStringParam(i, s);
			err = putUserIntoGroup(ioParms, inUser, s);
		}
		else
		{
			CUAGGroup* group = ioParms.GetParamObjectPrivateData<VJSGroup>(i);
			err = inUser->PutIntoGroup(group);
		}
	}
}
void VJSTextStream::_Write (VJSParms_callStaticFunction &ioParms, VJSTextStreamState *inStreamState)
{
	if (inStreamState == NULL && !inStreamState->fStream->IsWriting()) 

		XBOX::vThrowError(XBOX::VE_JVSC_INVALID_STATE, L"TextStream.write()");

	else {

		XBOX::VString	string;
 
		if (ioParms.CountParams() != 1 || !ioParms.IsStringParam(1) || !ioParms.GetStringParam(1, string))
	
			XBOX::vThrowError(XBOX::VE_JVSC_WRONG_PARAMETER_TYPE_NUMBER, "1");

		else {
	
			inStreamState->fStream->PutText(string);
			inStreamState->fPosition += string.GetLength();

		}

	}
}
Ejemplo n.º 18
0
void VJSStorageClass::_key (VJSParms_callStaticFunction &ioParms, VJSStorageObject *inStorageObject)
{
	xbox_assert(inStorageObject != NULL);

	sLONG	index;

	if (ioParms.CountParams() && ioParms.IsNumberParam(1) && ioParms.GetLongParam(1, &index)) {

		XBOX::VString	key;

		inStorageObject->GetKeyFromIndex(index, &key);
		if (!key.IsEmpty())

			ioParms.ReturnString(key);

		else

			ioParms.ReturnNullValue();	// Index is out of bound.

	} else

		ioParms.ReturnNullValue();
}
Ejemplo n.º 19
0
void VJSTimer::_SetTimer (VJSParms_callStaticFunction &ioParms, VJSWorker *inWorker, bool inIsInterval) 
{
	xbox_assert(inWorker != NULL);

	if (!ioParms.CountParams())
		
		return;

	XBOX::VJSContext	context(ioParms.GetContext());
	XBOX::VJSObject		functionObject(context);

	ioParms.GetParamObject(1, functionObject);
	if (!functionObject.IsFunction())

		return;

	functionObject.Protect();

	Real	duration;

	duration = 0.0;
	if (ioParms.CountParams() >= 2) {

		if (ioParms.IsNumberParam(2)) {
			
			if (!ioParms.GetRealParam(2, &duration))
			
				duration = 0.0;

		} else {

			// According to specification, if timeout is an object, call its toString() method if any. 
			// Then apply ToNumber() on the string to obtain duration.
			
			XBOX::VJSObject	timeOutObject(context);

			if (ioParms.GetParamObject(2, timeOutObject)) {

				timeOutObject.SetContext(context);
				if (timeOutObject.HasProperty("toString")) {

					XBOX::VJSObject	toStringObject = timeOutObject.GetPropertyAsObject("toString");

					if (toStringObject.IsFunction()) {

						std::vector<XBOX::VJSValue>	values;
						XBOX::VJSValue				string(context);

						toStringObject.SetContext(context);
						timeOutObject.CallFunction(toStringObject, &values, &string, NULL);

						if (string.IsString()) {

							// If Number() is called as a function (and not as a constructor), it acts as ToNumber().
							// See section 15.7.1 of ECMA-262 specification.

							XBOX::VJSObject	toNumberObject = context.GetGlobalObject().GetPropertyAsObject("Number");

							if (toNumberObject.IsFunction()) {

								XBOX::VJSValue	number(context);

								values.clear();
								values.push_back(string);
								toNumberObject.SetContext(context);
								context.GetGlobalObject().CallFunction(toNumberObject, &values, &number, NULL);

								if (number.IsNumber() && !number.GetReal(&duration))

									duration = 0.0;

							}

						}

					}

				}

			}
		
		}

		// (value != value) is true if value is a NaN.

		if (duration < 0.0 || duration > XBOX::kMAX_Real || duration != duration)
		
			duration = 0.0;

	}	
	
	std::vector<XBOX::VJSValue> *arguments;

	arguments = new std::vector<XBOX::VJSValue>;
	for (sLONG i = 3; i <= ioParms.CountParams(); i++) 

		arguments->push_back(ioParms.GetParamValue(i));

	sLONG		period, id;
	VJSTimer	*timer;

	period = (sLONG) duration;
	if (inIsInterval) {

		if (period < VJSTimer::kMinimumInterval)

			period = VJSTimer::kMinimumInterval;

	} else {

		if (period < VJSTimer::kMinimumTimeout)

			period = VJSTimer::kMinimumTimeout;

	}		 
	timer = new VJSTimer(functionObject, inIsInterval ? period : VJSTimer::kTimeOut);
	if ((id = inWorker->GetTimerContext()->InsertTimer(timer)) < 0) {

		// Too many timers (should never happen). Silently ignore. 
		// Returned ID (-1) isn't valid and a clear on it, will do nothing.

		timer->Release();
		delete arguments;
	
	} else {

		XBOX::VTime	triggerTime;
	
		triggerTime.FromSystemTime();
		triggerTime.AddMilliseconds(period);

		inWorker->QueueEvent(VJSTimerEvent::Create(timer, triggerTime, arguments));

	}

	ioParms.ReturnNumber(id);
}
void VJSStream::_PutBinary (VJSParms_callStaticFunction &ioParms, XBOX::VStream *inStream)
{
	xbox_assert(inStream != NULL);

	XBOX::VJSObject	binaryObject(ioParms.GetContext());
		
	if (!ioParms.GetParamObject(1, binaryObject)) {
	
		XBOX::vThrowError(XBOX::VE_JVSC_WRONG_PARAMETER, "1");
		return;

	} 

	bool			isBuffer;
	VJSBufferObject	*buffer;
	VJSDataSlice	*dataSlice;
	const void		*data;
	VSize			size;

	if (binaryObject.IsOfClass(VJSBufferClass::Class())) {
		
		isBuffer = true;

		buffer = binaryObject.GetPrivateData<VJSBufferClass>();
		xbox_assert(buffer != NULL);

		buffer->Retain();

		data = buffer->GetDataPtr();
		size = buffer->GetDataSize();

	} else if (binaryObject.IsOfClass(VJSBlob::Class())) {

		VJSBlobValue	*blob;

		isBuffer = false;

		blob = binaryObject.GetPrivateData<VJSBlob>();
		xbox_assert(blob != NULL);

		dataSlice = blob->RetainDataSlice();
		xbox_assert(dataSlice != NULL);
		
		data = dataSlice->GetDataPtr();
		size = dataSlice->GetDataSize();

	} else {

		XBOX::vThrowError(XBOX::VE_JVSC_WRONG_PARAMETER, "1");
		return;

	}

	bool	isOk;
	sLONG	index, length;

	isOk = true;	
		
	if (ioParms.CountParams() >= 2) {
		
		if (!ioParms.GetLongParam(2, &index)) {

			XBOX::vThrowError(XBOX::VE_JVSC_WRONG_PARAMETER_TYPE_NUMBER, "2");
			isOk = false;

		} 

	} else

		index = 0;

	if (isOk && ioParms.CountParams() >= 3) {
		
		if (!ioParms.GetLongParam(3, &length)) {

			XBOX::vThrowError(XBOX::VE_JVSC_WRONG_PARAMETER_TYPE_NUMBER, "3");
			isOk = false;

		} 

	} else {

		length = (sLONG) size;
		if (index > 0)

			length -= index;

	}

	// Silently ignore if out of bound.

	if (isOk && index >= 0 && length > 0 && index + length <= size) {

		XBOX::VError	error;

		if ((error = inStream->PutData((uBYTE *) data + index, length)) != XBOX::VE_OK)

			XBOX::vThrowError(error);

	}

	if (isBuffer)

		buffer->Release();

	else

		dataSlice->Release();

}