Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support creating NamespaceReference against empty namespace #1892

Merged
merged 1 commit into from
Jun 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -1302,6 +1302,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