Advertisement

Build fails - dependent on scope?

Started by May 12, 2007 12:57 AM
1 comment, last by _Sigma 17 years, 6 months ago
Must admit I'm at a loss here. If I call the function CompileScript from my kernel I get massive errors from AS on compiling (unknown symbol at row 1 col 1 ). It breaks on the build(...) command. This doesn't work:

//called from here
void TKernel::InitializeScript()
{
	TScript::GetInstance().Initialize();
	TScript::GetInstance().ExposeFn("void Log(string message)",asFUNCTION(TLog::WriteToLog));

	TScript::GetInstance().AddScript("Scripts\\main.tscript");

	TScript::GetInstance().CompileScripts();
	
	
}


....

void TScript::AddScript( std::string scriptPath)
{
	
	int r = 0; //return value from AS
	int len = 0; //length of script
	std::string script; //holds script
	std::string sname; //name used to reference script

	//load script from file on disk
	try
	{
		 m_script.readFile(scriptPath.c_str());
		 script = m_script.lookup("Script.script");
		 sname = m_script.lookup("Script.scriptName");
		 len = script.length();
	}
	catch (libconfig::ParseException& e) 
	{
		throw TFatalException("Script: Error on line: " +  boost::lexical_cast<std::string>(e.getLine()) + 
			". Error: " + boost::lexical_cast<std::string>(e.getError()) + " In file: " + scriptPath 
			+ "  \n\n Stack:\nTScript::CompileScript()");
	}
	catch (libconfig::FileIOException& e) 
	{
		throw TFatalException("Failed to load script file. \n\nReason: Failed to open file " + scriptPath 
			+ ".\n  Stack:\n TScript::CompileScript()");
	}
	catch(libconfig::SettingNotFoundException &e)
	{
		throw TFatalException("A required setting in " + scriptPath + "was not found. \n\n Stack: TScript::CompileScript()\n");
	}
	catch (...)
	{
		throw TFatalException("Unknown error parsing " + scriptPath + " \n in TScript::CompileScript()" );
	}


	// Add the script sections that will be compiled into executable code.
	// If we want to combine more than one file into the same script, then 
	// we can call AddScriptSection() several times for the same module and
	// the script engine will treat them all as if they were one. The script
	// section name, will allow us to localize any errors in the script code.
	r = m_engine->AddScriptSection(0, sname.c_str(), &script[0], len, 0, false);

	if( r < 0 ) 
	{
		TLog::GetInstance().LogMessage("TScript::CompileScript","AddScriptSection() failed on" + scriptPath,MSG_ERROR);

		throw TFatalException("AddScriptSection() failed on " + scriptPath + "\n\nIn:\nTScript::CompileScript() ");
	}

	// Compile the script.
	//CompileScripts();
	
}

void TScript::CompileScripts()
{
	// Compile the script. If there are any compiler messages they will
		// be written to the message stream that we set right after creating the 
		// script engine. If there are no errors, and no warnings, nothing will
		// be written to the stream.
	int r = m_engine->Build(0);

	if( r < 0 )
	{
		TLog::GetInstance().LogMessage("TScript::CompileScript","Build() failed" ,MSG_ERROR);

		throw TFatalException("Build() failed failed on module 0 \n\nIn:\nTScript::CompileScript() ");
	}

}



yet this works

//called from here
void TKernel::InitializeScript()
{
	TScript::GetInstance().Initialize();
	TScript::GetInstance().ExposeFn("void Log(string message)",asFUNCTION(TLog::WriteToLog));

	TScript::GetInstance().AddScript("Scripts\\main.tscript");

	//TScript::GetInstance().CompileScripts();
	
	
}


....

void TScript::AddScript( std::string scriptPath)
{
	
	int r = 0; //return value from AS
	int len = 0; //length of script
	std::string script; //holds script
	std::string sname; //name used to reference script

	//load script from file on disk
	try
	{
		 m_script.readFile(scriptPath.c_str());
		 script = m_script.lookup("Script.script");
		 sname = m_script.lookup("Script.scriptName");
		 len = script.length();
	}
	catch (libconfig::ParseException& e) 
	{
		throw TFatalException("Script: Error on line: " +  boost::lexical_cast<std::string>(e.getLine()) + 
			". Error: " + boost::lexical_cast<std::string>(e.getError()) + " In file: " + scriptPath 
			+ "  \n\n Stack:\nTScript::CompileScript()");
	}
	catch (libconfig::FileIOException& e) 
	{
		throw TFatalException("Failed to load script file. \n\nReason: Failed to open file " + scriptPath 
			+ ".\n  Stack:\n TScript::CompileScript()");
	}
	catch(libconfig::SettingNotFoundException &e)
	{
		throw TFatalException("A required setting in " + scriptPath + "was not found. \n\n Stack: TScript::CompileScript()\n");
	}
	catch (...)
	{
		throw TFatalException("Unknown error parsing " + scriptPath + " \n in TScript::CompileScript()" );
	}


	// Add the script sections that will be compiled into executable code.
	// If we want to combine more than one file into the same script, then 
	// we can call AddScriptSection() several times for the same module and
	// the script engine will treat them all as if they were one. The script
	// section name, will allow us to localize any errors in the script code.
	r = m_engine->AddScriptSection(0, sname.c_str(), &script[0], len, 0, false);

	if( r < 0 ) 
	{
		TLog::GetInstance().LogMessage("TScript::CompileScript","AddScriptSection() failed on" + scriptPath,MSG_ERROR);

		throw TFatalException("AddScriptSection() failed on " + scriptPath + "\n\nIn:\nTScript::CompileScript() ");
	}

	// Compile the script.
	CompileScripts();
	
}

void TScript::CompileScripts()
{
	// Compile the script. If there are any compiler messages they will
		// be written to the message stream that we set right after creating the 
		// script engine. If there are no errors, and no warnings, nothing will
		// be written to the stream.
	int r = m_engine->Build(0);

	if( r < 0 )
	{
		TLog::GetInstance().LogMessage("TScript::CompileScript","Build() failed" ,MSG_ERROR);

		throw TFatalException("Build() failed failed on module 0 \n\nIn:\nTScript::CompileScript() ");
	}

}



No changes to the script. Only differences in the code is that compileScripts is called from addScript instead of the from the kernel. m_engine is a private member var of TScript. Am I being silly and have just missed something? Cheers _S
In the example that fails, your script variable is going out of scope before you call the Build() method. That's why it fails, the memory buffer is no longer valid and the address you gave AngelScript is pointing to trash.

You can fix this by telling AngelScript to make a local copy of the script when you call AddScriptSection(). Just change the last parameter to true.

Regards,
Andreas

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

Advertisement
Oh. I must admit I assumed that AS had an internal copy...Good thing I read all the docs eh? =)

As always Andreas, you've been quite helpful. Thanks so much!

This topic is closed to new replies.

Advertisement