Lua , Rust, and D

22
Lua, Rust, and D Programming Languages and Design Paradigms Thomas Furman

description

Lua , Rust, and D. Programming Languages and Design Paradigms Thomas Furman. Designed and Developed by Roberto Ierusalimschy Pontifical Catholic University of Rio de Janeiro, Brazil Created in 1993; Publicly released in July 1994 Most Recent Release: Lua 5.2 Scripting Language - PowerPoint PPT Presentation

Transcript of Lua , Rust, and D

Page 1: Lua , Rust, and D

Lua, Rust, and DProgramming Languages and Design ParadigmsThomas Furman

Page 2: Lua , Rust, and D

Lua Introduction

• Designed and Developed by Roberto Ierusalimschy• Pontifical Catholic University of Rio de Janeiro, Brazil• Created in 1993; Publicly released in July 1994• Most Recent Release: Lua 5.2• Scripting Language• Multi-Paradigm Programming Language• Imperative and Functional

• Written in only 20,000 lines of code

Page 3: Lua , Rust, and D

Lua Portability

• Written in ANSI C• Utilizes C API• Interpreted via a Register-based Virtual Machine• Fastest Interpreted Scripting Language• Just-in-time compiler implementation available

• Runnable Cross-Platform using ANSI C• Embeddable into many other languages• C, C++, C#, Java, Smalltalk, Ada, Erlang, and more.

Page 4: Lua , Rust, and D

Lua Reserved Words and Typing

• Small set of reserved words• {and, break, do, else, elseif, end, false, for, function, if in,

local, nil, not or, repeat, return, then, true, until, while}• Dynamically typed language• Variables do not have a type explicitly assigned to them• Data types are assigned based on the value

• Atomic Data Types• Double-precision floating point numbers• Booleans• Strings

Page 5: Lua , Rust, and D

Lua Tables

• A built-in native data structure• Modeled after heterogeneous associative array• Uses Key and Data Pairing

• Table entries referred to by either• Key variable name string• Index number

• Table indices begin at 1 instead of 0

Page 6: Lua , Rust, and D

Lua Metatables

• Allow for user defined operations for specific values• For example:• “meta” + “table”• Use a metatable to define a operation to concatenate both

strings.• Metatables are stored as normal Lua tables• Lua’s form of creating objectivity and overriding

operators

Page 7: Lua , Rust, and D

Lua Functions and Closures

• Lua functions are classified as “First-Class” functions• Treated with high priority by the language• Given a significant degree of autonomy

• Closures are a form of keeping a value from a function intact after the function finishes execution

• Closure values are held in memory using special pointers• Called Upvalues

Page 8: Lua , Rust, and D

Lua Error Handling

• During compile time, an error will throw control back to the host and a C error code will be generated

• The user can explicitly generate an error using Lua functions

• These are similar to catch-try blocks that will throw potential errors

Page 9: Lua , Rust, and D

Lua Garbage Collection

• Garbage collection occurs automatically via mark-and-sweep method.

• Objects no longer reachable or with weak references are marked

• A table with only weak references is marked• Garbage collection can be called explicitly

Page 10: Lua , Rust, and D

Lua Coroutines

• Lua’s form of concurrency• Coroutine exists as a thread• Functions: Create, resume, or yield• Each coroutine has a personal stack• When a coroutine yields, its stack is set aside.

Page 11: Lua , Rust, and D

Rust Introduction

• Designed by Graydon Hoare• Develop started in 2006• Mozilla took over development in 2009• First released: January 2012• Current Release: Rust v0.8, September 2013• Supported primarily by the open source community• Major Application: Servo

Page 12: Lua , Rust, and D

Rust Paradigms and Goals

• Multi-paradigm language modeled after C and C++• Functional, Imperative, Object-Oriented

• Primary Goals:• Safe Memory Management• Safe Concurrency and Concurrent Controls

Page 13: Lua , Rust, and D

Rust Compiler

• The Rust Compiler, rustc, is written in Rust itself• Must use a precompiled “snapshot” version to install

itself• Uses the LLVM compiler on the backend• Compiles into a executable binary / machine code file

Page 14: Lua , Rust, and D

Rust Memory Management

• Claims to be a safe environment for memory• No Null, Dangling, or Wild Pointers• Objects are immutable by default• User has more explicit control over mutability

• Declared variables must be initialized prior to execution

Page 15: Lua , Rust, and D

Rust’s “Unsafe” Functions

• Functions are determined to be unsafe via specific behavior• Deferencing null or raw pointers• Data Races• Type Inheritance

• Using the “unsafe” keyword before a function allows the user to bypass the compiler’s strict enforcement

• The user is left to dealing with the integrity of the code

Page 16: Lua , Rust, and D

Rust’s Other Features

• First-Class Functions and Closures• Similar to Lua

• Algebraic data types (enums)• Class Traits• Similar to Java interfaces• Allows classes to share aspects

Page 17: Lua , Rust, and D

Rust’s Concurrency

• Tasks are Rust’s threads• Each task is given a stack and a heap• Stack Memory Allocation – A Slot• Heap Memory Allocation – A Box

• Tasks can share portions of their stacks with other tasks• However, these objects must be immutable

• Task States: Running, Blocked, Failing, Dead• A failing task has been interuptted by another process• A dead task is only viewable by other tasks

• Task Scheduling• Each task is given a finite time-slice• If a task doesn’t finish executing in that time, it is deferred until later

Page 18: Lua , Rust, and D

D Introduction

• Designed by Walter Bright• First Alpha Release December 2001• D1 Released in 2007• Current Release: D 2.063.2• Used in production by Facebook as of October 2013

Page 19: Lua , Rust, and D

D’s Paradigms and Goals

• D is modeled after C and C++• Originally meant as a reengineering of C++

• Multi-paradigm Language• Imperative• Mimics C’s procedural flow and syntact rules

• Object-Oriented• Single Inheritance and Interfaces like Java

• Meta-programming• Able to modify other programs during compile and run time

• Functional• Supports function literals, closures, immutable data types

• Concurrency• Creation and management of threads and their communication

Page 20: Lua , Rust, and D

D’s Compiler

• Built as a stand alone language after being modeled from C and C++

• The DMD compiler converts D source code into executable machine code

• GDC is a D front-end based gcc compiler• C and C++ source code cannot be natively compiled in D• D is able to be extended using the C Runtime Library

Page 21: Lua , Rust, and D

D’s New Features

• Auto-declaration of variables• Use the “auto” keyword in place of a data type in front of a

variable• Allows the compiler to choose the most appropriate data type

• Foreach keyword• Creates a loop to cycle through all entries in a data structure

• Scope Statements• Executable statements that occur when leaving a scope• Difference statements occur based on successful, failure, or

abnormal exits out of the scope• Useful in conjunction with error handling

Page 22: Lua , Rust, and D

D’s Garbage Collecting

• Garbage collector scans threads, stacks, and heaps for objects and pointers that are no longer used.

• Garbage collection occurs automatically and independently

• Negative Drawbacks in D:• Unpredictable when it runs• Requires all threads to block while garbage collection

occurs• May run only when memory is close to running out