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

Overwrite the behavior of require? #233

Open
stautonico opened this issue Apr 4, 2023 · 1 comment
Open

Overwrite the behavior of require? #233

stautonico opened this issue Apr 4, 2023 · 1 comment

Comments

@stautonico
Copy link

I would like to be able to overwrite the behavior of the require function. I want to be able to manually provide the lua functions (preferably via a string rather than from a file on the filesystem) since I am running in a very isolated environment that should have no filesystem access. How can I do this?

@stautonico
Copy link
Author

stautonico commented Apr 4, 2023

This is what I came up with. It seems to work, but I'm not sure if it'll be reliable in the long run

 self.lua.globals().require = self.require


def require(self, module_name):
    # Check if the module is already loaded
    if module_name in self.lua.globals().package.loaded:
        return self.lua.globals().package.loaded[module_name]

    # Try to load the .lua file from our custom directory (for debugging, we can get the source of the lua module however we want)
    try:
        with open(f"/tmp/{module_name}.lua", "r") as f:
            module_code = f.read()
    except FileNotFoundError:
        raise ModuleNotFoundError(f"module '{module_name}' not found")

    # Return the module
    self.lua.globals().package.loaded[module_name] = self.lua.execute(module_code)
    return self.lua.globals().package.loaded[module_name]

Also, are there docs for this library? I can't seem to find any and they would really help

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant