Jump to content
  • Advertisement
Sign in to follow this  
Hardguy

Problem with shared keyword

This topic is 2530 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

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.

Share this post


Link to post
Share on other sites
Advertisement
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?

Share this post


Link to post
Share on other sites
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();

Share this post


Link to post
Share on other sites
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");
}
}

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!