Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Does it specifically need to call the Lua standard library <code>print</code> function? Can you instead emulate the functionality of <code>print</code>? Because that would be the easiest way.</p> <p>However, if you want to have a wrapper around <code>print</code>, there are two ways to do it: with pure Lua code, and with C/C++ code.</p> <p>The pure Lua solution is as follows. Note that this should be done before loading any external scripts. First, open the Lua standard library that has <code>print</code> in it. Then run this Lua script:</p> <pre><code>local internal_print = print return function(...) --Do display logic. internal_print(...) --Or whatever else you want. end </code></pre> <p>This will return the "display" function. You can store it in a global variable called <code>display</code> if you like, or called something else.</p> <p>After that, you can <code>nil</code> out the <code>print</code> global variable, thus making it almost entirely inaccessible.</p> <p>If you want to do it from C/C++, it's very similar. First, as before, you register the Lua standard library that includes <code>print</code>, so that you can get the function for it. Then, you use <code>lua_getglobal(L, "print")</code> to get the <code>print</code> function and push it onto the stack. Next, you register your C/C++ function using using <code>lua_pushcclosure</code>. But you want to specify one upvalue, which Lua pops off the stack at registration time.</p> <p>And now your registered function is on the stack, waiting to be pushed into a Lua variable or global table entry.</p> <p><strong>Warning:</strong> the Lua debug library can poke at upvalues and thus get the <code>print</code> function from your new function. So if you want perfect security, get rid of <code>debug.getupvalue</code>.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload