public virtual void Execute(string code, string documentName) { VerifyNotDisposed(); if (code == null) { throw new ArgumentNullException( nameof(code), string.Format(Strings.Common_ArgumentIsNull, nameof(code)) ); } if (string.IsNullOrWhiteSpace(code)) { throw new ArgumentException( string.Format(Strings.Common_ArgumentIsEmpty, nameof(code)), nameof(code) ); } if (!string.IsNullOrWhiteSpace(documentName) && !ValidationHelpers.CheckDocumentNameFormat(documentName)) { throw new ArgumentException( string.Format(Strings.Usage_InvalidDocumentNameFormat, documentName), nameof(documentName) ); } InnerExecute(code, documentName); }
public virtual object Evaluate(string expression, string documentName) { VerifyNotDisposed(); if (expression == null) { throw new ArgumentNullException( nameof(expression), string.Format(Strings.Common_ArgumentIsNull, nameof(expression)) ); } if (string.IsNullOrWhiteSpace(expression)) { throw new ArgumentException( string.Format(Strings.Common_ArgumentIsEmpty, nameof(expression)), nameof(expression) ); } if (!string.IsNullOrWhiteSpace(documentName) && !ValidationHelpers.CheckDocumentNameFormat(documentName)) { throw new ArgumentException( string.Format(Strings.Usage_InvalidDocumentNameFormat, documentName), nameof(documentName) ); } return(InnerEvaluate(expression, documentName)); }
public override IPrecompiledScript PrecompileResource(string resourceName, Assembly assembly) { VerifyNotDisposed(); if (resourceName == null) { throw new ArgumentNullException( nameof(resourceName), string.Format(CoreStrings.Common_ArgumentIsNull, nameof(resourceName)) ); } if (assembly == null) { throw new ArgumentNullException( nameof(assembly), string.Format(CoreStrings.Common_ArgumentIsNull, nameof(assembly)) ); } if (string.IsNullOrWhiteSpace(resourceName)) { throw new ArgumentException( string.Format(CoreStrings.Common_ArgumentIsEmpty, nameof(resourceName)), nameof(resourceName) ); } if (!ValidationHelpers.CheckDocumentNameFormat(resourceName)) { throw new ArgumentException( string.Format(CoreStrings.Usage_InvalidResourceNameFormat, resourceName), nameof(resourceName) ); } OriginalCompiledScript compiledScript; string uniqueDocumentName = GetUniqueDocumentName(resourceName, false); lock (_executionSynchronizer) { try { var source = new ResourceScriptSource(uniqueDocumentName, resourceName, assembly); compiledScript = _jsEngine.Compile(source); } catch (OriginalSyntaxException e) { throw WrapSyntaxException(e); } catch (NullReferenceException) { throw; } } return(new JurassicPrecompiledScript(compiledScript)); }
/// <summary> /// Executes a code from embedded JS resource /// </summary> /// <param name="source">JS engine</param> /// <param name="resourceName">The case-sensitive resource name</param> /// <param name="assembly">The assembly, which contains the embedded resource</param> /// <param name="useCache">Flag for whether to use the cache for script code retrieved from /// the embedded resource</param> /// <exception cref="ObjectDisposedException"/> /// <exception cref="ArgumentNullException"/> /// <exception cref="ArgumentException"/> /// <exception cref="NullReferenceException"/> /// <exception cref="JsUsageException"/> /// <exception cref="JsCompilationException"/> /// <exception cref="JsTimeoutException"/> /// <exception cref="JsInterruptedException"/> /// <exception cref="JsRuntimeException"/> /// <exception cref="JsException"/> public static void ExecuteResource(this IJsEngine source, string resourceName, Assembly assembly, bool useCache) { if (useCache) { if (resourceName == null) { throw new ArgumentNullException( nameof(resourceName), string.Format(CoreStrings.Common_ArgumentIsNull, nameof(resourceName)) ); } if (assembly == null) { throw new ArgumentNullException( nameof(assembly), string.Format(CoreStrings.Common_ArgumentIsNull, nameof(assembly)) ); } if (string.IsNullOrWhiteSpace(resourceName)) { throw new ArgumentException( string.Format(CoreStrings.Common_ArgumentIsEmpty, nameof(resourceName)), nameof(resourceName) ); } if (!ValidationHelpers.CheckDocumentNameFormat(resourceName)) { throw new ArgumentException( string.Format(CoreStrings.Usage_InvalidResourceNameFormat, resourceName), nameof(resourceName) ); } if (!_scriptCodeCacheInitialized) { lock (_scriptCodeCacheInitializationSynchronizer) { if (!_scriptCodeCacheInitialized) { _scriptCodeCache = new ConcurrentDictionary <string, string>(); _scriptCodeCacheInitialized = true; } } } string scriptCode = _scriptCodeCache.GetOrAdd(resourceName, key => ReadResourceAsString(key, assembly)); source.Execute(scriptCode, resourceName); } else { source.ExecuteResource(resourceName, assembly); } }
public virtual void ExecuteResource(string resourceName, Type type) { VerifyNotDisposed(); if (resourceName == null) { throw new ArgumentNullException( nameof(resourceName), string.Format(Strings.Common_ArgumentIsNull, nameof(resourceName)) ); } if (type == null) { throw new ArgumentNullException( nameof(type), string.Format(Strings.Common_ArgumentIsNull, nameof(type)) ); } if (string.IsNullOrWhiteSpace(resourceName)) { throw new ArgumentException( string.Format(Strings.Common_ArgumentIsEmpty, nameof(resourceName)), nameof(resourceName) ); } if (!ValidationHelpers.CheckDocumentNameFormat(resourceName)) { throw new ArgumentException( string.Format(Strings.Usage_InvalidResourceNameFormat, resourceName), nameof(resourceName) ); } #if NET40 Assembly assembly = type.Assembly; #else Assembly assembly = type.GetTypeInfo().Assembly; #endif string nameSpace = type.Namespace; string resourceFullName = nameSpace != null ? nameSpace + "." + resourceName : resourceName; string code = Utils.GetResourceAsString(resourceFullName, assembly); if (string.IsNullOrWhiteSpace(code)) { throw new JsUsageException( string.Format(Strings.Usage_CannotExecuteEmptyResource, resourceFullName), Name, Version ); } InnerExecute(code, resourceName); }
public override void ExecuteResource(string resourceName, Assembly assembly) { VerifyNotDisposed(); if (resourceName == null) { throw new ArgumentNullException( nameof(resourceName), string.Format(CoreStrings.Common_ArgumentIsNull, nameof(resourceName)) ); } if (assembly == null) { throw new ArgumentNullException( nameof(assembly), string.Format(CoreStrings.Common_ArgumentIsNull, nameof(assembly)) ); } if (string.IsNullOrWhiteSpace(resourceName)) { throw new ArgumentException( string.Format(CoreStrings.Common_ArgumentIsEmpty, nameof(resourceName)), nameof(resourceName) ); } if (!ValidationHelpers.CheckDocumentNameFormat(resourceName)) { throw new ArgumentException( string.Format(CoreStrings.Usage_InvalidResourceNameFormat, resourceName), nameof(resourceName) ); } string uniqueDocumentName = _documentNameManager.GetUniqueName(resourceName); lock (_executionSynchronizer) { try { var source = new ResourceScriptSource(uniqueDocumentName, resourceName, assembly); _jsEngine.Execute(source); } catch (OriginalJavaScriptException e) { throw WrapJavaScriptException(e); } catch (NullReferenceException) { throw; } } }
public override IPrecompiledScript PrecompileResource(string resourceName, Assembly assembly) { VerifyNotDisposed(); if (resourceName == null) { throw new ArgumentNullException( nameof(resourceName), string.Format(CoreStrings.Common_ArgumentIsNull, nameof(resourceName)) ); } if (assembly == null) { throw new ArgumentNullException( nameof(assembly), string.Format(CoreStrings.Common_ArgumentIsNull, nameof(assembly)) ); } if (string.IsNullOrWhiteSpace(resourceName)) { throw new ArgumentException( string.Format(CoreStrings.Common_ArgumentIsEmpty, nameof(resourceName)), nameof(resourceName) ); } if (!ValidationHelpers.CheckDocumentNameFormat(resourceName)) { throw new ArgumentException( string.Format(CoreStrings.Usage_InvalidResourceNameFormat, resourceName), nameof(resourceName) ); } OriginalPrecompiledScript precompiledScript; try { precompiledScript = _jsEngine.PrecompileResource(resourceName, assembly); } catch (OriginalException e) { throw WrapJsException(e); } catch (NullReferenceException) { throw; } return(new MsiePrecompiledScript(precompiledScript)); }
public override IPrecompiledScript PrecompileFile(string path, Encoding encoding = null) { VerifyNotDisposed(); if (path == null) { throw new ArgumentNullException( nameof(path), string.Format(CoreStrings.Common_ArgumentIsNull, nameof(path)) ); } if (string.IsNullOrWhiteSpace(path)) { throw new ArgumentException( string.Format(CoreStrings.Common_ArgumentIsEmpty, nameof(path)), nameof(path) ); } if (!ValidationHelpers.CheckDocumentNameFormat(path)) { throw new ArgumentException( string.Format(CoreStrings.Usage_InvalidFileNameFormat, path), nameof(path) ); } OriginalCompiledScript compiledScript; string uniqueDocumentName = GetUniqueDocumentName(path, true); lock (_executionSynchronizer) { try { var source = new FileScriptSource(uniqueDocumentName, path, encoding); compiledScript = _jsEngine.Compile(source); } catch (OriginalSyntaxException e) { throw WrapSyntaxException(e); } catch (FileNotFoundException) { throw; } } return(new JurassicPrecompiledScript(compiledScript)); }
public override void ExecuteResource(string resourceName, Assembly assembly) { VerifyNotDisposed(); if (resourceName == null) { throw new ArgumentNullException( nameof(resourceName), string.Format(CoreStrings.Common_ArgumentIsNull, nameof(resourceName)) ); } if (assembly == null) { throw new ArgumentNullException( nameof(assembly), string.Format(CoreStrings.Common_ArgumentIsNull, nameof(assembly)) ); } if (string.IsNullOrWhiteSpace(resourceName)) { throw new ArgumentException( string.Format(CoreStrings.Common_ArgumentIsEmpty, nameof(resourceName)), nameof(resourceName) ); } if (!ValidationHelpers.CheckDocumentNameFormat(resourceName)) { throw new ArgumentException( string.Format(CoreStrings.Usage_InvalidResourceNameFormat, resourceName), nameof(resourceName) ); } try { _jsEngine.ExecuteResource(resourceName, assembly); } catch (OriginalException e) { throw WrapJsException(e); } catch (NullReferenceException) { throw; } }
public virtual void ExecuteResource(string resourceName, Assembly assembly) { VerifyNotDisposed(); if (resourceName == null) { throw new ArgumentNullException( nameof(resourceName), string.Format(Strings.Common_ArgumentIsNull, nameof(resourceName)) ); } if (assembly == null) { throw new ArgumentNullException( nameof(assembly), string.Format(Strings.Common_ArgumentIsNull, nameof(assembly)) ); } if (string.IsNullOrWhiteSpace(resourceName)) { throw new ArgumentException( string.Format(Strings.Common_ArgumentIsEmpty, nameof(resourceName)), nameof(resourceName) ); } if (!ValidationHelpers.CheckDocumentNameFormat(resourceName)) { throw new ArgumentException( string.Format(Strings.Usage_InvalidResourceNameFormat, resourceName), nameof(resourceName) ); } string code = Utils.GetResourceAsString(resourceName, assembly); if (string.IsNullOrWhiteSpace(code)) { throw new JsUsageException( string.Format(Strings.Usage_CannotExecuteEmptyResource, resourceName), Name, Version ); } InnerExecute(code, resourceName); }
public override void ExecuteFile(string path, Encoding encoding = null) { VerifyNotDisposed(); if (path == null) { throw new ArgumentNullException( nameof(path), string.Format(CoreStrings.Common_ArgumentIsNull, nameof(path)) ); } if (string.IsNullOrWhiteSpace(path)) { throw new ArgumentException( string.Format(CoreStrings.Common_ArgumentIsEmpty, nameof(path)), nameof(path) ); } if (!ValidationHelpers.CheckDocumentNameFormat(path)) { throw new ArgumentException( string.Format(CoreStrings.Usage_InvalidFileNameFormat, path), nameof(path) ); } string uniqueDocumentName = _documentNameManager.GetUniqueName(path); lock (_executionSynchronizer) { try { var source = new FileScriptSource(uniqueDocumentName, path, encoding); _jsEngine.Execute(source); } catch (OriginalJavaScriptException e) { throw WrapJavaScriptException(e); } catch (FileNotFoundException) { throw; } } }
public override IPrecompiledScript PrecompileFile(string path, Encoding encoding = null) { VerifyNotDisposed(); if (path == null) { throw new ArgumentNullException( nameof(path), string.Format(CoreStrings.Common_ArgumentIsNull, nameof(path)) ); } if (string.IsNullOrWhiteSpace(path)) { throw new ArgumentException( string.Format(CoreStrings.Common_ArgumentIsEmpty, nameof(path)), nameof(path) ); } if (!ValidationHelpers.CheckDocumentNameFormat(path)) { throw new ArgumentException( string.Format(CoreStrings.Usage_InvalidFileNameFormat, path), nameof(path) ); } OriginalPrecompiledScript precompiledScript; try { precompiledScript = _jsEngine.PrecompileFile(path, encoding); } catch (OriginalException e) { throw WrapJsException(e); } catch (FileNotFoundException) { throw; } return(new MsiePrecompiledScript(precompiledScript)); }
public virtual T Evaluate <T>(string expression, string documentName) { VerifyNotDisposed(); if (expression == null) { throw new ArgumentNullException( nameof(expression), string.Format(Strings.Common_ArgumentIsNull, nameof(expression)) ); } if (string.IsNullOrWhiteSpace(expression)) { throw new ArgumentException( string.Format(Strings.Common_ArgumentIsEmpty, nameof(expression)), nameof(expression) ); } if (!string.IsNullOrWhiteSpace(documentName) && !ValidationHelpers.CheckDocumentNameFormat(documentName)) { throw new ArgumentException( string.Format(Strings.Usage_InvalidDocumentNameFormat, documentName), nameof(documentName) ); } Type returnValueType = typeof(T); if (!ValidationHelpers.IsSupportedType(returnValueType)) { throw new ArgumentException( string.Format(Strings.Usage_ReturnValueTypeNotSupported, returnValueType.FullName), nameof(T) ); } return(InnerEvaluate <T>(expression, documentName)); }
public override void ExecuteFile(string path, Encoding encoding = null) { VerifyNotDisposed(); if (path == null) { throw new ArgumentNullException( nameof(path), string.Format(CoreStrings.Common_ArgumentIsNull, nameof(path)) ); } if (string.IsNullOrWhiteSpace(path)) { throw new ArgumentException( string.Format(CoreStrings.Common_ArgumentIsEmpty, nameof(path)), nameof(path) ); } if (!ValidationHelpers.CheckDocumentNameFormat(path)) { throw new ArgumentException( string.Format(CoreStrings.Usage_InvalidFileNameFormat, path), nameof(path) ); } try { _jsEngine.ExecuteFile(path, encoding); } catch (OriginalException e) { throw WrapJsException(e); } catch (FileNotFoundException) { throw; } }
public void DocumentNameFormatIsWrong() { // Arrange // Act bool documentName1FormatIsWrong = ValidationHelpers.CheckDocumentNameFormat("Script Document"); bool documentName2FormatIsWrong = ValidationHelpers.CheckDocumentNameFormat("Script Document <2>"); bool documentName3FormatIsWrong = ValidationHelpers.CheckDocumentNameFormat(" doc01.js"); bool documentName4FormatIsWrong = ValidationHelpers.CheckDocumentNameFormat(@"Document ""Test"""); bool documentName5FormatIsWrong = ValidationHelpers.CheckDocumentNameFormat("src/*.js"); bool documentName6FormatIsWrong = ValidationHelpers.CheckDocumentNameFormat( "/js/shared/SubScribeModal/subscribeChecker.js?v=2017-11-09"); // Assert Assert.False(documentName1FormatIsWrong); Assert.False(documentName2FormatIsWrong); Assert.False(documentName3FormatIsWrong); Assert.False(documentName4FormatIsWrong); Assert.False(documentName5FormatIsWrong); Assert.False(documentName6FormatIsWrong); }
public virtual void ExecuteFile(string path, Encoding encoding = null) { VerifyNotDisposed(); if (path == null) { throw new ArgumentNullException( nameof(path), string.Format(Strings.Common_ArgumentIsNull, nameof(path)) ); } if (string.IsNullOrWhiteSpace(path)) { throw new ArgumentException( string.Format(Strings.Common_ArgumentIsEmpty, nameof(path)), nameof(path) ); } if (!ValidationHelpers.CheckDocumentNameFormat(path)) { throw new ArgumentException( string.Format(Strings.Usage_InvalidFileNameFormat, path), nameof(path) ); } string code = Utils.GetFileTextContent(path, encoding); if (string.IsNullOrWhiteSpace(code)) { throw new JsUsageException( string.Format(Strings.Usage_CannotExecuteEmptyFile, path), Name, Version ); } InnerExecute(code, path); }
public void DocumentNameFormatIsCorrect() { // Arrange // Act bool documentName1FormatIsCorrect = ValidationHelpers.CheckDocumentNameFormat("Script Document"); bool documentName2FormatIsCorrect = ValidationHelpers.CheckDocumentNameFormat("Script Document [2]"); bool documentName3FormatIsCorrect = ValidationHelpers.CheckDocumentNameFormat("doc01.js"); bool documentName4FormatIsCorrect = ValidationHelpers.CheckDocumentNameFormat("/res/scripts.min.js"); bool documentName5FormatIsCorrect = ValidationHelpers.CheckDocumentNameFormat( @"C:\Users\Vasya\AppData\Roaming\npm\node_modules\typescript\lib\tsc.js"); bool documentName6FormatIsCorrect = ValidationHelpers.CheckDocumentNameFormat( "BundleTransformer.Less.Resources.less-combined.min.js"); // Assert Assert.True(documentName1FormatIsCorrect); Assert.True(documentName2FormatIsCorrect); Assert.True(documentName3FormatIsCorrect); Assert.True(documentName4FormatIsCorrect); Assert.True(documentName5FormatIsCorrect); Assert.True(documentName6FormatIsCorrect); }
public override void ExecuteResource(string resourceName, Type type) { VerifyNotDisposed(); if (resourceName == null) { throw new ArgumentNullException( nameof(resourceName), string.Format(CoreStrings.Common_ArgumentIsNull, nameof(resourceName)) ); } if (type == null) { throw new ArgumentNullException( nameof(type), string.Format(CoreStrings.Common_ArgumentIsNull, nameof(type)) ); } if (string.IsNullOrWhiteSpace(resourceName)) { throw new ArgumentException( string.Format(CoreStrings.Common_ArgumentIsEmpty, nameof(resourceName)), nameof(resourceName) ); } if (!ValidationHelpers.CheckDocumentNameFormat(resourceName)) { throw new ArgumentException( string.Format(CoreStrings.Usage_InvalidResourceNameFormat, resourceName), nameof(resourceName) ); } #if NET40 Assembly assembly = type.Assembly; #else Assembly assembly = type.GetTypeInfo().Assembly; #endif string nameSpace = type.Namespace; string resourceFullName = nameSpace != null ? nameSpace + "." + resourceName : resourceName; string uniqueDocumentName = GetUniqueDocumentName(resourceFullName, false); lock (_executionSynchronizer) { try { var source = new ResourceScriptSource(uniqueDocumentName, resourceFullName, assembly); _jsEngine.Execute(source); } catch (OriginalJavaScriptException e) { throw WrapJavaScriptException(e); } catch (NullReferenceException) { throw; } } }
public override IPrecompiledScript PrecompileResource(string resourceName, Type type) { VerifyNotDisposed(); if (resourceName == null) { throw new ArgumentNullException( nameof(resourceName), string.Format(CoreStrings.Common_ArgumentIsNull, nameof(resourceName)) ); } if (type == null) { throw new ArgumentNullException( nameof(type), string.Format(CoreStrings.Common_ArgumentIsNull, nameof(type)) ); } if (string.IsNullOrWhiteSpace(resourceName)) { throw new ArgumentException( string.Format(CoreStrings.Common_ArgumentIsEmpty, nameof(resourceName)), nameof(resourceName) ); } if (!ValidationHelpers.CheckDocumentNameFormat(resourceName)) { throw new ArgumentException( string.Format(CoreStrings.Usage_InvalidResourceNameFormat, resourceName), nameof(resourceName) ); } #if NET40 Assembly assembly = type.Assembly; #else Assembly assembly = type.GetTypeInfo().Assembly; #endif string nameSpace = type.Namespace; string resourceFullName = nameSpace != null ? nameSpace + "." + resourceName : resourceName; OriginalCompiledScript compiledScript; string uniqueDocumentName = _documentNameManager.GetUniqueName(resourceFullName); lock (_executionSynchronizer) { try { var source = new ResourceScriptSource(uniqueDocumentName, resourceFullName, assembly); compiledScript = OriginalCompiledScript.Compile(source); } catch (OriginalSyntaxException e) { throw WrapSyntaxException(e); } catch (NullReferenceException) { throw; } } return(new JurassicPrecompiledScript(compiledScript)); }