fengari-lua / fengari

🌙 φεγγάρι - The Lua VM written in JS ES6 for Node and the browser

Home Page:https://fengari.io/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Can't Put Globals Inside of State

tilkinsc opened this issue · comments

function draw_output(insert, file) {
	
	const luaconf  = fengari.luaconf;
	const lua      = fengari.lua;
	const lauxlib  = fengari.lauxlib;
	const lualib   = fengari.lualib;
	const interop  = fengari.interop;
	
	const L = lauxlib.luaL_newstate();
	
	lualib.luaL_openlibs(L);
	
	var text = "print(assert) test('abc')";
	// const response = await fetch(file);
	// if(response.ok) {
		// text = await response.text();
	// }
	
	function test(L) {
		lauxlib.luaL_checkstring(L, -1);
		console.log("yeet");
		lua.lua_pop(L, -1);
	}
	
	lua.lua_pushjsclosure(L, test, 0);
	lua.lua_setglobal(L, "test");
	
	var base = lua.lua_gettop(L);
	var status = 0;
	status = lauxlib.luaL_loadstring(L, fengari.to_luastring(text));
	if(status != 0) {
		var err = lua.lua_tojsstring(L, -1);
		insert.innerHTML += "failed to load lua file " + file + "<br>" + err + "<br>";
		lua.lua_close(L);
		return;
	}
	
	status = lua.lua_pcall(L, 0, 0, 0);
	console.log(status);
	if(status != 0) {
		var err = lua.lua_tojsstring(L, -1);
		insert.innerHTML += "failed to run lua file " + file + "<br>" + err + "<br>";
		lauxlib.luaL_traceback(L, L, "--", 1);
		insert.innerHTML += lua.lua_tojsstring(L, -1) + "<br>";
		lua.lua_close(L);
		return;
	}
	base = lua.lua_gettop(L) - base;
	console.log(base);
	if(base > 0) {
		insert.innerHTML += "Returned: ";
	}
	while(base > 0) {
		var msg = lua.lua_tojsstring(L, -base);
		insert.innerHTML += msg + " ";
		base--;
	}
	
	lua.lua_close(L);
}

I expect assert to be there. It indeed is. I expect test to be there. It gets ran. Prints yeet. However, any value I set to the 3rd argument to lua_pushjsclosure() I get a hard error.

image

Also, for some reason, lua_pcall keeps returning -1 as status. And lua_gettop(L) swears that there is 1 element on the stack, when there isn't.

image

However, any value I set to the 3rd argument to lua_pushjsclosure() I get a hard error.

Do you have an example of this?

Also, for some reason, lua_pcall keeps returning -1 as status.

This means a JS error was hit; usually via incorrect usage of the API. You can use a snippet such as the following to catch them:

    lua.lua_atnativeerror(L, function(L) {
        let u = lua.lua_touserdata(L, 1);
        console.error("fengari encountered a JS error", u);
        lua.lua_pushstring(L, "native error");
        return 1;
    });

An issue in your code is that your test function should return an integer, probably 0 in your case? Also your pop is redundant.

    function test(L) {
        lauxlib.luaL_checkstring(L, -1); // you would usually use positive integers here
        console.log("yeet");
        lua.lua_pop(L, -1); // returning 0 pops for you; this is redundant
        return 0;
    }

Yeah I knew the pop was redundant. I added it when I was looking at the stack to see whats up. I will work on it now and see if I can't resolve anything. The return is a good suggestion. I forgot about having to return. I am the author of https://github.com/tilkinsc/LuaConsole and this is embarrassing of me :D

My error was indeed the returns missing. Where can I find documentation on using fengari? I had to look at the tests in order to use it.

Where can I find documentation on using fengari?

There isn't any beyond the README in this repository. Much of it is meant to be a direct analogue to the Lua C API so that documentation wouldn't be needed.

Documentation would be nice to tell me how to interface with fengari. For example, in the fengari container, you have a function labeled fengari.lua_tojsstring(lua_State, idx) but it is nowhere to be found. Yet I got errors that came about from the Uint8Array change that was made to like the lua_loadbuffer*(...) function. They were very indirect in that the library would output a type error when passing a string with a quotation notation such as `` or '' or "". It was impossible to google to resolve this.

you have a function labeled fengari.lua_tojsstring(lua_State, idx) but it is nowhere to be found.

https://github.com/fengari-lua/fengari#strings

To get a Lua string on the stack as a JS string you can use lua_tojsstring which will attempt to convert it to a UTF-16 JS string. The latter won't give you what you expect if the Lua string is not a valid UTF-16 sequence. You can also convert strings with luastring_of, to_luastring, to_jsstring and to_uristring.

Oh I must of missed that.

image

Thanks c:

That said, its quite hidden in the readme. I'd be grateful if someone sent a pull request documenting the functions in the fengari module.