зеркало из https://github.com/mozilla/pjs.git
Continuing JSValue fun.
This commit is contained in:
Родитель
05b11e484e
Коммит
f2b4885566
|
@ -39,9 +39,10 @@ class ControlNode {
|
|||
|
||||
String print()
|
||||
{
|
||||
StringBuffer result = new StringBuffer("ControlNode ");
|
||||
StringBuffer result = new StringBuffer(getClass().toString().substring(6));
|
||||
result.append(" #");
|
||||
result.append(index);
|
||||
result.append("\nexpr: ");
|
||||
result.append("\nexpr: \n");
|
||||
if (expr == null)
|
||||
result.append("expr = null\n");
|
||||
else
|
||||
|
|
|
@ -27,6 +27,17 @@ class JSBoolean extends JSValue {
|
|||
return this;
|
||||
}
|
||||
|
||||
JSValue toPrimitive(String hint) {
|
||||
return this;
|
||||
}
|
||||
|
||||
JSString toJSString() {
|
||||
return (b) ? new JSString("true") : new JSString("false");
|
||||
}
|
||||
|
||||
JSDouble toJSDouble() {
|
||||
return (b) ? new JSDouble(1) : new JSDouble(0);
|
||||
}
|
||||
|
||||
boolean b;
|
||||
|
||||
|
|
|
@ -85,12 +85,16 @@ class JSDouble extends JSNumber {
|
|||
return this;
|
||||
}
|
||||
|
||||
JSValue toPrimitive() {
|
||||
return this;
|
||||
}
|
||||
|
||||
JSInteger toJSInteger() {
|
||||
return new JSInteger((int)d);
|
||||
}
|
||||
|
||||
JSBoolean toJSBoolean() {
|
||||
return (d != 0.0) ? JSBoolean.JSTrue : JSBoolean.JSFalse;
|
||||
return ((d == d) && (d != 0.0)) ? JSBoolean.JSTrue : JSBoolean.JSFalse;
|
||||
}
|
||||
|
||||
JSString toJSString() {
|
||||
|
|
|
@ -27,6 +27,10 @@ class JSInteger extends JSNumber {
|
|||
return this;
|
||||
}
|
||||
|
||||
JSValue toPrimitive() {
|
||||
return this;
|
||||
}
|
||||
|
||||
JSString toJSString() {
|
||||
return new JSString(Integer.toString(i));
|
||||
}
|
||||
|
|
|
@ -28,8 +28,124 @@ class JSObject extends JSValue {
|
|||
theEnv.theStack.push(v);
|
||||
}
|
||||
|
||||
public String toString()
|
||||
{
|
||||
JSValue defaultValue(String hint) {
|
||||
return null; // XXX
|
||||
}
|
||||
|
||||
JSValue toPrimitive(String hint) {
|
||||
JSValue result = defaultValue(hint);
|
||||
if (result instanceof JSObject)
|
||||
throw new JSException(new JSString("default value returned object"));
|
||||
else
|
||||
return result;
|
||||
}
|
||||
|
||||
JSBoolean toJSBoolean() {
|
||||
return JSBoolean.JSTrue;
|
||||
}
|
||||
|
||||
JSDouble toJSDouble() {
|
||||
return toPrimitive("Number").toJSDouble();
|
||||
}
|
||||
|
||||
void gt(Environment theEnv) {
|
||||
JSValue lV = toPrimitive("Number");
|
||||
JSValue rV = theEnv.theStack.pop().toPrimitive("Number");
|
||||
if ((lV instanceof JSString) && (rV instanceof JSString)) {
|
||||
theEnv.theStack.push(rV);
|
||||
lV.gt(theEnv);
|
||||
}
|
||||
else {
|
||||
theEnv.theStack.push(rV.toJSDouble());
|
||||
lV.toJSDouble().gt(theEnv);
|
||||
}
|
||||
}
|
||||
|
||||
void ge(Environment theEnv) {
|
||||
JSValue lV = toPrimitive("Number");
|
||||
JSValue rV = theEnv.theStack.pop().toPrimitive("Number");
|
||||
if ((lV instanceof JSString) && (rV instanceof JSString)) {
|
||||
theEnv.theStack.push(rV);
|
||||
lV.ge(theEnv);
|
||||
}
|
||||
else {
|
||||
theEnv.theStack.push(rV.toJSDouble());
|
||||
lV.toJSDouble().ge(theEnv);
|
||||
}
|
||||
}
|
||||
|
||||
void lt(Environment theEnv) {
|
||||
JSValue lV = toPrimitive("Number");
|
||||
JSValue rV = theEnv.theStack.pop().toPrimitive("Number");
|
||||
if ((lV instanceof JSString) && (rV instanceof JSString)) {
|
||||
theEnv.theStack.push(rV);
|
||||
lV.lt(theEnv);
|
||||
}
|
||||
else {
|
||||
theEnv.theStack.push(rV.toJSDouble());
|
||||
lV.toJSDouble().lt(theEnv);
|
||||
}
|
||||
}
|
||||
|
||||
void le(Environment theEnv) {
|
||||
JSValue lV = toPrimitive("Number");
|
||||
JSValue rV = theEnv.theStack.pop().toPrimitive("Number");
|
||||
if ((lV instanceof JSString) && (rV instanceof JSString)) {
|
||||
theEnv.theStack.push(rV);
|
||||
lV.le(theEnv);
|
||||
}
|
||||
else {
|
||||
theEnv.theStack.push(rV.toJSDouble());
|
||||
lV.toJSDouble().le(theEnv);
|
||||
}
|
||||
}
|
||||
|
||||
void eq(Environment theEnv) {
|
||||
JSValue lV = toPrimitive("Number");
|
||||
JSValue rV = theEnv.theStack.pop().toPrimitive("Number");
|
||||
if ((lV instanceof JSString) && (rV instanceof JSString)) {
|
||||
theEnv.theStack.push(rV);
|
||||
lV.eq(theEnv);
|
||||
}
|
||||
else {
|
||||
theEnv.theStack.push(rV.toJSDouble());
|
||||
lV.toJSDouble().eq(theEnv);
|
||||
}
|
||||
}
|
||||
|
||||
void ne(Environment theEnv) {
|
||||
JSValue lV = toPrimitive("Number");
|
||||
JSValue rV = theEnv.theStack.pop().toPrimitive("Number");
|
||||
if ((lV instanceof JSString) && (rV instanceof JSString)) {
|
||||
theEnv.theStack.push(rV);
|
||||
lV.ne(theEnv);
|
||||
}
|
||||
else {
|
||||
theEnv.theStack.push(rV.toJSDouble());
|
||||
lV.toJSDouble().ne(theEnv);
|
||||
}
|
||||
}
|
||||
|
||||
void add(Environment theEnv) {
|
||||
JSValue lV = toPrimitive("");
|
||||
JSValue rV = theEnv.theStack.pop().toPrimitive("");
|
||||
if ((lV instanceof JSString) || (rV instanceof JSString)) {
|
||||
theEnv.theStack.push(rV);
|
||||
lV.ne(theEnv);
|
||||
}
|
||||
else {
|
||||
theEnv.theStack.push(rV.toJSDouble());
|
||||
lV.toJSDouble().add(theEnv);
|
||||
}
|
||||
}
|
||||
|
||||
void subtract(Environment theEnv) {
|
||||
theEnv.theStack.push(theEnv.theStack.pop().toJSDouble());
|
||||
toJSDouble().subtract(theEnv);
|
||||
}
|
||||
|
||||
|
||||
public String toString() {
|
||||
return value;
|
||||
}
|
||||
|
||||
|
|
|
@ -16,8 +16,75 @@ class JSString extends JSValue {
|
|||
theEnv.theStack.push(new JSString(s + vR.s));
|
||||
}
|
||||
|
||||
JSString toJSString()
|
||||
{
|
||||
void gt(Environment theEnv) {
|
||||
JSValue vR = theEnv.theStack.peek();
|
||||
if (vR instanceof JSString) {
|
||||
theEnv.theStack.pop();
|
||||
theEnv.theStack.push((s.compareTo(vR.toJSString().s) == 1) ? JSBoolean.JSTrue : JSBoolean.JSFalse);
|
||||
}
|
||||
else
|
||||
toJSDouble().gt(theEnv);
|
||||
}
|
||||
|
||||
void ge(Environment theEnv) {
|
||||
JSValue vR = theEnv.theStack.peek();
|
||||
if (vR instanceof JSString) {
|
||||
theEnv.theStack.pop();
|
||||
theEnv.theStack.push((s.compareTo(vR.toJSString().s) != -1) ? JSBoolean.JSTrue : JSBoolean.JSFalse);
|
||||
}
|
||||
else
|
||||
toJSDouble().ge(theEnv);
|
||||
}
|
||||
|
||||
void lt(Environment theEnv) {
|
||||
JSValue vR = theEnv.theStack.peek();
|
||||
if (vR instanceof JSString) {
|
||||
theEnv.theStack.pop();
|
||||
theEnv.theStack.push((s.compareTo(vR.toJSString().s) == -1) ? JSBoolean.JSTrue : JSBoolean.JSFalse);
|
||||
}
|
||||
else
|
||||
toJSDouble().lt(theEnv);
|
||||
}
|
||||
|
||||
void le(Environment theEnv) {
|
||||
JSValue vR = theEnv.theStack.peek();
|
||||
if (vR instanceof JSString) {
|
||||
theEnv.theStack.pop();
|
||||
theEnv.theStack.push((s.compareTo(vR.toJSString().s) != 1) ? JSBoolean.JSTrue : JSBoolean.JSFalse);
|
||||
}
|
||||
else
|
||||
toJSDouble().le(theEnv);
|
||||
}
|
||||
|
||||
void eq(Environment theEnv) {
|
||||
JSValue vR = theEnv.theStack.peek();
|
||||
if (vR instanceof JSString) {
|
||||
theEnv.theStack.pop();
|
||||
theEnv.theStack.push((s.compareTo(vR.toJSString().s) == 0) ? JSBoolean.JSTrue : JSBoolean.JSFalse);
|
||||
}
|
||||
else
|
||||
toJSDouble().eq(theEnv);
|
||||
}
|
||||
|
||||
void ne(Environment theEnv) {
|
||||
JSValue vR = theEnv.theStack.peek();
|
||||
if (vR instanceof JSString) {
|
||||
theEnv.theStack.pop();
|
||||
theEnv.theStack.push((s.compareTo(vR.toJSString().s) != 0) ? JSBoolean.JSTrue : JSBoolean.JSFalse);
|
||||
}
|
||||
else
|
||||
toJSDouble().ne(theEnv);
|
||||
}
|
||||
|
||||
JSDouble toJSDouble() {
|
||||
return new JSDouble(s); // XXX Way More To Do, see Rhino ScriptRuntime.java
|
||||
}
|
||||
|
||||
JSString toJSString() {
|
||||
return this;
|
||||
}
|
||||
|
||||
JSValue toPrimitive() {
|
||||
return this;
|
||||
}
|
||||
|
||||
|
|
|
@ -40,11 +40,10 @@ class JSValue extends ExpressionNode {
|
|||
void eq(Environment theEnv) { unimplemented("eq"); }
|
||||
void ne(Environment theEnv) { unimplemented("ne"); }
|
||||
|
||||
|
||||
JSDouble toJSDouble() { unimplemented("toJSDouble"); return null; }
|
||||
JSInteger toJSInteger() { unimplemented("toJSInteger"); return null; }
|
||||
JSString toJSString() { unimplemented("toJSString"); return null; }
|
||||
JSBoolean toJSBoolean() { unimplemented("toJSBoolean"); return null; }
|
||||
|
||||
JSValue toPrimitive(String hint) { unimplemented("toPrimitive"); return null; }
|
||||
|
||||
}
|
Загрузка…
Ссылка в новой задаче