示例#1
0
        private object ExecuteWithScriptArgs(ExpressionScriptProvided expressionScript, ScriptArgs args)
        {
            using (var engine = new Microsoft.ClearScript.Windows.JScriptEngine())
            {
                var primitives             = new ExpandoObject();
                var primitivesAsDictionary = (IDictionary <string, object>)primitives;

                foreach (var binding in args.Bindings)
                {
                    if (IsPrimitive(binding.Value))
                    {
                        primitivesAsDictionary.Add(binding.Key, binding.Value);
                    }
                    else
                    {
                        engine.AddHostObject(binding.Key, binding.Value);
                    }
                }

                ExposeTypesToEngine(engine);

                engine.AddHostObject("__variables", primitives);
                engine.AddHostObject("host", new XHostFunctions());
                engine.AddHostObject("debug", new DebugFunctions(this));

                var writer = new StringWriter();
                WritePolyfills(writer);
                writer.WriteLine("var __result = (function() {");

                foreach (var binding in primitivesAsDictionary)
                {
                    writer.WriteLine("var {0} = __variables.{0};", binding.Key);
                }

                writer.WriteLine(expressionScript.Expression);
                writer.WriteLine("})();");

                engine.Execute(writer.ToString());

                var result = engine.Script.__result;
                if ((result is VoidResult) || (result is Undefined))
                {
                    return(null);
                }

                return(result);
            }
        }
示例#2
0
    //----------------------------------------------------------------------------------------------
    public static IntPtr CreateScope()
    {
        // まだエンジンが作られていなければ
        if (engine == null)
        {
            try
            {
                // Standardモードのコンプライアンスまで引き上げる
                engine = new Microsoft.ClearScript.Windows.JScriptEngine(Microsoft.ClearScript.Windows.WindowsScriptEngineFlags.EnableStandardsMode | Microsoft.ClearScript.Windows.WindowsScriptEngineFlags.EnableDebugging);
                engine.AddHostObject("clr", new HostTypeCollection("mscorlib", "System", "System.Core"));
                engine.AddHostObject("host", new ExtendedHostFunctions());
                engine.AddHostObject("AssemblyPath", new List <String>());

                hm = new Hidemaru();
                engine.AddHostType("hm", typeof(Hidemaru));
                console = new hmJSConsole();
                engine.AddHostType("console", typeof(hmJSConsole));

                // ヒアドキュメント用の関数 R(text)
                String expression = @"
                function R(text){
                    return text.toString().match(/\/\*([\s\S]*)\*\//)[1].toString();
                }
                ";
                engine.Execute(expression);

                dpr = new DllPathResolver();

                return((IntPtr)1);
            }
            catch (Exception e)
            {
                OutputDebugStream(e.Message);
                return((IntPtr)0);
            }
        }
        return((IntPtr)1);
    }
示例#3
0
        public static void StartScript(string script, List<int> identEnts)
        {
            var scriptEngine = new JScriptEngine();
            var collection = new HostTypeCollection(Assembly.LoadFrom("scripthookvdotnet.dll"),
                Assembly.LoadFrom("scripts\\NativeUI.dll"));
            scriptEngine.AddHostObject("API", collection);
            scriptEngine.AddHostObject("host", new HostFunctions());
            scriptEngine.AddHostObject("script", new ScriptContext());
            scriptEngine.AddHostType("Enumerable", typeof(Enumerable));
            scriptEngine.AddHostType("List", typeof(IList));
            scriptEngine.AddHostType("KeyEventArgs", typeof(KeyEventArgs));
            scriptEngine.AddHostType("Keys", typeof(Keys));

            foreach (var obj in identEnts)
            {
                var name = PropStreamer.Identifications[obj];
                if (MapEditor.IsPed(new Prop(obj)))
                    scriptEngine.AddHostObject(name, new Ped(obj));
                else if (MapEditor.IsVehicle(new Prop(obj)))
                    scriptEngine.AddHostObject(name, new Vehicle(obj));
                else if (MapEditor.IsProp(new Prop(obj)))
                    scriptEngine.AddHostObject(name, new Prop(obj));
            }

            try
            {
                scriptEngine.Execute(script);
            }
            catch (ScriptEngineException ex)
            {
                LogException(ex);
            }
            finally
            {
                lock (ScriptEngines)
                    ScriptEngines.Add(scriptEngine);
            }
        }
 private static void VariantClearTestHelper(object x)
 {
     using (var engine = new JScriptEngine())
     {
         engine.AddHostObject("x", x);
         engine.Evaluate("x");
     }
 }
示例#5
0
        private static void ExecuteScript(UserScript script, ScriptHost scriptHost, Logger logger, object[] args)
        {
            try
            {
                //var engine = new JScriptEngine(WindowsScriptEngineFlags.EnableDebugging);
                var engine = new JScriptEngine();
                engine.AddHostObject("host", scriptHost);

                string initArgsScript = string.Format("var arguments = {0};", args.ToJson());
                engine.Execute(initArgsScript);
                engine.Execute(script.Body);
            }
            catch (Exception ex)
            {
                var messge = string.Format("Error in user script {0}", script.Name);
                logger.ErrorException(messge, ex);
            }
        }
        public void VBScriptEngine_ErrorHandling_NestedHostException()
        {
            var innerEngine = new JScriptEngine("inner", WindowsScriptEngineFlags.EnableDebugging);
            innerEngine.AddHostObject("host", new HostFunctions());
            engine.AddHostObject("engine", innerEngine);

            TestUtil.AssertException<ScriptEngineException>(() =>
            {
                try
                {
                    engine.Execute("engine.Evaluate(\"host.newObj(0)\")");
                }
                catch (ScriptEngineException exception)
                {
                    TestUtil.AssertValidException(engine, exception);
                    Assert.IsNotNull(exception.InnerException);

                    var hostException = exception.InnerException;
                    Assert.IsInstanceOfType(hostException, typeof(TargetInvocationException));
                    TestUtil.AssertValidException(hostException);
                    Assert.IsNotNull(hostException.InnerException);

                    var nestedException = hostException.InnerException as ScriptEngineException;
                    Assert.IsNotNull(nestedException);
                    TestUtil.AssertValidException(innerEngine, nestedException);
                    Assert.IsNotNull(nestedException.InnerException);

                    var nestedHostException = nestedException.InnerException;
                    Assert.IsInstanceOfType(nestedHostException, typeof(RuntimeBinderException));
                    TestUtil.AssertValidException(nestedHostException);
                    Assert.IsNull(nestedHostException.InnerException);

                    Assert.AreEqual(nestedHostException.Message, nestedException.Message);
                    Assert.AreEqual(hostException.Message, exception.Message);
                    throw;
                }
            });
        }
        public void JScriptEngine_ErrorHandling_NestedHostException()
        {
            using (var innerEngine = new JScriptEngine("inner", WindowsScriptEngineFlags.EnableDebugging))
            {
                innerEngine.AddHostObject("host", new HostFunctions());
                engine.AddHostObject("engine", innerEngine);

                TestUtil.AssertException<ScriptEngineException>(() =>
                {
                    try
                    {
                        engine.Execute("engine.Evaluate('host.proc(0)')");
                    }
                    catch (ScriptEngineException exception)
                    {
                        TestUtil.AssertValidException(engine, exception);
                        Assert.IsNotNull(exception.InnerException);

                        var hostException = exception.InnerException;
                        Assert.IsInstanceOfType(hostException, typeof(TargetInvocationException));
                        TestUtil.AssertValidException(hostException);
                        Assert.IsNotNull(hostException.InnerException);

                        var nestedException = hostException.InnerException as ScriptEngineException;
                        Assert.IsNotNull(nestedException);
                        // ReSharper disable once AccessToDisposedClosure
                        TestUtil.AssertValidException(innerEngine, nestedException);
                        // ReSharper disable once PossibleNullReferenceException
                        Assert.IsNotNull(nestedException.InnerException);

                        var nestedHostException = nestedException.InnerException;
                        Assert.IsInstanceOfType(nestedHostException, typeof(RuntimeBinderException));
                        TestUtil.AssertValidException(nestedHostException);
                        Assert.IsNull(nestedHostException.InnerException);

                        Assert.AreEqual(nestedHostException.Message, nestedException.Message);
                        Assert.AreEqual(hostException.Message, exception.Message);
                        throw;
                    }
                });
            }
        }