# [PATCH] function call operator support

This topic is 2031 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi,

please find attached a patch to support function call operators in angelscript (the patch applies to the 2.28.0 official release). The initial topic discussing this project can be found here: http://www.gamedev.net/topic/652292-function-call-operators/

Due to my limited experience with Angelscript, you may want to do things differently, but I have tried to reuse existing code as much as possible to avoid duplication of the existing logic. Also, the impact should be pretty small on other features.

Here is an example of definition and usage:

// declaration example
class FuncCallClass
{
void opCall(){}
void opCall(int i){return 0}
};

// usage example
void main(void)
{
FuncCallClass func;
func();
int j=func(10);
}


So far, the following cases have been tested, on both Windows and Mac, with or without JIT:

- direct call for global or local variables

- calling this() inside a class method

- calling the operator on a class handle

- calling the operator on a class member variable

- calling the operator on a class member thru a property accessor

- calling the operator on an object in an array (array<Functor> a={f};  a[0]();)

- calling the operator with or without arguments and return values.

I hope you will find it useful. I do not have test cases for the angelscript tests suite right now, as I am having issues building the tests on Mac (I managed to do it but it requires a couple of changes, so the diff would be a mess!).

##### Share on other sites

Thanks for the patch. I'll review it as soon as I can, and unless I find something I dislike I'll include it in the next release after 2.28.1 (which is almost complete).

Regards,

Andreas

##### Share on other sites

Thanks! I hope you won't find it too ugly.

##### Share on other sites

I've added the support for opCall in revision 1852.

Let me know if you encounter any problems with my implementation.

##### Share on other sites

Great, thanks! Will check it out asap (I have been away from the forum for a while).

##### Share on other sites

Hi,

I have tested v2.28.2 that includes the new opCall feature. While it seems to be working fine for local variables, it fails for globals. For example, the following code will issue an error ("Expression doesn't form a function call. 'globalF' is a variable of a non-function type"):

class Functor
{
bool opCall(double d)
{
return d!=0;
}
};

Functor globalF;
void global()
{
bool ok=globalF(0);
}


I remember that when I did the implementation, there was something special to do for global variables to be recognized as they are handled differently.

##### Share on other sites

Also, angelscript crashes when using a functor as a property on a global variable. It is easy to reproduce with the following code (tested on Windows / MSVC12 / x64 / Debug):


int i=0;
class Functor
{
void opCall()
{
print("opCall"+i+"\n");
i++;
}
void callThis()
{
this();
}
};

class Hybrid
{
Functor f;
Functor g
{
get const
{
return f;
}
}
void callMember()
{
f();
}
void callMemberProp()
{
g();
}
}

Hybrid glob;

void member()
{
Hybrid local;
local.f();
local.callMember();
local.g();
local.callMemberProp();

glob.f();
glob.get_g()();
// The following line crashes angelscript during execution:
glob.g();
glob.callMember();
glob.callMemberProp();

Functor@ fPtr=glob.g;
fPtr();
}


I remember having to do something special for this case too and got a crash too before I did...

##### Share on other sites

Thanks. I'll look into these problems.

##### Share on other sites

Thanks. If it may help, maybe you want to look at my patch to see the particular cases that I had to handle for globals (even if your new implementation is quite different).

##### Share on other sites

I've fixed the problems in revision 1887.

Regards,

Andreas

• ### Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 9
• 11
• 15
• 21
• 26