JintFunctionDefinitionTest.cs 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. using Jint.Runtime.Interpreter;
  2. namespace Jint.Tests.Runtime.Interpreter;
  3. public class JintFunctionDefinitionTest
  4. {
  5. [Theory]
  6. [InlineData("function f(_ = probeParams = function() { return 42; }) { }", true)]
  7. [InlineData("function* g(_ = probeParams = function() { return 42; }) { }", true)]
  8. [InlineData("function x(t = {}) {}", false)]
  9. [InlineData("function x(e, t = {}) {}", false)]
  10. [InlineData("function x([t, e]) { }", false)]
  11. public void ShouldDetectParameterExpression(string functionCode, bool hasExpressions)
  12. {
  13. var parser = new Parser();
  14. var script = parser.ParseScript(functionCode);
  15. var function = (IFunction) script.Body.First();
  16. var state = JintFunctionDefinition.BuildState(function);
  17. state.HasParameterExpressions.Should().Be(hasExpressions);
  18. }
  19. [Theory]
  20. [InlineData("function g() { }", false)]
  21. [InlineData("function* g() { }", false)]
  22. [InlineData("async function g() { }", false)]
  23. [InlineData("() => { }", false)]
  24. [InlineData("async () => { }", false)]
  25. [InlineData("function g(a) { }", false)]
  26. [InlineData("function* g(a) { }", false)]
  27. [InlineData("async function g(a) { }", false)]
  28. [InlineData("(a) => { }", false)]
  29. [InlineData("async (a) => { }", false)]
  30. [InlineData("function g(a) { _ = arguments[0] }", false)]
  31. [InlineData("function* g(a) { _ = arguments[0] }", true)]
  32. [InlineData("async function g(a) { _ = arguments[0] }", true)]
  33. [InlineData("(a) => { _ = arguments[0] }", false)]
  34. [InlineData("async (a) => { _ = arguments[0] }", true)]
  35. public void ShouldIndicateArgumentsOwnershipIfNeeded(string functionCode, bool requiresOwnership)
  36. {
  37. var parser = new Parser();
  38. var script = parser.ParseScript(functionCode);
  39. Node statement = script.Body.First();
  40. var function = (IFunction) (
  41. statement is ExpressionStatement expr
  42. ? expr.Expression
  43. : statement
  44. );
  45. var state = JintFunctionDefinition.BuildState(function);
  46. state.RequiresInputArgumentsOwnership.Should().Be(requiresOwnership);
  47. }
  48. }