[PATCH] function call operator support

Started by
10 comments, last by gjl 10 years ago

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!).

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

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! I hope you won't find it too ugly. smile.png

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.

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

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

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.

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

Thanks. I'll look into these problems.

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

I've fixed the problems in revision 1887.

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