Skip to content

Commit

Permalink
Adding configurable ClassSymbolNative.native_object_getter class field
Browse files Browse the repository at this point in the history
  • Loading branch information
pachanga committed Jul 23, 2024
1 parent 884d471 commit f68323b
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 19 deletions.
35 changes: 26 additions & 9 deletions src/vm/symbol.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1635,34 +1635,49 @@ public class ClassSymbolNative : ClassSymbol, INativeType
IList<ProxyType> tmp_implements;

System.Type native_type;
Func<Val, object> native_object_getter;

public ClassSymbolNative(
Origin origin,
string name,
VM.ClassCreator creator = null,
System.Type native_type = null
System.Type native_type = null,
Func<Val, object> native_object_getter = null
)
: this(origin, name, new ProxyType(), null, creator, native_type)
: this(
origin, name,
new ProxyType(), null,
creator,
native_type, native_object_getter)
{}

public ClassSymbolNative(
Origin origin,
string name,
IList<ProxyType> proxy_implements,
VM.ClassCreator creator = null,
System.Type native_type = null
System.Type native_type = null,
Func<Val, object> native_object_getter = null
)
: this(origin, name, new ProxyType(), proxy_implements, creator, native_type)
: this(
origin, name,
new ProxyType(), proxy_implements,
creator,
native_type, native_object_getter)
{}

public ClassSymbolNative(
Origin origin,
string name,
ProxyType proxy_super_class,
VM.ClassCreator creator = null,
System.Type native_type = null
System.Type native_type = null,
Func<Val, object> native_object_getter = null
)
: this(origin, name, proxy_super_class, null, creator, native_type)
: this(origin, name,
proxy_super_class, null,
creator,
native_type, native_object_getter)
{}

public ClassSymbolNative(
Expand All @@ -1671,23 +1686,25 @@ public ClassSymbolNative(
ProxyType proxy_super_class,
IList<ProxyType> proxy_implements,
VM.ClassCreator creator = null,
System.Type native_type = null
System.Type native_type = null,
Func<Val, object> native_object_getter = null
)
: base(origin, name, creator)
{
this.tmp_super_class = proxy_super_class;
this.tmp_implements = proxy_implements;
this.native_type = native_type;
this.native_object_getter = native_object_getter;
}

public System.Type GetNativeType()
{
return native_type;
}

public virtual object GetNativeObject(Val v)
public object GetNativeObject(Val v)
{
return v?._obj;
return native_object_getter?.Invoke(v) ?? v?._obj;
}

public override void Setup()
Expand Down
18 changes: 8 additions & 10 deletions tests/test_type_casts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -640,8 +640,12 @@ func bool test()
AssertEqual(1, Execute(vm, "test").result.PopRelease().num);
CommonChecks(vm);
}

public interface INativeFoo
{}

public class NativeFoo {
public class NativeFoo : INativeFoo
{
public int foo = 10;
}
public class NativeBar : NativeFoo
Expand Down Expand Up @@ -738,12 +742,6 @@ func int test3()
});
}

public interface IFoo
{}

public class Foo : IFoo
{}

[IsTested()]
public void TestIsForClassImplementingNativeInterface()
{
Expand All @@ -759,7 +757,7 @@ func bool test() {
new Origin(),
"IFoo",
null,
typeof(IFoo)
typeof(INativeFoo)
);
ts.ns.Define(ifs);
ifs.Setup();
Expand All @@ -770,9 +768,9 @@ func bool test() {
new List<ProxyType>(){ ts.T("IFoo") },
delegate(VM.Frame frm, ref Val v, IType type)
{
v.SetObj(new Foo(), type);
v.SetObj(new NativeFoo(), type);
},
typeof(Foo)
typeof(NativeFoo)
);
ts.ns.Define(cl);
cl.Setup();
Expand Down

0 comments on commit f68323b

Please sign in to comment.