Sign in to follow this  

AngelScript function pointers now working

This topic is 2846 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 guys, I'd just wanted to let you know that I've finally been able to get to the point where function pointers are now fully working. I welcome those that have been waiting for this feature to give it a try so I can root out as many bugs as possible before I release version 2.18.2. Here's a script example that shows the syntax for the function pointers:
  // Define a function signature for the function pointer
  funcdef bool CALLBACK(int, int);

  // An example function that to use this
  void main()
  {
    // Declare a function pointer, and set it 
    // to point to the myCompare function.
    CALLBACK @func = @myCompare;

    // The function pointer can be compared with the 'is' operator
    if( func is null )
    {
      print("The function pointer is null\n");
      return;
    }

    // Call the function through the pointer, just as if it was a normal function
    if( func(1, 2) )
    {
      print("The function returned true\n");
    }
    else
    {
      print("The function returned false\n");
    }
  }

  // This function matches the CALLBACK definition, since it has 
  // the same return type and parameter types.
  bool myCompare(int a, int b)
  {
    return a > b;
  }
One disclaimer though, while the function pointers are now fully working, they are still a bit limited in their usefulness as there is still no way for the application to define the function definitions, this means that the function pointers can really only be used within scripts, but not be passed to or from the application. This is a limitation that I'll address with version 2.19, when I'll update the application interface. There are also some other limitations that I plan on improving over the coming releases. For example, the addition of a generic function pointer type that can hold any function pointer, regardless of signature, and the addition of reference casts for this generic type to the real function pointer type. This will make it much more versatile. Regards, Andreas

Share this post


Link to post
Share on other sites
I tried running the example in your post, on svn revision 561, and I get the error
script (5, 1) : INFO : Compiling void main()
script (9, 11) : ERR : Expected ';'
and line 9 is
CALLBACK @func = @myCompare;

Edit:
I ran the features test and everything passed...

Edit 2:
If the line
CALLBACK @func = @myCompare;
gets moved out side of main, it works.

Share this post


Link to post
Share on other sites
Quote:
Original post by Thy Reaper
Can this version of function pointers be used in import declarations? And similarly, will imports be treated as function pointers eventually?


Not yet, but the idea is that the imports will eventually be replaced with function pointers. Probably the CScriptBuilder will inherit the automatic binding of the imports as this is taken out of the core engine.

Share this post


Link to post
Share on other sites
Quote:
Original post by klusark
I tried running the example in your post, on svn revision 561, and I get the error


Just fixed this in revision 562. Thanks.

This is exactly why I want you guys to test this.

Regards,
Andreas

Share this post


Link to post
Share on other sites
I got a crash! Here is the script. What I am doing is probably not intended :P


funcdef bool CALLBACK(int, int);
funcdef bool CALLBACK3(CALLBACK @, CALLBACK @);
funcdef bool CALLBACK2(CALLBACK @, CALLBACK3 @);

void main()
{
CALLBACK @func = @myCompare;
CALLBACK2 @func2 = @test;
CALLBACK3 @func3 = @test2;
func2(func, func3);
}
bool test(CALLBACK @func, CALLBACK3 @func2)
{
if( func(1, 2) )
{
print("The function returned true\n");
}
else
{
print("The function returned false\n");
}
return true;
}
bool test2(CALLBACK @func, CALLBACK @func2)
{
if( func(1, 2) )
{
print("The function returned true\n");
}
else
{
print("The function returned false\n");
}
return true;
}
bool myCompare(int a, int b)
{
return a > b;
}

Share this post


Link to post
Share on other sites
Got another crash! I also tried using recursive functions, but that did not crash.

funcdef bool CALLBACK(int, int);
class testclass
{
CALLBACK @func;
testclass()
{
@func = @myCompare;
func(2,3);
}
}
void main()
{
testclass test;
}
bool myCompare(int a, int b)
{
testclass @testc = @testclass();
return a < b;
}





Edit: I dont think that is a function pointer bug. Here is another example that crashes

class testclass
{
testclass()
{
myCompare(1,3);
}
}
void main()
{
testclass test;
}
bool myCompare(int a, int b)
{
testclass @testc = @testclass();
return a < b;
}



[Edited by - klusark on February 25, 2010 12:42:31 PM]

Share this post


Link to post
Share on other sites
This bug should have been fixed in revision 565.

As you suspected, it was not related to function pointers. It was a bug in the virtual machine. When dynamically growing the stack space, the object pointer for the class method call wasn't copied over to the new stack block, which caused the access violation.

Share this post


Link to post
Share on other sites

This topic is 2846 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this