Files
Promptly/public/js/lua-bridge.js
T

157 lines
5.1 KiB
JavaScript

// Мост Lua ↔ JS для интерактивных презентаций.
// Использует глобальный fengari (vendor/fengari-web.js).
(function () {
const { lua, lauxlib, lualib, to_luastring, to_jsstring } = window.fengari;
const T = lua;
function readValue(L, idx) {
const type = T.lua_type(L, idx);
if (type === T.LUA_TBOOLEAN) return !!T.lua_toboolean(L, idx);
if (type === T.LUA_TNUMBER) return T.lua_tonumber(L, idx);
if (type === T.LUA_TSTRING) {
const s = T.lua_tostring(L, idx);
return s ? to_jsstring(s) : '';
}
return null;
}
function pushValue(L, value) {
if (typeof value === 'number' && Number.isFinite(value)) T.lua_pushnumber(L, value);
else if (typeof value === 'boolean') T.lua_pushboolean(L, value);
else if (typeof value === 'string') T.lua_pushstring(L, to_luastring(value));
else T.lua_pushnil(L);
}
function jsString(L, idx) {
const s = T.lua_tostring(L, idx);
return s ? to_jsstring(s) : '';
}
function createRuntime(hooks = {}) {
const L = lauxlib.luaL_newstate();
lualib.luaL_openlibs(L);
const noop = () => {};
const h = {
go: hooks.go || noop,
next: hooks.next || noop,
prev: hooks.prev || noop,
play: hooks.play || noop,
stop: hooks.stop || noop,
volume: hooks.volume || noop,
music: hooks.music || noop,
musicPlay: hooks.musicPlay || noop,
musicStop: hooks.musicStop || noop,
musicVolume: hooks.musicVolume || noop,
videoPlay: hooks.videoPlay || noop,
videoPause: hooks.videoPause || noop,
videoRestart: hooks.videoRestart || noop,
videoSeek: hooks.videoSeek || noop,
currentSlide: hooks.currentSlide || (() => 1),
log: hooks.log || ((msg) => console.log('[lua]', msg)),
error: hooks.error || ((msg) => console.error('[lua]', msg)),
};
const setFn = (name, fn) => {
T.lua_pushcfunction(L, (state) => {
try {
return fn(state) || 0;
} catch (err) {
h.error(String(err && err.message ? err.message : err));
return 0;
}
});
T.lua_setfield(L, -2, to_luastring(name));
};
// app.*
T.lua_newtable(L);
setFn('go', (Ls) => {
h.go(Math.max(0, Math.floor(readValue(Ls, 1) || 1) - 1));
return 0;
});
setFn('next', () => (h.next(), 0));
setFn('prev', () => (h.prev(), 0));
setFn('play', (Ls) => (h.play(jsString(Ls, 1)), 0));
setFn('stop', (Ls) => (h.stop(jsString(Ls, 1)), 0));
setFn('volume', (Ls) => (h.volume(jsString(Ls, 1), readValue(Ls, 2)), 0));
setFn('music', (Ls) => (h.music(jsString(Ls, 1)), 0));
setFn('musicPlay', () => (h.musicPlay(), 0));
setFn('musicStop', () => (h.musicStop(), 0));
setFn('musicVolume', (Ls) => (h.musicVolume(readValue(Ls, 1)), 0));
setFn('videoPlay', (Ls) => (h.videoPlay(jsString(Ls, 1)), 0));
setFn('videoPause', (Ls) => (h.videoPause(jsString(Ls, 1)), 0));
setFn('videoRestart', (Ls) => (h.videoRestart(jsString(Ls, 1)), 0));
setFn('videoSeek', (Ls) => (h.videoSeek(jsString(Ls, 1), readValue(Ls, 2)), 0));
setFn('slide', (Ls) => (T.lua_pushnumber(Ls, h.currentSlide()), 1));
setFn('log', (Ls) => (h.log(jsString(Ls, 1)), 0));
T.lua_setglobal(L, to_luastring('app'));
// vars
T.lua_newtable(L);
T.lua_setglobal(L, to_luastring('vars'));
function setVars(obj = {}) {
T.lua_getglobal(L, to_luastring('vars'));
for (const key of Object.keys(obj)) {
pushValue(L, obj[key]);
T.lua_setfield(L, -2, to_luastring(key));
}
T.lua_pop(L, 1);
}
function getVars() {
const result = {};
T.lua_getglobal(L, to_luastring('vars'));
const tableIndex = T.lua_gettop(L);
T.lua_pushnil(L);
while (T.lua_next(L, tableIndex) !== 0) {
const key = T.lua_tostring(L, -2);
if (key) result[to_jsstring(key)] = readValue(L, -1);
T.lua_pop(L, 1);
}
T.lua_pop(L, 1);
return result;
}
T.lua_getglobal(L, to_luastring('app'));
setFn('setVar', (Ls) => {
T.lua_getglobal(L, to_luastring('vars'));
pushValue(Ls, readValue(Ls, 2));
T.lua_setfield(Ls, -2, T.lua_tostring(Ls, 1));
T.lua_pop(Ls, 1);
return 0;
});
setFn('getVar', (Ls) => {
T.lua_getglobal(Ls, to_luastring('vars'));
T.lua_getfield(Ls, -1, T.lua_tostring(Ls, 1));
T.lua_remove(Ls, -2);
return 1;
});
T.lua_pop(L, 1);
function run(code, label = 'script') {
if (!code || !String(code).trim()) return getVars();
const status = lauxlib.luaL_dostring(L, to_luastring(String(code)));
if (status !== T.LUA_OK) {
const err = T.lua_tostring(L, -1);
h.error(`Lua (${label}): ${err ? to_jsstring(err) : 'unknown error'}`);
T.lua_pop(L, 1);
}
return getVars();
}
function reset(obj = {}) {
T.lua_newtable(L);
T.lua_setglobal(L, to_luastring('vars'));
setVars(obj);
}
reset(hooks.vars || {});
return { run, getVars, setVars, reset };
}
window.createLuaRuntime = createRuntime;
})();