NewArray() public method

Create an array with a specified initial length.
Create an array with a specified initial length.

public NewArray ( Scriptable scope, int length ) : Scriptable
scope Scriptable the scope to create the object in
length int /// the initial length (JavaScript arrays may have /// additional properties added dynamically). ///
return Scriptable
Example #1
0
		public override object ExecIdCall(IdFunctionObject f, Context cx, Scriptable scope, Scriptable thisObj, object[] args)
		{
			if (!f.HasTag(XMLOBJECT_TAG))
			{
				return base.ExecIdCall(f, cx, scope, thisObj, args);
			}
			int id = f.MethodId();
			if (id == Id_constructor)
			{
				return JsConstructor(cx, thisObj == null, args);
			}
			// All (XML|XMLList).prototype methods require thisObj to be XML
			if (!(thisObj is Rhino.Xmlimpl.XMLObjectImpl))
			{
				throw IncompatibleCallError(f);
			}
			Rhino.Xmlimpl.XMLObjectImpl realThis = (Rhino.Xmlimpl.XMLObjectImpl)thisObj;
			XML xml = realThis.GetXML();
			switch (id)
			{
				case Id_appendChild:
				{
					if (xml == null)
					{
						XmlMethodNotFound(realThis, "appendChild");
					}
					return xml.AppendChild(Arg(args, 0));
				}

				case Id_addNamespace:
				{
					if (xml == null)
					{
						XmlMethodNotFound(realThis, "addNamespace");
					}
					Namespace ns = lib.CastToNamespace(cx, Arg(args, 0));
					return xml.AddNamespace(ns);
				}

				case Id_childIndex:
				{
					if (xml == null)
					{
						XmlMethodNotFound(realThis, "childIndex");
					}
					return ScriptRuntime.WrapInt(xml.ChildIndex());
				}

				case Id_inScopeNamespaces:
				{
					if (xml == null)
					{
						XmlMethodNotFound(realThis, "inScopeNamespaces");
					}
					return cx.NewArray(scope, ToObjectArray(xml.InScopeNamespaces()));
				}

				case Id_insertChildAfter:
				{
					if (xml == null)
					{
						XmlMethodNotFound(realThis, "insertChildAfter");
					}
					object arg0 = Arg(args, 0);
					if (arg0 == null || arg0 is XML)
					{
						return xml.InsertChildAfter((XML)arg0, Arg(args, 1));
					}
					return Undefined.instance;
				}

				case Id_insertChildBefore:
				{
					if (xml == null)
					{
						XmlMethodNotFound(realThis, "insertChildBefore");
					}
					object arg0 = Arg(args, 0);
					if (arg0 == null || arg0 is XML)
					{
						return xml.InsertChildBefore((XML)arg0, Arg(args, 1));
					}
					return Undefined.instance;
				}

				case Id_localName:
				{
					if (xml == null)
					{
						XmlMethodNotFound(realThis, "localName");
					}
					return xml.LocalName();
				}

				case Id_name:
				{
					if (xml == null)
					{
						XmlMethodNotFound(realThis, "name");
					}
					return xml.Name();
				}

				case Id_namespace:
				{
					if (xml == null)
					{
						XmlMethodNotFound(realThis, "namespace");
					}
					string prefix = (args.Length > 0) ? ScriptRuntime.ToString(args[0]) : null;
					Namespace rv = xml.Namespace(prefix);
					if (rv == null)
					{
						return Undefined.instance;
					}
					else
					{
						return rv;
					}
					goto case Id_namespaceDeclarations;
				}

				case Id_namespaceDeclarations:
				{
					if (xml == null)
					{
						XmlMethodNotFound(realThis, "namespaceDeclarations");
					}
					Namespace[] array = xml.NamespaceDeclarations();
					return cx.NewArray(scope, ToObjectArray(array));
				}

				case Id_nodeKind:
				{
					if (xml == null)
					{
						XmlMethodNotFound(realThis, "nodeKind");
					}
					return xml.NodeKind();
				}

				case Id_prependChild:
				{
					if (xml == null)
					{
						XmlMethodNotFound(realThis, "prependChild");
					}
					return xml.PrependChild(Arg(args, 0));
				}

				case Id_removeNamespace:
				{
					if (xml == null)
					{
						XmlMethodNotFound(realThis, "removeNamespace");
					}
					Namespace ns = lib.CastToNamespace(cx, Arg(args, 0));
					return xml.RemoveNamespace(ns);
				}

				case Id_replace:
				{
					if (xml == null)
					{
						XmlMethodNotFound(realThis, "replace");
					}
					XMLName xmlName = lib.ToXMLNameOrIndex(cx, Arg(args, 0));
					object arg1 = Arg(args, 1);
					if (xmlName == null)
					{
						//    I refuse to believe that this number will exceed 2^31
						int index = (int)ScriptRuntime.LastUint32Result(cx);
						return xml.Replace(index, arg1);
					}
					else
					{
						return xml.Replace(xmlName, arg1);
					}
					goto case Id_setChildren;
				}

				case Id_setChildren:
				{
					if (xml == null)
					{
						XmlMethodNotFound(realThis, "setChildren");
					}
					return xml.SetChildren(Arg(args, 0));
				}

				case Id_setLocalName:
				{
					if (xml == null)
					{
						XmlMethodNotFound(realThis, "setLocalName");
					}
					string localName;
					object arg = Arg(args, 0);
					if (arg is QName)
					{
						localName = ((QName)arg).LocalName();
					}
					else
					{
						localName = ScriptRuntime.ToString(arg);
					}
					xml.SetLocalName(localName);
					return Undefined.instance;
				}

				case Id_setName:
				{
					if (xml == null)
					{
						XmlMethodNotFound(realThis, "setName");
					}
					object arg = (args.Length != 0) ? args[0] : Undefined.instance;
					QName qname = lib.ConstructQName(cx, arg);
					xml.SetName(qname);
					return Undefined.instance;
				}

				case Id_setNamespace:
				{
					if (xml == null)
					{
						XmlMethodNotFound(realThis, "setNamespace");
					}
					Namespace ns = lib.CastToNamespace(cx, Arg(args, 0));
					xml.SetNamespace(ns);
					return Undefined.instance;
				}

				case Id_attribute:
				{
					XMLName xmlName = XMLName.Create(lib.ToNodeQName(cx, Arg(args, 0), true), true, false);
					return realThis.GetMatches(xmlName);
				}

				case Id_attributes:
				{
					return realThis.GetMatches(XMLName.Create(Rhino.Xmlimpl.XmlNode.QName.Create(null, null), true, false));
				}

				case Id_child:
				{
					XMLName xmlName = lib.ToXMLNameOrIndex(cx, Arg(args, 0));
					if (xmlName == null)
					{
						//    Two billion or so is a fine upper limit, so we cast to int
						int index = (int)ScriptRuntime.LastUint32Result(cx);
						return realThis.Child(index);
					}
					else
					{
						return realThis.Child(xmlName);
					}
					goto case Id_children;
				}

				case Id_children:
				{
					return realThis.Children();
				}

				case Id_comments:
				{
					return realThis.Comments();
				}

				case Id_contains:
				{
					return ScriptRuntime.WrapBoolean(realThis.Contains(Arg(args, 0)));
				}

				case Id_copy:
				{
					return realThis.Copy();
				}

				case Id_descendants:
				{
					Rhino.Xmlimpl.XmlNode.QName qname = (args.Length == 0) ? Rhino.Xmlimpl.XmlNode.QName.Create(null, null) : lib.ToNodeQName(cx, args[0], false);
					return realThis.GetMatches(XMLName.Create(qname, false, true));
				}

				case Id_elements:
				{
					XMLName xmlName = (args.Length == 0) ? XMLName.FormStar() : lib.ToXMLName(cx, args[0]);
					return realThis.Elements(xmlName);
				}

				case Id_hasOwnProperty:
				{
					XMLName xmlName = lib.ToXMLName(cx, Arg(args, 0));
					return ScriptRuntime.WrapBoolean(realThis.HasOwnProperty(xmlName));
				}

				case Id_hasComplexContent:
				{
					return ScriptRuntime.WrapBoolean(realThis.HasComplexContent());
				}

				case Id_hasSimpleContent:
				{
					return ScriptRuntime.WrapBoolean(realThis.HasSimpleContent());
				}

				case Id_length:
				{
					return ScriptRuntime.WrapInt(realThis.Length());
				}

				case Id_normalize:
				{
					realThis.Normalize();
					return Undefined.instance;
				}

				case Id_parent:
				{
					return realThis.Parent();
				}

				case Id_processingInstructions:
				{
					XMLName xmlName = (args.Length > 0) ? lib.ToXMLName(cx, args[0]) : XMLName.FormStar();
					return realThis.ProcessingInstructions(xmlName);
				}

				case Id_propertyIsEnumerable:
				{
					return ScriptRuntime.WrapBoolean(realThis.PropertyIsEnumerable(Arg(args, 0)));
				}

				case Id_text:
				{
					return realThis.Text();
				}

				case Id_toString:
				{
					return realThis.ToString();
				}

				case Id_toSource:
				{
					int indent = ScriptRuntime.ToInt32(args, 0);
					return realThis.ToSource(indent);
				}

				case Id_toXMLString:
				{
					return realThis.ToXMLString();
				}

				case Id_valueOf:
				{
					return realThis.ValueOf();
				}
			}
			throw new ArgumentException(id.ToString());
		}
Example #2
0
		public static object EnumId(object enumObj, Context cx)
		{
			ScriptRuntime.IdEnumeration x = (ScriptRuntime.IdEnumeration)enumObj;
			if (x.iterator != null)
			{
				return x.currentId;
			}
			switch (x.enumType)
			{
				case ENUMERATE_KEYS:
				case ENUMERATE_KEYS_NO_ITERATOR:
				{
					return x.currentId;
				}

				case ENUMERATE_VALUES:
				case ENUMERATE_VALUES_NO_ITERATOR:
				{
					return EnumValue(enumObj, cx);
				}

				case ENUMERATE_ARRAY:
				case ENUMERATE_ARRAY_NO_ITERATOR:
				{
					object[] elements = new object[] { x.currentId, EnumValue(enumObj, cx) };
					return cx.NewArray(ScriptableObject.GetTopLevelScope(x.obj), elements);
				}

				default:
				{
					throw Kit.CodeBug();
				}
			}
		}
Example #3
0
		public static Scriptable NewArrayLiteral(object[] objects, int[] skipIndices, Context cx, Scriptable scope)
		{
			int SKIP_DENSITY = 2;
			int count = objects.Length;
			int skipCount = 0;
			if (skipIndices != null)
			{
				skipCount = skipIndices.Length;
			}
			int length = count + skipCount;
			if (length > 1 && skipCount * SKIP_DENSITY < length)
			{
				// If not too sparse, create whole array for constructor
				object[] sparse;
				if (skipCount == 0)
				{
					sparse = objects;
				}
				else
				{
					sparse = new object[length];
					int skip = 0;
					for (int i = 0, j = 0; i != length; ++i)
					{
						if (skip != skipCount && skipIndices[skip] == i)
						{
							sparse[i] = ScriptableConstants.NOT_FOUND;
							++skip;
							continue;
						}
						sparse[i] = objects[j];
						++j;
					}
				}
				return cx.NewArray(scope, sparse);
			}
			Scriptable array = cx.NewArray(scope, length);
			int skip_1 = 0;
			for (int i_1 = 0, j_1 = 0; i_1 != length; ++i_1)
			{
				if (skip_1 != skipCount && skipIndices[skip_1] == i_1)
				{
					++skip_1;
					continue;
				}
				ScriptableObject.PutProperty(array, i_1, objects[j_1]);
				++j_1;
			}
			return array;
		}
Example #4
0
		/// <summary>Creates a new instance of the require() function.</summary>
		/// <remarks>
		/// Creates a new instance of the require() function. Upon constructing it,
		/// you will either want to install it in the global (or some other) scope
		/// using
		/// <see cref="Install(Rhino.Scriptable)">Install(Rhino.Scriptable)</see>
		/// , or alternatively, you can load the
		/// program's main module using
		/// <see cref="RequireMain(Rhino.Context, string)">RequireMain(Rhino.Context, string)</see>
		/// and
		/// then act on the main module's exports.
		/// </remarks>
		/// <param name="cx">the current context</param>
		/// <param name="nativeScope">
		/// a scope that provides the standard native JavaScript
		/// objects.
		/// </param>
		/// <param name="moduleScriptProvider">a provider for module scripts</param>
		/// <param name="preExec">
		/// an optional script that is executed in every module's
		/// scope before its module script is run.
		/// </param>
		/// <param name="postExec">
		/// an optional script that is executed in every module's
		/// scope after its module script is run.
		/// </param>
		/// <param name="sandboxed">
		/// if set to true, the require function will be sandboxed.
		/// This means that it doesn't have the "paths" property, and also that the
		/// modules it loads don't export the "module.uri" property.
		/// </param>
		public Require(Context cx, Scriptable nativeScope, ModuleScriptProvider moduleScriptProvider, Script preExec, Script postExec, bool sandboxed)
		{
			// Modules that completed loading; visible to all threads
			// Modules currently being loaded on the thread. Used to resolve circular
			// dependencies while loading.
			this.moduleScriptProvider = moduleScriptProvider;
			this.nativeScope = nativeScope;
			this.sandboxed = sandboxed;
			this.preExec = preExec;
			this.postExec = postExec;
			SetPrototype(ScriptableObject.GetFunctionPrototype(nativeScope));
			if (!sandboxed)
			{
				paths = cx.NewArray(nativeScope, 0);
				DefineReadOnlyProperty(this, "paths", paths);
			}
			else
			{
				paths = null;
			}
		}
Example #5
0
		/// <summary>Call __noSuchMethod__.</summary>
		/// <remarks>Call __noSuchMethod__.</remarks>
		private static Interpreter.CallFrame InitFrameForNoSuchMethod(Context cx, Interpreter.CallFrame frame, int indexReg, object[] stack, double[] sDbl, int stackTop, int op, Scriptable funThisObj, Scriptable calleeScope, ScriptRuntime.NoSuchMethodShim noSuchMethodShim, InterpretedFunction ifun)
		{
			// create an args array from the stack
			object[] argsArray = null;
			// exactly like getArgsArray except that the first argument
			// is the method name from the shim
			int shift = stackTop + 2;
			object[] elements = new object[indexReg];
			for (int i = 0; i < indexReg; ++i, ++shift)
			{
				object val = stack[shift];
				if (val == UniqueTag.DOUBLE_MARK)
				{
					val = ScriptRuntime.WrapNumber(sDbl[shift]);
				}
				elements[i] = val;
			}
			argsArray = new object[2];
			argsArray[0] = noSuchMethodShim.methodName;
			argsArray[1] = cx.NewArray(calleeScope, elements);
			// exactly the same as if it's a regular InterpretedFunction
			Interpreter.CallFrame callParentFrame = frame;
			Interpreter.CallFrame calleeFrame = new Interpreter.CallFrame();
			if (op == Icode_TAIL_CALL)
			{
				callParentFrame = frame.parentFrame;
				ExitFrame(cx, frame, null);
			}
			// init the frame with the underlying method with the
			// adjusted args array and shim's function
			InitFrame(cx, calleeScope, funThisObj, argsArray, null, 0, 2, ifun, callParentFrame, calleeFrame);
			if (op != Icode_TAIL_CALL)
			{
				frame.savedStackTop = stackTop;
				frame.savedCallOp = op;
			}
			return calleeFrame;
		}
Example #6
0
		public virtual void Init(Context cx)
		{
			// Define some global functions particular to the shell. Note
			// that these functions are not part of ECMA.
			InitStandardObjects(cx, sealedStdLib);
			string[] names = new string[] { "defineClass", "deserialize", "doctest", "gc", "help", "load", "loadClass", "print", "quit", "readFile", "readUrl", "runCommand", "seal", "serialize", "spawn", "sync", "toint32", "version" };
			DefineFunctionProperties(names, typeof(Rhino.Tools.Shell.Global), ScriptableObject.DONTENUM);
			// Set up "environment" in the global scope to provide access to the
			// System environment variables.
			Environment.DefineClass(this);
			Environment environment = new Environment(this);
			DefineProperty("environment", environment, ScriptableObject.DONTENUM);
			history = (NativeArray)cx.NewArray(this, 0);
			DefineProperty("history", history, ScriptableObject.DONTENUM);
			initialized = true;
		}
Example #7
0
		public virtual object Js_split(Context cx, Scriptable scope, string target, object[] args)
		{
			// create an empty Array to return;
			Scriptable result = cx.NewArray(scope, 0);
			// return an array consisting of the target if no separator given
			// don't check against undefined, because we want
			// 'fooundefinedbar'.split(void 0) to split to ['foo', 'bar']
			if (args.Length < 1)
			{
				result.Put(0, result, target);
				return result;
			}
			// Use the second argument as the split limit, if given.
			bool limited = (args.Length > 1) && (args[1] != Undefined.instance);
			long limit = 0;
			// Initialize to avoid warning.
			if (limited)
			{
				limit = ScriptRuntime.ToUint32(args[1]);
				if (limit > target.Length)
				{
					limit = 1 + target.Length;
				}
			}
			string separator = null;
			int[] matchlen = new int[1];
			Scriptable re = null;
			RegExpProxy reProxy = null;
			if (args[0] is Scriptable)
			{
				reProxy = ScriptRuntime.GetRegExpProxy(cx);
				if (reProxy != null)
				{
					Scriptable test = (Scriptable)args[0];
					if (reProxy.IsRegExp(test))
					{
						re = test;
					}
				}
			}
			if (re == null)
			{
				separator = ScriptRuntime.ToString(args[0]);
				matchlen[0] = separator.Length;
			}
			// split target with separator or re
			int[] ip = new int[] { 0 };
			int match;
			int len = 0;
			bool[] matched = new bool[] { false };
			string[][] parens = new string[][] { null };
			int version = cx.GetLanguageVersion();
			while ((match = Find_split(cx, scope, target, separator, version, reProxy, re, ip, matchlen, matched, parens)) >= 0)
			{
				if ((limited && len >= limit) || (match > target.Length))
				{
					break;
				}
				string substr;
				if (target.Length == 0)
				{
					substr = target;
				}
				else
				{
					substr = Sharpen.Runtime.Substring(target, ip[0], match);
				}
				result.Put(len, result, substr);
				len++;
				if (re != null && matched[0] == true)
				{
					int size = parens[0].Length;
					for (int num = 0; num < size; num++)
					{
						if (limited && len >= limit)
						{
							break;
						}
						result.Put(len, result, parens[0][num]);
						len++;
					}
					matched[0] = false;
				}
				ip[0] = match + matchlen[0];
				if (version < Context.VERSION_1_3 && version != Context.VERSION_DEFAULT)
				{
					if (!limited && ip[0] == target.Length)
					{
						break;
					}
				}
			}
			return result;
		}
Example #8
0
		private static void Match_glob(GlobData mdata, Context cx, Scriptable scope, int count, RegExpImpl reImpl)
		{
			if (mdata.arrayobj == null)
			{
				mdata.arrayobj = cx.NewArray(scope, 0);
			}
			SubString matchsub = reImpl.lastMatch;
			string matchstr = matchsub.ToString();
			mdata.arrayobj.Put(count, mdata.arrayobj, matchstr);
		}
Example #9
0
		internal virtual object ExecuteRegExp(Context cx, Scriptable scope, RegExpImpl res, string str, int[] indexp, int matchType)
		{
			REGlobalData gData = new REGlobalData();
			int start = indexp[0];
			int end = str.Length;
			if (start > end)
			{
				start = end;
			}
			//
			// Call the recursive matcher to do the real work.
			//
			bool matches = MatchRegExp(gData, re, str, start, end, res.multiline);
			if (!matches)
			{
				if (matchType != PREFIX)
				{
					return null;
				}
				return Undefined.instance;
			}
			int index = gData.cp;
			int ep = indexp[0] = index;
			int matchlen = ep - (start + gData.skipped);
			index -= matchlen;
			object result;
			Scriptable obj;
			if (matchType == TEST)
			{
				result = true;
				obj = null;
			}
			else
			{
				result = cx.NewArray(scope, 0);
				obj = (Scriptable)result;
				string matchstr = Sharpen.Runtime.Substring(str, index, index + matchlen);
				obj.Put(0, obj, matchstr);
			}
			if (re.parenCount == 0)
			{
				res.parens = null;
				res.lastParen = SubString.emptySubString;
			}
			else
			{
				SubString parsub = null;
				int num;
				res.parens = new SubString[re.parenCount];
				for (num = 0; num < re.parenCount; num++)
				{
					int cap_index = gData.ParensIndex(num);
					string parstr;
					if (cap_index != -1)
					{
						int cap_length = gData.ParensLength(num);
						parsub = new SubString(str, cap_index, cap_length);
						res.parens[num] = parsub;
						if (matchType != TEST)
						{
							obj.Put(num + 1, obj, parsub.ToString());
						}
					}
					else
					{
						if (matchType != TEST)
						{
							obj.Put(num + 1, obj, Undefined.instance);
						}
					}
				}
				res.lastParen = parsub;
			}
			if (!(matchType == TEST))
			{
				obj.Put("index", obj, Sharpen.Extensions.ValueOf(start + gData.skipped));
				obj.Put("input", obj, str);
			}
			if (res.lastMatch == null)
			{
				res.lastMatch = new SubString();
				res.leftContext = new SubString();
				res.rightContext = new SubString();
			}
			res.lastMatch.str = str;
			res.lastMatch.index = index;
			res.lastMatch.length = matchlen;
			res.leftContext.str = str;
			if (cx.GetLanguageVersion() == Context.VERSION_1_2)
			{
				res.leftContext.index = start;
				res.leftContext.length = gData.skipped;
			}
			else
			{
				res.leftContext.index = 0;
				res.leftContext.length = start + gData.skipped;
			}
			res.rightContext.str = str;
			res.rightContext.index = ep;
			res.rightContext.length = end - ep;
			return result;
		}
Example #10
0
			public object Run(Context cx)
			{
				ScriptableObject global = ScriptRuntime.GetGlobal(cx);
				// get the command line arguments and define "arguments"
				// array in the top-level object
				object[] argsCopy = new object[args.Length];
				System.Array.Copy(args, 0, argsCopy, 0, args.Length);
				Scriptable argsObj = cx.NewArray(global, argsCopy);
				global.DefineProperty("arguments", argsObj, ScriptableObject.DONTENUM);
				script.Exec(cx, global);
				return null;
			}