diff options
| author | Mike Pall <mike> | 2009-12-08 19:49:20 +0100 |
|---|---|---|
| committer | Mike Pall <mike> | 2009-12-08 19:49:20 +0100 |
| commit | 1d1fed48a002dfc0919135911057ebc255a53e0a (patch) | |
| tree | c5c6643908374bb8f02f4c7691332d32f6645986 /doc/api.html | |
| parent | 55b16959717084884fd4a0cbae6d19e3786c20c7 (diff) | |
| download | luajit-2.0.0-beta2.tar.gz luajit-2.0.0-beta2.tar.bz2 luajit-2.0.0-beta2.zip | |
RELEASE LuaJIT-2.0.0-beta2v2.0.0-beta2
Diffstat (limited to 'doc/api.html')
| -rw-r--r-- | doc/api.html | 147 |
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 | |||
| 100 | Flushes the whole cache of compiled code. | 100 | Flushes 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> | ||
| 105 | Flushes the code for the specified root trace and all of its | ||
| 106 | side 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> |
| 110 | jit.off(func|true [,true|false])<br> | 104 | jit.off(func|true [,true|false])<br> |
| 111 | jit.flush(func|true [,true|false])</tt></h3> | 105 | jit.flush(func|true [,true|false])</tt></h3> |
| @@ -142,6 +136,13 @@ of a module to turn off JIT compilation for the whole module for | |||
| 142 | debugging purposes. | 136 | debugging purposes. |
| 143 | </p> | 137 | </p> |
| 144 | 138 | ||
| 139 | <h3 id="jit_flush_tr"><tt>status = jit.flush(tr)</tt></h3> | ||
| 140 | <p> | ||
| 141 | Tries to flush the code for the specified trace and all of its | ||
| 142 | side traces from the cache. Returns <tt>true</tt> on success. | ||
| 143 | Returns <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> |
| 147 | Contains the LuaJIT version string. | 148 | Contains the LuaJIT version string. |
| @@ -189,6 +190,140 @@ The debug modules <tt>-jbc</tt>, <tt>-jv</tt> and <tt>-jdump</tt> make | |||
| 189 | extensive use of these functions. Please check out their source code, | 190 | extensive use of these functions. Please check out their source code, |
| 190 | if you want to know more. | 191 | if you want to know more. |
| 191 | </p> | 192 | </p> |
| 193 | |||
| 194 | <h2 id="c_api">C API extensions</h2> | ||
| 195 | <p> | ||
| 196 | LuaJIT adds some extensions to the Lua/C API. The LuaJIT include | ||
| 197 | directory must be in the compiler search path (<tt>-I<i>path</i></tt>) | ||
| 198 | to be able to include the required header for C code: | ||
| 199 | </p> | ||
| 200 | <pre class="code"> | ||
| 201 | #include "luajit.h" | ||
| 202 | </pre> | ||
| 203 | <p> | ||
| 204 | Or 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 | — Control VM</h2> | ||
| 212 | <p> | ||
| 213 | This is a C API extension to allow control of the VM from C code. The | ||
| 214 | full prototype of <tt>LuaJIT_setmode</tt> is: | ||
| 215 | </p> | ||
| 216 | <pre class="code"> | ||
| 217 | LUA_API int luaJIT_setmode(lua_State *L, int idx, int mode); | ||
| 218 | </pre> | ||
| 219 | <p> | ||
| 220 | The returned status is either success (<tt>1</tt>) or failure (<tt>0</tt>). | ||
| 221 | The second argument is either <tt>0</tt> or a stack index (similar to the | ||
| 222 | other Lua/C API functions). | ||
| 223 | </p> | ||
| 224 | <p> | ||
| 225 | The third argument specifies the mode, which is 'or'ed with a flag. | ||
| 226 | The 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> | ||
| 231 | The 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> | ||
| 236 | Turn 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> | ||
| 243 | This sets the mode for the function at the stack index <tt>idx</tt> or | ||
| 244 | the parent of the calling function (<tt>idx = 0</tt>). It either | ||
| 245 | enables JIT compilation for a function, disables it and flushes any | ||
| 246 | already compiled code or only flushes already compiled code. This | ||
| 247 | applies 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 | LUAJIT_MODE_TRACE|LUAJIT_MODE_FLUSH)</tt></h3> | ||
| 254 | <p> | ||
| 255 | Tries to flush the code for the specified trace and all of its | ||
| 256 | side 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> | ||
| 261 | This mode defines a wrapper function for calls to C functions. The | ||
| 262 | first time this is called with <tt>LUAJIT_MODE_ON</tt>, the stack | ||
| 263 | index at <tt>idx</tt> must be a <tt>lightuserdata</tt> object holding | ||
| 264 | a pointer to the wrapper function. All <b>subsequently created C | ||
| 265 | functions</b> are called through the wrapper functions. After the initial | ||
| 266 | definition <tt>idx</tt> can be left at <tt>0</tt> when turning the mode | ||
| 267 | on or off. | ||
| 268 | </p> | ||
| 269 | <p> | ||
| 270 | The wrapper function can be used for debugging purposes or to catch | ||
| 271 | and convert foreign exceptions. Recommended usage can be seen in this | ||
| 272 | C++ code excerpt: | ||
| 273 | </p> | ||
| 274 | <pre class="code"> | ||
| 275 | #include <exception> | ||
| 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. | ||
| 280 | static 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 | |||
| 294 | static 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> | ||
| 313 | Note that you can only define <b>a single global wrapper function</b>, | ||
| 314 | so be careful when using this mechanism from multiple C++ modules. | ||
| 315 | Also note that this mechanism is not without overhead. It should only | ||
| 316 | be enabled for definitions of C++ functions that can actually throw | ||
| 317 | exceptions. If you're embedding LuaJIT into an application, only | ||
| 318 | enable it <b>after</b> running <tt>luaL_openlibs</tt>. | ||
| 319 | </p> | ||
| 320 | <p> | ||
| 321 | LuaJIT already intercepts exception handling for systems using | ||
| 322 | ELF/DWARF2 stack unwinding (e.g. Linux). This is a zero-cost mechanism | ||
| 323 | and always enabled. You don't need to use any wrapper functions, | ||
| 324 | except when you want to get a more specific error message than | ||
| 325 | <tt>"C++ exception"</tt>. | ||
| 326 | </p> | ||
| 192 | <br class="flush"> | 327 | <br class="flush"> |
| 193 | </div> | 328 | </div> |
| 194 | <div id="foot"> | 329 | <div id="foot"> |
