Passing large POD as reference

Started by
6 comments, last by WitchLord 12 years, 2 months ago
Hi

Is there any method to pass POD arguments by reference without copying them around? (with safe references turned on)
This POD values have guaranteed life time, but limited to called script function only. Something similar to opaque pointer described in this topic: http://www.gamedev.n...to-angelscript/

For now I just registered it as reference type with dummy AddRef/Release, but this allows to store handle for use beyond scope.
Btw. why does AS reject "(const Type &in name)" declaration, with error: " Parameter type can't be 'const Type&' ", but accepts "(const Type &inout name)"? (my ver is 2.22.0 r1024)
Advertisement
The tricky part is when you say the script must not store the pointer where it might survive beyond the call.

The only way this can be prevented is to disallow the script to store the pointer at all. The easiest way of doing it is to use the NOHANDLE flag when registering the type, then the script will only be able to access a reference given by the app but not pass it to around.

Another option could be to register the type as ordinary REF, but then add extra validation on the compilation to deny the scripts from declaring global variables or class members of that type.

I could add a flag to have the compiler verify this automatically, but I'm not sure how useful it is. Even without the flag it can quite easily be validated from the application after the compilation by enumerating class members and global variables.

Does that help?

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game


Btw. why does AS reject "(const Type &in name)" declaration, with error: " Parameter type can't be 'const Type&' ", but accepts "(const Type &inout name)"? (my ver is 2.22.0 r1024)


This looks like a bug. Under which circumstance does it happen?

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

I have tried NOHANDLE, but as I understand it works for global variables. My problematic PODs are temporary objects, created on thread memory pools, so I can't register them explicitly. Anyway your suggestion about post-compile validation is quite clever and simple in implementation, so I'll go that way. (It will be little more complicated with plug-in system, because engine does not know every registered type beforehand, but this can be solved, by attaching extra user info for registered types and building forbidden-type list each time module is compiled.)
Originally I was thinking about some run-time checks, that reset unwanted handles, but this would be over complicated (not to mention cpu time consuming).

As for possible bug. It happens with simple ref type being passed as const &in.
LPCTHSTR IPC = "IContactList";
r = pScript->RegisterObjectType(IPC,0,asOBJ_REF); assert(r>=0);
r = pScript->RegisterObjectBehaviour(IPC, asBEHAVE_ADDREF, "void f()", asFUNCTION(dummy), asCALL_CDECL_OBJLAST); assert(r>=0);
r = pScript->RegisterObjectBehaviour(IPC, asBEHAVE_RELEASE, "void f()", asFUNCTION(dummy), asCALL_CDECL_OBJLAST); assert(r>=0);

r = pScript->RegisterObjectProperty(IPC, "vec3 AvgPos", offsetof(CN3EventCollide, AvgPos)); assert(r >= 0);
//and so on...


And in AS file:
int OnCollide_plastic_concrete(CRigidBody@ pRB0, uint ShapeID0, CRigidBody@ pRB1, uint ShapeID1, const IContactList &in List) //error with &in but not &inout
{
return 0;
}
Thanks for the info. I'll investigate 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

This isn't a bug after all. The error message should just have been more explanatory.

The type is not allowed to be &in, because the type cannot be instanciated, which also means it cannot be copied.

Even though the function is declared to receive a reference, the &in means that the reference will only be used as input. The way AngelScript guarantees that the original value isn't accidentally modified by the function that said it would only use the value as input, is by making a copy and passing the reference to the copy instead.

I'll improve the error message 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

Thanks for info, I didn't know that rule also apply to reference types. Anyway, I discovered I can use standard "const Type& name" (skip the inout part) for this type, so it's ok. (inout with const looked a bit confusing)
This is one of the reasons why I have future plans to change the syntax a bit, to make it less confusing.

type &in => type
type &out => out type or type out
type &inout => type &

There won't be any difference in passing by value or by &in. Behind the scenes the objects will always be passed by reference, and the primitives by value.

To the C++ application there won't be any difference, but in the script language it will be less confusion.

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

This topic is closed to new replies.

Advertisement