diff options
author | Mike Pall <mike> | 2022-06-23 09:10:43 +0200 |
---|---|---|
committer | Mike Pall <mike> | 2022-06-23 09:10:43 +0200 |
commit | 4c2441c16ce3c4e312aaefecc6d40c4fe21de97c (patch) | |
tree | 0ee5ad7a3246f9a620265de9c6998308cb44a09b /doc/ext_ffi_tutorial.html | |
parent | 0065cff7e0222c234b75a71e72b8883df5d000c2 (diff) | |
parent | 2e98c3d0644fc0c265844908f43b7e4526dd819c (diff) | |
download | luajit-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.html | 24 |
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: | |||
85 | local ffi = require("ffi") | 85 | local ffi = require("ffi") |
86 | </pre> | 86 | </pre> |
87 | <p> | 87 | <p> |
88 | Please note this doesn't define an <tt>ffi</tt> variable in the table | 88 | Please note, this doesn't define an <tt>ffi</tt> variable in the table |
89 | of globals — you really need to use the local variable. The | 89 | of globals — 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">⑤</span> The <tt>poll()</tt> | 194 | <span class="mark">⑤</span> The <tt>poll()</tt> |
195 | function takes a couple more arguments we're not going to use. You can | 195 | function takes a couple more arguments we're not going to use. You can |
196 | simply use <tt>nil</tt> to pass a <tt>NULL</tt> pointer and <tt>0</tt> | 196 | simply use <tt>nil</tt> to pass a <tt>NULL</tt> pointer and <tt>0</tt> |
197 | for the <tt>nfds</tt> parameter. Please note that the | 197 | for the <tt>nfds</tt> parameter. Please note, that the |
198 | number <tt>0</tt> <em>does not convert to a pointer value</em>, | 198 | number <tt>0</tt> <em>does not convert to a pointer value</em>, |
199 | unlike in C++. You really have to pass pointers to pointer arguments | 199 | unlike in C++. You really have to pass pointers to pointer arguments |
200 | and numbers to number arguments. | 200 | and numbers to number arguments. |
@@ -291,12 +291,12 @@ Here's the step-by-step explanation: | |||
291 | <p> | 291 | <p> |
292 | <span class="mark">①</span> This defines some of the | 292 | <span class="mark">①</span> This defines some of the |
293 | C functions provided by zlib. For the sake of this example, some | 293 | C functions provided by zlib. For the sake of this example, some |
294 | type indirections have been reduced and it uses the pre-defined | 294 | type indirections have been reduced and it uses the predefined |
295 | fixed-size integer types, while still adhering to the zlib API/ABI. | 295 | fixed-size integer types, while still adhering to the zlib API/ABI. |
296 | </p> | 296 | </p> |
297 | <p> | 297 | <p> |
298 | <span class="mark">②</span> This loads the zlib shared | 298 | <span class="mark">②</span> This loads the zlib shared |
299 | library. On POSIX systems it's named <tt>libz.so</tt> and usually | 299 | library. On POSIX systems, it's named <tt>libz.so</tt> and usually |
300 | comes pre-installed. Since <tt>ffi.load()</tt> automatically adds any | 300 | comes pre-installed. Since <tt>ffi.load()</tt> automatically adds any |
301 | missing standard prefixes/suffixes, we can simply load the | 301 | missing 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> |
325 | In C you'd pass in the address of a local variable | 325 | In C you'd pass in the address of a local variable |
326 | (<tt>&buflen</tt>). But since there's no address-of operator in | 326 | (<tt>&buflen</tt>). But since there's no address-of operator in |
327 | Lua, we'll just pass in a one-element array. Conveniently it can be | 327 | Lua, we'll just pass in a one-element array. Conveniently, it can be |
328 | initialized with the maximum buffer size in one step. Calling the | 328 | initialized with the maximum buffer size in one step. Calling the |
329 | actual <tt>zlib.compress2</tt> function is then straightforward. | 329 | actual <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">⑥</span> The <tt>uncompress</tt> | 348 | <span class="mark">⑥</span> The <tt>uncompress</tt> |
349 | functions does the exact opposite of the <tt>compress</tt> function. | 349 | functions does the exact opposite of the <tt>compress</tt> function. |
350 | The compressed data doesn't include the size of the original string, | 350 | The compressed data doesn't include the size of the original string, |
351 | so this needs to be passed in. Otherwise no surprises here. | 351 | so this needs to be passed in. Otherwise, no surprises here. |
352 | </p> | 352 | </p> |
353 | <p> | 353 | <p> |
354 | <span class="mark">⑦</span> The code, that makes use | 354 | <span class="mark">⑦</span> The code, that makes use |
@@ -382,7 +382,7 @@ Ok, so the <tt>ffi.*</tt> functions generally accept cdata objects | |||
382 | wherever you'd want to use a number. That's why we get a away with | 382 | wherever you'd want to use a number. That's why we get a away with |
383 | passing <tt>n</tt> to <tt>ffi.string()</tt> above. But other Lua | 383 | passing <tt>n</tt> to <tt>ffi.string()</tt> above. But other Lua |
384 | library functions or modules don't know how to deal with this. So for | 384 | library functions or modules don't know how to deal with this. So for |
385 | maximum portability one needs to use <tt>tonumber()</tt> on returned | 385 | maximum 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 |
387 | application might work on some systems, but would fail in a POSIX/x64 | 387 | application might work on some systems, but would fail in a POSIX/x64 |
388 | environment. | 388 | environment. |
@@ -454,7 +454,7 @@ the origin. | |||
454 | </p> | 454 | </p> |
455 | <p> | 455 | <p> |
456 | <span class="mark">④</span> If we run out of operators, we can | 456 | <span class="mark">④</span> If we run out of operators, we can |
457 | define named methods, too. Here the <tt>__index</tt> table defines an | 457 | define 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 |
459 | define <tt>__index</tt> and <tt>__newindex</tt> <em>functions</em> instead. | 459 | define <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 | |||
468 | apply to any and all uses of this type. | 468 | apply to any and all uses of this type. |
469 | </p> | 469 | </p> |
470 | <p> | 470 | <p> |
471 | Please note that the association with a metatable is permanent and | 471 | Please 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">⑥</span> Here are some simple usage examples | 476 | <span class="mark">⑥</span> Here are some simple usage examples |
477 | for the point type and their expected results. The pre-defined | 477 | for the point type and their expected results. The predefined |
478 | operations (such as <tt>a.x</tt>) can be freely mixed with the newly | 478 | operations (such as <tt>a.x</tt>) can be freely mixed with the newly |
479 | defined metamethods. Note that <tt>area</tt> is a method and must be | 479 | defined metamethods. Note that <tt>area</tt> is a method and must be |
480 | called with the Lua syntax for methods: <tt>a:area()</tt>, not | 480 | called 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> |
484 | The C type metamethod mechanism is most useful when used in | 484 | The C type metamethod mechanism is most useful when used in |
485 | conjunction with C libraries that are written in an object-oriented | 485 | conjunction with C libraries that are written in an object-oriented |
486 | style. Creators return a pointer to a new instance and methods take an | 486 | style. Creators return a pointer to a new instance, and methods take an |
487 | instance pointer as the first argument. Sometimes you can just point | 487 | instance 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 |
489 | destructor and you're done. But often enough you'll want to add | 489 | destructor 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> |
571 | This turns them into indirect calls and generates bigger and slower | 571 | This turns them into indirect calls and generates bigger and slower |
572 | machine code. Instead you'll want to cache the namespace itself and | 572 | machine code. Instead, you'll want to cache the namespace itself and |
573 | rely on the JIT compiler to eliminate the lookups: | 573 | rely on the JIT compiler to eliminate the lookups: |
574 | </p> | 574 | </p> |
575 | <pre class="code"> | 575 | <pre class="code"> |