Skip to content

Commit

Permalink
Adding 'object GetNativeObject(Val v)' to INativeType and using it in…
Browse files Browse the repository at this point in the history
… Type.Is(..) checks
  • Loading branch information
pachanga committed Jul 23, 2024
1 parent a48c284 commit 884d471
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 19 deletions.
25 changes: 21 additions & 4 deletions src/vm/symbol.cs
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,12 @@ public System.Type GetNativeType()
{
return native_type;
}


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

public void Setup()
{
List<InterfaceSymbol> inherits = null;
Expand Down Expand Up @@ -984,13 +989,13 @@ public FieldSymbolScript()

void Getter(VM.Frame frm, Val ctx, ref Val v, FieldSymbol fld)
{
var m = (IList<Val>)ctx.obj;
var m = (IList<Val>)ctx._obj;
v.ValueCopyFrom(m[scope_idx]);
}

void Setter(VM.Frame frm, ref Val ctx, Val v, FieldSymbol fld)
{
var m = (IList<Val>)ctx.obj;
var m = (IList<Val>)ctx._obj;
var curr = m[scope_idx];
for(int i=0;i<curr._refs;++i)
{
Expand All @@ -1002,7 +1007,7 @@ void Setter(VM.Frame frm, ref Val ctx, Val v, FieldSymbol fld)

void Getref(VM.Frame frm, Val ctx, out Val v, FieldSymbol fld)
{
var m = (IList<Val>)ctx.obj;
var m = (IList<Val>)ctx._obj;
v = m[scope_idx];
}

Expand Down Expand Up @@ -1620,6 +1625,7 @@ public override string ToString()
public interface INativeType
{
System.Type GetNativeType();
object GetNativeObject(Val v);
}

public class ClassSymbolNative : ClassSymbol, INativeType
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand Down
21 changes: 16 additions & 5 deletions src/vm/type.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
8 changes: 4 additions & 4 deletions src/vm/type_array.cs
Original file line number Diff line number Diff line change
Expand Up @@ -184,9 +184,9 @@ public GenericArrayTypeSymbol()

static IList<Val> AsList(Val arr)
{
var lst = arr.obj as IList<Val>;
var lst = arr._obj as IList<Val>;
if(lst == null)
throw new Exception("Not a IList<Val>: " + (arr.obj != null ? arr.obj.GetType().Name : ""+arr));
throw new Exception("Not a IList<Val>: " + (arr._obj != null ? arr.obj.GetType().Name : ""+arr));
return lst;
}

Expand Down Expand Up @@ -318,9 +318,9 @@ protected override HashSet<IInstantiable> 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;
}

Expand Down
4 changes: 2 additions & 2 deletions src/vm/type_map.cs
Original file line number Diff line number Diff line change
Expand Up @@ -228,9 +228,9 @@ public GenericMapTypeSymbol()

static IDictionary<Val,Val> AsDictionary(Val map)
{
var dict = map.obj as IDictionary<Val,Val>;
var dict = map._obj as IDictionary<Val,Val>;
if(dict == null)
throw new Exception("Not a IDictionary<Val,Val>: " + (map.obj != null ? map.obj.GetType().Name : ""+map));
throw new Exception("Not a IDictionary<Val,Val>: " + (map._obj != null ? map._obj.GetType().Name : ""+map));
return dict;
}

Expand Down
19 changes: 15 additions & 4 deletions tests/test_type_casts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -737,6 +737,12 @@ func int test3()
CommonChecks(vm);
});
}

public interface IFoo
{}

public class Foo : IFoo
{}

[IsTested()]
public void TestIsForClassImplementingNativeInterface()
Expand All @@ -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<ProxyType>(){ ts.T("IFoo") },
var cl = new ClassSymbolNative(
new Origin(),
"Foo",
new List<ProxyType>(){ 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();
Expand Down

0 comments on commit 884d471

Please sign in to comment.