0
0
mirror of https://github.com/openwrt/luci.git synced 2025-01-30 14:51:49 +00:00
Jo-Philipp Wich 70ff5c3c4a contrib: introduce ucode-mod-lua
The ucode-mod-lua library provides an ucode-to-Lua bridge and a set of
functions to instantiate Lua VMs, invoke Lua functions as well as
exchanging data structures between ucode and Lua.

Example usage:

    #!/usr/bin/ucode

    'use strict';

    const lua = require("lua");

    let vm = lua.create();

    vm.set({
    	hello: function(...args) {
    		print(`A ucode "Hello world" function called from Lua! Got arguments: ${args}\n`);
    	},

    	data_from_ucode: {
    		bool: true,
    		float: 1.3,
    		int: 0x11223344,
    		string: "Hello from ucode!",
    		array: [ 1, 2, 3, null, 5 ],
    		object: {
    			apple: "green",
    			banana: "yellow",
    			[5]: "foo",
    			[-1]: null,
    			nested: {
    				a: [ 5, 6 ],
    				b: { c: NaN }
    			}
    		},
    		regexp: /foo/
    	}
    });

    vm.invoke("hello", true, 123, "Foo");
    vm.eval('print("Print from Lua!", data_from_ucode.int * data_from_ucode.float);');

    try {
    	vm.invoke("error", "Throwing a Lua exception...");
    }
    catch (e) {
    	print(`Caught exception: ${e}\n`);
    }

    print(`Lua VM version is: ${vm.get('_G', '_VERSION').value()}\n`);

Signed-off-by: Jo-Philipp Wich <jo@mein.io>
2022-05-30 14:25:33 +02:00

32 lines
729 B
Makefile

include $(TOPDIR)/rules.mk
PKG_NAME:=ucode-mod-lua
PKG_RELEASE:=1
PKG_LICENSE:=ISC
PKG_MAINTAINER:=Jo-Philipp Wich <jo@mein.io>
include $(INCLUDE_DIR)/package.mk
define Package/ucode-mod-lua
SECTION:=utils
CATEGORY:=Utilities
TITLE:=ucode to Lua bridge library
DEPENDS:=+libucode +liblua
endef
define Package/ucode-mod-lua/install
$(INSTALL_DIR) $(1)/usr/lib/ucode
$(CP) $(PKG_BUILD_DIR)/lua.so $(1)/usr/lib/ucode/
endef
define Build/Configure
endef
define Build/Compile
$(TARGET_CC) $(TARGET_CFLAGS) $(TARGET_LDFLAGS) $(FPIC) \
-Wall -ffunction-sections -Wl,--gc-sections -shared -Wl,--no-as-needed -llua \
-o $(PKG_BUILD_DIR)/lua.so $(PKG_BUILD_DIR)/lua.c
endef
$(eval $(call BuildPackage,ucode-mod-lua))