public static IntPtr DestroyScope() { if (engine != null) { // FreeLibrary直前の関数の呼び出し。 try { if (engine.Script["DestroyScope"] != null) { DoString("DestroyScope()", "DestroyScope"); } } catch (Exception) { } engine = null; } SetCodePage((IntPtr)default_codepage); iDllBindHandle = 0; tmpVar = null; dpr = null; return((IntPtr)0); }
private Plugin() { _engine = new JScriptEngine(WindowsScriptEngineFlags.EnableJITDebugging); _engine.Add("host", new ExtendedHostFunctions()); Icons = new Dictionary<string, Image>(); ItemTemplates = new List<ItemTemplate>(); ProjectTemplates = new List<ProjectTemplate>(); Highlightings = new Dictionary<string, string>(); }
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); } }
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); } }
//---------------------------------------------------------------------------------------------- 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); }
public void JScriptEngine_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 DynamicHostItem_CrossEngine() { const string format = "{0} foo {1} bar {2} baz {3} qux {4} quux {5}"; var args = new object[] { 1, 2, 3, 4, 5, 6 }; engine.Script.mscorlib = new HostTypeCollection("mscorlib"); using (var outerEngine = new JScriptEngine()) { outerEngine.Script.inner = engine; outerEngine.Script.format = format; outerEngine.Script.args = args; Assert.AreEqual(string.Format(format, args), outerEngine.Evaluate("inner.Script.mscorlib.System.String.Format(format, args)")); Assert.AreEqual(string.Format(format, args), outerEngine.Evaluate("inner.Script.mscorlib['System'].String['Format'](format, args)")); } }
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_NestedScriptError() { var innerEngine = new JScriptEngine("inner", WindowsScriptEngineFlags.EnableDebugging); engine.AddHostObject("engine", innerEngine); TestUtil.AssertException<ScriptEngineException>(() => { try { engine.Execute("engine.Execute(\"foo = {}; foo();\")"); } 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.IsNull(nestedException.InnerException); Assert.AreEqual(hostException.Message, exception.Message); throw; } }); }
public void PropertyBag_MultiEngine_Parallel() { // This is a torture test for ConcurrentWeakSet and general engine teardown/cleanup. // It has exposed some very tricky engine bugs. var bag = new PropertyBag(); engine.AddHostObject("bag", bag); const int threadCount = 256; var engineCount = 0; // 32-bit V8 starts failing requests to create new contexts rather quickly. This is // because each V8 isolate requires (among other things) a 32MB address space // reservation. 64-bit V8 reserves much larger blocks but benefits from the enormous // available address space. var maxV8Count = Environment.Is64BitProcess ? 128 : 16; var maxJScriptCount = (threadCount - maxV8Count) / 2; var startEvent = new ManualResetEventSlim(false); var checkpointEvent = new ManualResetEventSlim(false); var continueEvent = new ManualResetEventSlim(false); var stopEvent = new ManualResetEventSlim(false); ParameterizedThreadStart body = arg => { // ReSharper disable AccessToDisposedClosure var index = (int)arg; startEvent.Wait(); ScriptEngine scriptEngine; if (index < maxV8Count) { scriptEngine = new V8ScriptEngine(); } else if (index < (maxV8Count + maxJScriptCount)) { scriptEngine = new JScriptEngine(); } else { scriptEngine = new VBScriptEngine(); } scriptEngine.AddHostObject("bag", bag); if (Interlocked.Increment(ref engineCount) == threadCount) { checkpointEvent.Set(); } continueEvent.Wait(); scriptEngine.Dispose(); if (Interlocked.Decrement(ref engineCount) == 0) { stopEvent.Set(); } // ReSharper restore AccessToDisposedClosure }; var threads = Enumerable.Range(0, threadCount).Select(index => new Thread(body)).ToArray(); threads.ForEach((thread, index) => thread.Start(index)); startEvent.Set(); checkpointEvent.Wait(); Assert.AreEqual(threadCount + 1, bag.EngineCount); continueEvent.Set(); stopEvent.Wait(); GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced); Assert.AreEqual(1, bag.EngineCount); Array.ForEach(threads, thread => thread.Join()); startEvent.Dispose(); checkpointEvent.Dispose(); continueEvent.Dispose(); stopEvent.Dispose(); }
public void JScriptEngine_Current() { // ReSharper disable AccessToDisposedClosure using (var innerEngine = new JScriptEngine()) { engine.Script.test = new Action(() => { innerEngine.Script.test = new Action(() => Assert.AreSame(innerEngine, ScriptEngine.Current)); Assert.AreSame(engine, ScriptEngine.Current); innerEngine.Execute("test()"); innerEngine.Script.test(); Assert.AreSame(engine, ScriptEngine.Current); }); Assert.IsNull(ScriptEngine.Current); engine.Execute("test()"); engine.Script.test(); Assert.IsNull(ScriptEngine.Current); } // ReSharper restore AccessToDisposedClosure }
public void TestInitialize() { engine = new JScriptEngine(WindowsScriptEngineFlags.EnableDebugging); }
public void JScriptEngine_StandardsMode() { engine.Dispose(); engine = new JScriptEngine(WindowsScriptEngineFlags.EnableDebugging | WindowsScriptEngineFlags.EnableStandardsMode); Assert.AreEqual("{\"foo\":123,\"bar\":456.789}", engine.Evaluate("JSON.stringify({ foo: 123, bar: 456.789 })")); }
public void JScriptEngine_MarshalNullAsDispatch() { engine.Script.func = new Func<object>(() => null); Assert.IsTrue((bool)engine.Evaluate("func() === null")); engine.Dispose(); engine = new JScriptEngine(WindowsScriptEngineFlags.EnableDebugging | WindowsScriptEngineFlags.MarshalNullAsDispatch); engine.Script.func = new Func<object>(() => null); Assert.IsTrue((bool)engine.Evaluate("func() === null")); engine.Script.func = new Func<string>(() => null); Assert.IsTrue((bool)engine.Evaluate("func() === null")); engine.Script.func = new Func<bool?>(() => null); Assert.IsTrue((bool)engine.Evaluate("func() === null")); engine.Script.func = new Func<char?>(() => null); Assert.IsTrue((bool)engine.Evaluate("func() === null")); engine.Script.func = new Func<sbyte?>(() => null); Assert.IsTrue((bool)engine.Evaluate("func() === null")); engine.Script.func = new Func<byte?>(() => null); Assert.IsTrue((bool)engine.Evaluate("func() === null")); engine.Script.func = new Func<short?>(() => null); Assert.IsTrue((bool)engine.Evaluate("func() === null")); engine.Script.func = new Func<ushort?>(() => null); Assert.IsTrue((bool)engine.Evaluate("func() === null")); engine.Script.func = new Func<int?>(() => null); Assert.IsTrue((bool)engine.Evaluate("func() === null")); engine.Script.func = new Func<uint?>(() => null); Assert.IsTrue((bool)engine.Evaluate("func() === null")); engine.Script.func = new Func<long?>(() => null); Assert.IsTrue((bool)engine.Evaluate("func() === null")); engine.Script.func = new Func<ulong?>(() => null); Assert.IsTrue((bool)engine.Evaluate("func() === null")); engine.Script.func = new Func<float?>(() => null); Assert.IsTrue((bool)engine.Evaluate("func() === null")); engine.Script.func = new Func<double?>(() => null); Assert.IsTrue((bool)engine.Evaluate("func() === null")); engine.Script.func = new Func<decimal?>(() => null); Assert.IsTrue((bool)engine.Evaluate("func() === null")); engine.Script.func = new Func<Random>(() => null); Assert.IsTrue((bool)engine.Evaluate("func() === null")); engine.Script.func = new Func<DayOfWeek?>(() => null); Assert.IsTrue((bool)engine.Evaluate("func() === null")); }
public void JScriptEngine_MarshalDecimalAsCurrency() { engine.Script.func = new Func<object>(() => 123.456M); Assert.AreEqual("number", engine.Evaluate("typeof(func())")); Assert.AreEqual(123.456 + 5, engine.Evaluate("func() + 5")); engine.Dispose(); engine = new JScriptEngine(WindowsScriptEngineFlags.EnableDebugging | WindowsScriptEngineFlags.MarshalDecimalAsCurrency); engine.Script.func = new Func<object>(() => 123.456M); Assert.AreEqual("number", engine.Evaluate("typeof(func())")); Assert.AreEqual(123.456 + 5, engine.Evaluate("func() + 5")); }
public void JScriptEngine_MarshalArraysByValue_invokeMethod() { var args = new[] { Math.PI, Math.E }; engine.Script.args = args; engine.Execute("function foo(a, b) { return a * b; }"); Assert.AreEqual(Math.PI * Math.E, engine.Evaluate("EngineInternal.invokeMethod(null, foo, args)")); engine.Dispose(); engine = new JScriptEngine(WindowsScriptEngineFlags.EnableDebugging | WindowsScriptEngineFlags.MarshalArraysByValue); engine.Script.args = args; engine.Execute("function foo(a, b) { return a * b; }"); Assert.AreEqual(Math.PI * Math.E, engine.Evaluate("EngineInternal.invokeMethod(null, foo, args)")); }
public void JScriptEngine_MarshalArraysByValue() { var foo = new[] { DayOfWeek.Saturday, DayOfWeek.Friday, DayOfWeek.Thursday }; engine.Script.foo = foo; Assert.AreSame(foo, engine.Evaluate("foo")); engine.Dispose(); engine = new JScriptEngine(WindowsScriptEngineFlags.EnableDebugging | WindowsScriptEngineFlags.MarshalArraysByValue); engine.Script.foo = foo; Assert.AreNotSame(foo, engine.Evaluate("foo")); engine.Execute("foo = new VBArray(foo)"); Assert.AreEqual(foo.GetUpperBound(0), engine.Evaluate("foo.ubound(1)")); for (var index = 0; index < foo.Length; index++) { Assert.AreEqual(foo[index], engine.Evaluate("foo.getItem(" + index + ")")); } }
public void JScriptEngine_ErrorHandling_NestedScriptError() { using (var innerEngine = new JScriptEngine("inner", WindowsScriptEngineFlags.EnableDebugging)) { engine.AddHostObject("engine", innerEngine); TestUtil.AssertException<ScriptEngineException>(() => { try { engine.Execute("engine.Execute('foo = {}; foo();')"); } 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.IsNull(nestedException.InnerException); Assert.AreEqual(hostException.Message, exception.Message); throw; } }); } }
private static void VariantClearTestHelper(object x) { using (var engine = new JScriptEngine()) { engine.AddHostObject("x", x); engine.Evaluate("x"); } }
public void JScriptEngine_StandardsMode() { // ReSharper disable AccessToDisposedClosure TestUtil.AssertException<ScriptEngineException>(() => engine.Evaluate("JSON")); engine.Dispose(); engine = new JScriptEngine(WindowsScriptEngineFlags.EnableDebugging | WindowsScriptEngineFlags.EnableStandardsMode); Assert.AreEqual("{\"foo\":123,\"bar\":456.789}", engine.Evaluate("JSON.stringify({ foo: 123, bar: 456.789 })")); // ReSharper restore AccessToDisposedClosure }
public void JScriptEngine_AddCOMType_XMLHTTP() { int status = 0; string data = null; var thread = new Thread(() => { using (var testEngine = new JScriptEngine(WindowsScriptEngineFlags.EnableDebugging | WindowsScriptEngineFlags.EnableStandardsMode)) { testEngine.Script.onComplete = new Action<int, string>((xhrStatus, xhrData) => { status = xhrStatus; data = xhrData; Dispatcher.ExitAllFrames(); }); Dispatcher.CurrentDispatcher.BeginInvoke(new Action(() => { // ReSharper disable AccessToDisposedClosure testEngine.AddCOMType("XMLHttpRequest", "MSXML2.XMLHTTP"); testEngine.Execute(@" xhr = new XMLHttpRequest(); xhr.open('POST', 'http://httpbin.org/post', true); xhr.onreadystatechange = function() { if (xhr.readyState == 4) { onComplete(xhr.status, JSON.parse(xhr.responseText).data); } }; xhr.send('Hello, world!'); "); // ReSharper restore AccessToDisposedClosure })); Dispatcher.Run(); } }); thread.SetApartmentState(ApartmentState.STA); thread.Start(); thread.Join(); Assert.AreEqual(200, status); Assert.AreEqual("Hello, world!", data); }