Skip to main content
GameDev.net gamedev.net
🔒 Locked

Problem with shared keyword

Started by Hardguy Jan 12, 2012 at 10:58 AM 5 replies 2.4k views
Original Post
Hardguy
Hardguy
When using the shared keyword for an interface a class uses, I can not longer reload it properly.

Script code is something like
shared interface iMyInterface {
void MyFunc();
}

class cMyClass : iMyInterface {
void MyFunc() {
//Do stuff
}
}


If I reload this script then calling MyFunc() from C++ code no longer works.

The way I reload is to use DiscardModule() in script engine and then create it from scratch (just like I did the first time it was created).

If I remove "shared" from iMyInterface declaration, all works fine.
WitchLord
WitchLord
It is obviously related to the new "shared" functionality. I'll investigate it.

Do you get the new function id or function object after the build? It may or may not be the same, so you should always get a fresh id/object after re-building a script.

Are you calling the MyFunc() object on the interface or the object?
AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - <a href="http://www.angelcode.com/tower" rel
WitchLord
WitchLord
I wasn't able to reproduce this problem. The following is the test I wrote. Please let me know if you're doing something different in your code:


engine = asCreateScriptEngine(ANGELSCRIPT_VERSION);
engine->SetMessageCallback(asMETHOD(CBufferedOutStream,Callback), &bout, asCALL_THISCALL);

const char *code =
"shared interface iMyInterface { \n"
" void MyFunc(); \n"
"} \n"
"class cMyClass : iMyInterface { \n"
" void MyFunc() { \n"
" } \n"
"} \n";

asIScriptModule *mod = engine->GetModule("1", asGM_ALWAYS_CREATE);
mod->AddScriptSection("code", code);
bout.buffer = "";
r = mod->Build();
if( r < 0 )
TEST_FAILED;

int id = mod->GetTypeIdByDecl("cMyClass");
asIObjectType *type = engine->GetObjectTypeById(id);
asIScriptFunction *func = type->GetMethodByDecl("void MyFunc()");
if( func == 0 )
TEST_FAILED;

asIScriptObject *obj = (asIScriptObject*)engine->CreateScriptObject(id);
asIScriptContext *ctx = engine->CreateContext();

ctx->Prepare(func);
ctx->SetObject(obj);
r = ctx->Execute();
if( r != asEXECUTION_FINISHED )
TEST_FAILED;

ctx->Release();
obj->Release();

engine->DiscardModule("1");

// Build the module again
mod = engine->GetModule("1", asGM_ALWAYS_CREATE);
mod->AddScriptSection("code", code);
bout.buffer = "";
r = mod->Build();
if( r < 0 )
TEST_FAILED;

id = mod->GetTypeIdByDecl("cMyClass");
type = engine->GetObjectTypeById(id);
func = type->GetMethodByDecl("void MyFunc()");
if( func == 0 )
TEST_FAILED;

obj = (asIScriptObject*)engine->CreateScriptObject(id);
ctx = engine->CreateContext();

ctx->Prepare(func);
ctx->SetObject(obj);
r = ctx->Execute();
if( r != asEXECUTION_FINISHED )
TEST_FAILED;

ctx->Release();
obj->Release();

engine->Release();
AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - <a href="http://www.angelcode.com/tower" rel
Hardguy
Hardguy
I made a bunch of testing now and found out my code example was wrong!

Here is how to reproduce:

shared class iTest_Interface
{
void HelpFunc(){}
int mlBaseData;
}

//-----------------------------------

class cTest : iTest_Interface
{
cTest()
{
}

void DoSomething()
{
Print(" Inside DoSomething 1\n");
}
}
Hardguy
Hardguy
Note that that code does not make any sense to use, and it was just me setting "shared" where it was not really needed.

Still it might be something worth looking into, as it is still a bug? Or perhaps something that the compiler should report as invalid?
WitchLord
WitchLord
There is nothing wrong with the script. It is perfectly valid to have a non-shared class inherit from a shared class. The other way around is not valid though.

I'll give this new script a try and see if I can reproduce the problem.

[EDIT] Yes, now I was able to reproduce the problem. What happens is that when you discarded the module the object types were orphaned, but not yet destroyed as the garbage collector had not yet executed. When re-compiling the script the previously compiled and shared class was found again, but as it was orphaned, i.e. didn't belong to any module the code didn't behave correctly when trying to find the object methods.

I'll work on a solution for this.
AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - <a href="http://www.angelcode.com/tower" rel
WitchLord
WitchLord
I've fixed this problem in revision 1108.

Thanks,
Andreas
AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - <a href="http://www.angelcode.com/tower" rel

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.