Advertisement
snake5

SGScript [v2] native objects + meta-objects for methods

Aug 1st, 2014
422
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.50 KB | None | 0 0
  1. int nom_method_A( SGS_CTX )
  2. {
  3.     SGSFN( "nom::method_A" );
  4.     sgs_PushInt( C, 5 );
  5.     if( sgs_Method( C ) )
  6.     {
  7.         sgs_PushItem( C, -1 );
  8.         sgs_StoreProperty( C, 0, "a" );
  9.     }
  10.     return 1;
  11. }
  12.  
  13. int nom_method_B( SGS_CTX )
  14. {
  15.     SGSFN( "nom::method_B" );
  16.     sgs_PushInt( C, 7 );
  17.     if( sgs_Method( C ) )
  18.     {
  19.         sgs_PushItem( C, -1 );
  20.         sgs_StoreProperty( C, 0, "b" );
  21.     }
  22.     return 1;
  23. }
  24.  
  25. int nom_iface( SGS_CTX );
  26. int nom_ctor( SGS_CTX )
  27. {
  28.     SGSFN( "nom::nom" );
  29.     sgs_PushInterface( C, nom_iface );
  30.     sgs_PushDict( C, 0 );
  31.     sgs_ObjSetMetaObj( C, sgs_GetObjectStruct( C, -1 ), sgs_GetObjectStruct( C, -2 ) );
  32.     return 1;
  33. }
  34.  
  35. int nom_iface( SGS_CTX )
  36. {
  37.     sgs_PushString( C, "method_A" );
  38.     sgs_PushCFunction( C, nom_method_A );
  39.     sgs_PushString( C, "method_B" );
  40.     sgs_PushCFunction( C, nom_method_B );
  41.     sgs_PushString( C, "__call" );
  42.     sgs_PushCFunction( C, nom_ctor );
  43.     atf_assert( sgs_PushDict( C, 6 ) == SGS_SUCCESS );
  44.     atf_assert( sgs_StackSize( C ) >= 1 );
  45.     sgs_ObjSetMetaMethodEnable( sgs_GetObjectStruct( C, -1 ), 1 );
  46.     return 1;
  47. }
  48.  
  49. DEFINE_TEST( native_obj_meta )
  50. {
  51.     SGS_CTX = get_context();
  52.    
  53.     /* register as ctor/iface */
  54.     sgs_PushInterface( C, nom_iface );
  55.     sgs_StoreGlobal( C, "nom" );
  56.    
  57.     /* try creating and running the object */
  58.     atf_assert( sgs_ExecString( C, ""
  59.         "a = nom();\n"
  60.         "printvar(a.method_A());\n"
  61.         "printvar(a.method_B());\n"
  62.         "function nom.method_C(){ this.c = 9; return 9; }\n"
  63.         "printvar(a.method_C());\n"
  64.         "printvar(a);\n"
  65.     "" ) == SGS_SUCCESS );
  66.    
  67.     destroy_context( C );
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement