Note: This website is archived. For up-to-date information about D projects and development, please visit wiki.dlang.org.

Changes between Version 2 and Version 3 of LuaLibExamples

Show
Ignore:
Author:
Trass3r (IP: 141.24.41.65)
Timestamp:
01/19/09 01:49:04 (15 years ago)
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • LuaLibExamples

    v2 v3  
    22 
    33{{{ 
     4module test; 
     5 
    46import lua.all; 
    57 
    6 import tango.io.Stdout; 
    7 import Int = tango.text.convert.Integer; 
     8version (Tango) 
     9
     10        import tango.io.Stdout; 
     11        import tango.text.convert.Integer : parseInt = toString; 
     12
     13else 
     14
     15        import std.stdio; 
     16        import std.string : parseInt = toString; 
     17
    818 
    919class Integer 
    1020{ 
    11     private int data_; 
     21       private int data_; 
    1222 
    13     public this (LuaState L) 
    14     { 
     23        public this (LuaState L) 
     24        { 
     25                data_ = L.checkInteger(1); 
     26        } 
    1527 
    16     } 
     28        public this () 
     29        { 
     30                this.data_ = 1; 
     31        } 
    1732 
    18     public this () 
    19    
    20         this.data = 1
    21    
     33       public int data () 
     34       
     35               return this.data_
     36       
    2237 
    23     public int data () 
    24     { 
    25         return this.data_; 
    26     } 
     38        public int lua_get_data (LuaState L) 
     39        { 
     40                L.pushNumber (this.data_); 
     41                return 1; 
     42        } 
    2743 
    28     public int lua_get_data (LuaState L) 
    29     { 
    30                 //      Stdout.formatln ("lua_get_data called with: {}", new LuaStack (L));                                                                                                                         
     44        public int data (int value) 
     45        { 
     46                return this.data_ = value; 
     47        } 
    3148 
    32                 L.pushNumber (this.data); 
    33         return 1; 
    34     } 
     49        public int lua_set_data (LuaState L) 
     50        { 
     51                this.data_ = L.checkInteger (1); 
     52                return 0; 
     53        } 
    3554 
    36     public int data (int value) 
    37     { 
    38         return this.data_ = value; 
    39     } 
     55        public int lua_tostring (LuaState L) 
     56        { 
     57                                L.pushString (parseInt(data_)); 
     58                                return 1; 
     59        } 
    4060 
    41     public int lua_set_data (LuaState L) 
    42     { 
    43         this.data_ = cast (int) L.checkNumber (1); 
    44         return 0; 
    45     } 
     61        public static int lua_static (LuaState L) 
     62        { 
     63                                L.pushString ("Hello world!"); 
    4664 
    47     public int lua_tostring (LuaState L) 
    48     { 
    49                 L.pushString (Int.toString (data_)); 
    50                 return 1; 
    51     } 
    52  
    53     public static int lua_static (LuaState L) 
    54     { 
    55                 L.pushString ("Hello world!"); 
    56  
    57                 return 1; 
    58     } 
     65                                return 1; 
     66        } 
    5967} 
    6068 
    6169class Base 
    6270{ 
    63     private Integer data_; 
     71       private Integer data_; 
    6472 
    65     this () 
    66    
    67         this.data_ = new Integer; 
    68    
     73       this () 
     74       
     75               this.data_ = new Integer; 
     76       
    6977 
    70     public Integer data () 
    71    
    72         return this.data_; 
    73    
     78       public Integer data () 
     79       
     80               return this.data_; 
     81       
    7482 
    75     public int lua_get_data (LuaState L) 
    76    
    77                 L.wrapClass (data_); 
    78                 return 1; 
    79    
     83       public int lua_get_data (LuaState L) 
     84       
     85                               L.wrapClass (data_); 
     86                               return 1; 
     87       
    8088 
    81     public int lua_set_data (LuaState L) 
    82    
    83                 data_ = L.unwrapClass !(Integer) (1); 
     89       public int lua_set_data (LuaState L) 
     90       
     91                               data_ = L.unwrapClass !(Integer) (1); 
    8492 
    85                 return 0; 
    86    
     93                               return 0; 
     94       
    8795} 
    8896 
    8997class Derived : Base 
    9098{ 
    91     public int lua_get_one (LuaState L) 
    92    
    93                 L.pushNumber (1); 
    94                 return 1; 
    95    
     99       public int lua_get_one (LuaState L) 
     100       
     101                               L.pushNumber (1); 
     102                               return 1; 
     103       
    96104} 
    97105 
    98106int simpleFunction (LuaState L) 
    99107{ 
    100     Stdout.format ("simpleFunction got {} arguments!", L.top).newline
     108       writefln("lua_set_data called with: %s", new LuaStackTrace (L))
    101109 
    102     return 0; 
     110       return 0; 
    103111} 
    104112 
    105 int main (char[][] args) 
     113version(D_Version2) mixin("alias const(char) cchar;"); else alias char cchar; 
     114int main (string[] args) 
    106115{ 
    107     char[] stdout; 
    108     void writer (char[] data) 
    109    
    110         stdout ~= data; 
    111    
     116       char[] stdout; 
     117       void writer (cchar[] data) 
     118       
     119               stdout ~= data; 
     120       
    112121 
    113     auto L = new LuaState (&writer); 
     122       auto L = new LuaState (&writer); 
    114123 
    115     // register the function                                                                                                                                                                                        
    116     mixin (mixinLuaRegisterFunction ("L", "ClassTest.simpleFunction", "mylib.func")); 
    117     mixin (mixinLuaRegisterFunction ("L", "ClassTest.Integer.lua_static", "mylib.static")); 
     124        // register the function 
     125        mixin (mixinLuaRegisterFunction ("L", "simpleFunction", "mylib.func")); // full typeid string isn't required for functions 
     126       mixin (mixinLuaRegisterFunction ("L", "test.Integer.lua_static", "mylib.static")); 
    118127 
    119     // register the constructor                                                                                                                                                                                     
    120     mixin (mixinLuaRegisterConstructor ("L", "ClassTest.Integer", "mylib.newint")); 
     128        // register the constructor 
     129       mixin (mixinLuaRegisterConstructor ("L", "test.Integer", "mylib.newint")); 
    121130 
    122     // register the methods in this state                                                                                                                                                                           
    123     mixin (mixinLuaRegisterMethod ("L", "ClassTest.Integer.lua_get_data", "get")); 
    124     mixin (mixinLuaRegisterMethod ("L", "ClassTest.Integer.lua_set_data", "set")); 
    125     mixin (mixinLuaRegisterMethod ("L", "ClassTest.Integer.lua_tostring", "__tostring")); 
     131        // register the methods in this state 
     132        mixin (mixinLuaRegisterMethod ("L", "test.Integer.lua_get_data", "get")); // full typeid string is required for class stuff 
     133       mixin (mixinLuaRegisterMethod ("L", "test.Integer.lua_set_data", "set")); 
     134       mixin (mixinLuaRegisterMethod ("L", "test.Integer.lua_tostring", "__tostring")); 
    126135 
    127     mixin (mixinLuaRegisterMethod ("L", "ClassTest.Base.lua_get_data", "get")); 
     136       mixin (mixinLuaRegisterMethod ("L", "test.Base.lua_get_data", "get")); 
    128137 
    129     mixin (mixinLuaRegisterMethod ("L", "ClassTest.Base.lua_set_data", "set")); 
     138       mixin (mixinLuaRegisterMethod ("L", "test.Base.lua_set_data", "set")); 
    130139 
    131     mixin (mixinLuaRegisterMethod ("L", "ClassTest.Derived.lua_get_data", "get")); 
    132     mixin (mixinLuaRegisterMethod ("L", "ClassTest.Derived.lua_set_data", "set")); 
    133     mixin (mixinLuaRegisterMethod ("L", "ClassTest.Derived.lua_get_one", "one")); 
     140       mixin (mixinLuaRegisterMethod ("L", "test.Derived.lua_get_data", "get")); 
     141       mixin (mixinLuaRegisterMethod ("L", "test.Derived.lua_set_data", "set")); 
     142       mixin (mixinLuaRegisterMethod ("L", "test.Derived.lua_get_one", "one")); 
    134143 
    135     auto b = new Base (); 
    136     auto d = new Derived (); 
     144        L.pushNil (); 
     145        L.pushNumber (2.0); 
     146        L.pushInteger (3); 
    137147 
    138     // push a wrapper for b                                                                                                                                                                                         
    139     L.wrapClass (b); 
     148        writefln ("%s %s %s", L.getObject (-1).toString, L.getObject (-2).toString, L.getObject (-3).toString); 
    140149 
    141     // make it a global var                                                                                                                                                                                         
    142     L.setGlobal ("base"); 
     150         
     151        auto b = new Base (); 
     152        auto d = new Derived (); 
    143153 
    144     // also possible for properties, returning classes                                                                                                                                                              
    145     L.wrapClass (b.data).setGlobal ("i"); 
     154        // push a wrapper for b 
     155       L.wrapClass (b); 
    146156 
    147     L.wrapClass (d).setGlobal ("d"); 
     157        // make it a global var 
     158        L.setGlobal ("base"); 
    148159 
    149     char[] code = `                                                                                                                                                                                                 
    150         print (mylib.static ());                                                                                                                                                                                    
    151                 direct = base:get ();                                                                                                                                                                               
    152                 indirect = i;                                                                                                                                                                                       
    153                 idirect = base:get():get();                                                                                                                                                                         
    154                 iindirect = i:get();                                                                                                                                                                                
    155                 print (i:get());                                                                                                                                                                                    
    156         i:set(10);                                                                                                                                                                                                  
    157         base:set(i);                                                                                                                                                                                                
    158         print (base:get():get(20));                                                                                                                                                                                 
    159                 print (direct:get(), indirect:get(), idirect, iindirect);                                                                                                                                           
    160                 mylib.func ('abc', 1, 2, 3);                                                                                                                                                                        
    161                 bar = mylib.newint (100);                                                                                                                                                                           
    162                 print (bar:get());                                                                                                                                                                                  
    163                 print (d:one());                                                                                                                                                                                    
    164                 d:set( mylib.newint (13) );                                                                                                                                                                         
    165                 print (d:get ());`; 
     160        // also possible for properties, returning classes 
     161        L.wrapClass (b.data).setGlobal ("i"); 
    166162 
    167     L.doString (true, code, "Foo"); 
     163       L.wrapClass (d).setGlobal ("d"); 
    168164 
    169     Stdout ("Output:\n----------\n") (stdout) ("\n-----------\n") (); 
     165        string code = ` 
     166                print (mylib.static ()); 
     167                                direct = base:get (); 
     168                                indirect = i; 
     169                                idirect = base:get():get(); 
     170                                iindirect = i:get(); 
     171                                print (i:get()); 
     172                i:set(10); 
     173                base:set(i); 
     174                print (base:get():get()); 
     175                                print (direct:get(), indirect:get(), idirect, iindirect); 
     176                                mylib.func ('abc', 1, 2, 3); 
     177                                bar = mylib.newint (100); 
     178                                print (bar:get()); 
     179                                print (d:one()); 
     180                                d:set( mylib.newint (13) ); 
     181                                print (d:get ());`; 
    170182 
    171     return 0; 
     183        L.doString (true, code, "Foo"); 
     184 
     185        writef("Output:\n----------\n" ~ stdout ~ "\n-----------\n"); 
     186 
     187        return 0; 
    172188} 
    173  
    174  
    175189}}} 
    176190 
    177 Author: [http://xammy.homelinux.net/wiki/Homepage_von_Matthias_Walter Matthias Walter] 
     191Authors: [http://xammy.homelinux.net/wiki/Homepage_von_Matthias_Walter Matthias Walter] | [http://www.gameformats.de.vu Trass3r]