LiteLoaderBDS-1.16.40/Tools/ScriptX/docs/zh/Lua.md
2023-03-03 10:18:21 -08:00

125 lines
2.5 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# Lua语言
ScriptX和Lua语言类型对照表
| Lua | ScriptX |
| :--: | :--: |
| nil | Null |
| table | Object |
| table | Array |
| string | String |
| number | Number |
| boolean | Boolean |
| function | Function |
## 1-based or 0-based?
Lua语言使用1作为基数但是C++语言是用0作为基数。因此在ScriptX中对于数据等的操作仍然使用0-based逻辑。
比如:
```c++
Local<Array> arr;
arr.set(0, "hello");
// lua
arr[1] == "hello";
```
## Lua函数多返回值
Lua函数支持多返回值但是ScriptX的API只支持返回 `Local<Value>`。
当Lua函数返回多个值的时候ScriptX会把返回值用Array包起来。
## ByteBuffer
Lua语言没有ByteBuffer相关的API因此ScriptX通过 binding API提供了一套实现。
同时在LuaEngine的构造函数里使用者也可以传入自己的delegate实现ByteBuffer相关API
## instanceOf
Lua语言没有内建的 `instanceof` 操作符为了实现相应能力ScriptX会在Lua全局增加一个`ScriptX`工具类。
`ScriptX.isInstanceOf(instance, Class)`
```c++
// C++
class InstanceOfTest : public script::ScriptClass {
public:
using ScriptClass::ScriptClass;
};
auto instanceOfTestDefine =
defineClass<InstanceOfTest>("InstanceOfTest")
.constructor()
.build();
```
``` lua
# lua
local ins = InstanceOfTest();
ScriptX.isInstanceOf(ins, InstanceOfTest) == true;
```
## 绑定类的MetaData
由于Lua没有标准的OO对于一个ClassDefineScriptX会在lua中定义如下声明
```lua
Class = {}
local staticMeta = {
// constructor call
__call = function()
local ins = {};
setmetatable(inx, instanceMeta);
return ins
end
}
for staticFunc do
Class[staticFunc.name] = function() ... end
end
setmetatable(Class, staticMeta)
local instanceMeta = {
__index = function()
1. find from `ClassDefine.instanceProperty`
2. find from `ClassDefine.instanceFunction`
3. return nil
end
__newindex = function()
1. find from `ClassDefine.instanceProperty`
2. raw set to table
__gc = function()
1. delete this
end
instanceFunction = {...}
}
ScriptX.getInstanceMeta(Class) == instanceMeta;
```
比如,要扩展一个绑定的类,可以如下:
```lua
local meta = ScriptX.getInstanceMeta(Class)
function meta:__add() print(self, "add") end
function meta.instanceFunction:hello()
print(self, "hello")
end
// test
local ext = Class()
x = ext + ext
ext:hello();
// output
table: 0x7fc873c18ed0 add
table: 0x7fc873c18ed0 hello
```