SlideShare uma empresa Scribd logo
1 de 18
Script up your application
with Lua!
OpenWest 2014
Ryan Erickson
http://www.untestedhacks.com
History of Lua
• Lua.org
• Created in 1993 at PUC-RIO
• Predecessors: DEL and Sol
• Lua == Moon, not an acronym.
• Focus:
– Simplicity for non-programmer audience
– Portability
– Designed to be easily embedded, extended
– Clean ANSI C code
– Garbage Collected
Why: Size
• To embed, hook to a few functions.
• Small as < 100kb DLL/LIB
• Runs on mobile / embedded devices and
platforms
Why: Performance
• Interpreted Lua is generally faster than
Python, Ruby, Perl, and PHP
• Not fast enough? LuaJIT!
• LuaJIT is in C++ / Java 6 territory.
– luajit.org
Why: Momentum
• Used extensively in games
• Adobe Lightroom (60% lua) and Photoshop
• First interpreted language allowed on iOS
• Angry Birds
• World of Warcraft
• Wikipedia lists over 100 games using Lua
• Nginx / OpenResty
• Control4 (us!)
• Many, Many more
Why: Safety
• Lua code runs in a sandbox.
• Embedder chooses which modules to expose.
• Host application can provide APIs / primitives
to Lua engine.
Control4’s use case
• Existing (C++) Driver architecture
• XML Driver ‘document’ + compiled driver
• Considered Python, JavaScript, and Lua
• Embedded Lua engine into C++ driver
• Embedded Lua code into the XML driver
– CDATA (non-XML) section read by driver on
startup
Intro: General
• Dynamically typed
• Whitespace is not significant.
– Spaces, linebreaks, tabs – use what you like
• Semicolons not required, discouraged
• -- single-line comment
• Variables are global by default, ‘local’ keyword
Intro: Types
• Few types:
number, string, boolean, nil, table, function, u
serdata
• Numbers are double by default
• Can represent Floats *and* Integers
• No i++, no i+=2. i = i + 1
Intro: Types
• Strings:
– Single-quote / Double-quote / backslash to escape
– String concatenation uses .. not +
• nil:
– Empty value
– Evaluates to ‘false’
– Frees item for garbage collection
Intro: Tables
• Lua Tables – Simultaneous array and hashmap
• a = ,“apple”, “banana”, “orange”-
• b = {lua = “cool”, java = “sucks”, fred = 3}
• c = a
• print(c[3], b.fred, b.*“java”+, a.grape)
• orange 3 sucks nil
Intro: Functions
• Functions are first-class objects
function add(a, b)
print(a + b)
end
multiply = function(a, b) print(a * b) end
plus = add
plus(3, 5)
-> 8
Control Structures
if … then … elseif .. else .. End
do -- block definition…
print(“one”)
print(“two”)
end
for i = 1, 10 do print(i) end
for k,v in pairs(,“apple”, “banana”-) do print(k,v) end
Control Structures Cont’d
i = 0
while i < 10 do
print(i)
i = i + 1
End
repeat
i = i - 1
print(i)
until i < 0
‘C Programmer’ Hangups
• No curly braces… What’s this ‘begin…end’
business?
• No +=, ++
• Not Equals is not !=, it’s ~=.
• Not is not ~, it’s not!
• Arrays start at 1
• #array is not always ‘right’
• Get past the hangups, and Lua is a fantastic little
language!
C Interface API
• Set of functions that allow C to interact with
Lua
– Functions to read and write Lua global variables
– Functions to call Lua functions
– Functions to register C functions within Lua
• Stack-based parameter passing
Demo
• C IRC Bot (~100 lines of C code), featureless
• Lua 5.1 source, previously compiled
• ~50 lines of Lua Integration C code
• 9k executable => ~200k with Lua + included libs
• Fast turnaround, feature development, etc.
Questions?
Ryan Erickson
ryan@untestedhacks.com
• Resources:
– http://lua.org
– http://lua.org/pil
– http://lua-users.org/wiki/LuaShortReference

Mais conteúdo relacionado

Destaque

淺入淺出 GDB
淺入淺出 GDB淺入淺出 GDB
淺入淺出 GDB
Jim Chang
 
Gdb tutorial-handout
Gdb tutorial-handoutGdb tutorial-handout
Gdb tutorial-handout
Suraj Kumar
 
C/C++调试、跟踪及性能分析工具综述
C/C++调试、跟踪及性能分析工具综述C/C++调试、跟踪及性能分析工具综述
C/C++调试、跟踪及性能分析工具综述
Xiaozhe Wang
 
TIP1 - Overview of C/C++ Debugging/Tracing/Profiling Tools
TIP1 - Overview of C/C++ Debugging/Tracing/Profiling ToolsTIP1 - Overview of C/C++ Debugging/Tracing/Profiling Tools
TIP1 - Overview of C/C++ Debugging/Tracing/Profiling Tools
Xiaozhe Wang
 

Destaque (16)

Nginx+lua在阿里巴巴的使用
Nginx+lua在阿里巴巴的使用Nginx+lua在阿里巴巴的使用
Nginx+lua在阿里巴巴的使用
 
淺入淺出 GDB
淺入淺出 GDB淺入淺出 GDB
淺入淺出 GDB
 
Gdb tutorial-handout
Gdb tutorial-handoutGdb tutorial-handout
Gdb tutorial-handout
 
Learn C Programming Language by Using GDB
Learn C Programming Language by Using GDBLearn C Programming Language by Using GDB
Learn C Programming Language by Using GDB
 
Perl在nginx里的应用
Perl在nginx里的应用Perl在nginx里的应用
Perl在nginx里的应用
 
基于OpenResty的百万级长连接推送
基于OpenResty的百万级长连接推送基于OpenResty的百万级长连接推送
基于OpenResty的百万级长连接推送
 
Nginx-lua
Nginx-luaNginx-lua
Nginx-lua
 
The basics and design of lua table
The basics and design of lua tableThe basics and design of lua table
The basics and design of lua table
 
高性能Web服务器Nginx及相关新技术的应用实践
高性能Web服务器Nginx及相关新技术的应用实践高性能Web服务器Nginx及相关新技术的应用实践
高性能Web服务器Nginx及相关新技术的应用实践
 
Using ngx_lua / lua-nginx-module in pixiv
Using ngx_lua / lua-nginx-module in pixivUsing ngx_lua / lua-nginx-module in pixiv
Using ngx_lua / lua-nginx-module in pixiv
 
給自己更好未來的 3 個練習:嵌入式作業系統設計、實做,與移植 (2015 年春季 ) 課程說明
給自己更好未來的 3 個練習:嵌入式作業系統設計、實做,與移植 (2015 年春季 ) 課程說明給自己更好未來的 3 個練習:嵌入式作業系統設計、實做,與移植 (2015 年春季 ) 課程說明
給自己更好未來的 3 個練習:嵌入式作業系統設計、實做,與移植 (2015 年春季 ) 課程說明
 
Load balancing in the SRE way
Load balancing in the SRE wayLoad balancing in the SRE way
Load balancing in the SRE way
 
Microservices & API Gateways
Microservices & API Gateways Microservices & API Gateways
Microservices & API Gateways
 
Making Linux do Hard Real-time
Making Linux do Hard Real-timeMaking Linux do Hard Real-time
Making Linux do Hard Real-time
 
C/C++调试、跟踪及性能分析工具综述
C/C++调试、跟踪及性能分析工具综述C/C++调试、跟踪及性能分析工具综述
C/C++调试、跟踪及性能分析工具综述
 
TIP1 - Overview of C/C++ Debugging/Tracing/Profiling Tools
TIP1 - Overview of C/C++ Debugging/Tracing/Profiling ToolsTIP1 - Overview of C/C++ Debugging/Tracing/Profiling Tools
TIP1 - Overview of C/C++ Debugging/Tracing/Profiling Tools
 

Semelhante a Script up your application with Lua! -- RyanE -- OpenWest 2014

Benefits/tutorial of Lua in games
Benefits/tutorial of Lua in gamesBenefits/tutorial of Lua in games
Benefits/tutorial of Lua in games
action.vn
 
Lua London Meetup 2013
Lua London Meetup 2013Lua London Meetup 2013
Lua London Meetup 2013
Cloudflare
 

Semelhante a Script up your application with Lua! -- RyanE -- OpenWest 2014 (20)

2015 555 kharchenko_ppt
2015 555 kharchenko_ppt2015 555 kharchenko_ppt
2015 555 kharchenko_ppt
 
JS introduction
JS introductionJS introduction
JS introduction
 
Functional Programming for Busy Object Oriented Programmers
Functional Programming for Busy Object Oriented ProgrammersFunctional Programming for Busy Object Oriented Programmers
Functional Programming for Busy Object Oriented Programmers
 
Benefits/tutorial of Lua in games
Benefits/tutorial of Lua in gamesBenefits/tutorial of Lua in games
Benefits/tutorial of Lua in games
 
Unleash your inner console cowboy
Unleash your inner console cowboyUnleash your inner console cowboy
Unleash your inner console cowboy
 
Lua London Meetup 2013
Lua London Meetup 2013Lua London Meetup 2013
Lua London Meetup 2013
 
High Performance Drupal
High Performance DrupalHigh Performance Drupal
High Performance Drupal
 
How do event loops work in Python?
How do event loops work in Python?How do event loops work in Python?
How do event loops work in Python?
 
Leonid Vasilyev "Building, deploying and running production code at Dropbox"
Leonid Vasilyev  "Building, deploying and running production code at Dropbox"Leonid Vasilyev  "Building, deploying and running production code at Dropbox"
Leonid Vasilyev "Building, deploying and running production code at Dropbox"
 
Working Effectively with Routine Tasks
Working Effectively with Routine TasksWorking Effectively with Routine Tasks
Working Effectively with Routine Tasks
 
Code for Startup MVP (Ruby on Rails) Session 2
Code for Startup MVP (Ruby on Rails) Session 2Code for Startup MVP (Ruby on Rails) Session 2
Code for Startup MVP (Ruby on Rails) Session 2
 
Use of Lua in Lab Devices
Use of Lua in Lab DevicesUse of Lua in Lab Devices
Use of Lua in Lab Devices
 
Эффективная работа с рутинными задачами
Эффективная работа с рутинными задачамиЭффективная работа с рутинными задачами
Эффективная работа с рутинными задачами
 
C++ overview
C++ overviewC++ overview
C++ overview
 
Python Programming for ArcGIS: Part I
Python Programming for ArcGIS: Part IPython Programming for ArcGIS: Part I
Python Programming for ArcGIS: Part I
 
Python and Oracle : allies for best of data management
Python and Oracle : allies for best of data managementPython and Oracle : allies for best of data management
Python and Oracle : allies for best of data management
 
Rapid Application Development using Ruby on Rails
Rapid Application Development using Ruby on RailsRapid Application Development using Ruby on Rails
Rapid Application Development using Ruby on Rails
 
HTTP cache: keeping it fresh
HTTP cache: keeping it freshHTTP cache: keeping it fresh
HTTP cache: keeping it fresh
 
Scala final ppt vinay
Scala final ppt vinayScala final ppt vinay
Scala final ppt vinay
 
Speed up large-scale ML/DL offline inference job with Alluxio
Speed up large-scale ML/DL offline inference job with AlluxioSpeed up large-scale ML/DL offline inference job with Alluxio
Speed up large-scale ML/DL offline inference job with Alluxio
 

Último

Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Victor Rentea
 

Último (20)

AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024
 
Cyberprint. Dark Pink Apt Group [EN].pdf
Cyberprint. Dark Pink Apt Group [EN].pdfCyberprint. Dark Pink Apt Group [EN].pdf
Cyberprint. Dark Pink Apt Group [EN].pdf
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 

Script up your application with Lua! -- RyanE -- OpenWest 2014

  • 1. Script up your application with Lua! OpenWest 2014 Ryan Erickson http://www.untestedhacks.com
  • 2. History of Lua • Lua.org • Created in 1993 at PUC-RIO • Predecessors: DEL and Sol • Lua == Moon, not an acronym. • Focus: – Simplicity for non-programmer audience – Portability – Designed to be easily embedded, extended – Clean ANSI C code – Garbage Collected
  • 3. Why: Size • To embed, hook to a few functions. • Small as < 100kb DLL/LIB • Runs on mobile / embedded devices and platforms
  • 4. Why: Performance • Interpreted Lua is generally faster than Python, Ruby, Perl, and PHP • Not fast enough? LuaJIT! • LuaJIT is in C++ / Java 6 territory. – luajit.org
  • 5. Why: Momentum • Used extensively in games • Adobe Lightroom (60% lua) and Photoshop • First interpreted language allowed on iOS • Angry Birds • World of Warcraft • Wikipedia lists over 100 games using Lua • Nginx / OpenResty • Control4 (us!) • Many, Many more
  • 6. Why: Safety • Lua code runs in a sandbox. • Embedder chooses which modules to expose. • Host application can provide APIs / primitives to Lua engine.
  • 7. Control4’s use case • Existing (C++) Driver architecture • XML Driver ‘document’ + compiled driver • Considered Python, JavaScript, and Lua • Embedded Lua engine into C++ driver • Embedded Lua code into the XML driver – CDATA (non-XML) section read by driver on startup
  • 8. Intro: General • Dynamically typed • Whitespace is not significant. – Spaces, linebreaks, tabs – use what you like • Semicolons not required, discouraged • -- single-line comment • Variables are global by default, ‘local’ keyword
  • 9. Intro: Types • Few types: number, string, boolean, nil, table, function, u serdata • Numbers are double by default • Can represent Floats *and* Integers • No i++, no i+=2. i = i + 1
  • 10. Intro: Types • Strings: – Single-quote / Double-quote / backslash to escape – String concatenation uses .. not + • nil: – Empty value – Evaluates to ‘false’ – Frees item for garbage collection
  • 11. Intro: Tables • Lua Tables – Simultaneous array and hashmap • a = ,“apple”, “banana”, “orange”- • b = {lua = “cool”, java = “sucks”, fred = 3} • c = a • print(c[3], b.fred, b.*“java”+, a.grape) • orange 3 sucks nil
  • 12. Intro: Functions • Functions are first-class objects function add(a, b) print(a + b) end multiply = function(a, b) print(a * b) end plus = add plus(3, 5) -> 8
  • 13. Control Structures if … then … elseif .. else .. End do -- block definition… print(“one”) print(“two”) end for i = 1, 10 do print(i) end for k,v in pairs(,“apple”, “banana”-) do print(k,v) end
  • 14. Control Structures Cont’d i = 0 while i < 10 do print(i) i = i + 1 End repeat i = i - 1 print(i) until i < 0
  • 15. ‘C Programmer’ Hangups • No curly braces… What’s this ‘begin…end’ business? • No +=, ++ • Not Equals is not !=, it’s ~=. • Not is not ~, it’s not! • Arrays start at 1 • #array is not always ‘right’ • Get past the hangups, and Lua is a fantastic little language!
  • 16. C Interface API • Set of functions that allow C to interact with Lua – Functions to read and write Lua global variables – Functions to call Lua functions – Functions to register C functions within Lua • Stack-based parameter passing
  • 17. Demo • C IRC Bot (~100 lines of C code), featureless • Lua 5.1 source, previously compiled • ~50 lines of Lua Integration C code • 9k executable => ~200k with Lua + included libs • Fast turnaround, feature development, etc.
  • 18. Questions? Ryan Erickson ryan@untestedhacks.com • Resources: – http://lua.org – http://lua.org/pil – http://lua-users.org/wiki/LuaShortReference