2
0

FunctionTests.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. using Jint.Native;
  2. using Jint.Native.Function;
  3. using Jint.Runtime;
  4. using Jint.Runtime.Interop;
  5. namespace Jint.Tests.Runtime;
  6. public class FunctionTests
  7. {
  8. private readonly Engine _engine;
  9. public FunctionTests()
  10. {
  11. _engine = new Engine()
  12. .SetValue("equal", new Action<object, object>(Assert.Equal));
  13. }
  14. [Fact]
  15. public void BindCombinesBoundArgumentsToCallArgumentsCorrectly()
  16. {
  17. _engine.Evaluate("var testFunc = function (a, b, c) { return a + ', ' + b + ', ' + c + ', ' + JSON.stringify(arguments); }");
  18. Assert.Equal("a, 1, a, {\"0\":\"a\",\"1\":1,\"2\":\"a\"}", _engine.Evaluate("testFunc('a', 1, 'a');").AsString());
  19. Assert.Equal("a, 1, a, {\"0\":\"a\",\"1\":1,\"2\":\"a\"}", _engine.Evaluate("testFunc.bind('anything')('a', 1, 'a');").AsString());
  20. }
  21. [Fact]
  22. public void ArrowFunctionShouldBeExtensible()
  23. {
  24. new Engine()
  25. .SetValue("assert", new Action<bool>(Assert.True))
  26. .Execute(@"
  27. var a = () => null
  28. Object.defineProperty(a, 'hello', { enumerable: true, get: () => 'world' })
  29. assert(a.hello === 'world')
  30. a.foo = 'bar';
  31. assert(a.foo === 'bar');
  32. ");
  33. }
  34. [Fact]
  35. public void BlockScopeFunctionShouldWork()
  36. {
  37. const string Script = @"
  38. function execute(doc, args){
  39. var i = doc;
  40. {
  41. function doSomething() {
  42. return 'ayende';
  43. }
  44. i.Name = doSomething();
  45. }
  46. }
  47. ";
  48. var engine = new Engine(options =>
  49. {
  50. options.Strict();
  51. });
  52. engine.Execute(Script);
  53. var obj = engine.Evaluate("var obj = {}; execute(obj); return obj;").AsObject();
  54. Assert.Equal("ayende", obj.Get("Name").AsString());
  55. }
  56. [Fact]
  57. public void ObjectCoercibleForCallable()
  58. {
  59. const string Script = @"
  60. var booleanCount = 0;
  61. Boolean.prototype.then = function() {
  62. booleanCount += 1;
  63. };
  64. function test() {
  65. this.then();
  66. }
  67. testFunction.call(true);
  68. assertEqual(booleanCount, 1);
  69. ";
  70. var engine = new Engine();
  71. engine
  72. .SetValue("testFunction", new ClrFunction(engine, "testFunction", (thisValue, args) =>
  73. {
  74. return engine.Invoke(thisValue, "then", new[] { JsValue.Undefined, args.At(0) });
  75. }))
  76. .SetValue("assertEqual", new Action<object, object>((a, b) => Assert.Equal(b, a)))
  77. .Execute(Script);
  78. }
  79. [Fact]
  80. public void AnonymousLambdaShouldHaveNameDefined()
  81. {
  82. Assert.True(_engine.Evaluate("(()=>{}).hasOwnProperty('name')").AsBoolean());
  83. }
  84. [Fact]
  85. public void CanInvokeConstructorsFromEngine()
  86. {
  87. _engine.Evaluate("class TestClass { constructor(a, b) { this.a = a; this.b = b; }}");
  88. _engine.Evaluate("function TestFunction(a, b) { this.a = a; this.b = b; }");
  89. var instanceFromClass = _engine.Construct("TestClass", "abc", 123).AsObject();
  90. Assert.Equal("abc", instanceFromClass.Get("a"));
  91. Assert.Equal(123, instanceFromClass.Get("b"));
  92. var instanceFromFunction = _engine.Construct("TestFunction", "abc", 123).AsObject();
  93. Assert.Equal("abc", instanceFromFunction.Get("a"));
  94. Assert.Equal(123, instanceFromFunction.Get("b"));
  95. var arrayInstance = (JsArray) _engine.Construct("Array", "abc", 123).AsObject();
  96. Assert.Equal((uint) 2, arrayInstance.Length);
  97. Assert.Equal("abc", arrayInstance[0]);
  98. Assert.Equal(123, arrayInstance[1]);
  99. }
  100. [Fact]
  101. public void FunctionInstancesCanBePassedToHost()
  102. {
  103. var engine = new Engine();
  104. Func<JsValue, JsValue[], JsValue> ev = null;
  105. void addListener(Func<JsValue, JsValue[], JsValue> callback)
  106. {
  107. ev = callback;
  108. }
  109. engine.SetValue("addListener", new Action<Func<JsValue, JsValue[], JsValue>>(addListener));
  110. engine.Execute(@"
  111. var a = 5;
  112. (function() {
  113. var acc = 10;
  114. addListener(function (val) {
  115. a = (val || 0) + acc;
  116. });
  117. })();
  118. ");
  119. Assert.Equal(5, engine.Evaluate("a"));
  120. ev(null, new JsValue[0]);
  121. Assert.Equal(10, engine.Evaluate("a"));
  122. ev(null, new JsValue[] { 20 });
  123. Assert.Equal(30, engine.Evaluate("a"));
  124. }
  125. [Fact]
  126. public void BoundFunctionsCanBePassedToHost()
  127. {
  128. var engine = new Engine();
  129. Func<JsValue, JsValue[], JsValue> ev = null;
  130. void addListener(Func<JsValue, JsValue[], JsValue> callback)
  131. {
  132. ev = callback;
  133. }
  134. engine.SetValue("addListener", new Action<Func<JsValue, JsValue[], JsValue>>(addListener));
  135. engine.Execute(@"
  136. var a = 5;
  137. (function() {
  138. addListener(function (acc, val) {
  139. a = (val || 0) + acc;
  140. }.bind(null, 10));
  141. })();
  142. ");
  143. Assert.Equal(5, engine.Evaluate("a"));
  144. ev(null, new JsValue[0]);
  145. Assert.Equal(10, engine.Evaluate("a"));
  146. ev(null, new JsValue[] { 20 });
  147. Assert.Equal(30, engine.Evaluate("a"));
  148. }
  149. [Fact]
  150. public void ConstructorsCanBePassedToHost()
  151. {
  152. var engine = new Engine();
  153. Func<JsValue, JsValue[], JsValue> ev = null;
  154. void addListener(Func<JsValue, JsValue[], JsValue> callback)
  155. {
  156. ev = callback;
  157. }
  158. engine.SetValue("addListener", new Action<Func<JsValue, JsValue[], JsValue>>(addListener));
  159. engine.Execute(@"addListener(Boolean)");
  160. Assert.Equal(true, ev(JsValue.Undefined, new JsValue[] { "test" }));
  161. Assert.Equal(true, ev(JsValue.Undefined, new JsValue[] { 5 }));
  162. Assert.Equal(false, ev(JsValue.Undefined, new JsValue[] { false }));
  163. Assert.Equal(false, ev(JsValue.Undefined, new JsValue[] { 0}));
  164. Assert.Equal(false, ev(JsValue.Undefined, new JsValue[] { JsValue.Undefined }));
  165. }
  166. [Fact]
  167. public void FunctionsShouldResolveToSameReference()
  168. {
  169. _engine.SetValue("equal", new Action<object, object>(Assert.Equal));
  170. _engine.Execute(@"
  171. function testFn() {}
  172. equal(testFn, testFn);
  173. ");
  174. }
  175. [Fact]
  176. public void CanInvokeCallForFunctionInstance()
  177. {
  178. _engine.Evaluate(@"
  179. (function () {
  180. function foo(a = 123) { return a; }
  181. foo()
  182. })")
  183. .As<Function>().Call();
  184. var result = _engine.Evaluate(@"
  185. (function () {
  186. class Foo { test() { return 123 } }
  187. let f = new Foo()
  188. return f.test()
  189. })")
  190. .As<Function>().Call();
  191. Assert.True(result.IsInteger());
  192. Assert.Equal(123, result.AsInteger());
  193. }
  194. [Fact]
  195. public void CanInvokeCallForFunctionInstanceWithDifferingArgCounts()
  196. {
  197. _engine.Execute("function foo() { return Array.from(arguments).join(','); }");
  198. var function = _engine.GetValue("foo");
  199. Assert.Equal("", function.Call());
  200. Assert.Equal("1", function.Call(1));
  201. Assert.Equal("1,2", function.Call(1, 2));
  202. Assert.Equal("1,2,3", function.Call(1, 2, 3));
  203. Assert.Equal("1,2,3,4", function.Call(1, 2, 3, 4));
  204. }
  205. [Fact]
  206. public void CanInvokeFunctionViaEngineInstance()
  207. {
  208. var function = _engine.Evaluate("function bar(a) { return a; }; bar;");
  209. Assert.Equal(123, _engine.Call(function, 123));
  210. Assert.Equal(123, _engine.Call("bar", 123));
  211. }
  212. [Fact]
  213. public void CanInvokeFunctionViaEngineInstanceWithCustomThisObj()
  214. {
  215. var function = _engine.Evaluate("function baz() { return this; }; baz;");
  216. Assert.Equal("I'm this!", TypeConverter.ToString(_engine.Call(function, "I'm this!", Arguments.Empty)));
  217. Assert.Equal("I'm this!", TypeConverter.ToString(function.Call("I'm this!", Arguments.Empty)));
  218. }
  219. [Fact]
  220. public void ArrowFunction()
  221. {
  222. const string Script = @"var f = (function() { return z => arguments[0]; }(5)); equal(5, f(6));";
  223. _engine.Execute(Script);
  224. }
  225. }