public static object Stringify(Context cx, Scriptable scope, object value, object replacer, object space)
{
string indent = string.Empty;
string gap = string.Empty;
IList<object> propertyList = null;
Callable replacerFunction = null;
if (replacer is Callable)
{
replacerFunction = (Callable)replacer;
}
else
{
if (replacer is NativeArray)
{
propertyList = new List<object>();
NativeArray replacerArray = (NativeArray)replacer;
foreach (int i in replacerArray.GetIndexIds())
{
object v = replacerArray.Get(i, replacerArray);
if (v is string || v is Number)
{
propertyList.Add(v);
}
else
{
if (v is NativeString || v is NativeNumber)
{
propertyList.Add(ScriptRuntime.ToString(v));
}
}
}
}
}
if (space is NativeNumber)
{
space = ScriptRuntime.ToNumber(space);
}
else
{
if (space is NativeString)
{
space = ScriptRuntime.ToString(space);
}
}
if (space is Number)
{
int gapLength = (int)ScriptRuntime.ToInteger(space);
gapLength = Math.Min(MAX_STRINGIFY_GAP_LENGTH, gapLength);
gap = (gapLength > 0) ? Repeat(' ', gapLength) : string.Empty;
space = gapLength;
}
else
{
if (space is string)
{
gap = (string)space;
if (gap.Length > MAX_STRINGIFY_GAP_LENGTH)
{
gap = Sharpen.Runtime.Substring(gap, 0, MAX_STRINGIFY_GAP_LENGTH);
}
}
}
NativeJSON.StringifyState state = new NativeJSON.StringifyState(cx, scope, indent, gap, replacerFunction, propertyList, space);
ScriptableObject wrapper = new NativeObject();
wrapper.SetParentScope(scope);
wrapper.SetPrototype(ScriptableObject.GetObjectPrototype(scope));
wrapper.DefineProperty(string.Empty, value, 0);
return Str(string.Empty, wrapper, state);
}