Changes between Version 2 and Version 3 of LuaLibExamples
- Timestamp:
- 01/19/09 01:49:04 (15 years ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
LuaLibExamples
v2 v3 2 2 3 3 {{{ 4 module test; 5 4 6 import lua.all; 5 7 6 import tango.io.Stdout; 7 import Int = tango.text.convert.Integer; 8 version (Tango) 9 { 10 import tango.io.Stdout; 11 import tango.text.convert.Integer : parseInt = toString; 12 } 13 else 14 { 15 import std.stdio; 16 import std.string : parseInt = toString; 17 } 8 18 9 19 class Integer 10 20 { 11 21 private int data_; 12 22 13 public this (LuaState L) 14 { 23 public this (LuaState L) 24 { 25 data_ = L.checkInteger(1); 26 } 15 27 16 } 28 public this () 29 { 30 this.data_ = 1; 31 } 17 32 18 public this()19 20 this.data = 1;21 33 public int data () 34 { 35 return this.data_; 36 } 22 37 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 } 27 43 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 } 31 48 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 } 35 54 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 } 40 60 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!"); 46 64 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 } 59 67 } 60 68 61 69 class Base 62 70 { 63 71 private Integer data_; 64 72 65 66 67 68 73 this () 74 { 75 this.data_ = new Integer; 76 } 69 77 70 71 72 73 78 public Integer data () 79 { 80 return this.data_; 81 } 74 82 75 76 77 78 79 83 public int lua_get_data (LuaState L) 84 { 85 L.wrapClass (data_); 86 return 1; 87 } 80 88 81 82 83 89 public int lua_set_data (LuaState L) 90 { 91 data_ = L.unwrapClass !(Integer) (1); 84 92 85 86 93 return 0; 94 } 87 95 } 88 96 89 97 class Derived : Base 90 98 { 91 92 93 94 95 99 public int lua_get_one (LuaState L) 100 { 101 L.pushNumber (1); 102 return 1; 103 } 96 104 } 97 105 98 106 int simpleFunction (LuaState L) 99 107 { 100 Stdout.format ("simpleFunction got {} arguments!", L.top).newline;108 writefln("lua_set_data called with: %s", new LuaStackTrace (L)); 101 109 102 110 return 0; 103 111 } 104 112 105 int main (char[][] args) 113 version(D_Version2) mixin("alias const(char) cchar;"); else alias char cchar; 114 int main (string[] args) 106 115 { 107 108 void writer (char[] data)109 110 111 116 char[] stdout; 117 void writer (cchar[] data) 118 { 119 stdout ~= data; 120 } 112 121 113 122 auto L = new LuaState (&writer); 114 123 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")); 118 127 119 // register the constructor 120 mixin (mixinLuaRegisterConstructor ("L", "ClassTest.Integer", "mylib.newint"));128 // register the constructor 129 mixin (mixinLuaRegisterConstructor ("L", "test.Integer", "mylib.newint")); 121 130 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")); 126 135 127 mixin (mixinLuaRegisterMethod ("L", "ClassTest.Base.lua_get_data", "get"));136 mixin (mixinLuaRegisterMethod ("L", "test.Base.lua_get_data", "get")); 128 137 129 mixin (mixinLuaRegisterMethod ("L", "ClassTest.Base.lua_set_data", "set"));138 mixin (mixinLuaRegisterMethod ("L", "test.Base.lua_set_data", "set")); 130 139 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")); 134 143 135 auto b = new Base (); 136 auto d = new Derived (); 144 L.pushNil (); 145 L.pushNumber (2.0); 146 L.pushInteger (3); 137 147 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); 140 149 141 // make it a global var 142 L.setGlobal ("base"); 150 151 auto b = new Base (); 152 auto d = new Derived (); 143 153 144 // also possible for properties, returning classes 145 L.wrapClass (b.data).setGlobal ("i");154 // push a wrapper for b 155 L.wrapClass (b); 146 156 147 L.wrapClass (d).setGlobal ("d"); 157 // make it a global var 158 L.setGlobal ("base"); 148 159 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"); 166 162 167 L.doString (true, code, "Foo");163 L.wrapClass (d).setGlobal ("d"); 168 164 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 ());`; 170 182 171 return 0; 183 L.doString (true, code, "Foo"); 184 185 writef("Output:\n----------\n" ~ stdout ~ "\n-----------\n"); 186 187 return 0; 172 188 } 173 174 175 189 }}} 176 190 177 Author : [http://xammy.homelinux.net/wiki/Homepage_von_Matthias_Walter Matthias Walter]191 Authors: [http://xammy.homelinux.net/wiki/Homepage_von_Matthias_Walter Matthias Walter] | [http://www.gameformats.de.vu Trass3r]