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); }
/// <summary>Creates new script object.</summary> /// <remarks> /// Creates new script object. /// The default implementation of /// <see cref="Construct(Context, Scriptable, object[])">Construct(Context, Scriptable, object[])</see> /// uses the method to /// to get the value for <tt>thisObj</tt> argument when invoking /// <see cref="Call(Context, Scriptable, Scriptable, object[])">Call(Context, Scriptable, Scriptable, object[])</see> /// . /// The methos is allowed to return <tt>null</tt> to indicate that /// <see cref="Call(Context, Scriptable, Scriptable, object[])">Call(Context, Scriptable, Scriptable, object[])</see> /// will create a new object itself. In this case /// <see cref="Construct(Context, Scriptable, object[])">Construct(Context, Scriptable, object[])</see> /// will set scope and prototype on the result /// <see cref="Call(Context, Scriptable, Scriptable, object[])">Call(Context, Scriptable, Scriptable, object[])</see> /// unless they are already set. /// </remarks> public virtual Scriptable CreateObject(Context cx, Scriptable scope) { Scriptable newInstance = new NativeObject(); newInstance.SetPrototype(GetClassPrototype()); newInstance.SetParentScope(GetParentScope()); return newInstance; }