コード例 #1
0
 public void TestInitialize()
 {
     engine = new V8ScriptEngine(V8ScriptEngineFlags.EnableDebugging);
     engine.AddHostObject("host", new ExtendedHostFunctions());
     engine.AddHostObject("mscorlib", HostItemFlags.GlobalMembers, new HostTypeCollection("mscorlib"));
     engine.AddHostObject("ClearScriptTest", HostItemFlags.GlobalMembers, new HostTypeCollection("ClearScriptTest").GetNamespaceNode("Microsoft.ClearScript.Test"));
 }
コード例 #2
0
 public void TestInitialize()
 {
     engine = new V8ScriptEngine(V8ScriptEngineFlags.EnableDebugging);
     engine.AddHostObject("testObject", testInterface = testObject = new TestObject());
     engine.AddHostObject("testInterface", HostItem.Wrap(engine, testObject, typeof(ITestInterface)));
     engine.AddHostType("Guid", typeof(Guid));
 }
コード例 #3
0
 public void TestInitialize()
 {
     engine = new V8ScriptEngine(V8ScriptEngineFlags.EnableDebugging);
     engine.DisableCaseInsensitivePropertyLookups     = true;
     engine.AddHostObject("testObject", testInterface = testObject = new TestObject());
     engine.AddHostObject("testInterface", HostItem.Wrap(engine, testObject, typeof(ITestInterface)));
     engine.AddHostType("Guid", typeof(Guid));
 }
コード例 #4
0
 public void TestInitialize()
 {
     engine = new V8ScriptEngine(V8ScriptEngineFlags.EnableDebugging);
     engine.AddHostObject("host", new ExtendedHostFunctions());
     engine.AddHostObject("mscorlib", HostItemFlags.GlobalMembers, new HostTypeCollection("mscorlib"));
     engine.AddHostObject("ClearScriptTest", HostItemFlags.GlobalMembers, new HostTypeCollection("ClearScriptTest").GetNamespaceNode("Microsoft.ClearScript.Test"));
     engine.AddHostObject("testObject", testInterface = testObject = new TestObject());
     engine.Execute("var testInterface = host.cast(IExplicitBaseTestInterface, testObject)");
 }
コード例 #5
0
        public void Import(string objName, string objType, string objNameSpace)
        {
            var type = Type.GetType(objNameSpace + '.' + objType);
            var obj  = Activator.CreateInstance(type);

            engine.AddHostObject(objName, obj);
        }
コード例 #6
0
ファイル: SpecterJS.cs プロジェクト: imgits/specterjs
 public Specter(Options options)
 {
     Options = options;
     engine  = new V8ScriptEngine();
     engine.AddHostObject("phantom", new Phantom(this));
     engine.AddHostType("Timer", typeof(Bindings.Timer));
 }
コード例 #7
0
 public void HostFunctions_toDecimal_V8()
 {
     engine.Dispose();
     engine = new V8ScriptEngine();
     engine.AddHostObject("host", new HostFunctions());
     Assert.AreEqual(Convert.ToDecimal(127), engine.Evaluate("host.toDecimal(127)"));
 }
コード例 #8
0
        private void frmEvents_Load(object sender, EventArgs e)
        {
            var oFrmTestForm = new frmTestForm();

            engine = ClearScript.Create(EngineType.V8);
            engine.AddHostObject("form", this);
            engine.AddHostType("Form2", typeof(frmTestForm));
            engine.AddHostType("alert", typeof(AlertBox));
            engine.AddHostType("msgBox", typeof(MessageBox));

            engine.Execute(@"
                
                form.btnForm.Click.connect((s,e) => {
                 form.btnForm.Text = 'Clicked';
                 let form2 = new Form2();
                 form2.label1.Text = 'Opened from an event connected to ClearScript';
                 form2.Show();
            });
            ");

            engine.Execute(@"
                form.txtKeyPress.KeyPress.connect((s,e) => {
                 form.lblKeyPressResult.Text = 'Key pressed: '+e.KeyChar;
            });
            ");


            engine.Execute(@"
                form.btnAlert.Click.connect((s,e) => {
                 alert.Show('Hello from ClearScript');
            });
            ");


            if (!oFrmTestForm.IsDisposed)
            {
                engine.Execute(@"
                form.btnMsgBox.Click.connect((s,e) => {
                 msgBox.Show('Hello, I came from ClearScript :D');
            });
            ");
            }
            else
            {
                engine.AddHostObject("form2", oFrmTestForm);
            }
        }
コード例 #9
0
 public void Init()
 {
     _eng = ScriptFactory.GetScriptEngine(this.ScriptingLanguage);
     for (int i = 0; i < _source.FieldCount; i++)
     {
         _fields.Add(_source.GetName(i));
     }
     _eng.AddHostObject("Record", _rec);
     _eng.AddHostType("Console", typeof(Console));
 }
コード例 #10
0
        public void Install(IProjectSource source, ScriptEngine engine)
        {
            if (source is IScript == false)
            {
                return;
            }

            var console = new BrowserConsole((IScript) source);

            engine.AddHostObject(nameof(console), console);
        }
コード例 #11
0
        public void Install(IProjectSource source, ScriptEngine engine)
        {
            if (source is IScript == false)
            {
                return;
            }

            var console = new BrowserConsole((IScript)source);

            engine.AddHostObject(nameof(console), console);
        }
コード例 #12
0
        public ITestCollection Reflect(IScript script, ScriptEngine engine)
        {
            var testCollection = _componentContext.Resolve<ITestCollection>(new TypedParameter(typeof(IScript), script));

            engine.AddHostObject(nameof(testCollection), testCollection);

            engine.Execute(
                @"function describe(description, callback) {
                        testCollection.AddSuite(description, callback.toString());
                        callback();
                      }");

            engine.Execute(
                @"function it(description, callback) {
                        testCollection.AddTest(description, callback.toString());
                      }");

            return testCollection;
        }
コード例 #13
0
        public ITestCollection Reflect(IScript script, ScriptEngine engine)
        {
            var testCollection = _componentContext.Resolve <ITestCollection>(new TypedParameter(typeof(IScript), script));

            engine.AddHostObject(nameof(testCollection), testCollection);

            engine.Execute(
                @"function describe(description, callback) {
                        testCollection.AddSuite(description, callback.toString());
                        callback();
                      }");

            engine.Execute(
                @"function it(description, callback) {
                        testCollection.AddTest(description, callback.toString());
                      }");

            return(testCollection);
        }
コード例 #14
0
        public static ScriptEngine ApplyOptions(this ScriptEngine engine, ExecutionOptions options)
        {
            if (options != null)
            {
                if (options.HostObjects != null)
                {
                    foreach (HostObject hostObject in options.HostObjects)
                    {
                        engine.AddHostObject(hostObject);
                    }
                }

                if (options.HostTypes != null)
                {
                    foreach (HostType hostType in options.HostTypes)
                    {
                        engine.AddHostType(hostType);
                    }
                }
            }

            return(engine);
        }
コード例 #15
0
        public static void RunSuite(ScriptEngine engine)
        {
            // download raw test code if necessary
            if (!gotCode)
            {
                Console.Write("Downloading code... ");
                testPrefix   = DownloadFileAsString("sunspider-test-prefix.js");
                testContents = DownloadFileAsString("sunspider-test-contents.js");
                Console.WriteLine("Done");
                gotCode = true;
            }

            // set up dummy HTML DOM
            var mockDOM = new MockDOM();

            engine.AccessContext = typeof(SunSpider);
            engine.AddHostObject("document", mockDOM);
            engine.AddHostObject("window", mockDOM);
            engine.AddHostObject("parent", mockDOM);

            // load raw test code
            engine.Execute(testPrefix);
            engine.Execute(testContents);
            engine.Execute(@"
                function ClearScriptCleanup() {
                    delete Array.prototype.toJSONString;
                    delete Boolean.prototype.toJSONString;
                    delete Date.prototype.toJSONString;
                    delete Number.prototype.toJSONString;
                    delete Object.prototype.toJSONString;
                }
            ");

            // initialize
            var testCount     = (int)engine.Script.tests.length;
            var testIndices   = Enumerable.Range(0, testCount).ToList();
            var repeatIndices = Enumerable.Range(0, repeatCount).ToList();

            // run warmup cycle
            Console.Write("Warming up... ");
            testIndices.ForEach(testIndex => RunTest(engine, mockDOM, testIndex));
            Console.WriteLine("Done");

            // run main test
            var results = repeatIndices.Select(index => new Dictionary <string, int>()).ToArray();

            repeatIndices.ForEach(repeatIndex =>
            {
                Console.Write("Running iteration {0}... ", repeatIndex + 1);
                testIndices.ForEach(testIndex =>
                {
                    var name = (string)engine.Script.tests[testIndex];
                    results[repeatIndex][name] = RunTest(engine, mockDOM, testIndex);
                });
                Console.WriteLine("Done");
            });

            // show results
            var resultString = new StringBuilder("{\"v\":\"" + version + "\",");

            results[0].Keys.ToList().ForEach(name =>
            {
                resultString.Append("\"" + name + "\":[");
                resultString.Append(string.Join(",", repeatIndices.Select(repeatIndex => results[repeatIndex][name])));
                resultString.Append("],");
            });
            resultString.Length -= 1;
            resultString.Append("}");
            Process.Start((new Uri(baseUrl + "results.html?" + resultString)).AbsoluteUri);
        }
コード例 #16
0
 public void TestInitialize()
 {
     engine = new JScriptEngine(WindowsScriptEngineFlags.EnableDebugging);
     engine.AddHostObject("host", host = new HostFunctions());
 }
コード例 #17
0
ファイル: SunSpider.cs プロジェクト: dirkelfman/ClearScript
        public static void RunSuite(ScriptEngine engine)
        {
            // download raw test code if necessary
            if (!gotCode)
            {
                Console.Write("Downloading code... ");
                testPrefix = DownloadFileAsString("sunspider-test-prefix.js");
                testContents = DownloadFileAsString("sunspider-test-contents.js");
                Console.WriteLine("Done");
                gotCode = true;
            }

            // set up dummy HTML DOM
            var mockDOM = new MockDOM();
            engine.AccessContext = typeof(SunSpider);
            engine.AddHostObject("document", mockDOM);
            engine.AddHostObject("window", mockDOM);
            engine.AddHostObject("parent", mockDOM);

            // load raw test code
            engine.Execute(testPrefix);
            engine.Execute(testContents);
            engine.Execute(@"
                function ClearScriptCleanup() {
                    delete Array.prototype.toJSONString;
                    delete Boolean.prototype.toJSONString;
                    delete Date.prototype.toJSONString;
                    delete Number.prototype.toJSONString;
                    delete Object.prototype.toJSONString;
                }
            ");

            // initialize
            var testCount = (int)engine.Script.tests.length;
            var testIndices = Enumerable.Range(0, testCount).ToList();
            var repeatIndices = Enumerable.Range(0, repeatCount).ToList();

            // run warmup cycle
            Console.Write("Warming up... ");
            testIndices.ForEach(testIndex => RunTest(engine, mockDOM, testIndex));
            Console.WriteLine("Done");

            // run main test
            var results = repeatIndices.Select(index => new Dictionary<string, int>()).ToArray();
            repeatIndices.ForEach(repeatIndex =>
            {
                Console.Write("Running iteration {0}... ", repeatIndex + 1);
                testIndices.ForEach(testIndex =>
                {
                    var name = (string)engine.Script.tests[testIndex];
                    results[repeatIndex][name] = RunTest(engine, mockDOM, testIndex);
                });
                Console.WriteLine("Done");
            });

            // show results
            var resultString = new StringBuilder("{\"v\":\"" + version + "\",");
            results[0].Keys.ToList().ForEach(name =>
            {
                resultString.Append("\"" + name + "\":[");
                resultString.Append(string.Join(",", repeatIndices.Select(repeatIndex => results[repeatIndex][name])));
                resultString.Append("],");
            });
            resultString.Length -= 1;
            resultString.Append("}");
            Process.Start((new Uri(baseUrl + "results.html?" + resultString)).AbsoluteUri);
        }
コード例 #18
0
        public void PropertyBag_Property()
        {
            var host = new HostFunctions();
            var bag  = new PropertyBag {
                { "host", host }
            };

            engine.AddHostObject("bag", bag);
            Assert.AreSame(host, engine.Evaluate("bag.host"));
        }
コード例 #19
0
        static void initJsEngine(ScriptEngine eng)
        {
            eng.AddHostObject("log", cs_log);
            eng.AddHostObject("fileReadAllText", cs_fileReadAllText);
            eng.AddHostObject("fileWriteAllText", cs_fileWriteAllText);
            eng.AddHostObject("fileWriteLine", cs_fileWriteLine);
            eng.AddHostObject("TimeNow", cs_TimeNow);
            eng.AddHostObject("setShareData", cs_setShareData);
            eng.AddHostObject("getShareData", cs_getShareData);
            eng.AddHostObject("removeShareData", cs_removeShareData);
            eng.AddHostObject("request", cs_request);
            eng.AddHostObject("setTimeout", cs_setTimeout);
            eng.AddHostObject("mkdir", cs_mkdir);
            eng.AddHostObject("getWorkingPath", cs_getWorkingPath);
            eng.AddHostObject("startLocalHttpListen", cs_startLocalHttpListen);
            eng.AddHostObject("resetLocalHttpListener", cs_resetLocalHttpListener);
            eng.AddHostObject("stopLocalHttpListen", cs_stopLocalHttpListen);

            eng.AddHostObject("addBeforeActListener", cs_addBeforeActListener);
            eng.AddHostObject("removeBeforeActListener", cs_removeBeforeActListener);
            eng.AddHostObject("addAfterActListener", cs_addAfterActListener);
            eng.AddHostObject("removeAfterActListener", cs_removeAfterActListener);
            eng.AddHostObject("postTick", cs_postTick);
            eng.AddHostObject("setCommandDescribe", cs_setCommandDescribe);
            eng.AddHostObject("runcmd", cs_runcmd);
            eng.AddHostObject("logout", cs_logout);
            eng.AddHostObject("getOnLinePlayers", cs_getOnLinePlayers);
            eng.AddHostObject("getStructure", cs_getStructure);
            eng.AddHostObject("setStructure", cs_setStructure);
            eng.AddHostObject("setServerMotd", cs_setServerMotd);
            eng.AddHostObject("JSErunScript", cs_JSErunScript);
            eng.AddHostObject("JSEfireCustomEvent", cs_JSEfireCustomEvent);
            eng.AddHostObject("getscoreById", cs_getscoreById);
            eng.AddHostObject("setscoreById", cs_setscoreById);
            eng.AddHostObject("getAllScore", cs_getAllScore);
            eng.AddHostObject("setAllScore", cs_setAllScore);
            eng.AddHostObject("getMapColors", cs_getMapColors);
            eng.AddHostObject("exportPlayersData", cs_exportPlayersData);
            eng.AddHostObject("importPlayersData", cs_importPlayersData);

            eng.AddHostObject("reNameByUuid", cs_reNameByUuid);
            eng.AddHostObject("getPlayerAbilities", cs_getPlayerAbilities);
            eng.AddHostObject("setPlayerAbilities", cs_setPlayerAbilities);
            eng.AddHostObject("getPlayerAttributes", cs_getPlayerAttributes);
            eng.AddHostObject("setPlayerTempAttributes", cs_setPlayerTempAttributes);
            eng.AddHostObject("getPlayerMaxAttributes", cs_getPlayerMaxAttributes);
            eng.AddHostObject("setPlayerMaxAttributes", cs_setPlayerMaxAttributes);
            eng.AddHostObject("getPlayerItems", cs_getPlayerItems);
            eng.AddHostObject("getPlayerSelectedItem", cs_getPlayerSelectedItem);
            eng.AddHostObject("setPlayerItems", cs_setPlayerItems);
            eng.AddHostObject("addPlayerItemEx", cs_addPlayerItemEx);
            eng.AddHostObject("addPlayerItem", cs_addPlayerItem);
            eng.AddHostObject("getPlayerEffects", cs_getPlayerEffects);
            eng.AddHostObject("setPlayerEffects", cs_setPlayerEffects);
            eng.AddHostObject("setPlayerBossBar", cs_setPlayerBossBar);
            eng.AddHostObject("removePlayerBossBar", cs_removePlayerBossBar);
            eng.AddHostObject("selectPlayer", cs_selectPlayer);
            eng.AddHostObject("transferserver", cs_transferserver);
            eng.AddHostObject("teleport", cs_teleport);
            eng.AddHostObject("talkAs", cs_talkAs);
            eng.AddHostObject("runcmdAs", cs_runcmdAs);
            eng.AddHostObject("sendSimpleForm", cs_sendSimpleForm);
            eng.AddHostObject("sendModalForm", cs_sendModalForm);
            eng.AddHostObject("sendCustomForm", cs_sendCustomForm);
            eng.AddHostObject("releaseForm", cs_releaseForm);
            eng.AddHostObject("setPlayerSidebar", cs_setPlayerSidebar);
            eng.AddHostObject("removePlayerSidebar", cs_removePlayerSidebar);
            eng.AddHostObject("getPlayerPermissionAndGametype", cs_getPlayerPermissionAndGametype);
            eng.AddHostObject("setPlayerPermissionAndGametype", cs_setPlayerPermissionAndGametype);
            eng.AddHostObject("disconnectClient", cs_disconnectClient);
            eng.AddHostObject("sendText", cs_sendText);
            eng.AddHostObject("getscoreboard", cs_getscoreboard);
            eng.AddHostObject("setscoreboard", cs_setscoreboard);
            eng.AddHostObject("getPlayerIP", cs_getPlayerIP);
        }
コード例 #20
0
        public void Install(IProjectSource source, ScriptEngine engine)
        {
            if (source is IScript == false)
            {
                return;
            }

            var script = (IScript) source;

            var requireImpl = _componentContext.Resolve<RequireDefine>(
                new TypedParameter(typeof (ScriptEngine), engine),
                new TypedParameter(typeof (IScript), script));

            engine.AddHostObject(nameof(requireImpl), requireImpl);

            engine.Execute(
                @"var require = function (names, callback) {
                    var list = new mscorlib.System.Collections.ArrayList();

                    if (names && typeof names !== 'string') {
                        for (var i = 0; i < names.length; ++i) {
                          list.Add(names[i]);
                        }
                    }

                    var loaded = typeof name !== 'string'
                        ? requireImpl.RequireMultiple(list)
                        : requireImpl.RequireSingle(names);

                    if (typeof callback === 'function') {
                      return callback.apply(null, loaded);
                    }

                    return loaded;
                  };");

            engine.Execute(
              @"var define = function (name, deps, body) {
                  if (typeof name !== 'string') {
                      body = deps;
                      deps = name;
                      name = null;
                  }

                  if (deps instanceof Array === false) {
                    if (body == null && deps != null) {
                      body = deps;
                      deps = [];
                    }

                    body = deps;

                    deps = [];
                  }

                  var CallbackType = mscorlib.System.Func(mscorlib.System.Collections.ArrayList, mscorlib.System.Object);

                  var cb = new CallbackType(function(deps) {
                    if (typeof body === 'function') {
                      var transformed = [];

                      for (var i = 0; i < deps.Count; ++i) {
                        transformed.push(deps[i]);
                      }

                      return body.apply(null, deps);
                    }

                    return body;
                  });

                  var list = new mscorlib.System.Collections.ArrayList();

                  if (deps) {
                    for (var i = 0; i < deps.length; ++i) {
                      list.Add(deps[i]);
                    }
                  }

                  requireImpl.DefineModule(name, list, cb);

                  return require(name);
                };

                define['amd'] = true;");
        }
コード例 #21
0
 public void TestInitialize()
 {
     engine = new V8ScriptEngine(V8ScriptEngineFlags.EnableDebugging);
     engine.AddHostObject("host", new ExtendedHostFunctions());
     engine.AddHostObject("mscorlib", HostItemFlags.GlobalMembers, new HostTypeCollection("mscorlib"));
     engine.AddHostObject("ClearScriptTest", HostItemFlags.GlobalMembers, new HostTypeCollection("ClearScriptTest").GetNamespaceNode("Microsoft.ClearScript.Test"));
     engine.AddHostObject("testObject", testObject = new TestObject());
 }
コード例 #22
0
ファイル: TransformedReader.cs プロジェクト: raasiel/Reflow
 public void Init()
 {
     _eng = ScriptFactory.GetScriptEngine(this.ScriptingLanguage);
     for (int i = 0; i < _source.FieldCount; i++)
     {
         _fields.Add(_source.GetName(i));
     }
     _eng.AddHostObject("Record", _rec);
     _eng.AddHostType("Console", typeof(Console));
 }
コード例 #23
0
        public static ScriptEngine AddHostObject(this ScriptEngine engine, HostObject hostObject)
        {
            engine.AddHostObject(hostObject.Name, hostObject.Flags, hostObject.Target);

            return(engine);
        }
コード例 #24
0
 public void TestInitialize()
 {
     engine = new JScriptEngine(WindowsScriptEngineFlags.EnableDebugging);
     engine.DisableCaseInsensitivePropertyLookups = true;
     engine.AddHostObject("host", host            = new ExtendedHostFunctions());
 }
コード例 #25
0
 public void TestInitialize()
 {
     engine = new V8ScriptEngine(V8ScriptEngineFlags.EnableDebugging);
     engine.AddHostObject("clr", HostItemFlags.GlobalMembers, new HostTypeCollection("mscorlib", "System", "System.Core"));
     engine.AddHostObject("host", new HostFunctions());
 }
コード例 #26
0
        private void txtCommandLine_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Return)
            {
                code += this.txtCommandLine.Lines[this.txtCommandLine.Lines.Length - 2];
                if (code.Contains("exec"))
                {
                    code = code.Replace("exec", "");
                    engine.Execute(code);
                    code = "";
                }

                if (code.Contains("clear"))
                {
                    code = "";
                    this.txtCommandLine.Clear();
                    this.txtCommandLine.SelectionStart = this.txtCommandLine.Text.Length;
                }
                if (code.Contains("help"))
                {
                    this.txtCommandLine.Text          += "How to use: \n";
                    this.txtCommandLine.Text          += "Write your code like you would on a regular text editor, then type in exec on a new line to execute your commands \n";
                    this.txtCommandLine.Text          += "To clear the CLI, just type in clear: \n";
                    this.txtCommandLine.Text          += "To switch to another engine, just type in switch followed by the engine type: \n";
                    this.txtCommandLine.Text          += "example: switch vbscript to code using vbscipt, type switch v8 to go back to javascript \n";
                    this.txtCommandLine.Text          += "you can dynamically import Objects/Classes in this CLI, just type in import <your_object_name> <object_namespace> <object_type> \n";
                    this.txtCommandLine.Text          += "Using ClearScript is fun :D \n";
                    this.txtCommandLine.SelectionStart = this.txtCommandLine.Text.Length;
                    code = "";
                }

                if (code.Contains("switch"))
                {
                    if (code.Contains("vbscript") || code.Contains("vbs"))
                    {
                        try
                        {
                            engine = ClearScript.Create(EngineType.VBScript);
                            engine.AddHostObject("log", this);
                            this.txtCommandLine.Text += "Switched to VBScript \n";
                            code = "";
                            this.txtCommandLine.SelectionStart = this.txtCommandLine.Text.Length;
                        }
                        catch (ExecutionEngineException ex)
                        {
                            Output(ex.Message);
                            code = "";
                        }
                    }
                    else
                    if (code.Contains("v8"))
                    {
                        engine = ClearScript.Create(EngineType.V8);
                        engine.AddHostObject("log", this);
                        this.txtCommandLine.Text += "Switched to V8 \n";
                        code = "";
                        this.txtCommandLine.SelectionStart = this.txtCommandLine.Text.Length;
                    }
                    else
                    {
                        Output("Missing engine type, please type 'switch vbscript' or 'switch v8' to change engine types");
                        code = "";
                    }
                }

                if (code.Contains("import"))
                {
                    string command       = code.Substring(code.IndexOf("import"));
                    var    command_array = command.Split(' ');
                    if (command.Length < 4)
                    {
                        Output("Error, please type import <object_name> <object_namespace> <object_type>");
                        code = "";
                    }
                    else
                    {
                        try
                        {
                            Import(command_array[1], command_array[3], command_array[2]);
                            code = "";
                        }
                        catch
                        {
                            Output("Please verify that you typed the right command, or verify that the type of object you provided exists");
                        }
                    }
                }


                e.Handled = true;
            }
        }
コード例 #27
0
ファイル: JavascriptEngine.cs プロジェクト: octgn/OCTGN4
 public void AddObject(string name, object o)
 {
     _engine.AddHostObject(name, o);
 }
コード例 #28
0
ファイル: BugFixTest.cs プロジェクト: dirkelfman/ClearScript
        public void BugFix_AmbiguousIndexer_ADODB()
        {
            engine.Dispose();
            engine = new VBScriptEngine(WindowsScriptEngineFlags.EnableDebugging);

            var recordSet = new ADODB.Recordset();
            recordSet.Fields.Append("foo", ADODB.DataTypeEnum.adVarChar, 20);
            recordSet.Open(Missing.Value, Missing.Value, ADODB.CursorTypeEnum.adOpenStatic, ADODB.LockTypeEnum.adLockOptimistic, 0);
            recordSet.AddNew(Missing.Value, Missing.Value);
            recordSet.Fields["foo"].Value = "bar";

            engine.AddHostObject("recordSet", recordSet);
            Assert.AreEqual("bar", engine.Evaluate("recordSet.Fields.Item(\"foo\").Value"));

            engine.Execute("recordSet.Fields.Item(\"foo\").Value = \"qux\"");
            Assert.AreEqual("qux", engine.Evaluate("recordSet.Fields.Item(\"foo\").Value"));

            TestUtil.AssertException<ScriptEngineException>(() => engine.Evaluate("recordSet.Fields.Item(\"baz\")"));
        }
コード例 #29
0
        public void Install(IProjectSource source, ScriptEngine engine)
        {
            if (source is IScript == false)
            {
                return;
            }

            var script = (IScript)source;

            var requireImpl = _componentContext.Resolve <RequireDefine>(
                new TypedParameter(typeof(ScriptEngine), engine),
                new TypedParameter(typeof(IScript), script));

            engine.AddHostObject(nameof(requireImpl), requireImpl);

            engine.Execute(
                @"var require = function (names, callback) {
                    var list = new mscorlib.System.Collections.ArrayList();

                    if (names && typeof names !== 'string') {
                        for (var i = 0; i < names.length; ++i) {
                          list.Add(names[i]);
                        }
                    }

                    var loaded = typeof name !== 'string'
                        ? requireImpl.RequireMultiple(list)
                        : requireImpl.RequireSingle(names);
  
                    if (typeof callback === 'function') {
                      return callback.apply(null, loaded);
                    }

                    return loaded;
                  };");

            engine.Execute(
                @"var define = function (name, deps, body) {
                  if (typeof name !== 'string') {
                      body = deps;
                      deps = name;
                      name = null;
                  }

                  if (deps instanceof Array === false) {
                    if (body == null && deps != null) {
                      body = deps;
                      deps = [];
                    }

                    body = deps;

                    deps = [];
                  }
 
                  var CallbackType = mscorlib.System.Func(mscorlib.System.Collections.ArrayList, mscorlib.System.Object);

                  var cb = new CallbackType(function(deps) {
                    if (typeof body === 'function') {
                      var transformed = [];

                      for (var i = 0; i < deps.Count; ++i) {
                        transformed.push(deps[i]);
                      }

                      return body.apply(null, deps);
                    }

                    return body;
                  });

                  var list = new mscorlib.System.Collections.ArrayList();

                  if (deps) {
                    for (var i = 0; i < deps.length; ++i) {
                      list.Add(deps[i]);
                    }
                  }
    
                  requireImpl.DefineModule(name, list, cb);

                  return require(name);
                };

                define['amd'] = true;");
        }
コード例 #30
0
 public void TestInitialize()
 {
     engine = new V8ScriptEngine(V8ScriptEngineFlags.EnableDebugging);
     engine.AddHostObject("host", host = new ExtendedHostFunctions());
 }
コード例 #31
0
 private void frmCommandLine_Load(object sender, EventArgs e)
 {
     this.txtCommandLine.Text += "\n";
     engine = ClearScript.Create(EngineType.V8);
     engine.AddHostObject("log", this);
 }
コード例 #32
0
        public void HostVariable_out()
        {
            var value = new Random();
            var dict  = new Dictionary <string, Random> {
                { "key", value }
            };

            engine.AddHostObject("dict", dict);
            engine.Execute("var value = host.newVar(System.Random); var found = dict.TryGetValue('key', value.out); var result = value.value;");
            Assert.IsTrue(engine.Script.found);
            Assert.AreSame(value, engine.Script.result);
        }
コード例 #33
0
 public void TestInitialize()
 {
     engine = new JScriptEngine(WindowsScriptEngineFlags.EnableDebugging);
     engine.AddHostObject("host", new ExtendedHostFunctions());
     engine.AddHostObject("mscorlib", HostItemFlags.GlobalMembers, new HostTypeCollection("mscorlib"));
     engine.AddHostObject("ClearScriptTest", HostItemFlags.GlobalMembers, new HostTypeCollection("ClearScriptTest").GetNamespaceNode("Microsoft.ClearScript.Test"));
     engine.AddHostObject("testObject", testInterface = testObject = new TestObject());
     engine.Execute("var testInterface = host.cast(IExplicitTestInterface, testObject)");
 }