Beispiel #1
0
        public void CollectGarbage(bool exhaustive = false)
        {
            switch (Type)
            {
            case JsScriptRunnerType.Jint:
            {
                break;          // do nothing, garbage collection not implemeted in Jint
            }

            case JsScriptRunnerType.ClearScriptDebugMode:      // fallback to JsScriptRunnerType.ClearScript
            case JsScriptRunnerType.ClearScript:
            {
                ClearScriptEngine.CollectGarbage(exhaustive);          // see https://microsoft.github.io/ClearScript/Reference/html/M_Microsoft_ClearScript_V8_V8ScriptEngine_CollectGarbage.htm
                break;
            }
            }
        }
Beispiel #2
0
        public void AddHostObject(object hostObject, string name)
        {
            switch (Type)
            {
            case JsScriptRunnerType.Jint:
            {
                JintEngine.SetValue(name, hostObject);          // pass 'scriptingConnector' to Js
                break;
            }

            case JsScriptRunnerType.ClearScriptDebugMode:      // fallback to JsScriptRunnerType.ClearScript
            case JsScriptRunnerType.ClearScript:
            {
                ClearScriptEngine.AddHostObject(name, hostObject);          // pass 'scriptingConnector' to Js
                break;
            }
            }
        }
Beispiel #3
0
        /// <summary>
        /// Call a Js function by string, passing a single string argument to it (the argument can be a Json object parsed inside the function)
        /// </summary>
        public void JsRun(string function, string argument = null)
        {
            if (function is null)
            {
                throw new ArgumentNullException(nameof(function));
            }
            switch (Type)
            {
            case JsScriptRunnerType.Jint:
            {
                try
                {
                    JintEngine.Invoke(function, argument);          // execute the function passing the argument
                    break;
                }
                catch (Esprima.ParserException ex)
                {
                    throw new ApplicationException($"{ex.Error} (Line {ex.LineNumber}, Column {ex.Column})", ex);
                }
                catch (Jint.Runtime.JavaScriptException ex)          // from https://github.com/sebastienros/jint/issues/112
                {
                    throw new ApplicationException($"{ex.Error} (Line {ex.LineNumber}, Column {ex.Column})", ex);
                }
            }

            case JsScriptRunnerType.ClearScriptDebugMode:      // fallback to JsScriptRunnerType.ClearScript
            case JsScriptRunnerType.ClearScript:
            {
                try
                {
                    ClearScriptEngine.Invoke(function, argument);          // execute the function passing the argument
                    break;
                }
                catch (Microsoft.ClearScript.ScriptEngineException ex)          // from https://github.com/microsoft/ClearScript/issues/16
                {
                    throw new ApplicationException($"{ex.ErrorDetails}", ex);
                }
            }
            }
        }
Beispiel #4
0
        /// <summary>
        /// Evaluate a code and return a value
        /// </summary>
        /// <param name="discardFromDebugView">true to discard from Debug View, in Debug mode (used to prevent pollution of executed modules in Visual Studio Code)</param>
        public object Evaluate(string script, bool discardFromDebugView = false)
        {
            if (script is null)
            {
                throw new ArgumentNullException(nameof(script));
            }
            switch (Type)
            {
            case JsScriptRunnerType.Jint:
            {
                try
                {
                    return(JintEngine.Execute(script) // from https://github.com/sebastienros/jint
                           .GetCompletionValue()      // get the latest statement completion value
                           .ToObject());              // converts the value to .NET
                }
                catch (Esprima.ParserException ex)
                {
                    throw new ApplicationException($"{ex.Error} (Line {ex.LineNumber}, Column {ex.Column}), -> {ReadLine(script, ex.LineNumber)}", ex);
                }
                catch (Jint.Runtime.JavaScriptException ex)          // from https://github.com/sebastienros/jint/issues/112
                {
                    throw new ApplicationException($"{ex.Error} (Line {ex.LineNumber}, Column {ex.Column}), -> {ReadLine(script, ex.LineNumber)}", ex);
                }
            }

            case JsScriptRunnerType.ClearScriptDebugMode:
            {
                try
                {
                    if (discardFromDebugView)
                    {
                        return(ClearScriptEngine.Evaluate(string.Empty, true, script));
                    }
                    else
                    {
                        // see https://microsoft.github.io/ClearScript/Reference/html/M_Microsoft_ClearScript_ScriptEngine_Evaluate_2.htm
                        return(ClearScriptEngine.Evaluate(ReadAndNormalizeFirstNonEmptyLineOfAScript(script), false, script));
                    }
                }
                catch (Microsoft.ClearScript.ScriptEngineException ex)          // from https://github.com/microsoft/ClearScript/issues/16
                {
                    throw new ApplicationException($"{ex.ErrorDetails}", ex);
                }
            }

            case JsScriptRunnerType.ClearScript:
            {
                try
                {
                    // see https://microsoft.github.io/ClearScript/Reference/html/M_Microsoft_ClearScript_ScriptEngine_Evaluate_2.htm
                    return(ClearScriptEngine.Evaluate(script));
                }
                catch (Microsoft.ClearScript.ScriptEngineException ex)          // from https://github.com/microsoft/ClearScript/issues/16
                {
                    throw new ApplicationException($"{ex.ErrorDetails}", ex);
                }
            }
            }
            throw new InvalidOperationException("unexpected case");
        }
Beispiel #5
0
        public void Run(string script, bool discardFromDebugView = false)
        {
            if (script is null)
            {
                throw new ArgumentNullException(nameof(script));
            }
            switch (Type)
            {
            case JsScriptRunnerType.Jint:
            {
                try
                {
                    JintEngine.Execute(script);
                    break;
                }
                catch (Esprima.ParserException ex)
                {
                    throw new ApplicationException($"{ex.Error} (Line {ex.LineNumber}, Column {ex.Column}), -> {ReadLine(script, ex.LineNumber)}", ex);
                }
                catch (Jint.Runtime.JavaScriptException ex)          // from https://github.com/sebastienros/jint/issues/112
                {
                    throw new ApplicationException($"{ex.Error} (Line {ex.LineNumber}, Column {ex.Column}), -> {ReadLine(script, ex.LineNumber)}", ex);
                }
            }

            case JsScriptRunnerType.ClearScriptDebugMode:
            {
                try
                {
                    if (discardFromDebugView)
                    {
                        // see https://microsoft.github.io/ClearScript/Reference/html/M_Microsoft_ClearScript_ScriptEngine_Execute_2.htm
                        ClearScriptEngine.Execute(string.Empty, true, script);
                    }
                    else
                    {
                        ClearScriptEngine.Execute(ReadAndNormalizeFirstNonEmptyLineOfAScript(script), false, script);
                    }
                    break;
                }
                catch (Microsoft.ClearScript.ScriptEngineException ex)          // from https://github.com/microsoft/ClearScript/issues/16
                {
                    throw new ApplicationException($"{ex.ErrorDetails}", ex);
                }
            }

            case JsScriptRunnerType.ClearScript:
            {
                try
                {
                    // see https://microsoft.github.io/ClearScript/Reference/html/M_Microsoft_ClearScript_ScriptEngine_Execute_2.htm
                    ClearScriptEngine.Execute(script);
                    break;
                }
                catch (Microsoft.ClearScript.ScriptEngineException ex)          // from https://github.com/microsoft/ClearScript/issues/16
                {
                    throw new ApplicationException($"{ex.ErrorDetails}", ex);
                }
            }
            }
        }