• Advertisement
Sign in to follow this  

Global Variable Redeclaration

This topic is 1346 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Advertisement

Sounds like a bug. It shouldn't be allowed to exist multiple properties/variables of the same name in the same namespace. The variable name must also not conflict with a type name or the name of any function.

 

With global properties you mean application registered global properties? Or global variables declared from the script?

 

Can you give more detail on the exact scenario (to reduce my time in reproducing the problem)?

Share this post


Link to post
Share on other sites

I'm sorry, I mean global variables declared from the script.  I find that in any script two global variable declarations compile with no errors.

double x;
double x;
void test() {

}

It doesn't look like there is any test for duplicate names in asCCompiler::CompileGlobalVariable.

 

as_compiler.cpp(1196):

asSExprContext compiledCtx(engine);
bool preCompiled = false;
if( gvar->datatype.IsAuto() )
	preCompiled = CompileAutoType(gvar->datatype, compiledCtx, node, gvar->declaredAtNode);
if( gvar->property == 0 )
{
	gvar->property = builder->module->AllocateGlobalProperty(gvar->name.AddressOf(), gvar->datatype, gvar->ns);
	gvar->index = gvar->property->id;
}

as_module.cpp(1175):

asCGlobalProperty *asCModule::AllocateGlobalProperty(const char *name, const asCDataType &dt, asSNameSpace *ns)
{
	asCGlobalProperty *prop = engine->AllocateGlobalProperty();
	prop->name = name;
	prop->nameSpace = ns;

	// Allocate the memory for this property based on its type
	prop->type = dt;
	prop->AllocateMemory();

	// Make an entry in the address to variable map
	engine->varAddressMap.Insert(prop->GetAddressOfValue(), prop);

	// Store the variable in the module scope (the reference count is already set to 1)
	scriptGlobals.Put(prop);

	return prop;
}
Edited by Jason Goepel

Share this post


Link to post
Share on other sites

Thanks for the details.

 

The builder should definitely guarantee that no two global variables in the same namespace can have the same name. It should be done by CheckNameConflict, called by asCBuilder::RegisterGlobalVar, but I guess something has been broken recently.

 

I'll investigate further and have this fixed a.s.a.p.

Share this post


Link to post
Share on other sites

I've fixed this in revision 1921.

 

The check for name conflict had been broken by the implementation of 'auto' types in the WIP version. For some odd reason my regression test suite didn't have a test case for this basic scenario so it wasn't detected before. But now it has so it shouldn't be broken by mistake again in the future. :)

 

Regards,

Andreas

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement