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

Allowing to use nullable propagation on callables again. Fixed #1041 #1042

Merged
merged 2 commits into from
Jan 10, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
33 changes: 31 additions & 2 deletions Jint.Tests/Runtime/NullPropagation.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Esprima;
using System;
using Esprima;
using Jint.Native;
using Jint.Runtime;
using Jint.Runtime.Interop;
Expand All @@ -22,8 +23,18 @@ public bool TryPropertyReference(Engine engine, Reference reference, ref JsValue
return value.IsNull() || value.IsUndefined();
}

public bool TryGetCallable(Engine engine, object reference, out JsValue value)
public bool TryGetCallable(Engine engine, object callee, out JsValue value)
{
if (callee is Reference reference)
{
var name = reference.GetReferencedName().AsString();
if (name == "filter")
{
value = new ClrFunctionInstance(engine, "map", (thisObj, values) => engine.Realm.Intrinsics.Array.ConstructFast(0));
return true;
}
}

value = new ClrFunctionInstance(engine, "anonymous", (thisObj, values) => thisObj);
return true;
}
Expand All @@ -34,6 +45,24 @@ public bool CheckCoercible(JsValue value)
}
}

[Fact]
public void CanCallFilterOnNull()
{
var engine = new Engine(cfg => cfg.SetReferencesResolver(new NullPropagationReferenceResolver()));

const string Script = @"
var input = {};

var output = { Tags : input.Tags.filter(x=>x!=null) };
";

engine.Execute(Script);

var output = engine.GetValue("output").AsObject();

Assert.True(output.Get("Tags").IsArray());
}

[Fact]
public void NullPropagationTest()
{
Expand Down
10 changes: 6 additions & 4 deletions Jint/Runtime/Interpreter/Expressions/JintCallExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -167,11 +167,13 @@ private JsValue EvaluateCall(EvaluationContext context, JsValue func, object ref
if (baseValue.IsNullOrUndefined()
&& engine._referenceResolver.TryUnresolvableReference(engine, referenceRecord, out var value))
{
return value;
thisValue = value;
}
else
{
var refEnv = (EnvironmentRecord) baseValue;
thisValue = refEnv.WithBaseObject();
}

var refEnv = (EnvironmentRecord) baseValue;
thisValue = refEnv.WithBaseObject();
}
}
else
Expand Down