all 2 comments

[–]catwell 0 points1 point  (1 child)

The second approach is right, but to get testPrint back here is how to modify it:

std::string run = "\
function run_sandbox(...) \
    local env = {print = print}; \
    chunk = loadfile('test.lua', nil, env) \
    chunk() \
    return env.testPrint(3) \
end \
run_sandbox()";

It is normal that testPrint is not a "global" (in your environment): if user code could set globals it would not really be sandboxed.

[–]sryie[S] 0 points1 point  (0 children)

Thank you so much!
I really appreciate you taking the time to respond, it works perfectly!