Skip to content

Commit

Permalink
Fixes issues #96, #99 and #100 (IronPython compatibility, error in ma…
Browse files Browse the repository at this point in the history
…nagement of out params in overloads and time format in os module).
  • Loading branch information
xanathar committed Jun 30, 2015
1 parent 2019e8a commit 62a70e1
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 56 deletions.
83 changes: 32 additions & 51 deletions src/DevTools/Playground/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,69 +8,50 @@

namespace Playground
{
class MyDictionaryDescriptor : StandardUserDataDescriptor
public class Foo
{
public MyDictionaryDescriptor(Type type)
: base(type, InteropAccessMode.Default)
{ }
public static void Log1(string msg) { }

public override DynValue Index(Script script, object obj, DynValue index, bool isDirectIndexing)
public static void Test1(string msg, out string obj, string val)
{
if (isDirectIndexing)
{
string key = index.String;

if (key.StartsWith("_"))
index = DynValue.NewString(key.Substring(1));
else
isDirectIndexing = false;
}

return base.Index(script, obj, index, isDirectIndexing);
}

public override bool SetIndex(Script script, object obj, DynValue index, DynValue value, bool isDirectIndexing)
{
if (isDirectIndexing)
{
string key = index.String;

if (key.StartsWith("_"))
index = DynValue.NewString(key.Substring(1));
else
isDirectIndexing = false;
}

return base.SetIndex(script, obj, index, value, isDirectIndexing);
Console.WriteLine("{0} - {1}", msg ?? "(NULL)", val ?? "(NULL)");
obj = msg;
}

}

public static class FooExtension
{
public static void Log2(this Foo self, string msg) { }
}

class Program
{
static void Main(string[] args)
{
Dictionary<string, int> dic = new Dictionary<string, int>();

dic["hp"] = 33;

UserData.RegisterType<Dictionary<string, int>>(new MyDictionaryDescriptor(typeof(Dictionary<string, int>)));

Script s = new Script();

s.Globals["dic"] = dic;
try
{
s.DoString("print(dic['hp'])");
s.DoString("print(dic.hp)");
s.DoString("print(dic._count)");
}
catch (ScriptRuntimeException ex)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(ex.DecoratedMessage);
}
UserData.RegisterType<Foo>();
UserData.RegisterType<Dictionary<int, int>>();
UserData.RegisterExtensionType(typeof(FooExtension));

var lua = new Script();
lua.Globals["DictionaryIntInt"] = typeof(Dictionary<int, int>);

var script = @"local dict = DictionaryIntInt.__new(); local res, v = dict.TryGetValue(0)";
lua.DoString(script);
lua.DoString(script);


//var lua = new Script();
//lua.Globals["Foo"] = typeof(Foo);

//var script = @"local _, obj = Foo.Test1('ciao', 'hello'); print(obj);";
//lua.DoString(script);





Console.WriteLine("Done");
Console.ReadKey();


Expand Down
20 changes: 20 additions & 0 deletions src/MoonSharp.Interpreter/Interop/DescriptorHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,26 @@ public static List<string> GetMetaNamesFromAttributes(this MethodInfo mi)
.ToList();
}

/// <summary>
/// Gets the Types implemented in the assembly, catching the ReflectionTypeLoadException just in case..
/// </summary>
/// <param name="asm">The assebly</param>
/// <returns></returns>
public static Type[] SafeGetTypes(this Assembly asm)
{
try
{
return asm.GetTypes();
}
catch (ReflectionTypeLoadException)
{
return new Type[0];
}
}




/// <summary>
/// Gets the name of a conversion method to be exposed to Lua scripts
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// #define DEBUG_OVERLOAD_RESOLVER
#define DEBUG_OVERLOAD_RESOLVER

using System;
using System.Collections.Generic;
Expand Down Expand Up @@ -314,6 +314,9 @@ private int CalcScoreForOverload(ScriptExecutionContext context, CallbackArgumen
if (isExtMethod && i == 0)
continue;

if (method.Parameters[i].IsOut)
continue;

Type parameterType = method.Parameters[i].Type;

if ((parameterType == typeof(Script)) || (parameterType == typeof(ScriptExecutionContext)) || (parameterType == typeof(CallbackArguments)))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ internal static void RegisterAssembly(Assembly asm = null, bool includeExtension

if (includeExtensionTypes)
{
var extensionTypes = from t in asm.GetTypes()
var extensionTypes = from t in asm.SafeGetTypes()
let attributes = t.GetCustomAttributes(typeof(ExtensionAttribute), true)
where attributes != null && attributes.Length > 0
select new { Attributes = attributes, DataType = t };
Expand All @@ -42,7 +42,7 @@ internal static void RegisterAssembly(Assembly asm = null, bool includeExtension
}


var userDataTypes = from t in asm.GetTypes()
var userDataTypes = from t in asm.SafeGetTypes()
let attributes = t.GetCustomAttributes(typeof(MoonSharpUserDataAttribute), true)
where attributes != null && attributes.Length > 0
select new { Attributes = attributes, DataType = t };
Expand Down
3 changes: 2 additions & 1 deletion src/MoonSharp.Interpreter/Platforms/PlatformAutoDetector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Linq;
using System.Text;
using MoonSharp.Interpreter.Loaders;
using MoonSharp.Interpreter.Interop;

namespace MoonSharp.Interpreter.Platforms
{
Expand Down Expand Up @@ -70,7 +71,7 @@ private static void AutoDetectPlatformFlags()
#else
IsRunningOnUnity = AppDomain.CurrentDomain
.GetAssemblies()
.SelectMany(a => a.GetTypes())
.SelectMany(a => a.SafeGetTypes())
.Any(t => t.FullName.StartsWith("UnityEngine."));
#endif

Expand Down
2 changes: 1 addition & 1 deletion src/MoonSharp.Interpreter/Script.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public class Script : IScriptPrivateResource
/// <summary>
/// The version of the MoonSharp engine
/// </summary>
public const string VERSION = "0.9.6.2";
public const string VERSION = "0.9.7.1";

/// <summary>
/// The Lua version being supported
Expand Down

0 comments on commit 62a70e1

Please sign in to comment.