Default implemetation of toSource now lives in ScriptRuntime to allow its

reuse in othr objects.
This commit is contained in:
igor%mir2.org 2004-06-08 23:28:32 +00:00
parent 7d0180067b
commit ec1dd303f9
2 changed files with 68 additions and 66 deletions

View File

@ -92,7 +92,9 @@ final class NativeObjectPrototype extends NativeObject
case Id_toLocaleString: // For now just alias toString
case Id_toString: {
if (cx.hasFeature(Context.FEATURE_TO_STRING_AS_SOURCE)) {
String s = toSource(cx, scope, thisObj, args);
String s = ScriptRuntime.defaultObjectToSource(cx, scope,
thisObj,
args);
int L = s.length();
if (L != 0 && s.charAt(0) == '(' && s.charAt(L - 1) == ')')
{
@ -145,75 +147,12 @@ final class NativeObjectPrototype extends NativeObject
}
case Id_toSource:
return toSource(cx, scope, thisObj, args);
return ScriptRuntime.defaultObjectToSource(cx, scope, thisObj,
args);
}
return super.execMethod(f, cx, scope, thisObj, args);
}
private static String toSource(Context cx, Scriptable scope,
Scriptable thisObj, Object[] args)
throws JavaScriptException
{
boolean toplevel, iterating;
if (cx.iterating == null) {
toplevel = true;
iterating = false;
cx.iterating = new ObjToIntMap(31);
} else {
toplevel = false;
iterating = cx.iterating.has(thisObj);
}
StringBuffer result = new StringBuffer(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++) {
if (i > 0)
result.append(", ");
Object id = ids[i];
Object value;
if (id instanceof Integer) {
int intId = ((Integer)id).intValue();
value = thisObj.get(intId, thisObj);
result.append(intId);
} else {
String strId = (String)id;
value = thisObj.get(strId, thisObj);
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();
}
protected String getIdName(int id)
{
switch (id) {

View File

@ -624,6 +624,69 @@ public class ScriptRuntime {
throw Kit.badTypeJS(value);
}
static String defaultObjectToSource(Context cx, Scriptable scope,
Scriptable thisObj, Object[] args)
{
boolean toplevel, iterating;
if (cx.iterating == null) {
toplevel = true;
iterating = false;
cx.iterating = new ObjToIntMap(31);
} else {
toplevel = false;
iterating = cx.iterating.has(thisObj);
}
StringBuffer result = new StringBuffer(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++) {
if (i > 0)
result.append(", ");
Object id = ids[i];
Object value;
if (id instanceof Integer) {
int intId = ((Integer)id).intValue();
value = thisObj.get(intId, thisObj);
result.append(intId);
} else {
String strId = (String)id;
value = thisObj.get(strId, thisObj);
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();
}
public static Scriptable toObject(Scriptable scope, Object val)
{
if (val instanceof Scriptable && val != Undefined.instance) {