Skip to content

Commit

Permalink
Add support for supplying thisObj
Browse files Browse the repository at this point in the history
  • Loading branch information
lahma committed Jan 24, 2022
1 parent 11ce1ba commit 60b8187
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
11 changes: 11 additions & 0 deletions Jint.Tests/Runtime/FunctionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -193,5 +193,16 @@ public void CanInvokeFunctionViaEngineInstance()
Assert.Equal(123, engine.Call(function, 123));
Assert.Equal(123, engine.Call("bar", 123));
}

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

var function = engine.Evaluate("function baz() { return this; }; baz;");

Assert.Equal("I'm this!", TypeConverter.ToString(engine.Call(function, "I'm this!", Arguments.Empty)));
Assert.Equal("I'm this!", TypeConverter.ToString(function.Call("I'm this!", Arguments.Empty)));
}
}
}
2 changes: 1 addition & 1 deletion Jint/Engine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1245,7 +1245,7 @@ public JsValue Call(JsValue callable, params JsValue[] arguments)
/// <param name="thisObject">Value bound as this.</param>
/// <param name="arguments">The arguments of the call.</param>
/// <returns>The value returned by the call.</returns>
private JsValue Call(JsValue callable, JsValue thisObject, params JsValue[] arguments)
public JsValue Call(JsValue callable, JsValue thisObject, JsValue[] arguments)
{
JsValue Callback()
{
Expand Down
13 changes: 13 additions & 0 deletions Jint/JsValueExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,19 @@ public static JsValue Call(this JsValue value, params JsValue[] arguments)
return objectInstance.Engine.Call(value, arguments);
}

[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static JsValue Call(this JsValue value, JsValue thisObj, params JsValue[] arguments)
{
if (value is not ObjectInstance objectInstance)
{
ExceptionHelper.ThrowArgumentException(value + " is not object");
return null;
}

return objectInstance.Engine.Call(value, thisObj, arguments);
}

/// <summary>
/// If the value is a Promise
/// 1. If "Fulfilled" returns the value it was fulfilled with
Expand Down

0 comments on commit 60b8187

Please sign in to comment.