Advertisement

Enum collision across namespaces

Started by May 22, 2012 02:48 AM
5 comments, last by WitchLord 12 years, 6 months ago
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++.
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.
Advertisement
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.
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
}

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

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

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...
Advertisement
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

This topic is closed to new replies.

Advertisement