function compile_lua() { var text = texteditor.get_text(); let lua = require('tenshi/vendor-js/lua'); let lua_state = lua.ccall('luaL_newstate', 'number'); if (lua.ccall('luaL_loadstring', 'number', ['number', 'string'], [lua_state, text]) !== 0) { // TODO(rqou): Report the actual error throw "Didn't load properly!"; } let lua_bytecode = new Uint8Array(0); function lua_dump_callback(L, p, sz, ud) { let lua_bytecode_new = new Uint8Array(lua_bytecode.length + sz); lua_bytecode_new.set(lua_bytecode); lua_bytecode_new.set( lua.HEAPU8.subarray(p, p + sz), lua_bytecode.length); lua_bytecode = lua_bytecode_new; } let lua_dump_callback_ptr = lua.Runtime.addFunction(lua_dump_callback); if (lua.ccall('lua_dump', 'number', ['number', 'number', 'number'], [lua_state, lua_dump_callback_ptr, 0]) !== 0) { throw "Didn't dump properly!"; } lua.ccall('lua_close', null, ['number'], [lua_state]); lua.Runtime.removeFunction(lua_dump_callback_ptr); naive_packetizer.sendPacketizedData(lua_bytecode); }
it("should be able to use luaify", function() { function testfunc(a, b, c) { return (a + b) + c; } let testfunc_lua = lua_langsupp.luaify(testfunc); let testfunc_ptr = lua_emcc.Runtime.addFunction(testfunc_lua); let L = luaL_newstate(); lua_pushcclosure(L, testfunc_ptr); lua_langsupp.js_to_lua(L, 1); lua_langsupp.js_to_lua(L, 3); lua_langsupp.js_to_lua(L, "hi"); lua_callk(L, 3, 1, 0, 0); let val = lua_langsupp.lua_to_js(L, -1); lua_close(L); lua_emcc.Runtime.removeFunction(testfunc_ptr); expect(val).toEqual("4hi"); });