public void MappingRuntimeErrorDuringOutOfMemoryIsCorrect() { // Arrange const string input = @"var arr = []; for (var i = 0; i < 10000; i++) { arr.push('Current date: ' + new Date()); }"; JsRuntimeException exception = null; // Act using (IJsEngine jsEngine = new JintJsEngine( new JintSettings { MemoryLimit = 2 * 1024 * 1024 } )) { try { jsEngine.Execute(input); } catch (JsRuntimeException e) { exception = e; } } // Assert Assert.NotNull(exception); Assert.Equal("Runtime error", exception.Category); Assert.Matches(@"^Script has allocated \d+ but is limited to 2097152$", exception.Description); }
public void OverloadCallNoArgs() { var global = new Mock <IOverloads>(); var engine = new JintJsEngine(global.Object); engine.Execute("method()"); global.Verify(x => x.Method()); }
public void OverloadCallStringToBool() { var global = new Mock <IOverloads>(); var engine = new JintJsEngine(global.Object); engine.Execute("method('false')"); global.Verify(x => x.Method(true)); }
public static void ObjPreferableForInt() { var global = new Mock <IStringObjectOverload>(); var engine = new JintJsEngine(global.Object); engine.Execute("log(1)"); global.Verify(x => x.Log(1d), Times.Once); }
public static void ParamsMethodCallOneArg(string arg, object expected) { var global = new Mock <IParamsMethods>(); var engine = new JintJsEngine(global.Object); engine.Execute($"method1({arg})"); global.Verify(x => x.Method1(expected)); }
public static void ParamsMethodCallTwoArg() { var global = new Mock <IParamsMethods>(); var engine = new JintJsEngine(global.Object); engine.Execute("method1(1,'2')"); global.Verify(x => x.Method1(1d, "2")); }
public static void ParamsAndFixedArgument() { var global = new Mock <IParamsMethods>(); var engine = new JintJsEngine(global.Object); engine.Execute("method2(1,'23')"); global.Verify(x => x.Method2(1, "23")); }
public static void ParamsWithReturnValue() { var global = new Mock <IParamsMethods>(); var engine = new JintJsEngine(global.Object); engine.Execute("method3('23')"); global.Verify(x => x.Method3("23")); }
public static void ParamsMethodCallBoolAndArgs() { var global = new Mock <IParamsMethods>(); var engine = new JintJsEngine(global.Object); engine.Execute("method4(true)"); global.Verify(x => x.Method4(true)); }
public static void CallWarnObject() { var global = new Mock <IOverloadWithParams>(); var engine = new JintJsEngine(global.Object); engine.Execute("warn(this)"); global.Verify(x => x.Warn(global.Object)); }
public void OverloadCallObj() { var global = new Mock <IOverloads>(); var engine = new JintJsEngine(global.Object); engine.Execute("method({name:'x'})"); global.Verify(x => x.Method(It.IsAny <C>())); }
public static void CallCtorFunction(string code, int expectedWidth, int expectedHeight) { var global = new TestingGlobal(); var jsEngine = new JintJsEngine(global); var img = (Img)jsEngine.Evaluate(code); Assert.AreEqual(img.Width, expectedWidth, "Width"); Assert.AreEqual(img.Height, expectedHeight, "Height"); }
public async Task RunAsync() { using var engine = new JintJsEngine(); foreach (var entry in Container.Names) { var name = entry.Value; var type = entry.Key; engine.SetVariableValue(name, Container.Resolve(type)); } await Task.Run(() => engine.Execute(Code)); }
public void MappingTimeoutErrorDuringRegexHangingIsCorrect() { // Arrange const string input = @"var regexp = /^(\w+\s?)*$/, str = 'An input string that takes a long time or even makes this regular expression to hang!' ; // Will take a very long time regexp.test(str);"; JsTimeoutException exception = null; // Act using (var jsEngine = new JintJsEngine( new JintSettings { RegexTimeoutInterval = TimeSpan.FromMilliseconds(25) } )) { try { jsEngine.Execute(input, "regexp-hanging.js"); } catch (JsTimeoutException e) { exception = e; } } // Assert Assert.NotNull(exception); Assert.Equal("Timeout error", exception.Category); Assert.Equal("Script execution exceeded timeout.", exception.Description); Assert.Empty(exception.Type); Assert.Empty(exception.DocumentName); Assert.Equal(0, exception.LineNumber); Assert.Equal(0, exception.ColumnNumber); Assert.Empty(exception.SourceFragment); Assert.Empty(exception.CallStack); }
public void MappingRuntimeErrorDuringStatementsCountOverflowIsCorrect() { // Arrange const string input = @"while (true);"; JsRuntimeException exception = null; // Act using (var jsEngine = new JintJsEngine( new JintSettings { MaxStatements = 5 } )) { try { jsEngine.Execute(input, "infinite-loop.js"); } catch (JsRuntimeException e) { exception = e; } } // Assert Assert.NotNull(exception); Assert.Equal("Runtime error", exception.Category); Assert.Equal("The maximum number of statements executed have been reached.", exception.Description); Assert.Equal("RangeError", exception.Type); Assert.Empty(exception.DocumentName); Assert.Equal(0, exception.LineNumber); Assert.Equal(0, exception.ColumnNumber); Assert.Empty(exception.SourceFragment); Assert.Empty(exception.CallStack); }
public void MappingTimeoutErrorDuringExecutionOfCodeIsCorrect() { // Arrange const string input = @"while (true);"; JsTimeoutException exception = null; // Act using (var jsEngine = new JintJsEngine( new JintSettings { TimeoutInterval = TimeSpan.FromMilliseconds(30) } )) { try { jsEngine.Execute(input, "infinite-loop.js"); } catch (JsTimeoutException e) { exception = e; } } // Assert Assert.NotNull(exception); Assert.Equal("Timeout error", exception.Category); Assert.Equal("Script execution exceeded timeout.", exception.Description); Assert.Empty(exception.Type); Assert.Empty(exception.DocumentName); Assert.Equal(0, exception.LineNumber); Assert.Equal(0, exception.ColumnNumber); Assert.Empty(exception.SourceFragment); Assert.Empty(exception.CallStack); }
public static void DefineCtorFunction() { var jsEngine = new JintJsEngine(new TestingGlobal()); Assert.AreEqual("function", jsEngine.Evaluate("typeof Image")); }
public void MappingRuntimeErrorDuringRecursionDepthOverflowIsCorrect() { // Arrange const string input = @"function fibonacci(n) { if (n === 1) { return 1; } else if (n === 2) { return 1; } else { return fibonacci(n - 1) + fibonacci(n - 2); } } (function (fibonacci) { var a = 5; var b = 11; var c = fibonacci(b) - fibonacci(a); })(fibonacci);"; JsRuntimeException exception = null; // Act using (var jsEngine = new JintJsEngine( new JintSettings { MaxRecursionDepth = 5 } )) { try { jsEngine.Execute(input, "fibonacci.js"); } catch (JsRuntimeException e) { exception = e; } } // Assert Assert.NotNull(exception); Assert.Equal("Runtime error", exception.Category); Assert.Equal("The recursion is forbidden by script host.", exception.Description); Assert.Equal("RangeError", exception.Type); Assert.Empty(exception.DocumentName); Assert.Equal(0, exception.LineNumber); Assert.Equal(0, exception.ColumnNumber); Assert.Empty(exception.SourceFragment); Assert.Equal( " at fibonacci" + Environment.NewLine + " at fibonacci" + Environment.NewLine + " at fibonacci" + Environment.NewLine + " at fibonacci" + Environment.NewLine + " at fibonacci" + Environment.NewLine + " at fibonacci" + Environment.NewLine + " at Anonymous function", exception.CallStack ); }
public string AccessObjectIndex(string code) { var engine = new JintJsEngine(new { Data = new ObjectIndex() }); return((string)engine.Evaluate(code)); }