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
*/
class CheckType
{
#if SILVERLIGHT
private Dictionary<Type, ExtractValue> extractValues = new Dictionary<Type, ExtractValue>();
#else
private Dictionary<long, ExtractValue> extractValues = new Dictionary<long, ExtractValue> ();
#endif
private ExtractValue extractNetObject;
private ObjectTranslator translator;
public CheckType (ObjectTranslator translator)
{
this.translator = translator;
extractValues.Add (typeof(object).TypeHandle.Value.ToInt64 (), new ExtractValue (getAsObject));
extractValues.Add (typeof(sbyte).TypeHandle.Value.ToInt64 (), new ExtractValue (getAsSbyte));
extractValues.Add (typeof(byte).TypeHandle.Value.ToInt64 (), new ExtractValue (getAsByte));
extractValues.Add (typeof(short).TypeHandle.Value.ToInt64 (), new ExtractValue (getAsShort));
extractValues.Add (typeof(ushort).TypeHandle.Value.ToInt64 (), new ExtractValue (getAsUshort));
extractValues.Add (typeof(int).TypeHandle.Value.ToInt64 (), new ExtractValue (getAsInt));
extractValues.Add (typeof(uint).TypeHandle.Value.ToInt64 (), new ExtractValue (getAsUint));
extractValues.Add (typeof(long).TypeHandle.Value.ToInt64 (), new ExtractValue (getAsLong));
extractValues.Add (typeof(ulong).TypeHandle.Value.ToInt64 (), new ExtractValue (getAsUlong));
extractValues.Add (typeof(double).TypeHandle.Value.ToInt64 (), new ExtractValue (getAsDouble));
extractValues.Add (typeof(char).TypeHandle.Value.ToInt64 (), new ExtractValue (getAsChar));
extractValues.Add (typeof(float).TypeHandle.Value.ToInt64 (), new ExtractValue (getAsFloat));
extractValues.Add (typeof(decimal).TypeHandle.Value.ToInt64 (), new ExtractValue (getAsDecimal));
extractValues.Add (typeof(bool).TypeHandle.Value.ToInt64 (), new ExtractValue (getAsBoolean));
extractValues.Add (typeof(string).TypeHandle.Value.ToInt64 (), new ExtractValue (getAsString));
extractValues.Add (typeof(LuaFunction).TypeHandle.Value.ToInt64 (), new ExtractValue (getAsFunction));
extractValues.Add (typeof(LuaTable).TypeHandle.Value.ToInt64 (), new ExtractValue (getAsTable));
extractValues.Add (typeof(LuaUserData).TypeHandle.Value.ToInt64 (), new ExtractValue (getAsUserdata));
this.translator = translator;
extractValues.Add(getExtractDictionaryKey(typeof(object)), new ExtractValue(getAsObject));
extractValues.Add(getExtractDictionaryKey(typeof(sbyte)), new ExtractValue(getAsSbyte));
extractValues.Add(getExtractDictionaryKey(typeof(byte)), new ExtractValue(getAsByte));
extractValues.Add(getExtractDictionaryKey(typeof(short)), new ExtractValue(getAsShort));
extractValues.Add(getExtractDictionaryKey(typeof(ushort)), new ExtractValue(getAsUshort));
extractValues.Add(getExtractDictionaryKey(typeof(int)), new ExtractValue(getAsInt));
extractValues.Add(getExtractDictionaryKey(typeof(uint)), new ExtractValue(getAsUint));
extractValues.Add(getExtractDictionaryKey(typeof(long)), new ExtractValue(getAsLong));
extractValues.Add(getExtractDictionaryKey(typeof(ulong)), new ExtractValue(getAsUlong));
extractValues.Add(getExtractDictionaryKey(typeof(double)), new ExtractValue(getAsDouble));
extractValues.Add(getExtractDictionaryKey(typeof(char)), new ExtractValue(getAsChar));
extractValues.Add(getExtractDictionaryKey(typeof(float)), new ExtractValue(getAsFloat));
extractValues.Add(getExtractDictionaryKey(typeof(decimal)), new ExtractValue(getAsDecimal));
extractValues.Add(getExtractDictionaryKey(typeof(bool)), new ExtractValue(getAsBoolean));
extractValues.Add(getExtractDictionaryKey(typeof(string)), new ExtractValue(getAsString));
extractValues.Add(getExtractDictionaryKey(typeof(LuaFunction)), new ExtractValue(getAsFunction));
extractValues.Add(getExtractDictionaryKey(typeof(LuaTable)), new ExtractValue(getAsTable));
extractValues.Add(getExtractDictionaryKey(typeof(LuaUserData)), new ExtractValue(getAsUserdata));
extractNetObject = new ExtractValue (getAsNetObject);
}
......@@ -86,9 +90,9 @@ namespace NLua
{
if (paramType.IsByRef)
paramType = paramType.GetElementType ();
long runtimeHandleValue = paramType.TypeHandle.Value.ToInt64 ();
return extractValues.ContainsKey (runtimeHandleValue) ? extractValues [runtimeHandleValue] : extractNetObject;
var extractKey = getExtractDictionaryKey(paramType);
return extractValues.ContainsKey(extractKey) ? extractValues[extractKey] : extractNetObject;
}
internal ExtractValue checkType (LuaCore.lua_State luaState, int stackPos, Type paramType)
......@@ -102,48 +106,48 @@ namespace NLua
if (!underlyingType.IsNull ())
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)))
return extractValues [runtimeHandleValue];
return extractValues [extractKey];
//CP: Added support for generic parameters
if (paramType.IsGenericParameter) {
if (luatype == LuaTypes.Boolean)
return extractValues [typeof(bool).TypeHandle.Value.ToInt64 ()];
else if (luatype == LuaTypes.String)
return extractValues [typeof(string).TypeHandle.Value.ToInt64 ()];
return extractValues [getExtractDictionaryKey (typeof(bool))];
else if (luatype == LuaTypes.String)
return extractValues[getExtractDictionaryKey (typeof(string))];
else if (luatype == LuaTypes.Table)
return extractValues [typeof(LuaTable).TypeHandle.Value.ToInt64 ()];
return extractValues [getExtractDictionaryKey (typeof(LuaTable))];
else if (luatype == LuaTypes.UserData)
return extractValues [typeof(object).TypeHandle.Value.ToInt64 ()];
return extractValues [getExtractDictionaryKey (typeof(object))];
else if (luatype == LuaTypes.Function)
return extractValues [typeof(LuaFunction).TypeHandle.Value.ToInt64 ()];
return extractValues [getExtractDictionaryKey (typeof(LuaFunction))];
else if (luatype == LuaTypes.Number)
return extractValues [typeof(double).TypeHandle.Value.ToInt64 ()];
return extractValues [getExtractDictionaryKey (typeof(double))];
}
if (LuaLib.lua_isnumber (luaState, stackPos))
return extractValues [runtimeHandleValue];
return extractValues [extractKey];
if (paramType == typeof(bool)) {
if (LuaLib.lua_isboolean (luaState, stackPos))
return extractValues [runtimeHandleValue];
return extractValues [extractKey];
} else if (paramType == typeof(string)) {
if (LuaLib.lua_isstring (luaState, stackPos))
return extractValues [runtimeHandleValue];
return extractValues [extractKey];
else if (luatype == LuaTypes.Nil)
return extractNetObject; // kevinh - silently convert nil to a null string pointer
} else if (paramType == typeof(LuaTable)) {
if (luatype == LuaTypes.Table)
return extractValues [runtimeHandleValue];
return extractValues [extractKey];
} else if (paramType == typeof(LuaUserData)) {
if (luatype == LuaTypes.UserData)
return extractValues [runtimeHandleValue];
return extractValues [extractKey];
} else if (paramType == typeof(LuaFunction)) {
if (luatype == LuaTypes.Function)
return extractValues [runtimeHandleValue];
return extractValues [extractKey];
} else if (typeof(Delegate).IsAssignableFrom (paramType) && luatype == LuaTypes.Function)
return new ExtractValue (new DelegateGenerator (translator, paramType).extractGenerated);
else if (paramType.IsInterface && luatype == LuaTypes.Table)
......@@ -166,7 +170,19 @@ namespace NLua
}
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
......
......@@ -74,7 +74,7 @@ namespace NLua
assemblyName = new AssemblyName ();
assemblyName.Name = "NLua_generatedcode";
// Create a new assembly with one module.
#if !MONOTOUCH
#if !MONOTOUCH && !SILVERLIGHT
newAssembly = Thread.GetDomain ().DefineDynamicAssembly (assemblyName, AssemblyBuilderAccess.Run);
newModule = newAssembly.DefineDynamicModule ("NLua_generatedcode");
#endif
......@@ -95,6 +95,8 @@ namespace NLua
{
#if MONOTOUCH
throw new NotImplementedException (" Emit not available on MonoTouch ");
#elif SILVERLIGHT
throw new NotImplementedException(" Emit not available on Silverlight ");
#else
string typeName;
lock (this) {
......@@ -135,6 +137,8 @@ namespace NLua
{
#if MONOTOUCH
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
string typeName;
lock (this) {
......@@ -403,8 +407,13 @@ namespace NLua
for (int i = 0; i < paramTypes.Length; i++) {
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++;
}
if (paramTypes [i].IsByRef) {
returnTypesList.Add (paramTypes [i].GetElementType ());
......@@ -640,6 +649,8 @@ namespace NLua
{
#if MONOTOUCH
throw new NotImplementedException (" Emit not available on MonoTouch ");
#elif SILVERLIGHT
throw new NotImplementedException (" Emit not available on Silverlight ");
#else
Type eventConsumerType;
......
......@@ -27,6 +27,9 @@ using System;
using System.Reflection;
using System.Diagnostics.CodeAnalysis;
using NLua.Extensions;
#if SILVERLIGHT
using System.Linq;
#endif
namespace NLua
{
......@@ -108,8 +111,13 @@ namespace NLua
if (!type.IsEnum)
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);
var values = (T[])Enum.GetValues (type);
#endif
lua.NewTable (type.Name);
for (int i = 0; i < names.Length; i++) {
......
......@@ -812,14 +812,20 @@ namespace NLua
var paramInfo = method.GetParameters ();
int currentLuaParam = 1;
int nLuaParams = LuaLib.lua_gettop (luaState);
var paramList = new ArrayList ();
var paramList = new List<object> ();
var outList = new List<int> ();
var argTypes = new List<MethodArgs> ();
foreach (var currentNetParam in paramInfo) {
if (!currentNetParam.IsIn && currentNetParam.IsOut) // Skips out params
outList.Add (paramList.Add (null));
else if (currentLuaParam > nLuaParams) { // Adds optional parameters
#if !SILVERLIGHT
if (!currentNetParam.IsIn && currentNetParam.IsOut) // Skips out params
#else
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)
paramList.Add (currentNetParam.DefaultValue);
else {
......@@ -827,7 +833,9 @@ namespace NLua
break;
}
} 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 ();
methodArg.index = index;
methodArg.extractValue = extractValue;
......@@ -851,7 +859,11 @@ namespace NLua
int paramArrayIndex = 0;
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);
#endif
paramArrayIndex++;
}
} else {
......@@ -859,7 +871,8 @@ namespace NLua
paramArray.SetValue (luaParamValue, 0);
}
int index = paramList.Add (paramArray);
paramList.Add (paramArray);
int index = paramList.LastIndexOf (paramArray);
var methodArg = new MethodArgs ();
methodArg.index = index;
methodArg.extractValue = extractValue;
......
......@@ -152,7 +152,11 @@ namespace NLua.Method
paramArray = Array.CreateInstance (paramArrayType, table.Values.Count);
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);
#endif
} else {
paramArray = Array.CreateInstance (paramArrayType, 1);
paramArray.SetValue (luaParamValue, 0);
......
......@@ -44,7 +44,11 @@ namespace NLua.Method
var mi = value as MethodInfo;
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;
#endif
}
}
......
......@@ -270,8 +270,10 @@ namespace NLua
// The assemblyName was invalid. It is most likely a path.
}
#if !SILVERLIGHT
if (assembly.IsNull ())
assembly = Assembly.Load (AssemblyName.GetAssemblyName (assemblyName));
#endif
if (!assembly.IsNull () && !assemblies.Contains (assembly))
assemblies.Add (assembly);
......@@ -777,7 +779,7 @@ namespace NLua
if (oldTop == newTop)
return null;
else {
var returnValues = new ArrayList ();
var returnValues = new List<object> ();
for (int i = oldTop+1; i <= newTop; i++)
returnValues.Add (getObject (luaState, i));
......@@ -799,7 +801,7 @@ namespace NLua
return null;
else {
int iTypes;
var returnValues = new ArrayList ();
var returnValues = new List<object> ();
if (popTypes [0] == typeof(void))
iTypes = 1;
......@@ -823,7 +825,11 @@ namespace NLua
if (o is ILuaGeneratedType) {
// Make sure we are _really_ ILuaGenerated
var typ = o.GetType ();
#if SILVERLIGHT
return (!typ.GetInterface ("ILuaGeneratedType", true).IsNull ());
#else
return (!typ.GetInterface ("ILuaGeneratedType").IsNull ());
#endif
} else
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