public ScriptRunner() { _context = new CSharp.Context(); var file = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Resources/calculate.js"); _context.ExecuteFile(file); _calculate = _context.Globals.GetT<FunctionObject>("calculate"); }
private void WireUpJavaScriptConfigurationObject(CSharp.Context context) { var prototype = context.Environment.NewObject(); var constructor = IronJS.Native.Utils.CreateConstructor <Func <FunctionObject, CommonObject, CommonObject> >( context.Environment, 0, (ctor, _) => { var proto = ctor.GetT <CommonObject>("prototype"); return(new ConfigJsObject(ctor.Env, this, proto)); }); prototype.Prototype = context.Environment.Prototypes.Object; prototype.Put("run", IronJS.Native.Utils.CreateFunction <Action <FunctionObject, CommonObject, BoxedValue> >( context.Environment, 1, ConfigJsObject.Run)); prototype.Put("use", IronJS.Native.Utils.CreateFunction <Action <FunctionObject, CommonObject, CommonObject> >( context.Environment, 1, ConfigJsObject.Use)); prototype.Put("map", IronJS.Native.Utils.CreateFunction <Action <FunctionObject, CommonObject, string, BoxedValue> >( context.Environment, 1, ConfigJsObject.Map)); constructor.Put("prototype", prototype); context.SetGlobal("Config", constructor); }
public void CreateContext() { context = new CSharp.Context(); mock = new EditorMock(); var prototype = EditorObject.CreatePrototype(context.Environment); context.SetGlobal("edit", new EditorObject(context.Environment, prototype, mock)); }
static void Main(string[] args) { var ctx = new CSharp.Context(); dynamic Globals = ctx.Globals; Green(() => Console.WriteLine("This is the test bench for testing interop with IronJS")); Console.ReadKey(true); }
public void EditorsTest() { var mock = new MainWindowMock(); var context = new CSharp.Context(); WindowObject.AttachToContext(context, mock); bool result = (bool)context.Execute("!!window.editors"); Assert.IsTrue(result); }
public void ExitTest() { var mock = new MainWindowMock(); var context = new CSharp.Context(); WindowObject.AttachToContext(context, mock); context.Execute("window.exit()"); Assert.AreEqual(mock.ExitCalled, 1); }
public void ConstructorTest() { var context = new CSharp.Context(); CommandObject.Attach(context); context.Execute("var cmd = new Command()"); var cmd = context.Execute("cmd") as ICommand; Assert.IsNotNull(cmd); }
private static CSharp.Context SetupContext(ResourceHelper resourceHelper) { var context = new CSharp.Context(); context.CreatePrintFunction(); var require = new RequireDefinition(context, resourceHelper); require.Define(); return context; }
public void AddCommandTest() { var mock = new MainWindowMock(); var context = new CSharp.Context(); WindowObject.AttachToContext(context, mock); CommandObject.Attach(context); context.Execute("window.addCommand(new Command())"); Assert.AreEqual(mock.Commands.Count, 1); }
public void AddChildTest() { var context = new CSharp.Context(); CommandObject.Attach(context); context.Execute(@" var cmd = new Command(); cmd.addChild(new Command());"); var cmd = (ICommand)context.Execute("cmd"); Assert.AreEqual(1, cmd.Children.Count); }
public void GetTitleTest() { var mock = new MainWindowMock(); var context = new CSharp.Context(); WindowObject.AttachToContext(context, mock); string title = "Test"; mock.Title = title; string result = context.Execute("window.getTitle()").ToString(); Assert.AreEqual(result, title); }
/// <summary> /// Initializes a new instance of the <see cref="RequireDefinition"/> class. /// </summary> /// <param name="context">The context.</param> /// <param name="resourceHelper">The resource helper.</param> public RequireDefinition(CSharp.Context context, ResourceHelper resourceHelper) { if (context == null) throw new ArgumentNullException("context"); if (resourceHelper == null) throw new ArgumentNullException("resourceHelper"); this.context = context; this.resourceHelper = resourceHelper; this.requireCache = new Dictionary<string, CommonObject>(); this.cacheLock = new ReaderWriterLockSlim(LockRecursionPolicy.SupportsRecursion); this.require = Utils.CreateFunction<Func<string, CommonObject>>(this.context.Environment, 1, Require); }
public void GetIdTest() { var context = new CSharp.Context(); var id = "test"; CommandObject.Attach(context); context.Execute("var cmd = new Command()"); var cmd = context.GetGlobalAs<ICommand>("cmd"); cmd.Id = id; var result = context.Execute<string>("cmd.getId()"); Assert.AreEqual(result, id); }
private static CSharp.Context SetupContext(ResourceHelper resourceHelper) { var context = new CSharp.Context(); //context.CreatePrintFunction(); // Debug.registerConsolePrinter(); // IronJS.Support.Debug.registerAstPrinter(AstPrinter); // IronJS.Support.Debug.registerExprPrinter(ExprPrinter); ConsoleConstructor.AttachToContext(context); var require = new RequireDefinition(context, resourceHelper); require.Define(); return context; }
public void TestEvents() { var context = new CSharp.Context(); EventObject.Attach(context); bool result = (bool)context.Execute( @" var e = new EventObject(); var triggered = false; e.addHandler('test', function() { triggered = true; }); e.trigger('test'); triggered; "); Assert.IsTrue(result); }
private static void CompileCoffeeScriptUsingIronJs(string coffeeCompiler, string input) { context = new CSharp.Context(); //Create the JS object var robotConstructor = Utils.CreateConstructor <Func <FunctionObject, CommonObject, double, CommonObject> >(context.Environment, 1, Construct); var httpConstructor = Utils.CreateConstructor <Func <FunctionObject, CommonObject, string, CommonObject> >(context.Environment, 1, ConstructHttp); //setup the prototype (methods) on teh JS object var robotPrototype = context.Environment.NewObject(); robotPrototype.Prototype = context.Environment.Prototypes.Object; var respond = Utils.CreateFunction <Action <CommonObject, FunctionObject> >(context.Environment, 0, RobotObject.Respond); var hear = Utils.CreateFunction <Action <CommonObject, FunctionObject> >(context.Environment, 0, RobotObject.Hear); var send = Utils.CreateFunction <Action <CommonObject> >(context.Environment, 0, RobotObject.Send); var get = Utils.CreateFunction <Func <FunctionObject, CommonObject, FunctionObject, string> >(context.Environment, 0, HttpObject.HttpGet); var random = Utils.CreateFunction <Func <CommonObject, CommonObject> >(context.Environment, 1, RobotObject.Random); var httpPrototype = context.Environment.NewObject(); httpPrototype.Prototype = context.Environment.Prototypes.Object; httpPrototype.Put("get", get); httpConstructor.Put("prototype", httpPrototype, DescriptorAttrs.Immutable); //attach the methods robotPrototype.Put("respond", respond); robotPrototype.Put("send", send); robotPrototype.Put("hear", hear); robotPrototype.Put("http", httpConstructor); robotPrototype.Put("random", random); //attach the prototype robotConstructor.Put("prototype", robotPrototype, DescriptorAttrs.Immutable); context.SetGlobal("robot", robotConstructor); context.Execute(@"var bot = new robot();"); var result = context.Execute("2 + 2"); var result2 = context.Execute("bot.match"); //Setup and compile CoffeeScript context.Execute(coffeeCompiler); context.Execute("var compile = function (src) { return CoffeeScript.compile(src, { bare: true }); };"); //Attach basic ping script AttachScript(input); }
public void EventTest() { var context = new CSharp.Context(); CommandObject.Attach(context); context.Execute(@" var cmd = new Command(); var raised = false; var data = null; cmd.addHandler('test', function(d) { raised = true; data = d; });"); var cmd = (ICommand)context.Execute("cmd"); var data = "Testing"; cmd.RaiseEvent("test", data); Assert.IsTrue((bool)context.Execute("raised")); Assert.AreEqual(data, context.Execute("data")); }
public void NewEditorTest() { var mock = new EditorProviderMock(); var context = new CSharp.Context(); var provider = new EditorProviderObject( context.Environment, context.Environment.Prototypes.Object, mock); context.SetGlobal("editors", provider); // Create the editor context.Execute("var edit = editors.newEditor()"); var edit = context.GetGlobal("edit"); Assert.IsFalse(edit.IsUndefined); Assert.IsNotNull(edit.Clr); Assert.AreEqual(1, mock.Editors.Count); // Make sure it is an editor bool result = (bool)context.Execute("('setText' in edit)"); Assert.IsTrue(result); }
public void SetTitleTest() { var mock = new MainWindowMock(); var context = new CSharp.Context(); WindowObject.AttachToContext(context, mock); context.Execute("window.setTitle(\"Test\")"); Assert.AreEqual(mock.Title, "Test"); }
public void Render(ViewContext viewContext, System.IO.TextWriter writer) { var script = new StringBuilder(); try { this.Template.RenderScript("render_page", script); if (this.MasterView != null) { MasterView.Template.RenderScript("render_layout", script); } else { script.AppendLine("function render_layout(){ render_page(); }"); } var context = new CSharp.Context(); object model = viewContext.ViewData.Model; if (model is DynamicCommonObject) { model = ((DynamicCommonObject)model).CommonObject; } if (model != null) { context.SetGlobal("model", model); } else { script.AppendLine("var model = {};"); } // Passing Response directly to IronJS doesn't work right now // Apparently, .NET interop needs to be built yet. //context.SetGlobal("response", viewContext.HttpContext.Response); //context.SetGlobal("server", viewContext.HttpContext.Server); //context.SetGlobal("viewContext", viewContext); context.SetGlobal("response_write", Utils.CreateFunction<Action<string>>(context.Environment, 1, (obj) => { writer.Write(obj); })); context.SetGlobal("server_htmlencode", Utils.CreateFunction<Func<string, string>>(context.Environment, 1, (obj) => { return viewContext.HttpContext.Server.HtmlEncode(obj); })); script.AppendLine(@" var response = { Write: function(s){ response_write(s); } }; var server = { htmlEncode: function(s){ return server_htmlencode(s); } }; "); var html = new HtmlHelper(viewContext, new ViewDataContainer(viewContext.ViewData)); context.SetGlobal("html_actionlink1", Utils.CreateFunction<Func<string, string, MvcHtmlString>>(context.Environment, 2, (linkText, actionName) => { return html.ActionLink(linkText, actionName); })); context.SetGlobal("html_actionlink2", Utils.CreateFunction<Func<string, string, string, MvcHtmlString>>(context.Environment, 3, (linkText, actionName, controllerName) => { return html.ActionLink(linkText, actionName, new { controllerName = controllerName }); })); context.SetGlobal("html_actionlink3", Utils.CreateFunction<Func<string, string, string, CommonObject, MvcHtmlString>>(context.Environment, 4, (linkText, actionName, controllerName, obj) => { var routeValues = obj.ToRouteValueDictionary(); if (!routeValues.ContainsKey("controllerName")) { routeValues["controllerName"] = controllerName; } return html.ActionLink(linkText, actionName, new { controllerName = controllerName }); })); script.AppendLine(@" var html = { actionLink: function(){ if (arguments.length == 2) { return html_actionlink1(arguments[0], arguments[1]); } else if (arguments.length == 3) { return html_actionlink2(arguments[0], arguments[1], arguments[2]); } else if (arguments.length == 4) { return html_actionlink3(arguments[0], arguments[1], arguments[2], arguments[3]); } } }; "); script.AppendLine("render_layout();"); context.Execute(script.ToString()); } catch (Exception ex) { viewContext.HttpContext.Response.ClearContent(); writer.Write(ex.ToString().Replace("\n", "<br/>")); writer.Write("<br/><hr/><br/>"); writer.Write(script.Replace(" ", " ").Replace(">", ">").Replace("<", "<").Replace("\n", "<br/>")); } }
private static void CompileCoffeeScriptUsingIronJs(string coffeeCompiler, string input) { context = new CSharp.Context(); //Create the JS object var robotConstructor = Utils.CreateConstructor<Func<FunctionObject, CommonObject, double, CommonObject>>(context.Environment, 1, Construct); var httpConstructor = Utils.CreateConstructor<Func<FunctionObject, CommonObject, string, CommonObject>>(context.Environment, 1, ConstructHttp); //setup the prototype (methods) on teh JS object var robotPrototype = context.Environment.NewObject(); robotPrototype.Prototype = context.Environment.Prototypes.Object; var respond = Utils.CreateFunction<Action<CommonObject, FunctionObject>>(context.Environment, 0, RobotObject.Respond); var hear = Utils.CreateFunction<Action<CommonObject, FunctionObject>>(context.Environment, 0, RobotObject.Hear); var send = Utils.CreateFunction<Action<CommonObject>>(context.Environment, 0, RobotObject.Send); var get = Utils.CreateFunction<Func<FunctionObject, CommonObject, FunctionObject, string>>(context.Environment, 0, HttpObject.HttpGet); var random = Utils.CreateFunction<Func<CommonObject, CommonObject>>(context.Environment, 1, RobotObject.Random); var httpPrototype = context.Environment.NewObject(); httpPrototype.Prototype = context.Environment.Prototypes.Object; httpPrototype.Put("get", get); httpConstructor.Put("prototype", httpPrototype, DescriptorAttrs.Immutable); //attach the methods robotPrototype.Put("respond", respond); robotPrototype.Put("send", send); robotPrototype.Put("hear", hear); robotPrototype.Put("http", httpConstructor); robotPrototype.Put("random", random); //attach the prototype robotConstructor.Put("prototype", robotPrototype, DescriptorAttrs.Immutable); context.SetGlobal("robot", robotConstructor); context.Execute(@"var bot = new robot();"); var result = context.Execute("2 + 2"); var result2 = context.Execute("bot.match"); //Setup and compile CoffeeScript context.Execute(coffeeCompiler); context.Execute("var compile = function (src) { return CoffeeScript.compile(src, { bare: true }); };"); //Attach basic ping script AttachScript(input); }
public void InitializeLibrary(string libraryCode) { _engine = new CSharp.Context(); _engine.Execute(libraryCode); }
static JSEnvironment() { Main = new CSharp.Context(); CommandObject.Attach(Main); }
public void RemoveApplicationCommandTest() { var mock = new MainWindowMock(); var context = new CSharp.Context(); WindowObject.AttachToContext(context, mock); CommandObject.Attach(context); context.Execute(@" var c = new Command(); window.addApplicationCommand(c); window.removeApplicationCommand(c);"); Assert.AreEqual(mock.ApplicationCommands.Count, 0); }
public IronJSEngine() { ctx = new CSharp.Context(); }
public GetFiles(CSharp.Context context) { this.context = context; }
public void SetIdTest() { var context = new CSharp.Context(); var id = "test"; CommandObject.Attach(context); context.Execute(string.Format(@" var cmd = new Command(); cmd.setId('{0}');", id)); var cmd = context.GetGlobalAs<ICommand>("cmd"); Assert.AreEqual(id, cmd.Id); }
public void SetTextTest() { var context = new CSharp.Context(); var text = "MyText"; CommandObject.Attach(context); context.Execute(string.Format(@" var cmd = new Command(); cmd.setText('{0}');", text)); var cmd = context.GetGlobalAs<ICommand>("cmd"); Assert.AreEqual(text, cmd.Text); }
/// <summary> /// Initializes a new instance of the <see cref="Uglifier"/> class. /// </summary> public Uglifier() { this.resourceHelper = new ResourceHelper(); this.context = SetupContext(this.resourceHelper); this.uglify = LoadUglify(this.context, this.resourceHelper); }
public void GetTextTest() { var context = new CSharp.Context(); var text = "MyText"; CommandObject.Attach(context); context.Execute("var cmd = new Command()"); var cmd = context.GetGlobalAs<ICommand>("cmd"); cmd.Text = text; var result = context.Execute("cmd.getText()"); Assert.AreEqual("MyText", (string)result); }
public void TextChangedTest() { var context = new CSharp.Context(); CommandObject.Attach(context); context.Execute("var cmd = new Command()"); var command = context.GetGlobalAs<ICommand>("cmd"); bool fired = false; command.TextChanged += delegate { fired = true; }; context.Execute("cmd.setText('test')"); Assert.IsTrue(fired); }