Skip to content

Commit

Permalink
Allow JS class to extend CLR type
Browse files Browse the repository at this point in the history
  • Loading branch information
lahma committed Jan 11, 2022
1 parent ceee6b2 commit 91d119e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
15 changes: 15 additions & 0 deletions Jint.Tests/Runtime/InteropTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -787,6 +787,21 @@ public void CanUseDelegateAsFunction()
");
}

[Fact]
public void JavaScriptClassCanExtendClrType()
{
var engine = new Engine();
engine.SetValue("TestClass", TypeReference.CreateTypeReference<TestClass>(engine));

engine.Execute("class MyExtendedType extends TestClass {}");
engine.Evaluate("let obj = new MyExtendedType();");

engine.Evaluate("obj.setString('Hello World!');");
var value = engine.Evaluate("obj.string");

Assert.Equal("Hello World!", value);
}

private struct TestStruct
{
public int Value;
Expand Down
9 changes: 5 additions & 4 deletions Jint/Native/Function/ClassDefinition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Jint.Runtime;
using Jint.Runtime.Descriptors;
using Jint.Runtime.Environments;
using Jint.Runtime.Interop;
using Jint.Runtime.Interpreter;
using Jint.Runtime.Interpreter.Expressions;

Expand Down Expand Up @@ -81,7 +82,7 @@ static MethodDefinition CreateConstructorMethodDefinition(string source)
protoParent = null;
constructorParent = engine.Realm.Intrinsics.Function.PrototypeObject;
}
else if (!superclass!.IsConstructor)
else if (!superclass.IsConstructor)
{
ExceptionHelper.ThrowTypeError(engine.Realm, "super class is not a constructor");
}
Expand All @@ -92,13 +93,13 @@ static MethodDefinition CreateConstructorMethodDefinition(string source)
{
protoParent = protoParentObject;
}
else if (temp._type == InternalTypes.Null)
else if (temp.IsNull() || superclass is TypeReference)
{
// OK
}
else
{
ExceptionHelper.ThrowTypeError(engine.Realm);
ExceptionHelper.ThrowTypeError(engine.Realm, "cannot resolve super class prototype chain");
return null!;
}

Expand Down Expand Up @@ -209,4 +210,4 @@ static MethodDefinition CreateConstructorMethodDefinition(string source)
}
}
}
}
}

0 comments on commit 91d119e

Please sign in to comment.