О библиотеке LuaJ - PullRequest
       29

О библиотеке LuaJ

0 голосов
/ 07 апреля 2020

Я не могу понять документ библиотеки Luaj, как насчет песочницы. Какой смысл этих программ в коде здесь ? Я так запутался:

LuaValue sethook = user_globals.get("debug").get("sethook");
        user_globals.set("debug", LuaValue.NIL);

        // Set up the script to run in its own lua thread, which allows us
        // to set a hook function that limits the script to a specific number of cycles.
        // Note that the environment is set to the user globals, even though the
        // compiling is done with the server globals.
        LuaValue chunk = server_globals.load(script, "main", user_globals);
        LuaThread thread = new LuaThread(user_globals, chunk);

        // Set the hook function to immediately throw an Error, which will not be
        // handled by any Lua code other than the coroutine.
        LuaValue hookfunc = new ZeroArgFunction() {
            public LuaValue call() {
                // A simple lua error may be caught by the script, but a
                // Java Error will pass through to top and stop the script.
                throw new Error("Script overran resource limits.");
            }
        };
        final int instruction_count = 20;
        sethook.invoke(LuaValue.varargsOf(new LuaValue[] { thread, hookfunc,
                        LuaValue.EMPTYSTRING, LuaValue.valueOf(instruction_count) }));

        // When we resume the thread, it will run up to 'instruction_count' instructions
        // then call the hook function which will error out and stop the script.
        Varargs result = thread.resume(LuaValue.NIL);
        System.out.println("[["+script+"]] -> "+result);
...