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); }
public static Scriptable NewCatchScope(Exception t, Scriptable lastCatchScope, string exceptionName, Context cx, Scriptable scope) { object obj; bool cacheObj; if (t is JavaScriptException) { cacheObj = false; obj = ((JavaScriptException)t).GetValue(); } else { cacheObj = true; // Create wrapper object unless it was associated with // the previous scope object if (lastCatchScope != null) { NativeObject last = (NativeObject)lastCatchScope; obj = last.GetAssociatedValue(t); if (obj == null) { Kit.CodeBug(); } } else { obj = WrapException(t, scope, cx); } } NativeObject catchScopeObject = new NativeObject(); // See ECMA 12.4 catchScopeObject.DefineProperty(exceptionName, obj, ScriptableObject.PERMANENT); if (IsVisible(cx, t)) { // Add special Rhino object __exception__ defined in the catch // scope that can be used to retrieve the Java exception associated // with the JavaScript exception (to get stack trace info, etc.) catchScopeObject.DefineProperty("__exception__", Context.JavaToJS(t, scope), ScriptableObject.PERMANENT | ScriptableObject.DONTENUM); } if (cacheObj) { catchScopeObject.AssociateValue(t, obj); } return catchScopeObject; }
private object SetupDefaultPrototype() { lock (this) { if (prototypeProperty != null) { return prototypeProperty; } NativeObject obj = new NativeObject(); int attr = ScriptableObject.DONTENUM; obj.DefineProperty("constructor", this, attr); // put the prototype property into the object now, then in the // wacky case of a user defining a function Object(), we don't // get an infinite loop trying to find the prototype. prototypeProperty = obj; Scriptable proto = GetObjectPrototype(this); if (proto != obj) { // not the one we just made, it must remain grounded obj.SetPrototype(proto); } return obj; } }