Passing an Opaque Pointer to AngelScript

Started by
11 comments, last by ZsjaWkaos 12 years, 6 months ago
For example it should be allowed to store the OP in a local variable, but not a global variable, it must not be possible to store the OP in a class member, unless the class itself is guaranteed to die after the function returns. I'm not sure how you expect AngelScript to control this.


A class with an OP shouldn't be stored globally. To make it easier I wouldn't mind simply disallowing all global variables (it's good design anyway) I think I can easily patch this in myself with the following code (in as_compiler.cpp, after line 6269):

if( found ){
Error( "ERROR: NO GLOBAL VARIABLES ALLOWED!", errNode );
return -1;
}


All state data between calls will then be stored outside of AS.

This means that I'll be running a modified version of AS. I think making a better solution will require quite some work (disallowing classes with OPs to be stored in global variables) on your end and I don't want to deliver you any more work than necessary.

Unfortunately, I'm still not sure how disallow the following AS code (any pointers willbe greatly appreciated):


void main() {
SomeData some_data;
some_function( some_data );
}


While still allowing:


void main() {
SomeData some_data = some_other_function();
}


The "IsVariableInitialized" returned always "true" for the code I tested it with. I placed the following code right at the beginning/end of "asCCompiler::CompileVariableAccess" hoping that it would return false for the first case of "SomeData".

cout << " IsVariableInitialized" << name.AddressOf() << ", " << IsVariableInitialized( &ctx->type, errNode ) << endl;


Alternatively you can implement a proxy class [..]


This is an interesting alternative, I'll consider it. I'm not a big fan of runtime errors when they can be found at compile time. There is also a (small?) amount of additional overhead/complexity.
Advertisement
You don't have to modify the library to disable use of global variables. Just add a simple check after the compilation, e.g.


mod->Build();

// Check if global variables were declared
if( mod->GetGlobalVarCount() )
{
// Give error
return -1;
}


I may however consider adding an engine property to disable global variables, that would allow you to do this with a simple engine->SetEngineProperty(asEP_DISABLE_GLOBAL_VARS);




Why do you have to disallow the declaration of a local variable of the OP type? The script cannot assign an invalid value to it anyway. It would just initialize to null, which your code is hopefully treating anyway.


However, if you really wish to prevent it, the best place to do it is the asCCompiler::CompileDeclaration(). Give an error in this function unless the declaration is followed by an assignment.


The IsVariableInitialized only works for primitives, as it is assumed that registered types have a proper default value.



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


You don't have to modify the library to disable use of global variables. Just add a simple check after the compilation, e.g.


Neat! I'll check this out tomorrow, I didn't have time to code anything today.


Why do you have to disallow the declaration of a local variable of the OP type? The script cannot assign an invalid value to it anyway. It would just initialize to null, which your code is hopefully treating anyway.


That would be great. Even if it's not checked at compile time it would be enough for me to use it. If I can get both techniques working like you described I'll be using AngelCode in my next project. :D

Thanks for your patience and help.

This topic is closed to new replies.

Advertisement