index.html 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. <!DOCTYPE html>
  2. <!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
  3. <!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
  4. <head>
  5. <meta charset="utf-8">
  6. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  7. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  8. <title>Wrapping - Luwra</title>
  9. <link rel="shortcut icon" href="../img/favicon.ico">
  10. <link href='https://fonts.googleapis.com/css?family=Lato:400,700|Roboto+Slab:400,700|Inconsolata:400,700' rel='stylesheet' type='text/css'>
  11. <link rel="stylesheet" href="../css/theme.css" type="text/css" />
  12. <link rel="stylesheet" href="../css/theme_extra.css" type="text/css" />
  13. <link rel="stylesheet" href="../css/highlight.css">
  14. <script>
  15. // Current page data
  16. var mkdocs_page_name = "Wrapping";
  17. var mkdocs_page_input_path = "wrapping.md";
  18. var mkdocs_page_url = "/wrapping/";
  19. </script>
  20. <script src="../js/jquery-2.1.1.min.js"></script>
  21. <script src="../js/modernizr-2.8.3.min.js"></script>
  22. <script type="text/javascript" src="../js/highlight.pack.js"></script>
  23. <script src="../js/theme.js"></script>
  24. </head>
  25. <body class="wy-body-for-nav" role="document">
  26. <div class="wy-grid-for-nav">
  27. <nav data-toggle="wy-nav-shift" class="wy-nav-side stickynav">
  28. <div class="wy-side-nav-search">
  29. <a href=".." class="icon icon-home"> Luwra</a>
  30. <div role="search">
  31. <form id ="rtd-search-form" class="wy-form" action="../search.html" method="get">
  32. <input type="text" name="q" placeholder="Search docs" />
  33. </form>
  34. </div>
  35. </div>
  36. <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
  37. <ul class="current">
  38. <li>
  39. <li class="toctree-l1 ">
  40. <a class="" href="..">Home</a>
  41. </li>
  42. <li>
  43. <li>
  44. <li class="toctree-l1 ">
  45. <a class="" href="../stack-interaction/">Stack Interaction</a>
  46. </li>
  47. <li>
  48. <li>
  49. <li class="toctree-l1 ">
  50. <a class="" href="../advanced/">Advanced Stack Interaction</a>
  51. </li>
  52. <li>
  53. <li>
  54. <li class="toctree-l1 current">
  55. <a class="current" href="./">Wrapping</a>
  56. <ul>
  57. <li class="toctree-l3"><a href="#wrapping">Wrapping</a></li>
  58. <li><a class="toctree-l4" href="#functions">Functions</a></li>
  59. <li><a class="toctree-l4" href="#class-members">Class members</a></li>
  60. </ul>
  61. </li>
  62. <li>
  63. <li>
  64. <li class="toctree-l1 ">
  65. <a class="" href="../user-types/">User Types</a>
  66. </li>
  67. <li>
  68. </ul>
  69. </div>
  70. &nbsp;
  71. </nav>
  72. <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">
  73. <nav class="wy-nav-top" role="navigation" aria-label="top navigation">
  74. <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
  75. <a href="..">Luwra</a>
  76. </nav>
  77. <div class="wy-nav-content">
  78. <div class="rst-content">
  79. <div role="navigation" aria-label="breadcrumbs navigation">
  80. <ul class="wy-breadcrumbs">
  81. <li><a href="..">Docs</a> &raquo;</li>
  82. <li>Wrapping</li>
  83. <li class="wy-breadcrumbs-aside">
  84. <a href="https://github.com/vapourismo/luwra" class="icon icon-github"> Edit on GitHub</a>
  85. </li>
  86. </ul>
  87. <hr/>
  88. </div>
  89. <div role="main">
  90. <div class="section">
  91. <h1 id="wrapping">Wrapping</h1>
  92. <p>Luwra provides a simple way to generate Lua <a href="http://www.lua.org/manual/5.3/manual.html#lua_CFunction">C functions</a> from functions and class
  93. members like methods and accessors using the <code>LUWRA_WRAP</code> macro. These kind of C functions are
  94. useful, because they work just like regular Lua functions within the Lua virtual machine.
  95. Registering these functions is the most straightforward way of providing the functionality of your
  96. application to Lua.</p>
  97. <h2 id="functions">Functions</h2>
  98. <p>When wrapping functions, one must consider that all parameter types must be read from the
  99. stack and the return type must be pushed onto the stack.</p>
  100. <h3 id="example">Example</h3>
  101. <p>Lets assume you want to make the following function available in Lua.</p>
  102. <pre><code class="c++">int my_function(const char* a, int b) {
  103. return /* magic */;
  104. }
  105. </code></pre>
  106. <p>First, you must generate a Lua <a href="http://www.lua.org/manual/5.3/manual.html#lua_CFunction">C function</a>. One utilizes the <code>LUWRA_WRAP</code> macro for
  107. this.</p>
  108. <pre><code class="c++">lua_CFunction cfun = LUWRA_WRAP(my_function);
  109. </code></pre>
  110. <p><strong>Note:</strong> Do not provide the address of your function (e.g. <code>&amp;my_function</code>) to any wrapping macro.
  111. The macro will take care of this itself. You must provide only the name of the function.</p>
  112. <p>Once you have the C function, you can register it in the global namespace.</p>
  113. <pre><code class="c++">luwra::setGlobal(lua, &quot;my_function&quot;, cfun);
  114. </code></pre>
  115. <p>Invoking the function in Lua is fairly straightforward.</p>
  116. <pre><code class="lua">print(my_function(&quot;Hello World&quot;, 1337))
  117. </code></pre>
  118. <h3 id="performance">Performance</h3>
  119. <p><a href="http://www.lua.org/manual/5.3/manual.html#lua_CFunction">C functions</a> are dynamically created at compile-time. All of the functions involved
  120. in wrapping are marked as <code>inline</code>, which means modern compilers produce wrapper functions with zero
  121. overhead, when optimization is turned on.</p>
  122. <p>For the example above, the resulting code would look similiar to the following.</p>
  123. <pre><code class="c++">static int cfun(lua_State* state) {
  124. lua_pushinteger(
  125. state,
  126. my_function(
  127. luaL_checkstring(state, 1),
  128. luaL_checkinteger(state, 1)
  129. )
  130. );
  131. return 1;
  132. }
  133. </code></pre>
  134. <h2 id="class-members">Class members</h2>
  135. <p>Although a little trickier, it is also possible to turn C++ field accessors and methods into Lua
  136. <a href="http://www.lua.org/manual/5.3/manual.html#lua_CFunction">C functions</a>. The resulting Lua functions expect the first (or <code>self</code>) parameter to
  137. be an instance of the type which the wrapped field or method belongs to.</p>
  138. <p><strong>Note:</strong> Before you wrap fields and methods manually, you might want to take a look at the
  139. <a href="../user-types/">User Types</a> section.</p>
  140. <h3 id="example_1">Example</h3>
  141. <p>This example will operate on the following structure.</p>
  142. <pre><code class="c++">struct Point {
  143. double x, y;
  144. // ...
  145. void scale(double f) {
  146. x *= f;
  147. y *= f;
  148. }
  149. };
  150. </code></pre>
  151. <p>Wrapping field accessors and methods works identical to wrapping functions.</p>
  152. <pre><code class="c++">lua_CFunction cfun_x = LUWRA_WRAP(Point::x),
  153. cfun_y = LUWRA_WRAP(Point::y),
  154. cfun_scale = LUWRA_WRAP(Point::scale);
  155. // Register in global namespace
  156. luwra::setGlobal(lua, &quot;x&quot;, cfun_x);
  157. luwra::setGlobal(lua, &quot;y&quot;, cfun_y);
  158. luwra::setGlobal(lua, &quot;scale&quot;, cfun_scale);
  159. </code></pre>
  160. <p>Usage in Lua is analogous to function usage.</p>
  161. <pre><code class="lua">-- Instantiate 'Point' here, have a look at the User Types section to find out how to do this
  162. local my_point = ...
  163. -- Access 'x' and 'y' field
  164. print(x(my_point), y(my_point))
  165. -- Set 'x' and 'y' field
  166. x(my_point, 13.37)
  167. y(my_point, 73.31)
  168. -- Invoke 'scale' method
  169. scale(my_point, 2)
  170. </code></pre>
  171. </div>
  172. </div>
  173. <footer>
  174. <div class="rst-footer-buttons" role="navigation" aria-label="footer navigation">
  175. <a href="../user-types/" class="btn btn-neutral float-right" title="User Types">Next <span class="icon icon-circle-arrow-right"></span></a>
  176. <a href="../advanced/" class="btn btn-neutral" title="Advanced Stack Interaction"><span class="icon icon-circle-arrow-left"></span> Previous</a>
  177. </div>
  178. <hr/>
  179. <div role="contentinfo">
  180. <!-- Copyright etc -->
  181. </div>
  182. Built with <a href="http://www.mkdocs.org">MkDocs</a> using a <a href="https://github.com/snide/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>.
  183. </footer>
  184. </div>
  185. </div>
  186. </section>
  187. </div>
  188. <div class="rst-versions" role="note" style="cursor: pointer">
  189. <span class="rst-current-version" data-toggle="rst-current-version">
  190. <a href="https://github.com/vapourismo/luwra" class="icon icon-github" style="float: left; color: #fcfcfc"> GitHub</a>
  191. <span><a href="../advanced/" style="color: #fcfcfc;">&laquo; Previous</a></span>
  192. <span style="margin-left: 15px"><a href="../user-types/" style="color: #fcfcfc">Next &raquo;</a></span>
  193. </span>
  194. </div>
  195. </body>
  196. </html>