Skip to content
This repository has been archived by the owner on Mar 9, 2021. It is now read-only.

Commit

Permalink
Can now cast all objects to [Serializable] types without custom type …
Browse files Browse the repository at this point in the history
…check code (#419)
  • Loading branch information
erik-kallen committed Jul 24, 2015
1 parent c1d9634 commit 7e38606
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 0 deletions.
5 changes: 5 additions & 0 deletions Runtime/CoreLib.Plugin/OOPEmulator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,11 @@ private void AddClassMembers(JsClass c, string typevarName, List<JsStatement> st
}
_errorReporter.Region = oldReg;
}
else {
stmts.Add(JsExpression.Assign(
JsExpression.Member(JsExpression.Identifier(typevarName), "isInstanceOfType"),
JsExpression.FunctionDefinition(new string[0], JsStatement.Return(JsExpression.True))));
}
}

if (MetadataUtils.IsJsGeneric(c.CSharpTypeDefinition, _metadataImporter)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,26 @@ public void GetTypeOnNullInstanceThrowsException() {
}

#pragma warning disable 219
private T Cast<T>(object o) {
return (T)o;
}

[Test]
public void CastOperatorForSerializableTypeWithoutTypeCheckCodeAlwaysSucceedsGeneric() {
object o = new object();
var b = Cast<BS>(o);
Assert.IsTrue(ReferenceEquals(o, b));
}

[Test]
public void CastOperatorsWorkForSerializableTypesWithCustomTypeCheckCodeGeneric() {
object o1 = new { x = 1 };
object o2 = new { x = 1, y = 2 };
Assert.Throws<InvalidCastException>(() => { var x = Cast<DS>(o1); });
var ds = Cast<DS>(o2);
Assert.IsTrue(ReferenceEquals(o2, ds));
}

[Test]
public void CastOperatorsWorkForSerializableTypesWithCustomTypeCheckCode() {
object o1 = new { x = 1 };
Expand Down
9 changes: 9 additions & 0 deletions Runtime/CoreLib.Tests/OOPEmulatorTests/ClassTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -833,6 +833,9 @@ [Serializable] public class B {}
var $this = {B}.$ctor();
return $this;
};
$D.isInstanceOfType = function() {
return true;
};
global.D = $D;
-
{Script}.initClass($D, $asm, {}, {B});
Expand All @@ -859,6 +862,9 @@ [Serializable] public interface I1 {}
var $this = {};
return $this;
};
$C.isInstanceOfType = function() {
return true;
};
global.C = $C;
-
{Script}.initClass($C, $asm, {}, null, [{I1}]);
Expand Down Expand Up @@ -1131,6 +1137,9 @@ [Serializable] public interface I2 {}
var $I3 = function() {
};
$I3.__typeName = 'I3';
$I3.isInstanceOfType = function() {
return true;
};
global.I3 = $I3;
-
{Script}.initInterface($I3, $asm, {}, [{I2}]);
Expand Down
1 change: 1 addition & 0 deletions history.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
Can now cast all objects to [Serializable] types without custom type check code (#419).
Fixed handling of null in static string.Equals and non-static string.CompareTo and JsDate.CompareTo (#412).
BREAKING CHANGE: Use full name instead of assembly-qualified names for generic arguments when creating generic type names in order to work around a bug regarding generic inheritance (#385, #484).
Fixed a bug causing lifted comparisons to return the wrong value (#416).
Expand Down

0 comments on commit 7e38606

Please sign in to comment.