Bump copyright date to 2018.
[luajit-2.0.git] / doc / ext_c_api.html
blob3825956619586005e9b8f113a781766200cbef1b
1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
2 <html>
3 <head>
4 <title>Lua/C API Extensions</title>
5 <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
6 <meta name="Copyright" content="Copyright (C) 2005-2018">
7 <meta name="Language" content="en">
8 <link rel="stylesheet" type="text/css" href="bluequad.css" media="screen">
9 <link rel="stylesheet" type="text/css" href="bluequad-print.css" media="print">
10 </head>
11 <body>
12 <div id="site">
13 <a href="http://luajit.org"><span>Lua<span id="logo">JIT</span></span></a>
14 </div>
15 <div id="head">
16 <h1>Lua/C API Extensions</h1>
17 </div>
18 <div id="nav">
19 <ul><li>
20 <a href="luajit.html">LuaJIT</a>
21 <ul><li>
22 <a href="http://luajit.org/download.html">Download <span class="ext">&raquo;</span></a>
23 </li><li>
24 <a href="install.html">Installation</a>
25 </li><li>
26 <a href="running.html">Running</a>
27 </li></ul>
28 </li><li>
29 <a href="extensions.html">Extensions</a>
30 <ul><li>
31 <a href="ext_ffi.html">FFI Library</a>
32 <ul><li>
33 <a href="ext_ffi_tutorial.html">FFI Tutorial</a>
34 </li><li>
35 <a href="ext_ffi_api.html">ffi.* API</a>
36 </li><li>
37 <a href="ext_ffi_semantics.html">FFI Semantics</a>
38 </li></ul>
39 </li><li>
40 <a href="ext_jit.html">jit.* Library</a>
41 </li><li>
42 <a class="current" href="ext_c_api.html">Lua/C API</a>
43 </li></ul>
44 </li><li>
45 <a href="status.html">Status</a>
46 <ul><li>
47 <a href="changes.html">Changes</a>
48 </li></ul>
49 </li><li>
50 <a href="faq.html">FAQ</a>
51 </li><li>
52 <a href="http://luajit.org/performance.html">Performance <span class="ext">&raquo;</span></a>
53 </li><li>
54 <a href="http://wiki.luajit.org/">Wiki <span class="ext">&raquo;</span></a>
55 </li><li>
56 <a href="http://luajit.org/list.html">Mailing List <span class="ext">&raquo;</span></a>
57 </li></ul>
58 </div>
59 <div id="main">
60 <p>
61 LuaJIT adds some extensions to the standard Lua/C API. The LuaJIT include
62 directory must be in the compiler search path (<tt>-I<i>path</i></tt>)
63 to be able to include the required header for C code:
64 </p>
65 <pre class="code">
66 #include "luajit.h"
67 </pre>
68 <p>
69 Or for C++ code:
70 </p>
71 <pre class="code">
72 #include "lua.hpp"
73 </pre>
75 <h2 id="luaJIT_setmode"><tt>luaJIT_setmode(L, idx, mode)</tt>
76 &mdash; Control VM</h2>
77 <p>
78 This is a C API extension to allow control of the VM from C code. The
79 full prototype of <tt>LuaJIT_setmode</tt> is:
80 </p>
81 <pre class="code">
82 LUA_API int luaJIT_setmode(lua_State *L, int idx, int mode);
83 </pre>
84 <p>
85 The returned status is either success (<tt>1</tt>) or failure (<tt>0</tt>).
86 The second argument is either <tt>0</tt> or a stack index (similar to the
87 other Lua/C API functions).
88 </p>
89 <p>
90 The third argument specifies the mode, which is 'or'ed with a flag.
91 The flag can be <tt>LUAJIT_MODE_OFF</tt> to turn a feature off,
92 <tt>LUAJIT_MODE_ON</tt> to turn a feature on, or
93 <tt>LUAJIT_MODE_FLUSH</tt> to flush cached code.
94 </p>
95 <p>
96 The following modes are defined:
97 </p>
99 <h3 id="mode_engine"><tt>luaJIT_setmode(L, 0, LUAJIT_MODE_ENGINE|flag)</tt></h3>
101 Turn the whole JIT compiler on or off or flush the whole cache of compiled code.
102 </p>
104 <h3 id="mode_func"><tt>luaJIT_setmode(L, idx, LUAJIT_MODE_FUNC|flag)</tt><br>
105 <tt>luaJIT_setmode(L, idx, LUAJIT_MODE_ALLFUNC|flag)</tt><br>
106 <tt>luaJIT_setmode(L, idx, LUAJIT_MODE_ALLSUBFUNC|flag)</tt></h3>
108 This sets the mode for the function at the stack index <tt>idx</tt> or
109 the parent of the calling function (<tt>idx = 0</tt>). It either
110 enables JIT compilation for a function, disables it and flushes any
111 already compiled code or only flushes already compiled code. This
112 applies recursively to all sub-functions of the function with
113 <tt>LUAJIT_MODE_ALLFUNC</tt> or only to the sub-functions with
114 <tt>LUAJIT_MODE_ALLSUBFUNC</tt>.
115 </p>
117 <h3 id="mode_trace"><tt>luaJIT_setmode(L, trace,<br>
118 &nbsp;&nbsp;LUAJIT_MODE_TRACE|LUAJIT_MODE_FLUSH)</tt></h3>
120 Flushes the specified root trace and all of its side traces from the cache.
121 The code for the trace will be retained as long as there are any other
122 traces which link to it.
123 </p>
125 <h3 id="mode_wrapcfunc"><tt>luaJIT_setmode(L, idx, LUAJIT_MODE_WRAPCFUNC|flag)</tt></h3>
127 This mode defines a wrapper function for calls to C functions. If
128 called with <tt>LUAJIT_MODE_ON</tt>, the stack index at <tt>idx</tt>
129 must be a <tt>lightuserdata</tt> object holding a pointer to the wrapper
130 function. From now on all C functions are called through the wrapper
131 function. If called with <tt>LUAJIT_MODE_OFF</tt> this mode is turned
132 off and all C functions are directly called.
133 </p>
135 The wrapper function can be used for debugging purposes or to catch
136 and convert foreign exceptions. But please read the section on
137 <a href="extensions.html#exceptions">C++&nbsp;exception interoperability</a>
138 first. Recommended usage can be seen in this C++ code excerpt:
139 </p>
140 <pre class="code">
141 #include &lt;exception&gt;
142 #include "lua.hpp"
144 // Catch C++ exceptions and convert them to Lua error messages.
145 // Customize as needed for your own exception classes.
146 static int wrap_exceptions(lua_State *L, lua_CFunction f)
148 try {
149 return f(L); // Call wrapped function and return result.
150 } catch (const char *s) { // Catch and convert exceptions.
151 lua_pushstring(L, s);
152 } catch (std::exception& e) {
153 lua_pushstring(L, e.what());
154 } catch (...) {
155 lua_pushliteral(L, "caught (...)");
157 return lua_error(L); // Rethrow as a Lua error.
160 static int myinit(lua_State *L)
163 // Define wrapper function and enable it.
164 lua_pushlightuserdata(L, (void *)wrap_exceptions);
165 luaJIT_setmode(L, -1, LUAJIT_MODE_WRAPCFUNC|LUAJIT_MODE_ON);
166 lua_pop(L, 1);
169 </pre>
171 Note that you can only define <b>a single global wrapper function</b>,
172 so be careful when using this mechanism from multiple C++ modules.
173 Also note that this mechanism is not without overhead.
174 </p>
175 <br class="flush">
176 </div>
177 <div id="foot">
178 <hr class="hide">
179 Copyright &copy; 2005-2018
180 <span class="noprint">
181 &middot;
182 <a href="contact.html">Contact</a>
183 </span>
184 </div>
185 </body>
186 </html>