Skip to content

Commit

Permalink
Fixed custom descriptors
Browse files Browse the repository at this point in the history
  • Loading branch information
xanathar committed Jan 13, 2015
1 parent 757e5b6 commit b40dc7b
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 4 deletions.
69 changes: 68 additions & 1 deletion src/MoonSharp.Interpreter.Tests/EndToEnd/UserDataMethodsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ public interface Interface2
string Test2();
}


public class SomeOtherClass
{
public string Test1()
Expand All @@ -97,6 +98,47 @@ public string Test2()
}
}


public class SomeOtherClassCustomDescriptor
{
}

public class CustomDescriptor : IUserDataDescriptor
{
public string Name
{
get { return "ciao"; }
}

public Type Type
{
get { return typeof(SomeOtherClassCustomDescriptor); }
}

public DynValue Index(Script script, object obj, DynValue index)
{
return DynValue.NewNumber(index.Number * 4);
}

public bool SetIndex(Script script, object obj, DynValue index, DynValue value)
{
throw new NotImplementedException();
}

public string AsString(object obj)
{
return null;
}

public DynValue MetaIndex(Script script, object obj, string metaname)
{
throw new NotImplementedException();
}
}




public class SelfDescribingClass : IUserDataType
{
public DynValue Index(Script script, DynValue index)
Expand Down Expand Up @@ -481,9 +523,34 @@ public void Interop_TestSelfDescribingType()

Assert.AreEqual(DataType.Number, res.Type);
Assert.AreEqual(18, res.Number);

}

[Test]
public void Interop_TestCustomDescribedType()
{
UserData.UnregisterType<SomeOtherClassCustomDescriptor>();

string script = @"
a = myobj[1];
b = myobj[2];
c = myobj[3];
return a + b + c;
";

Script S = new Script();

SomeOtherClassCustomDescriptor obj = new SomeOtherClassCustomDescriptor();

UserData.RegisterType<SomeOtherClassCustomDescriptor>(new CustomDescriptor());

S.Globals.Set("myobj", UserData.Create(obj));

DynValue res = S.DoString(script);

Assert.AreEqual(DataType.Number, res.Type);
Assert.AreEqual(24, res.Number);
}

}
}
16 changes: 13 additions & 3 deletions src/MoonSharp.Interpreter/DataTypes/UserData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,16 @@ public static void RegisterType(Type type, InteropAccessMode accessMode = Intero
RegisterType_Impl(type, accessMode, friendlyName, null);
}

public static void RegisterType<T>(IUserDataDescriptor customDescriptor)
{
RegisterType_Impl(typeof(T), InteropAccessMode.Default, null, customDescriptor);
}

public static void RegisterType(Type type, IUserDataDescriptor customDescriptor)
{
RegisterType_Impl(type, InteropAccessMode.Default, null, customDescriptor);
}

public static void RegisterAssembly(Assembly asm = null)
{
asm = asm ?? Assembly.GetCallingAssembly();
Expand Down Expand Up @@ -140,13 +150,13 @@ private static IUserDataDescriptor RegisterType_Impl(Type type, InteropAccessMod
if (type.GetInterfaces().Any(ii => ii == typeof(IUserDataType)))
{
AutoDescribingUserDataDescriptor audd = new AutoDescribingUserDataDescriptor(type, friendlyName);
s_Registry.Add(audd.Type, audd);
s_Registry.Add(type, audd);
return audd;
}
else
{
StandardUserDataDescriptor udd = new StandardUserDataDescriptor(type, accessMode, friendlyName);
s_Registry.Add(udd.Type, udd);
s_Registry.Add(type, udd);

if (accessMode == InteropAccessMode.BackgroundOptimized)
{
Expand All @@ -158,7 +168,7 @@ private static IUserDataDescriptor RegisterType_Impl(Type type, InteropAccessMod
}
else
{
s_Registry.Add(descriptor.Type, descriptor);
s_Registry.Add(type, descriptor);
return descriptor;
}
}
Expand Down

0 comments on commit b40dc7b

Please sign in to comment.