/// <summary> /// <see>http://nodejs.org/docs/v0.4.8/api/buffers.html#new_Buffer</see> /// </summary> public static NodeBuffer Create(IronJS.Environment env, ArrayObject array) { NodeBuffer n = new NodeBuffer(env, array.Length); for (int i = 0; i < array.Length; i += 1) n.Data[i] = (byte)(uint)array.Get(i).Number; return n; }
protected override TestResult ExecuteTest(IronJS.Hosting.CSharp.Context ctx, string test) { var times = new List<long>(); try { //Warmup run so we don't get hit by IronJS assembly JIT overhead ctx.ExecuteFile(test); for (int i = 0; i < Runs; i++) { // Collect all garbage between runs GC.Collect(2, GCCollectionMode.Forced); // Run and time var sw = Stopwatch.StartNew(); ctx.ExecuteFile(test); sw.Stop(); times.Add(sw.ElapsedMilliseconds); } } catch (Exception ex) { return new TestResult { Error = ex.GetBaseException().Message, Tag = Tuple.Create(test, Enumerable.Repeat(-1L, Runs).ToArray()) }; } return new TestResult { Score = GetScore(times), Tag = Tuple.Create(test, times.ToArray()) }; }
public net( IronJS.Environment env ) : base( env, env.Maps.Base, env.Prototypes.Object ) { var objMethod = Utils.createHostFunction<Func<IronJS.FunctionObject, IronJS.CommonObject>>( Env, CreateServer ); log.Trace( objMethod ); this.Put( "createServer", objMethod ); }
/// <summary> /// The internal constructor for a NodeBuffer that copies information /// from an existing byte[] array. /// </summary> /// <param name="env">The JavaScript environment.</param> internal NodeBuffer(IronJS.Environment env, byte[] bytes, int offset, int length) : this(env) { this.m_Data = new byte[length]; for (int i = 0; i < length; i += 1) this.m_Data[i] = bytes[offset + length]; }
public NetStream( Stream stream, IronJS.Environment env ) : base( env, env.Maps.Base, env.Prototypes.Object ) { this.stream = stream; var objMethod = Utils.CreateFunction<Action<string, IronJS.FunctionObject>>( env, 0, addListener ); this.Put( "addListener", objMethod, DescriptorAttrs.Immutable ); var objRemoveMethod = Utils.CreateFunction<Action<string>>( env, 0, removeAllListeners ); this.Put( "removeAllListeners", objRemoveMethod, DescriptorAttrs.Immutable ); }
/// <summary> /// The public constructor for the 'process' module. /// </summary> /// <param name="env">The JavaScript environment.</param> public ProcessModule(IronJS.Environment env) : base(env) { // Open the console streams. this.m_stdout = new NodeWritableStream(env, new StreamWriter(Console.OpenStandardOutput())); this.m_stderr = new NodeWritableStream(env, new StreamWriter(Console.OpenStandardError())); this.m_stdin = new NodeReadableStream(env, new StreamReader(Console.OpenStandardInput())); }
public void addListener( string eventname, IronJS.Function callback ) { Console.WriteLine( "NetStream: adding listener: " + eventname ); if( eventname == "data" ) { dataCallbacks.Add( callback ); } else if( eventname == "end" ) { endCallbacks.Add( callback ); } }
public void addListener( string eventname, IronJS.FunctionObject callback ) { Server.instance.Listeners++; log.Trace( "NetStream: adding listener: " + eventname ); if( eventname == "data" ) { dataCallbacks.Add( callback ); } else if( eventname == "end" ) { endCallbacks.Add( callback ); } }
public HttpServer( IronJS.FunctionObject callback, IronJS.Environment env ) : base( env, env.Maps.Base, env.Prototypes.Object, IronJS.Classes.Object ) { // have to set this stuff up. not sure why yet Console.WriteLine( "creating HttpServer" ); var objMethod = Utils.createHostFunction<Action<object, string>>(env, listen); // this.Methods.PutRefProperty(this, "listen", objMethod, IronJS.TypeTags.Function); this.Put( "listen", objMethod, TypeTags.Function ) this.addListener( "connection", callback ); }
/// <summary> /// Constructs a new NodeReadableStream using the specified .NET stream reader. /// </summary> /// <param name="reader">The .NET StreamReader to wrap.</param> internal NodeReadableStream(IronJS.Environment env, StreamReader reader) : base(env) { this.m_Reader = reader; EventManager.Add(this); // Define the handler to respond to asynchronous operations. AsyncCallback handler = null; handler = (result) => { if (result.IsCompleted) { int bytes = this.m_Reader.BaseStream.EndRead(result); if (bytes == 0) { // There is no data at the moment. } else if (!this.m_Paused && (this.OnData != null || this.m_PipeDestination != null)) { // Send it now. if (this.m_PipeDestination != null) this.m_PipeDestination.write(new NodeBuffer(this.Env, this.m_Buffer, 0, bytes)); // TODO: Support writing as string as well? if (this.OnData != null) this.OnData(this, new DataEventArgs(this.m_Buffer, 0, bytes, this.m_Encoding)); } else { // Queue it up. this.m_Queue.Add(new DataEventArgs(this.m_Buffer, 0, bytes, this.m_Encoding)); } if (this.m_Reader.BaseStream.Length != this.m_Reader.BaseStream.Position || this.m_Queue.Count > 0) this.m_Reader.BaseStream.BeginRead(this.m_Buffer, 0, 256, handler, null); else { if (this.OnEnd != null) this.OnEnd(this, new EventArgs()); EventManager.Remove(this); } } }; // Starts an asynchronous reading operation. if (this.m_Reader.BaseStream.Length != this.m_Reader.BaseStream.Position) this.m_Reader.BaseStream.BeginRead(this.m_Buffer, 0, 256, handler, null); else { if (this.OnEnd != null) this.OnEnd(this, new EventArgs()); EventManager.Remove(this); } }
private BuilderJsObject(IronJS.Environment env, Builder builder) : base(env, env.NewPrototype()) { Put("run", IronJS.Native.Utils.CreateFunction<Action<CommonObject>>(env, 1, obj => builder.Run(new JavaScriptApp(obj)))); Put("use", IronJS.Native.Utils.CreateFunction<Action<CommonObject>>(env, 1, obj => builder.Use(typeof(JavaScriptApp), obj))); Put("map", IronJS.Native.Utils.CreateFunction<Action<string, FunctionObject>>(env, 2, (str, func) => builder.Map(str, MapBuilder(func)))); }
public void on( string eventname, IronJS.FunctionObject callback ) { log.Trace( "EventEmitter - adding listener: " + eventname ); // TODO: move listener counts to static field on the emitter Server.instance.Listeners++; // TODO: listen is not an event .. remove this if( Callbacks.ContainsKey( eventname ) ) { Callbacks[ eventname ].Add( callback ); } // TODO: I think that node allows any event to be registered, // so it probably isn't correct to throw on unexpected event else { throw new Exception( "on called for unsupported event" ); } }
public EnvironmentJsObject(IDictionary<string, object> environment, IronJS.Environment env, CommonObject prototype) : base(env, env.Maps.Base, prototype) { foreach(var key in environment.Keys) { if (key == "rack.input") { Put(key, new JsStream(env, environment[key] as Stream)); continue; } Put(key, environment[key].ToString()); } }
public static object Include(HttpContextBase httpContext, IronJS.Hosting.CSharp.Context context, string moduleName) { var filepath = httpContext.Server.MapPath(string.Format("~/Controllers/{0}.js", moduleName)); object obj = null; try { obj = context.ExecuteFile(filepath); } catch (IronJS.Error.Error ex) { throw new Exception("Source Module: " + moduleName + "\n\n" + ex.ToString(), ex); } return obj; }
public NetServer( IronJS.FunctionObject callback, IronJS.Environment env ) : base( env ) { // TODO: move as much of this as possible to EventEmitter Callbacks = new Dictionary<string, ArrayList>() { { "connect", new ArrayList() }, { "close", new ArrayList() } }; log.Trace( "creating NetServer" ); var objMethod = Utils.createHostFunction<Func<object, string, CommonObject>>(env, listen); var removeAllListenersMethod = Utils.createHostFunction<Action<string>>( env, removeAllListeners ); this.Put( "listen", objMethod, TypeTags.Function ); this.Put( "removeAllListeners", removeAllListenersMethod, TypeTags.Function ); this.addListener( "connect", callback ); }
public NetStream( Stream stream, IronJS.Environment env ) : base( env, env.Maps.Base, env.Prototypes.Object, IronJS.Classes.Object ) { // have to set this stuff up to satisfy IObj. not sure why yet Env = env; Methods = Env.Methods.Object; // IronJS objects set up the following also, not sure if we // really need this stuff though // Prototype = context.ObjectConstructor.Object_prototype; // Class = ObjClass.Array; this.stream = stream; var objMethod = IronJS.Api.HostFunction.create<Action<string, IronJS.Function>>(env, addListener); this.Methods.PutRefProperty(this, "addListener", objMethod, IronJS.TypeTags.Function); // this.SetOwnProperty( "addListener", new Action<string, IFunction>( addListener ) ); }
protected override TestResult ExecuteTest(IronJS.Hosting.CSharp.Context ctx, string test) { var errors = new StringBuilder(); Action<string> appendError = err => { if (errors.Length > 0) { errors.AppendLine(); } errors.Append(err); }; var score = string.Empty; Action<string, string> notifyResult = (name, result) => { }; Action<string, string> notifyError = (name, error) => appendError(name + ": " + error); Action<string> notifyScore = s => score = s; ctx.SetGlobal("NotifyResult", IronJS.Native.Utils.CreateFunction(ctx.Environment, 2, notifyResult)); ctx.SetGlobal("NotifyError", IronJS.Native.Utils.CreateFunction(ctx.Environment, 2, notifyError)); ctx.SetGlobal("NotifyScore", IronJS.Native.Utils.CreateFunction(ctx.Environment, 1, notifyScore)); try { ctx.ExecuteFile(test); ctx.Execute(@"BenchmarkSuite.RunSuites({ NotifyResult: NotifyResult, NotifyError: NotifyError, NotifyScore: NotifyScore });"); } catch (Exception ex) { appendError("Exception: " + ex.GetBaseException().Message); } if (errors.Length > 0) { return new TestResult { Error = errors.ToString() }; } else { return new TestResult { Score = score }; } }
public JsStream(IronJS.Environment env, Stream stream) : base(env, env.NewPrototype()) { Put("read", IronJS.Native.Utils.CreateFunction<Func<CommonObject>>(env, 0, () => { if (stream == null) { return null; } string input; using (var strm = stream) { strm.Position = 0; var reader = new StreamReader(strm); input = reader.ReadToEnd(); strm.Close(); } return env.NewString(input); })); }
public IronJS.Object CreateServer( IronJS.Function callback ) { Console.WriteLine( "net.createServer() called." ); Net.NetServer server = new Net.NetServer( callback, Env ); Console.WriteLine( server ); return( server ); }
public net( IronJS.Environment env ) : base( env, env.Maps.Base, env.Prototypes.Object, IronJS.Classes.Object ) { // have to set context to satisfy IronJS Obj Env = env; Methods = Env.Methods.Object; // SetOwnProperty( "createServer", new Fn_CreateServer( context ) ); var objMethod = IronJS.Api.HostFunction.create<Func<IronJS.Function, IronJS.Object>>(Env, CreateServer); Console.WriteLine( this.Methods == null ); Console.WriteLine( objMethod ); this.Methods.PutRefProperty(this, "createServer", objMethod, IronJS.TypeTags.Function); }
/// <summary> /// Constructs a new NodeWritableStream using the specified .NET stream writer. /// </summary> /// <param name="reader">The .NET StreamWriter to wrap.</param> internal NodeWritableStream(IronJS.Environment env, StreamWriter writer) : base(env) { this.m_Writer = writer; EventManager.Add(this); }
public NetServer( IronJS.Function callback, IronJS.Environment env ) : base( env, env.Maps.Base, env.Prototypes.Object, IronJS.Classes.Object ) { // have to set this stuff up. not sure why yet Env = env; Methods = Env.Methods.Object; // Prototype = context.ObjectConstructor.Object_prototype; // Class = ObjClass.Array; Console.WriteLine( "creating NetServer" ); //this.SetOwnProperty( "listen", new Action<object, string>( listen ) ); var objMethod = IronJS.Api.HostFunction.create<Action<object, string>>(env, listen); this.Methods.PutRefProperty(this, "listen", objMethod, IronJS.TypeTags.Function); this.addListener( "connection", callback ); }
/// <summary> /// The public constructor for the 'path' module. /// </summary> /// <param name="env">The JavaScript environment.</param> public PathModule(IronJS.Environment env) : base(env) { }
public void addListener( string eventname, IronJS.Function callback) { Console.WriteLine( "NetServer - adding listener: " + eventname ); if( eventname == "listening" ) { listeningCallbacks.Add( callback ); } else if( eventname == "connection" ) { connectionCallbacks.Add( callback ); } else if( eventname == "close" ) { closeCallbacks.Add( callback ); } else { throw new Exception( "addListener called for unsupported event" ); } }
/// <summary> /// The public constructor for the 'url' module. /// </summary> /// <param name="env">The JavaScript environment.</param> public UrlModule(IronJS.Environment env) : base(env) { }
protected NodeStream(IronJS.Environment env) : base(env) { }
public ConfigJsObject(IronJS.Environment env, ConfigBase config, CommonObject prototype) : base(env, prototype) { configBase = config; }
public abstract void pipe(NodeWritableStream destination, IronJS.ArrayObject options);
/// <summary> /// The public constructor for the 'dgram' module. /// </summary> /// <param name="env">The JavaScript environment.</param> public DgramModule(IronJS.Environment env) : base(env) { }
/// <summary> /// <see>http://nodejs.org/docs/v0.4.8/api/streams.html#stream.pipe</see> /// </summary> public override void pipe(NodeWritableStream destination, IronJS.ArrayObject options) { this.m_PipeDestination = destination; this.m_PipeDestination.DoPipe(this); }