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

Using debug hook to get a function name and argument(s) #528

Open
JD802 opened this issue Mar 8, 2024 · 0 comments
Open

Using debug hook to get a function name and argument(s) #528

JD802 opened this issue Mar 8, 2024 · 0 comments

Comments

@JD802
Copy link

JD802 commented Mar 8, 2024

Hello,

First of all, thank you for NLua, it's a wonderful project that has provided enormously useful for us.

We are using sandboxed NLua that executes user generated scripts on server side. I have setup a Lua debug hook to kill scripts that are running too long, and this works fine. I'd now like to optimize specific Lua function calls that users's scripts may contain, but I'm unsure how to set it up.

Example case

Lua function that needs to be optimized: GetParamValue(paramName) -> returns either string, integer, or float value.

I need to check whether a specific Lua function (GetParamValue) is invoked by utilizing a debug hook event handler. If the specific Lua function is called, it's crucial to examine the argument (paramName). In certain scenarios, which are not detailed here, executing Lua code within the C# event handler is necessary before allowing the execution of the user's Lua script to proceed to completion.

Is this possible to do in NLua? If yes, could someone more experienced provide some pointers?

NLua Initialization and event Subscription

_lua = new NLua.Lua();
_lua.State.Encoding = Encoding.UTF8;
_timeOutInMs = timeOutInMs;

_lua.DebugHook += LuaDebugHook;
_lua.SetDebugHook(KeraLua.LuaHookMask.Call, 1);
_lua.SetDebugHook(KeraLua.LuaHookMask.Line, 1);

Event handler

`        private void LuaDebugHook(object sender, NLua.Event.DebugHookEventArgs e)
        {

            if (_cancellationToken.IsCancellationRequested)
            {
                _lua.Push("Script execution cancelled");
                _lua.State.Error("Script execution cancelled");
                return;
            }


            if (e.LuaDebug.Event == KeraLua.LuaHookEvent.Call)
            {
		//TODO: check the name of the function, and its argument(s)
            }

            if (!_useTimeOut)
            {
                return;
            }


            var elapsedMs = Math.Round(Stopwatch.GetElapsedTime(_scriptStartTime).TotalMilliseconds);
            if (elapsedMs >= _timeOutInMs)
            {
                _lua.Push("Script execution timed out");
                _lua.State.Error("Script execution timed out");
                return;
            }
        }`
@JD802 JD802 changed the title Using debug hooks to get a function parameter Using debug hooks to get a function argument Mar 8, 2024
@JD802 JD802 changed the title Using debug hooks to get a function argument Using debug hook to get a function name and argument Mar 8, 2024
@JD802 JD802 changed the title Using debug hook to get a function name and argument Using debug hook to get a function name and argument(s) Mar 8, 2024
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