Sign in to follow this  
dkrusu

Namespace Bug?

Recommended Posts

I've encountered a bug with namespaces, if you create a object type inside of a namespace besides the global, then create a global property with the same name as the object type but in the global namespace,  the opIndex method fails. The following code should show whats going on more than my explanation

#include "angelscript.h"
#include <stdio.h>

void MessageCallback(const asSMessageInfo *msg, void *param)
{
	const char *type = "ERR ";
	if( msg->type == asMSGTYPE_WARNING ) 
		type = "WARN";
	else if( msg->type == asMSGTYPE_INFORMATION ) 
		type = "INFO";

	printf("%s (%d, %d) : %s : %s\n", msg->section, msg->row, msg->col, type, msg->message);
}

class FooObj
{
    public:
        FooObj() { };

        int operator[](int num)
        {
            return 1;
        }

        void test() { }
};

int main()
{
    asIScriptEngine *engine = asCreateScriptEngine(ANGELSCRIPT_VERSION);
	engine->SetMessageCallback(asFUNCTION(MessageCallback), 0, asCALL_CDECL);

    engine->SetDefaultNamespace("NSBugTest");
    engine->RegisterObjectType("FooObj", 0, asOBJ_REF | asOBJ_NOCOUNT);
    engine->RegisterObjectMethod("FooObj", "int opIndex(int)", asMETHOD(FooObj, FooObj::operator[]), asCALL_THISCALL);
    engine->RegisterObjectMethod("FooObj", "void test()", asMETHOD(FooObj, FooObj::test), asCALL_THISCALL);
    engine->SetDefaultNamespace("");

    FooObj foo;
    engine->RegisterGlobalProperty("NSBugTest::FooObj FooObj", &foo);

    asIScriptModule *mod = engine->GetModule("test", asGM_ALWAYS_CREATE);
	mod->AddScriptSection("test",
			"void main() \n"
			"{ \n"
            "   FooObj.test();\n"
            "   int num = FooObj[0];\n"
			"} \n");
	mod->Build();

    return 0;

}

The call to test() will work like it should, however the opIndex call generates the following error:

test (1, 1) : INFO : Compiling void main()
test (4, 21) : ERR  : Expected ']'
test (4, 21) : ERR  : Instead found '<integer constant>'

Thanks

Edited by dkrusu

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this