<span id="Label3"></p>Wax How to use Lua<p><p>Speaking <code class="prettyprint">Wax</code> of the features, it supports you to use any <code class="prettyprint">OC</code> of the classes in the script, and also supports you in creating a class.</p></p><p><p>When you use a class, you use This:</p></p> <table class="table table-bordered"> <tbody> <tr> <td><pre class="prettyprint undefined"><span class="line-number">1<span class="line-number">2</span></span></pre></td> <td><pre class="prettyprint java"><code class="lua"><span class="line"><span class="n">NSString <span class="c1">-- Returns the NSString <span class="class"><span class="keyword">class<span class="class"><span class="class"><span class="n"><span class="title">UIView <span class="c1">-- <span class="title">Returns <span class="title">the <span class="title">UIView <span class="title">class</span></span></span></span></span></span></span></span></span></span></span></span></span></span></code></pre></td> </tr> </tbody> </table><p><p>This invokes actually a syntactic sugar, in fact he calls wax.class["UIView", but we do not need to know this when using it, because in this framework has been implemented by the method of setting the Meta-table.</p></p><p><p>This is the case when defining a class:</p></p> <table class="table table-bordered"> <tbody> <tr> <td><pre class="prettyprint http"><span class="line-number"><span class="attribute">1</span></span></pre></td> <td><pre class="prettyprint undefined"><code class="lua"><span class="line"><span class="n">waxClass<span class="p">{<span class="s2">"<span class="s">MyClass"<span class="p">, <span class="n">NSObject<span class="p">}</span></span></span></span></span></span></span></span></code></pre></td> </tr> </tbody> </table><p><p>Classes that follow the Agreement:</p></p> <table class="table table-bordered"> <tbody> <tr> <td><pre class="prettyprint http"><span class="line-number"><span class="attribute">1</span></span></pre></td> <td><pre class="prettyprint undefined"><code class="lua"><span class="line"><span class="n">waxClass<span class="p">{<span class="s2">"<span class="s">MyClass"<span class="p">,<span class="n">NSObject<span class="p">,<span class="n">protocols<span class="o">=<span class="p">{<span class="s2">"<span class="s">UITableViewDelegate"<span class="p">,<span class="s2">"<span class="s">UITableViewDataSource"<span class="p">}}</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></code></pre></td> </tr> </tbody> </table><p><p>Other function abbreviations in the script file in which you define this class will be used as an instance method of this class. And the first argument of this method must be <code class="prettyprint">self</code> that this is the key to Wax's object-oriented imitation of objective-c. therefore, calling a method in wax uses a colon, similar to This:</p></p> <table class="table table-bordered"> <tbody> <tr> <td><pre class="prettyprint http"><span class="line-number"><span class="attribute">1</span></span></pre></td> <td><pre class="prettyprint ruby"><code class="lua"><span class="line"><span class="n"><span class="constant">UIApplication<span class="p"><span class="symbol">:<span class="symbol"><span class="n">sharedApplication<span class="n"><span class="p">()</span></span></span></span></span></span></span></span></span></code></pre></td> </tr> </tbody> </table><p><p>In fact he is equal to This:</p></p> <table class="table table-bordered"> <tbody> <tr> <td><pre class="prettyprint http"><span class="line-number"><span class="attribute">1</span></span></pre></td> <td><pre class="prettyprint undefined"><code class="lua"><span class="line"><span class="n">UIApplication<span class="p">.<span class="n">sharedApplication<span class="p">(<span class="n">UIApplication<span class="p">)</span></span></span></span></span></span></span></code></pre></td> </tr> </tbody> </table><p><p>When invoking a method with multiple parameters, use it <code class="prettyprint">_</code> instead of the oc: for example:</p></p> <table class="table table-bordered"> <tbody> <tr> <td> <pre class="prettyprint http"><span class="line-number"><span class="attribute">1 </span> </span> </pre> </td> <td> <pre class="prettyprint cs"><code class="objc"> <span class="line"><span class="p">[<span class="n">uialertview <span class="nl">initwithtitle:<span class="s"> <span class="string">@ "title" <span class="nl">message:<span class="s"><span class="string">@ "message" <span class="nl"><span class="keyword">delegate:<span class="nb">nil<span class="c1" ; <span><span class="comment">//oc way </span> </span> </span> </span> </span> </span> </span> </span> </span> </span> </span></span></span></span></code></pre></td> </tr> </tbody> </table> <table class="table table-bordered"> <tbody> <tr> <td> <pre class="prettyprint http"><span class="line-number"><span class="attribute">1 </span> </span> </pre> </td> <td> <pre class="prettyprint ruby"><code class="lua"> <span class="line"><span class="n"><span class="constant">uialertview<span class="p"><span class="symbol">:< Span class= "symbol" ><span class="n">initwithtitle_message_delegate<span class="n"><span class="p"> ( <span class="s2"><span class="string"> "<span class=" string "><span class=" s ">title" <span class="s"><span class="p"> , <span class="s2"><span class="string"> "<span class=" string "><span class=" s ">message" <span class="s"><span class="p"> , <span class="kc"><span class="keyword">nil<span class="p">) <span class="c1">--<span class="constant">wax mode </span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span> </span></span></span></span></span></span></span></span></span></code></pre></td> </tr> </tbody> </table><p><p>Using <code class="prettyprint">Wax</code> Create object does not require you <code class="prettyprint">alloc</code> , because he will help you implement memory management, how it is implemented Later.</p></p><p><p><code class="prettyprint">Wax</code>Attribute property is not supported, so you cannot use the DOT syntax in oc, which requires that the <code class="prettyprint">Wax</code> communication between LUA and OC must be done by means of a method, if you want to access one of the properties <code class="prettyprint">setter</code> <code class="prettyprint">getter</code> . If you use dot syntax in your script, you will create an instance variable for the object, but it is only at the Lua level, and at the OC level it does not know that you have created such an instance Variable.</p></p><p><p><code class="prettyprint">Wax</code>Will force the OC object to be converted to Lua's object, and he also supports reverse conversions, such as a method that requires a <code class="prettyprint">NSString</code> type parameter, and you can pass the LUA string in directly. Sometimes you don't want the OC object to be coerced into lua, it also provides a way to change back to the OC Object.</p></p><p><p><code class="prettyprint">Wax</code>The support for enumerations and structs is not very good, but it requires that the enumerations and structures you need to use are added to the format <code class="prettyprint">APP_ROOT/wax/stdlib/enums.lua和APP_ROOT/wax/wax-scripts/structs.lua</code> in which they are defined so that you can use them normally.</p></p><p><p><code class="prettyprint">Wax</code>The support of the agreement is not very good, some protocols <code class="prettyprint">Wax</code> can be used in the normal, and some can not, You will see a file like ProtocolLoader.h in the source file, he needs to pre-load the unsupported protocol into runtime, the author himself does not know why, perhaps a runtime method he did not know.</p></p><p><p><code class="prettyprint">Wax</code>Also does not support classification, but this use of relatively few, not support and nothing.</p></p>Wax Lua Implementation principle<p><p> We know that OC is a dynamic language, his <code class="prettyprint">runtime </code> is powerful and powerful so that you can create a class dynamically at run time, and <code class="prettyprint">wax </code> is really using OC <code class="prettyprint">runtime </code> implements a series of functions. Currently we are using the <code class="prettyprint">ccluaobjcbridge </code> This class also implements the method that Lua calls oc, and he uses the <code class="prettyprint">runtime </code> , But he is a lot simpler than <code class="prettyprint">wax </code> , as far as his limitations can be seen, It only supports static methods of classes, methods can have only one parameter, cannot create objects, and cannot invoke instance METHODS. It does this by finding the class object through the class name, generating <code class="prettyprint">selector </code> by pre-defined method names that can contain only one or no parameters, and then based on the class object and <code class="prettyprint">selector </code> Generate <code class="prettyprint">nsmethodsignature </code> , which is then generated by the Nsmethodsignature nsinvocation, makes the method call, plus the parameter and the return value of LUA and OC type conversion, the completion of an OC method Call. </p></p><p><p>Let's talk about it <code class="prettyprint">Wax</code> . <code class="prettyprint">Wax</code>in the source code <code class="prettyprint">wax_helpers.h/wax_helpers.m</code> , it provides a series of tool methods including the conversion between LUA and OC type, Lua using _ Method name into Oc: selector, according to the method called by Lua to find the corresponding method <code class="prettyprint">selector</code> , Interested students can go to see the Code.</p></p><p><p><code class="prettyprint">Wax</code>The main thing is to maintain such a structure, basically all the object-related operations are done on this basis:</p></p> <table class="table table-bordered"> <tbody> <tr> <td><pre class="prettyprint undefined"><span class="line-number">1<span class="line-number">2<span class="line-number">3<span class="line-number">4<span class="line-number">5<span class="line-number">6</span> </span> </span> </span> </span></span></pre></td> <td><pre class="prettyprint php"><code class="c"><span class="line"><span class="k">typedef<span class="k">struct<span class="n">_wax_instance_userdata<span class="p">{<span class="line"> <span class="n">Id<span class="n">Instance<span class="p">;<span class="line"> <span class="n">BOOL<span class="n">IsClass<span class="p">;<span class="line"> <span class="n"><span class="class"><span class="keyword">Class<span class="class"> <span class="n"><span class="title">Issuper<span class="p">;<span class="c1">//<span class="title">Issuper<span class="title">Not<span class="title">Only<span class="title">Stores<span class="title">Whether<span class="title">The<span class="title">Class<span class="title">Is<span class="title">A<span class="title">Super<span class="title">But<span class="title">It<span class="title">Also<span class="title">Contains<span class="title">The<span class="title">Value<span class="title"><span class="title">of <span class="title">the <span class="title">next <span class="title">superclass.<span class="class"><span class="line"> <span class="n"><span class="title">bool <span class="n"><span class="title"> Actassuper<span class="c1" ; <span>//<span class="title">it <span class="title">only <span class="title">acts <span class="title"> Like <span class="title">a <span class="title">super <span class="title">once, <span class="title">when <span class="title">it <span class="title">is <span class="title">called <span class="title">for <span class="title">the <span class="title">first <span class="title">time.<span class="line"> <span class="p">} <span class="n"><span class="title">wax_instance_userdata<span class="p" ; < span> </span> </span> </span> </span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span> </span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></code></pre></td> </tr> </tbody> </table><p><p> The first <code class="prettyprint">instance </code> is a pointer to the OC object, <code class="prettyprint">isclass </code> Identifies this is not a class object, <code class="prettyprint">issuper </code> is used to identify his parent object, similar to the <code class in oc="prettyprint">isa </code> pointer, This is done in order to find the subclass if it is not found in the method call, <code class="prettyprint">actassuper </code> is used to identify whether this object is being used as a parent class, <code class="prettyprint">wax </code> An object is intelligently treated as a parent class Once. </p></p><p>The <p> <code class="prettyprint">wax </code> also maintains two tables, one <code class="prettyprint">userdatatable </code> A <code class="prettyprint">stronguserdatatable </code>. Both tables are stored in <code class="prettyprint">wax_instance_userdata->instance </code> <code class=" Prettyprint ">key </code> ,<code class=" prettyprint ">wax_instance_userdata </code> Key-value pairs for Values. <code class="prettyprint">userdatatable </code> is a weak table with a value of wake, which is used to store all created objects, is a weak reference, where he stores OC objects created through LUA , because it is a weak table, the <code class="prettyprint">__gc </code> This meta method is called when not in use, and the OC object is Destroyed. <code class="prettyprint">stronguserdatatable </code> is a strong reference table he saved is all through <code class="prettyprint">wax </code> created object, He is not a weak table so need to manage memory manually. This means that using <code class="prettyprint">wax </code> creates objects in addition to <code class="prettyprint">userdatatable </code> save a copy of in <code class="prettyprint">stronguserdatatable </code> Save one Copy. </p></p><p>The <p> is talking about Here. <code class="prettyprint">wax </code> memory management, <code class="prettyprint">wax </code> 's memory management is also based on reference counting, and he does not use <code class="prettyprint">autoreleasepool </code> . All reference counting operations are done for you in the framework, so you can't call <code class="prettyprint">alloc </code> method in lua, and use <code class="directly" " prettyprint>init </code> method, because he will judge your method is not <code class=" prettyprint ">init </code> initialization method, If yes <code class="prettyprint">wax </code> will help you invoke <code class="prettyprint">alloc </code> Method. Object <code class="prettyprint">release </code> There are two kinds of <code class="prettyprint">userdatatable Objects in the </code> <code class="prettyprint" in <code>__gc </code> Meta method >release and the other is <code class="prettyprint">wax </code> run with a timer timer, constantly polling <code class="prettyprint">stronguserdatatable </code> The reference count of objects in is less than 2, then <code class="prettyprint" release < code> . </code></p></p><p><p> <code class="prettyprint">wax </code> Creating classes and objects, and method calls are all implemented through META-METHODS. first, create a class by defining the class name and the parent class, creating a class at run time through the string and the runtime api, via <code class="prettyprint">class_addmethod () </code> function to create this class registration method, and the implementation of this method is an imp (function pointer), wax is such a method, the method includes the User's own written function, lua, At the OC level, the parameters and the return value of this function are transferred between OC and lua, and these two parts are combined to form a method. That is, when invoking a method written in lua, the parameter is first converted to the LUA type and then by the <code class="prettyprint">lua_pcall () </code> calls the method in lua, After completion, the return value is converted to OC Type. </p></p><p><p>finally, <code class="prettyprint">Wax</code> The method call, whether it is OC own method or the User's own method of writing the end is to call the Imp (function pointer), so before either the call OC native method and the User's own definition of the method, the way is handled the Same. In the Meta-method <code class="prettyprint">__index</code> , the invocation of the method is used as a closure push into lua, and the method is override in the Meta-method <code class="prettyprint">__newindex</code> . The method call in closure is the same as the <code class="prettyprint">CCLuaObjcBridge</code> one that was acquired, <code class="prettyprint">selector</code> generated, <code class="prettyprint">NSMethodSignature</code> then generated, and <code class="prettyprint">NSInvocation</code> then Called. The <code class="prettyprint">CCLuaObjcBridge</code> difference is that because the object is <code class="prettyprint">wax_instance_userdata</code> in a class object that is <code class="prettyprint">instance</code> not generated by the class name, he can invoke an instance Method.</p></p><p><p>The above is only a few individuals understand that their own C API for LUA and OC Runtime API is not very familiar with the use of a <code class="prettyprint">Wax</code> large number of these apis, so there is a wrong place also please point Out.</p></p><p><p>iOS Waxpatch LUA syntax</p></p></span>
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service