This first edition was written for Lua 5.0. While still largely relevant for later versions, there are some differences.
The fourth edition targets Lua 5.3 and is available at Amazon and other bookstores.
Clash of clans mobile game download. By buying the book, you also help to support the Lua project.
|Programming in Lua|
|Part I. The LanguageChapter 9. Coroutines|
25.2 – Calling Lua Functions. A great strength of Lua is that a configuration file can define functions to be called by the application. For instance, you can write an application to plot the graph of a function and use Lua to define the functions to be plotted.
9.1 – Coroutine Basics
Lua offers all its coroutine functions packed in the
create function creates new coroutines.It has a single argument,a function with the code that the coroutine will run.It returns a value of type
thread,which represents the new coroutine.Quite often, the argument to
create is an anonymous function,like here:
A coroutine can be in one of three different states:suspended, running, and dead.When we create a coroutine, it starts in the suspended state.That means that a coroutine does not run its body automaticallywhen we create it.We can check the state of a coroutine with the
status function:The function
coroutine.resume (re)starts the execution ofa coroutine, changing its state from suspended to running:In this example,the coroutine body simply prints
'hi' and terminates,leaving the coroutine in the dead state,from which it cannot return: Samsung g935p 7.0 frp.
Until now, coroutines look like nothing more than a complicatedway to call functions.The real power of coroutines stems from the
yield function,which allows a running coroutine to suspend its executionso that it can be resumed later.Let us see a simple example:Now, when we resume this coroutine,it starts its execution and runs until the first
yield:If we check its status,we can see that the coroutine is suspendedand therefore can be resumed again:From the coroutine's point of view,all activity that happens while it is suspendedis happening inside its call to
yield.When we resume the coroutine,this call to
yield finally returnsand the coroutine continues its executionuntil the next yield or until its end:During the last call to
resume,the coroutine body finished the loop and then returned,so the coroutine is dead now.If we try to resume it again,
resume returns false plus an error message:Note that
resume runs in protected mode.Therefore, if there is any error inside a coroutine,Lua will not show the error message,but instead will return it to the
A useful facility in Lua is thata pair resume-yield can exchange data between them.The first
resume, which has no corresponding
yieldwaiting for it, passes its extra arguments as arguments to thecoroutine main function:A call to
resume returns,after the true that signals no errors,any arguments passed to the corresponding
yield returns any extra argumentspassed to the corresponding
resume:Finally, when a coroutine ends,any values returned by its main function go tothe corresponding
We seldom use all these facilities in the same coroutine,but all of them have their uses.
For those that already know something about coroutines,it is important to clarify some concepts before we go on.Lua offers what I call asymmetric coroutines.That means that it has a function to suspend theexecution of a coroutineand a different function to resume a suspended coroutine.Some other languages offer symmetric coroutines,where there is only one function to transfercontrol from any coroutine to another.
Some people call asymmetric coroutine semi-coroutines(because they are not symmetrical,they are not really co).However, other people use the same term semi-coroutineto denote a restricted implementation of coroutines,where a coroutine can only suspend its execution when it is notinside any auxiliary function, that is,when it has no pending calls in its control stack.In other words, only the main body of such semi-coroutines can yield.A generator in Python is an exampleof this meaning of semi-coroutines.
Unlike the difference between symmetric and asymmetric coroutines,the difference between coroutines and generators(as presented in Python) is a deep one;generators are simply not powerful enough to implementseveral interesting constructions that we can write with true coroutines.Lua offers true, asymmetric coroutines.Those that prefer symmetric coroutines can implement them ontop of the asymmetric facilities of Lua.It is an easy task.(Basically, each transfer does a yield followed by a resume.)
Get Lua Value From Chi
|Copyright © 2003–2004 Roberto Ierusalimschy. All rights reserved.|