From 884d471f5f854b597ce2a607f9f192e4ddaf09e5 Mon Sep 17 00:00:00 2001 From: Pavel Shevaev Date: Tue, 23 Jul 2024 15:54:07 +0300 Subject: [PATCH] Adding 'object GetNativeObject(Val v)' to INativeType and using it in Type.Is(..) checks --- src/vm/symbol.cs | 25 +++++++++++++++++++++---- src/vm/type.cs | 21 ++++++++++++++++----- src/vm/type_array.cs | 8 ++++---- src/vm/type_map.cs | 4 ++-- tests/test_type_casts.cs | 19 +++++++++++++++---- 5 files changed, 58 insertions(+), 19 deletions(-) diff --git a/src/vm/symbol.cs b/src/vm/symbol.cs index 244476da..5e013ebc 100644 --- a/src/vm/symbol.cs +++ b/src/vm/symbol.cs @@ -378,7 +378,12 @@ public System.Type GetNativeType() { return native_type; } - + + public object GetNativeObject(Val v) + { + return v?._obj; + } + public void Setup() { List inherits = null; @@ -984,13 +989,13 @@ public FieldSymbolScript() void Getter(VM.Frame frm, Val ctx, ref Val v, FieldSymbol fld) { - var m = (IList)ctx.obj; + var m = (IList)ctx._obj; v.ValueCopyFrom(m[scope_idx]); } void Setter(VM.Frame frm, ref Val ctx, Val v, FieldSymbol fld) { - var m = (IList)ctx.obj; + var m = (IList)ctx._obj; var curr = m[scope_idx]; for(int i=0;i)ctx.obj; + var m = (IList)ctx._obj; v = m[scope_idx]; } @@ -1620,6 +1625,7 @@ public override string ToString() public interface INativeType { System.Type GetNativeType(); + object GetNativeObject(Val v); } public class ClassSymbolNative : ClassSymbol, INativeType @@ -1679,6 +1685,11 @@ public System.Type GetNativeType() return native_type; } + public virtual object GetNativeObject(Val v) + { + return v?._obj; + } + public override void Setup() { ResolveTmpSuperClassAndInterfaces(this); @@ -1918,6 +1929,12 @@ public System.Type GetNativeType() { return native_type; } + + public object GetNativeObject(Val v) + { + //TODO: is it valid? + return v?._num; + } } public class EnumItemSymbol : Symbol, IType diff --git a/src/vm/type.cs b/src/vm/type.cs index f968fccf..8cccdb1a 100644 --- a/src/vm/type.cs +++ b/src/vm/type.cs @@ -643,7 +643,7 @@ static void SetupClassType() var fld = new FieldSymbol(new Origin(), "Name", String, delegate(VM.Frame frm, Val ctx, ref Val v, FieldSymbol _) { - var t = (IType)ctx.obj; + var t = (IType)ctx._obj; v.SetStr(t.GetName()); }, null @@ -730,12 +730,23 @@ static public bool Is(IType type, IType dest_type) return false; } - static public bool Is(Val v, IType dest_type) + static public bool Is(Val val, IType dest_type) { - if(v?.obj != null && dest_type is INativeType ndi) - return ndi.GetNativeType().IsAssignableFrom(v.obj.GetType()); + if(dest_type is INativeType dest_intype) + { + if(val?.type is INativeType val_intype) + { + var dest_ntype = dest_intype.GetNativeType(); + var nobj = val_intype.GetNativeObject(val); + return dest_ntype?.IsAssignableFrom( + nobj?.GetType() ?? val_intype.GetNativeType() + ) ?? false; + } + else + return false; + } else - return Is(v?.type, dest_type); + return Is(val?.type, dest_type); } static public bool CheckCast(IType dest_type, IType from_type) diff --git a/src/vm/type_array.cs b/src/vm/type_array.cs index 8b8d8d77..9a31f4cd 100644 --- a/src/vm/type_array.cs +++ b/src/vm/type_array.cs @@ -184,9 +184,9 @@ public GenericArrayTypeSymbol() static IList AsList(Val arr) { - var lst = arr.obj as IList; + var lst = arr._obj as IList; if(lst == null) - throw new Exception("Not a IList: " + (arr.obj != null ? arr.obj.GetType().Name : ""+arr)); + throw new Exception("Not a IList: " + (arr._obj != null ? arr.obj.GetType().Name : ""+arr)); return lst; } @@ -318,9 +318,9 @@ protected override HashSet CollectAllRelatedTypesSet() static IList AsIList(Val arr) { - var lst = arr.obj as IList; + var lst = arr._obj as IList; if(lst == null) - throw new Exception("Not a IList: " + (arr.obj != null ? arr.obj.GetType().Name : ""+arr)); + throw new Exception("Not a IList: " + (arr._obj != null ? arr._obj.GetType().Name : ""+arr)); return lst; } diff --git a/src/vm/type_map.cs b/src/vm/type_map.cs index c0a8cb96..7a7c6b2c 100644 --- a/src/vm/type_map.cs +++ b/src/vm/type_map.cs @@ -228,9 +228,9 @@ public GenericMapTypeSymbol() static IDictionary AsDictionary(Val map) { - var dict = map.obj as IDictionary; + var dict = map._obj as IDictionary; if(dict == null) - throw new Exception("Not a IDictionary: " + (map.obj != null ? map.obj.GetType().Name : ""+map)); + throw new Exception("Not a IDictionary: " + (map._obj != null ? map._obj.GetType().Name : ""+map)); return dict; } diff --git a/tests/test_type_casts.cs b/tests/test_type_casts.cs index 3a9bd015..70558d22 100644 --- a/tests/test_type_casts.cs +++ b/tests/test_type_casts.cs @@ -737,6 +737,12 @@ func int test3() CommonChecks(vm); }); } + + public interface IFoo + {} + + public class Foo : IFoo + {} [IsTested()] public void TestIsForClassImplementingNativeInterface() @@ -752,16 +758,21 @@ func bool test() { var ifs = new InterfaceSymbolNative( new Origin(), "IFoo", - null + null, + typeof(IFoo) ); ts.ns.Define(ifs); ifs.Setup(); - var cl = new ClassSymbolNative(new Origin(), "Foo", new List(){ ts.T("IFoo") }, + var cl = new ClassSymbolNative( + new Origin(), + "Foo", + new List(){ ts.T("IFoo") }, delegate(VM.Frame frm, ref Val v, IType type) { - v.SetObj(null/*dummy*/, type); - } + v.SetObj(new Foo(), type); + }, + typeof(Foo) ); ts.ns.Define(cl); cl.Setup();