Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add table created by lua_bundle to namespace #35

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

kedorlaomer
Copy link

The name is @@@. Therefore, the table doesn't pollute the namespace
unless the programmer deliberately uses _ENV["@@@"] for his own
values.

This facility is helpful for interoperability with luaproc:
Every thread can decide to receive some or all values values from this table
and load them to access
functionality otherwise only available to the main thread.

Questions:

  • Can this be implemented in a better or more generic way?
  • Specifically, should one add a switch to enable or disable this feature?
  • Or to choose the variable name?

The name is @@@. Therefore, the table doesn't pollute the namespace
unless the programmer deliberately uses _ENV["@@@"] for his own
values.

This facility is helpful for interoperability with luaproc: Every thread
could receive some or all values values from this table to access
functionality otherwise only available to the main thread.
@ers35
Copy link
Owner

ers35 commented Mar 2, 2021

Please give a code example of using this table with luaproc.

@kedorlaomer
Copy link
Author

The main thread has a pure lua module module:

-- file module.lua
local M = {}
function M.some_function(arg1, arg2)
-- do something cool
end

return M

We load this module as module = require "module" and compile its source via luastatic into the bundle.

Now, several of my threads might want to use it, e. g. a function g that will later be called as luaproc.newproc(g).

function g()
-- oh no, how do I get access to `module`?
end

So we need to load the string representation of module:

local module_source = _ENV['@@@'].module
g = function() -- this is closed over the environment, can access module_source
    local module = load(module_source)()
    module.some_function("foo", "bar)
end

Caveats:

  1. This could also be implemented by dumping (via string.dump) all functions from module, although constants need to be dumped in a different way. Of course, the solution via _ENV['@@@'] is a bit more elegant.
  2. This doesn't work for C functions.

I am actually using the patched version in an internal project (which I will gladly point to once it's not too embarassing).

@kedorlaomer
Copy link
Author

For future searches: There's a somewhat ugly solution for C functions. In luaproc/luaproc.c, there's a function luaproc_openlualibs where additional C libraries can be registered. (Sorry for spamming this issue.)

@ers35 ers35 force-pushed the master branch 3 times, most recently from 203babe to c3e1b2e Compare April 30, 2022 17:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants