Jump to content

  • Log In with Google      Sign In   
  • Create Account


- - - - -

Enum collision across namespaces


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
6 replies to this topic

#1 Ricky C.   Members   -  Reputation: 132

Like
2Likes
Like

Posted 21 May 2012 - 08:48 PM

I've never been a fan of C++'s preference to slam enum values into the global namespace, thus I use namespaces in my C++ code to limit that progression. So it was with delight that I upgraded my AS as soon as I found out that namespaces had come. However I just found a problem. Here's some C++-based pseudocode giving an example of what I do:
// Create a namespaced enumeration
namespace MY_ENUM {
  enum TYPE {
	VALUE_A,
	VALUE_B,
  }
}
// And another
namespace ANOTHER_ENUM {
  enum TYPE {
	VALUE_A,
	VALUE_B,
  }
}

// Example usage of the types
ANOTHER_ENUM::TYPE MyFunction(MY_ENUM::TYPE);

// Example usage of the values
if (MyFunction(MY_ENUM::VALUE_B) == ANOTHER_ENUM::VALUE_A) {
  ...
}

Now, to do the same thing in AS when I'm registering my interface:
asIScriptEngine* as_engine = ....; // Go get the current pointer.
int ret = 0;
ret = as_engine->SetDefaultNamespace("MY_ENUM"); assert(ret >= 0);
ret = as_engine->RegisterEnum("TYPE"); assert(ret >= 0);
ret = as_engine->RegisterEnumValue("TYPE", "VALUE_A", 0); assert(ret >= 0);
ret = as_engine->RegisterEnumValue("TYPE", "VALUE_B", 0); assert(ret >= 0);

ret = as_engine->SetDefaultNamespace("ANOTHER_ENUM"); assert(ret >= 0);
ret = as_engine->RegisterEnum("TYPE"); assert(ret >= 0);
ret = as_engine->RegisterEnumValue("TYPE", "VALUE_A", 0); assert(ret >= 0);
ret = as_engine->RegisterEnumValue("TYPE", "VALUE_B", 0); assert(ret >= 0);

// Clean up the namespace
ret = as_engine->SetDefaultNamespace(""); assert(ret >= 0);

What was my surprise when the second RegisterEnum("TYPE") caused the assert to fail and the following message to be placed in my AS log:
Failed in call to function 'RegisterEnum' with 'TYPE'

Since both example enums are supposed to be in separate namespaces, they shouldn't have any collisions... Global properties and functions have no such issue:
float e = 2.71828183f;
float p = 3.14159265f;

ret = as_engine->SetDefaultNamespace("WONDER"); assert(ret >= 0);
ret = as_engine->RegisterGlobalProperty("float Flubber", &e); assert(ret >= 0);
ret = as_engine->RegisterGlobalFunction("void function()", asFUNCTION(funcA), asCALL_CDECL); assert(ret >= 0);

ret = as_engine->SetDefaultNamespace("AMUSEMENT"); assert(ret >= 0);
ret = as_engine->RegisterGlobalProperty("float Flubber", &p); assert(ret >= 0);
ret = as_engine->RegisterGlobalFunction("void function()", asFUNCTION(funcB), asCALL_CDECL); assert(ret >= 0);

--

After hacking my registration to get it operable I found myself playing hide-and-seek with my enum values. For some reason I found my enum value hiding at MY_ENUM::TYPE::VALUE_A instead of where I expected, based on how C++ operates, at MY_ENUM::VALUE_A. This is a good thing, and quite a nice change, but it's almost entirely unlike C++.

Edited by Andreas Jonsson, 23 May 2012 - 06:23 PM.


Sponsor:

#2 adam4813   Members   -  Reputation: 655

Like
2Likes
Like

Posted 21 May 2012 - 10:06 PM

The offending block of code from as_builder.cpp line 3943

asCObjectType *asCBuilder::GetObjectType(const char *type, const asCString &ns)
{
// TODO: namespace: Registered types should also allow namespaces
asCObjectType *ot = engine->GetObjectType(type);
if( !ot && module )
  ot = module->GetObjectType(type, ns);
return ot;
}

It checks for the type by name only in engine. This check causes it to return the type registered under the first namespace rather than checking with the namespace as well.

Edited by adam4813, 21 May 2012 - 10:07 PM.


#3 adam4813   Members   -  Reputation: 655

Like
2Likes
Like

Posted 21 May 2012 - 10:18 PM

Additionally found that object types suffer this issue. Both lines 1426 snf 1433 in as_scriptengine.cpp are lacking checks for namespace
if( objectTypes[n] && objectTypes[n]->name == typeName ) // 1426
if( templateTypes[n] && templateTypes[n]->name == typeName ) // 1433

After patching them to read:
if( objectTypes[n] && objectTypes[n]->name == typeName && objectTypes[n]->nameSpace == defaultNamespace ) // 1426
if( templateTypes[n] && templateTypes[n]->name == typeName && templateTypes[n]->nameSpace == defaultNamespace ) // 1433
The parse continues on and errors out at my previous reply.

Edited by adam4813, 21 May 2012 - 10:26 PM.


#4 Andreas Jonsson   Moderators   -  Reputation: 3268

Like
1Likes
Like

Posted 22 May 2012 - 07:56 AM

Thanks to both of you. I'll investigate the problem reported and have it fixed as soon as possible.

In the meantime you may want to set the engine property asEP_REQUIRE_ENUM_SCOPE to true. This changes the compiler to only accept the enum values if they actual enum type is informed with the scope operator.

enum MYENUM
{
   VALUE
}

void func()
{
  MYENUM v;

  v = VALUE;		  // With asEP_REQUIRE_ENUM_SCOPE this will give an error
  v = MYENUM::VALUE;	 // OK
}

Edited by Andreas Jonsson, 22 May 2012 - 07:57 AM.

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

#5 Andreas Jonsson   Moderators   -  Reputation: 3268

Like
0Likes
Like

Posted 23 May 2012 - 06:22 PM

I've checked in the fixes for these problems in revision 1315.

There were a few more places that needed adjustments, but now it should be working properly. Let me know if you find any other problems.

Thanks,
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

#6 Ricky C.   Members   -  Reputation: 132

Like
0Likes
Like

Posted 25 May 2012 - 07:23 AM

Thanks Andreas!

I've not tested, but it might be good to verify that typedefs get namespaced as well. Currently they are of limited use, but hopefully...

#7 Andreas Jonsson   Moderators   -  Reputation: 3268

Like
1Likes
Like

Posted 25 May 2012 - 10:56 AM

That was fixed along with the enums and other object types. :)
AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS