Skip to content

[Question] Access to the lua environment. #238

@uwukle

Description

@uwukle

I can't find any information about getting the current environment of the lua being run. I set my environment when lua loads, but I can't get it when a function is executed. Is this possible?

LuaState state = LuaState.Create();

// I create a specific environment so that different blocks of lua code can run independently.
LuaTable environment = new(0, 1);
enviroment["myfunc"] = new LuaFunction("myfunc", MyFunction);

// And now I will set the environment.
LuaClosure closure = state.Load("myfunc()", "anonymous", enviroment);

_ = await state.ExecuteAsync(closure);
private static ValueTask<int> MyFunction(LuaFunctionExecutionContext context, CancellationToken cancellationToken = default)
{
    LuaTable environment = /* I need to get the current environment. */
    // Some code here...
    return new(context.Return());
}

I don't understand the concept of environments at all. Standard functions are loaded through the global environment and are linked to LuaState, but during execution, I can specify my own environment, which won't contain any references to the global one.
To run code in isolation, do I need to create unique LuaStates?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions