Advertisement
tinyevil

Untitled

Oct 20th, 2018
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.02 KB | None | 0 0
  1. class Foo{
  2.     public function fnc(x:int, y:Number):String{
  3.         return x.toString() + y.toString();
  4.     }
  5. }
  6.  
  7. String* fnc(int x, double y){
  8.     return String::concat(Int::toString(x), Number::toString(y));
  9. }
  10.  
  11. Any fnc_wrapper(Any* closure, RestParams params){
  12.     if ( params.length < 2 ){
  13.         throw new TypeError("not enough arguments, expected no less than 2");
  14.     }
  15.     if ( params.length > 2 ){
  16.         throw new TypeError("too many arguments, expected no more than 2");
  17.     }
  18.     int a0 = Any::unwrapInt(params[0]);
  19.     double a1 = Any::unwrapNumber(params[1]);
  20.     Foo* this_object = (Foo*)closure->object;
  21.     return Any::wrap(this_object->fnc(a0, a1));
  22. }
  23.  
  24.  
  25. // you can do this
  26. var foo:Foo = new Foo();
  27. var method_closure:Function = foo['fnc'];
  28. var y:int = method_closure(4, "22.0");
  29. trace(y); // prints "422"
  30.  
  31. Foo* foo = Foo::new();
  32. Function method_closure = Function::NewMethodClosure(foo, fnc_wrapper);
  33. int y = Any::unwrapInt(
  34.     method_closure.invoke(
  35.         {
  36.             Any::wrap(4),
  37.             Any::wrap(new String("22.0"))
  38.         }
  39.     )
  40. );
  41. trace({Any::wrap(y)});
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement