Re: unprotected error in call to Lua API

Alexey Tourbin <[email protected]>
Newsgroups gmane.comp.lang.lua.general,gmane.comp.web.links
Message-ID <[email protected]>
On Mon, Apr 16, 2007 at 04:11:33PM -0400, Jerome Vuarand wrote:
> Alexey Tourbin wrote:
> > What is wrong with the following code?
> > 
> > #include <lua.h>
> > #include <lualib.h>
> > #include <lauxlib.h>
> > int main()
> > {
> >         lua_State *L = lua_open();
> >         luaopen_base(L);
> >         luaopen_table(L);
> >         luaopen_io(L);
> >         luaopen_string(L);
> >         luaopen_math(L);
> >         return 0;
> > }
> > 
> > [...]
> > 
> > lua-5.1.2
> 
> Manual, section 7.3:
> 
> http://www.lua.org/manual/5.1/manual.html#7.3
> 
> "The luaopen_* functions (to open libraries) cannot be called directly,
> like a regular C function. They must be called through Lua, like a Lua
> function."

Thanks.  Actually I'm trying to fix the ELinks web browser code.
I guess using luaL_openlibs() is just okay, but it probably requires
lua >= 5.1.

commit 8c4596096c122cc9fa2eba1e35f7e13f0b69039c
Author: Alexey Tourbin <[email protected]>
Date:   Mon Apr 16 22:55:32 2007 +0400

    elinks-0.11.1-alt-lua51.patch
    
    Ported to lua-5.1.  This also implies some compatibility fixes in lua5
    package, i.e. liblualib.so -> liblua.so (so I do not alter configre.in
    about it).
    
    (cherry picked from commit ce2d9e404543caf7f37aca6a73dc05f4e2d164b7)
    
    Conflicts:
    
    	configure.in

diff --git a/configure.in b/configure.in
index cba0c81..bcbcacf 100644
--- a/configure.in
+++ b/configure.in
@@ -907,11 +907,7 @@ if test -z "$disable_lua"; then
 				AC_TRY_LINK([	#include <lua.h>
 						#include <lualib.h>],
 					    [	lua_State *L = lua_open();
-						luaopen_base(L);
-						luaopen_table(L);
-						luaopen_io(L);
-						luaopen_string(L);
-						luaopen_math(L);
+						luaL_openlibs(L);
 						lua_pushboolean(L, 1);
 						lua_close(L);],
 					    cf_result=yes, cf_result=no)
diff --git a/src/scripting/lua/core.c b/src/scripting/lua/core.c
index fcd7176..7fa4405 100644
--- a/src/scripting/lua/core.c
+++ b/src/scripting/lua/core.c
@@ -655,7 +655,7 @@ do_hooks_file(LS, unsigned char *prefix, unsigned char *filename)
 	if (file_can_read(file)) {
 		int oldtop = lua_gettop(S);
 
-		if (lua_dofile(S, file) != 0)
+		if (luaL_dofile(S, file) != 0)
 			sleep(3); /* Let some time to see error messages. */
 		lua_settop(S, oldtop);
 	}
@@ -667,12 +667,7 @@ void
 init_lua(struct module *module)
 {
 	L = lua_open();
-
-	luaopen_base(L);
-	luaopen_table(L);
-	luaopen_io(L);
-	luaopen_string(L);
-	luaopen_math(L);
+	luaL_openlibs(L);
 
 	lua_register(L, LUA_ALERT, l_alert);
 	lua_register(L, "current_url", l_current_url);
@@ -777,7 +772,7 @@ handle_ret_eval(struct session *ses)
 		int oldtop = lua_gettop(L);
 
 		if (prepare_lua(ses) == 0) {
-			lua_dostring(L, expr);
+			(void)luaL_dostring(L, expr);
 			lua_settop(L, oldtop);
 			finish_lua();
 		}
diff --git a/src/scripting/lua/hooks.c b/src/scripting/lua/hooks.c
index d79ad80..47c0091 100644
--- a/src/scripting/lua/hooks.c
+++ b/src/scripting/lua/hooks.c
@@ -200,7 +200,7 @@ static enum evhook_status
 script_hook_quit(va_list ap, void *data)
 {
 	if (!prepare_lua(NULL)) {
-		lua_dostring(lua_state, "if quit_hook then quit_hook() end");
+		(void)luaL_dostring(lua_state, "if quit_hook then quit_hook() end");
 		finish_lua();
 	}
signature.asc (application/pgp-signature, 189 B)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.7 (GNU/Linux)

iD8DBQFGI+WffBKgtDjnu0YRAp+YAJ4px3E+3zoLyrKBE8FI5JFhAI3KOgCg4+Yr
AeGpFSyndXEwqIetE7NVSTY=
=0Sue
-----END PGP SIGNATURE-----
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.