Lua basics for interview questions

1. The use of metatable to realize an object-oriented class

The process of Lua looking up table elements

Basic learning: Lua object-oriented programming (__index, __newindex, __call, __add)_ccsu_deer-CSDN blog

Practice: use __index to achieve inheritance

--继承关系
Shape = {area = 0}
function Shape:new(o, side)
	o = o or {}
	setmetatable(o, self)
	self.__index = self  --保持继承链,不加此行, t1就没有__index元方法,下面的t2 = t1:new({}, 4)会报错
	side = side or 0
	o.area = side * side
	return o
end
 
t1 = Shape:new({key1 = 1}, 3)
print("t1.area:", t1.area)
print("t1.key1:", t1.key1)
t2 = t1:new({key2 = 2}, 4)
print("t2.key1:", t2.key1)
print("t2.key2:", t2.key2)
t3 = t2:new({key3 = 3}, 5)
 
print("t3.key1:", t3.key1)
print("t3.key2:", t3.key2)
print("t3.key3:", t3.key3)

2. The use of metamethods

Use setmetatable to overload the relevant operation functions in the metatable

like:

__add(a, b) corresponds to the expression a + b

__sub(a, b) corresponds to the expression a - b

__mul(a, b) corresponds to the expression a * b

__div(a, b) corresponds to the expression a / b

__mod(a, b) corresponds to the expression a % b

__pow(a, b) corresponds to the expression a ^ b

__unm(a) corresponds to the expression -a

__concat(a, b) corresponds to the expression a .. b

__len(a) corresponds to the expression #a

__eq(a, b) corresponds to the expression a == b

__lt(a, b) corresponds to the expression a < b

__le(a, b) corresponds to the expression a <= b

Example of overloading __add:

--只读table
 
t = setmetatable({1, 2, 3},{
	__add = function(self, newtable)
		len1 = #self
		len2 = #newtable
		sum = 0
		for i=1, len1 do
			sum = sum + self[i]
		end
		for i=1, len2 do
			sum = sum + newtable[i]
		end
		return sum
 
	end
})
 
 
t1 = {2,3,4}
 
answer = t + t1
print("answer:", answer)

 3. Lua's GC

Three-color marking method: In-depth exploration of Lua's GC algorithm_jinxinliu1's column-CSDN blog_lua's gc

1. The principle of GC and its algorithm design

Different languages ​​have different designs for GC algorithms. Common GC algorithms are reference counting and Mark-Sweep algorithms. C# uses the Mark-sweep && compact algorithm, and Lua uses the Mark-sweep algorithm. Let’s talk about it separately:

Reference counting algorithm: When an object is referenced, its reference count is increased by 1, otherwise, it is decreased by 1. If the count value is 0, it will be recycled during GC. One problem with this algorithm is circular references.

Mark-sweep algorithm: Every time a GC is performed, all objects are scanned once. If there is no reference to the object, it will be recycled, otherwise it will be saved.

In Lua5.0 and earlier versions, Lua's GC is a one-time process that cannot be interrupted. The Mark algorithm used is a two-color mark algorithm (Two color mark), so that the objects in the system are either black or white, or Be referenced, or not be referenced, this will bring a problem: if a new object is added during the GC process, no matter how the newly added object is set at this time, it will cause problems. If it is set to white, if it is in the recycling phase, Then the object will be recycled without traversing its associated objects; if it is marked black, it will be marked as non-recyclable without being scanned, which is incorrect.

In order to reduce the performance problems caused by one-time recycling and the problems of the two-color algorithm, after Lua5.1, Lua uses distributed recycling and tri-color incremental mark and sweep algorithm (Tri-color incremental mark and sweep)

The basic principle pseudo code, the original text in the reference book is:

每个新创建的对象颜色设置为白色

//初始化阶段

遍历root节点中引用的对象,从白色置为灰色,并且放入到灰色节点列表中

//标记阶段

while(灰色链表中还有未扫描的元素):

从中取出一个对象,将其置为黑色

遍历这个对象关联的其他所有对象:

if 为白色

标记为灰色,加入到灰色链表中(insert to the head)



//回收阶段

遍历所有对象:

if 为白色,

没有被引用的对象,执行回收

else

重新塞入到对象链表中,等待下一轮GC

4. Mutual call between Lua and C++

Lua and C call each other - ccsu_deer-CSDN Blog

5. setfenv and getfenv

6. Realization and process of hot update

1. Compare the difference files

2. Download

3. Unzip

4. Delete old files and move new files

5. Delete redundant files

6. Reload the script

1. Enter the app for the first time, initialize the version number version=0

2. socket::send() sends the version number to the server for version verification

3. Check the server version number. If the app version number is inconsistent with the server version number, download the resource list

4. The socket::receive() app checks whether the resource to be downloaded already exists in the version resource. If it exists, the corresponding resource is not downloaded; if it does not exist, it is downloaded. Case 2: Remove the existing local version, and then download it

5. The app reads resources and downloads resources 
 

Guess you like

Origin blog.csdn.net/qq_41286356/article/details/123312293