public override void SetGlobal(string name, object value) { Trace.Assert(!String.IsNullOrWhiteSpace(name)); Trace.Assert(ctx != null); ctx.SetGlobal(name, value); }
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)); }
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 static CSharp.Context Initialize() { if (!_isInitialized) { lock (SyncLock) { if (!_isInitialized) { Context.Execute(CommonJS); Context.SetGlobal("read", IronJS.Native.Utils.CreateFunction <Func <string, string> >(Context.Environment, 1, ReadSource)); _isInitialized = true; } } } return(Context); }
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 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 string this[string index] { get { return(ctx.GetGlobalAs <string>(index)); } set { ctx.SetGlobal <string>(index, value); } }
public void SetObject(string name, object value) { context.SetGlobal(name, value); }