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

Fix throwing on accessing CLR FunctionDeclaration #1949

Merged
merged 1 commit into from
Aug 18, 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
40 changes: 40 additions & 0 deletions Jint.Tests/Runtime/InteropTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using Jint.Native;
using Jint.Native.Function;
using Jint.Runtime;
using Jint.Runtime.Interop;
using Jint.Tests.Runtime.Converters;
Expand Down Expand Up @@ -3545,4 +3546,43 @@ public void ShouldSeeClrMethods2()

props.Should().BeEquivalentTo(["Get_A"]);
}

[Fact]
public void ShouldNotThrowOnInspectingClrFunction()
{
var engine = new Engine();

engine.SetValue("clrDelegate", () => 4);

var val = engine.GetValue("clrDelegate");

var fn = val as Function;
var decl = fn!.FunctionDeclaration;

decl.Should().BeNull();
}

private class ShouldNotThrowOnInspectingClrFunctionTestClass
{
public int MyInt()
{
return 4;
}
}

[Fact]
public void ShouldNotThrowOnInspectingClrClassFunction()
{
var engine = new Engine();

engine.SetValue("clrCls", new ShouldNotThrowOnInspectingClrFunctionTestClass());

var val = engine.GetValue("clrCls");
var clrFn = val.Get("MyInt");

var fn = clrFn as Function;
var decl = fn!.FunctionDeclaration;

decl.Should().BeNull();
}
}
4 changes: 2 additions & 2 deletions Jint/Engine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1047,7 +1047,7 @@ private void GlobalDeclarationInstantiation(
var env = (FunctionEnvironment) ExecutionContext.LexicalEnvironment;
var strict = _isStrict || StrictModeScope.IsStrictModeCode;

var configuration = func.Initialize();
var configuration = func!.Initialize();
var parameterNames = configuration.ParameterNames;
var hasDuplicates = configuration.HasDuplicates;
var simpleParameterList = configuration.IsSimpleParameterList;
Expand Down Expand Up @@ -1618,4 +1618,4 @@ public EngineDebugView(Engine engine)
public Environment VariableEnvironment => _engine.ExecutionContext.VariableEnvironment;
public Environment LexicalEnvironment => _engine.ExecutionContext.LexicalEnvironment;
}
}
}
4 changes: 2 additions & 2 deletions Jint/Native/Function/Function.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public abstract partial class Function : ObjectInstance, ICallable
internal PropertyDescriptor? _nameDescriptor;

internal Environment? _environment;
internal readonly JintFunctionDefinition _functionDefinition = null!;
internal readonly JintFunctionDefinition? _functionDefinition;
internal readonly FunctionThisMode _thisMode;
internal JsValue _homeObject = Undefined;
internal ConstructorKind _constructorKind = ConstructorKind.Base;
Expand Down Expand Up @@ -71,7 +71,7 @@ internal Function(
}

// for example RavenDB wants to inspect this
public IFunction FunctionDeclaration => _functionDefinition.Function;
public IFunction? FunctionDeclaration => _functionDefinition?.Function;

internal override bool IsCallable => true;

Expand Down
4 changes: 2 additions & 2 deletions Jint/Native/Function/ScriptFunction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ internal ScriptFunction(
/// </summary>
protected internal override JsValue Call(JsValue thisObject, JsValue[] arguments)
{
var strict = _functionDefinition.Strict || _thisMode == FunctionThisMode.Strict;
var strict = _functionDefinition!.Strict || _thisMode == FunctionThisMode.Strict;
using (new StrictModeScope(strict, true))
{
try
Expand Down Expand Up @@ -158,7 +158,7 @@ ObjectInstance IConstructor.Construct(JsValue[] arguments, JsValue newTarget)

var context = _engine._activeEvaluationContext ?? new EvaluationContext(_engine);

var result = _functionDefinition.EvaluateBody(context, this, arguments);
var result = _functionDefinition!.EvaluateBody(context, this, arguments);

// The DebugHandler needs the current execution context before the return for stepping through the return point
// We exclude the empty constructor generated for classes without an explicit constructor.
Expand Down