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 = _documentNameManager.GetUniqueName(resourceName);

            lock (_executionSynchronizer)
            {
                try
                {
                    var source = new ResourceScriptSource(uniqueDocumentName, resourceName, assembly);
                    compiledScript = OriginalCompiledScript.Compile(source);
                }
                catch (OriginalSyntaxException e)
                {
                    throw WrapSyntaxException(e);
                }
                catch (NullReferenceException)
                {
                    throw;
                }
            }

            return(new JurassicPrecompiledScript(compiledScript));
        }
        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 = _documentNameManager.GetUniqueName(path);

            lock (_executionSynchronizer)
            {
                try
                {
                    var source = new FileScriptSource(uniqueDocumentName, path, encoding);
                    compiledScript = OriginalCompiledScript.Compile(source);
                }
                catch (OriginalSyntaxException e)
                {
                    throw WrapSyntaxException(e);
                }
                catch (FileNotFoundException)
                {
                    throw;
                }
            }

            return(new JurassicPrecompiledScript(compiledScript));
        }
        protected override IPrecompiledScript InnerPrecompile(string code, string documentName)
        {
            OriginalCompiledScript compiledScript;
            string uniqueDocumentName = _documentNameManager.GetUniqueName(documentName);

            lock (_executionSynchronizer)
            {
                try
                {
                    var source = new OriginalStringScriptSource(code, uniqueDocumentName);
                    compiledScript = OriginalCompiledScript.Compile(source);
                }
                catch (OriginalSyntaxException e)
                {
                    throw WrapSyntaxException(e);
                }
            }

            return(new JurassicPrecompiledScript(compiledScript));
        }
Пример #4
0
        public bool JurassicCompiled(Parameter parameter)
        {
            Jurassic.ScriptEngine engine = new Jurassic.ScriptEngine();

            Jurassic.CompiledScript compiledScript = engine.Compile(new Jurassic.StringScriptSource("n = " + EXPRESSION));

            List <int> results = new List <int>(parameter.Numbers.Length);

            foreach (int number in parameter.Numbers)
            {
                engine.SetGlobalValue("n", number);

                compiledScript.Execute(engine);

                int result = engine.GetGlobalValue <int>("n");

                results.Add(result);
            }

            return(Assert(results, parameter.Sum));
        }
 /// <summary>
 /// Constructs an instance of pre-compiled script
 /// </summary>
 /// <param name="compiledScript">The compiled script</param>
 public JurassicPrecompiledScript(OriginalCompiledScript compiledScript)
 {
     CompiledScript = compiledScript;
 }