Skip to content

Commit

Permalink
add Slua.Class for support inherit class
Browse files Browse the repository at this point in the history
  • Loading branch information
pangweiwei committed Apr 1, 2015
1 parent 20fc806 commit d78aff3
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 2 deletions.
Binary file modified Assets/Plugins/x86/slua.dll
Binary file not shown.
21 changes: 21 additions & 0 deletions Assets/Slua/Resources/main.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,20 @@ local somemodule = require 'module.some'

import "UnityEngine"

MyVector3=Slua.Class(Vector3,
nil, --static function
{ --instance member function
Normalize=function(self)
print "overloaded Normalize"
local l=math.sqrt(self.x*self.x+self.y*self.y,self.z*self.z)
self.x=self.x/l
self.y=self.y/l
self.z=self.z/l
end,
}
)


function main()

if jit then
Expand All @@ -10,6 +24,11 @@ function main()
print("using lua")
end

-- test inherite class
local mv = MyVector3(1,2,3)
mv:Normalize()
print(mv.x,mv.y,mv.z)

-- test value type
local x=Vector3(3,3,3)
x:Normalize()
Expand Down Expand Up @@ -99,6 +118,8 @@ function main()
local y = y:y()
Yield(y)
print("Yield ok")

HelloWorld.setv({name="yes",value=12})
end)
coroutine.resume(c)

Expand Down
43 changes: 43 additions & 0 deletions Assets/Slua/Script/Helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,39 @@ namespace SLua
class Helper : LuaObject
{

static string classfunc = @"
local getmetatable=getmetatable
local function Class(base,static,instance)
local mt = getmetatable(base)
local class=static or {}
setmetatable(class,
{
__call=function(...)
local r = mt.__call(...)
local ret = instance or {}
ret.__base=r
local ret = setmetatable(ret,{
__index=function(t,k)
return r[k]
end,
__newindex=function(t,k,v)
r[k]=v
end,
})
return ret
end,
}
)
return class
end
return Class
";

[MonoPInvokeCallbackAttribute(typeof(LuaCSFunction))]
static public int _iter(IntPtr l)
{
Expand Down Expand Up @@ -142,6 +175,16 @@ static public void reg(IntPtr l)
reg(l, CreateClass, "Slua");
reg(l, iter, "Slua");
reg(l, ToString, "Slua");


newTypeTable(l, "Slua");
if (LuaDLL.luaL_dostring(l, classfunc) != 0)
{
throwLuaError(l);
return;
}
LuaDLL.lua_setfield(l, -2, "Class");
LuaDLL.lua_pop(l, 1);
}
}
}
2 changes: 1 addition & 1 deletion Assets/Slua/Script/LuaObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -508,7 +508,7 @@ protected static void addMember(IntPtr l, int v, string name)
LuaDLL.lua_setfield(l, -2, name);
}

static void throwLuaError(IntPtr l)
internal static void throwLuaError(IntPtr l)
{
string err = LuaDLL.lua_tostring(l, -1);
LuaDLL.lua_pop(l, 1);
Expand Down
2 changes: 1 addition & 1 deletion Assets/Slua/slua_src/slua.c
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ static void setelement(lua_State* L, int p, float v, const char* key) {
if (!isnan(v)) {
lua_pushstring(L, key);
lua_pushnumber(L, v);
lua_rawset(L, p);
lua_settable(L, p);
}
}

Expand Down

0 comments on commit d78aff3

Please sign in to comment.