Skip to content

SEA - Compile to shared library #23

@codebycruz

Description

@codebycruz

I don't think this has been explored by any scripting language, let alone lua.

Typically the extent of ffi in scripting languages is calling out to C code. But the inverse is not possible without explicitly creating an interpreter to manually run the script.

This isn't as simple as creating a library in a systems language or statically typed language, as functions are not statically known or analyzable (well, they technically can be, but it's not expected for upheld by lua).

Exposing a function to C via Rust

#[no_mangle]
extern "C" fn my_function() {}

Possible lua api:

---@export "C" foo fun(a: uint32_t, b: uint32_t): uint32_t

This would be statically scanned for by the SEA library, and generate functions that take the function's type signature, convert them to lua and pass to the function running with the same interpreter, and compile.

Rough generated code (lua api is probably wrong but close enough)

uint32_t foo(uint32_t a, uint32_t b) {
    l = <get the state from some singleton, ensure same thread>
    lua_pushnumber(l, a);
    lua_pushnumber(l, b);
    lua_getglobal(l, "foo");
    lua_call(l, 1, 2);
    uint32_t r = lua_tonumber(l, -1);
    return r;
}

This would allow for writing things like native plugins for applications in lua, and would be pretty novel. As previously stated, I don't think anyone has done this.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions