Exemplo n.º 1
0
        /// <summary>
        /// Opens a file, appends the specified string to the file, and then closes the file. If the file does not exist, this method creates a file, writes the specified string to the file, then closes the file.
        /// </summary>
        internal static void AppendJson(FunctionObject ctx, ScriptObject instance, BoxedValue path, BoxedValue contents, BoxedValue onComplete, BoxedValue encodingName)
        {
            if (!path.IsString)
                throw new ArgumentException("[appendJson] First parameter should be defined and be a string.");
            if (!contents.IsStrictlyObject)
                throw new ArgumentException("[appendJson] Second parameter should be defined and be an object.");
            if (!onComplete.IsUndefined && !onComplete.IsFunction)
                throw new ArgumentException("[appendJson] Third parameter should be an onComplete function.");

            // Get the curent channel
            var channel = Channel.Current;

            // Dispatch the task
            channel.Async(() =>
            {
                // The encoding to use
                Encoding encoding = encodingName.IsString
                    ? TextEncoding.GetEncoding(encodingName.String)
                    : null;

                // Defaults to UTF8
                if (encoding == null)
                    encoding = TextEncoding.UTF8;

                // Unbox the array of lines and execute the append
                File.AppendAllText(
                    path.Unbox<string>(),
                    Native.Serialize(instance.Env, contents, false).Unbox<string>(),
                    encoding
                    );

                // Dispatch the on complete asynchronously
                channel.DispatchCallback(onComplete, instance);
            });
        }
Exemplo n.º 2
0
        /// <summary>
        /// Represents an event fired when a script object property has been changed.
        /// </summary>
        /// <param name="instance">The instance of the script object that contains the property.</param>
        /// <param name="changeType">The type of the change.</param>
        /// <param name="propertyName">The name of the property.</param>
        /// <param name="newValue">The new value of the property.</param>
        /// <param name="oldValue">The old value of the property.</param>
        public static void OnPropertyChange(ScriptObject instance, PropertyChangeType changeType, string propertyName, BoxedValue newValue, BoxedValue oldValue)
        {
            // Ignore identifier property
            if (propertyName == "$i")
                return;

            switch (changeType)
            {
                // Occurs when a new property is assigned to an object, either by
                // using the indexer, property access or array methods.
                case PropertyChangeType.Put:
                {
                    // We need to make sure the new value is marked as observed
                    // as well, so its members will notify us too.
                    if (newValue.IsStrictlyObject)
                        newValue.Object.Observe();

                    // Send the change through the current scope
                    Channel.Current.SendPropertyChange(PropertyChangeType.Put, instance.Oid, propertyName, newValue);
                    break;
                }

                // Occurs when a property change occurs, by using an assignment
                // operator, or the array indexer.
                case PropertyChangeType.Set:
                {
                    // We need to unmark the old value and ignore it, as we no
                    // longer require observations from that value.
                    if (oldValue.IsStrictlyObject)
                        oldValue.Object.Ignore();

                    // We need to make sure the new value is marked as observed
                    // as well, so its members will notify us too.
                    if (newValue.IsStrictlyObject)
                        newValue.Object.Observe();

                    // Send the change through the current scope
                    Channel.Current.SendPropertyChange(PropertyChangeType.Set, instance.Oid, propertyName, newValue);
                    break;
                }

                // Occurs when a property was deleted from the object, either by
                // using a 'delete' keyword or the array removal methods.
                case PropertyChangeType.Delete:
                {
                    // We need to unmark the old value and ignore it, as we no
                    // longer require observations from that value.
                    if (oldValue.IsStrictlyObject)
                        oldValue.Object.Ignore();

                    // Send the change through the current scope
                    Channel.Current.SendPropertyChange(PropertyChangeType.Delete, instance.Oid, propertyName, newValue);
                    break;
                }

            }

            //Console.WriteLine("Observe: [{0}] {1} = {2}", changeType.ToString(), propertyName, propertyValue);
        }
Exemplo n.º 3
0
        public static BoxedValue GetValue(ScriptObject o)
        {
            var vo = o as ValueObject;

            if (vo == null)
                return o.Env.RaiseTypeError<BoxedValue>("Cannot read the value of a non-value object.");

            return vo.Value.Value;
        }
Exemplo n.º 4
0
        /// <summary>
        /// Gets the session time.
        /// </summary>
        /// <param name="ctx">The function context.</param>
        /// <param name="instance">The console object instance.</param>
        /// <param name="eventName">The name of the event.</param>
        internal static BoxedValue GetConnectedFor(FunctionObject ctx, ScriptObject instance)
        {
            // Get the first client
            var client = Channel.Current.First;
            if (client == null)
                return Undefined.Boxed;

            // Return the address
            return BoxedValue.Box(
                client.Channel.ConnectedFor
                );
        }
Exemplo n.º 5
0
        /// <summary>
        /// Creates a new instance of an object.
        /// </summary>
        /// <param name="prototype">The prototype to use.</param>
        /// <param name="param">Either the size of the buffer, a string or the array of octets.</param>
        /// <param name="encodingName">The encoding of a string.</param>
        public BufferObject(ScriptObject prototype, BoxedValue param, BoxedValue encodingName)
            : base(prototype)
        {
            if (param.IsNumber)
            {
                // Create a new array with the provided size
                this.Buffer = new ArraySegment<byte>(
                    new byte[(int)param.Number]
                    );
                this.Put("length", this.Buffer.Count, DescriptorAttrs.ReadOnly);
                return;
            }

            if (param.IsString)
            {
                // The encoding to use
                Encoding encoding = encodingName.IsString
                    ? TextEncoding.GetEncoding(encodingName.String)
                    : null;

                // Defaults to UTF8
                if (encoding == null)
                    encoding = TextEncoding.UTF8;

                // Decode
                this.Buffer = new ArraySegment<byte>(encoding.GetBytes(param.String));
                this.Put("length", this.Buffer.Count, DescriptorAttrs.ReadOnly);
                return;
            }

            if (param.IsStrictlyObject && param.Object is ArrayObject)
            {
                // Allocate a new array
                var bytes = (param.Object as ArrayObject);
                this.Buffer = new ArraySegment<byte>(new byte[bytes.Length]);
                this.Put("length", this.Buffer.Count, DescriptorAttrs.ReadOnly);

                // Iterate through the array and convert each integer to a byte
                for (int i = 0; i < bytes.Length; ++i)
                {
                    // Get the number and convert to byte
                    var item = bytes.Get(i);
                    if (item.IsNumber)
                        this.Buffer.Array[i] = Convert.ToByte(item.Number);
                }
            }
        }
Exemplo n.º 6
0
 public static BoxedValue ToBoxedValue(ScriptObject o)
 {
     return BoxedValue.Box(o);
 }
Exemplo n.º 7
0
 public static void InvokePropertyChange(ScriptObject instance, PropertyChangeType type, string name, double newValue, BoxedValue oldValue)
 {
     // Invoke the changed event
     if (ScriptObject.PropertyChange != null)
         ScriptObject.PropertyChange(instance, type, name, BoxedValue.Box(newValue), oldValue);
 }
Exemplo n.º 8
0
 public static BoxedValue ToPrimitive(ScriptObject o, DefaultValueHint hint)
 {
     return o.DefaultValue(hint);
 }
Exemplo n.º 9
0
 public static string ToString(ScriptObject o)
 {
     var s = o as StringObject;
     return s != null
         ? s.Value.Value.String
         : ToString(o.DefaultValue(DefaultValueHint.String));
 }
Exemplo n.º 10
0
 /// <summary>
 /// Displays an interactive listing of the properties of a specified JavaScript object. This 
 /// listing lets you use disclosure triangles to examine the contents of child objects.
 /// </summary>
 /// <param name="ctx">The function context.</param>
 /// <param name="instance">The console object instance.</param>
 /// <param name="eventName">The name of the event.</param>
 /// <param name="eventValue">The value of the event.</param>
 internal static void Dir(FunctionObject ctx, ScriptObject instance, BoxedValue eventValue)
 {
     ConsoleObject.SendEvent("dir", eventValue);
 }
Exemplo n.º 11
0
        /// <summary>
        /// Opens a binary file, reads the contents of the file into a byte array, and then closes the file.
        /// </summary>
        internal static void ReadBuffer(FunctionObject ctx, ScriptObject instance, BoxedValue path, BoxedValue onComplete)
        {
            if (!path.IsString)
                throw new ArgumentException("[readBuffer] First parameter should be defined and be a string.");
            if (!onComplete.IsFunction)
                throw new ArgumentException("[readBuffer] Second parameter should be an onComplete function.");

            // Get the curent channel
            var channel = Channel.Current;

            // Dispatch the task
            channel.Async(() =>
            {
                // Read the file
                var arr = File.ReadAllBytes(path.Unbox<string>());
                var seg = new ArraySegment<byte>(arr);

                // Unbox the array of lines and execute the append
                var buffer = BoxedValue.Box(
                    new BufferObject(seg, instance.Env)
                    );

                // Dispatch the on complete asynchronously
                channel.DispatchCallback(onComplete, instance, buffer);
            });
        }
Exemplo n.º 12
0
        /// <summary>
        /// Opens a file, reads all lines of the file with the specified encoding, and then closes the file.
        /// </summary>
        internal static void ReadLines(FunctionObject ctx, ScriptObject instance, BoxedValue path, BoxedValue onComplete, BoxedValue encodingName)
        {
            if (!path.IsString)
                throw new ArgumentException("[readLines] First parameter should be defined and be a string.");
            if (!onComplete.IsFunction)
                throw new ArgumentException("[readLines] Second parameter should be an onComplete function.");

            // Get the curent channel
            var channel = Channel.Current;

            // Dispatch the task
            channel.Async(() =>
            {
                // The encoding to use
                Encoding encoding = encodingName.IsString
                    ? TextEncoding.GetEncoding(encodingName.String)
                    : null;

                // Defaults to UTF8
                if (encoding == null)
                    encoding = TextEncoding.UTF8;

                // Unbox the array of lines and execute the append
                var lines = File.ReadAllLines(
                    path.Unbox<string>(),
                    encoding
                    );

                // Create a new array
                var array = new ArrayObject(instance.Env, (uint)lines.Length);
                for (uint i = 0; i < lines.Length; ++i)
                {
                    // Put a boxed string inside for each line
                    array.Put(i, BoxedValue.Box(lines[i]));
                }

                // Dispatch the on complete asynchronously
                channel.DispatchCallback(onComplete, instance, BoxedValue.Box(array));

            });
        }
Exemplo n.º 13
0
        /// <summary>
        /// Replaces an existing file with a new file.
        /// </summary>
        internal static void Replace(FunctionObject ctx, ScriptObject instance, BoxedValue sourceFileName, BoxedValue destFileName, BoxedValue onComplete)
        {
            if (!sourceFileName.IsString)
                throw new ArgumentException("[replace] First parameter should be defined and be a string.");
            if (!destFileName.IsString)
                throw new ArgumentException("[replace] Second parameter should be defined and be a string.");
            if (!onComplete.IsUndefined && !onComplete.IsFunction)
                throw new ArgumentException("[replace] Third parameter should be an onComplete function.");

            // Get the curent channel
            var channel = Channel.Current;

            // Dispatch the task
            channel.Async(() =>
            {
                // Copy the file
                if (File.Exists(destFileName.String))
                    File.Copy(sourceFileName.String, destFileName.String, true);

                // Dispatch the on complete asynchronously
                channel.DispatchCallback(onComplete, instance);
            });
        }
Exemplo n.º 14
0
 public ValueObject(Environment env, Schema map, ScriptObject prototype)
     : base(env, map, prototype)
 {
 }
Exemplo n.º 15
0
 /// <summary>
 /// Gets the hash code reference of the channel.
 /// </summary>
 /// <param name="ctx">The function context.</param>
 /// <param name="instance">The console object instance.</param>
 /// <param name="eventName">The name of the event.</param>
 internal static BoxedValue GetHashCode(FunctionObject ctx, ScriptObject instance)
 {
     // Return the address
     return BoxedValue.Box(
         Channel.Current.GetHashCode()
         );
 }
Exemplo n.º 16
0
 /// <summary>
 /// Creates a new instance of an object.
 /// </summary>
 /// <param name="prototype">The prototype to use.</param>
 public FileObject(ScriptObject prototype)
     : base(prototype)
 {
 }
Exemplo n.º 17
0
        /// <summary>
        /// Creates a new file by using the specified encoding, writes a collection of strings to the file, and then closes the file.
        /// </summary>
        internal static void WriteLines(FunctionObject ctx, ScriptObject instance, BoxedValue path, BoxedValue contents, BoxedValue onComplete, BoxedValue encodingName)
        {
            if (!path.IsString)
                throw new ArgumentException("[writeLines] TFirst parameter should be defined and be a string.");
            if (!contents.IsArray)
                throw new ArgumentException("[writeLines] TSecond parameter should be defined and be an array.");
            if (!onComplete.IsUndefined && !onComplete.IsFunction)
                throw new ArgumentException("[writeLines] Third parameter should be an onComplete function.");

            // Get the curent channel
            var channel = Channel.Current;

            // Dispatch the task
            channel.Async(() =>
            {
                // The encoding to use
                Encoding encoding = encodingName.IsString
                    ? TextEncoding.GetEncoding(encodingName.String)
                    : null;

                // Defaults to UTF8
                if (encoding == null)
                    encoding = TextEncoding.UTF8;

                // Unbox the array of lines and execute the append
                File.WriteAllLines(
                    path.Unbox<string>(),
                    contents.Array.ToArray<string>(),
                    encoding
                    );

                // Dispatch the on complete asynchronously
                channel.DispatchCallback(onComplete, instance);
            });
        }
Exemplo n.º 18
0
        /// <summary>
        /// Creates a new file, writes the specified byte array to the file, and then closes the file. If the target file already exists, it is overwritten.
        /// </summary>
        internal static void WriteBuffer(FunctionObject ctx, ScriptObject instance, BoxedValue path, BoxedValue contents, BoxedValue onComplete)
        {
            if (!path.IsString)
                throw new ArgumentException("[writeBuffer] First parameter should be defined and be a string.");
            if (!contents.IsObject || !(contents.Object is BufferObject))
                throw new ArgumentException("[writeBuffer] Second parameter should be defined and be a Buffer.");
            if (!onComplete.IsUndefined && !onComplete.IsFunction)
                throw new ArgumentException("[writeBuffer] Third parameter should be an onComplete function.");

            // Get the curent channel
            var channel = Channel.Current;

            // Dispatch the task
            channel.Async(() =>
            {
                // Get the buffer
                var buffer = contents.Object as BufferObject;

                // Write the contents
                File.WriteAllBytes(
                    path.Unbox<string>(),
                    buffer.Array
                    );

                // Dispatch the on complete asynchronously
                channel.DispatchCallback(onComplete, instance);
            });
        }
Exemplo n.º 19
0
 /// <summary>
 /// Constructs a new instance of an <see cref="AppScope"/>.
 /// </summary>
 /// <param name="name">The unique name for the scope.</param>
 /// <param name="parent">The parent scope to attach to the scope.</param>
 /// <param name="context">The execution context that is used to execute scripts.</param>
 internal PageScope(string name, SessionScope parent, ScriptContext context, ScriptObject prototype)
     : base(name, parent, context, prototype)
 {
 }
Exemplo n.º 20
0
 /// <summary>
 /// Stops the specified timer and logs the elapsed time in seconds since its start.
 /// </summary>
 /// <param name="ctx">The function context.</param>
 /// <param name="instance">The console object instance.</param>
 /// <param name="eventName">The name of the event.</param>
 /// <param name="eventValue">The value of the event.</param>
 internal static void TimeEnd(FunctionObject ctx, ScriptObject instance, BoxedValue eventValue)
 {
     ConsoleObject.SendEvent("timeEnd", eventValue);
 }
Exemplo n.º 21
0
 /// <summary>
 /// Creates a new container that servers as an application container for a scope.
 /// </summary>
 /// <param name="prototype">The prototype to use.</param>
 internal PageScope(ScriptObject prototype)
     : base(prototype)
 {
 }
Exemplo n.º 22
0
        /// <summary>
        /// Opens a file, reads all lines of the file with the specified encoding, and then closes the file.
        /// </summary>
        internal static void ReadText(FunctionObject ctx, ScriptObject instance, BoxedValue path, BoxedValue onComplete, BoxedValue encodingName)
        {
            if (!path.IsString)
                throw new ArgumentException("[readText] First parameter should be defined and be a string.");
            if (!onComplete.IsFunction)
                throw new ArgumentException("[readText] Second parameter should be an onComplete function.");

            // Get the curent channel
            var channel = Channel.Current;

            // Dispatch the task
            channel.Async(() =>
            {
                // The encoding to use
                Encoding encoding = encodingName.IsString
                    ? TextEncoding.GetEncoding(encodingName.String)
                    : null;

                // Defaults to UTF8
                if (encoding == null)
                    encoding = TextEncoding.UTF8;

                // Read the text
                var text = BoxedValue.Box(
                        File.ReadAllText(path.Unbox<string>(), encoding)
                    );

                // Dispatch the on complete asynchronously
                channel.DispatchCallback(onComplete, instance, text);
            });
        }
Exemplo n.º 23
0
 /// <summary>
 /// Creates a new container that servers as an application container for a scope.
 /// </summary>
 /// <param name="prototype">The prototype to use.</param>
 public RemoteObject(ScriptObject prototype)
     : base(prototype)
 {
 }
Exemplo n.º 24
0
 public static object ToClrObject(ScriptObject o)
 {
     return o;
 }
Exemplo n.º 25
0
 /// <summary>
 /// Exits the current inline group. 
 /// </summary>
 /// <param name="ctx">The function context.</param>
 /// <param name="instance">The console object instance.</param>
 /// <param name="eventName">The name of the event.</param>
 /// <param name="eventValue">The value of the event.</param>
 internal static void GroupEnd(FunctionObject ctx, ScriptObject instance)
 {
     ConsoleObject.SendEvent("groupEnd", Undefined.Boxed);
 }
Exemplo n.º 26
0
 public static double ToNumber(ScriptObject o)
 {
     var n = o as NumberObject;
     return n != null
         ? n.Value.Value.Number
         : ToNumber(o.DefaultValue(DefaultValueHint.Number));
 }
Exemplo n.º 27
0
 /// <summary>
 /// Creates a new container that servers as an application container for a scope.
 /// </summary>
 /// <param name="prototype">The prototype to use.</param>
 public ConsoleObject(ScriptObject prototype)
     : base(prototype)
 {
 }
Exemplo n.º 28
0
 public static ScriptObject ToObject(Environment env, ScriptObject o)
 {
     return o;
 }
Exemplo n.º 29
0
 /// <summary>
 /// Creates a new inline group, indenting all following output by another level; unlike group(),
 /// this starts with the inline group collapsed, requiring the use of a disclosure button to
 /// expand it. To move back out a level, call groupEnd()
 /// </summary>
 /// <param name="ctx">The function context.</param>
 /// <param name="instance">The console object instance.</param>
 /// <param name="eventName">The name of the event.</param>
 /// <param name="eventValue">The value of the event.</param>
 internal static void GroupCollapsed(FunctionObject ctx, ScriptObject instance, BoxedValue eventValue)
 {
     ConsoleObject.SendEvent("groupCollapsed", eventValue);
 }
Exemplo n.º 30
0
 public static bool ToBoolean(ScriptObject o)
 {
     return true;
 }