Beispiel #1
0
		internal static string DefaultObjectToSource(Context cx, Scriptable scope, Scriptable thisObj, object[] args)
		{
			bool toplevel;
			bool iterating;
			if (cx.iterating == null)
			{
				toplevel = true;
				iterating = false;
				cx.iterating = new ObjToIntMap(31);
			}
			else
			{
				toplevel = false;
				iterating = cx.iterating.Has(thisObj);
			}
			StringBuilder result = new StringBuilder(128);
			if (toplevel)
			{
				result.Append("(");
			}
			result.Append('{');
			// Make sure cx.iterating is set to null when done
			// so we don't leak memory
			try
			{
				if (!iterating)
				{
					cx.iterating.Intern(thisObj);
					// stop recursion.
					object[] ids = thisObj.GetIds();
					for (int i = 0; i < ids.Length; i++)
					{
						object id = ids[i];
						object value;
						if (id is int)
						{
							int intId = System.Convert.ToInt32(((int)id));
							value = thisObj.Get(intId, thisObj);
							if (value == ScriptableConstants.NOT_FOUND)
							{
								continue;
							}
							// a property has been removed
							if (i > 0)
							{
								result.Append(", ");
							}
							result.Append(intId);
						}
						else
						{
							string strId = (string)id;
							value = thisObj.Get(strId, thisObj);
							if (value == ScriptableConstants.NOT_FOUND)
							{
								continue;
							}
							// a property has been removed
							if (i > 0)
							{
								result.Append(", ");
							}
							if (ScriptRuntime.IsValidIdentifierName(strId))
							{
								result.Append(strId);
							}
							else
							{
								result.Append('\'');
								result.Append(ScriptRuntime.EscapeString(strId, '\''));
								result.Append('\'');
							}
						}
						result.Append(':');
						result.Append(ScriptRuntime.Uneval(cx, scope, value));
					}
				}
			}
			finally
			{
				if (toplevel)
				{
					cx.iterating = null;
				}
			}
			result.Append('}');
			if (toplevel)
			{
				result.Append(')');
			}
			return result.ToString();
		}
Beispiel #2
0
		private static string Jo(Scriptable value, NativeJSON.StringifyState state)
		{
			if (state.stack.Search(value) != -1)
			{
				throw ScriptRuntime.TypeError0("msg.cyclic.value");
			}
			state.stack.Push(value);
			string stepback = state.indent;
			state.indent = state.indent + state.gap;
			object[] k = null;
			if (state.propertyList != null)
			{
				k = Sharpen.Collections.ToArray(state.propertyList);
			}
			else
			{
				k = value.GetIds();
			}
			IList<object> partial = new List<object>();
			foreach (object p in k)
			{
				object strP = Str(p, value, state);
				if (strP != Undefined.instance)
				{
					string member = Quote(p.ToString()) + ":";
					if (state.gap.Length > 0)
					{
						member = member + " ";
					}
					member = member + strP;
					partial.Add(member);
				}
			}
			string finalValue;
			if (partial.IsEmpty())
			{
				finalValue = "{}";
			}
			else
			{
				if (state.gap.Length == 0)
				{
					finalValue = '{' + Join(partial, ",") + '}';
				}
				else
				{
					string separator = ",\n" + state.indent;
					string properties = Join(partial, separator);
					finalValue = "{\n" + state.indent + properties + '\n' + stepback + '}';
				}
			}
			state.stack.Pop();
			state.indent = stepback;
			return finalValue;
		}