An overview of Lua. Julia Lawall. February 7, 2008 DIKU



Similar documents
The Evolution of Lua. Waldemar Celes Luiz Henrique de Figueiredo Roberto Ierusalimschy

The Implementation of Lua 5.0

The Evolution of Lua

Interpreters and virtual machines. Interpreters. Interpreters. Why interpreters? Tree-based interpreters. Text-based interpreters

The Evolution of Lua

Organization of Programming Languages CS320/520N. Lecture 05. Razvan C. Bunescu School of Electrical Engineering and Computer Science

1 The Java Virtual Machine

Embedded Systems. Review of ANSI C Topics. A Review of ANSI C and Considerations for Embedded C Programming. Basic features of C

Advanced compiler construction. General course information. Teacher & assistant. Course goals. Evaluation. Grading scheme. Michel Schinz

C Compiler Targeting the Java Virtual Machine

The Advantages of Using NCL 2.3

language 1 (source) compiler language 2 (target) Figure 1: Compiling a program

Time Limit: X Flags: -std=gnu99 -w -O2 -fomitframe-pointer. Time Limit: X. Flags: -std=c++0x -w -O2 -fomit-frame-pointer - lm

The Java Virtual Machine and Mobile Devices. John Buford, Ph.D. Oct 2003 Presented to Gordon College CS 311

CS 241 Data Organization Coding Standards

Compiling Object Oriented Languages. What is an Object-Oriented Programming Language? Implementation: Dynamic Binding

Chapter 5 Names, Bindings, Type Checking, and Scopes

Lecture 1 Introduction to Android

C++FA 5.1 PRACTICE MID-TERM EXAM

Stack Allocation. Run-Time Data Structures. Static Structures

Chapter 1. Dr. Chris Irwin Davis Phone: (972) Office: ECSS CS-4337 Organization of Programming Languages

The Design of the Inferno Virtual Machine. Introduction

Crash Course in Java

C# and Other Languages

Semantic Analysis: Types and Type Checking

CS412/CS413. Introduction to Compilers Tim Teitelbaum. Lecture 20: Stack Frames 7 March 08

KITES TECHNOLOGY COURSE MODULE (C, C++, DS)

Last Class: OS and Computer Architecture. Last Class: OS and Computer Architecture

Sources: On the Web: Slides will be available on:

General Introduction

1/20/2016 INTRODUCTION

COMP 356 Programming Language Structures Notes for Chapter 10 of Concepts of Programming Languages Implementing Subprograms.

Embedded Programming in C/C++: Lesson-1: Programming Elements and Programming in C

Parameter Passing. Parameter Passing. Parameter Passing Modes in Fortran. Parameter Passing Modes in C

qwertyuiopasdfghjklzxcvbnmqwerty uiopasdfghjklzxcvbnmqwertyuiopasd fghjklzxcvbnmqwertyuiopasdfghjklzx cvbnmqwertyuiopasdfghjklzxcvbnmq

Habanero Extreme Scale Software Research Project

picojava TM : A Hardware Implementation of the Java Virtual Machine

Keil C51 Cross Compiler

02 B The Java Virtual Machine

Glossary of Object Oriented Terms

Chapter 7D The Java Virtual Machine

Programming Language Features (cont.) CMSC 330: Organization of Programming Languages. Parameter Passing in OCaml. Call-by-Value

Symbol Tables. Introduction

Object Oriented Software Design

CS 111 Classes I 1. Software Organization View to this point:

An Efficient Implementation of SELF, a Dynamically-Typed Object-Oriented Language Based on Prototypes *

Lecture 10: Dynamic Memory Allocation 1: Into the jaws of malloc()

Obfuscation: know your enemy

What Is Recursion? Recursion. Binary search example postponed to end of lecture

Open Source building blocks for the Internet of Things. Benjamin Cabé JFokus 2013

The Java Series. Java Essentials I What is Java? Basic Language Constructs. Java Essentials I. What is Java?. Basic Language Constructs Slide 1

GBIL: Generic Binary Instrumentation Language. Andrew Calvano. September 24 th, 2015

Departamento de Investigación. LaST: Language Study Tool. Nº 143 Edgard Lindner y Enrique Molinari Coordinación: Graciela Matich

Lecture Data Types and Types of a Language

1) The postfix expression for the infix expression A+B*(C+D)/F+D*E is ABCD+*F/DE*++

Moving from CS 61A Scheme to CS 61B Java

Free Java textbook available online. Introduction to the Java programming language. Compilation. A simple java program

1. Overview of the Java Language

Lecture 11 Doubly Linked Lists & Array of Linked Lists. Doubly Linked Lists

Boolean Expressions, Conditions, Loops, and Enumerations. Precedence Rules (from highest to lowest priority)

Free Java textbook available online. Introduction to the Java programming language. Compilation. A simple java program

Functional Programming. Functional Programming Languages. Chapter 14. Introduction

Object Oriented Software Design II

Name: Class: Date: 9. The compiler ignores all comments they are there strictly for the convenience of anyone reading the program.

Language Evaluation Criteria. Evaluation Criteria: Readability. Evaluation Criteria: Writability. ICOM 4036 Programming Languages

Multichoice Quetions 1. Atributes a. are listed in the second part of the class box b. its time is preceded by a colon. c. its default value is

Parameter passing in LISP

Data Structure with C

C++FA 3.1 OPTIMIZING C++

C++ INTERVIEW QUESTIONS

Object Oriented Software Design

Chapter 6: Programming Languages

System Structures. Services Interface Structure

COMPUTER SCIENCE TRIPOS

A GENERIC PURPOSE, CROSS-PLATFORM, HIGH EXTENSIBLE VIRTUAL MACHINE. John Vlachoyiannis

Software Engineering Techniques

CSE 452: Programming Languages. Acknowledgements. Contents. Java and its Evolution

Lua as a business logic language in high load application. Ilya Martynov ilya@iponweb.net CTO at IPONWEB

Coding Rules. Encoding the type of a function into the name (so-called Hungarian notation) is forbidden - it only confuses the programmer.

Comp 411 Principles of Programming Languages Lecture 34 Semantics of OO Languages. Corky Cartwright Swarat Chaudhuri November 30, 20111

Jonathan Worthington Scarborough Linux User Group

Lecture 9. Semantic Analysis Scoping and Symbol Table

Database Programming with PL/SQL: Learning Objectives

Sandy. The Malicious Exploit Analysis. Static Analysis and Dynamic exploit analysis. Garage4Hackers

Mouse Programming Mouse Interrupts Useful Mouse functions Mouselib.h Mouselib.c

Lecture 22: C Programming 4 Embedded Systems

Stacks. Linear data structures

Krishna Institute of Engineering & Technology, Ghaziabad Department of Computer Application MCA-213 : DATA STRUCTURES USING C

Raima Database Manager Version 14.0 In-memory Database Engine

QEMU, a Fast and Portable Dynamic Translator

EMSCRIPTEN - COMPILING LLVM BITCODE TO JAVASCRIPT (?!)

Fundamentals of Java Programming

Parallel and Distributed Computing Programming Assignment 1

PART-A Questions. 2. How does an enumerated statement differ from a typedef statement?

Objectives. Chapter 2: Operating-System Structures. Operating System Services (Cont.) Operating System Services. Operating System Services (Cont.

In this Chapter you ll learn:

Scoping (Readings 7.1,7.4,7.6) Parameter passing methods (7.5) Building symbol tables (7.6)

MPLAB Harmony System Service Libraries Help

Transcription:

An overview of Lua Julia Lawall DIKU February 7, 2008

Lua Pre-history (1992) DEL (Data-Entry Language) The problem: programmers had to enter data in columns. The goal: programmers type data into a relevant diagram. DEL describes places in a diagram and constraints on the values. Developers: Luiz Henrique de Figueiredo, et al. (Pontifical Catholic University of Rio de Janeiro, Brazil)

Lua Pre-history (1992), part 2 SOL (Simple Object Language) Language for translating data into human readable reports (specifying colors, fonts, etc). Developers: Roberto Ierusalimschy and Waldemar Celes (Pontifical Catholic University of Rio de Janeiro, Brazil.) DEL and SOL highlighted the need for a flexible, programmatic way of manipulating data.

Lua (1993-) Developers Goals Roberto Ierusalimschy, Luiz Henrique de Figueiredo, Waldemar Celes. (Computer graphics technology group, Pontifical Catholic University of Rio de Janeiro, Brazil.) Motivated by the need for more programming in DEL and SOL. the only language developed outside the industrialized world to have achieved global relevance. Simple syntax, small implementation. Extensibility. Fast compilation. Easy interaction with C.

Some examples A simple example width = 420 height = width * 3/2 color = "blue" A more complex example function Bound(w, h) if w < 20 then w = 20 elseif w > 500 then w = 500 end local minh = w*3/2 if h < minh then h = minh end return w, h end width, height = Bound(420, 500); if monochrome then color = "black" else color = "blue" end

Some language features Tables (associative arrays). Fallbacks. Functions. Register-based virtual machine. Interaction with C. Lua has garbage collection and programs are compiled to bytecode.

Tables Lua 1.1 (1994) @() @[2, 4, 9, 16, 25] @{name = "John", age = 35} @foo[2, 4, 9], @bar{name = "John", age = 35} Lua 2.1 (1995) { } {2, 4, 9, 16, 25} {name = "John", age = 35} {2, 4, 9; name = "John", age = 35} Lua 5.0 (2003) {2, 4, name = "John", age = 35, 9}

Using tables > info = {2, 4, name = "John", age = 35, 9} > = info[1], info[2], info[3] 2 4 9 > = info.name, info.age John 35 > john = info > john.birthdate = 2005-35 > = info.birthdate 1970

Table implementation Header key "x" nil value 9.2 value 100 200 300 nil

Fallbacks: error handling/language extension Lua 1.1 Lua 2.1 Lua 3.0 Lua 5.0 One hook for handling all errors. Host program can set a handler for each kind of error. Examples: arith, index, gettable, function Tags and tag methods. Apply to all objects with a specific tag. Metatables and metamethods. Attached to an individual object.

Implementing inheritance (Lua 2.1) function Index (a,i) if i == "parent" then return nil end local p = a.parent if type(p) == "table" then return p[i] else return nil end end setfallback("index", Index) -- avoid loops -- may trigger Index again -- set Index as a fallback a=window{x=100, y=200, color="red"} b=window{x=300, y=400, parent=a} print(b.color)

Implementing inheritance (Lua 3.0) function Index (a,i) if i == "parent" then return nil end... end -- set Index as a fallback inherit_by_parent = newtag() settagmethod(inherit_by_parent,"index", Index) a=window{x=100, y=200, color="red"} b=window{x=300, y=400, parent=a} settag(b,inherit_by_parent) print(b.color)

Implementing inheritance (Lua 5.0) function Index (a,i) if i == "parent" then return nil end... end a=window{x=100, y=200, color="red"} b=window{x=300, y=400, parent=a} setmetatable(b,{ index = Index}) print(b.color) Alternatively: a=window{x=100, y=200, color="red"} b=window{x=300, y=400} setmetatable(b,{ index = a}) print(b.color)

Functions Lua functions are first-class values. They can be: Passed as arguments Stored in tables Returned from functions Created at run time, since Lua 3.1 Lexically scoped, since Lua 5.0

Implementing first-class functions function fn () local x = 3 local returned = function () return x end x = 27 return returned end new_fn = fn() print(new_fn()) A function is implemented as a closure: the function text and an environment. Normally in Lua, local x is stored on the stack. That location is invalid when new fn is called. The creation-time value of x cannot be used, because of x = 27.

Lisp solution (define (fn) (let* ([x 3] [returned (lambda () x)]) (set! x 27) returned)) (define new_fn (fn)) (let ([x 452]) (printf "~a~n" (new_fn))) Variables get their value from their most recent definition at runtime. Result is 452. Difficult to understand and easy to abuse.

ML solution let fn() = let x = ref 3 in let returned = function () ->!x in x := 27; returned let new_fn = fn() let _ = Printf.printf "%d\n" (new_fn()) The value of x is a heap location, which persists. This value is copied into the closure. The value is still valid when new fn is called. The hard work is done by the programmer!

Scheme solution (define (fn) (let* ([x 3] [returned (lambda () x)]) (set! x 27) returned)) (define new_fn (fn)) (printf "~a~n" (new_fn)) Scan once to find the variables that can be stored on the stack and the variables that must be stored in the heap. Construct ML-like boxing and unboxing accordingly. Larger, slower, two-pass compiler! the cfa of Bigloo... is more than ten times larger than the whole Lua implementation

Lua 3.1 solution function fn () local x = 3 local returned = function () return %x end x = 27 return returned end new_fn = fn() print(new_fn()) Creation-time value of x is stored in the closure. print(new fn()) prints 3. Confusing, less expressive.

Lua 5.0 solution top pending vars pending vars x open upvalue top x closed upvalue stack x closure stack x closure

The Lua virtual machine Lua programs are compiled to bytecode. Compilation may be online or offline. Before Lua 5.0: a stack-based virtual machine. After Lua 5.0: a register-based virtual machine.

The Lua stack-based virtual machine (before Lua 5.0) Operators take their arguments from the stack. Operands must first be pushed on the stack. Advantages: Small, easily decoded instructions. Natural for nested computations. Disadvantages: Many instructions per operation. Much value copying, eg from variables to stack. a = a + i xxx 1: GETLOCAL 0 2: GETLOCAL 2 3: ADD 4: GETLOCAL 0 a = a + 1 1: GETLOCAL 0 2: ADDI 1 3: SETLOCAL 0

The Lua register-based virtual machine (since Lua 5.0) Operators and arguments in a single instruction. Each local variable has its own register (globals stored in a Lua table). Advantages: Few instructions. Little unnecessary value copying. Disadvantage: Complex instruction decoding. Temporaries still require stack-like handling, with the overhead of argument specifications. a = a + i xxx 1: ADD 0 0 2 a = a + 1 1: ADD 0 0 250

Performance comparison program Lua 4.0 Lua 5 Lua 5.0 sum (2e7) 1.23 0.54 (44%) 0.54 (44%) fibo (30) 0.95 0.68 (72%) 0.69 (73%) ack (8) 1.00 0.86 (86%) 0.88 (88%) random (1e6) 1.04 0.96 (92%) 0.96 (92%) sieve (100) 0.93 0.82 (88%) 0.57 (61%) heapsort (5e4) 1.08 1.05 (97%) 0.70 (65%) matrix (50) 0.84 0.82 (98%) 0.59 (70%) (See http://dada.perl.it/shootout/)

Interaction with the host program Issues: Using host functions in Lua. Using Lua values in host-language code. Using Lua functions in the host program. Invoke the Lua interpreter. Using host values in Lua code. Complex host values have the Lua type userdata.

C API: Using Lua values in the host-language code Lua values remain on a stack or other internal storage. Implementation (inaccessible to the host program): tag, value Project: lua tonumber extracts a float from a Lua value Test type: lua isnumber tests whether a Lua value represents a float Embed: lua pushnumber provides a float to Lua

C API: Using host functions in Lua void my_atan(void) { if (!lua_isnumber(lua_getparam(1))) lua_error("first arg not a number"); if (!lua_isnumber(lua_getparam(2))) lua_error("second arg not a number"); lua_pushnumber( atan2(lua_getnumber(lua_getparam(1)), lua_getnumber(lua_getparam(2)))); } lua_register("atan",my_atan); It would be nice to encapsulate this glue code in a function, but not possible in C.

Another example Lua function function foo(t) return t.x end C version: Lua 1.0 typedef struct Object *lua_object; // ptr into Lua memory void foo_l (void) { lua_object t = lua_getparam(1); lua_object r = lua_getfield(t, "x"); lua_pushobject(r); return; } Problem: lua Object value can be unexpectedly collected by a call back to Lua.

The example in Lua 2.1 Values for use in C code copied to a separate storage space. typedef unsigned int lua_object; // index into the storage space void foo_l (void) { lua_object t = lua_getparam(1); lua_pushobject(t); lua_pushstring("x"); lua_object r = lua_getsubscript(); lua_pushobject(r); return; } Storage space freed on the return from the C function. Values could be locked to be kept longer.

Problem with the Lua 2.1 solution Values stay in the storage space until the C function completes. Possible overflow. void l_loop (void) { lua_object f = lua_getparam(1); for(;;) { lua_object res; lua_callfunction(f); res = lua_getresult(1); if (lua_isnil(res)) break; printf("%s\n", lua_getstring(res)); } }

The example in Lua 4.0 The flat storage space is replaced by a stack. int foo_l (lua_state *L) { lua_pushstring(l, "x"); lua_gettable(l, 1); return 1; // the number of values to return to Lua } lua pop for removing values from the stack.

Lua and game programming Advantages of Lua: Portability. Efficiency and small size. Ease of embedding: Lua and C/C++ can each be used where appropriate. Simplicity. Stability. Convenient license. Coroutines, procedural representation of data. Lua developers did not target game programming.

Summary Goals Simple syntax, small implementation. Extensibility. Fast compilation. Easy interaction with C. Features Tables, fallbacks, functions. Language embedding Interacts with C, C++, and ML.

Literature R. Ierusalimschy, L. H. de Figueiredo, W. Celes, Lua 5.0 Reference Manual, Technical Report MCC-14/03, PUC-Rio, 2003. R. Ierusalimschy, L. H. de Figueiredo, W. Celes, Lua-an extensible extension language, Software: Practice & Experience 26 #6 (1996) 635-652. R. Ierusalimschy, L. H. de Figueiredo, W. Celes, The implementation of Lua 5.0, Journal of Universal Computer Science 11 #7 (2005) 1159-1176. Also Proceedings of IX Brazilian Symposium on Programming Languages (2005) 63-75. R. Ierusalimschy, L. H. de Figueiredo, W. Celes, The evolution of Lua, ACM HOPL III (2007). N. Ramsey, Embedding an Interpreted Language Using Higher-Order Functions and Types. ACM SIGPLAN 2003 Workshop on Interpreters, Virtual Machines and Emulators. www.lua.org