aboutsummaryrefslogtreecommitdiff
path: root/doc/api.html
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--doc/api.html147
1 files changed, 141 insertions, 6 deletions
diff --git a/doc/api.html b/doc/api.html
index 79788d95..3bb10967 100644
--- a/doc/api.html
+++ b/doc/api.html
@@ -100,12 +100,6 @@ These functions are typically used with the command line options
100Flushes the whole cache of compiled code. 100Flushes the whole cache of compiled code.
101</p> 101</p>
102 102
103<h3 id="jit_flush_tr"><tt>jit.flush(tr)</tt></h3>
104<p>
105Flushes the code for the specified root trace and all of its
106side traces from the cache.
107</p>
108
109<h3 id="jit_onoff_func"><tt>jit.on(func|true [,true|false])<br> 103<h3 id="jit_onoff_func"><tt>jit.on(func|true [,true|false])<br>
110jit.off(func|true [,true|false])<br> 104jit.off(func|true [,true|false])<br>
111jit.flush(func|true [,true|false])</tt></h3> 105jit.flush(func|true [,true|false])</tt></h3>
@@ -142,6 +136,13 @@ of a module to turn off JIT compilation for the whole module for
142debugging purposes. 136debugging purposes.
143</p> 137</p>
144 138
139<h3 id="jit_flush_tr"><tt>status = jit.flush(tr)</tt></h3>
140<p>
141Tries to flush the code for the specified trace and all of its
142side traces from the cache. Returns <tt>true</tt> on success.
143Returns <tt>false</tt> if there are still links to this trace.
144</p>
145
145<h3 id="jit_version"><tt>jit.version</tt></h3> 146<h3 id="jit_version"><tt>jit.version</tt></h3>
146<p> 147<p>
147Contains the LuaJIT version string. 148Contains the LuaJIT version string.
@@ -189,6 +190,140 @@ The debug modules <tt>-jbc</tt>, <tt>-jv</tt> and <tt>-jdump</tt> make
189extensive use of these functions. Please check out their source code, 190extensive use of these functions. Please check out their source code,
190if you want to know more. 191if you want to know more.
191</p> 192</p>
193
194<h2 id="c_api">C API extensions</h2>
195<p>
196LuaJIT adds some extensions to the Lua/C API. The LuaJIT include
197directory must be in the compiler search path (<tt>-I<i>path</i></tt>)
198to be able to include the required header for C code:
199</p>
200<pre class="code">
201#include "luajit.h"
202</pre>
203<p>
204Or for C++ code:
205</p>
206<pre class="code">
207#include "lua.hpp"
208</pre>
209
210<h2 id="luaJIT_setmode"><tt>luaJIT_setmode(L, idx, mode)</tt>
211&mdash; Control VM</h2>
212<p>
213This is a C API extension to allow control of the VM from C code. The
214full prototype of <tt>LuaJIT_setmode</tt> is:
215</p>
216<pre class="code">
217LUA_API int luaJIT_setmode(lua_State *L, int idx, int mode);
218</pre>
219<p>
220The returned status is either success (<tt>1</tt>) or failure (<tt>0</tt>).
221The second argument is either <tt>0</tt> or a stack index (similar to the
222other Lua/C API functions).
223</p>
224<p>
225The third argument specifies the mode, which is 'or'ed with a flag.
226The flag can be <tt>LUAJIT_MODE_OFF</tt> to turn a feature on,
227<tt>LUAJIT_MODE_ON</tt> to turn a feature off, or
228<tt>LUAJIT_MODE_FLUSH</tt> to flush cached code.
229</p>
230<p>
231The following modes are defined:
232</p>
233
234<h3 id="mode_engine"><tt>luaJIT_setmode(L, 0, LUAJIT_MODE_ENGINE|flag)</tt></h3>
235<p>
236Turn the whole JIT compiler on or off or flush the whole cache of compiled code.
237</p>
238
239<h3 id="mode_func"><tt>luaJIT_setmode(L, idx, LUAJIT_MODE_FUNC|flag)</tt><br>
240<tt>luaJIT_setmode(L, idx, LUAJIT_MODE_ALLFUNC|flag)</tt><br>
241<tt>luaJIT_setmode(L, idx, LUAJIT_MODE_ALLSUBFUNC|flag)</tt></h3>
242<p>
243This sets the mode for the function at the stack index <tt>idx</tt> or
244the parent of the calling function (<tt>idx = 0</tt>). It either
245enables JIT compilation for a function, disables it and flushes any
246already compiled code or only flushes already compiled code. This
247applies recursively to all subfunctions of the function with
248<tt>LUAJIT_MODE_ALLFUNC</tt> or only to the subfunctions with
249<tt>LUAJIT_MODE_ALLSUBFUNC</tt>.
250</p>
251
252<h3 id="mode_engine"><tt>luaJIT_setmode(L, trace,<br>
253&nbsp;&nbsp;LUAJIT_MODE_TRACE|LUAJIT_MODE_FLUSH)</tt></h3>
254<p>
255Tries to flush the code for the specified trace and all of its
256side traces from the cache.
257</p>
258
259<h3 id="mode_engine"><tt>luaJIT_setmode(L, idx, LUAJIT_MODE_WRAPCFUNC|flag)</tt></h3>
260<p>
261This mode defines a wrapper function for calls to C functions. The
262first time this is called with <tt>LUAJIT_MODE_ON</tt>, the stack
263index at <tt>idx</tt> must be a <tt>lightuserdata</tt> object holding
264a pointer to the wrapper function. All <b>subsequently created C
265functions</b> are called through the wrapper functions. After the initial
266definition <tt>idx</tt> can be left at <tt>0</tt> when turning the mode
267on or off.
268</p>
269<p>
270The wrapper function can be used for debugging purposes or to catch
271and convert foreign exceptions. Recommended usage can be seen in this
272C++ code excerpt:
273</p>
274<pre class="code">
275#include &lt;exception&gt;
276#include "lua.hpp"
277
278// Catch C++ exceptions and convert them to Lua error messages.
279// Customize as needed for your own exception classes.
280static int wrap_exceptions(lua_State *L, lua_CFunction f)
281{
282 try {
283 return f(L); // Call wrapped function and return result.
284 } catch (const char *s) { // Catch and convert exceptions.
285 lua_pushstring(L, s);
286 } catch (std::exception& e) {
287 lua_pushstring(L, e.what());
288 } catch (...) {
289 lua_pushliteral(L, "caught (...)");
290 }
291 return lua_error(L); // Rethrow as a Lua error.
292}
293
294static int myregister(lua_State *L)
295{
296 ...
297 // Define wrapper function and enable it.
298 lua_pushlightuserdata(L, (void *)wrap_exceptions);
299 luaJIT_setmode(L, -1, LUAJIT_MODE_WRAPCFUNC|LUAJIT_MODE_ON);
300 lua_pop(L, 1);
301 luaL_register(L, "mymodule", myfuncs); // Pass luaL_Reg list.
302 luaJIT_setmode(L, 0, LUAJIT_MODE_WRAPCFUNC|LUAJIT_MODE_OFF);
303 ...
304 // Wrap some more C++ functions which might throw an exception.
305 luaJIT_setmode(L, 0, LUAJIT_MODE_WRAPCFUNC|LUAJIT_MODE_ON);
306 lua_pushcfunction(L, mythrowingfunc1);
307 lua_pushcclosure(L, mythrowingfunc2, 1);
308 luaJIT_setmode(L, 0, LUAJIT_MODE_WRAPCFUNC|LUAJIT_MODE_OFF);
309 ...
310}
311</pre>
312<p>
313Note that you can only define <b>a single global wrapper function</b>,
314so be careful when using this mechanism from multiple C++ modules.
315Also note that this mechanism is not without overhead. It should only
316be enabled for definitions of C++ functions that can actually throw
317exceptions. If you're embedding LuaJIT into an application, only
318enable it <b>after</b> running <tt>luaL_openlibs</tt>.
319</p>
320<p>
321LuaJIT already intercepts exception handling for systems using
322ELF/DWARF2 stack unwinding (e.g. Linux). This is a zero-cost mechanism
323and always enabled. You don't need to use any wrapper functions,
324except when you want to get a more specific error message than
325<tt>"C++&nbsp;exception"</tt>.
326</p>
192<br class="flush"> 327<br class="flush">
193</div> 328</div>
194<div id="foot"> 329<div id="foot">