Jump to content
  • Advertisement
Sign in to follow this  
gjl

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

If you intended to correct an error in the post then please contact us.

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


Link to post
Share on other sites
Advertisement

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


Link to post
Share on other sites

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

 

I ended up doing it differently from what you had implemented in the patch. Not because your code was bad, but I had already made other changes before adding your patch so it became easier to just implement it from scratch.

 

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

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 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!