Advertisement
tinyevil

Untitled

Oct 20th, 2018
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.79 KB | None | 0 0
  1. struct AnyVtable{
  2.     String* (*toString)(Any* thisobj);
  3.     ...
  4.     Any (*call)(Any* thisobj, RestParams params);
  5. };
  6.  
  7. struct Any{
  8.     AnyVtable* vtable;
  9.     union{
  10.         Object* object;
  11.         ...
  12.     };
  13. };
  14.  
  15.  
  16. Any fnc_wrapper(Any* closure, RestParams params){
  17.     if ( params.length < 2 ){
  18.         throw new TypeError("not enough arguments, expected no less than 2");
  19.     }
  20.     if ( params.length > 2 ){
  21.         throw new TypeError("too many arguments, expected no more than 2");
  22.     }
  23.     int a0 = Any::unwrapInt(params[0]);
  24.     double a1 = Any::unwrapNumber(params[1]);
  25.     Foo* this_object = (Foo*)closure->object;
  26.     return Any::wrap(this_object->fnc(a0, a1));
  27. }
  28.  
  29.  
  30. AnyVtable Foo_fnc_vtable = {
  31.     /*toString = */ [](Any*) -> String*{
  32.         return new String("Foo::fnc");
  33.     },
  34.     ...
  35.     /*call = */ fnc_wrapper
  36. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement