luci-base: runtime.uc: avoid Lua not installed exeption from trycompile()

Make sure to request loading the Lua bridge as optional when initializing
the Lua VM context from trycompile() in order to not raise a fatal exception
in case the Lua runtime support is not present.

Ref: https://forum.openwrt.org/t/x/141426
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
This commit is contained in:
Jo-Philipp Wich 2022-11-02 10:17:48 +01:00
parent c99602e4ac
commit 815028ef93

View file

@ -43,14 +43,18 @@ function format_lua_exception(ex) {
} }
const Class = { const Class = {
init_lua: function() { init_lua: function(optional) {
if (!this.L) { if (!this.L) {
this.L = this.env.dispatcher.load_luabridge().create(); let bridge = this.env.dispatcher.load_luabridge(optional);
if (bridge) {
this.L = bridge.create();
this.L.set('L', proto({ write: print }, this.env)); this.L.set('L', proto({ write: print }, this.env));
this.L.invoke('require', 'luci.ucodebridge'); this.L.invoke('require', 'luci.ucodebridge');
this.env.lua_active = true; this.env.lua_active = true;
} }
}
return this.L; return this.L;
}, },
@ -80,10 +84,12 @@ const Class = {
} }
else { else {
try { try {
let vm = this.init_lua(); let vm = this.init_lua(true);
let compile = vm.get('_G', 'luci', 'ucodebridge', 'compile');
compile.call(path); if (vm)
vm.get('_G', 'luci', 'ucodebridge', 'compile').call(path);
else
return `Unable to compile '${path}' as Lua template: Unable to load Lua runtime`;
} }
catch (lua_err) { catch (lua_err) {
return `Unable to compile '${path}' as Lua template: ${format_lua_exception(lua_err)}`; return `Unable to compile '${path}' as Lua template: ${format_lua_exception(lua_err)}`;