diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1997-09-16 16:25:59 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1997-09-16 16:25:59 -0300 |
commit | c31aa863ac29fad5bb3f44c4e08640f877b65f25 (patch) | |
tree | 3eb3fb1dcb50a3b1beefaece33aba00e572e5ad6 /lauxlib.c | |
parent | ff08b0f4069e322ec4c2b02aa5553424227357ba (diff) | |
download | lua-c31aa863ac29fad5bb3f44c4e08640f877b65f25.tar.gz lua-c31aa863ac29fad5bb3f44c4e08640f877b65f25.tar.bz2 lua-c31aa863ac29fad5bb3f44c4e08640f877b65f25.zip |
Auxiliar functions for building Lua libraries
Diffstat (limited to 'lauxlib.c')
-rw-r--r-- | lauxlib.c | 76 |
1 files changed, 76 insertions, 0 deletions
diff --git a/lauxlib.c b/lauxlib.c new file mode 100644 index 00000000..37a6e41a --- /dev/null +++ b/lauxlib.c | |||
@@ -0,0 +1,76 @@ | |||
1 | /* | ||
2 | ** $Id: $ | ||
3 | ** Auxiliar functions for building Lua libraries | ||
4 | ** See Copyright Notice in lua.h | ||
5 | */ | ||
6 | |||
7 | |||
8 | #include <stdarg.h> | ||
9 | #include <stdio.h> | ||
10 | #include <string.h> | ||
11 | |||
12 | #include "lauxlib.h" | ||
13 | #include "lua.h" | ||
14 | #include "luadebug.h" | ||
15 | |||
16 | |||
17 | |||
18 | void luaL_arg_check(int cond, int numarg, char *extramsg) | ||
19 | { | ||
20 | if (!cond) { | ||
21 | char *funcname; | ||
22 | lua_getobjname(lua_stackedfunction(0), &funcname); | ||
23 | if (funcname == NULL) | ||
24 | funcname = "???"; | ||
25 | if (extramsg == NULL) | ||
26 | luaL_verror("bad argument #%d to function `%.50s'", numarg, funcname); | ||
27 | else | ||
28 | luaL_verror("bad argument #%d to function `%.50s' (%.100s)", | ||
29 | numarg, funcname, extramsg); | ||
30 | } | ||
31 | } | ||
32 | |||
33 | char *luaL_check_string (int numArg) | ||
34 | { | ||
35 | lua_Object o = lua_getparam(numArg); | ||
36 | luaL_arg_check(lua_isstring(o), numArg, "string expected"); | ||
37 | return lua_getstring(o); | ||
38 | } | ||
39 | |||
40 | char *luaL_opt_string (int numArg, char *def) | ||
41 | { | ||
42 | return (lua_getparam(numArg) == LUA_NOOBJECT) ? def : | ||
43 | luaL_check_string(numArg); | ||
44 | } | ||
45 | |||
46 | double luaL_check_number (int numArg) | ||
47 | { | ||
48 | lua_Object o = lua_getparam(numArg); | ||
49 | luaL_arg_check(lua_isnumber(o), numArg, "number expected"); | ||
50 | return lua_getnumber(o); | ||
51 | } | ||
52 | |||
53 | |||
54 | double luaL_opt_number (int numArg, double def) | ||
55 | { | ||
56 | return (lua_getparam(numArg) == LUA_NOOBJECT) ? def : | ||
57 | luaL_check_number(numArg); | ||
58 | } | ||
59 | |||
60 | void luaL_openlib (struct luaL_reg *l, int n) | ||
61 | { | ||
62 | int i; | ||
63 | for (i=0; i<n; i++) | ||
64 | lua_register(l[i].name, l[i].func); | ||
65 | } | ||
66 | |||
67 | |||
68 | void luaL_verror (char *fmt, ...) | ||
69 | { | ||
70 | char buff[500]; | ||
71 | va_list argp; | ||
72 | va_start(argp, fmt); | ||
73 | vsprintf(buff, fmt, argp); | ||
74 | va_end(argp); | ||
75 | lua_error(buff); | ||
76 | } | ||