protected override void InnerExecute(IPrecompiledScript precompiledScript) { var jintPrecompiledScript = precompiledScript as JintPrecompiledScript; if (jintPrecompiledScript == null) { throw new WrapperUsageException( string.Format(CoreStrings.Usage_CannotConvertPrecompiledScriptToInternalType, typeof(JintPrecompiledScript).FullName), Name, Version ); } lock (_executionSynchronizer) { try { _jsEngine.Execute(jintPrecompiledScript.ParsedScript); } catch (OriginalRuntimeException e) { throw WrapRuntimeException(e); } catch (TimeoutException e) { throw WrapTimeoutException(e); } } }
protected override void InnerExecute(IPrecompiledScript precompiledScript) { var chakraCorePrecompiledScript = precompiledScript as ChakraCorePrecompiledScript; if (chakraCorePrecompiledScript == null) { throw new WrapperUsageException( string.Format(CoreStrings.Usage_CannotConvertPrecompiledScriptToInternalType, typeof(ChakraCorePrecompiledScript).FullName), Name, Version ); } _dispatcher.Invoke(() => { using (CreateJsScope()) { try { JsContext.RunSerializedScript(chakraCorePrecompiledScript.Code, chakraCorePrecompiledScript.CachedBytes, chakraCorePrecompiledScript.LoadScriptSourceCodeCallback, _jsSourceContext++, chakraCorePrecompiledScript.DocumentName); } catch (OriginalException e) { throw WrapJsException(e); } finally { GC.KeepAlive(chakraCorePrecompiledScript); } } }); }
protected override void InnerExecute(IPrecompiledScript precompiledScript) { var jurassicPrecompiledScript = precompiledScript as JurassicPrecompiledScript; if (jurassicPrecompiledScript == null) { throw new WrapperUsageException( string.Format(CoreStrings.Usage_CannotConvertPrecompiledScriptToInternalType, typeof(JurassicPrecompiledScript).FullName), Name, Version ); } lock (_executionSynchronizer) { try { jurassicPrecompiledScript.CompiledScript.Execute(_jsEngine); } catch (OriginalJavaScriptException e) { throw WrapJavaScriptException(e); } } }
public virtual void ExecutionOfPrecompiledResourceByNameAndAssemblyIsCorrect() { // Arrange const string resourceName = "JavaScriptEngineSwitcher.Tests.Resources.declinationOfDays.js"; const string functionName = "declinationOfDays"; const int itemCount = 4; int[] inputDays = new int[itemCount] { 0, 1, 3, 80 }; string[] targetOutputStrings = new string[itemCount] { "дней", "день", "дня", "дней" }; string[] outputStrings = new string[itemCount]; // Act bool supportsScriptPrecompilation = false; IPrecompiledScript precompiledResource = null; using (var jsEngine = CreateJsEngine()) { supportsScriptPrecompilation = jsEngine.SupportsScriptPrecompilation; if (supportsScriptPrecompilation) { precompiledResource = jsEngine.PrecompileResource(resourceName, typeof(PrecompilationTestsBase).GetTypeInfo().Assembly); jsEngine.Execute(precompiledResource); outputStrings[0] = jsEngine.CallFunction <string>(functionName, inputDays[0]); } } if (supportsScriptPrecompilation) { Parallel.For(1, itemCount, itemIndex => { using (var jsEngine = CreateJsEngine()) { jsEngine.Execute(precompiledResource); outputStrings[itemIndex] = jsEngine.CallFunction <string>(functionName, inputDays[itemIndex]); } }); } // Assert if (supportsScriptPrecompilation) { for (int itemIndex = 0; itemIndex < itemCount; itemIndex++) { Assert.Equal(targetOutputStrings[itemIndex], outputStrings[itemIndex]); } } }
public virtual void ExecutionOfPrecompiledFileIsCorrect() { // Arrange string filePath = Path.GetFullPath(Path.Combine(_baseDirectoryPath, "../SharedFiles/declinationOfMinutes.js")); const string functionName = "declinationOfMinutes"; const int itemCount = 4; int[] inputMinutes = new int[itemCount] { 0, 1, 22, 88 }; string[] targetOutputStrings = new string[itemCount] { "минут", "минута", "минуты", "минут" }; string[] outputStrings = new string[itemCount]; // Act bool supportsScriptPrecompilation = false; IPrecompiledScript precompiledFile = null; using (var jsEngine = CreateJsEngine()) { supportsScriptPrecompilation = jsEngine.SupportsScriptPrecompilation; if (supportsScriptPrecompilation) { precompiledFile = jsEngine.PrecompileFile(filePath); jsEngine.Execute(precompiledFile); outputStrings[0] = jsEngine.CallFunction <string>(functionName, inputMinutes[0]); } } if (supportsScriptPrecompilation) { Parallel.For(1, itemCount, itemIndex => { using (var jsEngine = CreateJsEngine()) { jsEngine.Execute(precompiledFile); outputStrings[itemIndex] = jsEngine.CallFunction <string>(functionName, inputMinutes[itemIndex]); } }); } // Assert if (supportsScriptPrecompilation) { for (int itemIndex = 0; itemIndex < itemCount; itemIndex++) { Assert.Equal(targetOutputStrings[itemIndex], outputStrings[itemIndex]); } } }
public virtual void ExecutionOfPrecompiledResourceByNameAndTypeIsCorrect() { // Arrange const string resourceName = "Resources.declinationOfHours.js"; const string functionName = "declinationOfHours"; const int itemCount = 4; int[] inputHours = new int[itemCount] { 0, 1, 24, 48 }; string[] targetOutputStrings = new string[itemCount] { "часов", "час", "часа", "часов" }; string[] outputStrings = new string[itemCount]; // Act bool supportsScriptPrecompilation = false; IPrecompiledScript precompiledResource = null; using (var jsEngine = CreateJsEngine()) { supportsScriptPrecompilation = jsEngine.SupportsScriptPrecompilation; if (supportsScriptPrecompilation) { precompiledResource = jsEngine.PrecompileResource(resourceName, typeof(PrecompilationTestsBase)); jsEngine.Execute(precompiledResource); outputStrings[0] = jsEngine.CallFunction <string>(functionName, inputHours[0]); } } if (supportsScriptPrecompilation) { Parallel.For(1, itemCount, itemIndex => { using (var jsEngine = CreateJsEngine()) { jsEngine.Execute(precompiledResource); outputStrings[itemIndex] = jsEngine.CallFunction <string>(functionName, inputHours[itemIndex]); } }); } // Assert if (supportsScriptPrecompilation) { for (int itemIndex = 0; itemIndex < itemCount; itemIndex++) { Assert.Equal(targetOutputStrings[itemIndex], outputStrings[itemIndex]); } } }
/// <summary> /// Transliterates a strings /// </summary> /// <param name="createJsEngine">Delegate for create an instance of the JS engine</param> /// <param name="withPrecompilation">Flag for whether to allow execution of JS code with pre-compilation</param> private static void TransliterateStrings(Func <IJsEngine> createJsEngine, bool withPrecompilation) { // Arrange string[] outputStrings = new string[ItemCount]; IPrecompiledScript precompiledCode = null; // Act using (var jsEngine = createJsEngine()) { if (withPrecompilation) { if (!jsEngine.SupportsScriptPrecompilation) { throw new NotSupportedException($"{jsEngine.Name} does not support precompilation."); } precompiledCode = jsEngine.Precompile(_libraryCode, LibraryFileName); jsEngine.Execute(precompiledCode); } else { jsEngine.Execute(_libraryCode, LibraryFileName); } outputStrings[0] = jsEngine.CallFunction <string>(FunctionName, _inputStrings[0], _inputTypes[0]); } for (int itemIndex = 1; itemIndex < ItemCount; itemIndex++) { using (var jsEngine = createJsEngine()) { if (withPrecompilation) { jsEngine.Execute(precompiledCode); } else { jsEngine.Execute(_libraryCode, LibraryFileName); } outputStrings[itemIndex] = jsEngine.CallFunction <string>(FunctionName, _inputStrings[itemIndex], _inputTypes[itemIndex]); } } // Assert for (int itemIndex = 0; itemIndex < ItemCount; itemIndex++) { Assert.Equal(_targetOutputStrings[itemIndex], outputStrings[itemIndex]); } }
/// <summary> /// Render a templates /// </summary> /// <param name="createJsEngine">Delegate for create an instance of the JS engine</param> /// <param name="withPrecompilation">Flag for whether to allow execution of JS code with pre-compilation</param> private static void RenderTemplates(Func <IJsEngine> createJsEngine, bool withPrecompilation) { // Arrange IPrecompiledScript precompiledCode = null; // Act using (var jsEngine = createJsEngine()) { if (withPrecompilation) { if (!jsEngine.SupportsScriptPrecompilation) { throw new NotSupportedException($"{jsEngine.Name} does not support precompilation."); } precompiledCode = jsEngine.Precompile(_libraryCode, LibraryFileName); jsEngine.Execute(precompiledCode); } else { jsEngine.Execute(_libraryCode, LibraryFileName); } _contentItems[0].Output = jsEngine.CallFunction <string>(FunctionName, _contentItems[0].TemplateCode, _contentItems[0].SerializedData); } for (int itemIndex = 1; itemIndex < _contentItems.Length; itemIndex++) { using (var jsEngine = createJsEngine()) { if (withPrecompilation) { jsEngine.Execute(precompiledCode); } else { jsEngine.Execute(_libraryCode, LibraryFileName); } _contentItems[itemIndex].Output = jsEngine.CallFunction <string>(FunctionName, _contentItems[itemIndex].TemplateCode, _contentItems[itemIndex].SerializedData); } } // Assert foreach (ContentItem item in _contentItems) { Assert.Equal(item.TargetOutput, item.Output, true); } }
public void MappingRuntimeErrorDuringExecutionOfPrecompiledCodeIsCorrect() { // Arrange const string input = @"function getItem(items, itemIndex) { var item = items[itemIndex]; return item; } (function (getItem) { var items = null, item = getItem(items, 5) ; return item; })(getItem);"; JsRuntimeException exception = null; // Act using (var jsEngine = CreateJsEngine()) { try { IPrecompiledScript precompiledScript = jsEngine.Precompile(input, "get-item.js"); jsEngine.Execute(precompiledScript); } catch (JsRuntimeException e) { exception = e; } } // Assert Assert.NotNull(exception); Assert.Equal("Runtime error", exception.Category); Assert.Equal("Cannot read properties of null (reading '5')", exception.Description); Assert.Equal("TypeError", exception.Type); Assert.Equal("get-item.js", exception.DocumentName); Assert.Equal(2, exception.LineNumber); Assert.Equal(18, exception.ColumnNumber); Assert.Equal(" var item = items[itemIndex];", exception.SourceFragment); Assert.Equal( " at getItem (get-item.js:2:18)" + Environment.NewLine + " at get-item.js:9:10" + Environment.NewLine + " at get-item.js:13:3", exception.CallStack ); }
public void MappingRuntimeErrorDuringExecutionOfPrecompiledCodeIsCorrect() { // Arrange const string input = @"function getItem(items, itemIndex) { var item = items[itemIndex]; return item; } (function (getItem) { var items = null, item = getItem(items, 5) ; return item; })(getItem);"; JsRuntimeException exception = null; // Act using (var jsEngine = CreateJsEngine()) { try { IPrecompiledScript precompiledScript = jsEngine.Precompile(input, "get-item.js"); jsEngine.Execute(precompiledScript); } catch (JsRuntimeException e) { exception = e; } } // Assert Assert.NotNull(exception); Assert.Equal("Runtime error", exception.Category); Assert.Equal("Unable to get property '5' of undefined or null reference", exception.Description); Assert.Equal("TypeError", exception.Type); Assert.Equal("get-item.js", exception.DocumentName); Assert.Equal(2, exception.LineNumber); Assert.Equal(2, exception.ColumnNumber); Assert.Empty(exception.SourceFragment); Assert.Equal( " at getItem (get-item.js:2:2)" + Environment.NewLine + " at Anonymous function (get-item.js:9:3)" + Environment.NewLine + " at Global code (get-item.js:7:2)", exception.CallStack ); }
public void MappingRuntimeErrorDuringExecutionOfPrecompiledCodeIsCorrect() { // Arrange const string input = @"function getItem(items, itemIndex) { var item = items[itemIndex]; return item; } (function (getItem) { var items = null, item = getItem(items, 5) ; return item; })(getItem);"; JsRuntimeException exception = null; // Act using (var jsEngine = CreateJsEngine()) { try { IPrecompiledScript precompiledScript = jsEngine.Precompile(input, "get-item.js"); jsEngine.Execute(precompiledScript); } catch (JsRuntimeException e) { exception = e; } } // Assert Assert.NotNull(exception); Assert.Equal("Runtime error", exception.Category); Assert.Equal("null cannot be converted to an object", exception.Description); Assert.Equal("TypeError", exception.Type); Assert.Equal("get-item.js", exception.DocumentName); Assert.Equal(2, exception.LineNumber); Assert.Equal(0, exception.ColumnNumber); Assert.Empty(exception.SourceFragment); Assert.Equal( " at getItem (get-item.js:2)", exception.CallStack ); }
public void MappingCompilationErrorDuringPrecompilationOfCodeIsCorrect() { // Arrange const string input = @"function guid() { function s4() { return Math.floor((1 + Math.random() * 0x10000) .toString(16) .substring(1) ; } var result = s4() + s4() + '-' + s4() + '-' + s4() + '-' + s4() + '-' + s4() + s4() + s4(); return result; }"; IPrecompiledScript precompiledScript = null; JsCompilationException exception = null; // Act using (var jsEngine = CreateJsEngine()) { try { precompiledScript = jsEngine.Precompile(input, "guid.js"); } catch (JsCompilationException e) { exception = e; } } // Assert Assert.Null(precompiledScript); Assert.NotNull(exception); Assert.Equal("Compilation error", exception.Category); Assert.Equal("Expected ')'", exception.Description); Assert.Equal("SyntaxError", exception.Type); Assert.Equal("guid.js", exception.DocumentName); Assert.Equal(6, exception.LineNumber); Assert.Equal(4, exception.ColumnNumber); Assert.Equal(" ;", exception.SourceFragment); }
public void GenerationOfRuntimeErrorMessageIsCorrect() { // Arrange const string input = @"function getFullName(firstName, lastName) { var fullName = firstName + ' ' + middleName + ' ' + lastName; return fullName; } (function (getFullName) { var firstName = 'Vasya', lastName = 'Pupkin' ; return getFullName(firstName, lastName); })(getFullName);"; string targetOutput = "ReferenceError: middleName is not defined" + Environment.NewLine + " at getFullName (get-full-name.js:2:35) -> var fullName = firstName + "+ "' ' + middleName + ' ' + lastName;" + Environment.NewLine + " at get-full-name.js:12:9" + Environment.NewLine + " at get-full-name.js:13:3" ; JsRuntimeException exception = null; // Act using (var jsEngine = CreateJsEngine()) { try { IPrecompiledScript precompiledScript = jsEngine.Precompile(input, "get-full-name.js"); jsEngine.Execute(precompiledScript); } catch (JsRuntimeException e) { exception = e; } } Assert.NotNull(exception); Assert.Equal(targetOutput, exception.Message); }
protected override void InnerExecute(IPrecompiledScript precompiledScript) { var jintPrecompiledScript = precompiledScript as JintPrecompiledScript; if (jintPrecompiledScript == null) { throw new WrapperUsageException( string.Format(CoreStrings.Usage_CannotConvertPrecompiledScriptToInternalType, typeof(JintPrecompiledScript).FullName), Name, Version ); } lock (_executionSynchronizer) { try { _jsEngine.Execute(jintPrecompiledScript.Program); } catch (OriginalJavaScriptException e) { throw WrapJavaScriptException(e); } catch (OriginalMemoryLimitExceededException e) { throw WrapMemoryLimitExceededException(e); } catch (OriginalRecursionDepthOverflowException e) { throw WrapRecursionDepthOverflowException(e); } catch (OriginalStatementsCountOverflowException e) { throw WrapStatementsCountOverflowException(e); } catch (TimeoutException e) { throw WrapTimeoutException(e); } } }
public void GenerationOfCompilationErrorMessageIsCorrect() { // Arrange const string input = @"function makeId(length) { var result = '', possible = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789', charIndex ; for (charIndex = 0; charIndex < length; charIndex++) result += possible.charAt(Math.floor(Math.random() * possible.length)); } return result; }"; string targetOutput = "SyntaxError: 'return' statement outside of function" + Environment.NewLine + " at make-id.js:11:2 -> return result;" ; IPrecompiledScript precompiledScript = null; JsCompilationException exception = null; // Act using (var jsEngine = CreateJsEngine()) { try { precompiledScript = jsEngine.Precompile(input, "make-id.js"); } catch (JsCompilationException e) { exception = e; } } Assert.Null(precompiledScript); Assert.NotNull(exception); Assert.Equal(targetOutput, exception.Message); }
protected override void InnerExecute(IPrecompiledScript precompiledScript) { var msiePrecompiledScript = precompiledScript as MsiePrecompiledScript; if (msiePrecompiledScript == null) { throw new WrapperUsageException( string.Format(CoreStrings.Usage_CannotConvertPrecompiledScriptToInternalType, typeof(MsiePrecompiledScript).FullName), Name, Version ); } try { _jsEngine.Execute(msiePrecompiledScript.PrecompiledScript); } catch (OriginalException e) { throw WrapJsException(e); } }
static FormulaManager() { var engineSwitcher = JsEngineSwitcher.Current; engineSwitcher.EngineFactories.Add(new V8JsEngineFactory()); engineSwitcher.DefaultEngineName = V8JsEngine.EngineName; using (var engine = JsEngineSwitcher.Current.CreateDefaultEngine()) { startCode = engine.Precompile(@"function returnDate(date) { return date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate(); } function getDate(arg) { var args = arg.split('-'); return new Date(parseInt(args[0],10),parseInt(args[1],10),parseInt(args[2],10)); } "); } }
protected override void InnerExecute(IPrecompiledScript precompiledScript) { var v8PrecompiledScript = precompiledScript as V8PrecompiledScript; if (v8PrecompiledScript == null) { throw new WrapperUsageException( string.Format(CoreStrings.Usage_CannotConvertPrecompiledScriptToInternalType, typeof(V8PrecompiledScript).FullName), Name, Version ); } bool cacheAccepted; try { using (OriginalScript script = _jsEngine.Compile(v8PrecompiledScript.DocumentName, v8PrecompiledScript.Code, v8PrecompiledScript.CacheKind, v8PrecompiledScript.CachedBytes, out cacheAccepted)) { if (!cacheAccepted) { throw new WrapperUsageException(Strings.Usage_PrecompiledScriptNotAccepted, Name, Version); } _jsEngine.Execute(script); } } catch (OriginalException e) { throw WrapScriptEngineException(e); } catch (OriginalInterruptedException e) { throw WrapScriptInterruptedException(e); } }
public virtual void Execute(IPrecompiledScript precompiledScript) { VerifyNotDisposed(); if (precompiledScript == null) { throw new ArgumentNullException( nameof(precompiledScript), string.Format(Strings.Common_ArgumentIsNull, nameof(precompiledScript)) ); } if (precompiledScript.EngineName != Name) { throw new JsUsageException( string.Format(Strings.Usage_CannotExecutePrecompiledScriptForAnotherJsEngine, precompiledScript.EngineName), Name, Version ); } InnerExecute(precompiledScript); }
protected override IPrecompiledScript InnerPrecompile(string code, string documentName) { string uniqueDocumentName = _documentNameManager.GetUniqueName(documentName); IPrecompiledScript precompiledScript = _dispatcher.Invoke(() => { using (CreateJsScope()) { try { JsParseScriptAttributes parseAttributes = JsParseScriptAttributes.None; byte[] cachedBytes = JsContext.SerializeScript(code, ref parseAttributes); return(new ChakraCorePrecompiledScript(code, parseAttributes, cachedBytes, uniqueDocumentName)); } catch (OriginalException e) { throw WrapJsException(e, uniqueDocumentName); } } }); return(precompiledScript); }
protected override void InnerExecute(IPrecompiledScript precompiledScript) { throw new NotSupportedException(); }
protected virtual void InnerExecute(IPrecompiledScript precompiledScript) { throw new NotImplementedException(); }
public virtual void ExecutionOfPrecompiledFileIsCorrect() { // Arrange string filePath = Path.GetFullPath(Path.Combine(_baseDirectoryPath, "declinationOfMinutes.js")); const string functionName = "declinationOfMinutes"; const int input0 = 0; const string targetOutput0 = "минут"; const int input1 = 1; const string targetOutput1 = "минута"; const int input2 = 22; const string targetOutput2 = "минуты"; const int input3 = 88; const string targetOutput3 = "минут"; // Act bool supportsScriptPrecompilation = false; IPrecompiledScript precompiledFile = null; string output0 = string.Empty; string output1 = string.Empty; string output2 = string.Empty; string output3 = string.Empty; using (var jsEngine = CreateJsEngine()) { supportsScriptPrecompilation = jsEngine.SupportsScriptPrecompilation; if (supportsScriptPrecompilation) { precompiledFile = jsEngine.PrecompileFile(filePath); jsEngine.Execute(precompiledFile); output0 = jsEngine.CallFunction <string>(functionName, input0); } } if (supportsScriptPrecompilation) { using (var firstJsEngine = CreateJsEngine()) { firstJsEngine.Execute(precompiledFile); output1 = firstJsEngine.CallFunction <string>(functionName, input1); } using (var secondJsEngine = CreateJsEngine()) { secondJsEngine.Execute(precompiledFile); output2 = secondJsEngine.CallFunction <string>(functionName, input2); } using (var thirdJsEngine = CreateJsEngine()) { thirdJsEngine.Execute(precompiledFile); output3 = thirdJsEngine.CallFunction <string>(functionName, input3); } } // Assert if (supportsScriptPrecompilation) { Assert.Equal(targetOutput0, output0); Assert.Equal(targetOutput1, output1); Assert.Equal(targetOutput2, output2); Assert.Equal(targetOutput3, output3); } }
public virtual void ExecutionOfPrecompiledCodeIsCorrect() { // Arrange const string libraryCode = @"function declensionOfNumerals(number, titles) { var result, titleIndex, cases = [2, 0, 1, 1, 1, 2], caseIndex ; if (number % 100 > 4 && number % 100 < 20) { titleIndex = 2; } else { caseIndex = number % 10 < 5 ? number % 10 : 5; titleIndex = cases[caseIndex]; } result = titles[titleIndex]; return result; } function declinationOfSeconds(number) { return declensionOfNumerals(number, ['секунда', 'секунды', 'секунд']); }"; const string functionName = "declinationOfSeconds"; const int itemCount = 4; int[] inputSeconds = new int[itemCount] { 0, 1, 42, 600 }; string[] targetOutputStrings = new string[itemCount] { "секунд", "секунда", "секунды", "секунд" }; string[] outputStrings = new string[itemCount]; // Act bool supportsScriptPrecompilation = false; IPrecompiledScript precompiledCode = null; using (var jsEngine = CreateJsEngine()) { supportsScriptPrecompilation = jsEngine.SupportsScriptPrecompilation; if (supportsScriptPrecompilation) { precompiledCode = jsEngine.Precompile(libraryCode, "declinationOfSeconds.js"); jsEngine.Execute(precompiledCode); outputStrings[0] = jsEngine.CallFunction <string>(functionName, inputSeconds[0]); } } if (supportsScriptPrecompilation) { Parallel.For(1, itemCount, itemIndex => { using (var jsEngine = CreateJsEngine()) { jsEngine.Execute(precompiledCode); outputStrings[itemIndex] = jsEngine.CallFunction <string>(functionName, inputSeconds[itemIndex]); } }); } // Assert if (supportsScriptPrecompilation) { for (int itemIndex = 0; itemIndex < itemCount; itemIndex++) { Assert.Equal(targetOutputStrings[itemIndex], outputStrings[itemIndex]); } } }
public virtual void ExecutionOfPrecompiledResourceByNameAndAssemblyIsCorrect() { // Arrange const string resourceName = "JavaScriptEngineSwitcher.Tests.Resources.declinationOfDays.js"; const string functionName = "declinationOfDays"; const int input0 = 0; const string targetOutput0 = "дней"; const int input1 = 1; const string targetOutput1 = "день"; const int input2 = 3; const string targetOutput2 = "дня"; const int input3 = 80; const string targetOutput3 = "дней"; // Act bool supportsScriptPrecompilation = false; IPrecompiledScript precompiledResource = null; string output0 = string.Empty; string output1 = string.Empty; string output2 = string.Empty; string output3 = string.Empty; using (var jsEngine = CreateJsEngine()) { supportsScriptPrecompilation = jsEngine.SupportsScriptPrecompilation; if (supportsScriptPrecompilation) { precompiledResource = jsEngine.PrecompileResource(resourceName, typeof(PrecompilationTestsBase).GetTypeInfo().Assembly); jsEngine.Execute(precompiledResource); output0 = jsEngine.CallFunction <string>(functionName, input0); } } if (supportsScriptPrecompilation) { using (var firstJsEngine = CreateJsEngine()) { firstJsEngine.Execute(precompiledResource); output1 = firstJsEngine.CallFunction <string>(functionName, input1); } using (var secondJsEngine = CreateJsEngine()) { secondJsEngine.Execute(precompiledResource); output2 = secondJsEngine.CallFunction <string>(functionName, input2); } using (var thirdJsEngine = CreateJsEngine()) { thirdJsEngine.Execute(precompiledResource); output3 = thirdJsEngine.CallFunction <string>(functionName, input3); } } // Assert if (supportsScriptPrecompilation) { Assert.Equal(targetOutput0, output0); Assert.Equal(targetOutput1, output1); Assert.Equal(targetOutput2, output2); Assert.Equal(targetOutput3, output3); } }
/// <summary> /// Executes a pre-compiled script /// </summary> /// <param name="precompiledScript">A pre-compiled script that can be executed by different /// instances of JS engine</param> public virtual void Execute(IPrecompiledScript precompiledScript) { InnerEngine.Execute(precompiledScript); }
public virtual void ExecutionOfPrecompiledResourceByNameAndTypeIsCorrect() { // Arrange const string resourceName = "Resources.declinationOfHours.js"; const string functionName = "declinationOfHours"; const int input0 = 0; const string targetOutput0 = "часов"; const int input1 = 1; const string targetOutput1 = "час"; const int input2 = 24; const string targetOutput2 = "часа"; const int input3 = 48; const string targetOutput3 = "часов"; // Act bool supportsScriptPrecompilation = false; IPrecompiledScript precompiledResource = null; string output0 = string.Empty; string output1 = string.Empty; string output2 = string.Empty; string output3 = string.Empty; using (var jsEngine = CreateJsEngine()) { supportsScriptPrecompilation = jsEngine.SupportsScriptPrecompilation; if (supportsScriptPrecompilation) { precompiledResource = jsEngine.PrecompileResource(resourceName, typeof(PrecompilationTestsBase)); jsEngine.Execute(precompiledResource); output0 = jsEngine.CallFunction <string>(functionName, input0); } } if (supportsScriptPrecompilation) { using (var firstJsEngine = CreateJsEngine()) { firstJsEngine.Execute(precompiledResource); output1 = firstJsEngine.CallFunction <string>(functionName, input1); } using (var secondJsEngine = CreateJsEngine()) { secondJsEngine.Execute(precompiledResource); output2 = secondJsEngine.CallFunction <string>(functionName, input2); } using (var thirdJsEngine = CreateJsEngine()) { thirdJsEngine.Execute(precompiledResource); output3 = thirdJsEngine.CallFunction <string>(functionName, input3); } } // Assert if (supportsScriptPrecompilation) { Assert.Equal(targetOutput0, output0); Assert.Equal(targetOutput1, output1); Assert.Equal(targetOutput2, output2); Assert.Equal(targetOutput3, output3); } }
public virtual void ExecutionOfPrecompiledCodeIsCorrect() { // Arrange const string libraryCode = @"function declensionOfNumerals(number, titles) { var result, titleIndex, cases = [2, 0, 1, 1, 1, 2], caseIndex ; if (number % 100 > 4 && number % 100 < 20) { titleIndex = 2; } else { caseIndex = number % 10 < 5 ? number % 10 : 5; titleIndex = cases[caseIndex]; } result = titles[titleIndex]; return result; } function declinationOfSeconds(number) { return declensionOfNumerals(number, ['секунда', 'секунды', 'секунд']); }"; const string functionName = "declinationOfSeconds"; const int input0 = 0; const string targetOutput0 = "секунд"; const int input1 = 1; const string targetOutput1 = "секунда"; const int input2 = 42; const string targetOutput2 = "секунды"; const int input3 = 600; const string targetOutput3 = "секунд"; // Act bool supportsScriptPrecompilation = false; IPrecompiledScript precompiledCode = null; string output0 = string.Empty; string output1 = string.Empty; string output2 = string.Empty; string output3 = string.Empty; using (var jsEngine = CreateJsEngine()) { supportsScriptPrecompilation = jsEngine.SupportsScriptPrecompilation; if (supportsScriptPrecompilation) { precompiledCode = jsEngine.Precompile(libraryCode, "declinationOfSeconds.js"); jsEngine.Execute(precompiledCode); output0 = jsEngine.CallFunction <string>(functionName, input0); } } if (supportsScriptPrecompilation) { using (var firstJsEngine = CreateJsEngine()) { firstJsEngine.Execute(precompiledCode); output1 = firstJsEngine.CallFunction <string>(functionName, input1); } using (var secondJsEngine = CreateJsEngine()) { secondJsEngine.Execute(precompiledCode); output2 = secondJsEngine.CallFunction <string>(functionName, input2); } using (var thirdJsEngine = CreateJsEngine()) { thirdJsEngine.Execute(precompiledCode); output3 = thirdJsEngine.CallFunction <string>(functionName, input3); } } // Assert if (supportsScriptPrecompilation) { Assert.Equal(targetOutput0, output0); Assert.Equal(targetOutput1, output1); Assert.Equal(targetOutput2, output2); Assert.Equal(targetOutput3, output3); } }
static FormulaManager() { var engineSwitcher = JsEngineSwitcher.Current; engineSwitcher.EngineFactories.Add(new V8JsEngineFactory()); engineSwitcher.DefaultEngineName = V8JsEngine.EngineName; using (var engine = JsEngineSwitcher.Current.CreateDefaultEngine()) { startCode = engine.Precompile(@"function returnDate(date) { return date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate(); } function getDate(arg) { var args = arg.split('-'); return new Date(parseInt(args[0],10),parseInt(args[1],10),parseInt(args[2],10)); } function newDate(year,month,day) { return new Date(year,month - 1,day); } function getYear(date) { return date.getFullYear(); } function getMonth(date) { return date.getMonth() + 1; } function getDay(date) { return date.getDate(); } function equalDate(date1,date2) { return date1.getTime() == date2.getTime(); } function nonEqualDate(date1,date2) { return date1.getTime() != date2.getTime(); } function lessDate(date1,date2) { return date1.getTime() < date2.getTime(); } function greaterDate(date1,date2) { return date1.getTime() > date2.getTime(); } function lessEqualDate(date1,date2) { return date1.getTime() <= date2.getTime(); } function greaterEqualDate(date1,date2) { return date1.getTime() >= date2.getTime(); } "); } }