I++ Talk

The programmer`s three romantic works are
OS, Compiler and CG.
-- So said the VCZH.

Why use scripting language

Chrome
Network, Webkit, V8, etc. (Code in Javascript)

C++ VS Lua

C++

Jobs:

  • Class Hierarchy (OOP Design)
  • Data Structure & Algorithm
  • Low Level Graphic/Audio/Hardware Interface
  • Graphic: Vertex Info, Render States, Shader, e.g. draw() method
  • Audio: Codec, Sample, Streaming
  • Game Engine Structures
  • Scene Tree
  • Scheduler
  • ActionManager
  • TouchDispather
  • Physics Structure
  • AudioEngine
  • AI Structure
  • ContentManager
  • etc

The Problems:

Too many syntax details of C++ language.

  • Strongly typed
  • Strict type conversion rules
  • Low level API
  • Bloated syntax to use
  • Uneasy ways for memory management. (unique|shared|weak Pointers, Circular Reference)
  • Take more codes to write a simple function. (Life is too short)
  • Lots of compile time to waste. (Life is too short)

Our business, the changing game logic

Simple codes to call engine APIs
and operate on data easier.

Lua

Jobs:
Above

Solve the Problems:

  • Simple syntax. (Simpler than Javascript)
  • Code in either OOP or FP Styles. (Dealing with business faster. There are not many scenarios for OOP. It`s better to organize objects by aggregation/composition instead of inheritance.)
  • Better memory management. (Reference Chain, Mark Sweep, Less concerning memory issues)
  • Code less.(with Moonscript, code less and less)
  • Run time compile.(JIT)

Likely 40% of C Performance by luajit

Embed Lua in C++

  • APIs Binding to Lua. (Require Knowledges of Lua structures)
  • Cross language interactions. (Invocations, passing data)
  • Simulating C++ class system.
  • Object life time management.
  • Some traps. (Isolated references for Lua userdata and C++ object)

Memory Model

Lua Usage

coroutine

-- basic usage
local f1,f2

f1 = function()
	print("Hello")
	print("World")
end

f2 = function()
	print("I++")
	print("Club")
end

f1()
f2()

print("Try again:")

f1 = coroutine.wrap(function()
	print("Hello")
	coroutine.yield()
	print("World")
end)

f2 = coroutine.wrap(function()
	print("I++")
	coroutine.yield()
	print("Club")
end)

f1()
f2()
f1()
f2()

--[[
output:
Hello
I++
World
Club
]]

-- a simple framework for multi-threading
local yield = coroutine.yield
local routines = {}

local function spawn(f) -- create thread
	table.insert(routines,coroutine.wrap(f))
end

local function execute() -- run threads (main process)
	repeat
		local i = 1
		while i <= #routines do
			if routines[i]() then -- run a thread one time and tell whether it`s ended
				table.remove(routines,i)
			else
				i = i + 1
			end
		end
	until #routines == 0 -- routines remain to go
end

-- test codes
spawn(function() -- thread 1
	for i = 1, 4 do
		print("R1: step "..tostring(i))
		yield()
	end

	print("R1: end")
	return true -- return true to stop
end)

spawn(function() -- thread 2
	print("R2: step 1")
	yield()
	print("R2: step 2")
	yield()

	print("R2: step 3 begin")
	spawn(function() -- thread 3
		print("R3: step 1")
		yield()
		print("R3: step 2")
		yield()

		print("R3: end")
		return true
	end)
	print("R2: step 3 end")
	yield()

	print("R2: end")
	return true
end)

execute() -- run

Non Blocking IO without callbacks

  • IO bound
  • CPU bound
  • Don`t make CPU wait for IO
oRoutine(function()
	print("start loading")
	oCache:loadAsync("image.png")
	print("image loaded")
	local sprite = CCSprite("image.png")
	...
end)

local function loadAsync(filename)
	local loaded = false
	local callback = function()
		loaded = true
	end
	readFile(filename,callback) -- read file in a real thread with blocking IO API
	                                        -- invoke callback when finished
	while not loaded do
		yield()
	end
end

Parallel Processing in Lua

  • Lua codes generate pure Lua data structure without context (the object reference chain).
  • Create multiple Lua VMs, each VM contains a running stack and a lua data structure.
标题目录