diff options
| author | Mike Pall <mike> | 2022-06-23 09:10:09 +0200 |
|---|---|---|
| committer | Mike Pall <mike> | 2022-06-23 09:10:09 +0200 |
| commit | 2e98c3d0644fc0c265844908f43b7e4526dd819c (patch) | |
| tree | b9116c671f4268be7776696abdb189de4be5cf43 /doc/ext_ffi_tutorial.html | |
| parent | 7dc3850e78493eed1e85fa7bc0b96068ae7fb9f4 (diff) | |
| download | luajit-2e98c3d0644fc0c265844908f43b7e4526dd819c.tar.gz luajit-2e98c3d0644fc0c265844908f43b7e4526dd819c.tar.bz2 luajit-2e98c3d0644fc0c265844908f43b7e4526dd819c.zip | |
Grammar and spell check.
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 c5e423b8..de6b6f5e 100644 --- a/doc/ext_ffi_tutorial.html +++ b/doc/ext_ffi_tutorial.html | |||
| @@ -81,7 +81,7 @@ of its functions: | |||
| 81 | local ffi = require("ffi") | 81 | local ffi = require("ffi") |
| 82 | </pre> | 82 | </pre> |
| 83 | <p> | 83 | <p> |
| 84 | Please note this doesn't define an <tt>ffi</tt> variable in the table | 84 | Please note, this doesn't define an <tt>ffi</tt> variable in the table |
| 85 | of globals — you really need to use the local variable. The | 85 | of globals — you really need to use the local variable. The |
| 86 | <tt>require</tt> function ensures the library is only loaded once. | 86 | <tt>require</tt> function ensures the library is only loaded once. |
| 87 | </p> | 87 | </p> |
| @@ -190,7 +190,7 @@ don't need to declare them as such. | |||
| 190 | <span class="mark">⑤</span> The <tt>poll()</tt> | 190 | <span class="mark">⑤</span> The <tt>poll()</tt> |
| 191 | function takes a couple more arguments we're not going to use. You can | 191 | function takes a couple more arguments we're not going to use. You can |
| 192 | simply use <tt>nil</tt> to pass a <tt>NULL</tt> pointer and <tt>0</tt> | 192 | simply use <tt>nil</tt> to pass a <tt>NULL</tt> pointer and <tt>0</tt> |
| 193 | for the <tt>nfds</tt> parameter. Please note that the | 193 | for the <tt>nfds</tt> parameter. Please note, that the |
| 194 | number <tt>0</tt> <em>does not convert to a pointer value</em>, | 194 | number <tt>0</tt> <em>does not convert to a pointer value</em>, |
| 195 | unlike in C++. You really have to pass pointers to pointer arguments | 195 | unlike in C++. You really have to pass pointers to pointer arguments |
| 196 | and numbers to number arguments. | 196 | and numbers to number arguments. |
| @@ -287,12 +287,12 @@ Here's the step-by-step explanation: | |||
| 287 | <p> | 287 | <p> |
| 288 | <span class="mark">①</span> This defines some of the | 288 | <span class="mark">①</span> This defines some of the |
| 289 | C functions provided by zlib. For the sake of this example, some | 289 | C functions provided by zlib. For the sake of this example, some |
| 290 | type indirections have been reduced and it uses the pre-defined | 290 | type indirections have been reduced and it uses the predefined |
| 291 | fixed-size integer types, while still adhering to the zlib API/ABI. | 291 | fixed-size integer types, while still adhering to the zlib API/ABI. |
| 292 | </p> | 292 | </p> |
| 293 | <p> | 293 | <p> |
| 294 | <span class="mark">②</span> This loads the zlib shared | 294 | <span class="mark">②</span> This loads the zlib shared |
| 295 | library. On POSIX systems it's named <tt>libz.so</tt> and usually | 295 | library. On POSIX systems, it's named <tt>libz.so</tt> and usually |
| 296 | comes pre-installed. Since <tt>ffi.load()</tt> automatically adds any | 296 | comes pre-installed. Since <tt>ffi.load()</tt> automatically adds any |
| 297 | missing standard prefixes/suffixes, we can simply load the | 297 | missing standard prefixes/suffixes, we can simply load the |
| 298 | <tt>"z"</tt> library. On Windows it's named <tt>zlib1.dll</tt> and | 298 | <tt>"z"</tt> library. On Windows it's named <tt>zlib1.dll</tt> and |
| @@ -320,7 +320,7 @@ actual length that was used. | |||
| 320 | <p> | 320 | <p> |
| 321 | In C you'd pass in the address of a local variable | 321 | In C you'd pass in the address of a local variable |
| 322 | (<tt>&buflen</tt>). But since there's no address-of operator in | 322 | (<tt>&buflen</tt>). But since there's no address-of operator in |
| 323 | Lua, we'll just pass in a one-element array. Conveniently it can be | 323 | Lua, we'll just pass in a one-element array. Conveniently, it can be |
| 324 | initialized with the maximum buffer size in one step. Calling the | 324 | initialized with the maximum buffer size in one step. Calling the |
| 325 | actual <tt>zlib.compress2</tt> function is then straightforward. | 325 | actual <tt>zlib.compress2</tt> function is then straightforward. |
| 326 | </p> | 326 | </p> |
| @@ -344,7 +344,7 @@ for garbage collection and string interning. | |||
| 344 | <span class="mark">⑥</span> The <tt>uncompress</tt> | 344 | <span class="mark">⑥</span> The <tt>uncompress</tt> |
| 345 | functions does the exact opposite of the <tt>compress</tt> function. | 345 | functions does the exact opposite of the <tt>compress</tt> function. |
| 346 | The compressed data doesn't include the size of the original string, | 346 | The compressed data doesn't include the size of the original string, |
| 347 | so this needs to be passed in. Otherwise no surprises here. | 347 | so this needs to be passed in. Otherwise, no surprises here. |
| 348 | </p> | 348 | </p> |
| 349 | <p> | 349 | <p> |
| 350 | <span class="mark">⑦</span> The code, that makes use | 350 | <span class="mark">⑦</span> The code, that makes use |
| @@ -378,7 +378,7 @@ Ok, so the <tt>ffi.*</tt> functions generally accept cdata objects | |||
| 378 | wherever you'd want to use a number. That's why we get a away with | 378 | wherever you'd want to use a number. That's why we get a away with |
| 379 | passing <tt>n</tt> to <tt>ffi.string()</tt> above. But other Lua | 379 | passing <tt>n</tt> to <tt>ffi.string()</tt> above. But other Lua |
| 380 | library functions or modules don't know how to deal with this. So for | 380 | library functions or modules don't know how to deal with this. So for |
| 381 | maximum portability one needs to use <tt>tonumber()</tt> on returned | 381 | maximum portability, one needs to use <tt>tonumber()</tt> on returned |
| 382 | <tt>long</tt> results before passing them on. Otherwise the | 382 | <tt>long</tt> results before passing them on. Otherwise the |
| 383 | application might work on some systems, but would fail in a POSIX/x64 | 383 | application might work on some systems, but would fail in a POSIX/x64 |
| 384 | environment. | 384 | environment. |
| @@ -450,7 +450,7 @@ the origin. | |||
| 450 | </p> | 450 | </p> |
| 451 | <p> | 451 | <p> |
| 452 | <span class="mark">④</span> If we run out of operators, we can | 452 | <span class="mark">④</span> If we run out of operators, we can |
| 453 | define named methods, too. Here the <tt>__index</tt> table defines an | 453 | define named methods, too. Here, the <tt>__index</tt> table defines an |
| 454 | <tt>area</tt> function. For custom indexing needs, one might want to | 454 | <tt>area</tt> function. For custom indexing needs, one might want to |
| 455 | define <tt>__index</tt> and <tt>__newindex</tt> <em>functions</em> instead. | 455 | define <tt>__index</tt> and <tt>__newindex</tt> <em>functions</em> instead. |
| 456 | </p> | 456 | </p> |
| @@ -464,13 +464,13 @@ be used e.g. to create an array of points. The metamethods automatically | |||
| 464 | apply to any and all uses of this type. | 464 | apply to any and all uses of this type. |
| 465 | </p> | 465 | </p> |
| 466 | <p> | 466 | <p> |
| 467 | Please note that the association with a metatable is permanent and | 467 | Please note, that the association with a metatable is permanent and |
| 468 | <b>the metatable must not be modified afterwards!</b> Ditto for the | 468 | <b>the metatable must not be modified afterwards!</b> Ditto for the |
| 469 | <tt>__index</tt> table. | 469 | <tt>__index</tt> table. |
| 470 | </p> | 470 | </p> |
| 471 | <p> | 471 | <p> |
| 472 | <span class="mark">⑥</span> Here are some simple usage examples | 472 | <span class="mark">⑥</span> Here are some simple usage examples |
| 473 | for the point type and their expected results. The pre-defined | 473 | for the point type and their expected results. The predefined |
| 474 | operations (such as <tt>a.x</tt>) can be freely mixed with the newly | 474 | operations (such as <tt>a.x</tt>) can be freely mixed with the newly |
| 475 | defined metamethods. Note that <tt>area</tt> is a method and must be | 475 | defined metamethods. Note that <tt>area</tt> is a method and must be |
| 476 | called with the Lua syntax for methods: <tt>a:area()</tt>, not | 476 | called with the Lua syntax for methods: <tt>a:area()</tt>, not |
| @@ -479,7 +479,7 @@ called with the Lua syntax for methods: <tt>a:area()</tt>, not | |||
| 479 | <p> | 479 | <p> |
| 480 | The C type metamethod mechanism is most useful when used in | 480 | The C type metamethod mechanism is most useful when used in |
| 481 | conjunction with C libraries that are written in an object-oriented | 481 | conjunction with C libraries that are written in an object-oriented |
| 482 | style. Creators return a pointer to a new instance and methods take an | 482 | style. Creators return a pointer to a new instance, and methods take an |
| 483 | instance pointer as the first argument. Sometimes you can just point | 483 | instance pointer as the first argument. Sometimes you can just point |
| 484 | <tt>__index</tt> to the library namespace and <tt>__gc</tt> to the | 484 | <tt>__index</tt> to the library namespace and <tt>__gc</tt> to the |
| 485 | destructor and you're done. But often enough you'll want to add | 485 | destructor and you're done. But often enough you'll want to add |
| @@ -565,7 +565,7 @@ end | |||
| 565 | </pre> | 565 | </pre> |
| 566 | <p> | 566 | <p> |
| 567 | This turns them into indirect calls and generates bigger and slower | 567 | This turns them into indirect calls and generates bigger and slower |
| 568 | machine code. Instead you'll want to cache the namespace itself and | 568 | machine code. Instead, you'll want to cache the namespace itself and |
| 569 | rely on the JIT compiler to eliminate the lookups: | 569 | rely on the JIT compiler to eliminate the lookups: |
| 570 | </p> | 570 | </p> |
| 571 | <pre class="code"> | 571 | <pre class="code"> |
