Commit 4cfc1f2b authored by Mangatome's avatar Mangatome
Browse files

Silverlight fixes

- Replaces for all platforms the usage of legacy collections (ArrayList,
Hashtables, and ListDictionary) by their generic and recommanded
equivalents.
- Fixes calls which are unsupported in Silverlight.
parent 96e10fff
...@@ -45,31 +45,35 @@ namespace NLua ...@@ -45,31 +45,35 @@ namespace NLua
*/ */
class CheckType class CheckType
{ {
#if SILVERLIGHT
private Dictionary<Type, ExtractValue> extractValues = new Dictionary<Type, ExtractValue>();
#else
private Dictionary<long, ExtractValue> extractValues = new Dictionary<long, ExtractValue> (); private Dictionary<long, ExtractValue> extractValues = new Dictionary<long, ExtractValue> ();
#endif
private ExtractValue extractNetObject; private ExtractValue extractNetObject;
private ObjectTranslator translator; private ObjectTranslator translator;
public CheckType (ObjectTranslator translator) public CheckType (ObjectTranslator translator)
{ {
this.translator = translator; this.translator = translator;
extractValues.Add (typeof(object).TypeHandle.Value.ToInt64 (), new ExtractValue (getAsObject)); extractValues.Add(getExtractDictionaryKey(typeof(object)), new ExtractValue(getAsObject));
extractValues.Add (typeof(sbyte).TypeHandle.Value.ToInt64 (), new ExtractValue (getAsSbyte)); extractValues.Add(getExtractDictionaryKey(typeof(sbyte)), new ExtractValue(getAsSbyte));
extractValues.Add (typeof(byte).TypeHandle.Value.ToInt64 (), new ExtractValue (getAsByte)); extractValues.Add(getExtractDictionaryKey(typeof(byte)), new ExtractValue(getAsByte));
extractValues.Add (typeof(short).TypeHandle.Value.ToInt64 (), new ExtractValue (getAsShort)); extractValues.Add(getExtractDictionaryKey(typeof(short)), new ExtractValue(getAsShort));
extractValues.Add (typeof(ushort).TypeHandle.Value.ToInt64 (), new ExtractValue (getAsUshort)); extractValues.Add(getExtractDictionaryKey(typeof(ushort)), new ExtractValue(getAsUshort));
extractValues.Add (typeof(int).TypeHandle.Value.ToInt64 (), new ExtractValue (getAsInt)); extractValues.Add(getExtractDictionaryKey(typeof(int)), new ExtractValue(getAsInt));
extractValues.Add (typeof(uint).TypeHandle.Value.ToInt64 (), new ExtractValue (getAsUint)); extractValues.Add(getExtractDictionaryKey(typeof(uint)), new ExtractValue(getAsUint));
extractValues.Add (typeof(long).TypeHandle.Value.ToInt64 (), new ExtractValue (getAsLong)); extractValues.Add(getExtractDictionaryKey(typeof(long)), new ExtractValue(getAsLong));
extractValues.Add (typeof(ulong).TypeHandle.Value.ToInt64 (), new ExtractValue (getAsUlong)); extractValues.Add(getExtractDictionaryKey(typeof(ulong)), new ExtractValue(getAsUlong));
extractValues.Add (typeof(double).TypeHandle.Value.ToInt64 (), new ExtractValue (getAsDouble)); extractValues.Add(getExtractDictionaryKey(typeof(double)), new ExtractValue(getAsDouble));
extractValues.Add (typeof(char).TypeHandle.Value.ToInt64 (), new ExtractValue (getAsChar)); extractValues.Add(getExtractDictionaryKey(typeof(char)), new ExtractValue(getAsChar));
extractValues.Add (typeof(float).TypeHandle.Value.ToInt64 (), new ExtractValue (getAsFloat)); extractValues.Add(getExtractDictionaryKey(typeof(float)), new ExtractValue(getAsFloat));
extractValues.Add (typeof(decimal).TypeHandle.Value.ToInt64 (), new ExtractValue (getAsDecimal)); extractValues.Add(getExtractDictionaryKey(typeof(decimal)), new ExtractValue(getAsDecimal));
extractValues.Add (typeof(bool).TypeHandle.Value.ToInt64 (), new ExtractValue (getAsBoolean)); extractValues.Add(getExtractDictionaryKey(typeof(bool)), new ExtractValue(getAsBoolean));
extractValues.Add (typeof(string).TypeHandle.Value.ToInt64 (), new ExtractValue (getAsString)); extractValues.Add(getExtractDictionaryKey(typeof(string)), new ExtractValue(getAsString));
extractValues.Add (typeof(LuaFunction).TypeHandle.Value.ToInt64 (), new ExtractValue (getAsFunction)); extractValues.Add(getExtractDictionaryKey(typeof(LuaFunction)), new ExtractValue(getAsFunction));
extractValues.Add (typeof(LuaTable).TypeHandle.Value.ToInt64 (), new ExtractValue (getAsTable)); extractValues.Add(getExtractDictionaryKey(typeof(LuaTable)), new ExtractValue(getAsTable));
extractValues.Add (typeof(LuaUserData).TypeHandle.Value.ToInt64 (), new ExtractValue (getAsUserdata)); extractValues.Add(getExtractDictionaryKey(typeof(LuaUserData)), new ExtractValue(getAsUserdata));
extractNetObject = new ExtractValue (getAsNetObject); extractNetObject = new ExtractValue (getAsNetObject);
} }
...@@ -87,8 +91,8 @@ namespace NLua ...@@ -87,8 +91,8 @@ namespace NLua
if (paramType.IsByRef) if (paramType.IsByRef)
paramType = paramType.GetElementType (); paramType = paramType.GetElementType ();
long runtimeHandleValue = paramType.TypeHandle.Value.ToInt64 (); var extractKey = getExtractDictionaryKey(paramType);
return extractValues.ContainsKey (runtimeHandleValue) ? extractValues [runtimeHandleValue] : extractNetObject; return extractValues.ContainsKey(extractKey) ? extractValues[extractKey] : extractNetObject;
} }
internal ExtractValue checkType (LuaCore.lua_State luaState, int stackPos, Type paramType) internal ExtractValue checkType (LuaCore.lua_State luaState, int stackPos, Type paramType)
...@@ -103,47 +107,47 @@ namespace NLua ...@@ -103,47 +107,47 @@ namespace NLua
if (!underlyingType.IsNull ()) if (!underlyingType.IsNull ())
paramType = underlyingType; // Silently convert nullable types to their non null requics paramType = underlyingType; // Silently convert nullable types to their non null requics
long runtimeHandleValue = paramType.TypeHandle.Value.ToInt64 (); var extractKey = getExtractDictionaryKey (paramType);
if (paramType.Equals (typeof(object))) if (paramType.Equals (typeof(object)))
return extractValues [runtimeHandleValue]; return extractValues [extractKey];
//CP: Added support for generic parameters //CP: Added support for generic parameters
if (paramType.IsGenericParameter) { if (paramType.IsGenericParameter) {
if (luatype == LuaTypes.Boolean) if (luatype == LuaTypes.Boolean)
return extractValues [typeof(bool).TypeHandle.Value.ToInt64 ()]; return extractValues [getExtractDictionaryKey (typeof(bool))];
else if (luatype == LuaTypes.String) else if (luatype == LuaTypes.String)
return extractValues [typeof(string).TypeHandle.Value.ToInt64 ()]; return extractValues[getExtractDictionaryKey (typeof(string))];
else if (luatype == LuaTypes.Table) else if (luatype == LuaTypes.Table)
return extractValues [typeof(LuaTable).TypeHandle.Value.ToInt64 ()]; return extractValues [getExtractDictionaryKey (typeof(LuaTable))];
else if (luatype == LuaTypes.UserData) else if (luatype == LuaTypes.UserData)
return extractValues [typeof(object).TypeHandle.Value.ToInt64 ()]; return extractValues [getExtractDictionaryKey (typeof(object))];
else if (luatype == LuaTypes.Function) else if (luatype == LuaTypes.Function)
return extractValues [typeof(LuaFunction).TypeHandle.Value.ToInt64 ()]; return extractValues [getExtractDictionaryKey (typeof(LuaFunction))];
else if (luatype == LuaTypes.Number) else if (luatype == LuaTypes.Number)
return extractValues [typeof(double).TypeHandle.Value.ToInt64 ()]; return extractValues [getExtractDictionaryKey (typeof(double))];
} }
if (LuaLib.lua_isnumber (luaState, stackPos)) if (LuaLib.lua_isnumber (luaState, stackPos))
return extractValues [runtimeHandleValue]; return extractValues [extractKey];
if (paramType == typeof(bool)) { if (paramType == typeof(bool)) {
if (LuaLib.lua_isboolean (luaState, stackPos)) if (LuaLib.lua_isboolean (luaState, stackPos))
return extractValues [runtimeHandleValue]; return extractValues [extractKey];
} else if (paramType == typeof(string)) { } else if (paramType == typeof(string)) {
if (LuaLib.lua_isstring (luaState, stackPos)) if (LuaLib.lua_isstring (luaState, stackPos))
return extractValues [runtimeHandleValue]; return extractValues [extractKey];
else if (luatype == LuaTypes.Nil) else if (luatype == LuaTypes.Nil)
return extractNetObject; // kevinh - silently convert nil to a null string pointer return extractNetObject; // kevinh - silently convert nil to a null string pointer
} else if (paramType == typeof(LuaTable)) { } else if (paramType == typeof(LuaTable)) {
if (luatype == LuaTypes.Table) if (luatype == LuaTypes.Table)
return extractValues [runtimeHandleValue]; return extractValues [extractKey];
} else if (paramType == typeof(LuaUserData)) { } else if (paramType == typeof(LuaUserData)) {
if (luatype == LuaTypes.UserData) if (luatype == LuaTypes.UserData)
return extractValues [runtimeHandleValue]; return extractValues [extractKey];
} else if (paramType == typeof(LuaFunction)) { } else if (paramType == typeof(LuaFunction)) {
if (luatype == LuaTypes.Function) if (luatype == LuaTypes.Function)
return extractValues [runtimeHandleValue]; return extractValues [extractKey];
} else if (typeof(Delegate).IsAssignableFrom (paramType) && luatype == LuaTypes.Function) } else if (typeof(Delegate).IsAssignableFrom (paramType) && luatype == LuaTypes.Function)
return new ExtractValue (new DelegateGenerator (translator, paramType).extractGenerated); return new ExtractValue (new DelegateGenerator (translator, paramType).extractGenerated);
else if (paramType.IsInterface && luatype == LuaTypes.Table) else if (paramType.IsInterface && luatype == LuaTypes.Table)
...@@ -168,6 +172,18 @@ namespace NLua ...@@ -168,6 +172,18 @@ namespace NLua
return null; return null;
} }
#if SILVERLIGHT
private Type getExtractDictionaryKey(Type targetType)
{
return targetType;
}
#else
private long getExtractDictionaryKey(Type targetType)
{
return targetType.TypeHandle.Value.ToInt64();
}
#endif
/* /*
* The following functions return the value in the Lua stack * The following functions return the value in the Lua stack
* index stackPos as the desired type if it can, or null * index stackPos as the desired type if it can, or null
......
...@@ -74,7 +74,7 @@ namespace NLua ...@@ -74,7 +74,7 @@ namespace NLua
assemblyName = new AssemblyName (); assemblyName = new AssemblyName ();
assemblyName.Name = "NLua_generatedcode"; assemblyName.Name = "NLua_generatedcode";
// Create a new assembly with one module. // Create a new assembly with one module.
#if !MONOTOUCH #if !MONOTOUCH && !SILVERLIGHT
newAssembly = Thread.GetDomain ().DefineDynamicAssembly (assemblyName, AssemblyBuilderAccess.Run); newAssembly = Thread.GetDomain ().DefineDynamicAssembly (assemblyName, AssemblyBuilderAccess.Run);
newModule = newAssembly.DefineDynamicModule ("NLua_generatedcode"); newModule = newAssembly.DefineDynamicModule ("NLua_generatedcode");
#endif #endif
...@@ -95,6 +95,8 @@ namespace NLua ...@@ -95,6 +95,8 @@ namespace NLua
{ {
#if MONOTOUCH #if MONOTOUCH
throw new NotImplementedException (" Emit not available on MonoTouch "); throw new NotImplementedException (" Emit not available on MonoTouch ");
#elif SILVERLIGHT
throw new NotImplementedException(" Emit not available on Silverlight ");
#else #else
string typeName; string typeName;
lock (this) { lock (this) {
...@@ -135,6 +137,8 @@ namespace NLua ...@@ -135,6 +137,8 @@ namespace NLua
{ {
#if MONOTOUCH #if MONOTOUCH
throw new NotImplementedException ("GenerateDelegate is not available on iOS, please register your LuaDelegate type with Lua.RegisterLuaDelegateType( yourDelegate, theLuaDelegateHandler) "); throw new NotImplementedException ("GenerateDelegate is not available on iOS, please register your LuaDelegate type with Lua.RegisterLuaDelegateType( yourDelegate, theLuaDelegateHandler) ");
#elif SILVERLIGHT
throw new NotImplementedException("GenerateDelegate is not available on Silverlight, please register your LuaDelegate type with Lua.RegisterLuaDelegateType( yourDelegate, theLuaDelegateHandler) ");
#else #else
string typeName; string typeName;
lock (this) { lock (this) {
...@@ -403,8 +407,13 @@ namespace NLua ...@@ -403,8 +407,13 @@ namespace NLua
for (int i = 0; i < paramTypes.Length; i++) { for (int i = 0; i < paramTypes.Length; i++) {
paramTypes [i] = paramInfo [i].ParameterType; paramTypes [i] = paramInfo [i].ParameterType;
if ((!paramInfo [i].IsIn) && paramInfo [i].IsOut) #if SILVERLIGHT
if (paramInfo[i].IsOut) {
#else
if ((!paramInfo [i].IsIn) && paramInfo [i].IsOut) {
#endif
nOutParams++; nOutParams++;
}
if (paramTypes [i].IsByRef) { if (paramTypes [i].IsByRef) {
returnTypesList.Add (paramTypes [i].GetElementType ()); returnTypesList.Add (paramTypes [i].GetElementType ());
...@@ -640,6 +649,8 @@ namespace NLua ...@@ -640,6 +649,8 @@ namespace NLua
{ {
#if MONOTOUCH #if MONOTOUCH
throw new NotImplementedException (" Emit not available on MonoTouch "); throw new NotImplementedException (" Emit not available on MonoTouch ");
#elif SILVERLIGHT
throw new NotImplementedException (" Emit not available on Silverlight ");
#else #else
Type eventConsumerType; Type eventConsumerType;
......
...@@ -27,6 +27,9 @@ using System; ...@@ -27,6 +27,9 @@ using System;
using System.Reflection; using System.Reflection;
using System.Diagnostics.CodeAnalysis; using System.Diagnostics.CodeAnalysis;
using NLua.Extensions; using NLua.Extensions;
#if SILVERLIGHT
using System.Linq;
#endif
namespace NLua namespace NLua
{ {
...@@ -108,8 +111,13 @@ namespace NLua ...@@ -108,8 +111,13 @@ namespace NLua
if (!type.IsEnum) if (!type.IsEnum)
throw new ArgumentException ("The type must be an enumeration!"); throw new ArgumentException ("The type must be an enumeration!");
#if SILVERLIGHT
string[] names = type.GetFields().Where(x => x.IsLiteral).Select(field => field.Name).ToArray();
var values = type.GetFields().Where(x => x.IsLiteral).Select(field => (T)field.GetValue(null)).ToArray();
#else
string[] names = Enum.GetNames (type); string[] names = Enum.GetNames (type);
var values = (T[])Enum.GetValues (type); var values = (T[])Enum.GetValues (type);
#endif
lua.NewTable (type.Name); lua.NewTable (type.Name);
for (int i = 0; i < names.Length; i++) { for (int i = 0; i < names.Length; i++) {
......
...@@ -812,14 +812,20 @@ namespace NLua ...@@ -812,14 +812,20 @@ namespace NLua
var paramInfo = method.GetParameters (); var paramInfo = method.GetParameters ();
int currentLuaParam = 1; int currentLuaParam = 1;
int nLuaParams = LuaLib.lua_gettop (luaState); int nLuaParams = LuaLib.lua_gettop (luaState);
var paramList = new ArrayList (); var paramList = new List<object> ();
var outList = new List<int> (); var outList = new List<int> ();
var argTypes = new List<MethodArgs> (); var argTypes = new List<MethodArgs> ();
foreach (var currentNetParam in paramInfo) { foreach (var currentNetParam in paramInfo) {
#if !SILVERLIGHT
if (!currentNetParam.IsIn && currentNetParam.IsOut) // Skips out params if (!currentNetParam.IsIn && currentNetParam.IsOut) // Skips out params
outList.Add (paramList.Add (null)); #else
else if (currentLuaParam > nLuaParams) { // Adds optional parameters if (currentNetParam.IsOut) // Skips out params
#endif
{
paramList.Add (null);
outList.Add (paramList.LastIndexOf (null));
} else if (currentLuaParam > nLuaParams) { // Adds optional parameters
if (currentNetParam.IsOptional) if (currentNetParam.IsOptional)
paramList.Add (currentNetParam.DefaultValue); paramList.Add (currentNetParam.DefaultValue);
else { else {
...@@ -827,7 +833,9 @@ namespace NLua ...@@ -827,7 +833,9 @@ namespace NLua
break; break;
} }
} else if (_IsTypeCorrect (luaState, currentLuaParam, currentNetParam, out extractValue)) { // Type checking } else if (_IsTypeCorrect (luaState, currentLuaParam, currentNetParam, out extractValue)) { // Type checking
int index = paramList.Add (extractValue (luaState, currentLuaParam)); var value = extractValue (luaState, currentLuaParam);
paramList.Add (value);
int index = paramList.LastIndexOf (value);
var methodArg = new MethodArgs (); var methodArg = new MethodArgs ();
methodArg.index = index; methodArg.index = index;
methodArg.extractValue = extractValue; methodArg.extractValue = extractValue;
...@@ -851,7 +859,11 @@ namespace NLua ...@@ -851,7 +859,11 @@ namespace NLua
int paramArrayIndex = 0; int paramArrayIndex = 0;
while (tableEnumerator.MoveNext()) { while (tableEnumerator.MoveNext()) {
#if SILVERLIGHT
paramArray.SetValue (Convert.ChangeType (tableEnumerator.Value, currentNetParam.ParameterType.GetElementType (), System.Globalization.CultureInfo.InvariantCulture), paramArrayIndex);
#else
paramArray.SetValue (Convert.ChangeType (tableEnumerator.Value, currentNetParam.ParameterType.GetElementType ()), paramArrayIndex); paramArray.SetValue (Convert.ChangeType (tableEnumerator.Value, currentNetParam.ParameterType.GetElementType ()), paramArrayIndex);
#endif
paramArrayIndex++; paramArrayIndex++;
} }
} else { } else {
...@@ -859,7 +871,8 @@ namespace NLua ...@@ -859,7 +871,8 @@ namespace NLua
paramArray.SetValue (luaParamValue, 0); paramArray.SetValue (luaParamValue, 0);
} }
int index = paramList.Add (paramArray); paramList.Add (paramArray);
int index = paramList.LastIndexOf (paramArray);
var methodArg = new MethodArgs (); var methodArg = new MethodArgs ();
methodArg.index = index; methodArg.index = index;
methodArg.extractValue = extractValue; methodArg.extractValue = extractValue;
......
...@@ -152,7 +152,11 @@ namespace NLua.Method ...@@ -152,7 +152,11 @@ namespace NLua.Method
paramArray = Array.CreateInstance (paramArrayType, table.Values.Count); paramArray = Array.CreateInstance (paramArrayType, table.Values.Count);
for (int x = 1; x <= table.Values.Count; x++) for (int x = 1; x <= table.Values.Count; x++)
#if SILVERLIGHT
paramArray.SetValue(Convert.ChangeType(table[x], paramArrayType, System.Globalization.CultureInfo.InvariantCulture), x - 1);
#else
paramArray.SetValue (Convert.ChangeType (table [x], paramArrayType), x - 1); paramArray.SetValue (Convert.ChangeType (table [x], paramArrayType), x - 1);
#endif
} else { } else {
paramArray = Array.CreateInstance (paramArrayType, 1); paramArray = Array.CreateInstance (paramArrayType, 1);
paramArray.SetValue (luaParamValue, 0); paramArray.SetValue (luaParamValue, 0);
......
...@@ -44,7 +44,11 @@ namespace NLua.Method ...@@ -44,7 +44,11 @@ namespace NLua.Method
var mi = value as MethodInfo; var mi = value as MethodInfo;
if (!mi.IsNull ()) if (!mi.IsNull ())
#if SILVERLIGHT
IsReturnVoid = string.Compare(mi.ReturnType.Name, "System.Void", StringComparison.InvariantCulture) == 0;
#else
IsReturnVoid = string.Compare (mi.ReturnType.Name, "System.Void", true) == 0; IsReturnVoid = string.Compare (mi.ReturnType.Name, "System.Void", true) == 0;
#endif
} }
} }
......
...@@ -270,8 +270,10 @@ namespace NLua ...@@ -270,8 +270,10 @@ namespace NLua
// The assemblyName was invalid. It is most likely a path. // The assemblyName was invalid. It is most likely a path.
} }
#if !SILVERLIGHT
if (assembly.IsNull ()) if (assembly.IsNull ())
assembly = Assembly.Load (AssemblyName.GetAssemblyName (assemblyName)); assembly = Assembly.Load (AssemblyName.GetAssemblyName (assemblyName));
#endif
if (!assembly.IsNull () && !assemblies.Contains (assembly)) if (!assembly.IsNull () && !assemblies.Contains (assembly))
assemblies.Add (assembly); assemblies.Add (assembly);
...@@ -777,7 +779,7 @@ namespace NLua ...@@ -777,7 +779,7 @@ namespace NLua
if (oldTop == newTop) if (oldTop == newTop)
return null; return null;
else { else {
var returnValues = new ArrayList (); var returnValues = new List<object> ();
for (int i = oldTop+1; i <= newTop; i++) for (int i = oldTop+1; i <= newTop; i++)
returnValues.Add (getObject (luaState, i)); returnValues.Add (getObject (luaState, i));
...@@ -799,7 +801,7 @@ namespace NLua ...@@ -799,7 +801,7 @@ namespace NLua
return null; return null;
else { else {
int iTypes; int iTypes;
var returnValues = new ArrayList (); var returnValues = new List<object> ();
if (popTypes [0] == typeof(void)) if (popTypes [0] == typeof(void))
iTypes = 1; iTypes = 1;
...@@ -823,7 +825,11 @@ namespace NLua ...@@ -823,7 +825,11 @@ namespace NLua
if (o is ILuaGeneratedType) { if (o is ILuaGeneratedType) {
// Make sure we are _really_ ILuaGenerated // Make sure we are _really_ ILuaGenerated
var typ = o.GetType (); var typ = o.GetType ();
#if SILVERLIGHT
return (!typ.GetInterface ("ILuaGeneratedType", true).IsNull ());
#else
return (!typ.GetInterface ("ILuaGeneratedType").IsNull ()); return (!typ.GetInterface ("ILuaGeneratedType").IsNull ());
#endif
} else } else
return false; return false;
} }
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment