Jump to content

  • Log In with Google      Sign In   
  • Create Account


- - - - -

Problem with shared keyword


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 Hardguy   Members   -  Reputation: 244

Like
0Likes
Like

Posted 12 January 2012 - 04:58 AM

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.


Sponsor:

#2 Andreas Jonsson   Moderators   -  Reputation: 3238

Like
0Likes
Like

Posted 12 January 2012 - 03:26 PM

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 - Tower - free puzzle game

#3 Andreas Jonsson   Moderators   -  Reputation: 3238

Like
0Likes
Like

Posted 12 January 2012 - 04:04 PM

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 - Tower - free puzzle game

#4 Hardguy   Members   -  Reputation: 244

Like
0Likes
Like

Posted 13 January 2012 - 03:48 AM

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");
}
}



#5 Hardguy   Members   -  Reputation: 244

Like
0Likes
Like

Posted 13 January 2012 - 03:52 AM

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?


#6 Andreas Jonsson   Moderators   -  Reputation: 3238

Like
0Likes
Like

Posted 13 January 2012 - 03:15 PM

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 - Tower - free puzzle game

#7 Andreas Jonsson   Moderators   -  Reputation: 3238

Like
0Likes
Like

Posted 13 January 2012 - 05:04 PM

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