GetElements() public method

Get the elements of a JavaScript array.
Get the elements of a JavaScript array.

If the object defines a length property convertible to double number, then the number is converted Uint32 value as defined in Ecma 9.6 and Java array of that size is allocated. The array is initialized with the values obtained by calling get() on object for each value of i in [0,length-1]. If there is not a defined value for a property the Undefined value is used to initialize the corresponding element in the array. The Java array is then returned. If the object doesn't define a length property or it is not a number, empty array is returned.

public GetElements ( Scriptable @object ) : object[]
@object Scriptable
return object[]
Example #1
0
		/// <summary>
		/// Execute the specified command with the given argument and options
		/// as a separate process and return the exit status of the process.
		/// </summary>
		/// <remarks>
		/// Execute the specified command with the given argument and options
		/// as a separate process and return the exit status of the process.
		/// <p>
		/// Usage:
		/// <pre>
		/// runCommand(command)
		/// runCommand(command, arg1, ..., argN)
		/// runCommand(command, arg1, ..., argN, options)
		/// </pre>
		/// All except the last arguments to runCommand are converted to strings
		/// and denote command name and its arguments. If the last argument is a
		/// JavaScript object, it is an option object. Otherwise it is converted to
		/// string denoting the last argument and options objects assumed to be
		/// empty.
		/// The following properties of the option object are processed:
		/// <ul>
		/// <li><tt>args</tt> - provides an array of additional command arguments
		/// <li><tt>env</tt> - explicit environment object. All its enumerable
		/// properties define the corresponding environment variable names.
		/// <li><tt>input</tt> - the process input. If it is not
		/// java.io.InputStream, it is converted to string and sent to the process
		/// as its input. If not specified, no input is provided to the process.
		/// <li><tt>output</tt> - the process output instead of
		/// java.lang.System.out. If it is not instance of java.io.OutputStream,
		/// the process output is read, converted to a string, appended to the
		/// output property value converted to string and put as the new value of
		/// the output property.
		/// <li><tt>err</tt> - the process error output instead of
		/// java.lang.System.err. If it is not instance of java.io.OutputStream,
		/// the process error output is read, converted to a string, appended to
		/// the err property value converted to string and put as the new
		/// value of the err property.
		/// </ul>
		/// </remarks>
		/// <exception cref="System.IO.IOException"></exception>
		public static object RunCommand(Context cx, Scriptable thisObj, object[] args, Function funObj)
		{
			int L = args.Length;
			if (L == 0 || (L == 1 && args[0] is Scriptable))
			{
				throw ReportRuntimeError("msg.runCommand.bad.args");
			}
			Stream @in = null;
			Stream @out = null;
			Stream err = null;
			MemoryStream outBytes = null;
			MemoryStream errBytes = null;
			object outObj = null;
			object errObj = null;
			string[] environment = null;
			Scriptable @params = null;
			object[] addArgs = null;
			if (args[L - 1] is Scriptable)
			{
				@params = (Scriptable)args[L - 1];
				--L;
				object envObj = ScriptableObject.GetProperty(@params, "env");
				if (envObj != ScriptableConstants.NOT_FOUND)
				{
					if (envObj == null)
					{
						environment = new string[0];
					}
					else
					{
						if (!(envObj is Scriptable))
						{
							throw ReportRuntimeError("msg.runCommand.bad.env");
						}
						Scriptable envHash = (Scriptable)envObj;
						object[] ids = ScriptableObject.GetPropertyIds(envHash);
						environment = new string[ids.Length];
						for (int i = 0; i != ids.Length; ++i)
						{
							object keyObj = ids[i];
							object val;
							string key;
							if (keyObj is string)
							{
								key = (string)keyObj;
								val = ScriptableObject.GetProperty(envHash, key);
							}
							else
							{
								int ikey = System.Convert.ToInt32(((Number)keyObj));
								key = Sharpen.Extensions.ToString(ikey);
								val = ScriptableObject.GetProperty(envHash, ikey);
							}
							if (val == ScriptableObject.NOT_FOUND)
							{
								val = Undefined.instance;
							}
							environment[i] = key + '=' + ScriptRuntime.ToString(val);
						}
					}
				}
				object inObj = ScriptableObject.GetProperty(@params, "input");
				if (inObj != ScriptableConstants.NOT_FOUND)
				{
					@in = ToInputStream(inObj);
				}
				outObj = ScriptableObject.GetProperty(@params, "output");
				if (outObj != ScriptableConstants.NOT_FOUND)
				{
					@out = ToOutputStream(outObj);
					if (@out == null)
					{
						outBytes = new MemoryStream();
						@out = outBytes;
					}
				}
				errObj = ScriptableObject.GetProperty(@params, "err");
				if (errObj != ScriptableConstants.NOT_FOUND)
				{
					err = ToOutputStream(errObj);
					if (err == null)
					{
						errBytes = new MemoryStream();
						err = errBytes;
					}
				}
				object addArgsObj = ScriptableObject.GetProperty(@params, "args");
				if (addArgsObj != ScriptableConstants.NOT_FOUND)
				{
					Scriptable s = Context.ToObject(addArgsObj, GetTopLevelScope(thisObj));
					addArgs = cx.GetElements(s);
				}
			}
			Rhino.Tools.Shell.Global global = GetInstance(funObj);
			if (@out == null)
			{
				@out = (global != null) ? global.GetOut() : System.Console.Out;
			}
			if (err == null)
			{
				err = (global != null) ? global.GetErr() : System.Console.Error;
			}
			// If no explicit input stream, do not send any input to process,
			// in particular, do not use System.in to avoid deadlocks
			// when waiting for user input to send to process which is already
			// terminated as it is not always possible to interrupt read method.
			string[] cmd = new string[(addArgs == null) ? L : L + addArgs.Length];
			for (int i_1 = 0; i_1 != L; ++i_1)
			{
				cmd[i_1] = ScriptRuntime.ToString(args[i_1]);
			}
			if (addArgs != null)
			{
				for (int i = 0; i_1 != addArgs.Length; ++i_1)
				{
					cmd[L + i_1] = ScriptRuntime.ToString(addArgs[i_1]);
				}
			}
			int exitCode = RunProcess(cmd, environment, @in, @out, err);
			if (outBytes != null)
			{
				string s = ScriptRuntime.ToString(outObj) + outBytes.ToString();
				ScriptableObject.PutProperty(@params, "output", s);
			}
			if (errBytes != null)
			{
				string s = ScriptRuntime.ToString(errObj) + errBytes.ToString();
				ScriptableObject.PutProperty(@params, "err", s);
			}
			return exitCode;
		}
Example #2
0
		internal static object[] GetApplyArguments(Context cx, object arg1)
		{
			if (arg1 == null || arg1 == Undefined.instance)
			{
				return ScriptRuntime.emptyArgs;
			}
			else
			{
				if (arg1 is NativeArray || arg1 is Arguments)
				{
					return cx.GetElements((Scriptable)arg1);
				}
				else
				{
					throw ScriptRuntime.TypeError0("msg.arg.isnt.array");
				}
			}
		}
Example #3
0
		/// <summary>
		/// The spawn function runs a given function or script in a different
		/// thread.
		/// </summary>
		/// <remarks>
		/// The spawn function runs a given function or script in a different
		/// thread.
		/// js&gt; function g() { a = 7; }
		/// js&gt; a = 3;
		/// 3
		/// js&gt; spawn(g)
		/// Thread[Thread-1,5,main]
		/// js&gt; a
		/// 3
		/// </remarks>
		public static object Spawn(Context cx, Scriptable thisObj, object[] args, Function funObj)
		{
			Scriptable scope = funObj.GetParentScope();
			Runner runner;
			if (args.Length != 0 && args[0] is Function)
			{
				object[] newArgs = null;
				if (args.Length > 1 && args[1] is Scriptable)
				{
					newArgs = cx.GetElements((Scriptable)args[1]);
				}
				if (newArgs == null)
				{
					newArgs = ScriptRuntime.emptyArgs;
				}
				runner = new Runner(scope, (Function)args[0], newArgs);
			}
			else
			{
				if (args.Length != 0 && args[0] is Script)
				{
					runner = new Runner(scope, (Script)args[0]);
				}
				else
				{
					throw ReportRuntimeError("msg.spawn.args");
				}
			}
			runner.factory = cx.GetFactory();
			Sharpen.Thread thread = new Sharpen.Thread(runner);
			thread.Start();
			return thread;
		}