Skip to content

Commit

Permalink
Fix throwing on accessing CLR FunctionDeclaration (#1949)
Browse files Browse the repository at this point in the history
  • Loading branch information
lofcz authored Aug 18, 2024
1 parent ddbc2e3 commit 4767b3a
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 6 deletions.
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

0 comments on commit 4767b3a

Please sign in to comment.