lauxlib.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. ** $Id: lauxlib.c,v 1.8 1998/01/09 15:06:07 roberto Exp $
  3. ** Auxiliar functions for building Lua libraries
  4. ** See Copyright Notice in lua.h
  5. */
  6. #include <stdarg.h>
  7. #include <stdio.h>
  8. /* Please Notice: This file uses only the oficial API of Lua
  9. ** Any function declared here could be written as an application
  10. ** function. With care, these functions can be used by other libraries.
  11. */
  12. #include "lauxlib.h"
  13. #include "lua.h"
  14. #include "luadebug.h"
  15. void luaL_argerror (int numarg, char *extramsg)
  16. {
  17. char *funcname;
  18. lua_getobjname(lua_stackedfunction(0), &funcname);
  19. if (funcname == NULL)
  20. funcname = "???";
  21. if (extramsg == NULL)
  22. luaL_verror("bad argument #%d to function `%.50s'", numarg, funcname);
  23. else
  24. luaL_verror("bad argument #%d to function `%.50s' (%.100s)",
  25. numarg, funcname, extramsg);
  26. }
  27. char *luaL_check_string (int numArg)
  28. {
  29. lua_Object o = lua_getparam(numArg);
  30. luaL_arg_check(lua_isstring(o), numArg, "string expected");
  31. return lua_getstring(o);
  32. }
  33. char *luaL_opt_string (int numArg, char *def)
  34. {
  35. return (lua_getparam(numArg) == LUA_NOOBJECT) ? def :
  36. luaL_check_string(numArg);
  37. }
  38. double luaL_check_number (int numArg)
  39. {
  40. lua_Object o = lua_getparam(numArg);
  41. luaL_arg_check(lua_isnumber(o), numArg, "number expected");
  42. return lua_getnumber(o);
  43. }
  44. double luaL_opt_number (int numArg, double def)
  45. {
  46. return (lua_getparam(numArg) == LUA_NOOBJECT) ? def :
  47. luaL_check_number(numArg);
  48. }
  49. lua_Object luaL_tablearg (int arg)
  50. {
  51. lua_Object o = lua_getparam(arg);
  52. luaL_arg_check(lua_istable(o), arg, "table expected");
  53. return o;
  54. }
  55. lua_Object luaL_functionarg (int arg)
  56. {
  57. lua_Object o = lua_getparam(arg);
  58. luaL_arg_check(lua_isfunction(o), arg, "function expected");
  59. return o;
  60. }
  61. lua_Object luaL_nonnullarg (int numArg)
  62. {
  63. lua_Object o = lua_getparam(numArg);
  64. luaL_arg_check(o != LUA_NOOBJECT, numArg, "value expected");
  65. return o;
  66. }
  67. void luaL_openlib (struct luaL_reg *l, int n)
  68. {
  69. int i;
  70. lua_open(); /* make sure lua is already open */
  71. for (i=0; i<n; i++)
  72. lua_register(l[i].name, l[i].func);
  73. }
  74. void luaL_verror (char *fmt, ...)
  75. {
  76. char buff[500];
  77. va_list argp;
  78. va_start(argp, fmt);
  79. vsprintf(buff, fmt, argp);
  80. va_end(argp);
  81. lua_error(buff);
  82. }