You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have this app in which users can provide their lua scripts. I don't have those in the filesystem so I need to intercept require calls somehow (I will be returning them from the database or as chunks from ETS).
An option is to override require itself, but I think that adding a searcher after the preload one is more lua-esque.
In case it's better to just override require, how would yo approach returning preloaded modules?
Basically I don't know if luerl provides a mechanism to hook into require and if we can wrap it in this lib.
This works, I can return anything from the imported script:
defmoduleErluaTestdouseExUnit.CasedoctestErluaimportLuadefmoduleCustomdo@imports%{"custom"=>~LUA""" return { greet = function(name) print("Printed hello, " .. name) end, template = function(name) return "Templating hello, " .. name end }"""}defimport([],[path],state)do{:ok,ret,state}=Luerl.New.do(state,@imports[path]){ret,state}endendtest"Loading lua scripts dynamically"doentrypoint=~LUA""" print("I am the entrypoint") local imported = import("custom") imported.greet("world") print("gonna print a rendered template: " .. imported.template("world"))"""result=Lua.new()|>Lua.set!([:import],{Custom,:import,[]})|>Lua.eval!(entrypoint)endend
But as you can see I had to use Luerl do, you can't use eval! there.
Also, conceptually I don't know if the import function should reuse the state or use a blank slate. I like the latter to avoid unexpected side effects.
is there still interest on this?
If you want to hook into the lua require you have to write a searcher and loader.
I did it here, feel free to copy.
There you can do stuff like require([[luerl_ext_json]]) and get a table returned by the table/2 of the registered beam module.
I would be down to writing a PR for this, assuming I understand the issue correctly.
Just to be clear, you are looking for a function like setup_require(lua_vm, [ api_module ]) :: lua_vm, where an api_module is an elixir module implementing the Lua.Api behaviour, correct?
Imagine an app in which users can define their own lua modules.
Those are parsed and stored as chunks somewhere (in the db as binary, on ETS, doesn't matter).
When require is called, we can hook into it and try to find what they want to load and return that. Perhaps if we don't find it, then we can fallback to regular require (which would hit the filesystem). But there aren't Elixir modules involved here.
Activity
Papipo commentedon Aug 8, 2024
Alternatively, creating a separate function (ie.
import) that works like require but allows to return modules, that's also fine.Papipo commentedon Aug 9, 2024
This works, I can return anything from the imported script:
But as you can see I had to use Luerl
do, you can't useeval!there.Also, conceptually I don't know if the import function should reuse the state or use a blank slate. I like the latter to avoid unexpected side effects.
davydog187 commentedon Aug 23, 2024
Hello @Papipo, I'm still thinking about ways to do this. I'll get back to you
iggi42 commentedon Jun 12, 2025
is there still interest on this?
If you want to hook into the lua
requireyou have to write a searcher and loader.I did it here, feel free to copy.
There you can do stuff like
require([[luerl_ext_json]])and get a table returned by thetable/2of the registered beam module.davydog187 commentedon Jun 13, 2025
I will look into this, thanks!
iggi42 commentedon Jun 22, 2025
I would be down to writing a PR for this, assuming I understand the issue correctly.
Just to be clear, you are looking for a function like
setup_require(lua_vm, [ api_module ]) :: lua_vm, where anapi_moduleis an elixir module implementing theLua.Apibehaviour, correct?soo it can be used in lua
davydog187 commentedon Jun 22, 2025
https://hexdocs.pm/lua/Lua.html#load_api/3 already exists for that purpose. I believe the ask is a bit different
Papipo commentedon Jun 23, 2025
Imagine an app in which users can define their own lua modules.
Those are parsed and stored as chunks somewhere (in the db as binary, on ETS, doesn't matter).
When
requireis called, we can hook into it and try to find what they want to load and return that. Perhaps if we don't find it, then we can fallback to regular require (which would hit the filesystem). But there aren't Elixir modules involved here.iggi42 commentedon Jun 23, 2025
yeah,
you can already do that, there is no need to change this project for that, imo.
See package.searchers in lua for how to.
Papipo commentedon Jul 3, 2025
@davydog187 if what iggi says is true then this can be closed.
davydog187 commentedon Jul 3, 2025
There's quite a bit of documentation I'd like to add on this topic. So ill close when that is done @Papipo