aboutsummaryrefslogtreecommitdiff
path: root/doc/ext_ffi_tutorial.html
diff options
context:
space:
mode:
authorMike Pall <mike>2022-06-23 09:10:43 +0200
committerMike Pall <mike>2022-06-23 09:10:43 +0200
commit4c2441c16ce3c4e312aaefecc6d40c4fe21de97c (patch)
tree0ee5ad7a3246f9a620265de9c6998308cb44a09b /doc/ext_ffi_tutorial.html
parent0065cff7e0222c234b75a71e72b8883df5d000c2 (diff)
parent2e98c3d0644fc0c265844908f43b7e4526dd819c (diff)
downloadluajit-4c2441c16ce3c4e312aaefecc6d40c4fe21de97c.tar.gz
luajit-4c2441c16ce3c4e312aaefecc6d40c4fe21de97c.tar.bz2
luajit-4c2441c16ce3c4e312aaefecc6d40c4fe21de97c.zip
Merge branch 'master' into v2.1
Diffstat (limited to 'doc/ext_ffi_tutorial.html')
-rw-r--r--doc/ext_ffi_tutorial.html24
1 files changed, 12 insertions, 12 deletions
diff --git a/doc/ext_ffi_tutorial.html b/doc/ext_ffi_tutorial.html
index ff104f83..e0bf9040 100644
--- a/doc/ext_ffi_tutorial.html
+++ b/doc/ext_ffi_tutorial.html
@@ -85,7 +85,7 @@ of its functions:
85local ffi = require("ffi") 85local ffi = require("ffi")
86</pre> 86</pre>
87<p> 87<p>
88Please note this doesn't define an <tt>ffi</tt> variable in the table 88Please note, this doesn't define an <tt>ffi</tt> variable in the table
89of globals &mdash; you really need to use the local variable. The 89of globals &mdash; you really need to use the local variable. The
90<tt>require</tt> function ensures the library is only loaded once. 90<tt>require</tt> function ensures the library is only loaded once.
91</p> 91</p>
@@ -194,7 +194,7 @@ don't need to declare them as such.
194<span class="mark">&#9316;</span> The <tt>poll()</tt> 194<span class="mark">&#9316;</span> The <tt>poll()</tt>
195function takes a couple more arguments we're not going to use. You can 195function takes a couple more arguments we're not going to use. You can
196simply use <tt>nil</tt> to pass a <tt>NULL</tt> pointer and <tt>0</tt> 196simply use <tt>nil</tt> to pass a <tt>NULL</tt> pointer and <tt>0</tt>
197for the <tt>nfds</tt> parameter. Please note that the 197for the <tt>nfds</tt> parameter. Please note, that the
198number&nbsp;<tt>0</tt> <em>does not convert to a pointer value</em>, 198number&nbsp;<tt>0</tt> <em>does not convert to a pointer value</em>,
199unlike in C++. You really have to pass pointers to pointer arguments 199unlike in C++. You really have to pass pointers to pointer arguments
200and numbers to number arguments. 200and numbers to number arguments.
@@ -291,12 +291,12 @@ Here's the step-by-step explanation:
291<p> 291<p>
292<span class="mark">&#9312;</span> This defines some of the 292<span class="mark">&#9312;</span> This defines some of the
293C&nbsp;functions provided by zlib. For the sake of this example, some 293C&nbsp;functions provided by zlib. For the sake of this example, some
294type indirections have been reduced and it uses the pre-defined 294type indirections have been reduced and it uses the predefined
295fixed-size integer types, while still adhering to the zlib API/ABI. 295fixed-size integer types, while still adhering to the zlib API/ABI.
296</p> 296</p>
297<p> 297<p>
298<span class="mark">&#9313;</span> This loads the zlib shared 298<span class="mark">&#9313;</span> This loads the zlib shared
299library. On POSIX systems it's named <tt>libz.so</tt> and usually 299library. On POSIX systems, it's named <tt>libz.so</tt> and usually
300comes pre-installed. Since <tt>ffi.load()</tt> automatically adds any 300comes pre-installed. Since <tt>ffi.load()</tt> automatically adds any
301missing standard prefixes/suffixes, we can simply load the 301missing standard prefixes/suffixes, we can simply load the
302<tt>"z"</tt> library. On Windows it's named <tt>zlib1.dll</tt> and 302<tt>"z"</tt> library. On Windows it's named <tt>zlib1.dll</tt> and
@@ -324,7 +324,7 @@ actual length that was used.
324<p> 324<p>
325In C you'd pass in the address of a local variable 325In C you'd pass in the address of a local variable
326(<tt>&amp;buflen</tt>). But since there's no address-of operator in 326(<tt>&amp;buflen</tt>). But since there's no address-of operator in
327Lua, we'll just pass in a one-element array. Conveniently it can be 327Lua, we'll just pass in a one-element array. Conveniently, it can be
328initialized with the maximum buffer size in one step. Calling the 328initialized with the maximum buffer size in one step. Calling the
329actual <tt>zlib.compress2</tt> function is then straightforward. 329actual <tt>zlib.compress2</tt> function is then straightforward.
330</p> 330</p>
@@ -348,7 +348,7 @@ for garbage collection and string interning.
348<span class="mark">&#9317;</span> The <tt>uncompress</tt> 348<span class="mark">&#9317;</span> The <tt>uncompress</tt>
349functions does the exact opposite of the <tt>compress</tt> function. 349functions does the exact opposite of the <tt>compress</tt> function.
350The compressed data doesn't include the size of the original string, 350The compressed data doesn't include the size of the original string,
351so this needs to be passed in. Otherwise no surprises here. 351so this needs to be passed in. Otherwise, no surprises here.
352</p> 352</p>
353<p> 353<p>
354<span class="mark">&#9318;</span> The code, that makes use 354<span class="mark">&#9318;</span> The code, that makes use
@@ -382,7 +382,7 @@ Ok, so the <tt>ffi.*</tt> functions generally accept cdata objects
382wherever you'd want to use a number. That's why we get a away with 382wherever you'd want to use a number. That's why we get a away with
383passing <tt>n</tt> to <tt>ffi.string()</tt> above. But other Lua 383passing <tt>n</tt> to <tt>ffi.string()</tt> above. But other Lua
384library functions or modules don't know how to deal with this. So for 384library functions or modules don't know how to deal with this. So for
385maximum portability one needs to use <tt>tonumber()</tt> on returned 385maximum portability, one needs to use <tt>tonumber()</tt> on returned
386<tt>long</tt> results before passing them on. Otherwise the 386<tt>long</tt> results before passing them on. Otherwise the
387application might work on some systems, but would fail in a POSIX/x64 387application might work on some systems, but would fail in a POSIX/x64
388environment. 388environment.
@@ -454,7 +454,7 @@ the origin.
454</p> 454</p>
455<p> 455<p>
456<span class="mark">&#9315;</span> If we run out of operators, we can 456<span class="mark">&#9315;</span> If we run out of operators, we can
457define named methods, too. Here the <tt>__index</tt> table defines an 457define named methods, too. Here, the <tt>__index</tt> table defines an
458<tt>area</tt> function. For custom indexing needs, one might want to 458<tt>area</tt> function. For custom indexing needs, one might want to
459define <tt>__index</tt> and <tt>__newindex</tt> <em>functions</em> instead. 459define <tt>__index</tt> and <tt>__newindex</tt> <em>functions</em> instead.
460</p> 460</p>
@@ -468,13 +468,13 @@ be used e.g. to create an array of points. The metamethods automatically
468apply to any and all uses of this type. 468apply to any and all uses of this type.
469</p> 469</p>
470<p> 470<p>
471Please note that the association with a metatable is permanent and 471Please note, that the association with a metatable is permanent and
472<b>the metatable must not be modified afterwards!</b> Ditto for the 472<b>the metatable must not be modified afterwards!</b> Ditto for the
473<tt>__index</tt> table. 473<tt>__index</tt> table.
474</p> 474</p>
475<p> 475<p>
476<span class="mark">&#9317;</span> Here are some simple usage examples 476<span class="mark">&#9317;</span> Here are some simple usage examples
477for the point type and their expected results. The pre-defined 477for the point type and their expected results. The predefined
478operations (such as <tt>a.x</tt>) can be freely mixed with the newly 478operations (such as <tt>a.x</tt>) can be freely mixed with the newly
479defined metamethods. Note that <tt>area</tt> is a method and must be 479defined metamethods. Note that <tt>area</tt> is a method and must be
480called with the Lua syntax for methods: <tt>a:area()</tt>, not 480called with the Lua syntax for methods: <tt>a:area()</tt>, not
@@ -483,7 +483,7 @@ called with the Lua syntax for methods: <tt>a:area()</tt>, not
483<p> 483<p>
484The C&nbsp;type metamethod mechanism is most useful when used in 484The C&nbsp;type metamethod mechanism is most useful when used in
485conjunction with C&nbsp;libraries that are written in an object-oriented 485conjunction with C&nbsp;libraries that are written in an object-oriented
486style. Creators return a pointer to a new instance and methods take an 486style. Creators return a pointer to a new instance, and methods take an
487instance pointer as the first argument. Sometimes you can just point 487instance pointer as the first argument. Sometimes you can just point
488<tt>__index</tt> to the library namespace and <tt>__gc</tt> to the 488<tt>__index</tt> to the library namespace and <tt>__gc</tt> to the
489destructor and you're done. But often enough you'll want to add 489destructor and you're done. But often enough you'll want to add
@@ -569,7 +569,7 @@ end
569</pre> 569</pre>
570<p> 570<p>
571This turns them into indirect calls and generates bigger and slower 571This turns them into indirect calls and generates bigger and slower
572machine code. Instead you'll want to cache the namespace itself and 572machine code. Instead, you'll want to cache the namespace itself and
573rely on the JIT compiler to eliminate the lookups: 573rely on the JIT compiler to eliminate the lookups:
574</p> 574</p>
575<pre class="code"> 575<pre class="code">