Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
ruanhaishen
NLua
Commits
5ce70d75
Commit
5ce70d75
authored
Jul 18, 2014
by
Vinicius Jarina
Browse files
[NLua] Implemented operator + handling using metatable method __add.
- Missing tests
parent
0a4d984b
Changes
5
Hide whitespace changes
Inline
Side-by-side
ConsoleTest/Program.cs
View file @
5ce70d75
...
@@ -43,13 +43,16 @@ namespace ConsoleTest
...
@@ -43,13 +43,16 @@ namespace ConsoleTest
{
{
lua
.
DebugHook
+=
DebugHook
;
lua
.
DebugHook
+=
DebugHook
;
lua
.
SetDebugHook
(
NLua
.
Event
.
EventMasks
.
LUA_MASKLINE
,
0
);
lua
.
SetDebugHook
(
NLua
.
Event
.
EventMasks
.
LUA_MASKLINE
,
0
);
var
a
=
new
System
.
Numerics
.
Complex
(
10
,
0
);
lua
.
DoString
(
@"function testing_hooks() return 10 end
var
b
=
new
System
.
Numerics
.
Complex
(
0
,
3
);
val = testing_hooks()
var
x
=
a
+
b
;
val = val + 1
"
);
// lua.LoadCLRPackage ();
double
res
=
(
double
)
lua
[
"val"
];
lua
[
"a"
]
=
a
;
Console
.
WriteLine
(
"{0}"
,
res
);
lua
[
"b"
]
=
1
;
var
res
=
lua
.
DoString
(
@"return a + b"
)[
0
];
}
}
...
...
Core/NLua/Extensions/GeneralExtensions.cs
View file @
5ce70d75
...
@@ -55,6 +55,18 @@ namespace NLua.Extensions
...
@@ -55,6 +55,18 @@ namespace NLua.Extensions
#endif
#endif
}
}
static
class
TypeExtensions
{
public
static
bool
HasAdditionOpertator
(
this
Type
t
)
{
if
(
t
.
IsPrimitive
)
return
true
;
var
op_add
=
t
.
GetMethod
(
"op_Addition"
);
return
op_add
!=
null
;
}
}
static
class
StringExtensions
static
class
StringExtensions
{
{
public
static
IEnumerable
<
string
>
SplitWithEscape
(
this
string
input
,
char
separator
,
char
escapeCharacter
)
public
static
IEnumerable
<
string
>
SplitWithEscape
(
this
string
input
,
char
separator
,
char
escapeCharacter
)
...
...
Core/NLua/Lua.cs
View file @
5ce70d75
...
@@ -273,8 +273,8 @@ end
...
@@ -273,8 +273,8 @@ end
public
Lua
()
public
Lua
()
{
{
luaState
=
LuaLib
.
LuaLNewState
();
// steffenj: Lua 5.1.1 API change (lua_open is gone)
luaState
=
LuaLib
.
LuaLNewState
();
LuaLib
.
LuaLOpenLibs
(
luaState
);
// steffenj: Lua 5.1.1 API change (luaopen_base is gone, just open all libs right here)
LuaLib
.
LuaLOpenLibs
(
luaState
);
Init
();
Init
();
// We need to keep this in a managed reference so the delegate doesn't get garbage collected
// We need to keep this in a managed reference so the delegate doesn't get garbage collected
panicCallback
=
new
LuaNativeFunction
(
PanicCallback
);
panicCallback
=
new
LuaNativeFunction
(
PanicCallback
);
...
...
Core/NLua/Metatables.cs
100644 → 100755
View file @
5ce70d75
...
@@ -54,7 +54,7 @@ namespace NLua
...
@@ -54,7 +54,7 @@ namespace NLua
public
class
MetaFunctions
public
class
MetaFunctions
{
{
internal
LuaNativeFunction
gcFunction
,
indexFunction
,
newindexFunction
,
baseIndexFunction
,
internal
LuaNativeFunction
gcFunction
,
indexFunction
,
newindexFunction
,
baseIndexFunction
,
classIndexFunction
,
classNewindexFunction
,
execDelegateFunction
,
callConstructorFunction
,
toStringFunction
;
classIndexFunction
,
classNewindexFunction
,
execDelegateFunction
,
callConstructorFunction
,
toStringFunction
,
addFunction
;
private
Dictionary
<
object
,
object
>
memberCache
=
new
Dictionary
<
object
,
object
>
();
private
Dictionary
<
object
,
object
>
memberCache
=
new
Dictionary
<
object
,
object
>
();
private
ObjectTranslator
translator
;
private
ObjectTranslator
translator
;
...
@@ -89,7 +89,8 @@ namespace NLua
...
@@ -89,7 +89,8 @@ namespace NLua
callConstructorFunction
=
new
LuaNativeFunction
(
MetaFunctions
.
CallConstructor
);
callConstructorFunction
=
new
LuaNativeFunction
(
MetaFunctions
.
CallConstructor
);
classIndexFunction
=
new
LuaNativeFunction
(
MetaFunctions
.
GetClassMethod
);
classIndexFunction
=
new
LuaNativeFunction
(
MetaFunctions
.
GetClassMethod
);
classNewindexFunction
=
new
LuaNativeFunction
(
MetaFunctions
.
SetClassFieldOrProperty
);
classNewindexFunction
=
new
LuaNativeFunction
(
MetaFunctions
.
SetClassFieldOrProperty
);
execDelegateFunction
=
new
LuaNativeFunction
(
MetaFunctions
.
RunFunctionDelegate
);
execDelegateFunction
=
new
LuaNativeFunction
(
MetaFunctions
.
RunFunctionDelegate
);
addFunction
=
new
LuaNativeFunction
(
MetaFunctions
.
AddLua
);
}
}
/*
/*
...
@@ -158,6 +159,42 @@ namespace NLua
...
@@ -158,6 +159,42 @@ namespace NLua
LuaLib
.
LuaPushNil
(
luaState
);
LuaLib
.
LuaPushNil
(
luaState
);
return
1
;
return
1
;
}
/*
* __tostring metafunction of CLR objects.
*/
#if MONOTOUCH
[
MonoTouch
.
MonoPInvokeCallback
(
typeof
(
LuaNativeFunction
))]
#endif
[
System
.
Runtime
.
InteropServices
.
AllowReversePInvokeCalls
]
static
int
AddLua
(
LuaState
luaState
)
{
var
translator
=
ObjectTranslatorPool
.
Instance
.
Find
(
luaState
);
return
AddLua
(
luaState
,
translator
);
}
static
int
AddLua
(
LuaState
luaState
,
ObjectTranslator
translator
)
{
object
obj1
=
translator
.
GetRawNetObject
(
luaState
,
1
);
object
obj2
=
translator
.
GetRawNetObject
(
luaState
,
2
);
if
(
obj1
==
null
||
obj2
==
null
)
{
translator
.
ThrowError
(
luaState
,
"Cannot add a nil object"
);
LuaLib
.
LuaPushNil
(
luaState
);
return
1
;
}
Type
type
=
obj1
.
GetType
();
MethodInfo
opAddition
=
type
.
GetMethod
(
"op_Addition"
);
if
(
opAddition
==
null
)
{
translator
.
ThrowError
(
luaState
,
"Cannot add object ("
+
type
.
Name
+
" does not overload the operator +)"
);
LuaLib
.
LuaPushNil
(
luaState
);
return
1
;
}
obj1
=
opAddition
.
Invoke
(
obj1
,
new
object
[]
{
obj1
,
obj2
});
translator
.
Push
(
luaState
,
obj1
);
return
1
;
}
}
...
...
Core/NLua/ObjectTranslator.cs
View file @
5ce70d75
...
@@ -638,7 +638,8 @@ namespace NLua
...
@@ -638,7 +638,8 @@ namespace NLua
LuaLib
.
LuaRawSet
(
luaState
,
-
3
);
LuaLib
.
LuaRawSet
(
luaState
,
-
3
);
LuaLib
.
LuaPushString
(
luaState
,
"__newindex"
);
LuaLib
.
LuaPushString
(
luaState
,
"__newindex"
);
LuaLib
.
LuaPushStdCallCFunction
(
luaState
,
metaFunctions
.
newindexFunction
);
LuaLib
.
LuaPushStdCallCFunction
(
luaState
,
metaFunctions
.
newindexFunction
);
LuaLib
.
LuaRawSet
(
luaState
,
-
3
);
LuaLib
.
LuaRawSet
(
luaState
,
-
3
);
RegisterOperatorsFunctions
(
luaState
,
o
.
GetType
());
}
}
}
else
}
else
LuaLib
.
LuaLGetMetatable
(
luaState
,
metatable
);
LuaLib
.
LuaLGetMetatable
(
luaState
,
metatable
);
...
@@ -655,6 +656,15 @@ namespace NLua
...
@@ -655,6 +656,15 @@ namespace NLua
LuaLib
.
LuaRemove
(
luaState
,
-
2
);
LuaLib
.
LuaRemove
(
luaState
,
-
2
);
}
}
void
RegisterOperatorsFunctions
(
LuaState
luaState
,
Type
type
)
{
if
(
type
.
HasAdditionOpertator
())
{
LuaLib
.
LuaPushString
(
luaState
,
"__add"
);
LuaLib
.
LuaPushStdCallCFunction
(
luaState
,
metaFunctions
.
addFunction
);
LuaLib
.
LuaRawSet
(
luaState
,
-
3
);
}
}
/*
/*
* Gets an object from the Lua stack with the desired type, if it matches, otherwise
* Gets an object from the Lua stack with the desired type, if it matches, otherwise
* returns null.
* returns null.
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment