Skip to content

Commit

Permalink
Support creating NamespaceReference against empty namespace (#1888)
Browse files Browse the repository at this point in the history
  • Loading branch information
lahma authored Jun 13, 2024
1 parent 7414296 commit 5d91655
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 7 deletions.
5 changes: 5 additions & 0 deletions Jint.Tests/Runtime/Domain/Shape.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
using Jint.Tests.Runtime.Domain;

public class ShapeWithoutNameSpace : Shapes.Shape
{
public override double Perimeter() => 42;
}

namespace Shapes
{
public abstract class Shape
Expand Down
16 changes: 16 additions & 0 deletions Jint.Tests/Runtime/InteropTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1301,6 +1301,22 @@ public void ShouldImportNamespace()
");
}

[Fact]
public void ShouldImportEmptyNamespace()
{
RunTest("""
var nullSpace = importNamespace(null);
var c1 = new nullSpace.ShapeWithoutNameSpace();
assert(c1.Perimeter() === 42);
var undefinedSpace = importNamespace(undefined);
var c2 = new undefinedSpace.ShapeWithoutNameSpace();
assert(c2.Perimeter() === 42);
var defaultSpace = importNamespace();
var c3 = new defaultSpace.ShapeWithoutNameSpace();
assert(c3.Perimeter() === 42);
""");
}

[Fact]
public void ShouldConstructReferenceTypeWithParameters()
{
Expand Down
3 changes: 1 addition & 2 deletions Jint/Options.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,7 @@ internal void Apply(Engine engine)
engine.Realm.GlobalObject.SetProperty("importNamespace", new PropertyDescriptor(new ClrFunction(
engine,
"importNamespace",
(thisObj, arguments) =>
new NamespaceReference(engine, TypeConverter.ToString(arguments.At(0)))),
(_, arguments) => new NamespaceReference(engine, arguments.At(0).IsNullOrUndefined() ? null : TypeConverter.ToString(arguments.At(0)))),
PropertyFlag.AllForbidden));

engine.Realm.GlobalObject.SetProperty("clrHelper", new PropertyDescriptor(ObjectWrapper.Create(engine, new ClrHelper(Interop)), PropertyFlag.AllForbidden));
Expand Down
15 changes: 10 additions & 5 deletions Jint/Runtime/Interop/NamespaceReference.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ namespace Jint.Runtime.Interop
[RequiresUnreferencedCode("Dynamic loading")]
public class NamespaceReference : ObjectInstance, ICallable
{
private readonly string _path;
private readonly string? _path;

public NamespaceReference(Engine engine, string path) : base(engine)
public NamespaceReference(Engine engine, string? path) : base(engine)
{
_path = path;
}
Expand Down Expand Up @@ -75,7 +75,9 @@ JsValue ICallable.Call(JsValue thisObject, JsValue[] arguments)

public override JsValue Get(JsValue property, JsValue receiver)
{
var newPath = _path + "." + property;
var newPath = string.IsNullOrEmpty(_path)
? property.ToString()
: $"{_path}.{property}";

return GetPath(newPath);
}
Expand Down Expand Up @@ -123,8 +125,11 @@ public JsValue GetPath(string path)
}

var lastPeriodPos = path.LastIndexOf('.');
var trimPath = path.Substring(0, lastPeriodPos);
type = GetType(assembly, trimPath);
if (lastPeriodPos != -1)
{
var trimPath = path.Substring(0, lastPeriodPos);
type = GetType(assembly, trimPath);
}
if (type != null)
{
foreach (Type nType in GetAllNestedTypes(type))
Expand Down

0 comments on commit 5d91655

Please sign in to comment.