Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Lua
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== Implementation == Lua programs are not [[Interpreter (computing)|interpreted]] directly from the textual Lua file, but are [[Compiler|compiled]] into bytecode, which is then run on the Lua [[virtual machine]] (VM). The compiling process is typically invisible to the user and is performed during [[Run time (program lifecycle phase)|run-time]], especially when a [[just-in-time compilation]] (JIT) compiler is used, but it can be done offline to increase loading performance or reduce the memory footprint of the host environment by leaving out the compiler. Lua bytecode can also be produced and executed from within Lua, using the <code>dump</code> function from the string library and the <code>load/loadstring/loadfile</code> functions. Lua version 5.3.4 is implemented in approximately 24,000 lines of C code.<ref name="luaspe"/><ref name="luaabout"/> Like most CPUs, and unlike most virtual machines (which are [[Stack machine|stack-based]]), the Lua VM is [[Register machine|register-based]], and therefore more closely resembles most hardware design. The register architecture both avoids excessive copying of values, and reduces the total number of instructions per function. The virtual machine of Lua 5 is one of the first register-based pure VMs to have a wide use.<ref name=lua5impl>{{cite journal |last1=Ierusalimschy |first1=R. |last2=Figueiredo |first2=L. H. |last3=Celes |first3=W. |year=2005 |title=The implementation of Lua 5.0 |journal=J. Of Universal Comp. Sci. |volume=11 |issue=7 |pages=1159β1176 |url=http://www.jucs.org/jucs_11_7/the_implementation_of_lua/jucs_11_7_1159_1176_defigueiredo.html |doi=10.3217/jucs-011-07-1159 |doi-access=free}}</ref> [[Parrot virtual machine|Parrot]] and [[Android (operating system)|Android]]'s [[Dalvik (software)|Dalvik]] are two other well-known register-based VMs. PCScheme's VM was also register-based.<ref>{{cite book |last1=Texas Instruments |year=1990 |title=PC Scheme: Users Guide and Language Reference Manual, Trade Edition |publisher=MIP Press |isbn=0-262-70040-9}}</ref> This example is the bytecode listing of the factorial function defined [[#Factorial example|above]] (as shown by the <code>luac</code> 5.1 compiler):<ref name=luavm51instr>{{cite web |last1=Man |first1=Kein-Hong |year=2006 |title=A No-Frills Introduction to Lua 5.1 VM Instructions |url=https://talk.pokitto.com/uploads/default/original/2X/7/716c67a0b5b1636cbc4dc1fec232ca2536cb74d1.pdf}}</ref><!-- See earlier revisions for now-dead original and archive.org snapshots of this link, in case it moves again. It is CC-BY-NC-SA so it can be rehosted if needed. --> function <factorial.lua:1,7> (9 instructions, 36 bytes at 0x8063c60) 1 param, 6 slots, 0 upvalues, 6 locals, 2 constants, 0 functions 1 [2] LOADK 1 -1 ; 1 2 [3] LOADK 2 -2 ; 2 3 [3] MOVE 3 0 4 [3] LOADK 4 -1 ; 1 5 [3] FORPREP 2 1 ; to 7 6 [4] MUL 1 1 5 7 [3] FORLOOP 2 -2 ; to 6 8 [6] RETURN 1 2 9 [7] RETURN 0 1
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)