@@ -76,73 +76,61 @@ static const struct luaL_reg fslib[] = {
76
76
{NULL , NULL },
77
77
};
78
78
79
- static apr_status_t lua_wapr_register (lua_State * L , apr_pool_t * root_pool )
79
+ static void lua_lkl_register (lua_State * L )
80
80
{
81
- gp = root_pool ;
82
81
lua_register (L , "stat" , luk_stat );
83
82
luaL_register (L , "lkl" , fslib );
84
- return APR_SUCCESS ;
85
83
}
86
- ///=========================================================
87
-
88
-
89
-
90
-
91
-
92
-
93
-
94
-
95
-
96
-
97
84
98
85
99
86
100
87
101
88
102
-
103
-
104
-
105
- ///==============================================================
106
-
107
- static const luaL_reg lualibs [] =
89
+ typedef void (* my_lua_registerer_t ) (lua_State * L );
90
+ static const my_lua_registerer_t lualibs [] =
108
91
{
109
- { "base" , luaopen_base } ,
110
- { "lkl" , lua_wapr_register } ,
111
- { NULL , NULL }
92
+ luaL_openlibs ,
93
+ luaopen_wapr ,
94
+ lua_lkl_register
112
95
};
113
96
114
97
/* A function to open up all the Lua libraries we've declared above. */
115
98
static void openlualibs (lua_State * l )
116
99
{
117
- const luaL_reg * lib ;
118
-
119
- for (lib = lualibs ; lib -> func != NULL ; lib ++ )
100
+ int i ;
101
+ for (i = 0 ; i < sizeof (lualibs )/sizeof (my_lua_registerer_t ); i ++ )
120
102
{
121
- lib -> func (l );
122
- lua_settop (l , 0 );
103
+ lualibs [ i ] (l );
104
+ lua_settop (l , 0 );
123
105
}
124
106
}
125
107
126
- static void lua_run_script (lua_State * L , const char * filename ) {
127
- #if defined(LUA_VERSION_NUM ) && LUA_VERSION_NUM >=501
128
- (void )luaL_dofile (L , filename );
129
- #else
130
- (void )lua_dofile (L , filename );
131
- #endif
108
+ static void lua_run_script (lua_State * L , const char * filename )
109
+ {
110
+ int s = luaL_loadfile (L , filename );
111
+ if (0 == s )
112
+ {
113
+ s = lua_pcall (L , 0 , LUA_MULTRET , 0 );
114
+ }
115
+ if (0 != s )
116
+ {
117
+ fprintf (stderr , "LUA Error: %s\n" , lua_tostring (L , -1 ));
118
+ lua_pop (L , 1 ); // remove error message
119
+ }
132
120
}
133
121
134
122
135
123
apr_status_t lua_lkl_main (const char * script_file , apr_pool_t * root_pool )
136
124
{
125
+ gp = root_pool ;
137
126
/* initialize Lua */
138
127
L = lua_open ();
128
+ if (NULL == L )
129
+ return -1 ;
139
130
140
- /* load Lua base libraries */
131
+ /* load Lua base libraries & our extensions */
141
132
openlualibs (L );
142
133
143
- /* register our functions */
144
- luaopen_wapr (L );
145
-
146
134
/* run the script */
147
135
lua_run_script (L , script_file );
148
136
0 commit comments