I added ScriptRuntime.setObjectProtoAndParent to initialize prototype and parent of objects created outside the standard constructor calls. It allowed to replace scattered setPrototype/setParentScope and shrink code size.

This commit is contained in:
igor%mir2.org 2004-09-26 14:36:01 +00:00
Родитель ad0943adef
Коммит f2e4f3318c
11 изменённых файлов: 80 добавлений и 74 удалений

Просмотреть файл

@ -1537,8 +1537,8 @@ public class Context
*/
public final Scriptable newArray(Scriptable scope, int length)
{
Scriptable result = new NativeArray(length);
newArrayHelper(scope, result);
NativeArray result = new NativeArray(length);
ScriptRuntime.setObjectProtoAndParent(result, scope);
return result;
}
@ -1556,8 +1556,8 @@ public class Context
{
if (elements.getClass().getComponentType() != ScriptRuntime.ObjectClass)
throw new IllegalArgumentException();
Scriptable result = new NativeArray(elements);
newArrayHelper(scope, result);
NativeArray result = new NativeArray(elements);
ScriptRuntime.setObjectProtoAndParent(result, scope);
return result;
}
@ -2427,14 +2427,6 @@ public class Context
return regExpProxy;
}
private void newArrayHelper(Scriptable scope, Scriptable array)
{
scope = ScriptableObject.getTopLevelScope(scope);
array.setParentScope(scope);
array.setPrototype(
ScriptableObject.getClassPrototype(scope, array.getClassName()));
}
final boolean isVersionECMA1()
{
return version == VERSION_DEFAULT || version >= VERSION_1_3;

Просмотреть файл

@ -184,7 +184,7 @@ public class FunctionObject extends BaseFunction
}
}
ScriptRuntime.setFunctionProtoAndParent(scope, this);
ScriptRuntime.setFunctionProtoAndParent(this, scope);
}
/**
@ -340,8 +340,9 @@ public class FunctionObject extends BaseFunction
* @see org.mozilla.javascript.Scriptable#setPrototype
* @see org.mozilla.javascript.Scriptable#getClassName
*/
public void addAsConstructor(Scriptable scope, Scriptable prototype) {
ScriptRuntime.setFunctionProtoAndParent(scope, this);
public void addAsConstructor(Scriptable scope, Scriptable prototype)
{
ScriptRuntime.setFunctionProtoAndParent(this, scope);
setImmunePrototypeProperty(prototype);
prototype.setParentScope(this);

Просмотреть файл

@ -3822,10 +3822,8 @@ switch (op) {
int stackTop)
{
Continuation c = new Continuation();
Scriptable topScope = ScriptRuntime.getTopCallScope(cx);
c.setParentScope(topScope);
c.setPrototype(
ScriptableObject.getClassPrototype(topScope, c.getClassName()));
ScriptRuntime.setObjectProtoAndParent(
c, ScriptRuntime.getTopCallScope(cx));
// Make sure that all frames upstack frames are frozen
CallFrame x = frame.parentFrame;

Просмотреть файл

@ -356,7 +356,7 @@ class JavaMembers
}
NativeJavaMethod fun = new NativeJavaMethod(methodBoxes);
if (scope != null) {
ScriptRuntime.setFunctionProtoAndParent(scope, fun);
ScriptRuntime.setFunctionProtoAndParent(fun, scope);
}
ht.put(name, fun);
}

Просмотреть файл

@ -66,7 +66,7 @@ public class NativeFunction extends BaseFunction
this.argCount = argCount;
this.version = version;
ScriptRuntime.setFunctionProtoAndParent(scope, this);
ScriptRuntime.setFunctionProtoAndParent(this, scope);
}
public final void initScriptObject(int version, String[] varNames)

Просмотреть файл

@ -108,7 +108,7 @@ public class NativeJavaPackage extends ScriptableObject {
// set up a name which is known to be a package so we don't
// need to look for a class by that name
void forcePackage(String name)
void forcePackage(String name, Scriptable scope)
{
NativeJavaPackage pkg;
int end = name.indexOf('.');
@ -125,12 +125,11 @@ public class NativeJavaPackage extends ScriptableObject {
? id
: packageName + "." + id;
pkg = new NativeJavaPackage(true, newPackage, classLoader);
pkg.setParentScope(this);
pkg.setPrototype(getPrototype());
ScriptRuntime.setObjectProtoAndParent(pkg, scope);
super.put(id, this, pkg);
}
if (end < name.length()) {
pkg.forcePackage(name.substring(end+1));
pkg.forcePackage(name.substring(end+1), scope);
}
}
@ -155,14 +154,16 @@ public class NativeJavaPackage extends ScriptableObject {
}
if (cl != null) {
newValue = new NativeJavaClass(getTopLevelScope(this), cl);
newValue.setPrototype(getPrototype());
}
}
if (newValue == null && createPkg) {
newValue = new NativeJavaPackage(true, className, classLoader);
NativeJavaPackage pkg;
pkg = new NativeJavaPackage(true, className, classLoader);
ScriptRuntime.setObjectProtoAndParent(pkg, getParentScope());
newValue = pkg;
}
if (newValue != null) {
newValue.setParentScope(this);
newValue.setPrototype(getPrototype());
// Make it available for fast lookup and sharing of
// lazily-reflected constructors and static members.
super.put(name, start, newValue);

Просмотреть файл

@ -111,7 +111,7 @@ public class NativeJavaTopPackage
String[] names = Kit.semicolonSplit(commonPackages);
for (int i = 0; i != names.length; ++i) {
top.forcePackage(names[i]);
top.forcePackage(names[i], scope);
}
// getClass implementation

Просмотреть файл

@ -138,8 +138,7 @@ class NativeScript extends NativeFunction
: ScriptRuntime.toString(args[0]);
Script script = compile(cx, source);
NativeScript nscript = new NativeScript(script);
nscript.setParentScope(scope);
nscript.setPrototype(getClassPrototype(scope, "Script"));
ScriptRuntime.setObjectProtoAndParent(nscript, scope);
return nscript;
}

Просмотреть файл

@ -3076,11 +3076,22 @@ public class ScriptRuntime {
return nw.getParentScope();
}
public static void setFunctionProtoAndParent(Scriptable scope,
BaseFunction fn)
public static void setFunctionProtoAndParent(BaseFunction fn,
Scriptable scope)
{
fn.setPrototype(ScriptableObject.getFunctionPrototype(scope));
fn.setParentScope(scope);
fn.setPrototype(ScriptableObject.getFunctionPrototype(scope));
}
public static void setObjectProtoAndParent(ScriptableObject object,
Scriptable scope)
{
// Compared with function it always sets the scope to top scope
scope = ScriptableObject.getTopLevelScope(scope);
object.setParentScope(scope);
Scriptable proto
= ScriptableObject.getClassPrototype(scope, object.getClassName());
object.setPrototype(proto);
}
public static void initFunction(Context cx, Scriptable scope,

Просмотреть файл

@ -141,7 +141,7 @@ public class NativeRegExp extends IdScriptableObject implements Function
NativeRegExpCtor ctor = new NativeRegExpCtor();
ScriptRuntime.setFunctionProtoAndParent(scope, ctor);
ScriptRuntime.setFunctionProtoAndParent(ctor, scope);
ctor.setImmunePrototypeProperty(proto);
@ -157,9 +157,7 @@ public class NativeRegExp extends IdScriptableObject implements Function
{
this.re = (RECompiled)regexpCompiled;
this.lastIndex = 0;
scope = getTopLevelScope(scope);
setPrototype(getClassPrototype(scope, "RegExp"));
setParentScope(scope);
ScriptRuntime.setObjectProtoAndParent(this, scope);
}
public String getClassName()

Просмотреть файл

@ -19,6 +19,9 @@
* Rights Reserved.
*
* Contributor(s):
* Norris Boyd
* Igor Bukanov
* Brendan Eich
*
* Alternatively, the contents of this file may be used under the
* terms of the GNU Public License (the "GPL"), in which case the
@ -50,22 +53,19 @@ import java.lang.reflect.Method;
* @author Brendan Eich
* @author Norris Boyd
*/
public class NativeRegExpCtor extends NativeFunction {
class NativeRegExpCtor extends NativeFunction {
public NativeRegExpCtor() {
NativeRegExpCtor()
{
functionName = "RegExp";
}
public String getClassName() {
return "Function";
}
public Object call(Context cx, Scriptable scope, Scriptable thisObj,
Object[] args)
{
if (args.length > 0 && args[0] instanceof NativeRegExp &&
(args.length == 1 || args[1] == Undefined.instance))
{
{
return args[0];
}
return construct(cx, scope, args);
@ -75,22 +75,16 @@ public class NativeRegExpCtor extends NativeFunction {
{
NativeRegExp re = new NativeRegExp();
re.compile(cx, scope, args);
re.setPrototype(getClassPrototype(scope, "RegExp"));
re.setParentScope(getParentScope());
ScriptRuntime.setObjectProtoAndParent(re, scope);
return re;
}
static RegExpImpl getImpl()
private static RegExpImpl getImpl()
{
Context cx = Context.getCurrentContext();
return (RegExpImpl) ScriptRuntime.getRegExpProxy(cx);
}
private static String stringResult(Object obj)
{
return (obj == null) ? "" : obj.toString();
}
// #string_id_map#
private static final int
@ -226,34 +220,46 @@ public class NativeRegExpCtor extends NativeFunction {
int shifted = id - idBase;
if (1 <= shifted && shifted <= MAX_INSTANCE_ID) {
RegExpImpl impl = getImpl();
Object stringResult;
switch (shifted) {
case Id_multiline:
case Id_STAR:
return ScriptRuntime.wrapBoolean(impl.multiline);
case Id_multiline:
case Id_STAR:
return ScriptRuntime.wrapBoolean(impl.multiline);
case Id_input:
case Id_UNDERSCORE:
return stringResult(impl.input);
case Id_input:
case Id_UNDERSCORE:
stringResult = impl.input;
break;
case Id_lastMatch:
case Id_AMPERSAND:
return stringResult(impl.lastMatch);
case Id_lastMatch:
case Id_AMPERSAND:
stringResult = impl.lastMatch;
break;
case Id_lastParen:
case Id_PLUS:
return stringResult(impl.lastParen);
case Id_lastParen:
case Id_PLUS:
stringResult = impl.lastParen;
break;
case Id_leftContext:
case Id_BACK_QUOTE:
return stringResult(impl.leftContext);
case Id_leftContext:
case Id_BACK_QUOTE:
stringResult = impl.leftContext;
break;
case Id_rightContext:
case Id_QUOTE:
return stringResult(impl.rightContext);
case Id_rightContext:
case Id_QUOTE:
stringResult = impl.rightContext;
break;
default:
{
// Must be one of $1..$9, convert to 0..8
int substring_number = shifted - DOLLAR_ID_BASE - 1;
stringResult = impl.getParenSubString(substring_number);
break;
}
}
// Must be one of $1..$9, convert to 0..8
int substring_number = shifted - DOLLAR_ID_BASE - 1;
return impl.getParenSubString(substring_number).toString();
return (stringResult == null) ? "" : stringResult.toString();
}
return super.getInstanceIdValue(id);
}