diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2000-08-28 14:57:04 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2000-08-28 14:57:04 -0300 |
commit | 9fdf73bc9a6b4c6afbfff1d8181fface6b1c6761 (patch) | |
tree | da8d97d954e5ffabf9ff275df725f1e0a3a5b3e6 /lauxlib.c | |
parent | f1fd9b5c2c21f24d25d7813f431a3495702ebea6 (diff) | |
download | lua-9fdf73bc9a6b4c6afbfff1d8181fface6b1c6761.tar.gz lua-9fdf73bc9a6b4c6afbfff1d8181fface6b1c6761.tar.bz2 lua-9fdf73bc9a6b4c6afbfff1d8181fface6b1c6761.zip |
first version for new API
Diffstat (limited to 'lauxlib.c')
-rw-r--r-- | lauxlib.c | 68 |
1 files changed, 29 insertions, 39 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lauxlib.c,v 1.29 2000/06/12 13:52:05 roberto Exp roberto $ | 2 | ** $Id: lauxlib.c,v 1.30 2000/08/09 19:16:57 roberto Exp roberto $ |
3 | ** Auxiliary functions for building Lua libraries | 3 | ** Auxiliary functions for building Lua libraries |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -41,72 +41,61 @@ void luaL_argerror (lua_State *L, int narg, const char *extramsg) { | |||
41 | } | 41 | } |
42 | 42 | ||
43 | 43 | ||
44 | static void type_error (lua_State *L, int narg, const char *type_name, | 44 | static void type_error (lua_State *L, int narg, const char *type_name) { |
45 | lua_Object o) { | ||
46 | char buff[100]; | 45 | char buff[100]; |
47 | const char *otype = (o == LUA_NOOBJECT) ? "no value" : lua_type(L, o); | 46 | const char *rt = lua_type(L, narg); |
48 | sprintf(buff, "%.10s expected, got %.10s", type_name, otype); | 47 | if (*rt == 'N') rt = "no value"; |
48 | sprintf(buff, "%.10s expected, got %.10s", type_name, rt); | ||
49 | luaL_argerror(L, narg, buff); | 49 | luaL_argerror(L, narg, buff); |
50 | } | 50 | } |
51 | 51 | ||
52 | 52 | ||
53 | static const char *checkstr (lua_State *L, lua_Object o, int narg, | 53 | /* |
54 | size_t *len) { | 54 | ** use the 3rd letter of type names for testing: |
55 | const char *s = lua_getstring(L, o); | 55 | ** nuMber, niL, stRing, fuNction, usErdata, taBle, anY |
56 | if (!s) type_error(L, narg, "string", o); | 56 | */ |
57 | if (len) *len = lua_strlen(L, o); | 57 | void luaL_checktype(lua_State *L, int narg, const char *tname) { |
58 | const char *rt = lua_type(L, narg); | ||
59 | if (!(*rt != 'N' && (tname[2] == 'y' || tname[2] == rt[2]))) | ||
60 | type_error(L, narg, tname); | ||
61 | } | ||
62 | |||
63 | |||
64 | static const char *checkstr (lua_State *L, int narg, size_t *len) { | ||
65 | const char *s = lua_tostring(L, narg); | ||
66 | if (!s) type_error(L, narg, "string"); | ||
67 | if (len) *len = lua_strlen(L, narg); | ||
58 | return s; | 68 | return s; |
59 | } | 69 | } |
60 | 70 | ||
61 | const char *luaL_check_lstr (lua_State *L, int narg, size_t *len) { | 71 | const char *luaL_check_lstr (lua_State *L, int narg, size_t *len) { |
62 | return checkstr(L, lua_getparam(L, narg), narg, len); | 72 | return checkstr(L, narg, len); |
63 | } | 73 | } |
64 | 74 | ||
65 | const char *luaL_opt_lstr (lua_State *L, int narg, const char *def, | 75 | const char *luaL_opt_lstr (lua_State *L, int narg, const char *def, |
66 | size_t *len) { | 76 | size_t *len) { |
67 | lua_Object o = lua_getparam(L, narg); | 77 | if (lua_isnull(L, narg)) { |
68 | if (o == LUA_NOOBJECT) { | ||
69 | if (len) *len = def ? strlen(def) : 0; | 78 | if (len) *len = def ? strlen(def) : 0; |
70 | return def; | 79 | return def; |
71 | } | 80 | } |
72 | else return checkstr(L, o, narg, len); | 81 | else return checkstr(L, narg, len); |
73 | } | 82 | } |
74 | 83 | ||
75 | double luaL_check_number (lua_State *L, int narg) { | 84 | double luaL_check_number (lua_State *L, int narg) { |
76 | lua_Object o = lua_getparam(L, narg); | 85 | if (!lua_isnumber(L, narg)) type_error(L, narg, "number"); |
77 | if (!lua_isnumber(L, o)) type_error(L, narg, "number", o); | 86 | return lua_tonumber(L, narg); |
78 | return lua_getnumber(L, o); | ||
79 | } | 87 | } |
80 | 88 | ||
81 | 89 | ||
82 | double luaL_opt_number (lua_State *L, int narg, double def) { | 90 | double luaL_opt_number (lua_State *L, int narg, double def) { |
83 | lua_Object o = lua_getparam(L, narg); | 91 | if (lua_isnull(L, narg)) return def; |
84 | if (o == LUA_NOOBJECT) return def; | ||
85 | else { | 92 | else { |
86 | if (!lua_isnumber(L, o)) type_error(L, narg, "number", o); | 93 | if (!lua_isnumber(L, narg)) type_error(L, narg, "number"); |
87 | return lua_getnumber(L, o); | 94 | return lua_tonumber(L, narg); |
88 | } | 95 | } |
89 | } | 96 | } |
90 | 97 | ||
91 | 98 | ||
92 | lua_Object luaL_tablearg (lua_State *L, int narg) { | ||
93 | lua_Object o = lua_getparam(L, narg); | ||
94 | if (!lua_istable(L, o)) type_error(L, narg, "table", o); | ||
95 | return o; | ||
96 | } | ||
97 | |||
98 | lua_Object luaL_functionarg (lua_State *L, int narg) { | ||
99 | lua_Object o = lua_getparam(L, narg); | ||
100 | if (!lua_isfunction(L, o)) type_error(L, narg, "function", o); | ||
101 | return o; | ||
102 | } | ||
103 | |||
104 | lua_Object luaL_nonnullarg (lua_State *L, int narg) { | ||
105 | lua_Object o = lua_getparam(L, narg); | ||
106 | luaL_arg_check(L, o != LUA_NOOBJECT, narg, "value expected"); | ||
107 | return o; | ||
108 | } | ||
109 | |||
110 | void luaL_openlib (lua_State *L, const struct luaL_reg *l, int n) { | 99 | void luaL_openlib (lua_State *L, const struct luaL_reg *l, int n) { |
111 | int i; | 100 | int i; |
112 | for (i=0; i<n; i++) | 101 | for (i=0; i<n; i++) |
@@ -150,3 +139,4 @@ void luaL_filesource (char *out, const char *filename, int len) { | |||
150 | if (filename == NULL) filename = "(stdin)"; | 139 | if (filename == NULL) filename = "(stdin)"; |
151 | sprintf(out, "@%.*s", len-2, filename); /* -2 for '@' and '\0' */ | 140 | sprintf(out, "@%.*s", len-2, filename); /* -2 for '@' and '\0' */ |
152 | } | 141 | } |
142 | |||