void VJSLanguageSyntaxTester::_GetSyntaxColoring( XBOX::VJSParms_callStaticFunction &ioParams, void * )
{
	// We expect two parameters -- the extension for the type of information we have (js, html, etc) and
	// a line of text to be colored.  We will perform the coloring, and return back to the user a list
	// of objects that contain the style information.
	if (ioParams.CountParams() < 2)	return;

	VString extension, data;
	if (!ioParams.GetStringParam( 1, extension ))	return;
	if (!ioParams.GetStringParam( 2, data ))		return;

	CLanguageSyntaxComponent *languageSyntax = (CLanguageSyntaxComponent *)VComponentManager::RetainComponent( (CType)CLanguageSyntaxComponent::Component_Type );
	if (!languageSyntax)	return;
	ISyntaxInterface *syntaxEngine = languageSyntax->GetSyntaxByExtension( extension );
	if (syntaxEngine) {
		// Now that we've got the syntax engine, we can ask it for the coloring information
		std::vector< size_t > offsets;
		std::vector< size_t > lengths;
		std::vector< sLONG > styles;
		syntaxEngine->GetColoringForTesting( data, offsets, lengths, styles );

		// Loop over the returned results, turning them into a vector of VJSValue objects
		std::vector< VJSValue > results;
		std::vector< JSColorResult * > releaseList;
		for (size_t i = 0; i < offsets.size(); i++) {
			JSColorResult *result = new JSColorResult( offsets[ i ], lengths[ i ], styles[ i ] );
			releaseList.push_back( result );
			results.push_back( VJSLanguageSyntaxTesterColorResults::CreateInstance( ioParams.GetContextRef(), result ) );
		}

		// Return an array of our results
		VJSArray arr( ioParams.GetContextRef() );
		arr.PushValues( results );
		ioParams.ReturnValue( arr );

		for (std::vector< JSColorResult * >::iterator iter = releaseList.begin(); iter != releaseList.end(); ++iter) {
			(*iter)->Release();
		}
	}

	languageSyntax->Release();
}
void VJSLanguageSyntaxTester::_Tokenize( XBOX::VJSParms_callStaticFunction &ioParams, void * )
{
	// We are expecting 2 JavaScript parameters:
	// - source code to tokenize
	// - extension of the fake file

	VString sourceCode, extension;

	if (ioParams.CountParams() == 2)
	{
		if (!ioParams.GetStringParam( 1, sourceCode ))	return;
		if (!ioParams.GetStringParam( 2, extension ))	return;
	}
	else
		return;

	CLanguageSyntaxComponent *languageSyntax = (CLanguageSyntaxComponent *)VComponentManager::RetainComponent( (CType)CLanguageSyntaxComponent::Component_Type );
	if (languageSyntax)
	{
		ISyntaxInterface *syntaxEngine = languageSyntax->GetSyntaxByExtension( extension );

		if (syntaxEngine)
		{
			VectorOfVString	tokens;

			syntaxEngine->GetTokensForTesting(sourceCode, tokens);

			VJSArray result( ioParams.GetContextRef() );
			VJSValue jsval(ioParams.GetContextRef());

			for (VectorOfVString::const_iterator it = tokens.begin(); it != tokens.end(); ++it)
			{
				jsval.SetString(*it);
				result.PushValue(jsval);
			}
			ioParams.ReturnValue(result);
 		}
		languageSyntax->Release();
	}
}
void VJSImage::_saveMeta(XBOX::VJSParms_callStaticFunction& ioParms, VJSPictureContainer* inPict)
{
	VPicture* pic = inPict->GetPict();
	if (pic != nil)
	{
		bool okmeta = false;
		VString jsonString;
		if (ioParms.IsObjectParam(1))
		{
			VJSObject metadata(ioParms.GetContextRef());
			ioParms.GetParamObject(1, metadata);
			okmeta = true;
			VJSJSON json(ioParms.GetContextRef());
			json.Stringify(metadata, jsonString);
		}
		else
		{
			if (inPict->MetaInfoInited())
			{
				okmeta = true;
				VJSValue metadata(ioParms.GetContextRef(), inPict->GetMetaInfo());
				VJSJSON json(ioParms.GetContextRef());
				json.Stringify(metadata, jsonString);
			}
		}
		if (okmeta)
		{
			VValueBag* bag = new VValueBag();
			bag->FromJSONString(jsonString);
			inPict->SetMetaBag(bag);
			QuickReleaseRefCountable(bag);

		}
		//inPict->SetModif();
	}
}
void VJSLanguageSyntaxTester::_GotoDefinition( XBOX::VJSParms_callStaticFunction &ioParams, void * )
{
	VString filePathStr, symTablePathStr, selectionStr;
	sLONG	line = 0;

	if (ioParams.CountParams() == 4)
	{
		if (!ioParams.GetStringParam( 1, symTablePathStr )) return;
		if (!ioParams.GetStringParam( 2, filePathStr ))		return;
		if (!ioParams.GetLongParam( 3, &line ))				return;
		if (!ioParams.GetStringParam( 4, selectionStr ))	return;
	}
	else
		return;

	VFilePath symbolTablePath( symTablePathStr, FPS_POSIX);
	VFilePath filePath( filePathStr, FPS_POSIX);

	if ( ! symbolTablePath.IsValid() || ! filePath.IsValid() )
		return;

	if ( line < 1 )
		return;

	CLanguageSyntaxComponent *languageSyntax = (CLanguageSyntaxComponent *)VComponentManager::RetainComponent( (CType)CLanguageSyntaxComponent::Component_Type );
	if (languageSyntax)
	{
		// First, load up the symbol table as an actual table instead of just a path string
		ISymbolTable *symTable = NULL;
		if (!symTablePathStr.IsEmpty())
		{
			symTable = languageSyntax->CreateSymbolTable();
			if (symTable)
			{
				VFile file(symbolTablePath);

				if (symTable->OpenSymbolDatabase( file ))
				{
					// Now that we have all of the parameters grabbed, we can figure out which language syntax engine
					// we want to load up (based on the test file), and ask it to give us completions.  We can then take
					// those completions and turn them into an array of values to pass back to the caller.
					VString extension;
					filePath.GetExtension( extension );
					ISyntaxInterface *syntaxEngine = languageSyntax->GetSyntaxByExtension( extension );
					if (syntaxEngine)
					{
						// Now that we've got the syntax engine, we can ask it for the completions we need
						std::vector< IDefinition > definitions;
						syntaxEngine->GetDefinitionsForTesting( selectionStr, symTable, filePath.GetPath(), line, definitions );

						JSDefinitionResult *results = new JSDefinitionResult( definitions );
						ioParams.ReturnValue( VJSLanguageSyntaxTesterDefinitionResults::CreateInstance( ioParams.GetContextRef(), results ) );
						results->Release();
					}
				}
				symTable->Release();
			}
		}
		languageSyntax->Release();
	}
}
void VJSLanguageSyntaxTester::_GetSymbol( XBOX::VJSParms_callStaticFunction &ioParams, void * )
{
	// We are expecting 4 JavaScript parameters:
	// - path to the symbol table
	// - symbol name
	// - symbol definition file
	// - symbol definition line number
	
	VString	symTablePathStr, symbolName, symbolDefPath;
	sLONG	symbolDefLineNumber;

	if (ioParams.CountParams() == 4) {
		if (!ioParams.GetStringParam( 1, symTablePathStr )) return;
		if (!ioParams.GetStringParam( 2, symbolName ))		return;
		if (!ioParams.GetStringParam( 3, symbolDefPath )) return;
		if (!ioParams.GetLongParam( 4, &symbolDefLineNumber ))		return;
	}
	else
		return;
	
	VFilePath symbolTablePath( symTablePathStr, FPS_POSIX);
	VFilePath symbolDefFilePath( symbolDefPath, FPS_POSIX);

	if ( ! symbolTablePath.IsValid() || ! symbolDefFilePath.IsValid() )
		return;

	CLanguageSyntaxComponent *languageSyntax = (CLanguageSyntaxComponent *)VComponentManager::RetainComponent( (CType)CLanguageSyntaxComponent::Component_Type );
	if (languageSyntax)
	{
		// First, load up the symbol table as an actual table instead of just a path string
		ISymbolTable *symTable = NULL;
		if (!symTablePathStr.IsEmpty())
		{
			symTable = languageSyntax->CreateSymbolTable();
			if (symTable)
			{
				VFile file(symbolTablePath);

				if (symTable->OpenSymbolDatabase( file ))
				{
					// Get symbol file
					std::vector< Symbols::IFile * > files = symTable->GetFilesByPathAndBaseFolder( symbolDefFilePath, eSymbolFileBaseFolderProject );	
					if ( ! files.empty() )
					{
						Symbols::IFile *file = files.front();
						file->Retain();
						for (std::vector< Symbols::IFile * >::iterator iter = files.begin(); iter != files.end(); ++iter)
							(*iter)->Release();

						// Gets symbol corresponding to given parameters
						Symbols::ISymbol* owner = symTable->GetSymbolOwnerByLine(file, symbolDefLineNumber);
						Symbols::ISymbol* sym = NULL;

						if (NULL != owner && owner->GetName() == symbolName)
						{
							sym = owner;
							sym->Retain();
						}
						
						if (NULL == sym)
						{
							std::vector< Symbols::ISymbol * >	syms = symTable->GetSymbolsByName(owner, symbolName,file);
							for (std::vector< Symbols::ISymbol * >::iterator iter = syms.begin(); iter != syms.end(); ++iter)
							{
								if ( ( (*iter)->GetLineNumber() + 1 ) == symbolDefLineNumber )
								{
									sym = *iter;
									(*iter)->Retain();
								}
								(*iter)->Release();
							}	
						}

						if (NULL != sym)
						{
							VJSObject	result(ioParams.GetContextRef());
							VJSValue	jsval(ioParams.GetContextRef());
							VString		signature;

							symTable->GetSymbolSignature( sym, signature );

							result.MakeEmpty();

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

							jsval.SetString( sym->GetTypeName() );
							result.SetProperty(L"type", jsval, JS4D::PropertyAttributeNone);

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

							jsval.SetString( signature );
							result.SetProperty(L"signature", jsval, JS4D::PropertyAttributeNone);

							jsval.SetString( sym->GetKindString() );
							result.SetProperty(L"kind", jsval, JS4D::PropertyAttributeNone);

							ioParams.ReturnValue(result);
							sym->Release();
						}
						
						if (NULL != owner)
							owner->Release();
						file->Release();	
					}
				}
				symTable->Release();
			}
		}
		languageSyntax->Release();
	}
}
void VJSLanguageSyntaxTester::_GetCompletions( XBOX::VJSParms_callStaticFunction &ioParams, void * )
{
	// We are expecting 5 parameters for JavaScript.  One for the path to the symbol table, one for the path to the test file (which
	// must already be a part of the symbol table), one for the line number within the file, one for the string that
	// we are going to be completing at that point and one for the completion mode.  If we get only three parameters, then it's a CSS completion test, which is
	// fine -- we just modify the parameters we pass into GetSuggestionsForTesting.
	
	VString symTablePathStr, testFilePathStr, completionString, completionModeStr;
	sLONG completionLocation;
	if (ioParams.CountParams() == 5) {
		if (!ioParams.GetStringParam( 1, symTablePathStr ))		return;
		if (!ioParams.GetStringParam( 2, completionString ))	return;
		if (!ioParams.GetStringParam( 3, testFilePathStr ))		return;
		if (!ioParams.GetLongParam( 4, &completionLocation ))	return;
		if (!ioParams.GetStringParam( 5, completionModeStr ))	return;
	} else if (ioParams.CountParams() == 3) {
		bool curlyBraces;
		if (!ioParams.GetStringParam( 1, completionString ))	return;
		if (!ioParams.GetBoolParam( 2, &curlyBraces ))			return;
		if (!ioParams.GetStringParam( 3, testFilePathStr ))		return;

		// We fudge the completion location information when working with CSS files so that it
		// denotes whether we're inside of curly braces or not.
		completionLocation = curlyBraces ? 1 : 0;
	} else {
		return;
	}
	
	VFilePath symbolTablePath( symTablePathStr, FPS_POSIX);
	VFilePath testFilePath( testFilePathStr, FPS_POSIX);

	if ( ! symbolTablePath.IsValid() || ! testFilePath.IsValid() )
		return;

	EJSCompletionTestingMode completionMode;
	if (completionModeStr == "text")
		completionMode = eText;
	else if (completionModeStr == "displayText")
		completionMode = eDisplayText;
	else
		return;

	CLanguageSyntaxComponent *languageSyntax = (CLanguageSyntaxComponent *)VComponentManager::RetainComponent( (CType)CLanguageSyntaxComponent::Component_Type );
	if (languageSyntax)
	{
		// First, load up the symbol table as an actual table instead of just a path string
		ISymbolTable *symTable = NULL;
		if (!symTablePathStr.IsEmpty())
		{
			symTable = languageSyntax->CreateSymbolTable();
			if ( symTable )
			{
				VFile file(symbolTablePath);
				if (symTable->OpenSymbolDatabase( file ))
				{
					// Now that we have all of the parameters grabbed, we can figure out which language syntax engine
					// we want to load up (based on the test file), and ask it to give us completions.  We can then take
					// those completions and turn them into an array of values to pass back to the caller.
					VString extension;
					testFilePath.GetExtension( extension );
					ISyntaxInterface *syntaxEngine = languageSyntax->GetSyntaxByExtension( extension );
					if (syntaxEngine)
					{
						// Now that we've got the syntax engine, we can ask it for the completions we need
						std::vector< VString > suggestions;
						syntaxEngine->GetSuggestionsForTesting( completionString, symTable, testFilePath.GetPath() , completionLocation, completionMode, suggestions );

						JSResult *results = new JSResult( suggestions );
						ioParams.ReturnValue( VJSLanguageSyntaxTesterResults::CreateInstance( ioParams.GetContextRef(), results ) );
						results->Release();
					}
				}
				symTable->Release();
			}
		}
		languageSyntax->Release();
	}
}