示例#1
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);
}
示例#2
0
void VJSSession::_unPromote(VJSParms_callStaticFunction& ioParms, CUAGSession* inSession)
{
	sLONG promotionToken = 0;
	ioParms.GetLongParam(1, &promotionToken);
	CUAGThreadPrivilege* privileges = static_cast<CUAGThreadPrivilege*>(ioParms.GetContext().GetGlobalObjectPrivateInstance()->GetSpecific('uagX'));
	inSession->UnPromoteFromToken(promotionToken, privileges);
}
void VJSLanguageSyntaxTesterDefinitionResults::_GetResult( VJSParms_callStaticFunction &ioParams, IJSLanguageSyntaxTesterDefinitionResults *inResults )
{
	if (inResults) {
		sLONG index;
		if (ioParams.GetLongParam( 1, &index ))
		{
			IDefinition definition = inResults->GetResult( index );
			VJSObject	result(ioParams.GetContextRef());
			VJSValue	jsval(ioParams.GetContextRef());

			result.MakeEmpty();

			jsval.SetString( definition.GetName() );
			result.SetProperty(L"name", jsval, JS4D::PropertyAttributeNone);

			jsval.SetString( definition.GetFilePath() );
			result.SetProperty(L"file", jsval, JS4D::PropertyAttributeNone);

			jsval.SetNumber( definition.GetLineNumber() + 1 );
			result.SetProperty(L"line", jsval, JS4D::PropertyAttributeNone);

			ioParams.ReturnValue(result);
		}
	}
}
void VJSLanguageSyntaxTesterResults::_GetResult( VJSParms_callStaticFunction &ioParams, IJSLanguageSyntaxTesterResults *inResults )
{
	if (inResults) {
		sLONG index;
		if (ioParams.GetLongParam( 1, &index )) {
			ioParams.ReturnString( inResults->GetResult( index ) );
		}
	}
}
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);

		}

	}
}
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();
}
void VJSGlobalClass::do_LoadText(VJSParms_callStaticFunction& ioParms, VJSGlobalObject*)
{
	bool okloaded = false;

	VFile* file = ioParms.RetainFileParam(1);

	if (file != nil)
	{
		if (file->Exists())
		{
			VFileStream inp(file);
			VError err = inp.OpenReading();
			if (err == VE_OK)
			{
				CharSet defaultCharSet = VTC_UTF_8;
				sLONG xcharset = 0;
				if (ioParms.GetLongParam(2, &xcharset) && xcharset != 0)
					defaultCharSet = (CharSet)xcharset;
				inp.GuessCharSetFromLeadingBytes(defaultCharSet );
				inp.SetCarriageReturnMode( eCRM_NATIVE );
				VString s;
				err = inp.GetText(s);
				if (err == VE_OK)
				{
					ioParms.ReturnString(s);
					okloaded = true;
				}
				inp.CloseReading();
			}
			
			
		}
		file->Release();
	}
	else
		vThrowError(VE_JVSC_WRONG_PARAMETER_TYPE_FILE, "1");

	if (!okloaded)
		ioParms.ReturnNullValue();

}
void VJSGlobalClass::do_SaveText(VJSParms_callStaticFunction& ioParms, VJSGlobalObject*)
{
	VString s;
	if (ioParms.IsStringParam(1))
	{
		if (ioParms.GetStringParam(1, s))
		{
			VFile* file = ioParms.RetainFileParam(2);
			if (file != nil)
			{
				VError err = VE_OK;
				if (file->Exists())
					err = file->Delete();
				if (err == VE_OK)
					err = file->Create();
				if (err == VE_OK)
				{
					CharSet defaultCharSet = VTC_UTF_8;
					sLONG xcharset = 0;
					if (ioParms.GetLongParam(3, &xcharset) && xcharset != 0)
						defaultCharSet = (CharSet)xcharset;
					VFileStream inp(file);
					err = inp.OpenWriting();
					if (err == VE_OK)
					{
						inp.SetCharSet(defaultCharSet);
						inp.PutText(s);
						inp.CloseWriting();
					}
				}
			}
			else
				vThrowError(VE_JVSC_WRONG_PARAMETER_TYPE_FILE, "2");
		}
	}
	else
		vThrowError(VE_JVSC_WRONG_PARAMETER_TYPE_STRING, "1");
}
示例#9
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();
}
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();

}
void VJSStream::_GetBinary (VJSParms_callStaticFunction &ioParms, XBOX::VStream* inStream, bool inIsBuffer)
{
	xbox_assert(inStream != NULL);

	sLONG	size;
	void	*buffer;

	if (!ioParms.GetLongParam(1, &size) || size < 0) {

		XBOX::vThrowError(XBOX::VE_JVSC_WRONG_PARAMETER_TYPE_NUMBER, "1");
		ioParms.ReturnNullValue();

	} else if (!size) {

		if (inIsBuffer)

			ioParms.ReturnValue(VJSBufferClass::NewInstance(ioParms.GetContext(), 0, NULL));

		else 

			ioParms.ReturnValue(VJSBlob::NewInstance(ioParms.GetContext(), 0, NULL, ""));
			
	} else if ((buffer = ::malloc(size)) == NULL) {
	
		vThrowError(XBOX::VE_MEMORY_FULL);
		ioParms.ReturnNullValue();
	
	} else {

		XBOX::VError	error;
		VSize			bytesRead;

		// Prevent error throwing for XBOX::VE_STREAM_EOF, this will allow to read less than requested.

		{
			StErrorContextInstaller	context(false, true);

			error = inStream->GetData(buffer, size, &bytesRead);
		}

/*
		// Do ::realloc()?

		if (bytesRead < size)

			buffer = ::realloc(buffer, bytesRead);
*/
				
		if (error != XBOX::VE_OK && error != XBOX::VE_STREAM_EOF) {

			XBOX::vThrowError(error);
			ioParms.ReturnNullValue();
			::free(buffer);

		} else {
			
			if (inIsBuffer) 

				ioParms.ReturnValue(VJSBufferClass::NewInstance(ioParms.GetContext(), bytesRead, buffer));

			else

				ioParms.ReturnValue(VJSBlob::NewInstance(ioParms.GetContext(), bytesRead, buffer, ""));

			// Check if instance creation has succeed, if not free memory.

			if (ioParms.GetReturnValue().IsNull())

				::free(buffer);
			
		}	

	}
}
void VJSSession::_unPromote(VJSParms_callStaticFunction& ioParms, CUAGSession* inSession)
{
	sLONG promotionToken = 0;
	ioParms.GetLongParam(1, &promotionToken);
	inSession->UnPromoteFromToken(promotionToken);
}