candle 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE.txt +20 -0
- data/README.md +4 -0
- data/VERSION +1 -0
- data/bin/candle +21 -0
- data/lib/candle.rb +68 -0
- data/lib/candle/command.rb +27 -0
- data/lib/candle/generators/actions.rb +183 -0
- data/lib/candle/generators/blank.rb +127 -0
- data/lib/candle/generators/cli.rb +55 -0
- data/lib/candle/generators/help.rb +47 -0
- data/lib/candle/generators/jam.rb +80 -0
- data/lib/candle/generators/lua/scripts/AppDelegate.lua +36 -0
- data/lib/candle/generators/lua/scripts/tests/init.lua +6 -0
- data/lib/candle/generators/lua/scripts/tests/someTest.lua +12 -0
- data/lib/candle/generators/lua/wax/ProtocolLoader.h +12 -0
- data/lib/candle/generators/lua/wax/bin/hammer +157 -0
- data/lib/candle/generators/lua/wax/bin/update-xibs +131 -0
- data/lib/candle/generators/lua/wax/bin/waxsim +0 -0
- data/lib/candle/generators/lua/wax/lib/build-scripts/compile-stdlib.sh +14 -0
- data/lib/candle/generators/lua/wax/lib/build-scripts/copy-scripts.sh +58 -0
- data/lib/candle/generators/lua/wax/lib/build-scripts/luac.lua +80 -0
- data/lib/candle/generators/lua/wax/lib/extensions/CGAffine/wax_CGTransform.h +12 -0
- data/lib/candle/generators/lua/wax/lib/extensions/CGAffine/wax_CGTransform.m +85 -0
- data/lib/candle/generators/lua/wax/lib/extensions/CGContext/wax_CGContext.h +12 -0
- data/lib/candle/generators/lua/wax/lib/extensions/CGContext/wax_CGContext.m +251 -0
- data/lib/candle/generators/lua/wax/lib/extensions/HTTP/wax_http.h +14 -0
- data/lib/candle/generators/lua/wax/lib/extensions/HTTP/wax_http.m +240 -0
- data/lib/candle/generators/lua/wax/lib/extensions/HTTP/wax_http_connection.h +54 -0
- data/lib/candle/generators/lua/wax/lib/extensions/HTTP/wax_http_connection.m +304 -0
- data/lib/candle/generators/lua/wax/lib/extensions/filesystem/wax_filesystem.h +9 -0
- data/lib/candle/generators/lua/wax/lib/extensions/filesystem/wax_filesystem.m +273 -0
- data/lib/candle/generators/lua/wax/lib/extensions/json/Rakefile +10 -0
- data/lib/candle/generators/lua/wax/lib/extensions/json/wax_json.c +304 -0
- data/lib/candle/generators/lua/wax/lib/extensions/json/wax_json.h +11 -0
- data/lib/candle/generators/lua/wax/lib/extensions/json/yajl-1.0.9.tar.gz +0 -0
- data/lib/candle/generators/lua/wax/lib/extensions/json/yajl/api/yajl_common.h +85 -0
- data/lib/candle/generators/lua/wax/lib/extensions/json/yajl/api/yajl_gen.h +159 -0
- data/lib/candle/generators/lua/wax/lib/extensions/json/yajl/api/yajl_parse.h +193 -0
- data/lib/candle/generators/lua/wax/lib/extensions/json/yajl/yajl.c +159 -0
- data/lib/candle/generators/lua/wax/lib/extensions/json/yajl/yajl_alloc.c +65 -0
- data/lib/candle/generators/lua/wax/lib/extensions/json/yajl/yajl_alloc.h +50 -0
- data/lib/candle/generators/lua/wax/lib/extensions/json/yajl/yajl_buf.c +119 -0
- data/lib/candle/generators/lua/wax/lib/extensions/json/yajl/yajl_buf.h +73 -0
- data/lib/candle/generators/lua/wax/lib/extensions/json/yajl/yajl_bytestack.h +85 -0
- data/lib/candle/generators/lua/wax/lib/extensions/json/yajl/yajl_common.h +85 -0
- data/lib/candle/generators/lua/wax/lib/extensions/json/yajl/yajl_encode.c +188 -0
- data/lib/candle/generators/lua/wax/lib/extensions/json/yajl/yajl_encode.h +50 -0
- data/lib/candle/generators/lua/wax/lib/extensions/json/yajl/yajl_gen.c +322 -0
- data/lib/candle/generators/lua/wax/lib/extensions/json/yajl/yajl_gen.h +159 -0
- data/lib/candle/generators/lua/wax/lib/extensions/json/yajl/yajl_lex.c +737 -0
- data/lib/candle/generators/lua/wax/lib/extensions/json/yajl/yajl_lex.h +133 -0
- data/lib/candle/generators/lua/wax/lib/extensions/json/yajl/yajl_parse.h +193 -0
- data/lib/candle/generators/lua/wax/lib/extensions/json/yajl/yajl_parser.c +448 -0
- data/lib/candle/generators/lua/wax/lib/extensions/json/yajl/yajl_parser.h +82 -0
- data/lib/candle/generators/lua/wax/lib/lua/lapi.c +1087 -0
- data/lib/candle/generators/lua/wax/lib/lua/lapi.h +16 -0
- data/lib/candle/generators/lua/wax/lib/lua/lauxlib.c +652 -0
- data/lib/candle/generators/lua/wax/lib/lua/lauxlib.h +174 -0
- data/lib/candle/generators/lua/wax/lib/lua/lbaselib.c +653 -0
- data/lib/candle/generators/lua/wax/lib/lua/lcode.c +839 -0
- data/lib/candle/generators/lua/wax/lib/lua/lcode.h +76 -0
- data/lib/candle/generators/lua/wax/lib/lua/ldblib.c +397 -0
- data/lib/candle/generators/lua/wax/lib/lua/ldebug.c +638 -0
- data/lib/candle/generators/lua/wax/lib/lua/ldebug.h +33 -0
- data/lib/candle/generators/lua/wax/lib/lua/ldo.c +518 -0
- data/lib/candle/generators/lua/wax/lib/lua/ldo.h +57 -0
- data/lib/candle/generators/lua/wax/lib/lua/ldump.c +164 -0
- data/lib/candle/generators/lua/wax/lib/lua/lfunc.c +174 -0
- data/lib/candle/generators/lua/wax/lib/lua/lfunc.h +34 -0
- data/lib/candle/generators/lua/wax/lib/lua/lgc.c +711 -0
- data/lib/candle/generators/lua/wax/lib/lua/lgc.h +110 -0
- data/lib/candle/generators/lua/wax/lib/lua/linit.c +38 -0
- data/lib/candle/generators/lua/wax/lib/lua/liolib.c +553 -0
- data/lib/candle/generators/lua/wax/lib/lua/llex.c +461 -0
- data/lib/candle/generators/lua/wax/lib/lua/llex.h +81 -0
- data/lib/candle/generators/lua/wax/lib/lua/llimits.h +128 -0
- data/lib/candle/generators/lua/wax/lib/lua/lmathlib.c +263 -0
- data/lib/candle/generators/lua/wax/lib/lua/lmem.c +86 -0
- data/lib/candle/generators/lua/wax/lib/lua/lmem.h +49 -0
- data/lib/candle/generators/lua/wax/lib/lua/loadlib.c +666 -0
- data/lib/candle/generators/lua/wax/lib/lua/lobject.c +214 -0
- data/lib/candle/generators/lua/wax/lib/lua/lobject.h +381 -0
- data/lib/candle/generators/lua/wax/lib/lua/lopcodes.c +102 -0
- data/lib/candle/generators/lua/wax/lib/lua/lopcodes.h +268 -0
- data/lib/candle/generators/lua/wax/lib/lua/loslib.c +243 -0
- data/lib/candle/generators/lua/wax/lib/lua/lparser.c +1339 -0
- data/lib/candle/generators/lua/wax/lib/lua/lparser.h +82 -0
- data/lib/candle/generators/lua/wax/lib/lua/lstate.c +214 -0
- data/lib/candle/generators/lua/wax/lib/lua/lstate.h +169 -0
- data/lib/candle/generators/lua/wax/lib/lua/lstring.c +111 -0
- data/lib/candle/generators/lua/wax/lib/lua/lstring.h +31 -0
- data/lib/candle/generators/lua/wax/lib/lua/lstrlib.c +869 -0
- data/lib/candle/generators/lua/wax/lib/lua/ltable.c +588 -0
- data/lib/candle/generators/lua/wax/lib/lua/ltable.h +40 -0
- data/lib/candle/generators/lua/wax/lib/lua/ltablib.c +287 -0
- data/lib/candle/generators/lua/wax/lib/lua/ltm.c +75 -0
- data/lib/candle/generators/lua/wax/lib/lua/ltm.h +54 -0
- data/lib/candle/generators/lua/wax/lib/lua/lua.h +388 -0
- data/lib/candle/generators/lua/wax/lib/lua/luaconf.h +753 -0
- data/lib/candle/generators/lua/wax/lib/lua/lualib.h +53 -0
- data/lib/candle/generators/lua/wax/lib/lua/lundump.c +227 -0
- data/lib/candle/generators/lua/wax/lib/lua/lundump.h +36 -0
- data/lib/candle/generators/lua/wax/lib/lua/lvm.c +763 -0
- data/lib/candle/generators/lua/wax/lib/lua/lvm.h +36 -0
- data/lib/candle/generators/lua/wax/lib/lua/lzio.c +82 -0
- data/lib/candle/generators/lua/wax/lib/lua/lzio.h +67 -0
- data/lib/candle/generators/lua/wax/lib/lua/print.c +227 -0
- data/lib/candle/generators/lua/wax/lib/project.rake +159 -0
- data/lib/candle/generators/lua/wax/lib/stdlib/enums.lua +396 -0
- data/lib/candle/generators/lua/wax/lib/stdlib/ext/http.lua +43 -0
- data/lib/candle/generators/lua/wax/lib/stdlib/ext/init.lua +4 -0
- data/lib/candle/generators/lua/wax/lib/stdlib/ext/number.lua +21 -0
- data/lib/candle/generators/lua/wax/lib/stdlib/ext/string.lua +97 -0
- data/lib/candle/generators/lua/wax/lib/stdlib/ext/table.lua +165 -0
- data/lib/candle/generators/lua/wax/lib/stdlib/helpers/WaxServer.lua +49 -0
- data/lib/candle/generators/lua/wax/lib/stdlib/helpers/autoload.lua +10 -0
- data/lib/candle/generators/lua/wax/lib/stdlib/helpers/base64.lua +64 -0
- data/lib/candle/generators/lua/wax/lib/stdlib/helpers/bit.lua +274 -0
- data/lib/candle/generators/lua/wax/lib/stdlib/helpers/cache.lua +73 -0
- data/lib/candle/generators/lua/wax/lib/stdlib/helpers/callback.lua +22 -0
- data/lib/candle/generators/lua/wax/lib/stdlib/helpers/frame.lua +76 -0
- data/lib/candle/generators/lua/wax/lib/stdlib/helpers/init.lua +78 -0
- data/lib/candle/generators/lua/wax/lib/stdlib/helpers/pickView.lua +54 -0
- data/lib/candle/generators/lua/wax/lib/stdlib/helpers/time.lua +102 -0
- data/lib/candle/generators/lua/wax/lib/stdlib/init.lua +18 -0
- data/lib/candle/generators/lua/wax/lib/stdlib/luaspec/init.lua +2 -0
- data/lib/candle/generators/lua/wax/lib/stdlib/luaspec/luamock.lua +84 -0
- data/lib/candle/generators/lua/wax/lib/stdlib/luaspec/luaspec.lua +377 -0
- data/lib/candle/generators/lua/wax/lib/stdlib/repl.lua +9 -0
- data/lib/candle/generators/lua/wax/lib/stdlib/structs.lua +11 -0
- data/lib/candle/generators/lua/wax/lib/stdlib/waxClass.lua +42 -0
- data/lib/candle/generators/lua/wax/lib/wax.h +16 -0
- data/lib/candle/generators/lua/wax/lib/wax.m +260 -0
- data/lib/candle/generators/lua/wax/lib/wax_class.h +18 -0
- data/lib/candle/generators/lua/wax/lib/wax_class.m +190 -0
- data/lib/candle/generators/lua/wax/lib/wax_gc.h +20 -0
- data/lib/candle/generators/lua/wax/lib/wax_gc.m +56 -0
- data/lib/candle/generators/lua/wax/lib/wax_helpers.h +102 -0
- data/lib/candle/generators/lua/wax/lib/wax_helpers.m +870 -0
- data/lib/candle/generators/lua/wax/lib/wax_instance.h +34 -0
- data/lib/candle/generators/lua/wax/lib/wax_instance.m +810 -0
- data/lib/candle/generators/lua/wax/lib/wax_server.h +47 -0
- data/lib/candle/generators/lua/wax/lib/wax_server.m +252 -0
- data/lib/candle/generators/lua/wax/lib/wax_stdlib.h +3 -0
- data/lib/candle/generators/lua/wax/lib/wax_struct.h +26 -0
- data/lib/candle/generators/lua/wax/lib/wax_struct.m +335 -0
- data/lib/candle/generators/templates/blank/WaxApplication.xcodeproj/project.pbxproj +836 -0
- data/lib/candle/generators/templates/blank/WaxApplication.xcodeproj/project.xcworkspace/contents.xcworkspacedata +7 -0
- data/lib/candle/generators/templates/blank/WaxApplication.xcodeproj/project.xcworkspace/xcuserdata/eiffel.xcuserdatad/UserInterfaceState.xcuserstate +0 -0
- data/lib/candle/generators/templates/blank/WaxApplication.xcodeproj/xcuserdata/eiffel.xcuserdatad/xcschemes/WaxApplication.xcscheme +86 -0
- data/lib/candle/generators/templates/blank/WaxApplication.xcodeproj/xcuserdata/eiffel.xcuserdatad/xcschemes/xcschememanagement.plist +22 -0
- data/lib/candle/generators/templates/blank/WaxApplication/Default-568h@2x.png +0 -0
- data/lib/candle/generators/templates/blank/WaxApplication/Default.png +0 -0
- data/lib/candle/generators/templates/blank/WaxApplication/Default@2x.png +0 -0
- data/lib/candle/generators/templates/blank/WaxApplication/ProtocolLoader.h +12 -0
- data/lib/candle/generators/templates/blank/WaxApplication/WaxApplication-Info.plist.tt +38 -0
- data/lib/candle/generators/templates/blank/WaxApplication/WaxApplication-Prefix.pch.tt +14 -0
- data/lib/candle/generators/templates/blank/WaxApplication/en.lproj/InfoPlist.strings +2 -0
- data/lib/candle/generators/templates/blank/WaxApplication/main.m.tt +20 -0
- data/lib/candle/tasks.rb +22 -0
- data/lib/candle/utility.rb +30 -0
- data/lib/candle/version.rb +9 -0
- data/lib/candle/view.rb +48 -0
- metadata +582 -0
@@ -0,0 +1,174 @@
|
|
1
|
+
/*
|
2
|
+
** $Id: lauxlib.h,v 1.88.1.1 2007/12/27 13:02:25 roberto Exp $
|
3
|
+
** Auxiliary functions for building Lua libraries
|
4
|
+
** See Copyright Notice in lua.h
|
5
|
+
*/
|
6
|
+
|
7
|
+
|
8
|
+
#ifndef lauxlib_h
|
9
|
+
#define lauxlib_h
|
10
|
+
|
11
|
+
|
12
|
+
#include <stddef.h>
|
13
|
+
#include <stdio.h>
|
14
|
+
|
15
|
+
#include "lua.h"
|
16
|
+
|
17
|
+
|
18
|
+
#if defined(LUA_COMPAT_GETN)
|
19
|
+
LUALIB_API int (luaL_getn) (lua_State *L, int t);
|
20
|
+
LUALIB_API void (luaL_setn) (lua_State *L, int t, int n);
|
21
|
+
#else
|
22
|
+
#define luaL_getn(L,i) ((int)lua_objlen(L, i))
|
23
|
+
#define luaL_setn(L,i,j) ((void)0) /* no op! */
|
24
|
+
#endif
|
25
|
+
|
26
|
+
#if defined(LUA_COMPAT_OPENLIB)
|
27
|
+
#define luaI_openlib luaL_openlib
|
28
|
+
#endif
|
29
|
+
|
30
|
+
|
31
|
+
/* extra error code for `luaL_load' */
|
32
|
+
#define LUA_ERRFILE (LUA_ERRERR+1)
|
33
|
+
|
34
|
+
|
35
|
+
typedef struct luaL_Reg {
|
36
|
+
const char *name;
|
37
|
+
lua_CFunction func;
|
38
|
+
} luaL_Reg;
|
39
|
+
|
40
|
+
|
41
|
+
|
42
|
+
LUALIB_API void (luaI_openlib) (lua_State *L, const char *libname,
|
43
|
+
const luaL_Reg *l, int nup);
|
44
|
+
LUALIB_API void (luaL_register) (lua_State *L, const char *libname,
|
45
|
+
const luaL_Reg *l);
|
46
|
+
LUALIB_API int (luaL_getmetafield) (lua_State *L, int obj, const char *e);
|
47
|
+
LUALIB_API int (luaL_callmeta) (lua_State *L, int obj, const char *e);
|
48
|
+
LUALIB_API int (luaL_typerror) (lua_State *L, int narg, const char *tname);
|
49
|
+
LUALIB_API int (luaL_argerror) (lua_State *L, int numarg, const char *extramsg);
|
50
|
+
LUALIB_API const char *(luaL_checklstring) (lua_State *L, int numArg,
|
51
|
+
size_t *l);
|
52
|
+
LUALIB_API const char *(luaL_optlstring) (lua_State *L, int numArg,
|
53
|
+
const char *def, size_t *l);
|
54
|
+
LUALIB_API lua_Number (luaL_checknumber) (lua_State *L, int numArg);
|
55
|
+
LUALIB_API lua_Number (luaL_optnumber) (lua_State *L, int nArg, lua_Number def);
|
56
|
+
|
57
|
+
LUALIB_API lua_Integer (luaL_checkinteger) (lua_State *L, int numArg);
|
58
|
+
LUALIB_API lua_Integer (luaL_optinteger) (lua_State *L, int nArg,
|
59
|
+
lua_Integer def);
|
60
|
+
|
61
|
+
LUALIB_API void (luaL_checkstack) (lua_State *L, int sz, const char *msg);
|
62
|
+
LUALIB_API void (luaL_checktype) (lua_State *L, int narg, int t);
|
63
|
+
LUALIB_API void (luaL_checkany) (lua_State *L, int narg);
|
64
|
+
|
65
|
+
LUALIB_API int (luaL_newmetatable) (lua_State *L, const char *tname);
|
66
|
+
LUALIB_API void *(luaL_checkudata) (lua_State *L, int ud, const char *tname);
|
67
|
+
|
68
|
+
LUALIB_API void (luaL_where) (lua_State *L, int lvl);
|
69
|
+
LUALIB_API int (luaL_error) (lua_State *L, const char *fmt, ...);
|
70
|
+
|
71
|
+
LUALIB_API int (luaL_checkoption) (lua_State *L, int narg, const char *def,
|
72
|
+
const char *const lst[]);
|
73
|
+
|
74
|
+
LUALIB_API int (luaL_ref) (lua_State *L, int t);
|
75
|
+
LUALIB_API void (luaL_unref) (lua_State *L, int t, int ref);
|
76
|
+
|
77
|
+
LUALIB_API int (luaL_loadfile) (lua_State *L, const char *filename);
|
78
|
+
LUALIB_API int (luaL_loadbuffer) (lua_State *L, const char *buff, size_t sz,
|
79
|
+
const char *name);
|
80
|
+
LUALIB_API int (luaL_loadstring) (lua_State *L, const char *s);
|
81
|
+
|
82
|
+
LUALIB_API lua_State *(luaL_newstate) (void);
|
83
|
+
|
84
|
+
|
85
|
+
LUALIB_API const char *(luaL_gsub) (lua_State *L, const char *s, const char *p,
|
86
|
+
const char *r);
|
87
|
+
|
88
|
+
LUALIB_API const char *(luaL_findtable) (lua_State *L, int idx,
|
89
|
+
const char *fname, int szhint);
|
90
|
+
|
91
|
+
|
92
|
+
|
93
|
+
|
94
|
+
/*
|
95
|
+
** ===============================================================
|
96
|
+
** some useful macros
|
97
|
+
** ===============================================================
|
98
|
+
*/
|
99
|
+
|
100
|
+
#define luaL_argcheck(L, cond,numarg,extramsg) \
|
101
|
+
((void)((cond) || luaL_argerror(L, (numarg), (extramsg))))
|
102
|
+
#define luaL_checkstring(L,n) (luaL_checklstring(L, (n), NULL))
|
103
|
+
#define luaL_optstring(L,n,d) (luaL_optlstring(L, (n), (d), NULL))
|
104
|
+
#define luaL_checkint(L,n) ((int)luaL_checkinteger(L, (n)))
|
105
|
+
#define luaL_optint(L,n,d) ((int)luaL_optinteger(L, (n), (d)))
|
106
|
+
#define luaL_checklong(L,n) ((long)luaL_checkinteger(L, (n)))
|
107
|
+
#define luaL_optlong(L,n,d) ((long)luaL_optinteger(L, (n), (d)))
|
108
|
+
|
109
|
+
#define luaL_typename(L,i) lua_typename(L, lua_type(L,(i)))
|
110
|
+
|
111
|
+
#define luaL_dofile(L, fn) \
|
112
|
+
(luaL_loadfile(L, fn) || lua_pcall(L, 0, LUA_MULTRET, 0))
|
113
|
+
|
114
|
+
#define luaL_dostring(L, s) \
|
115
|
+
(luaL_loadstring(L, s) || lua_pcall(L, 0, LUA_MULTRET, 0))
|
116
|
+
|
117
|
+
#define luaL_getmetatable(L,n) (lua_getfield(L, LUA_REGISTRYINDEX, (n)))
|
118
|
+
|
119
|
+
#define luaL_opt(L,f,n,d) (lua_isnoneornil(L,(n)) ? (d) : f(L,(n)))
|
120
|
+
|
121
|
+
/*
|
122
|
+
** {======================================================
|
123
|
+
** Generic Buffer manipulation
|
124
|
+
** =======================================================
|
125
|
+
*/
|
126
|
+
|
127
|
+
|
128
|
+
|
129
|
+
typedef struct luaL_Buffer {
|
130
|
+
char *p; /* current position in buffer */
|
131
|
+
int lvl; /* number of strings in the stack (level) */
|
132
|
+
lua_State *L;
|
133
|
+
char buffer[LUAL_BUFFERSIZE];
|
134
|
+
} luaL_Buffer;
|
135
|
+
|
136
|
+
#define luaL_addchar(B,c) \
|
137
|
+
((void)((B)->p < ((B)->buffer+LUAL_BUFFERSIZE) || luaL_prepbuffer(B)), \
|
138
|
+
(*(B)->p++ = (char)(c)))
|
139
|
+
|
140
|
+
/* compatibility only */
|
141
|
+
#define luaL_putchar(B,c) luaL_addchar(B,c)
|
142
|
+
|
143
|
+
#define luaL_addsize(B,n) ((B)->p += (n))
|
144
|
+
|
145
|
+
LUALIB_API void (luaL_buffinit) (lua_State *L, luaL_Buffer *B);
|
146
|
+
LUALIB_API char *(luaL_prepbuffer) (luaL_Buffer *B);
|
147
|
+
LUALIB_API void (luaL_addlstring) (luaL_Buffer *B, const char *s, size_t l);
|
148
|
+
LUALIB_API void (luaL_addstring) (luaL_Buffer *B, const char *s);
|
149
|
+
LUALIB_API void (luaL_addvalue) (luaL_Buffer *B);
|
150
|
+
LUALIB_API void (luaL_pushresult) (luaL_Buffer *B);
|
151
|
+
|
152
|
+
|
153
|
+
/* }====================================================== */
|
154
|
+
|
155
|
+
|
156
|
+
/* compatibility with ref system */
|
157
|
+
|
158
|
+
/* pre-defined references */
|
159
|
+
#define LUA_NOREF (-2)
|
160
|
+
#define LUA_REFNIL (-1)
|
161
|
+
|
162
|
+
#define lua_ref(L,lock) ((lock) ? luaL_ref(L, LUA_REGISTRYINDEX) : \
|
163
|
+
(lua_pushstring(L, "unlocked references are obsolete"), lua_error(L), 0))
|
164
|
+
|
165
|
+
#define lua_unref(L,ref) luaL_unref(L, LUA_REGISTRYINDEX, (ref))
|
166
|
+
|
167
|
+
#define lua_getref(L,ref) lua_rawgeti(L, LUA_REGISTRYINDEX, (ref))
|
168
|
+
|
169
|
+
|
170
|
+
#define luaL_reg luaL_Reg
|
171
|
+
|
172
|
+
#endif
|
173
|
+
|
174
|
+
|
@@ -0,0 +1,653 @@
|
|
1
|
+
/*
|
2
|
+
** $Id: lbaselib.c,v 1.191.1.6 2008/02/14 16:46:22 roberto Exp $
|
3
|
+
** Basic library
|
4
|
+
** See Copyright Notice in lua.h
|
5
|
+
*/
|
6
|
+
|
7
|
+
|
8
|
+
|
9
|
+
#include <ctype.h>
|
10
|
+
#include <stdio.h>
|
11
|
+
#include <stdlib.h>
|
12
|
+
#include <string.h>
|
13
|
+
|
14
|
+
#define lbaselib_c
|
15
|
+
#define LUA_LIB
|
16
|
+
|
17
|
+
#include "lua.h"
|
18
|
+
|
19
|
+
#include "lauxlib.h"
|
20
|
+
#include "lualib.h"
|
21
|
+
|
22
|
+
|
23
|
+
|
24
|
+
|
25
|
+
/*
|
26
|
+
** If your system does not support `stdout', you can just remove this function.
|
27
|
+
** If you need, you can define your own `print' function, following this
|
28
|
+
** model but changing `fputs' to put the strings at a proper place
|
29
|
+
** (a console window or a log file, for instance).
|
30
|
+
*/
|
31
|
+
static int luaB_print (lua_State *L) {
|
32
|
+
int n = lua_gettop(L); /* number of arguments */
|
33
|
+
int i;
|
34
|
+
lua_getglobal(L, "tostring");
|
35
|
+
for (i=1; i<=n; i++) {
|
36
|
+
const char *s;
|
37
|
+
lua_pushvalue(L, -1); /* function to be called */
|
38
|
+
lua_pushvalue(L, i); /* value to print */
|
39
|
+
lua_call(L, 1, 1);
|
40
|
+
s = lua_tostring(L, -1); /* get result */
|
41
|
+
if (s == NULL)
|
42
|
+
return luaL_error(L, LUA_QL("tostring") " must return a string to "
|
43
|
+
LUA_QL("print"));
|
44
|
+
if (i>1) fputs("\t", stdout);
|
45
|
+
fputs(s, stdout);
|
46
|
+
lua_pop(L, 1); /* pop result */
|
47
|
+
}
|
48
|
+
fputs("\n", stdout);
|
49
|
+
return 0;
|
50
|
+
}
|
51
|
+
|
52
|
+
|
53
|
+
static int luaB_tonumber (lua_State *L) {
|
54
|
+
int base = luaL_optint(L, 2, 10);
|
55
|
+
if (base == 10) { /* standard conversion */
|
56
|
+
luaL_checkany(L, 1);
|
57
|
+
if (lua_isnumber(L, 1)) {
|
58
|
+
lua_pushnumber(L, lua_tonumber(L, 1));
|
59
|
+
return 1;
|
60
|
+
}
|
61
|
+
}
|
62
|
+
else {
|
63
|
+
const char *s1 = luaL_checkstring(L, 1);
|
64
|
+
char *s2;
|
65
|
+
unsigned long n;
|
66
|
+
luaL_argcheck(L, 2 <= base && base <= 36, 2, "base out of range");
|
67
|
+
n = strtoul(s1, &s2, base);
|
68
|
+
if (s1 != s2) { /* at least one valid digit? */
|
69
|
+
while (isspace((unsigned char)(*s2))) s2++; /* skip trailing spaces */
|
70
|
+
if (*s2 == '\0') { /* no invalid trailing characters? */
|
71
|
+
lua_pushnumber(L, (lua_Number)n);
|
72
|
+
return 1;
|
73
|
+
}
|
74
|
+
}
|
75
|
+
}
|
76
|
+
lua_pushnil(L); /* else not a number */
|
77
|
+
return 1;
|
78
|
+
}
|
79
|
+
|
80
|
+
|
81
|
+
static int luaB_error (lua_State *L) {
|
82
|
+
int level = luaL_optint(L, 2, 1);
|
83
|
+
lua_settop(L, 1);
|
84
|
+
if (lua_isstring(L, 1) && level > 0) { /* add extra information? */
|
85
|
+
luaL_where(L, level);
|
86
|
+
lua_pushvalue(L, 1);
|
87
|
+
lua_concat(L, 2);
|
88
|
+
}
|
89
|
+
return lua_error(L);
|
90
|
+
}
|
91
|
+
|
92
|
+
|
93
|
+
static int luaB_getmetatable (lua_State *L) {
|
94
|
+
luaL_checkany(L, 1);
|
95
|
+
if (!lua_getmetatable(L, 1)) {
|
96
|
+
lua_pushnil(L);
|
97
|
+
return 1; /* no metatable */
|
98
|
+
}
|
99
|
+
luaL_getmetafield(L, 1, "__metatable");
|
100
|
+
return 1; /* returns either __metatable field (if present) or metatable */
|
101
|
+
}
|
102
|
+
|
103
|
+
|
104
|
+
static int luaB_setmetatable (lua_State *L) {
|
105
|
+
int t = lua_type(L, 2);
|
106
|
+
luaL_checktype(L, 1, LUA_TTABLE);
|
107
|
+
luaL_argcheck(L, t == LUA_TNIL || t == LUA_TTABLE, 2,
|
108
|
+
"nil or table expected");
|
109
|
+
if (luaL_getmetafield(L, 1, "__metatable"))
|
110
|
+
luaL_error(L, "cannot change a protected metatable");
|
111
|
+
lua_settop(L, 2);
|
112
|
+
lua_setmetatable(L, 1);
|
113
|
+
return 1;
|
114
|
+
}
|
115
|
+
|
116
|
+
|
117
|
+
static void getfunc (lua_State *L, int opt) {
|
118
|
+
if (lua_isfunction(L, 1)) lua_pushvalue(L, 1);
|
119
|
+
else {
|
120
|
+
lua_Debug ar;
|
121
|
+
int level = opt ? luaL_optint(L, 1, 1) : luaL_checkint(L, 1);
|
122
|
+
luaL_argcheck(L, level >= 0, 1, "level must be non-negative");
|
123
|
+
if (lua_getstack(L, level, &ar) == 0)
|
124
|
+
luaL_argerror(L, 1, "invalid level");
|
125
|
+
lua_getinfo(L, "f", &ar);
|
126
|
+
if (lua_isnil(L, -1))
|
127
|
+
luaL_error(L, "no function environment for tail call at level %d",
|
128
|
+
level);
|
129
|
+
}
|
130
|
+
}
|
131
|
+
|
132
|
+
|
133
|
+
static int luaB_getfenv (lua_State *L) {
|
134
|
+
getfunc(L, 1);
|
135
|
+
if (lua_iscfunction(L, -1)) /* is a C function? */
|
136
|
+
lua_pushvalue(L, LUA_GLOBALSINDEX); /* return the thread's global env. */
|
137
|
+
else
|
138
|
+
lua_getfenv(L, -1);
|
139
|
+
return 1;
|
140
|
+
}
|
141
|
+
|
142
|
+
|
143
|
+
static int luaB_setfenv (lua_State *L) {
|
144
|
+
luaL_checktype(L, 2, LUA_TTABLE);
|
145
|
+
getfunc(L, 0);
|
146
|
+
lua_pushvalue(L, 2);
|
147
|
+
if (lua_isnumber(L, 1) && lua_tonumber(L, 1) == 0) {
|
148
|
+
/* change environment of current thread */
|
149
|
+
lua_pushthread(L);
|
150
|
+
lua_insert(L, -2);
|
151
|
+
lua_setfenv(L, -2);
|
152
|
+
return 0;
|
153
|
+
}
|
154
|
+
else if (lua_iscfunction(L, -2) || lua_setfenv(L, -2) == 0)
|
155
|
+
luaL_error(L,
|
156
|
+
LUA_QL("setfenv") " cannot change environment of given object");
|
157
|
+
return 1;
|
158
|
+
}
|
159
|
+
|
160
|
+
|
161
|
+
static int luaB_rawequal (lua_State *L) {
|
162
|
+
luaL_checkany(L, 1);
|
163
|
+
luaL_checkany(L, 2);
|
164
|
+
lua_pushboolean(L, lua_rawequal(L, 1, 2));
|
165
|
+
return 1;
|
166
|
+
}
|
167
|
+
|
168
|
+
|
169
|
+
static int luaB_rawget (lua_State *L) {
|
170
|
+
luaL_checktype(L, 1, LUA_TTABLE);
|
171
|
+
luaL_checkany(L, 2);
|
172
|
+
lua_settop(L, 2);
|
173
|
+
lua_rawget(L, 1);
|
174
|
+
return 1;
|
175
|
+
}
|
176
|
+
|
177
|
+
static int luaB_rawset (lua_State *L) {
|
178
|
+
luaL_checktype(L, 1, LUA_TTABLE);
|
179
|
+
luaL_checkany(L, 2);
|
180
|
+
luaL_checkany(L, 3);
|
181
|
+
lua_settop(L, 3);
|
182
|
+
lua_rawset(L, 1);
|
183
|
+
return 1;
|
184
|
+
}
|
185
|
+
|
186
|
+
|
187
|
+
static int luaB_gcinfo (lua_State *L) {
|
188
|
+
lua_pushinteger(L, lua_getgccount(L));
|
189
|
+
return 1;
|
190
|
+
}
|
191
|
+
|
192
|
+
|
193
|
+
static int luaB_collectgarbage (lua_State *L) {
|
194
|
+
static const char *const opts[] = {"stop", "restart", "collect",
|
195
|
+
"count", "step", "setpause", "setstepmul", NULL};
|
196
|
+
static const int optsnum[] = {LUA_GCSTOP, LUA_GCRESTART, LUA_GCCOLLECT,
|
197
|
+
LUA_GCCOUNT, LUA_GCSTEP, LUA_GCSETPAUSE, LUA_GCSETSTEPMUL};
|
198
|
+
int o = luaL_checkoption(L, 1, "collect", opts);
|
199
|
+
int ex = luaL_optint(L, 2, 0);
|
200
|
+
int res = lua_gc(L, optsnum[o], ex);
|
201
|
+
switch (optsnum[o]) {
|
202
|
+
case LUA_GCCOUNT: {
|
203
|
+
int b = lua_gc(L, LUA_GCCOUNTB, 0);
|
204
|
+
lua_pushnumber(L, res + ((lua_Number)b/1024));
|
205
|
+
return 1;
|
206
|
+
}
|
207
|
+
case LUA_GCSTEP: {
|
208
|
+
lua_pushboolean(L, res);
|
209
|
+
return 1;
|
210
|
+
}
|
211
|
+
default: {
|
212
|
+
lua_pushnumber(L, res);
|
213
|
+
return 1;
|
214
|
+
}
|
215
|
+
}
|
216
|
+
}
|
217
|
+
|
218
|
+
|
219
|
+
static int luaB_type (lua_State *L) {
|
220
|
+
luaL_checkany(L, 1);
|
221
|
+
lua_pushstring(L, luaL_typename(L, 1));
|
222
|
+
return 1;
|
223
|
+
}
|
224
|
+
|
225
|
+
|
226
|
+
static int luaB_next (lua_State *L) {
|
227
|
+
luaL_checktype(L, 1, LUA_TTABLE);
|
228
|
+
lua_settop(L, 2); /* create a 2nd argument if there isn't one */
|
229
|
+
if (lua_next(L, 1))
|
230
|
+
return 2;
|
231
|
+
else {
|
232
|
+
lua_pushnil(L);
|
233
|
+
return 1;
|
234
|
+
}
|
235
|
+
}
|
236
|
+
|
237
|
+
|
238
|
+
static int luaB_pairs (lua_State *L) {
|
239
|
+
luaL_checktype(L, 1, LUA_TTABLE);
|
240
|
+
lua_pushvalue(L, lua_upvalueindex(1)); /* return generator, */
|
241
|
+
lua_pushvalue(L, 1); /* state, */
|
242
|
+
lua_pushnil(L); /* and initial value */
|
243
|
+
return 3;
|
244
|
+
}
|
245
|
+
|
246
|
+
|
247
|
+
static int ipairsaux (lua_State *L) {
|
248
|
+
int i = luaL_checkint(L, 2);
|
249
|
+
luaL_checktype(L, 1, LUA_TTABLE);
|
250
|
+
i++; /* next value */
|
251
|
+
lua_pushinteger(L, i);
|
252
|
+
lua_rawgeti(L, 1, i);
|
253
|
+
return (lua_isnil(L, -1)) ? 0 : 2;
|
254
|
+
}
|
255
|
+
|
256
|
+
|
257
|
+
static int luaB_ipairs (lua_State *L) {
|
258
|
+
luaL_checktype(L, 1, LUA_TTABLE);
|
259
|
+
lua_pushvalue(L, lua_upvalueindex(1)); /* return generator, */
|
260
|
+
lua_pushvalue(L, 1); /* state, */
|
261
|
+
lua_pushinteger(L, 0); /* and initial value */
|
262
|
+
return 3;
|
263
|
+
}
|
264
|
+
|
265
|
+
|
266
|
+
static int load_aux (lua_State *L, int status) {
|
267
|
+
if (status == 0) /* OK? */
|
268
|
+
return 1;
|
269
|
+
else {
|
270
|
+
lua_pushnil(L);
|
271
|
+
lua_insert(L, -2); /* put before error message */
|
272
|
+
return 2; /* return nil plus error message */
|
273
|
+
}
|
274
|
+
}
|
275
|
+
|
276
|
+
|
277
|
+
static int luaB_loadstring (lua_State *L) {
|
278
|
+
size_t l;
|
279
|
+
const char *s = luaL_checklstring(L, 1, &l);
|
280
|
+
const char *chunkname = luaL_optstring(L, 2, s);
|
281
|
+
return load_aux(L, luaL_loadbuffer(L, s, l, chunkname));
|
282
|
+
}
|
283
|
+
|
284
|
+
|
285
|
+
static int luaB_loadfile (lua_State *L) {
|
286
|
+
const char *fname = luaL_optstring(L, 1, NULL);
|
287
|
+
return load_aux(L, luaL_loadfile(L, fname));
|
288
|
+
}
|
289
|
+
|
290
|
+
|
291
|
+
/*
|
292
|
+
** Reader for generic `load' function: `lua_load' uses the
|
293
|
+
** stack for internal stuff, so the reader cannot change the
|
294
|
+
** stack top. Instead, it keeps its resulting string in a
|
295
|
+
** reserved slot inside the stack.
|
296
|
+
*/
|
297
|
+
static const char *generic_reader (lua_State *L, void *ud, size_t *size) {
|
298
|
+
(void)ud; /* to avoid warnings */
|
299
|
+
luaL_checkstack(L, 2, "too many nested functions");
|
300
|
+
lua_pushvalue(L, 1); /* get function */
|
301
|
+
lua_call(L, 0, 1); /* call it */
|
302
|
+
if (lua_isnil(L, -1)) {
|
303
|
+
*size = 0;
|
304
|
+
return NULL;
|
305
|
+
}
|
306
|
+
else if (lua_isstring(L, -1)) {
|
307
|
+
lua_replace(L, 3); /* save string in a reserved stack slot */
|
308
|
+
return lua_tolstring(L, 3, size);
|
309
|
+
}
|
310
|
+
else luaL_error(L, "reader function must return a string");
|
311
|
+
return NULL; /* to avoid warnings */
|
312
|
+
}
|
313
|
+
|
314
|
+
|
315
|
+
static int luaB_load (lua_State *L) {
|
316
|
+
int status;
|
317
|
+
const char *cname = luaL_optstring(L, 2, "=(load)");
|
318
|
+
luaL_checktype(L, 1, LUA_TFUNCTION);
|
319
|
+
lua_settop(L, 3); /* function, eventual name, plus one reserved slot */
|
320
|
+
status = lua_load(L, generic_reader, NULL, cname);
|
321
|
+
return load_aux(L, status);
|
322
|
+
}
|
323
|
+
|
324
|
+
|
325
|
+
static int luaB_dofile (lua_State *L) {
|
326
|
+
const char *fname = luaL_optstring(L, 1, NULL);
|
327
|
+
int n = lua_gettop(L);
|
328
|
+
if (luaL_loadfile(L, fname) != 0) lua_error(L);
|
329
|
+
lua_call(L, 0, LUA_MULTRET);
|
330
|
+
return lua_gettop(L) - n;
|
331
|
+
}
|
332
|
+
|
333
|
+
|
334
|
+
static int luaB_assert (lua_State *L) {
|
335
|
+
luaL_checkany(L, 1);
|
336
|
+
if (!lua_toboolean(L, 1))
|
337
|
+
return luaL_error(L, "%s", luaL_optstring(L, 2, "assertion failed!"));
|
338
|
+
return lua_gettop(L);
|
339
|
+
}
|
340
|
+
|
341
|
+
|
342
|
+
static int luaB_unpack (lua_State *L) {
|
343
|
+
int i, e, n;
|
344
|
+
luaL_checktype(L, 1, LUA_TTABLE);
|
345
|
+
i = luaL_optint(L, 2, 1);
|
346
|
+
e = luaL_opt(L, luaL_checkint, 3, luaL_getn(L, 1));
|
347
|
+
if (i > e) return 0; /* empty range */
|
348
|
+
n = e - i + 1; /* number of elements */
|
349
|
+
if (n <= 0 || !lua_checkstack(L, n)) /* n <= 0 means arith. overflow */
|
350
|
+
return luaL_error(L, "too many results to unpack");
|
351
|
+
lua_rawgeti(L, 1, i); /* push arg[i] (avoiding overflow problems) */
|
352
|
+
while (i++ < e) /* push arg[i + 1...e] */
|
353
|
+
lua_rawgeti(L, 1, i);
|
354
|
+
return n;
|
355
|
+
}
|
356
|
+
|
357
|
+
|
358
|
+
static int luaB_select (lua_State *L) {
|
359
|
+
int n = lua_gettop(L);
|
360
|
+
if (lua_type(L, 1) == LUA_TSTRING && *lua_tostring(L, 1) == '#') {
|
361
|
+
lua_pushinteger(L, n-1);
|
362
|
+
return 1;
|
363
|
+
}
|
364
|
+
else {
|
365
|
+
int i = luaL_checkint(L, 1);
|
366
|
+
if (i < 0) i = n + i;
|
367
|
+
else if (i > n) i = n;
|
368
|
+
luaL_argcheck(L, 1 <= i, 1, "index out of range");
|
369
|
+
return n - i;
|
370
|
+
}
|
371
|
+
}
|
372
|
+
|
373
|
+
|
374
|
+
static int luaB_pcall (lua_State *L) {
|
375
|
+
int status;
|
376
|
+
luaL_checkany(L, 1);
|
377
|
+
status = lua_pcall(L, lua_gettop(L) - 1, LUA_MULTRET, 0);
|
378
|
+
lua_pushboolean(L, (status == 0));
|
379
|
+
lua_insert(L, 1);
|
380
|
+
return lua_gettop(L); /* return status + all results */
|
381
|
+
}
|
382
|
+
|
383
|
+
|
384
|
+
static int luaB_xpcall (lua_State *L) {
|
385
|
+
int status;
|
386
|
+
luaL_checkany(L, 2);
|
387
|
+
lua_settop(L, 2);
|
388
|
+
lua_insert(L, 1); /* put error function under function to be called */
|
389
|
+
status = lua_pcall(L, 0, LUA_MULTRET, 1);
|
390
|
+
lua_pushboolean(L, (status == 0));
|
391
|
+
lua_replace(L, 1);
|
392
|
+
return lua_gettop(L); /* return status + all results */
|
393
|
+
}
|
394
|
+
|
395
|
+
|
396
|
+
static int luaB_tostring (lua_State *L) {
|
397
|
+
luaL_checkany(L, 1);
|
398
|
+
if (luaL_callmeta(L, 1, "__tostring")) /* is there a metafield? */
|
399
|
+
return 1; /* use its value */
|
400
|
+
switch (lua_type(L, 1)) {
|
401
|
+
case LUA_TNUMBER:
|
402
|
+
lua_pushstring(L, lua_tostring(L, 1));
|
403
|
+
break;
|
404
|
+
case LUA_TSTRING:
|
405
|
+
lua_pushvalue(L, 1);
|
406
|
+
break;
|
407
|
+
case LUA_TBOOLEAN:
|
408
|
+
lua_pushstring(L, (lua_toboolean(L, 1) ? "true" : "false"));
|
409
|
+
break;
|
410
|
+
case LUA_TNIL:
|
411
|
+
lua_pushliteral(L, "nil");
|
412
|
+
break;
|
413
|
+
default:
|
414
|
+
lua_pushfstring(L, "%s: %p", luaL_typename(L, 1), lua_topointer(L, 1));
|
415
|
+
break;
|
416
|
+
}
|
417
|
+
return 1;
|
418
|
+
}
|
419
|
+
|
420
|
+
|
421
|
+
static int luaB_newproxy (lua_State *L) {
|
422
|
+
lua_settop(L, 1);
|
423
|
+
lua_newuserdata(L, 0); /* create proxy */
|
424
|
+
if (lua_toboolean(L, 1) == 0)
|
425
|
+
return 1; /* no metatable */
|
426
|
+
else if (lua_isboolean(L, 1)) {
|
427
|
+
lua_newtable(L); /* create a new metatable `m' ... */
|
428
|
+
lua_pushvalue(L, -1); /* ... and mark `m' as a valid metatable */
|
429
|
+
lua_pushboolean(L, 1);
|
430
|
+
lua_rawset(L, lua_upvalueindex(1)); /* weaktable[m] = true */
|
431
|
+
}
|
432
|
+
else {
|
433
|
+
int validproxy = 0; /* to check if weaktable[metatable(u)] == true */
|
434
|
+
if (lua_getmetatable(L, 1)) {
|
435
|
+
lua_rawget(L, lua_upvalueindex(1));
|
436
|
+
validproxy = lua_toboolean(L, -1);
|
437
|
+
lua_pop(L, 1); /* remove value */
|
438
|
+
}
|
439
|
+
luaL_argcheck(L, validproxy, 1, "boolean or proxy expected");
|
440
|
+
lua_getmetatable(L, 1); /* metatable is valid; get it */
|
441
|
+
}
|
442
|
+
lua_setmetatable(L, 2);
|
443
|
+
return 1;
|
444
|
+
}
|
445
|
+
|
446
|
+
|
447
|
+
static const luaL_Reg base_funcs[] = {
|
448
|
+
{"assert", luaB_assert},
|
449
|
+
{"collectgarbage", luaB_collectgarbage},
|
450
|
+
{"dofile", luaB_dofile},
|
451
|
+
{"error", luaB_error},
|
452
|
+
{"gcinfo", luaB_gcinfo},
|
453
|
+
{"getfenv", luaB_getfenv},
|
454
|
+
{"getmetatable", luaB_getmetatable},
|
455
|
+
{"loadfile", luaB_loadfile},
|
456
|
+
{"load", luaB_load},
|
457
|
+
{"loadstring", luaB_loadstring},
|
458
|
+
{"next", luaB_next},
|
459
|
+
{"pcall", luaB_pcall},
|
460
|
+
{"print", luaB_print},
|
461
|
+
{"rawequal", luaB_rawequal},
|
462
|
+
{"rawget", luaB_rawget},
|
463
|
+
{"rawset", luaB_rawset},
|
464
|
+
{"select", luaB_select},
|
465
|
+
{"setfenv", luaB_setfenv},
|
466
|
+
{"setmetatable", luaB_setmetatable},
|
467
|
+
{"tonumber", luaB_tonumber},
|
468
|
+
{"tostring", luaB_tostring},
|
469
|
+
{"type", luaB_type},
|
470
|
+
{"unpack", luaB_unpack},
|
471
|
+
{"xpcall", luaB_xpcall},
|
472
|
+
{NULL, NULL}
|
473
|
+
};
|
474
|
+
|
475
|
+
|
476
|
+
/*
|
477
|
+
** {======================================================
|
478
|
+
** Coroutine library
|
479
|
+
** =======================================================
|
480
|
+
*/
|
481
|
+
|
482
|
+
#define CO_RUN 0 /* running */
|
483
|
+
#define CO_SUS 1 /* suspended */
|
484
|
+
#define CO_NOR 2 /* 'normal' (it resumed another coroutine) */
|
485
|
+
#define CO_DEAD 3
|
486
|
+
|
487
|
+
static const char *const statnames[] =
|
488
|
+
{"running", "suspended", "normal", "dead"};
|
489
|
+
|
490
|
+
static int costatus (lua_State *L, lua_State *co) {
|
491
|
+
if (L == co) return CO_RUN;
|
492
|
+
switch (lua_status(co)) {
|
493
|
+
case LUA_YIELD:
|
494
|
+
return CO_SUS;
|
495
|
+
case 0: {
|
496
|
+
lua_Debug ar;
|
497
|
+
if (lua_getstack(co, 0, &ar) > 0) /* does it have frames? */
|
498
|
+
return CO_NOR; /* it is running */
|
499
|
+
else if (lua_gettop(co) == 0)
|
500
|
+
return CO_DEAD;
|
501
|
+
else
|
502
|
+
return CO_SUS; /* initial state */
|
503
|
+
}
|
504
|
+
default: /* some error occured */
|
505
|
+
return CO_DEAD;
|
506
|
+
}
|
507
|
+
}
|
508
|
+
|
509
|
+
|
510
|
+
static int luaB_costatus (lua_State *L) {
|
511
|
+
lua_State *co = lua_tothread(L, 1);
|
512
|
+
luaL_argcheck(L, co, 1, "coroutine expected");
|
513
|
+
lua_pushstring(L, statnames[costatus(L, co)]);
|
514
|
+
return 1;
|
515
|
+
}
|
516
|
+
|
517
|
+
|
518
|
+
static int auxresume (lua_State *L, lua_State *co, int narg) {
|
519
|
+
int status = costatus(L, co);
|
520
|
+
if (!lua_checkstack(co, narg))
|
521
|
+
luaL_error(L, "too many arguments to resume");
|
522
|
+
if (status != CO_SUS) {
|
523
|
+
lua_pushfstring(L, "cannot resume %s coroutine", statnames[status]);
|
524
|
+
return -1; /* error flag */
|
525
|
+
}
|
526
|
+
lua_xmove(L, co, narg);
|
527
|
+
lua_setlevel(L, co);
|
528
|
+
status = lua_resume(co, narg);
|
529
|
+
if (status == 0 || status == LUA_YIELD) {
|
530
|
+
int nres = lua_gettop(co);
|
531
|
+
if (!lua_checkstack(L, nres + 1))
|
532
|
+
luaL_error(L, "too many results to resume");
|
533
|
+
lua_xmove(co, L, nres); /* move yielded values */
|
534
|
+
return nres;
|
535
|
+
}
|
536
|
+
else {
|
537
|
+
lua_xmove(co, L, 1); /* move error message */
|
538
|
+
return -1; /* error flag */
|
539
|
+
}
|
540
|
+
}
|
541
|
+
|
542
|
+
|
543
|
+
static int luaB_coresume (lua_State *L) {
|
544
|
+
lua_State *co = lua_tothread(L, 1);
|
545
|
+
int r;
|
546
|
+
luaL_argcheck(L, co, 1, "coroutine expected");
|
547
|
+
r = auxresume(L, co, lua_gettop(L) - 1);
|
548
|
+
if (r < 0) {
|
549
|
+
lua_pushboolean(L, 0);
|
550
|
+
lua_insert(L, -2);
|
551
|
+
return 2; /* return false + error message */
|
552
|
+
}
|
553
|
+
else {
|
554
|
+
lua_pushboolean(L, 1);
|
555
|
+
lua_insert(L, -(r + 1));
|
556
|
+
return r + 1; /* return true + `resume' returns */
|
557
|
+
}
|
558
|
+
}
|
559
|
+
|
560
|
+
|
561
|
+
static int luaB_auxwrap (lua_State *L) {
|
562
|
+
lua_State *co = lua_tothread(L, lua_upvalueindex(1));
|
563
|
+
int r = auxresume(L, co, lua_gettop(L));
|
564
|
+
if (r < 0) {
|
565
|
+
if (lua_isstring(L, -1)) { /* error object is a string? */
|
566
|
+
luaL_where(L, 1); /* add extra info */
|
567
|
+
lua_insert(L, -2);
|
568
|
+
lua_concat(L, 2);
|
569
|
+
}
|
570
|
+
lua_error(L); /* propagate error */
|
571
|
+
}
|
572
|
+
return r;
|
573
|
+
}
|
574
|
+
|
575
|
+
|
576
|
+
static int luaB_cocreate (lua_State *L) {
|
577
|
+
lua_State *NL = lua_newthread(L);
|
578
|
+
luaL_argcheck(L, lua_isfunction(L, 1) && !lua_iscfunction(L, 1), 1,
|
579
|
+
"Lua function expected");
|
580
|
+
lua_pushvalue(L, 1); /* move function to top */
|
581
|
+
lua_xmove(L, NL, 1); /* move function from L to NL */
|
582
|
+
return 1;
|
583
|
+
}
|
584
|
+
|
585
|
+
|
586
|
+
static int luaB_cowrap (lua_State *L) {
|
587
|
+
luaB_cocreate(L);
|
588
|
+
lua_pushcclosure(L, luaB_auxwrap, 1);
|
589
|
+
return 1;
|
590
|
+
}
|
591
|
+
|
592
|
+
|
593
|
+
static int luaB_yield (lua_State *L) {
|
594
|
+
return lua_yield(L, lua_gettop(L));
|
595
|
+
}
|
596
|
+
|
597
|
+
|
598
|
+
static int luaB_corunning (lua_State *L) {
|
599
|
+
if (lua_pushthread(L))
|
600
|
+
lua_pushnil(L); /* main thread is not a coroutine */
|
601
|
+
return 1;
|
602
|
+
}
|
603
|
+
|
604
|
+
|
605
|
+
static const luaL_Reg co_funcs[] = {
|
606
|
+
{"create", luaB_cocreate},
|
607
|
+
{"resume", luaB_coresume},
|
608
|
+
{"running", luaB_corunning},
|
609
|
+
{"status", luaB_costatus},
|
610
|
+
{"wrap", luaB_cowrap},
|
611
|
+
{"yield", luaB_yield},
|
612
|
+
{NULL, NULL}
|
613
|
+
};
|
614
|
+
|
615
|
+
/* }====================================================== */
|
616
|
+
|
617
|
+
|
618
|
+
static void auxopen (lua_State *L, const char *name,
|
619
|
+
lua_CFunction f, lua_CFunction u) {
|
620
|
+
lua_pushcfunction(L, u);
|
621
|
+
lua_pushcclosure(L, f, 1);
|
622
|
+
lua_setfield(L, -2, name);
|
623
|
+
}
|
624
|
+
|
625
|
+
|
626
|
+
static void base_open (lua_State *L) {
|
627
|
+
/* set global _G */
|
628
|
+
lua_pushvalue(L, LUA_GLOBALSINDEX);
|
629
|
+
lua_setglobal(L, "_G");
|
630
|
+
/* open lib into global table */
|
631
|
+
luaL_register(L, "_G", base_funcs);
|
632
|
+
lua_pushliteral(L, LUA_VERSION);
|
633
|
+
lua_setglobal(L, "_VERSION"); /* set global _VERSION */
|
634
|
+
/* `ipairs' and `pairs' need auxliliary functions as upvalues */
|
635
|
+
auxopen(L, "ipairs", luaB_ipairs, ipairsaux);
|
636
|
+
auxopen(L, "pairs", luaB_pairs, luaB_next);
|
637
|
+
/* `newproxy' needs a weaktable as upvalue */
|
638
|
+
lua_createtable(L, 0, 1); /* new table `w' */
|
639
|
+
lua_pushvalue(L, -1); /* `w' will be its own metatable */
|
640
|
+
lua_setmetatable(L, -2);
|
641
|
+
lua_pushliteral(L, "kv");
|
642
|
+
lua_setfield(L, -2, "__mode"); /* metatable(w).__mode = "kv" */
|
643
|
+
lua_pushcclosure(L, luaB_newproxy, 1);
|
644
|
+
lua_setglobal(L, "newproxy"); /* set global `newproxy' */
|
645
|
+
}
|
646
|
+
|
647
|
+
|
648
|
+
LUALIB_API int luaopen_base (lua_State *L) {
|
649
|
+
base_open(L);
|
650
|
+
luaL_register(L, LUA_COLIBNAME, co_funcs);
|
651
|
+
return 2;
|
652
|
+
}
|
653
|
+
|