Changeset 324
- Timestamp:
- 01/18/09 20:37:17 (3 years ago)
- Files:
-
- trunk/lua/buffer.d (modified) (5 diffs)
- trunk/lua/common.d (modified) (3 diffs)
- trunk/lua/data.d (modified) (27 diffs)
- trunk/lua/error.d (modified) (21 diffs)
- trunk/lua/lauxlib.d (modified) (5 diffs)
- trunk/lua/lua.d (modified) (9 diffs)
- trunk/lua/lua.lib (added)
- trunk/lua/luaconf.d (modified) (1 diff)
- trunk/lua/lualib.d (modified) (2 diffs)
- trunk/lua/mixins.d (modified) (18 diffs)
- trunk/lua/state.d (modified) (135 diffs)
- trunk/lua/utils.d (modified) (12 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/lua/buffer.d
r314 r324 1 /*******************************************************************************1 /******************************************************************************* 2 2 3 3 copyright: Copyright (c) 2008 Matthias Walter. All rights reserved … … 9 9 module lua.buffer; 10 10 11 private import lua.common; 11 12 private import lua.state; 12 13 private import lua.lauxlib; … … 118 119 *******************************************************************************/ 119 120 120 public LuaBuffer addString (c har[]string)121 public LuaBuffer addString (cstring string) 121 122 { 122 123 luaL_addlstring (&this.buffer_, string.ptr, string.length); … … 196 197 { 197 198 198 char[]output = "";199 200 void write (c har[]data)199 string output = ""; 200 201 void write (cstring data) 201 202 { 202 203 output ~= data; … … 211 212 buffer.addValue (); 212 213 213 auto buf = buffer.prepare ();214 auto buf = buffer.prepare(); 214 215 buf[0] = 'B'; 215 216 buf[1] = 'u'; trunk/lua/common.d
r323 r324 9 9 module lua.common; 10 10 11 // automatically link lua C library 12 version(Windows)pragma(lib, "lua\\lua.lib"); 13 version(linux) pragma(lib, "lua/lua.lib"); 14 11 15 package 12 16 { … … 14 18 version (D_Version2) 15 19 { 20 pragma(msg, "D2 detected. Taking care of constness."); 21 22 // we need a mixin cause the code is syntactically illegal under D1 23 mixin(` 24 alias const(char) cchar; /// const char type 25 alias invariant(char) ichar; /// invariant char type 26 16 27 alias char[] mstring; /// mutable string type 17 28 alias const(char)[] cstring; /// const string type … … 24 35 alias dchar[] mdstring; 25 36 alias const(dchar)[] cdstring; 26 alias invariant(dchar)[] idstring; 37 alias invariant(dchar)[] idstring;`); 27 38 } 28 39 else 29 40 { 30 /* 31 alias char[] string; 32 alias wchar[] wstring; 33 alias dchar[] dstring; 34 */ 41 pragma(msg, "D1 detected. All strings are mutable."); 42 alias char cchar; 43 alias char ichar; 44 45 alias char[] mstring; 46 alias char[] cstring; 47 alias char[] istring; 48 49 alias wchar[] mwstring; 50 alias wchar[] cwstring; 51 alias wchar[] iwstring; 52 53 alias dchar[] mdstring; 54 alias dchar[] cdstring; 55 alias dchar[] idstring; 35 56 } 36 57 } trunk/lua/data.d
r314 r324 1 /*******************************************************************************1 /******************************************************************************* 2 2 3 3 copyright: Copyright (c) 2008 Matthias Walter. All rights reserved … … 9 9 module lua.data; 10 10 11 private import lua.common; 11 12 private import lua.lua; 12 13 private import lua.buffer, lua.mixins, lua.error, lua.state; … … 15 16 version (Tango) 16 17 { 17 private import TangoFloat = tango.text.convert.Float; 18 alias TangoFloat.toString float2string; 18 private import tango.text.convert.Float : float2string = toString; 19 19 } 20 20 else … … 41 41 42 42 /// Returns a user-friendly string representation of the Object. 43 public abstract char[]toString ();43 public abstract istring toString (); 44 44 45 45 /// Size of the header (currently only the LuaType in binary form.) … … 83 83 { 84 84 if (data.length < HEADER_SIZE) 85 throw new LuaFatalException ("Invalid stream: Could not read header." );85 throw new LuaFatalException ("Invalid stream: Could not read header.", __FILE__, __LINE__); 86 86 87 87 LuaObject result; … … 97 97 case LuaType.TABLE: result = new LuaTableObject (); break; 98 98 default: 99 throw new LuaFatalException ("Invalid stream: Wrong type code " ~ int2string (cast (int) * (cast (LuaType*) &data[0])) );99 throw new LuaFatalException ("Invalid stream: Wrong type code " ~ int2string (cast (int) * (cast (LuaType*) &data[0])), __FILE__, __LINE__); 100 100 } 101 101 bytes_read = HEADER_SIZE; … … 124 124 /// Pushes Nil onto the stack. 125 125 126 public void push (LuaState state)126 public override void push (LuaState state) 127 127 { 128 128 state.pushNil (); … … 131 131 /// Returns the string representation. 132 132 133 public override char[]toString ()133 public override istring toString () 134 134 { 135 135 return "nil"; … … 169 169 /// You cannot push a saved Thread into a LuaState! 170 170 171 public void push (LuaState state)172 { 173 throw new LuaFatalException ("Error in LuaThreadObject: Cannot push threads." );174 } 175 176 /// Returns the string representation. 177 178 public override char[]toString ()171 public override void push (LuaState state) 172 { 173 throw new LuaFatalException ("Error in LuaThreadObject: Cannot push threads.", __FILE__, __LINE__); 174 } 175 176 /// Returns the string representation. 177 178 public override istring toString () 179 179 { 180 180 return "thread"; … … 217 217 /// Pushes the value onto the stack. 218 218 219 public void push (LuaState state)219 public override void push (LuaState state) 220 220 { 221 221 state.pushNumber (this.value_); … … 238 238 /// Returns the string representation. 239 239 240 public override char[]toString ()241 { 242 return float2string (this.value_);240 public override istring toString () 241 { 242 return float2string (this.value_); 243 243 } 244 244 … … 257 257 { 258 258 if (data.length < double.sizeof) 259 throw new LuaFatalException ("Invalid stream: Could not read Number." );259 throw new LuaFatalException ("Invalid stream: Could not read Number.", __FILE__, __LINE__); 260 260 261 261 this.value_ = *(cast (double*) data.ptr); … … 286 286 /// Pushes the bool value onto the Lua stack. 287 287 288 public void push (LuaState state)288 public override void push (LuaState state) 289 289 { 290 290 state.pushBool (this.value_); … … 307 307 /// Returns the string representation. 308 308 309 public override char[]toString ()309 public override istring toString () 310 310 { 311 311 return this.value_ ? "true" : "false"; … … 326 326 { 327 327 if (data.length < bool.sizeof) 328 throw new LuaFatalException ("Invalid stream: Could not read Boolean." );328 throw new LuaFatalException ("Invalid stream: Could not read Boolean.", __FILE__, __LINE__); 329 329 330 330 this.value_ = *(cast (bool*) data.ptr); … … 344 344 { 345 345 /// String value 346 private char[]value_;346 private mstring value_; 347 347 348 348 /// Constructs a LuaStringObject. 349 349 350 public this (c har[]value = "")351 { 352 this.value_ = value ;350 public this (cstring value = "") 351 { 352 this.value_ = value.dup; 353 353 } 354 354 355 355 /// Pushes the string onto the Lua stack. 356 356 357 public void push (LuaState state)357 public override void push (LuaState state) 358 358 { 359 359 state.pushString (this.value_); … … 361 361 362 362 /// Sets the value. 363 364 public char[] value (char[]v)365 { 366 return (this.value_ = v);363 364 public istring value (cstring v) 365 { 366 return cast(istring) (this.value_ = v.dup); 367 367 } 368 368 369 369 /// Returns the value. 370 370 371 public char[]value ()372 { 373 return this.value_;374 } 375 376 /// Returns the string representation. 377 378 public override char[]toString ()379 { 380 return "\"" ~ this.value_ ~ "\"";371 public istring value () 372 { 373 return cast(istring) this.value_; 374 } 375 376 /// Returns the string representation. 377 378 public override istring toString () 379 { 380 return cast(istring) ("\"" ~ this.value_ ~ "\""); 381 381 } 382 382 … … 398 398 { 399 399 if (data.length < size_t.sizeof) 400 throw new LuaFatalException ("Invalid stream: Could not read String length." );400 throw new LuaFatalException ("Invalid stream: Could not read String length.", __FILE__, __LINE__); 401 401 402 402 this.value_ = new char[ *(cast (size_t*) data.ptr) ]; 403 403 if (data.length < size_t.sizeof + this.value_.length) 404 throw new LuaFatalException ("Invalid stream: Could not read String." );404 throw new LuaFatalException ("Invalid stream: Could not read String.", __FILE__, __LINE__); 405 405 406 406 auto p = cast (char*) &data[size_t.sizeof]; … … 443 443 /// Pushes the LightUserdata onto the stack. Throws an error if it is a plain Userdata. 444 444 445 public void push (LuaState state)445 public override void push (LuaState state) 446 446 { 447 447 if (this.is_lightuserdata_) 448 448 state.pushLightUserdata (this.value_); 449 449 else 450 throw new LuaFatalException ("Error in LuaUserdataObject: Cannot push userdata." );450 throw new LuaFatalException ("Error in LuaUserdataObject: Cannot push userdata.", __FILE__, __LINE__); 451 451 } 452 452 … … 467 467 /// Returns the string representation. 468 468 469 public override char[]toString ()469 public override istring toString () 470 470 { 471 471 return (this.is_lightuserdata_ ? "LightUserdata: " : "Userdata: ") ~ int2string (cast (int) this.value_); … … 486 486 { 487 487 if (data.length < (void*).sizeof) 488 throw new LuaFatalException ("Invalid stream: Could not read Userdata." );488 throw new LuaFatalException ("Invalid stream: Could not read Userdata.", __FILE__, __LINE__); 489 489 490 490 this.value_ = *(cast (void**) data.ptr); … … 529 529 /// Pushes the C function onto the stack. 530 530 531 public void push (LuaState state)531 public override void push (LuaState state) 532 532 { 533 533 state.pushCFunction (this.value_); … … 536 536 /// Returns the string representation. 537 537 538 public override char[]toString ()538 public override istring toString () 539 539 { 540 540 if (this.value_ is null) … … 558 558 { 559 559 if (data.length < LuaCFunction.sizeof) 560 throw new LuaFatalException ("Invalid stream: Could not read Function." );560 throw new LuaFatalException ("Invalid stream: Could not read Function.", __FILE__, __LINE__); 561 561 562 562 this.value_ = *(cast (LuaCFunction*) data.ptr); … … 609 609 /// Pushes onto the stack the complete table. 610 610 611 public void push (LuaState state)611 public override void push (LuaState state) 612 612 { 613 613 state.createTable (); … … 623 623 /// Returns the string representation. 624 624 625 public override char[]toString ()625 public override istring toString () 626 626 { 627 627 if (this.data_ !is null) 628 628 { 629 char[]result = "{ ";629 istring result = "{ "; 630 630 foreach (pair; this.data_) 631 631 { … … 676 676 { 677 677 if (data.length < uint.sizeof) 678 throw new LuaFatalException ("Invalid stream: Could not read Table." );678 throw new LuaFatalException ("Invalid stream: Could not read Table.", __FILE__, __LINE__); 679 679 680 680 this.data_ = new KeyValuePair [ *(cast (uint*) data.ptr) ]; trunk/lua/error.d
r314 r324 1 /*******************************************************************************1 /******************************************************************************* 2 2 3 3 copyright: Copyright (c) 2008 Matthias Walter. All rights reserved … … 9 9 module lua.error; 10 10 11 private import lua.common; 11 12 private import lua.lua; 12 13 private import lua.lauxlib; … … 55 56 ***************************************************************************/ 56 57 57 private static CodeType parseCodeType (c har[]str)58 private static CodeType parseCodeType (cstring str) 58 59 { 59 60 switch (str) … … 63 64 case "main": return CodeType.MAIN; 64 65 case "tail": return CodeType.TAIL; 65 default: throw new LuaFatalException ( str ~ " is an invalid LuaCodeType.");66 default: throw new LuaFatalException ("parseCodeType: " ~ str ~ " is an invalid LuaCodeType."); 66 67 } 67 68 } … … 76 77 ***************************************************************************/ 77 78 78 public static char[]toString (CodeType code_type)79 public static istring toString (CodeType code_type) 79 80 { 80 81 switch (code_type) … … 84 85 case CodeType.MAIN: return "main"; 85 86 case CodeType.TAIL: return "tail call"; 87 default: throw new LuaFatalException ("toString: " ~ int2string(cast(int)code_type) ~ " is an invalid LuaCodeType."); 86 88 } 87 89 } … … 96 98 ***************************************************************************/ 97 99 98 private static NameType parseNameType (c har[]str)100 private static NameType parseNameType (cstring str) 99 101 { 100 102 switch (str) … … 118 120 ***************************************************************************/ 119 121 120 public static char[]toString (NameType name_type)122 public static istring toString (NameType name_type) 121 123 { 122 124 switch (name_type) … … 137 139 private NameType name_type_; 138 140 /// Name 139 private char[]name_;141 private mstring name_; 140 142 /// Whether this function was loaded from a file. 141 143 private bool is_from_file_; 142 144 /// Source string, containing the source or filename. 143 private char[]source_;145 private mstring source_; 144 146 /// Start of the definition of this function in the source. 145 147 private uint definition_start_; … … 221 223 /// Returns the name of the function. 222 224 223 public char[]name ()224 { 225 return this.name_;225 public istring name () 226 { 227 return cast(istring) this.name_; 226 228 } 227 229 … … 235 237 /// Returns the source for the function or the filename, where it was defined. 236 238 237 public char[]source ()238 { 239 return this.source_;239 public istring source () 240 { 241 return cast(istring) this.source_; 240 242 } 241 243 … … 270 272 /// Returns of compact string representation of the information about the function. 271 273 272 public char[]toString ()273 { 274 char[]result = toString (codeType) ~ " function";274 public istring toString () 275 { 276 istring result = toString (codeType) ~ " function"; 275 277 276 278 if (this.name_ !is null) … … 326 328 /// Prints the Call Trace in a user-friendly way. 327 329 328 public char[]toString ()329 { 330 331 char[]result = "* Call Trace (last function call)\n";330 public istring toString () 331 { 332 333 istring result = "* Call Trace (last function call)\n"; 332 334 foreach (level, record; this.records_) 333 335 { … … 371 373 /// Prints the Stack Trace in a user-friendly way. 372 374 373 public char[]toString ()374 { 375 char[]result = "# Stack Trace (top of stack)\n";375 public istring toString () 376 { 377 istring result = "# Stack Trace (top of stack)\n"; 376 378 foreach_reverse (i, obj; this.objects_) 377 379 { … … 401 403 ***************************************************************************/ 402 404 403 this (char[] message) 404 { 405 super (message); 406 } 405 this (cstring message) 406 { 407 super (cast(istring) message); 408 } 409 410 /// ditto 411 this (cstring message, cstring file, size_t line) 412 { 413 version(D_Version2) 414 super(cast(istring) message, cast(istring) file, line); 415 else 416 super(this.classinfo.name ~ "@" ~ file ~ "(" ~ int2string(line) ~ "): " ~ message); 417 } 407 418 408 419 /*************************************************************************** … … 428 439 super ("Unknown Lua error"); 429 440 } 441 442 /// ditto 443 this (int error, cstring file, size_t line) 444 { 445 version(D_Version2) 446 { 447 if (error == LUA_ERRRUN) 448 super ("Runtime error", cast(istring) file, line); 449 else if (error == LUA_ERRMEM) 450 super ("Memory allocation error", cast(istring) file, line); 451 else if (error == LUA_ERRERR) 452 super ("Error while running the error handler function", cast(istring) file, line); 453 else if (error == LUA_ERRFILE) 454 super ("Error opening the file", cast(istring) file, line); 455 else 456 super ("Unknown Lua error", cast(istring) file, line); 457 } 458 else 459 { 460 if (error == LUA_ERRRUN) 461 super (this.classinfo.name ~ "@" ~ file ~ "(" ~ int2string(line) ~ "): " ~ "Runtime error"); 462 else if (error == LUA_ERRMEM) 463 super (this.classinfo.name ~ "@" ~ file ~ "(" ~ int2string(line) ~ "): " ~ "Memory allocation error"); 464 else if (error == LUA_ERRERR) 465 super (this.classinfo.name ~ "@" ~ file ~ "(" ~ int2string(line) ~ "): " ~ "Error while running the error handler function"); 466 else if (error == LUA_ERRFILE) 467 super (this.classinfo.name ~ "@" ~ file ~ "(" ~ int2string(line) ~ "): " ~ "Error opening the file"); 468 else 469 super (this.classinfo.name ~ "@" ~ file ~ "(" ~ int2string(line) ~ "): " ~ "Unknown Lua error"); 470 } 471 } 430 472 } 431 473 … … 445 487 ***************************************************************************/ 446 488 447 public this (char[] message) 448 { 449 super (message); 450 } 489 public this (cstring message) 490 { 491 super (cast(istring) message); 492 } 493 494 /// ditto 495 this (cstring message, cstring file, size_t line) 496 { 497 version(D_Version2) 498 super(cast(istring) message, cast(istring) file, line); 499 else 500 super(this.classinfo.name ~ "@" ~ file ~ "(" ~ int2string(line) ~ "): " ~ message); 501 } 451 502 } 452 503 … … 465 516 ***************************************************************************/ 466 517 467 public this (char[] message) 468 { 469 super (message); 470 } 471 } 472 473 474 475 476 518 public this (cstring message) 519 { 520 super (cast(istring) message); 521 } 522 523 /// ditto 524 this (cstring message, cstring file, size_t line) 525 { 526 version(D_Version2) 527 super(cast(istring) message, cast(istring) file, line); 528 else 529 super(this.classinfo.name ~ "@" ~ file ~ "(" ~ int2string(line) ~ "): " ~ message); 530 } 531 } 532 533 534 535 536 // D1 phobos Exceptions are missing an "Exception next;" attribute 477 537 version (Tango) 478 {479 538 alias Exception ExtendedException; 480 } 539 else version (D_Version2) 540 alias Exception ExtendedException; 481 541 else 482 542 { 483 543 class ExtendedException : Exception 484 544 { 485 private Exception next_; 486 487 this (char[] message, Exception next) 488 { 489 this.next_ = next; 490 super (message); 491 } 492 493 this (char[] message, char[] file, int line, Exception next) 494 { 495 this.next_ = next; 496 super (message); 497 } 545 Exception next; 546 istring file; 547 size_t line; 548 549 this (cstring message, Exception next=null) 550 { 551 this.next = next; 552 super (message); 553 } 554 555 this (cstring message, cstring file, size_t line, Exception next=null) 556 { 557 this.next = next; 558 super (message); 559 this.file = file; 560 this.line = line; 561 } 562 563 override istring toString() 564 { 565 mstring buf; 566 567 if (this.file) 568 { 569 buf ~= this.classinfo.name ~ "@" ~ this.file ~ "(" ~ int2string(this.line) ~ "): " ~ this.msg; 570 } 571 else 572 { 573 buf ~= this.classinfo.name ~ ": " ~ this.msg; 574 } 575 buf ~= this.next.classinfo.name ~ this.next.toString(); 576 return cast(istring) buf; 577 } 498 578 } 499 579 } … … 541 621 ***************************************************************************/ 542 622 543 public this (LuaState state, Exception exception, c har[]catcher_name)544 { 545 super (ca tcher_name ~ " caught:\n\n" ~ this.call_trace_.toString ~ "\n\n" ~ this.stack_trace_.toString, exception);623 public this (LuaState state, Exception exception, cstring catcher_name) 624 { 625 super (cast(istring) (catcher_name ~ " caught:\n\n" ~ this.call_trace_.toString ~ "\n\n" ~ this.stack_trace_.toString), exception); 546 626 547 627 this.call_trace_ = new LuaCallTrace (state); … … 563 643 ***************************************************************************/ 564 644 565 public this (LuaState state, Exception exception, c har[] catcher_name, char[] file, longline)645 public this (LuaState state, Exception exception, cstring catcher_name, cstring file, size_t line) 566 646 { 567 647 this.call_trace_ = new LuaCallTrace (state); 568 648 this.stack_trace_ = new LuaStackTrace (state); 569 649 570 super (ca tcher_name ~ " caught:\n\n" ~ this.call_trace_.toString ~ "\n\n" ~ this.stack_trace_.toString,file, line, exception);650 super (cast(istring) (catcher_name ~ " caught:\n\n" ~ this.call_trace_.toString ~ "\n\n" ~ this.stack_trace_.toString), cast(istring) file, line, exception); 571 651 } 572 652 … … 580 660 ***************************************************************************/ 581 661 582 public void forward (c har[]forwarder_name)583 { 584 this.msg = forwarder_name ~ " forwarded, " ~ this.msg;662 public void forward (cstring forwarder_name) 663 { 664 this.msg = cast(istring) (forwarder_name ~ " forwarded, " ~ this.msg); 585 665 } 586 666 trunk/lua/lauxlib.d
r265 r324 1 module lua.lauxlib;1 module lua.lauxlib; 2 2 3 private import lua.common; 3 4 private import lua.lua; 4 5 private import lua.luaconf; … … 13 14 struct luaL_Reg 14 15 { 15 c har *name;16 cchar *name; 16 17 lua_CFunction func; 17 18 } 18 19 19 void luaL_openlib(lua_State *L, c har *libname, luaL_Reg *l, int nup);20 void luaL_register(lua_State *L, c har *libname, luaL_Reg *l);21 int luaL_getmetafield(lua_State *L, int obj, c har *e);22 int luaL_callmeta(lua_State *L, int obj, c har *e);23 int luaL_typerror(lua_State *L, int narg, c har *tname);24 int luaL_argerror(lua_State *L, int numarg, c har *extramsg);25 char * luaL_checklstring(lua_State *L, int numArg, size_t *l);26 char * luaL_optlstring(lua_State *L, int numArg,char *def, size_t *l);20 void luaL_openlib(lua_State *L, cchar *libname, luaL_Reg *l, int nup); 21 void luaL_register(lua_State *L, cchar *libname, luaL_Reg *l); 22 int luaL_getmetafield(lua_State *L, int obj, cchar *e); 23 int luaL_callmeta(lua_State *L, int obj, cchar *e); 24 int luaL_typerror(lua_State *L, int narg, cchar *tname); 25 int luaL_argerror(lua_State *L, int numarg, cchar *extramsg); 26 ichar * luaL_checklstring(lua_State *L, int numArg, size_t *l); 27 ichar * luaL_optlstring(lua_State *L, int numArg, cchar *def, size_t *l); 27 28 lua_Number luaL_checknumber(lua_State *L, int numArg); 28 29 lua_Number luaL_optnumber(lua_State *L, int nArg, lua_Number def); … … 31 32 lua_Integer luaL_optinteger(lua_State *L, int nArg, lua_Integer def); 32 33 33 void luaL_checkstack(lua_State *L, int sz, c har *msg);34 void luaL_checkstack(lua_State *L, int sz, cchar *msg); 34 35 void luaL_checktype(lua_State *L, int narg, int t); 35 36 void luaL_checkany(lua_State *L, int narg); 36 37 37 int luaL_newmetatable(lua_State *L, c har *tname);38 void * luaL_checkudata(lua_State *L, int ud, c har *tname);38 int luaL_newmetatable(lua_State *L, cchar *tname); 39 void * luaL_checkudata(lua_State *L, int ud, cchar *tname); 39 40 40 41 void luaL_where(lua_State *L, int lvl); 41 int luaL_error(lua_State *L, c har *fmt,...);42 int luaL_error(lua_State *L, cchar *fmt,...); 42 43 43 int luaL_checkoption(lua_State *L, int narg, c har *def,char **lst);44 int luaL_checkoption(lua_State *L, int narg, cchar *def, cchar **lst); 44 45 45 46 int luaL_ref(lua_State *L, int t); 46 47 void luaL_unref(lua_State *L, int t, int _ref); 47 48 48 int luaL_loadfile(lua_State *L, c har *filename);49 int luaL_loadbuffer(lua_State *L, c har *buff, size_t sz,char *name);50 int luaL_loadstring(lua_State *L, c har *s);49 int luaL_loadfile(lua_State *L, cchar *filename); 50 int luaL_loadbuffer(lua_State *L, cchar *buff, size_t sz, cchar *name); 51 int luaL_loadstring(lua_State *L, cchar *s); 51 52 52 53 lua_State * luaL_newstate(); 53 54 54 char * luaL_gsub(lua_State *L, char *s, char *p,char *r);55 ichar * luaL_gsub(lua_State *L, cchar *s, cchar *p, cchar *r); 55 56 56 char * luaL_findtable(lua_State *L, int idx,char *fname, int szhint);57 ichar * luaL_findtable(lua_State *L, int idx, cchar *fname, int szhint); 57 58 58 59 // some useful macros 59 60 60 void luaL_argcheck(lua_State* L, int cond, int numarg, c har* extramsg) { if (!cond) luaL_argerror(L, numarg, extramsg); }61 char* luaL_checkstring(lua_State* L, int n) { return luaL_checklstring(L, n, null); }62 char* luaL_optstring(lua_State* L, int n,char* d) { return luaL_optlstring(L, n, d, null); }61 void luaL_argcheck(lua_State* L, int cond, int numarg, cchar* extramsg) { if (!cond) luaL_argerror(L, numarg, extramsg); } 62 ichar* luaL_checkstring(lua_State* L, int n) { return luaL_checklstring(L, n, null); } 63 ichar* luaL_optstring(lua_State* L, int n, cchar* d) { return luaL_optlstring(L, n, d, null); } 63 64 int luaL_checkint(lua_State* L, int n) { return luaL_checkinteger(L, n); } 64 65 int luaL_optint (lua_State* L, int n, int d) { return luaL_optinteger(L, n, d); } … … 66 67 int luaL_optlong(lua_State* L, int n, int d) { return luaL_optinteger(L, n, d); } 67 68 68 char* luaL_typename(lua_State* L, int i) { return lua_typename(L, lua_type(L, i)); }69 ichar* luaL_typename(lua_State* L, int i) { return lua_typename(L, lua_type(L, i)); } 69 70 70 int luaL_dofile(lua_State* L, c har* fn) { return luaL_loadfile(L, fn) || lua_pcall(L, 0, LUA_MULTRET, 0); }71 int luaL_dofile(lua_State* L, cchar* fn) { return luaL_loadfile(L, fn) || lua_pcall(L, 0, LUA_MULTRET, 0); } 71 72 72 int luaL_dostring(lua_State*L, c har* s) { return luaL_loadstring(L, s) || lua_pcall(L, 0, LUA_MULTRET, 0); }73 int luaL_dostring(lua_State*L, cchar* s) { return luaL_loadstring(L, s) || lua_pcall(L, 0, LUA_MULTRET, 0); } 73 74 74 void luaL_getmetatable(lua_State* L, c har* s) { lua_getfield(L, LUA_REGISTRYINDEX, s); }75 void luaL_getmetatable(lua_State* L, cchar* s) { lua_getfield(L, LUA_REGISTRYINDEX, s); } 75 76 76 77 bool luaL_opt(lua_State* L, int function(lua_State*, int) f, int n, int d) { return luaL_opt(L, f, n, d); } … … 100 101 void luaL_buffinit(lua_State *L, luaL_Buffer *B); 101 102 char * luaL_prepbuffer(luaL_Buffer *B); 102 void luaL_addlstring(luaL_Buffer *B, c har *s, size_t l);103 void luaL_addstring(luaL_Buffer *B, c har *s);103 void luaL_addlstring(luaL_Buffer *B, cchar *s, size_t l); 104 void luaL_addstring(luaL_Buffer *B, cchar *s); 104 105 void luaL_addvalue(luaL_Buffer *B); 105 106 void luaL_pushresult(luaL_Buffer *B); trunk/lua/lua.d
r265 r324 1 module lua.lua; 2 1 module lua.lua; 2 3 private import lua.common; 3 4 private import lua.luaconf, lua.lauxlib; 4 5 5 6 extern (C): 6 7 7 const char[]LUA_VERSION = "Lua 5.1";8 const char[] LUA_RELEASE = "Lua 5.1.3";8 const LUA_VERSION = "Lua 5.1"; 9 const LUA_RELEASE = "Lua 5.1.4"; 9 10 const LUA_VERSION_NUM = 501; 10 const char[]LUA_COPYRIGHT = "Copyright (C) 1994-2008 Lua.org, PUC-Rio";11 const char[]LUA_AUTHORS = "R. Ierusalimschy, L. H. de Figueiredo & W. Celes";11 const LUA_COPYRIGHT = "Copyright (C) 1994-2008 Lua.org, PUC-Rio"; 12 const LUA_AUTHORS = "R. Ierusalimschy, L. H. de Figueiredo & W. Celes"; 12 13 13 14 14 15 // mark for precompiled code (`<esc>Lua') 15 const char[]LUA_SIGNATURE = "\033Lua";16 const LUA_SIGNATURE = "\033Lua"; 16 17 17 18 // option for multiple returns in `lua_pcall' and `lua_call' … … 91 92 int lua_isuserdata(lua_State *L, int idx); 92 93 int lua_type(lua_State *L, int idx); 93 char * lua_typename(lua_State *L, int tp);94 ichar * lua_typename(lua_State *L, int tp); 94 95 int lua_equal(lua_State *L, int idx1, int idx2); 95 96 int lua_rawequal(lua_State *L, int idx1, int idx2); … … 99 100 lua_Integer lua_tointeger(lua_State *L, int idx); 100 101 int lua_toboolean(lua_State *L, int idx); 101 char * lua_tolstring(lua_State *L, int idx, size_t *len);102 ichar * lua_tolstring(lua_State *L, int idx, size_t *len); 102 103 size_t lua_objlen(lua_State *L, int idx); 103 104 lua_CFunction lua_tocfunction(lua_State *L, int idx); … … 111 112 void lua_pushnumber(lua_State *L, lua_Number n); 112 113 void lua_pushinteger(lua_State *L, lua_Integer n); 113 void lua_pushlstring(lua_State *L, c har *s, size_t l);114 void lua_pushstring(lua_State *L, c har *s);115 char * lua_pushvfstring(lua_State *L,char *fmt, ...);116 char * lua_pushfstring(lua_State *L,char *fmt,...);114 void lua_pushlstring(lua_State *L, cchar *s, size_t l); 115 void lua_pushstring(lua_State *L, cchar *s); 116 ichar * lua_pushvfstring(lua_State *L, cchar *fmt, ...); 117 ichar * lua_pushfstring(lua_State *L, cchar *fmt,...); 117 118 void lua_pushcclosure(lua_State *L, lua_CFunction fn, int n); 118 119 void lua_pushboolean(lua_State *L, int b); … … 122 123 // get functions (Lua -> stack) 123 124 void lua_gettable(lua_State *L, int idx); 124 void lua_getfield(lua_State *L, int idx, c har *k);125 void lua_getfield(lua_State *L, int idx, cchar *k); 125 126 void lua_rawget(lua_State *L, int idx); 126 127 void lua_rawgeti(lua_State *L, int idx, int n); … … 132 133 // set functions (stack -> Lua) 133 134 void lua_settable(lua_State *L, int idx); 134 void lua_setfield(lua_State *L, int idx, c har *k);135 void lua_setfield(lua_State *L, int idx, cchar *k); 135 136 void lua_rawset(lua_State *L, int idx); 136 137 void lua_rawseti(lua_State *L, int idx, int n); … … 142 143 int lua_pcall(lua_State *L, int nargs, int nresults, int errfunc); 143 144 int lua_cpcall(lua_State *L, lua_CFunction func, void *ud); 144 int lua_load(lua_State *L, lua_Reader reader, void *dt, c har *chunkname);145 int lua_load(lua_State *L, lua_Reader reader, void *dt, cchar *chunkname); 145 146 int lua_dump(lua_State *L, lua_Writer writer, void *data); 146 147 … … 188 189 void lua_setglobal(lua_State* L, char* s) { lua_setfield(L, LUA_GLOBALSINDEX, s); } 189 190 void lua_getglobal(lua_State* L, char* s) { lua_getfield(L, LUA_GLOBALSINDEX, s); } 190 char* lua_tostring(lua_State* L, int i) { return lua_tolstring(L, i, null); }191 ichar* lua_tostring(lua_State* L, int i) { return lua_tolstring(L, i, null); } 191 192 192 193 // compatibility macros and functions … … 219 220 220 221 int lua_getstack(lua_State *L, int level, lua_Debug *ar); 221 int lua_getinfo(lua_State *L, c har *what, lua_Debug *ar);222 char * lua_getlocal(lua_State *L, lua_Debug *ar, int n);223 char * lua_setlocal(lua_State *L, lua_Debug *ar, int n);224 char * lua_getupvalue(lua_State *L, int funcindex, int n);225 char * lua_setupvalue(lua_State *L, int funcindex, int n);222 int lua_getinfo(lua_State *L, cchar *what, lua_Debug *ar); 223 ichar * lua_getlocal(lua_State *L, lua_Debug *ar, int n); 224 ichar * lua_setlocal(lua_State *L, lua_Debug *ar, int n); 225 ichar * lua_getupvalue(lua_State *L, int funcindex, int n); 226 ichar * lua_setupvalue(lua_State *L, int funcindex, int n); 226 227 int lua_sethook(lua_State *L, lua_Hook func, int mask, int count); 227 228 lua_Hook lua_gethook(lua_State *L); trunk/lua/luaconf.d
r265 r324 1 module lua.luaconf;1 module lua.luaconf; 2 2 3 3 extern (C): trunk/lua/lualib.d
r265 r324 1 module lua.lualib;1 module lua.lualib; 2 2 3 3 private import lua.lua; … … 5 5 extern (C): 6 6 7 const char[]LUA_FILEHANDLE = "FILE*";8 const char[]LUA_COLIBNAME = "coroutine";7 const LUA_FILEHANDLE = "FILE*"; 8 const LUA_COLIBNAME = "coroutine"; 9 9 int luaopen_base(lua_State *L); 10 const char[]LUA_TABLIBNAME = "table";10 const LUA_TABLIBNAME = "table"; 11 11 int luaopen_table(lua_State *L); 12 const char[]LUA_IOLIBNAME = "io";12 const LUA_IOLIBNAME = "io"; 13 13 int luaopen_io(lua_State *L); 14 const char[]LUA_OSLIBNAME = "os";14 const LUA_OSLIBNAME = "os"; 15 15 int luaopen_os(lua_State *L); 16 const char[]LUA_STRLIBNAME = "string";16 const LUA_STRLIBNAME = "string"; 17 17 int luaopen_string(lua_State *L); 18 const char[]LUA_MATHLIBNAME = "math";18 const LUA_MATHLIBNAME = "math"; 19 19 int luaopen_math(lua_State *L); 20 const char[]LUA_DBLIBNAME = "debug";20 const LUA_DBLIBNAME = "debug"; 21 21 int luaopen_debug(lua_State *L); 22 const char[]LUA_LOADLIBNAME = "package";22 const LUA_LOADLIBNAME = "package"; 23 23 int luaopen_package(lua_State *L); 24 24 trunk/lua/mixins.d
r314 r324 1 /*******************************************************************************1 /******************************************************************************* 2 2 3 3 copyright: Copyright (c) 2008 Matthias Walter. All rights reserved … … 9 9 module lua.mixins; 10 10 11 public import lua.error : LuaForwardException; 12 private import lua.utils : rfind, ltrim, removeFirst, join; 13 public import lua.utils : int2string; 14 public import lua.lauxlib : luaL_error; 15 public import lua.lua : lua_State; 11 private import lua.common; 12 public import lua.error; 13 private import lua.utils; 14 public import lua.utils; 15 public import lua.lauxlib; 16 private import lua.lua; 16 17 17 18 /******************************************************************************* … … 21 22 *******************************************************************************/ 22 23 23 public static char[] mangleClass (char[]class_name)24 { 25 char[]result = "d_class_" ~ class_name;24 public static mstring mangleClass (cstring class_name) 25 { 26 mstring result = "d_class_" ~ class_name; 26 27 foreach (i, c; result) 27 28 { … … 38 39 *******************************************************************************/ 39 40 40 public static char[] mangleFunction (char[]function_name)41 { 42 char[]result = "d_function_" ~ function_name;41 public static mstring mangleFunction (cstring function_name) 42 { 43 mstring result = "d_function_" ~ function_name; 43 44 foreach (i, c; result) 44 45 { … … 63 64 *******************************************************************************/ 64 65 65 public static char[] mixinLuaRegisterMethodAtLine (char[] lua_state, char[] class_name, char[] method_name, char[] lua_name, int line_number) 66 { 67 char[] wrapper = `lua_wrapper_` ~ mangleClass (class_name) ~ "_" ~ method_name ~ "_" ~ int2string (line_number); 68 69 return `` 70 ~ `{` 66 public static istring mixinLuaRegisterMethodAtLine (cstring lua_state, cstring class_name, cstring method_name, cstring lua_name, int line_number) 67 { 68 istring wrapper = cast(istring) (`lua_wrapper_` ~ mangleClass (class_name) ~ "_" ~ method_name ~ "_" ~ int2string (line_number)); 69 70 return cast(istring) (`{` 71 71 ~ ` extern (C) static int ` ~ wrapper ~ ` (lua_State *L)` 72 72 ~ ` {` … … 75 75 ~ ` {` 76 76 ~ ` void* userdata = luaL_checkudata (L, 1, "` ~ mangleClass (class_name) ~ `");` // Check, whether it's the correct userdata 77 ~ ` luaL_argcheck (L, userdata ! =null, 1, "class pointer expected");`77 ~ ` luaL_argcheck (L, userdata !is null, 1, "class pointer expected");` 78 78 ~ ` lua_remove (L, 1);` // Remove the userdata 79 ~ ` return (cast (` ~class_name ~ ` *) userdata).` ~ method_name ~ ` ( LuaState.states[L]);` // Call the wrapped79 ~ ` return (cast (` ~class_name ~ ` *) userdata).` ~ method_name ~ ` (state);` // Call the wrapped 80 80 ~ ` }` 81 81 ~ ` catch (Exception e)` … … 83 83 ~ ` auto f = new LuaForwardException (state, e, "` ~ class_name ~ `.` ~ method_name ~ `", __FILE__, __LINE__);` 84 84 ~ ` LuaForwardException.exceptions[cast (void*) f] = f;` 85 ~ ` return luaL_error (L, lua.utils.toStringz ("LFE=" ~ lua.mixins.int2string (cast ( ulong) cast(void*) f) ~ ";"));`85 ~ ` return luaL_error (L, lua.utils.toStringz ("LFE=" ~ lua.mixins.int2string (cast (size_t) cast(void*) f) ~ ";"));` 86 86 ~ ` }` 87 87 ~ ` }` … … 89 89 ~ ` ` ~ lua_state ~ `.registerMethod ("` ~ lua_name ~ `", cast (LuaCFunction) &` ~ wrapper ~ `);` 90 90 ~ ` ` ~ lua_state ~ `.pop ();` 91 ~ `}` ;91 ~ `}`); 92 92 } 93 93 … … 126 126 *******************************************************************************/ 127 127 128 public static char[] mixinLuaRegisterMethod (char[] lua_state, char[] class_dot_method, char[]lua_name)128 public static istring mixinLuaRegisterMethod (cstring lua_state, cstring class_dot_method, cstring lua_name) 129 129 { 130 130 int pos = rfind (class_dot_method, '.'); 131 char[] class_name = class_dot_method [0 .. pos]; 132 char[] method_name = class_dot_method [pos+1 .. $]; 133 134 return `mixin (mixinLuaRegisterMethodAtLine ("` ~ lua_state ~ `", "` ~ class_name ~ `", "` ~ method_name ~ `", "` ~ lua_name ~ `", __LINE__));`; 131 cstring class_name = class_dot_method [0 .. pos]; 132 cstring method_name = class_dot_method [pos+1 .. $]; 133 return cast(istring) (`mixin (mixinLuaRegisterMethodAtLine ("` ~ lua_state ~ `", "` ~ class_name ~ `", "` ~ method_name ~ `", "` ~ lua_name ~ `", __LINE__));`); 135 134 } 136 135 … … 148 147 *******************************************************************************/ 149 148 150 public static char[] mixinLuaPushFunctionAtLine (char[] lua_state, char[]name, int line_number)151 { 152 char[] wrapper = "lua_wrapper_" ~ mangleFunction (name) ~ "_" ~ int2string (line_number);153 154 return ``149 public static istring mixinLuaPushFunctionAtLine (cstring lua_state, cstring name, int line_number) 150 { 151 istring wrapper = cast(istring) ("lua_wrapper_" ~ mangleFunction (name) ~ "_" ~ int2string (line_number)); 152 153 return cast(istring) (`` 155 154 ~ `{` 156 155 ~ ` extern (C) static int ` ~ wrapper ~ ` (lua_State *L)` … … 165 164 ~ ` auto f = new LuaForwardException (state, e, "` ~ name ~ `", __FILE__, __LINE__);` 166 165 ~ ` LuaForwardException.exceptions[cast (void*) f] = f;` 167 ~ ` return luaL_error (L, lua.utils.toStringz ("LFE=" ~ lua.mixins.int2string (cast ( ulong) cast(void*) f) ~ ";"));`166 ~ ` return luaL_error (L, lua.utils.toStringz ("LFE=" ~ lua.mixins.int2string (cast (size_t) cast(void*) f) ~ ";"));` 168 167 ~ ` }` 169 168 ~ ` }` 170 169 ~ ` ` ~ lua_state ~ `.pushCFunction (cast (int function (lua_State *L)) &` ~ wrapper ~ `);` 171 ~ `}` ;170 ~ `}`); 172 171 } 173 172 … … 198 197 *******************************************************************************/ 199 198 200 public static char[] mixinLuaPushFunction (char[] lua_state, char[]name)201 { 202 return `mixin (mixinLuaPushFunctionAtLine ("` ~ lua_state ~ `", "` ~ name ~ `", __LINE__));`;199 public static istring mixinLuaPushFunction (cstring lua_state, cstring name) 200 { 201 return cast(istring) (`mixin (mixinLuaPushFunctionAtLine ("` ~ lua_state ~ `", "` ~ name ~ `", __LINE__));`); 203 202 } 204 203 … … 217 216 *******************************************************************************/ 218 217 219 public static char[] mixinLuaRegisterFunctionAtLine (char[] lua_state, char[] name, char[]lua_library_dot_name, int line_number)218 public static istring mixinLuaRegisterFunctionAtLine (cstring lua_state, cstring name, cstring lua_library_dot_name, int line_number) 220 219 { 221 220 int pos = rfind (lua_library_dot_name, '.'); 222 c har[]lua_library = pos < 0 ? "null" : "\"" ~ lua_library_dot_name[0 .. pos] ~ "\"";223 c har[]lua_function = lua_library_dot_name[pos+1 .. $];224 c har[]wrapper = "lua_wrapper_" ~ mangleFunction (name) ~ "_" ~ int2string (line_number);225 226 return ``221 cstring lua_library = pos < 0 ? "null" : "\"" ~ lua_library_dot_name[0 .. pos] ~ "\""; 222 cstring lua_function = lua_library_dot_name[pos+1 .. $]; 223 cstring wrapper = "lua_wrapper_" ~ mangleFunction (name) ~ "_" ~ int2string (line_number); 224 225 return cast(istring) (`` 227 226 ~ `{` 228 227 ~ ` extern (C) static int ` ~ wrapper ~ ` (lua_State* L)` … … 237 236 ~ ` auto f = new LuaForwardException (state, e, "` ~ name ~ `", __FILE__, __LINE__);` 238 237 ~ ` LuaForwardException.exceptions[cast (void*) f] = f;` 239 ~ ` return luaL_error (L, lua.utils.toStringz ("LFE=" ~ lua.mixins.int2string (cast ( ulong) cast(void*) f) ~ ";"));`238 ~ ` return luaL_error (L, lua.utils.toStringz ("LFE=" ~ lua.mixins.int2string (cast (size_t) cast(void*) f) ~ ";"));` 240 239 ~ ` }` 241 240 ~ ` }` 242 241 ~ ` ` ~ lua_state ~ `.registerFunction ("` ~ lua_function ~ `", cast (int function (lua_State* L)) &` ~ wrapper ~ `, ` ~ lua_library ~ `);` 243 ~ `}` ;242 ~ `}`); 244 243 } 245 244 … … 271 270 *******************************************************************************/ 272 271 273 public static char[] mixinLuaRegisterFunction (char[] lua_state, char[] name, char[]lua_library_dot_name)274 { 275 return `mixin (mixinLuaRegisterFunctionAtLine ("` ~ lua_state ~ `", "` ~ name ~ `", "` ~ lua_library_dot_name ~ `", __LINE__));`;272 public static istring mixinLuaRegisterFunction (cstring lua_state, cstring name, cstring lua_library_dot_name) 273 { 274 return cast(istring) (`mixin (mixinLuaRegisterFunctionAtLine ("` ~ lua_state ~ `", "` ~ name ~ `", "` ~ lua_library_dot_name ~ `", __LINE__));`); 276 275 } 277 276 … … 290 289 *******************************************************************************/ 291 290 292 public static char[] mixinLuaRegisterConstructorAtLine (char[] lua_state, char[] class_name, char[]lua_library_dot_name, int line_number)291 public static istring mixinLuaRegisterConstructorAtLine (cstring lua_state, cstring class_name, cstring lua_library_dot_name, int line_number) 293 292 { 294 293 int pos = rfind (lua_library_dot_name, '.'); 295 c har[]lua_library = pos < 0 ? "" : "\"" ~ lua_library_dot_name[0 .. pos] ~ "\"";296 c har[]lua_function = lua_library_dot_name[pos+1 .. $];297 c har[]wrapper = "lua_wrapper_" ~ mangleClass (class_name) ~ "_ctor_" ~ int2string (line_number);298 299 return ``294 cstring lua_library = pos < 0 ? "" : "\"" ~ lua_library_dot_name[0 .. pos] ~ "\""; 295 cstring lua_function = lua_library_dot_name[pos+1 .. $]; 296 cstring wrapper = "lua_wrapper_" ~ mangleClass (class_name) ~ "_ctor_" ~ int2string (line_number); 297 298 return cast(istring) (`` 300 299 ~ `{` 301 300 ~ ` extern (C) static int ` ~ wrapper ~ ` (lua_State *L)` … … 312 311 ~ ` auto f = new LuaForwardException (state, e, "` ~ class_name ~ `.this", __FILE__, __LINE__);` 313 312 ~ ` LuaForwardException.exceptions[cast (void*) f] = f;` 314 ~ ` return luaL_error (L, lua.utils.toStringz ("LFE=" ~ lua.mixins.int2string (cast ( ulong) cast(void*) f) ~ ";"));`313 ~ ` return luaL_error (L, lua.utils.toStringz ("LFE=" ~ lua.mixins.int2string (cast (size_t) cast(void*) f) ~ ";"));` 315 314 ~ ` }` 316 315 ~ ` }` 317 316 ~ ` ` ~ lua_state ~ `.registerFunction ("` ~ lua_function ~ `", cast (int function (lua_State *L)) &` ~ wrapper ~ `, ` ~ lua_library ~ `);` 318 ~ `}` ;317 ~ `}`); 319 318 } 320 319 … … 351 350 *******************************************************************************/ 352 351 353 public static char[] mixinLuaRegisterConstructor (char[] lua_state, char[] class_name, char[]lua_library_dot_name)354 { 355 return `mixin (mixinLuaRegisterConstructorAtLine ("` ~ lua_state ~ `", "` ~ class_name ~ `", "` ~ lua_library_dot_name ~ `", __LINE__));`;356 } 352 public static istring mixinLuaRegisterConstructor (cstring lua_state, cstring class_name, cstring lua_library_dot_name) 353 { 354 return cast(istring) (`mixin (mixinLuaRegisterConstructorAtLine ("` ~ lua_state ~ `", "` ~ class_name ~ `", "` ~ lua_library_dot_name ~ `", __LINE__));`); 355 } trunk/lua/state.d
r314 r324 1 /*******************************************************************************2 3 copyright: Copyright (c) 2008 Matthias Walter. All rights reserved4 5 authors:Matthias Walter, Andreas Hollandt, Clemens Hofreither1 /******************************************************************************* 2 3 copyright: Copyright (c) 2008 Matthias Walter. All rights reserved 4 5 authors: Matthias Walter, Andreas Hollandt, Clemens Hofreither 6 6 7 7 *******************************************************************************/ … … 9 9 module lua.state; 10 10 11 private import lua.common; 11 12 private import lua.lua, lua.lualib, lua.lauxlib; 12 13 private import lua.buffer, lua.mixins, lua.error, lua.data, lua.utils; … … 16 17 private import tango.stdc.stdio : fputs, stdout; 17 18 } 18 19 version (Phobos) 19 else 20 20 { 21 private import std.string : fputs, stdout;21 private import std.string : fputs, stdout; 22 22 } 23 23 … … 34 34 public enum LuaStandardLibraries : int 35 35 { 36 BASE = 0,37 TABLE = 1,38 STRING = 2,39 MATH = 4,40 OS = 8,41 IO = 16,42 DEBUG = 32,43 PACKAGE = 64,44 SAFE = BASE | TABLE | STRING | MATH | OS | PACKAGE,45 ALL = BASE | TABLE | STRING | MATH | OS | PACKAGE | IO | DEBUG36 BASE = 0, 37 TABLE = 1, 38 STRING = 2, 39 MATH = 4, 40 OS = 8, 41 IO = 16, 42 DEBUG = 32, 43 PACKAGE = 64, 44 SAFE = BASE | TABLE | STRING | MATH | OS | PACKAGE, 45 ALL = BASE | TABLE | STRING | MATH | OS | PACKAGE | IO | DEBUG 46 46 } 47 47 … … 54 54 public enum LuaType : int 55 55 { 56 NONE = LUA_TNONE,57 NIL = LUA_TNIL,58 NUMBER = LUA_TNUMBER,59 BOOL = LUA_TBOOLEAN,60 STRING = LUA_TSTRING,61 TABLE = LUA_TTABLE,62 FUNCTION = LUA_TFUNCTION,63 USERDATA = LUA_TUSERDATA,64 THREAD = LUA_TTHREAD,65 LIGHTUSERDATA = LUA_TLIGHTUSERDATA56 NONE = LUA_TNONE, 57 NIL = LUA_TNIL, 58 NUMBER = LUA_TNUMBER, 59 BOOL = LUA_TBOOLEAN, 60 STRING = LUA_TSTRING, 61 TABLE = LUA_TTABLE, 62 FUNCTION = LUA_TFUNCTION, 63 USERDATA = LUA_TUSERDATA, 64 THREAD = LUA_TTHREAD, 65 LIGHTUSERDATA = LUA_TLIGHTUSERDATA 66 66 } 67 67 … … 75 75 public enum LuaGarbageCollectorCommand : int 76 76 { 77 STOP = LUA_GCSTOP,78 RESTART = LUA_GCRESTART,79 COLLECT = LUA_GCCOLLECT,80 COUNT = LUA_GCCOUNT,81 STEP = LUA_GCSTEP,82 SET_PAUSE = LUA_GCSETPAUSE,83 SET_STEP_MULTIPLIER = LUA_GCSETSTEPMUL77 STOP = LUA_GCSTOP, 78 RESTART = LUA_GCRESTART, 79 COLLECT = LUA_GCCOLLECT, 80 COUNT = LUA_GCCOUNT, 81 STEP = LUA_GCSTEP, 82 SET_PAUSE = LUA_GCSETPAUSE, 83 SET_STEP_MULTIPLIER = LUA_GCSETSTEPMUL 84 84 } 85 85 … … 100 100 struct LuaRegistry 101 101 { 102 char[]name;103 LuaCFunction cfunction;104 int category;102 istring name; 103 LuaCFunction cfunction; 104 int category; 105 105 } 106 106 107 107 /// Delegate type for redirecting Lua's stdout to the D host program. 108 108 109 alias void delegate (c har[]output) LuaPrint;109 alias void delegate (cstring output) LuaPrint; 110 110 111 111 /******************************************************************************* … … 140 140 { 141 141 /// Wrapper Lua state 142 private lua_State* state_;143 144 /// Writer delegate, the Lua stdout145 private LuaPrint write_;146 147 /// Whether the LuaState was created as a sub-thread of another state by calling newThread().142 private lua_State* state_; 143 144 /// Writer delegate, the Lua stdout 145 private LuaPrint write_; 146 147 /// Whether the LuaState was created as a sub-thread of another state by calling newThread(). 148 148 private bool is_thread_state_; 149 149 150 /// Constant to pass instead of a number of expected results, if this number is unknown.151 public const int MULTIPLE_RETURN_VALUES = LUA_MULTRET;152 153 /*******************************************************************************150 /// Constant to pass instead of a number of expected results, if this number is unknown. 151 public const int MULTIPLE_RETURN_VALUES = LUA_MULTRET; 152 153 /******************************************************************************* 154 154 155 155 Constructs a state with an optional printer delegate. If the latter is … … 162 162 Replaces lua_open, lua_newstate and luaL_newstate. 163 163 164 *******************************************************************************/165 166 public this (LuaPrint write = null)167 {168 this.state_ = lua_open ();169 this.write_ = (write is null) ? &writeDefault : write;170 171 openLibraries (LuaStandardLibraries.BASE);172 173 LuaState.states[this.state_] = this;174 }175 176 /*******************************************************************************164 *******************************************************************************/ 165 166 public this (LuaPrint write = null) 167 { 168 this.state_ = lua_open (); 169 this.write_ = (write is null) ? &writeDefault : write; 170 171 openLibraries (LuaStandardLibraries.BASE); 172 173 LuaState.states[this.state_] = this; 174 } 175 176 /******************************************************************************* 177 177 178 178 Internal Constructor for Lua thread creation. … … 182 182 state = Lua state 183 183 184 *******************************************************************************/185 186 private this (LuaPrint write, lua_State* state)187 {184 *******************************************************************************/ 185 186 private this (LuaPrint write, lua_State* state) 187 { 188 188 this.write_ = write; 189 189 this.state_ = state; 190 190 191 191 LuaState.states[this.state_] = this; 192 }193 194 /*******************************************************************************192 } 193 194 /******************************************************************************* 195 195 196 196 Destructor … … 199 199 Replaces lua_close. 200 200 201 *******************************************************************************/202 203 private ~this ()204 {205 LuaState.states.remove (this.state_);206 207 // bugfix by Clemens:208 // states which represent child threads may not be destroyed201 *******************************************************************************/ 202 203 private ~this () 204 { 205 LuaState.states.remove (this.state_); 206 207 // bugfix by Clemens: 208 // states which represent child threads may not be destroyed 209 209 if (!is_thread_state_) 210 210 { 211 211 lua_close (this.state_); 212 212 } 213 }214 215 /*******************************************************************************213 } 214 215 /******************************************************************************* 216 216 217 217 Returns: … … 222 222 most functionality there should be a corresponding wrapper function. 223 223 224 *******************************************************************************/225 226 public lua_State* state ()227 {228 return this.state_;229 }230 231 /*******************************************************************************224 *******************************************************************************/ 225 226 public lua_State* state () 227 { 228 return this.state_; 229 } 230 231 /******************************************************************************* 232 232 233 233 Opens the specified standard libraries and adds their functionality to this state. … … 240 240 Replaces luaL_openlibs. 241 241 242 *******************************************************************************/243 244 public LuaState openLibraries (LuaStandardLibraries categories = LuaStandardLibraries.ALL)245 {242 *******************************************************************************/ 243 244 public LuaState openLibraries (LuaStandardLibraries categories = LuaStandardLibraries.ALL) 245 { 246 246 static LuaRegistry[] lualibs = [ 247 { "", &luaopen_base, LuaStandardLibraries.BASE },248 { LUA_TABLIBNAME, &luaopen_table, LuaStandardLibraries.TABLE },249 { LUA_IOLIBNAME, &luaopen_io, LuaStandardLibraries.IO },250 { LUA_OSLIBNAME, &luaopen_os, LuaStandardLibraries.OS },251 { LUA_STRLIBNAME, &luaopen_string, LuaStandardLibraries.STRING },252 { LUA_MATHLIBNAME, &luaopen_math, LuaStandardLibraries.MATH },253 { LUA_DBLIBNAME, &luaopen_debug, LuaStandardLibraries.DEBUG },254 { LUA_LOADLIBNAME, &luaopen_package, LuaStandardLibraries.PACKAGE }247 { "", &luaopen_base, LuaStandardLibraries.BASE }, 248 { LUA_TABLIBNAME, &luaopen_table, LuaStandardLibraries.TABLE }, 249 { LUA_IOLIBNAME, &luaopen_io, LuaStandardLibraries.IO }, 250 { LUA_OSLIBNAME, &luaopen_os, LuaStandardLibraries.OS }, 251 { LUA_STRLIBNAME, &luaopen_string, LuaStandardLibraries.STRING }, 252 { LUA_MATHLIBNAME, &luaopen_math, LuaStandardLibraries.MATH }, 253 { LUA_DBLIBNAME, &luaopen_debug, LuaStandardLibraries.DEBUG }, 254 { LUA_LOADLIBNAME, &luaopen_package, LuaStandardLibraries.PACKAGE } 255 255 ]; 256 256 … … 260 260 261 261 return this; 262 }263 264 /*******************************************************************************262 } 263 264 /******************************************************************************* 265 265 266 266 Opens all of the given libraries, which match the given categories bits, … … 271 271 categories = Choice to open. Default is -1, which opens all. 272 272 273 *******************************************************************************/274 275 public LuaState openLibraries (LuaRegistry[] libraries, int categories = -1)276 {273 *******************************************************************************/ 274 275 public LuaState openLibraries (LuaRegistry[] libraries, int categories = -1) 276 { 277 277 foreach (LuaRegistry reg; libraries) 278 {279 if (reg.category == 0 || (reg.category & categories) != 0)280 {281 openLibrary (reg.name, reg.cfunction);282 }283 }284 return this; 285 }286 287 /*******************************************************************************278 { 279 if (reg.category == 0 || (reg.category & categories) != 0) 280 { 281 openLibrary (reg.name, reg.cfunction); 282 } 283 } 284 return this; 285 } 286 287 /******************************************************************************* 288 288 289 289 Opens one library, adding its functionality to this state. The … … 295 295 registration_function = C-linkage function to call for registration 296 296 297 ******************************************************************************/298 299 public LuaState openLibrary (char[]library_name, LuaCFunction registration_function)300 {297 ******************************************************************************/ 298 299 public LuaState openLibrary (cstring library_name, LuaCFunction registration_function) 300 { 301 301 pushCFunction (registration_function); 302 302 pushString (library_name); … … 304 304 305 305 return this; 306 }307 308 /*******************************************************************************306 } 307 308 /******************************************************************************* 309 309 310 310 Internal method to pass the given data to the writer delegate. … … 313 313 data = Output to write. 314 314 315 ******************************************************************************/316 317 private void write (char[]data)318 {319 this.write_ (data);320 }321 322 /*******************************************************************************315 ******************************************************************************/ 316 317 private void write (cstring data) 318 { 319 this.write_ (data); 320 } 321 322 /******************************************************************************* 323 323 324 324 Standard writer function, if none is specified in Constructor. … … 327 327 output = Output to write. 328 328 329 ******************************************************************************/330 331 private void writeDefault (char[]output)332 {333 fputs (toStringz (output), stdout);334 }335 336 /*******************************************************************************329 ******************************************************************************/ 330 331 private void writeDefault (cstring output) 332 { 333 fputs (toStringz (output), stdout); 334 } 335 336 /******************************************************************************* 337 337 338 338 Calls the C function with only one element in its stack, a LightUserdata, … … 349 349 Replaces lua_cpcall. 350 350 351 ******************************************************************************/352 353 public LuaState call (bool protection, LuaCFunction cfunction, void* userdata)354 {355 pushCFunction (cfunction);356 pushLightUserdata (userdata);357 call (protection, 1, 0);358 359 return this; 360 }361 362 /*******************************************************************************351 ******************************************************************************/ 352 353 public LuaState call (bool protection, LuaCFunction cfunction, void* userdata) 354 { 355 pushCFunction (cfunction); 356 pushLightUserdata (userdata); 357 call (protection, 1, 0); 358 359 return this; 360 } 361 362 /******************************************************************************* 363 363 364 364 Method to call a Lua function, using the following protocol: … … 410 410 Replaces lua_call and lua_pcall. 411 411 412 ******************************************************************************/413 414 public LuaState call (bool protection, uint arguments = 0, int results = MULTIPLE_RETURN_VALUES)415 {412 ******************************************************************************/ 413 414 public LuaState call (bool protection, uint arguments = 0, int results = MULTIPLE_RETURN_VALUES) 415 { 416 416 if (protection) 417 417 { 418 int errors = lua_pcall (this.state_, arguments, results, 0);419 if (errors != 0)420 {421 char[]error = popString ();418 int errors = lua_pcall (this.state_, arguments, results, 0); 419 if (errors != 0) 420 { 421 istring error = popString (); 422 422 423 423 // parse the error string to extract the address of the exception 424 char[]pointer_string = null;424 istring pointer_string = null; 425 425 for (int start = error.length-6; start >= 0; start--) 426 426 { 427 if (error[start .. start + 4] == "LFE=")428 {427 if (error[start .. start + 4] == "LFE=") 428 { 429 429 for (int end = start+5; end < error.length; end++) 430 430 { 431 if (error[end] == ';')432 {433 pointer_string = error[start+4 .. end];434 break;435 }431 if (error[end] == ';') 432 { 433 pointer_string = error[start+4 .. end]; 434 break; 435 } 436 436 } 437 437 break; 438 }438 } 439 439 } 440 440 441 if (pointer_string ! =null)441 if (pointer_string !is null) 442 442 { 443 void* p = cast (void*) string2int !(ulong) (pointer_string);444 LuaForwardException e = LuaForwardException.exceptions[p];445 LuaForwardException.exceptions.remove (p);446 e.forward ("LuaState.call");447 throw e;443 void* p = cast (void*) string2int !(size_t) (pointer_string); 444 LuaForwardException e = LuaForwardException.exceptions[p]; 445 LuaForwardException.exceptions.remove (p); 446 e.forward ("LuaState.call"); 447 throw e; 448 448 } 449 449 else 450 450 { 451 throw new LuaCodeException (error);451 throw new LuaCodeException (error, __FILE__, __LINE__); 452 452 } 453 }453 } 454 454 } 455 455 else 456 456 { 457 lua_call (this.state_, arguments, results);457 lua_call (this.state_, arguments, results); 458 458 } 459 459 460 460 return this; 461 }462 463 /*******************************************************************************461 } 462 463 /******************************************************************************* 464 464 465 465 Loads and runs the given string. … … 475 475 Replaces luaL_dostring. 476 476 477 ******************************************************************************/478 479 public LuaState doString (bool protection, char[] code, uint arguments = 0, int results = MULTIPLE_RETURN_VALUES, char[]chunk_name = null)480 {481 load (code, chunk_name);482 483 return call (protection, arguments, results);484 }485 486 /// ditto487 488 public LuaState doString (bool protection, char[] code, char[]chunk_name, uint arguments = 0, int results = MULTIPLE_RETURN_VALUES)489 {490 return doString (protection, code, arguments, results, chunk_name);491 }492 493 /*******************************************************************************477 ******************************************************************************/ 478 479 public LuaState doString (bool protection, cstring code, uint arguments = 0, int results = MULTIPLE_RETURN_VALUES, cstring chunk_name = null) 480 { 481 load (code, chunk_name); 482 483 return call (protection, arguments, results); 484 } 485 486 /// ditto 487 488 public LuaState doString (bool protection, cstring code, cstring chunk_name, uint arguments = 0, int results = MULTIPLE_RETURN_VALUES) 489 { 490 return doString (protection, code, arguments, results, chunk_name); 491 } 492 493 /******************************************************************************* 494 494 495 495 Loads and runs the given file. … … 504 504 Replaces luaL_dofile. 505 505 506 ******************************************************************************/507 508 public LuaState doFile (bool protection, char[]filename, uint arguments = 0, int results = MULTIPLE_RETURN_VALUES)509 {506 ******************************************************************************/ 507 508 public LuaState doFile (bool protection, cstring filename, uint arguments = 0, int results = MULTIPLE_RETURN_VALUES) 509 { 510 510 loadFile (filename); 511 511 512 512 return call (protection, arguments, results); 513 }514 515 /*******************************************************************************513 } 514 515 /******************************************************************************* 516 516 517 517 If the object at the specified index has a metatable with the specified … … 524 524 Replaces luaL_callmeta. 525 525 526 ******************************************************************************/527 528 public bool callMeta (int index, char[]field)529 {530 return luaL_callmeta (this.state_, index, toStringz (field)) != 0;531 }532 533 /*******************************************************************************526 ******************************************************************************/ 527 528 public bool callMeta (int index, cstring field) 529 { 530 return luaL_callmeta (this.state_, index, toStringz (field)) != 0; 531 } 532 533 /******************************************************************************* 534 534 535 535 If the function argument is number, returns this number cast to an int. … … 540 540 Replaces luaL_optint, luaL_optlong and luaL_optinteger. 541 541 542 ******************************************************************************/543 544 public LuaInteger optInteger (uint argument, LuaInteger default_value)545 {546 return luaL_optint (this.state_, argument, default_value);547 }548 549 /*******************************************************************************542 ******************************************************************************/ 543 544 public LuaInteger optInteger (uint argument, LuaInteger default_value) 545 { 546 return luaL_optint (this.state_, argument, default_value); 547 } 548 549 /******************************************************************************* 550 550 551 551 If the function argument is number, returns this number. … … 556 556 Replaces luaL_optnumber. 557 557 558 ******************************************************************************/558 ******************************************************************************/ 559 559 560 560 public LuaNumber optNumber (uint argument, LuaNumber default_value) … … 563 563 } 564 564 565 /*******************************************************************************565 /******************************************************************************* 566 566 567 567 If the function argument is a string, returns this string. If this … … 572 572 Replaces luaL_optlstring and luaL_optstring. 573 573 574 ******************************************************************************/575 576 public char[] optString (uint argument, char[]default_value)574 ******************************************************************************/ 575 576 public istring optString (uint argument, cstring default_value) 577 577 { 578 578 size_t len; 579 char* ptr = luaL_optlstring (this.state_, argument, toStringz (default_value), &len);579 ichar* ptr = luaL_optlstring (this.state_, argument, toStringz (default_value), &len); 580 580 581 581 return ptr[0 .. len]; 582 582 } 583 583 584 /*******************************************************************************584 /******************************************************************************* 585 585 586 586 Checks whether the function argument is a string and searches for this … … 599 599 Replaces luaL_checkoption. 600 600 601 ******************************************************************************/602 603 public int checkOption (uint argument, char[][] value_list, char[]default_value = null)604 {605 char[]name = (default_value !is null) ? optString (argument, default_value) : checkString (argument);606 607 foreach (i, s; value_list)608 {609 if (s == name)610 return i;611 }612 613 throw new LuaCodeException ("Invalid option: '" ~ name ~ "'");614 }615 616 /*******************************************************************************601 ******************************************************************************/ 602 603 public int checkOption (uint argument, char[][] value_list, cstring default_value = null) 604 { 605 istring name = (default_value !is null) ? optString (argument, default_value) : checkString (argument); 606 607 foreach (i, s; value_list) 608 { 609 if (s == name) 610 return i; 611 } 612 613 throw new LuaCodeException ("Invalid option: '" ~ name ~ "'", __FILE__, __LINE__); 614 } 615 616 /******************************************************************************* 617 617 618 618 Raises a Lua error specified by the message argument. If the latter is … … 626 626 Replaces lua_error and luaL_error. 627 627 628 ******************************************************************************/629 630 public int raiseError (char[]message = null)631 {628 ******************************************************************************/ 629 630 public int raiseError (cstring message = null) 631 { 632 632 if (message !is null) 633 633 { 634 pushString (message);634 pushString (message); 635 635 } 636 636 return lua_error (this.state_); 637 }638 639 /*******************************************************************************637 } 638 639 /******************************************************************************* 640 640 641 641 Raises a Lua error due to a bad function argument, where the function … … 653 653 Replaces luaL_argerror. 654 654 655 ******************************************************************************/656 657 public int argumentError (uint argument, char[]message)658 {659 return luaL_argerror (this.state_, argument, toStringz (message));660 }661 662 /*******************************************************************************655 ******************************************************************************/ 656 657 public int argumentError (uint argument, cstring message) 658 { 659 return luaL_argerror (this.state_, argument, toStringz (message)); 660 } 661 662 /******************************************************************************* 663 663 664 664 Raises an error with a message like the following: 665 665 666 <loc>: bad argument <argument> to '<func>' (<typename> expected, got <rt>)666 <loc>: bad argument <argument> to '<func>' (<typename> expected, got <rt>) 667 667 668 668 where loc is produced by LuaState.where, func is the name of the current … … 678 678 Replaces luaL_typerror. 679 679 680 ******************************************************************************/681 682 public int typeError (uint argument, char[]typename)683 {684 return luaL_typerror (this.state_, argument, toStringz (typename));685 }686 687 /*******************************************************************************680 ******************************************************************************/ 681 682 public int typeError (uint argument, cstring typename) 683 { 684 return luaL_typerror (this.state_, argument, toStringz (typename)); 685 } 686 687 /******************************************************************************* 688 688 689 689 Constructs a LuaBuffer and attaches it to this state. … … 692 692 The constructed Lua buffer. 693 693 694 *******************************************************************************/695 696 public LuaBuffer createBuffer ()697 {698 return new LuaBuffer (this);699 }700 701 /*******************************************************************************694 *******************************************************************************/ 695 696 public LuaBuffer createBuffer () 697 { 698 return new LuaBuffer (this); 699 } 700 701 /******************************************************************************* 702 702 703 703 Dumps a function as a binary chunk. Receives a Lua function on the top … … 717 717 Replaces lua_dump. 718 718 719 *******************************************************************************/720 721 public int dump (int delegate (char[]data) dg)722 {723 return lua_dump (this.state_, &writer, &dg);724 }725 726 /*******************************************************************************719 *******************************************************************************/ 720 721 public int dump (int delegate (cstring data) dg) 722 { 723 return lua_dump (this.state_, &writer, &dg); 724 } 725 726 /******************************************************************************* 727 727 728 728 Dumps a function as a binary chunk. It works exactly like the above dump … … 730 730 complete result in one character array. 731 731 732 *******************************************************************************/733 734 public char[]dump ()735 {736 char[]result = [];737 738 int dumper (c har[]data)732 *******************************************************************************/ 733 734 public mstring dump () 735 { 736 mstring result = []; 737 738 int dumper (cstring data) 739 739 { 740 result ~= data;741 return 0;740 result ~= data; 741 return 0; 742 742 } 743 743 … … 745 745 746 746 return result; 747 }748 749 /*******************************************************************************750 751 Loads a Lua chunk. If there are no errors, load pushes the compiled752 chunk as a Lua function on top of the stack. Otherwise, it raises753 either a LuaCodeException, if the code cannot be compiled or a754 LuaFatalException on other errors.747 } 748 749 /******************************************************************************* 750 751 Loads a Lua chunk. If there are no errors, load pushes the compiled 752 chunk as a Lua function on top of the stack. Otherwise, it raises 753 either a LuaCodeException, if the code cannot be compiled or a 754 LuaFatalException on other errors. 755 755 756 756 This function only loads a chunk; it does not run it. It automatically … … 767 767 auto L = new LuaState; 768 768 L.doString (true, ` 769 function fac (n)770 if n == 0 then 771 return 1;772 else773 return n * fac(n-1);774 end775 end776 `);777 778 L.getGlobal ("fac"); // put function onto stack779 char[] data = L.dump (); // dump it780 L.pop (); // pop it769 function fac (n) 770 if n == 0 then 771 return 1; 772 else 773 return n * fac(n-1); 774 end 775 end 776 `); 777 778 L.getGlobal ("fac"); // put function onto stack 779 char[] data = L.dump (); // dump it 780 L.pop (); // pop it 781 781 L.load (data, "foo"); // reload function as chunk 'foo' 782 782 L.setGlobal ("fac2"); // save it as the global function fac2 … … 790 790 Replaces lua_load. 791 791 792 *******************************************************************************/793 794 public LuaState load (char[] delegate () dg, char[]chunk_name = null)795 {792 *******************************************************************************/ 793 794 public LuaState load (cstring delegate () dg, cstring chunk_name = null) 795 { 796 796 int errors = lua_load (this.state_, &reader, &dg, toStringz (chunk_name)); 797 797 798 798 if (errors != 0) 799 799 { 800 if (errors == LUA_ERRSYNTAX)801 throw new LuaCodeException ("Syntax error during pre-compilation: " ~ popString);802 else803 throw new LuaFatalException (errors);800 if (errors == LUA_ERRSYNTAX) 801 throw new LuaCodeException ("Syntax error during pre-compilation: " ~ popString, __FILE__, __LINE__); 802 else 803 throw new LuaFatalException (errors, __FILE__, __LINE__); 804 804 } 805 805 806 806 return this; 807 }808 809 /*******************************************************************************807 } 808 809 /******************************************************************************* 810 810 811 811 Loads a Lua chunk. It works exactly like the above dump method, but … … 820 820 Replaces luaL_loadstring and luaL_loadbuffer. 821 821 822 *******************************************************************************/823 824 public LuaState load (char[] data, char[]chunk_name = null)825 {822 *******************************************************************************/ 823 824 public LuaState load (cstring data, cstring chunk_name = null) 825 { 826 826 bool done = false; 827 827 828 828 load ( 829 {829 { 830 830 if (!done) 831 831 { 832 done = true;832 done = true; 833 833 } 834 834 else 835 835 { 836 data = "";836 data = ""; 837 837 } 838 838 return data; 839 },840 chunk_name );841 842 return this; 843 }844 845 /*******************************************************************************839 }, 840 chunk_name ); 841 842 return this; 843 } 844 845 /******************************************************************************* 846 846 847 847 Loads a file as a Lua chunk. This function uses the load method to load … … 860 860 Throws: 861 861 LuaIOException, if an error occured while reading the file. 862 LuaCodeException, if an error occured during pre-compilation. 862 863 863 864 Remarks: 864 865 Replaces luaL_loadfile. 865 866 866 *******************************************************************************/ 867 868 public LuaState loadFile (char[] filename) 869 { 870 if (luaL_loadfile (this.state_, toStringz (filename)) != 0) 871 throw new LuaIOException ("I/O error: loadFile failed to read \"" ~ filename ~ "\"!"); 872 873 return this; 874 } 875 876 /******************************************************************************* 867 *******************************************************************************/ 868 869 public LuaState loadFile (cstring filename) 870 { 871 int errors = luaL_loadfile (this.state_, toStringz (filename)); 872 if (errors != 0) 873 { 874 if (errors == LUA_ERRSYNTAX) 875 throw new LuaCodeException ("Syntax error during pre-compilation: " ~ popString, __FILE__, __LINE__); 876 else if (errors == LUA_ERRFILE) 877 throw new LuaIOException ("I/O error: loadFile failed to read \"" ~ filename ~ "\"!", __FILE__, __LINE__); 878 else 879 throw new LuaFatalException (errors, __FILE__, __LINE__); 880 } 881 return this; 882 } 883 884 /******************************************************************************* 877 885 878 886 Controls the garbage collector. … … 881 889 command parameter: 882 890 883 * STOP: Stops the garbage collector.884 * RESTART: Restarts the garbage collector.885 * COLLECT: Performs a full garbage-collection cycle.886 * COUNT: Returns the current amount of memory (in Kbytes) in use by Lua.887 * STEP: Performs an incremental step of garbage collection. The step888 "size" is controlled by data (larger values mean more steps) in a889 non-specified way. If you want to control the step size you must890 experimentally tune the value of data. The function returns 1 if the891 step finished a garbage-collection cycle.892 * SET_PAUSE: Sets data/100 as the new value for the pause of the893 collector. The function returns the previous value of the pause.894 * SET_STEP_MULTIPLIER: Sets data/100 as the new value for the step895 multiplier of the collector. The function returns the previous896 value of the step multiplier.891 * STOP: Stops the garbage collector. 892 * RESTART: Restarts the garbage collector. 893 * COLLECT: Performs a full garbage-collection cycle. 894 * COUNT: Returns the current amount of memory (in Kbytes) in use by Lua. 895 * STEP: Performs an incremental step of garbage collection. The step 896 "size" is controlled by data (larger values mean more steps) in a 897 non-specified way. If you want to control the step size you must 898 experimentally tune the value of data. The function returns 1 if the 899 step finished a garbage-collection cycle. 900 * SET_PAUSE: Sets data/100 as the new value for the pause of the 901 collector. The function returns the previous value of the pause. 902 * SET_STEP_MULTIPLIER: Sets data/100 as the new value for the step 903 multiplier of the collector. The function returns the previous 904 value of the step multiplier. 897 905 898 906 Params: … … 903 911 Replaces lua_gc. 904 912 905 *******************************************************************************/906 907 public int gc (LuaGarbageCollectorCommand command, int data = 0)908 {909 return lua_gc (this.state_, command, data);910 }911 912 /*******************************************************************************913 *******************************************************************************/ 914 915 public int gc (LuaGarbageCollectorCommand command, int data = 0) 916 { 917 return lua_gc (this.state_, command, data); 918 } 919 920 /******************************************************************************* 913 921 914 922 Returns true if the two values in acceptable indices a and b are … … 923 931 Replaces lua_rawequal 924 932 925 *******************************************************************************/926 927 public bool rawEqual (int a, int b)928 {929 return lua_rawequal (this.state_, a, b) != 0;930 }931 932 /*******************************************************************************933 *******************************************************************************/ 934 935 public bool rawEqual (int a, int b) 936 { 937 return lua_rawequal (this.state_, a, b) != 0; 938 } 939 940 /******************************************************************************* 933 941 934 942 Similar to getTable, but does a raw access (i.e., without metamethods): … … 947 955 Replaces lua_rawget 948 956 949 *******************************************************************************/950 951 public LuaState rawGet (int index)952 {957 *******************************************************************************/ 958 959 public LuaState rawGet (int index) 960 { 953 961 lua_rawget (this.state_, index); 954 962 955 963 return this; 956 }957 958 /*******************************************************************************964 } 965 966 /******************************************************************************* 959 967 960 968 Pushes onto the stack the value t[n], where t is the value at the given … … 968 976 Replaces lua_rawgeti 969 977 970 *******************************************************************************/971 972 public LuaState rawGetIndex (int index, int n)973 {978 *******************************************************************************/ 979 980 public LuaState rawGetIndex (int index, int n) 981 { 974 982 lua_rawgeti (this.state_, index, n); 975 983 976 984 return this; 977 }978 979 /*******************************************************************************985 } 986 987 /******************************************************************************* 980 988 981 989 Similar to setTable, but does a raw assignment (i.e., without metamethods): … … 994 1002 Replaces lua_rawset 995 1003 996 *******************************************************************************/997 998 public LuaState rawSet (int index)999 {1004 *******************************************************************************/ 1005 1006 public LuaState rawSet (int index) 1007 { 1000 1008 lua_rawset (this.state_, index); 1001 1009 1002 1010 return this; 1003 }1004 1005 /*******************************************************************************1011 } 1012 1013 /******************************************************************************* 1006 1014 1007 1015 Does the equivalent of t[n] = v, where t is the value at the given … … 1018 1026 Replaces lua_rawseti 1019 1027 1020 *******************************************************************************/1021 1022 public LuaState rawSetIndex (int index, int n)1023 {1028 *******************************************************************************/ 1029 1030 public LuaState rawSetIndex (int index, int n) 1031 { 1024 1032 lua_rawseti (this.state_, index, n); 1025 1033 1026 1034 return this; 1027 }1028 1029 /*******************************************************************************1035 } 1036 1037 /******************************************************************************* 1030 1038 1031 1039 Checks whether condition is true. If not, calls argumentError … … 1039 1047 Replaces luaL_argcheck. 1040 1048 1041 *******************************************************************************/1042 1043 public LuaState checkArgument (bool condition, uint argument, char[]message)1044 {1049 *******************************************************************************/ 1050 1051 public LuaState checkArgument (bool condition, uint argument, cstring message) 1052 { 1045 1053 luaL_argcheck (this.state_, condition, argument, toStringz (message)); 1046 1054 1047 1055 return this; 1048 }1049 1050 public alias checkArgument argCheck;1051 1052 /*******************************************************************************1056 } 1057 1058 public alias checkArgument argCheck; 1059 1060 /******************************************************************************* 1053 1061 1054 1062 Checks whether the function has an argument of any type (including nil) … … 1061 1069 Replaces luaL_checkany. 1062 1070 1063 *******************************************************************************/1064 1065 public LuaState checkAny (int argument)1066 {1071 *******************************************************************************/ 1072 1073 public LuaState checkAny (int argument) 1074 { 1067 1075 luaL_checkany (this.state_, argument); 1068 1076 1069 1077 return this; 1070 }1071 1072 /*******************************************************************************1078 } 1079 1080 /******************************************************************************* 1073 1081 1074 1082 Returns true if the given acceptable index is not valid (that is, it … … 1081 1089 Replaces lua_isnone. 1082 1090 1083 *******************************************************************************/1084 1085 public bool isNone (int index)1086 {1087 return lua_isnone (this.state_, index) != 0;1088 }1089 1090 /*******************************************************************************1091 *******************************************************************************/ 1092 1093 public bool isNone (int index) 1094 { 1095 return lua_isnone (this.state_, index) != 0; 1096 } 1097 1098 /******************************************************************************* 1091 1099 1092 1100 Returns true if the value at the given acceptable index is nil, and … … 1099 1107 Replaces lua_isnil. 1100 1108 1101 *******************************************************************************/1102 1103 public bool isNil (int index)1104 {1105 return lua_isnil (this.state_, index) != 0;1106 }1107 1108 /*******************************************************************************1109 *******************************************************************************/ 1110 1111 public bool isNil (int index) 1112 { 1113 return lua_isnil (this.state_, index) != 0; 1114 } 1115 1116 /******************************************************************************* 1109 1117 1110 1118 Returns true if the given acceptable index is not valid (that is, it … … 1118 1126 Replaces lua_isnoneornil. 1119 1127 1120 *******************************************************************************/1121 1122 public bool isNoneOrNil (int index)1123 {1124 return lua_isnoneornil (this.state_, index) != 0;1125 }1126 1127 /*******************************************************************************1128 *******************************************************************************/ 1129 1130 public bool isNoneOrNil (int index) 1131 { 1132 return lua_isnoneornil (this.state_, index) != 0; 1133 } 1134 1135 /******************************************************************************* 1128 1136 1129 1137 Returns the "length" of the value at the given acceptable index: … … 1140 1148 Replaces lua_objlen. 1141 1149 1142 *******************************************************************************/1143 1144 public size_t objectLength (int index)1145 {1146 return lua_objlen (this.state_, index);1147 }1148 1149 /*******************************************************************************1150 *******************************************************************************/ 1151 1152 public size_t objectLength (int index) 1153 { 1154 return lua_objlen (this.state_, index); 1155 } 1156 1157 /******************************************************************************* 1150 1158 1151 1159 Pushes a nil value onto the stack. … … 1154 1162 Replaces lua_pushnil. 1155 1163 1156 *******************************************************************************/1157 1158 public LuaState pushNil ()1159 {1164 *******************************************************************************/ 1165 1166 public LuaState pushNil () 1167 { 1160 1168 lua_pushnil (this.state_); 1161 1169 1162 1170 return this; 1163 }1164 1165 /*******************************************************************************1171 } 1172 1173 /******************************************************************************* 1166 1174 1167 1175 Pushes a copy of the element at the given valid index onto the stack. … … 1170 1178 Replaces lua_pushvalue. 1171 1179 1172 *******************************************************************************/1173 1174 public LuaState pushValue (int index)1175 {1180 *******************************************************************************/ 1181 1182 public LuaState pushValue (int index) 1183 { 1176 1184 lua_pushvalue (this.state_, index); 1177 1185 1178 1186 return this; 1179 }1180 1181 /*******************************************************************************1187 } 1188 1189 /******************************************************************************* 1182 1190 1183 1191 Accepts any acceptable index, or 0, and sets the stack top to this index. … … 1191 1199 Replaces lua_settop. 1192 1200 1193 *******************************************************************************/1194 1195 public LuaState setTop (int index)1196 {1201 *******************************************************************************/ 1202 1203 public LuaState setTop (int index) 1204 { 1197 1205 lua_settop (this.state_, index); 1198 1206 1199 1207 return this; 1200 }1201 1202 /*******************************************************************************1208 } 1209 1210 /******************************************************************************* 1203 1211 1204 1212 Returns the index of the top element in the stack. Because indices … … 1209 1217 Replaces lua_gettop. 1210 1218 1211 *******************************************************************************/1212 1213 public int getTop ()1214 {1215 return lua_gettop (this.state_);1216 }1217 1218 public alias getTop top;1219 1220 1221 /*******************************************************************************1219 *******************************************************************************/ 1220 1221 public int getTop () 1222 { 1223 return lua_gettop (this.state_); 1224 } 1225 1226 public alias getTop top; 1227 1228 1229 /******************************************************************************* 1222 1230 1223 1231 Grows the stack size to top + needed elements, raising an error if the … … 1232 1240 Replaces lua_checkstack and luaL_checkstack. 1233 1241 1234 *******************************************************************************/1235 1236 public LuaState checkStack (uint needed, char[]message = "")1237 {1238 if (lua_checkstack (this.state_, needed) == false)1239 throw new LuaFatalException ("Fatal error: checkStack failed to grow stack! " ~ message);1240 1241 return this;1242 }1243 1244 /*******************************************************************************1242 *******************************************************************************/ 1243 1244 public LuaState checkStack (uint needed, cstring message = "") 1245 { 1246 if (lua_checkstack (this.state_, needed) == false) 1247 throw new LuaFatalException ("Fatal error: checkStack failed to grow stack! " ~ message, __FILE__, __LINE__); 1248 1249 return this; 1250 } 1251 1252 /******************************************************************************* 1245 1253 1246 1254 Pops amount elements from the stack. … … 1252 1260 Replaces lua_pop. 1253 1261 1254 *******************************************************************************/1255 1256 public LuaState pop (uint amount = 1)1257 {1262 *******************************************************************************/ 1263 1264 public LuaState pop (uint amount = 1) 1265 { 1258 1266 lua_pop (this.state_, amount); 1259 1267 1260 1268 return this; 1261 }1262 1263 /*******************************************************************************1269 } 1270 1271 /******************************************************************************* 1264 1272 1265 1273 Removes the element at the given valid index, shifting down the elements … … 1273 1281 Replaces lua_remove. 1274 1282 1275 *******************************************************************************/1276 1277 public LuaState remove (int index)1278 {1283 *******************************************************************************/ 1284 1285 public LuaState remove (int index) 1286 { 1279 1287 lua_remove (this.state_, index); 1280 1288 1281 1289 return this; 1282 }1283 1284 /*******************************************************************************1290 } 1291 1292 /******************************************************************************* 1285 1293 1286 1294 Moves the top element into the given position (and pops it), without … … 1293 1301 Replaces lua_replace. 1294 1302 1295 *******************************************************************************/1296 1297 public LuaState replace (int index)1298 {1303 *******************************************************************************/ 1304 1305 public LuaState replace (int index) 1306 { 1299 1307 lua_replace (this.state_, index); 1300 1308 1301 1309 return this; 1302 }1303 1304 /*******************************************************************************1310 } 1311 1312 /******************************************************************************* 1305 1313 1306 1314 Checks whether the function argument has the type. … … 1313 1321 Replaces luaL_checktype. 1314 1322 1315 *******************************************************************************/1316 1317 public LuaState checkType (int argument, LuaType type)1318 {1323 *******************************************************************************/ 1324 1325 public LuaState checkType (int argument, LuaType type) 1326 { 1319 1327 luaL_checktype (this.state_, argument, type); 1320 1328 1321 1329 return this; 1322 }1323 1324 /*******************************************************************************1330 } 1331 1332 /******************************************************************************* 1325 1333 1326 1334 Returns the type of the value in the given acceptable index, or … … 1334 1342 Replaces lua_type. 1335 1343 1336 *******************************************************************************/1337 1338 public LuaType type (int index)1339 {1340 return cast (LuaType) lua_type (this.state_, index);1341 }1342 1343 /*******************************************************************************1344 *******************************************************************************/ 1345 1346 public LuaType type (int index) 1347 { 1348 return cast (LuaType) lua_type (this.state_, index); 1349 } 1350 1351 /******************************************************************************* 1344 1352 1345 1353 Returns the name of the type encoded by the value type, which must be … … 1354 1362 of type 1355 1363 1356 *******************************************************************************/1357 1358 public char[]typeName (LuaType type)1359 {1360 char* string = lua_typename (this.state_, type);1364 *******************************************************************************/ 1365 1366 public istring typeName (LuaType type) 1367 { 1368 ichar* string = lua_typename (this.state_, type); 1361 1369 return string [0 .. strlenz (string)]; 1362 }1363 1364 /*******************************************************************************1370 } 1371 1372 /******************************************************************************* 1365 1373 1366 1374 Checks whether the function argument argument is a number and returns … … 1373 1381 Replaces luaL_checkinteger, luaL_checkint and luaL_checklong. 1374 1382 1375 *******************************************************************************/1376 1377 public LuaInteger checkInteger (int argument)1378 {1379 return luaL_checkinteger (this.state_, argument);1380 }1381 1382 /*******************************************************************************1383 *******************************************************************************/ 1384 1385 public LuaInteger checkInteger (int argument) 1386 { 1387 return luaL_checkinteger (this.state_, argument); 1388 } 1389 1390 /******************************************************************************* 1383 1391 1384 1392 Converts the Lua value at the given acceptable index to the signed … … 1394 1402 Replaces lua_tointeger. 1395 1403 1396 *******************************************************************************/1397 1398 public LuaInteger toInteger (int index)1399 {1400 return lua_tointeger (this.state_, index);1401 }1402 1403 /*******************************************************************************1404 *******************************************************************************/ 1405 1406 public LuaInteger toInteger (int index) 1407 { 1408 return lua_tointeger (this.state_, index); 1409 } 1410 1411 /******************************************************************************* 1404 1412 1405 1413 Pushes a number with the specified value onto the stack. … … 1411 1419 Replaces lua_pushinteger. 1412 1420 1413 *******************************************************************************/1414 1415 public LuaState pushInteger (LuaInteger value)1416 {1421 *******************************************************************************/ 1422 1423 public LuaState pushInteger (LuaInteger value) 1424 { 1417 1425 lua_pushinteger (this.state_, value); 1418 1426 1419 1427 return this; 1420 }1421 1422 /*******************************************************************************1428 } 1429 1430 /******************************************************************************* 1423 1431 1424 1432 Checks whether the function argument is a number and returns this number. … … 1430 1438 Replaces luaL_checknumber. 1431 1439 1432 *******************************************************************************/1433 1434 public LuaNumber checkNumber (int index)1435 {1436 return luaL_checknumber (this.state_, index);1437 }1438 1439 /*******************************************************************************1440 *******************************************************************************/ 1441 1442 public LuaNumber checkNumber (int index) 1443 { 1444 return luaL_checknumber (this.state_, index); 1445 } 1446 1447 /******************************************************************************* 1440 1448 1441 1449 Converts the Lua value at the given acceptable index to the type LuaNumber. … … 1449 1457 Replaces lua_tonumber. 1450 1458 1451 *******************************************************************************/1452 1453 public LuaNumber toNumber (int index)1454 {1455 return lua_tonumber (this.state_, index);1456 }1457 1458 /*******************************************************************************1459 *******************************************************************************/ 1460 1461 public LuaNumber toNumber (int index) 1462 { 1463 return lua_tonumber (this.state_, index); 1464 } 1465 1466 /******************************************************************************* 1459 1467 1460 1468 Pushes a number with the specified value onto the stack. … … 1466 1474 Replaces lua_pushnumber. 1467 1475 1468 *******************************************************************************/1469 1470 public LuaState pushNumber (LuaNumber value)1471 {1476 *******************************************************************************/ 1477 1478 public LuaState pushNumber (LuaNumber value) 1479 { 1472 1480 lua_pushnumber (this.state_, value); 1473 1481 1474 1482 return this; 1475 }1476 1477 /*******************************************************************************1483 } 1484 1485 /******************************************************************************* 1478 1486 1479 1487 Returns true if the value at the given acceptable index is a number or … … 1486 1494 Replaces lua_isnumber. 1487 1495 1488 *******************************************************************************/1489 1490 public bool isNumber (int index)1491 {1492 return lua_isnumber (this.state_, index) != 0;1493 }1494 1495 /*******************************************************************************1496 *******************************************************************************/ 1497 1498 public bool isNumber (int index) 1499 { 1500 return lua_isnumber (this.state_, index) != 0; 1501 } 1502 1503 /******************************************************************************* 1496 1504 1497 1505 Returns true if the value at the given acceptable index has type … … 1504 1512 Replaces lua_isboolean. 1505 1513 1506 *******************************************************************************/1507 1508 public bool isBool (int index)1509 {1510 return lua_isboolean (this.state_, index) != 0;1511 }1512 1513 /*******************************************************************************1514 *******************************************************************************/ 1515 1516 public bool isBool (int index) 1517 { 1518 return lua_isboolean (this.state_, index) != 0; 1519 } 1520 1521 /******************************************************************************* 1514 1522 1515 1523 Pushes a boolean with the specified value onto the stack. … … 1521 1529 Replaces lua_pushboolean. 1522 1530 1523 *******************************************************************************/1524 1525 public LuaState pushBool (bool value)1526 {1531 *******************************************************************************/ 1532 1533 public LuaState pushBool (bool value) 1534 { 1527 1535 lua_pushboolean (this.state_, value); 1528 1536 1529 1537 return this; 1530 }1531 1532 /*******************************************************************************1538 } 1539 1540 /******************************************************************************* 1533 1541 1534 1542 Converts the Lua value at the given acceptable index to a bool value. … … 1544 1552 Replaces lua_toboolean. 1545 1553 1546 *******************************************************************************/1547 1548 public bool toBool (int index)1549 {1550 return lua_toboolean (this.state_, index) != 0;1551 }1552 1553 /*******************************************************************************1554 *******************************************************************************/ 1555 1556 public bool toBool (int index) 1557 { 1558 return lua_toboolean (this.state_, index) != 0; 1559 } 1560 1561 /******************************************************************************* 1554 1562 1555 1563 Checks whether the function argument is a string and returns this string. … … 1561 1569 Replaces luaL_checkstring. 1562 1570 1563 *******************************************************************************/1564 1565 public char[]checkString (int index)1566 {1567 uint len;1568 char* str = luaL_checklstring (this.state_, index, &len);1569 return str[0 .. len];1570 }1571 1572 /*******************************************************************************1571 *******************************************************************************/ 1572 1573 public istring checkString (int index) 1574 { 1575 uint len; 1576 ichar* str = luaL_checklstring (this.state_, index, &len); 1577 return str[0 .. len]; 1578 } 1579 1580 /******************************************************************************* 1573 1581 1574 1582 Returns true if the value at the given acceptable index is a string or … … 1581 1589 Replaces lua_isstring. 1582 1590 1583 *******************************************************************************/1584 1585 public bool isString (int index)1586 {1587 return lua_isstring (this.state_, index) != 0;1588 }1589 1590 /*******************************************************************************1591 *******************************************************************************/ 1592 1593 public bool isString (int index) 1594 { 1595 return lua_isstring (this.state_, index) != 0; 1596 } 1597 1598 /******************************************************************************* 1591 1599 1592 1600 Pushes the string onto the stack. Lua makes (or reuses) an internal copy … … 1600 1608 Replaces lua_pushstring, lua_pushliteral and lua_pushlstring. 1601 1609 1602 *******************************************************************************/1603 1604 public LuaState pushString (char[]string)1605 {1606 lua_pushlstring (this.state_, string.ptr, string.length);1607 1608 return this;1609 }1610 1611 /*******************************************************************************1610 *******************************************************************************/ 1611 1612 public LuaState pushString (cstring string) 1613 { 1614 lua_pushlstring (this.state_, string.ptr, string.length); 1615 1616 return this; 1617 } 1618 1619 /******************************************************************************* 1612 1620 1613 1621 Converts the Lua value at the given acceptable index to a string. The Lua … … 1630 1638 Replaces lua_tostring and lua_tolstring. 1631 1639 1632 *******************************************************************************/1633 1634 public char[]toString (int index)1635 {1636 uint len;1637 char* str = lua_tolstring (this.state_, index, &len);1638 return str[0 .. len];1639 }1640 1641 /*******************************************************************************1640 *******************************************************************************/ 1641 1642 public istring toString (int index) 1643 { 1644 uint len; 1645 ichar* str = lua_tolstring (this.state_, index, &len); 1646 return str[0 .. len]; 1647 } 1648 1649 /******************************************************************************* 1642 1650 1643 1651 Returns the same as toString(-1) and pops the value afterwards. 1644 1652 1645 *******************************************************************************/1646 1647 public char[]popString ()1648 {1649 char[]result = toString (-1);1650 pop ();1651 return result;1652 }1653 1654 /*******************************************************************************1653 *******************************************************************************/ 1654 1655 public istring popString () 1656 { 1657 istring result = toString (-1); 1658 pop (); 1659 return result; 1660 } 1661 1662 /******************************************************************************* 1655 1663 1656 1664 Returns true if the value at the given acceptable index is a function … … 1663 1671 Replaces lua_isfunction. 1664 1672 1665 *******************************************************************************/1666 1667 public bool isFunction (int index)1668 {1669 return lua_isfunction (this.state_, index) != 0;1670 }1671 1672 /*******************************************************************************1673 *******************************************************************************/ 1674 1675 public bool isFunction (int index) 1676 { 1677 return lua_isfunction (this.state_, index) != 0; 1678 } 1679 1680 /******************************************************************************* 1673 1681 1674 1682 Converts a value at the given acceptable index to a C function. That … … 1681 1689 Replaces lua_tocfunction. 1682 1690 1683 *******************************************************************************/1684 1685 public LuaCFunction toCFunction (int index)1686 {1687 return lua_tocfunction (this.state_, index);1688 }1689 1690 /*******************************************************************************1691 *******************************************************************************/ 1692 1693 public LuaCFunction toCFunction (int index) 1694 { 1695 return lua_tocfunction (this.state_, index); 1696 } 1697 1698 /******************************************************************************* 1691 1699 1692 1700 Pushes a new C closure onto the stack. … … 1708 1716 Replaces lua_pushcclosure and lua_pushcfunction. 1709 1717 1710 *******************************************************************************/1711 1712 public LuaState pushCClosure (LuaCFunction cfunction, int upvalues = 0)1713 {1714 lua_pushcclosure (this.state_, cfunction, upvalues);1715 1716 return this;1717 }1718 public alias pushCClosure pushCFunction;1719 1720 /*******************************************************************************1718 *******************************************************************************/ 1719 1720 public LuaState pushCClosure (LuaCFunction cfunction, int upvalues = 0) 1721 { 1722 lua_pushcclosure (this.state_, cfunction, upvalues); 1723 1724 return this; 1725 } 1726 public alias pushCClosure pushCFunction; 1727 1728 /******************************************************************************* 1721 1729 1722 1730 Returns true if the value at the given acceptable index is a C or D … … 1729 1737 Replaces lua_iscfunction. 1730 1738 1731 *******************************************************************************/1732 1733 public bool isCFunction (int index)1734 {1735 return lua_iscfunction (this.state_, index) != 0;1736 }1737 1738 /*******************************************************************************1739 *******************************************************************************/ 1740 1741 public bool isCFunction (int index) 1742 { 1743 return lua_iscfunction (this.state_, index) != 0; 1744 } 1745 1746 /******************************************************************************* 1739 1747 1740 1748 Pushes onto the stack the environment table of the value at the given index. … … 1746 1754 Replaces lua_getfenv. 1747 1755 1748 *******************************************************************************/1749 1750 public LuaState getFunctionEnvironment (int index)1751 {1756 *******************************************************************************/ 1757 1758 public LuaState getFunctionEnvironment (int index) 1759 { 1752 1760 lua_getfenv (this.state_, index); 1753 1761 1754 1762 return this; 1755 }1756 1757 /*******************************************************************************1763 } 1764 1765 /******************************************************************************* 1758 1766 1759 1767 Pops a table from the stack and sets it as the new environment for the … … 1767 1775 Replaces lua_setfenv. 1768 1776 1769 *******************************************************************************/1770 1771 public LuaState setFunctionEnvironment (int index)1772 {1773 if (lua_setfenv (this.state_, index) == 0)1774 throw new LuaCodeException ("Cannot set function environment for " ~ typeName (type (index)));1775 1776 return this; 1777 }1778 1779 /*******************************************************************************1777 *******************************************************************************/ 1778 1779 public LuaState setFunctionEnvironment (int index) 1780 { 1781 if (lua_setfenv (this.state_, index) == 0) 1782 throw new LuaCodeException ("Cannot set function environment for " ~ typeName (type (index)), __FILE__, __LINE__); 1783 1784 return this; 1785 } 1786 1787 /******************************************************************************* 1780 1788 1781 1789 Returns true if the value at the given acceptable index is a table, … … 1788 1796 Replaces lua_istable. 1789 1797 1790 *******************************************************************************/1791 1792 public bool isTable (int index)1793 {1794 return lua_istable (this.state_, index) != 0;1795 }1796 1797 /*******************************************************************************1798 *******************************************************************************/ 1799 1800 public bool isTable (int index) 1801 { 1802 return lua_istable (this.state_, index) != 0; 1803 } 1804 1805 /******************************************************************************* 1798 1806 1799 1807 Creates a new empty table and pushes it onto the stack. The new table … … 1808 1816 Replaces lua_createtable and lua_newtable. 1809 1817 1810 *******************************************************************************/1811 1812 public LuaState createTable (uint array_elements = 0, uint record_elements = 0)1813 {1814 lua_createtable (this.state_, array_elements, record_elements);1815 1816 return this;1817 }1818 1819 public alias createTable newTable;1820 1821 /*******************************************************************************1818 *******************************************************************************/ 1819 1820 public LuaState createTable (uint array_elements = 0, uint record_elements = 0) 1821 { 1822 lua_createtable (this.state_, array_elements, record_elements); 1823 1824 return this; 1825 } 1826 1827 public alias createTable newTable; 1828 1829 /******************************************************************************* 1822 1830 1823 1831 Does the equivalent to t[k] = v, where t is the value at the given valid … … 1834 1842 Replaces lua_settable. 1835 1843 1836 *******************************************************************************/1837 1838 public LuaState setTable (int index)1839 {1840 lua_settable (this.state_, index);1841 1842 return this;1843 }1844 1845 /*******************************************************************************1844 *******************************************************************************/ 1845 1846 public LuaState setTable (int index) 1847 { 1848 lua_settable (this.state_, index); 1849 1850 return this; 1851 } 1852 1853 /******************************************************************************* 1846 1854 1847 1855 Pushes onto the stack the value t[k], where t is the value at the given … … 1858 1866 Replaces lua_gettable. 1859 1867 1860 *******************************************************************************/1861 1862 public LuaState getTable (int index)1863 {1864 lua_gettable (this.state_, index);1865 1866 return this;1867 }1868 1869 /*******************************************************************************1868 *******************************************************************************/ 1869 1870 public LuaState getTable (int index) 1871 { 1872 lua_gettable (this.state_, index); 1873 1874 return this; 1875 } 1876 1877 /******************************************************************************* 1870 1878 1871 1879 Pops a key from the stack, and pushes a key-value pair from the table at the … … 1882 1890 L.pop (); 1883 1891 } 1884 ---1892 --- 1885 1893 1886 1894 While traversing a table, do not call toString directly on a key, unless you … … 1894 1902 Replaces lua_next. 1895 1903 1896 *******************************************************************************/1897 1898 public bool next (int index)1899 {1900 return lua_next (this.state_, index) != 0;1901 }1902 1903 /*******************************************************************************1904 *******************************************************************************/ 1905 1906 public bool next (int index) 1907 { 1908 return lua_next (this.state_, index) != 0; 1909 } 1910 1911 /******************************************************************************* 1904 1912 1905 1913 Creates and returns a reference, in the table at index t, for the object … … 1922 1930 Replaces luaL_ref. 1923 1931 1924 *******************************************************************************/1925 1926 public int createReference (int index)1927 {1928 return luaL_ref (this.state_, index);1929 }1930 1931 /*******************************************************************************1932 *******************************************************************************/ 1933 1934 public int createReference (int index) 1935 { 1936 return luaL_ref (this.state_, index); 1937 } 1938 1939 /******************************************************************************* 1932 1940 1933 1941 Releases reference ref from the table at index t (see createReference). … … 1944 1952 Replaces luaL_unref. 1945 1953 1946 *******************************************************************************/1947 1948 public LuaState releaseReference (int table_index, int reference)1949 {1950 luaL_unref (this.state_, table_index, reference);1951 1952 return this;1953 }1954 1955 /*******************************************************************************1954 *******************************************************************************/ 1955 1956 public LuaState releaseReference (int table_index, int reference) 1957 { 1958 luaL_unref (this.state_, table_index, reference); 1959 1960 return this; 1961 } 1962 1963 /******************************************************************************* 1956 1964 1957 1965 Pushes onto the stack the value t[k], where t is the value at the given … … 1966 1974 Replaces lua_getfield. 1967 1975 1968 *******************************************************************************/1969 1970 public LuaState getField (int index, char[]key)1971 {1972 lua_getfield (this.state_, index, toStringz (key));1973 1974 return this;1975 }1976 1977 /*******************************************************************************1976 *******************************************************************************/ 1977 1978 public LuaState getField (int index, cstring key) 1979 { 1980 lua_getfield (this.state_, index, toStringz (key)); 1981 1982 return this; 1983 } 1984 1985 /******************************************************************************* 1978 1986 1979 1987 Does the equivalent to t[k] = v, where t is the value at the given valid … … 1990 1998 Replaces lua_setfield. 1991 1999 1992 *******************************************************************************/1993 1994 public LuaState setField (int index, char[]key)1995 {2000 *******************************************************************************/ 2001 2002 public LuaState setField (int index, cstring key) 2003 { 1996 2004 lua_setfield (this.state_, index, toStringz (key)); 1997 2005 1998 2006 return this; 1999 }2000 2001 /*******************************************************************************2007 } 2008 2009 /******************************************************************************* 2002 2010 2003 2011 Pops a value from the stack and sets it as the new value of global name. … … 2010 2018 Replaces lua_setglobal. 2011 2019 2012 *******************************************************************************/2013 2014 public LuaState setGlobal (char[]name)2015 {2016 lua_setfield (this.state_, LUA_GLOBALSINDEX, toStringz (name));2017 2018 return this;2019 }2020 2021 /*******************************************************************************2020 *******************************************************************************/ 2021 2022 public LuaState setGlobal (cstring name) 2023 { 2024 lua_setfield (this.state_, LUA_GLOBALSINDEX, toStringz (name)); 2025 2026 return this; 2027 } 2028 2029 /******************************************************************************* 2022 2030 2023 2031 Pushes onto the stack the value of the global name. … … 2029 2037 Replaces lua_getglobal. 2030 2038 2031 *******************************************************************************/2032 2033 public LuaState getGlobal (char[]key)2034 {2039 *******************************************************************************/ 2040 2041 public LuaState getGlobal (cstring key) 2042 { 2035 2043 int last_dot = -1; 2036 2044 foreach (i, c; key) 2037 2045 { 2038 if (c == '.')2039 {2046 if (c == '.') 2047 { 2040 2048 if (last_dot < 0) 2041 2049 { 2042 lua_getfield (this.state_, LUA_GLOBALSINDEX, (key[0 .. i] ~ '\0').ptr);2043 last_dot = i;2050 lua_getfield (this.state_, LUA_GLOBALSINDEX, (key[0 .. i] ~ '\0').ptr); 2051 last_dot = i; 2044 2052 } 2045 2053 else 2046 2054 { 2047 lua_getfield (this.state_, -1, (key[last_dot + 1 .. i] ~ '\0').ptr);2048 lua_remove (this.state_, -2);2049 last_dot = i;2055 lua_getfield (this.state_, -1, (key[last_dot + 1 .. i] ~ '\0').ptr); 2056 lua_remove (this.state_, -2); 2057 last_dot = i; 2050 2058 } 2051 }2059 } 2052 2060 } 2053 2061 2054 2062 if (last_dot < 0) 2055 lua_getfield (this.state_, LUA_GLOBALSINDEX, toStringz (key));2063 lua_getfield (this.state_, LUA_GLOBALSINDEX, toStringz (key)); 2056 2064 else 2057 2065 { 2058 lua_getfield (this.state_, -1, (key[last_dot + 1 .. $] ~ '\0').ptr);2059 lua_remove (this.state_, -2);2066 lua_getfield (this.state_, -1, (key[last_dot + 1 .. $] ~ '\0').ptr); 2067 lua_remove (this.state_, -2); 2060 2068 } 2061 2069 2062 return this;2063 }2064 2065 /*******************************************************************************2070 return this; 2071 } 2072 2073 /******************************************************************************* 2066 2074 2067 2075 If the registry already has the key name, returns false. Otherwise, … … 2078 2086 Replaces luaL_newmetatable. 2079 2087 2080 *******************************************************************************/2081 2082 public bool newMetatable (char[]name)2083 {2088 *******************************************************************************/ 2089 2090 public bool newMetatable (cstring name) 2091 { 2084 2092 return luaL_newmetatable (this.state_, toStringz (name)) != 0; 2085 }2086 2087 /*******************************************************************************2093 } 2094 2095 /******************************************************************************* 2088 2096 2089 2097 Pushes onto the stack the metatable of the value at the given acceptable … … 2098 2106 Replaces lua_getmetatable. 2099 2107 2100 *******************************************************************************/2101 2102 public bool getMetatable (int index)2103 {2108 *******************************************************************************/ 2109 2110 public bool getMetatable (int index) 2111 { 2104 2112 return lua_getmetatable (this.state_, index) != 0; 2105 }2106 2107 /*******************************************************************************2113 } 2114 2115 /******************************************************************************* 2108 2116 2109 2117 Pushes onto the stack the metatable associated with name in the registry. … … 2115 2123 Replaces luaL_getmetatable. 2116 2124 2117 *******************************************************************************/2118 2119 public LuaState getMetatable (char[]name)2120 {2125 *******************************************************************************/ 2126 2127 public LuaState getMetatable (cstring name) 2128 { 2121 2129 luaL_getmetatable (this.state_, toStringz (name)); 2122 2130 2123 2131 return this; 2124 }2125 2126 /*******************************************************************************2132 } 2133 2134 /******************************************************************************* 2127 2135 2128 2136 Pops a table from the stack and sets it as the new metatable for the … … 2135 2143 Replaces lua_setmetatable. 2136 2144 2137 *******************************************************************************/2138 2139 public LuaState setMetatable (int index)2140 {2145 *******************************************************************************/ 2146 2147 public LuaState setMetatable (int index) 2148 { 2141 2149 lua_setmetatable (this.state_, index); 2142 2150 2143 2151 return this; 2144 }2145 2146 /*******************************************************************************2152 } 2153 2154 /******************************************************************************* 2147 2155 2148 2156 Pushes onto the stack the field from the metatable of the object at the … … 2157 2165 Replaces luaL_getmetafield. 2158 2166 2159 *******************************************************************************/2160 2161 public int getMetafield (int index, char[]field)2162 {2163 return luaL_getmetafield (this.state_, index, toStringz (field));2164 }2165 2166 /*******************************************************************************2167 *******************************************************************************/ 2168 2169 public int getMetafield (int index, cstring field) 2170 { 2171 return luaL_getmetafield (this.state_, index, toStringz (field)); 2172 } 2173 2174 /******************************************************************************* 2167 2175 2168 2176 This function allocates a new block of memory with the given size, pushes … … 2185 2193 Replaces lua_newuserdata. 2186 2194 2187 *******************************************************************************/2188 2189 public void* newUserdata (uint size)2190 {2191 return lua_newuserdata (this.state_, size);2192 }2193 2194 /*******************************************************************************2195 *******************************************************************************/ 2196 2197 public void* newUserdata (uint size) 2198 { 2199 return lua_newuserdata (this.state_, size); 2200 } 2201 2202 /******************************************************************************* 2195 2203 2196 2204 If the value at the given acceptable index is a full userdata, returns … … 2204 2212 Replaces lua_touserdata. 2205 2213 2206 ******************************************************************************/2207 2208 public void* toUserdata (int index)2209 {2210 return lua_touserdata (this.state_, index);2211 }2212 2213 /*******************************************************************************2214 ******************************************************************************/ 2215 2216 public void* toUserdata (int index) 2217 { 2218 return lua_touserdata (this.state_, index); 2219 } 2220 2221 /******************************************************************************* 2214 2222 2215 2223 Returns true if the value at the given acceptable index is a userdata … … 2222 2230 Replaces lua_isuserdata. 2223 2231 2224 ******************************************************************************/2225 2226 public bool isUserdata (int index)2227 {2228 return lua_isuserdata (this.state_, index) != 0;2229 }2230 2231 /*******************************************************************************2232 ******************************************************************************/ 2233 2234 public bool isUserdata (int index) 2235 { 2236 return lua_isuserdata (this.state_, index) != 0; 2237 } 2238 2239 /******************************************************************************* 2232 2240 2233 2241 Checks whether the function argument is a userdata of the type name. … … 2239 2247 Replaces luaL_checkudata. 2240 2248 2241 ******************************************************************************/2242 2243 public void* checkUserdata (int argument, char[]name)2244 {2245 return luaL_checkudata (this.state_, argument, toStringz (name));2246 }2247 2248 /*******************************************************************************2249 ******************************************************************************/ 2250 2251 public void* checkUserdata (int argument, cstring name) 2252 { 2253 return luaL_checkudata (this.state_, argument, toStringz (name)); 2254 } 2255 2256 /******************************************************************************* 2249 2257 2250 2258 Returns true if the value at the given acceptable index is a light … … 2257 2265 Replaces lua_islightuserdata. 2258 2266 2259 *******************************************************************************/2260 2261 public bool isLightUserdata (int index)2262 {2263 return lua_islightuserdata (this.state_, index) != 0;2264 }2265 2266 /*******************************************************************************2267 *******************************************************************************/ 2268 2269 public bool isLightUserdata (int index) 2270 { 2271 return lua_islightuserdata (this.state_, index) != 0; 2272 } 2273 2274 /******************************************************************************* 2267 2275 2268 2276 Pushes a light userdata onto the stack. … … 2279 2287 Replaces lua_pushlightuserdata. 2280 2288 2281 *******************************************************************************/2282 2283 public LuaState pushLightUserdata (void* pointer)2284 {2285 lua_pushlightuserdata (this.state_, pointer);2286 2287 return this;2288 }2289 2290 /*******************************************************************************2289 *******************************************************************************/ 2290 2291 public LuaState pushLightUserdata (void* pointer) 2292 { 2293 lua_pushlightuserdata (this.state_, pointer); 2294 2295 return this; 2296 } 2297 2298 /******************************************************************************* 2291 2299 2292 2300 Moves the top element into the given valid index, shifting up the … … 2300 2308 Replaces lua_insert. 2301 2309 2302 *******************************************************************************/2303 2304 public LuaState insert (int index)2305 {2310 *******************************************************************************/ 2311 2312 public LuaState insert (int index) 2313 { 2306 2314 lua_insert (this.state_, index); 2307 2315 2308 2316 return this; 2309 }2310 2311 /*******************************************************************************2317 } 2318 2319 /******************************************************************************* 2312 2320 2313 2321 Concatenates the amount values at the top of the stack, pops them, and … … 2323 2331 Replaces lua_concat. 2324 2332 2325 *******************************************************************************/2326 2327 public LuaState concat (uint amount)2328 {2333 *******************************************************************************/ 2334 2335 public LuaState concat (uint amount) 2336 { 2329 2337 lua_concat (this.state_, amount); 2330 2338 2331 2339 return this; 2332 }2333 2334 /*******************************************************************************2340 } 2341 2342 /******************************************************************************* 2335 2343 2336 2344 Returns true if the two values in acceptable indices a and b are equal, … … 2346 2354 Replaces lua_equal. 2347 2355 2348 *******************************************************************************/2349 2350 public bool equal (int a, int b)2351 {2352 return lua_equal (this.state_, a, b) != 0;2353 }2354 2355 /*******************************************************************************2356 *******************************************************************************/ 2357 2358 public bool equal (int a, int b) 2359 { 2360 return lua_equal (this.state_, a, b) != 0; 2361 } 2362 2363 /******************************************************************************* 2356 2364 2357 2365 Returns true if the value at acceptable index a is smaller than the value … … 2367 2375 Replaces lua_lessthan. 2368 2376 2369 *******************************************************************************/2370 2371 public bool lessthan (int a, int b)2372 {2373 return lua_lessthan (this.state_, a, b) != 0;2374 }2375 2376 /*******************************************************************************2377 *******************************************************************************/ 2378 2379 public bool lessthan (int a, int b) 2380 { 2381 return lua_lessthan (this.state_, a, b) != 0; 2382 } 2383 2384 /******************************************************************************* 2377 2385 2378 2386 Converts the value at the given acceptable index to a Lua thread … … 2386 2394 Replaces lua_tothread. 2387 2395 2388 *******************************************************************************/2389 2390 public LuaState toThread (int index)2391 {2392 lua_State* state = lua_tothread (this.state_, index);2393 if (state is null)2394 return null;2395 else2396 {2397 auto thread = new LuaState (this.write_, state);2396 *******************************************************************************/ 2397 2398 public LuaState toThread (int index) 2399 { 2400 lua_State* state = lua_tothread (this.state_, index); 2401 if (state is null) 2402 return null; 2403 else 2404 { 2405 auto thread = new LuaState (this.write_, state); 2398 2406 thread.is_thread_state_ = true; 2399 2407 return thread; 2400 2408 } 2401 }2402 2403 /*******************************************************************************2409 } 2410 2411 /******************************************************************************* 2404 2412 2405 2413 Returns true if the value at the given acceptable index is a thread, … … 2412 2420 Replaces lua_isthread. 2413 2421 2414 *******************************************************************************/2415 2416 public bool isThread (int index)2417 {2418 return lua_isthread (this.state_, index) != 0;2419 }2420 2421 /*******************************************************************************2422 *******************************************************************************/ 2423 2424 public bool isThread (int index) 2425 { 2426 return lua_isthread (this.state_, index) != 0; 2427 } 2428 2429 /******************************************************************************* 2422 2430 2423 2431 Creates a new thread, pushes it on the stack, and returns a LuaState that … … 2432 2440 Replaces lua_newthread. 2433 2441 2434 *******************************************************************************/2435 2436 public LuaState newThread ()2437 {2438 auto thread = new LuaState (this.write_, lua_newthread (this.state_));2442 *******************************************************************************/ 2443 2444 public LuaState newThread () 2445 { 2446 auto thread = new LuaState (this.write_, lua_newthread (this.state_)); 2439 2447 thread.is_thread_state_ = true; 2440 2448 return thread; 2441 }2442 2443 /*******************************************************************************2449 } 2450 2451 /******************************************************************************* 2444 2452 2445 2453 Pushes the thread represented by the argument onto the stack. Returns … … 2449 2457 Replaces lua_pushthread. 2450 2458 2451 *******************************************************************************/2452 2453 public bool pushThread (LuaState thread)2454 {2455 return lua_pushthread (thread.state_) == 1;2456 }2457 2458 /*******************************************************************************2459 *******************************************************************************/ 2460 2461 public bool pushThread (LuaState thread) 2462 { 2463 return lua_pushthread (thread.state_) == 1; 2464 } 2465 2466 /******************************************************************************* 2459 2467 2460 2468 Yields a coroutine. … … 2463 2471 function, as follows: 2464 2472 --- 2465 return L.yield (result_number);2466 ---2473 return L.yield (result_number); 2474 --- 2467 2475 2468 2476 When a function calls yield in that way, the running coroutine suspends … … 2477 2485 Replaces lua_yield. 2478 2486 2479 *******************************************************************************/2480 2481 public int yield (int results)2482 {2483 return lua_yield (this.state_, results);2484 }2485 2486 /*******************************************************************************2487 *******************************************************************************/ 2488 2489 public int yield (int results) 2490 { 2491 return lua_yield (this.state_, results); 2492 } 2493 2494 /******************************************************************************* 2487 2495 2488 2496 Starts and resumes a coroutine in a given thread. … … 2505 2513 Replaces lua_resume. 2506 2514 2507 *******************************************************************************/2508 2509 public bool resume (uint arguments)2510 {2511 int errors = lua_resume (this.state_, arguments);2515 *******************************************************************************/ 2516 2517 public bool resume (uint arguments) 2518 { 2519 int errors = lua_resume (this.state_, arguments); 2512 2520 if (errors == 0) 2513 return false;2521 return false; 2514 2522 else if (errors == LUA_YIELD) 2515 return true;2523 return true; 2516 2524 else 2517 throw new LuaFatalException (errors);2518 }2519 2520 /*******************************************************************************2525 throw new LuaFatalException (errors, __FILE__, __LINE__); 2526 } 2527 2528 /******************************************************************************* 2521 2529 2522 2530 Returns the status of the thread. … … 2528 2536 Replaces lua_status. 2529 2537 2530 *******************************************************************************/2531 2532 public bool status ()2533 {2538 *******************************************************************************/ 2539 2540 public bool status () 2541 { 2534 2542 int errors = lua_status (this.state_); 2535 2543 if (errors == 0) 2536 return false;2544 return false; 2537 2545 else if (errors == LUA_YIELD) 2538 return true;2546 return true; 2539 2547 else 2540 throw new LuaFatalException (errors);2541 }2542 2543 /*******************************************************************************2548 throw new LuaFatalException (errors, __FILE__, __LINE__); 2549 } 2550 2551 /******************************************************************************* 2544 2552 2545 2553 Exchange values between different threads of the same global state. … … 2551 2559 Replaces lua_xmove. 2552 2560 2553 *******************************************************************************/2554 2555 public static void xmove (LuaState from_thread, LuaState to_thread, int amount)2556 {2557 lua_xmove (from_thread.state_, to_thread.state_, amount);2558 }2559 2560 /*******************************************************************************2561 *******************************************************************************/ 2562 2563 public static void xmove (LuaState from_thread, LuaState to_thread, int amount) 2564 { 2565 lua_xmove (from_thread.state_, to_thread.state_, amount); 2566 } 2567 2568 /******************************************************************************* 2561 2569 2562 2570 Converts the value at the given acceptable index to a generic pointer … … 2573 2581 Replaces lua_xmove. 2574 2582 2575 *******************************************************************************/2576 2577 public void* toPointer (int index)2578 {2579 return lua_topointer (this.state_, index);2580 }2581 2582 /*******************************************************************************2583 *******************************************************************************/ 2584 2585 public void* toPointer (int index) 2586 { 2587 return lua_topointer (this.state_, index); 2588 } 2589 2590 /******************************************************************************* 2583 2591 2584 2592 Returns the Activation Record of a given level. It contains information … … 2596 2604 Replaces lua_getstack. 2597 2605 2598 *******************************************************************************/2599 2600 public LuaActivationRecord getActivationRecord (uint level)2601 {2602 lua_Debug record;2603 2604 if (lua_getstack (this.state_, level, &record))2605 return new LuaActivationRecord (this.state_, &record);2606 else2607 return null;2608 }2609 2610 /*******************************************************************************2606 *******************************************************************************/ 2607 2608 public LuaActivationRecord getActivationRecord (uint level) 2609 { 2610 lua_Debug record; 2611 2612 if (lua_getstack (this.state_, level, &record)) 2613 return new LuaActivationRecord (this.state_, &record); 2614 else 2615 return null; 2616 } 2617 2618 /******************************************************************************* 2611 2619 2612 2620 Returns the current Call Trace which contains all Activation Records. 2613 2621 2614 *******************************************************************************/2615 2616 public LuaCallTrace getCallTrace ()2617 {2618 return new LuaCallTrace (this);2619 }2620 2621 /*******************************************************************************2622 *******************************************************************************/ 2623 2624 public LuaCallTrace getCallTrace () 2625 { 2626 return new LuaCallTrace (this); 2627 } 2628 2629 /******************************************************************************* 2622 2630 2623 2631 Returns the current Stack Trace which contains all Objects on the stack. 2624 2632 2625 *******************************************************************************/2626 2627 public LuaStackTrace getStackTrace ()2628 {2629 return new LuaStackTrace (this);2630 }2631 2632 /*******************************************************************************2633 *******************************************************************************/ 2634 2635 public LuaStackTrace getStackTrace () 2636 { 2637 return new LuaStackTrace (this); 2638 } 2639 2640 /******************************************************************************* 2633 2641 2634 2642 Returns a LuaObject (more precisely a subclass of it) of the value at … … 2639 2647 This is useful for stack traces (see getStackTrace) and to save a Lua 2640 2648 value (or whole tables) in a binary format. See LuaObject's methods. 2649 2650 Example: 2641 2651 --- 2642 2652 auto L = new LuaState; 2643 2653 L.pushNil (); 2644 L.pushNumber (2.0);2645 L.pushInteger (3);2646 2647 Stdout.formatln ("{} {} {}", L.getObject (1), L.getObject (2), L.getObject (3));2648 ---2649 2650 *******************************************************************************/2651 2652 // TODO: Support Lua functions (They should be dumped / loaded)2653 2654 public LuaObject getObject (int index)2655 {2656 LuaType type = type (index);2657 switch (type)2658 {2659 case LuaType.NIL: return new LuaNilObject ();2660 case LuaType.NUMBER: return new LuaNumberObject (toNumber (index));2661 case LuaType.BOOL: return new LuaBoolObject (toBool (index));2662 case LuaType.STRING: return new LuaStringObject (toString (index));2663 case LuaType.USERDATA: return new LuaUserdataObject (toUserdata (index), false);2664 case LuaType.LIGHTUSERDATA: return new LuaUserdataObject (toUserdata (index), true);2665 case LuaType.FUNCTION: return new LuaFunctionObject (toCFunction (index));2666 case LuaType.TABLE: return new LuaTableObject (this, index);2667 default: return null;2668 }2669 }2670 2671 /*******************************************************************************2654 L.pushNumber (2.0); 2655 L.pushInteger (3); 2656 2657 Stdout.formatln ("{} {} {}", L.getObject (-3), L.getObject (-2), L.getObject (-1)); 2658 --- 2659 2660 *******************************************************************************/ 2661 2662 // TODO: Support Lua functions (They should be dumped / loaded) 2663 2664 public LuaObject getObject (int index) 2665 { 2666 LuaType type = type (index); 2667 switch (type) 2668 { 2669 case LuaType.NIL: return new LuaNilObject (); 2670 case LuaType.NUMBER: return new LuaNumberObject (toNumber (index)); 2671 case LuaType.BOOL: return new LuaBoolObject (toBool (index)); 2672 case LuaType.STRING: return new LuaStringObject (toString (index)); 2673 case LuaType.USERDATA: return new LuaUserdataObject (toUserdata (index), false); 2674 case LuaType.LIGHTUSERDATA: return new LuaUserdataObject (toUserdata (index), true); 2675 case LuaType.FUNCTION: return new LuaFunctionObject (toCFunction (index)); 2676 case LuaType.TABLE: return new LuaTableObject (this, index); 2677 default: return null; 2678 } 2679 } 2680 2681 /******************************************************************************* 2672 2682 2673 2683 Pushes onto the stack the given object. … … 2676 2686 This does not work for plain Userdata and Lua function! 2677 2687 2678 *******************************************************************************/2679 2680 public void pushObject (LuaObject obj)2681 {2682 obj.push (this);2683 }2684 2685 /*******************************************************************************2688 *******************************************************************************/ 2689 2690 public void pushObject (LuaObject obj) 2691 { 2692 obj.push (this); 2693 } 2694 2695 /******************************************************************************* 2686 2696 2687 2697 Pushes onto the stack a string identifying the current position of the … … 2689 2699 the following format: 2690 2700 2691 chunkname:currentline:2701 chunkname:currentline: 2692 2702 2693 2703 Level 0 is the running function, level 1 is the function that called the … … 2702 2712 Replaces luaL_where. 2703 2713 2704 *******************************************************************************/2705 2706 public LuaState where (int level)2707 {2714 *******************************************************************************/ 2715 2716 public LuaState where (int level) 2717 { 2708 2718 luaL_where (this.state_, level); 2709 2719 2710 2720 return this; 2711 }2712 2713 /*******************************************************************************2721 } 2722 2723 /******************************************************************************* 2714 2724 2715 2725 Registers all functions from the given list under the given Lua library … … 2721 2731 categories = Bit-mask to select the functions to register, default is to select all. 2722 2732 2723 *******************************************************************************/2724 2725 public LuaState registerLibrary (LuaRegistry[] functions, char[]library_name = null, int categories = -1)2726 {2727 foreach (LuaRegistry reg; functions)2728 {2729 if (reg.category == 0 || (reg.category & categories) != 0)2730 {2731 registerFunction (reg.name, reg.cfunction, library_name);2732 }2733 }2734 2735 return this;2736 }2737 2738 /*******************************************************************************2733 *******************************************************************************/ 2734 2735 public LuaState registerLibrary (LuaRegistry[] functions, cstring library_name = null, int categories = -1) 2736 { 2737 foreach (LuaRegistry reg; functions) 2738 { 2739 if (reg.category == 0 || (reg.category & categories) != 0) 2740 { 2741 registerFunction (reg.name, reg.cfunction, library_name); 2742 } 2743 } 2744 2745 return this; 2746 } 2747 2748 /******************************************************************************* 2739 2749 2740 2750 Registers a C function as a global function in the given Lua library. … … 2748 2758 Together with registerMethod, replaces lua_register and luaL_register. 2749 2759 2750 *******************************************************************************/2751 2752 public LuaState registerFunction (char[] name, LuaCFunction cfunction, char[]library_name = null)2753 {2760 *******************************************************************************/ 2761 2762 public LuaState registerFunction (cstring name, LuaCFunction cfunction, cstring library_name = null) 2763 { 2754 2764 if (library_name is null) 2755 2765 { 2756 lua_register (this.state_, toStringz (name), cast (lua_CFunction) cfunction);2766 lua_register (this.state_, toStringz (name), cast (lua_CFunction) cfunction); 2757 2767 } 2758 2768 else 2759 2769 { 2760 extern (C) static luaL_reg[] reg = [ { null, null }, {null, null} ];2761 reg[0].name = toStringz (name);2762 reg[0].func = cast (lua_CFunction) cfunction;2763 2764 luaL_register (this.state_, toStringz (library_name), reg.ptr);2765 pop ();2770 extern (C) static luaL_reg[] reg = [ { null, null }, {null, null} ]; 2771 reg[0].name = toStringz (name); 2772 reg[0].func = cast (lua_CFunction) cfunction; 2773 2774 luaL_register (this.state_, toStringz (library_name), reg.ptr); 2775 pop (); 2766 2776 } 2767 2777 2768 2778 return this; 2769 }2770 2771 /*******************************************************************************2779 } 2780 2781 /******************************************************************************* 2772 2782 2773 2783 Registers a C function as a method of the table ontop of the stack. … … 2780 2790 Together with registerFunction, replaces luaL_register. 2781 2791 2782 *******************************************************************************/2783 2784 public LuaState registerMethod (char[]name, LuaCFunction cfunction)2785 {2792 *******************************************************************************/ 2793 2794 public LuaState registerMethod (cstring name, LuaCFunction cfunction) 2795 { 2786 2796 extern (C) static luaL_reg[] reg = [ { null, null }, {null, null} ]; 2787 2797 … … 2792 2802 2793 2803 return this; 2794 } 2795 2796 /******************************************************************************* 2797 2798 A helper method for wrapClass. 2799 2800 Params: 2801 instance = Class instance to wrap 2802 type_name = Name of the class type 2803 2804 *******************************************************************************/ 2804 } 2805 2805 2806 public LuaState wrapClass_ (Object instance, char[] type_name) 2807 { 2808 auto ptr = cast (Object *) newUserdata ( (Object *).sizeof); 2809 *ptr = instance; 2810 loadClassMetatable (type_name); 2811 setMetatable (-2); 2812 2813 return this; 2814 } 2815 2816 /******************************************************************************* 2806 /******************************************************************************* 2817 2807 2818 2808 Wraps a D class instance, whose class type must be registered first, in … … 2826 2816 Together with registerFunction, replaces luaL_register. 2827 2817 2828 *******************************************************************************/ 2829 2830 public LuaState wrapClass (T) (T instance) 2831 { 2832 return wrapClass_ (instance, typeid (T).toString); 2833 } 2834 2835 /******************************************************************************* 2818 *******************************************************************************/ 2819 2820 public LuaState wrapClass (T) (T instance) 2821 { 2822 wrapClassHelper (instance, typeid (T).toString); 2823 return this; 2824 } 2825 2826 /******************************************************************************* 2827 2828 Imagine you register a lot of classes. 2829 This helper method for wrapClass reduces the size of generated wrapClass methods. 2830 2831 Params: 2832 instance = Class instance to wrap 2833 typeString = Name of the class type 2834 2835 *******************************************************************************/ 2836 2837 private void wrapClassHelper (Object instance, cstring typeString) 2838 { 2839 auto ptr = cast (Object *) newUserdata ( (Object *).sizeof); 2840 *ptr = instance; 2841 2842 loadClassMetatable (typeString); // leaves the metatable on the stack 2843 setMetatable (-2); // pops table and sets it as metatable of the userdata 2844 } 2845 2846 /******************************************************************************* 2836 2847 2837 2848 Unwraps a userdata at a given index into a D class instance, whose class … … 2841 2852 index = Index to unwrap. 2842 2853 2843 *******************************************************************************/2844 2845 public T unwrapClass (T) (int index)2846 {2854 *******************************************************************************/ 2855 2856 public T unwrapClass (T) (int index) 2857 { 2847 2858 void* userdata = checkUserdata (index, mangleClass (typeid (T).toString)); 2848 2859 2849 2860 if (userdata is null) 2850 throw new LuaFatalException ("Error in LuaState.wrapClass: Cannot unwrap userdata to class " ~ typeid (T).toString ~ "! It is not registered for this LuaState!");2861 throw new LuaFatalException ("Error in LuaState.unwrapClass: Cannot unwrap userdata to class " ~ typeid (T).toString ~ "! It is not registered for this LuaState!", __FILE__, __LINE__); 2851 2862 2852 2863 T instance = * (cast (T *) userdata); 2853 2864 2854 2865 return instance; 2855 }2856 2857 /*******************************************************************************2866 } 2867 2868 /******************************************************************************* 2858 2869 2859 2870 Loads a class Metatable and creates it, if it does not exist. 2860 2861 *******************************************************************************/ 2862 2863 public LuaState loadClassMetatable (char[] class_name) 2864 { 2865 class_name = mangleClass (class_name); 2866 getMetatable (class_name); 2867 if (isNil (-1)) 2871 Leaves the metatable on the stack. 2872 2873 *******************************************************************************/ 2874 2875 public LuaState loadClassMetatable (cstring class_name) 2876 { 2877 class_name = mangleClass (class_name); 2878 getMetatable (class_name); 2879 if (isNil (-1)) // if the metatable hasn't been loaded yet, create it 2868 2880 { 2869 pop (); 2870 newMetatable (class_name); 2871 pushString ("__index"); 2872 pushValue (-2); 2873 setTable (-3); 2874 }2875 2876 return this; 2877 }2878 2879 2880 /*******************************************************************************2881 pop (); // remove the nil 2882 newMetatable (class_name); // create a new metatable t, push it and assoc. it with class_name in the registry 2883 pushString ("__index"); // table index 2884 pushValue (-2); // pushes the metatable 2885 setTable (-3); // metatable.__index = metatable 2886 } 2887 2888 return this; 2889 } 2890 2891 2892 /******************************************************************************* 2881 2893 2882 2894 Registers a D class in Lua, creating its metatable. The template parameter 2883 2895 must be the class. 2884 2896 2885 *******************************************************************************/2886 2887 public LuaState registerClass (T) ()2888 {2889 loadClassMetatable (typeid (T).toString ());2890 pop ();2891 2892 return this; 2893 }2894 2895 /*******************************************************************************2897 *******************************************************************************/ 2898 2899 public LuaState registerClass (T) () 2900 { 2901 loadClassMetatable (typeid (T).toString ()); 2902 pop (); 2903 2904 return this; 2905 } 2906 2907 /******************************************************************************* 2896 2908 2897 2909 Internal print function for Lua, which prints to the writer delegate … … 2901 2913 lua_state = Internal Lua state pointer 2902 2914 2903 *******************************************************************************/2904 2905 private extern (C) static int print (lua_State *lua_state)2906 {2907 LuaState L = states[lua_state];2908 2909 int argc = L.getTop ();2910 L.getGlobal ("tostring");2911 for (int i = 1; i <= argc; i++)2912 {2913 L.pushValue (-1);2914 L.pushValue (i);2915 L.call (false, 1, 1);2916 char[] s = L.toString(-1);2917 if (s is null)2918 {2919 return L.raiseError ("'tostring' must return a string to 'print'");2920 }2921 if (i > 1)2922 {2923 L.write ("\t");2924 }2925 L.write (s);2926 L.pop (1);2927 }2928 L.write ("\n");2929 return 0;2930 }2931 2932 /*******************************************************************************2915 *******************************************************************************/ 2916 2917 private extern (C) static int print (lua_State *lua_state) 2918 { 2919 LuaState L = states[lua_state]; 2920 2921 int argc = L.getTop (); 2922 L.getGlobal ("tostring"); 2923 for (int i = 1; i <= argc; i++) 2924 { 2925 L.pushValue (-1); 2926 L.pushValue (i); 2927 L.call (false, 1, 1); 2928 istring s = L.toString(-1); 2929 if (s is null) 2930 { 2931 return L.raiseError ("'tostring' must return a string to 'print'"); 2932 } 2933 if (i > 1) 2934 { 2935 L.write ("\t"); 2936 } 2937 L.write (s); 2938 L.pop (1); 2939 } 2940 L.write ("\n"); 2941 return 0; 2942 } 2943 2944 /******************************************************************************* 2933 2945 2934 2946 Internal writer function, wrapping Writer delegates for the dump family. … … 2940 2952 userdata = Userdata containing the delegate pointer 2941 2953 2942 *******************************************************************************/2943 2944 private extern (C) static int writer (lua_State* lua_state, void* ptr, uint size, void* userdata)2945 {2946 return ( * (cast (intdelegate (char[] data) *) userdata )) ((cast (char*) ptr)[0 .. size]);2947 }2948 2949 /*******************************************************************************2954 *******************************************************************************/ 2955 2956 private extern (C) static int writer (lua_State* lua_state, void* ptr, uint size, void* userdata) 2957 { 2958 return ( * (cast (int delegate (char[] data) *) userdata )) ((cast (char*) ptr)[0 .. size]); 2959 } 2960 2961 /******************************************************************************* 2950 2962 2951 2963 Internal reader function, wrapping Reader delegates for the load family. … … 2956 2968 psize = Return-parameter for the read size. 2957 2969 2958 *******************************************************************************/2959 2960 private extern (C) static char* reader (lua_State* lua_state, void* userdata, size_t* psize)2961 {2970 *******************************************************************************/ 2971 2972 private extern (C) static char* reader (lua_State* lua_state, void* userdata, size_t* psize) 2973 { 2962 2974 char[] data = ( * (cast (char[] delegate () *) userdata )) (); 2963 2975 2964 2976 *psize = data.length; 2965 2977 return data.ptr; 2966 }2967 2968 /// Associative array which maps C states to D state classes2969 private static LuaState[lua_State*] states;2978 } 2979 2980 /// Associative array which maps C states to D state classes 2981 private static LuaState[lua_State*] states; 2970 2982 } trunk/lua/utils.d
r314 r324 1 /*******************************************************************************1 /******************************************************************************* 2 2 3 3 copyright: Copyright (c) 2008 Matthias Walter. All rights reserved … … 13 13 14 14 module lua.utils; 15 private import lua.common; 16 private import lua.error; 15 17 16 18 /******************************************************************************* … … 75 77 *******************************************************************************/ 76 78 77 public static char[]int2string (T) (T i, int radix = 10)79 public static istring int2string (T) (T i, int radix = 10) 78 80 { 79 81 if (i < 0) … … 109 111 *******************************************************************************/ 110 112 111 public static T parseInt (T) (c har[]str, out int read, uint radix = 10)113 public static T parseInt (T) (cstring str, out int read, uint radix = 10) 112 114 { 113 115 read = 0; … … 175 177 *******************************************************************************/ 176 178 177 public static T string2int (T = long) (c har[]str, uint radix = 10)179 public static T string2int (T = long) (cstring str, uint radix = 10) 178 180 { 179 181 int read; … … 182 184 return result; 183 185 else if (read < 0) 184 throw new Ex ception ("Parse error in '" ~ str ~ "': Integer overflow.");186 throw new ExtendedException (cast(istring) ("Parse error in '" ~ str ~ "': Integer overflow."), __FILE__, __LINE__); 185 187 else 186 throw new Ex ception ("Parse error in '" ~ str ~ "' at position " ~ int2string (read));188 throw new ExtendedException (cast(istring) ("Parse error in '" ~ str ~ "' at position " ~ int2string (read)), __FILE__, __LINE__); 187 189 } 188 190 … … 230 232 *******************************************************************************/ 231 233 232 public static int find (c har[]str, char c)234 public static int find (cstring str, char c) 233 235 { 234 236 foreach (int i, char ch; str) … … 249 251 *******************************************************************************/ 250 252 251 public static int rfind (c har[]str, char c)253 public static int rfind (cstring str, char c) 252 254 { 253 255 foreach_reverse (int i, char ch; str) … … 265 267 *******************************************************************************/ 266 268 267 public static char* toStringz (c har[]s)269 public static char* toStringz (cstring s) 268 270 { 269 271 if (s.ptr) … … 272 274 s = s ~ '\0'; 273 275 } 274 return s.ptr;276 return cast(char*) s.ptr; 275 277 } 276 278 … … 281 283 *******************************************************************************/ 282 284 283 public static size_t strlenz (c har* s)285 public static size_t strlenz (cchar* s) 284 286 { 285 287 size_t i; … … 294 296 295 297 296 public static c har[] ltrim (char[] str, char[]chars)298 public static cstring ltrim (cstring str, cstring chars) 297 299 { 298 300 foreach (i, c; str)
