t.push(isPrefix ? val : num);
break;
}
+
+
+ case ADD: {
+ int count = ((Number)arg[pc]).intValue();
+ if(count < 2) throw new Error("this should never happen");
+ if(count == 2) {
+ // common case
+ Object right = t.pop();
+ Object left = t.pop();
+ if(left instanceof String || right instanceof String) t.push(JS.toString(left).concat(JS.toString(right)));
+ else t.push(new Double(JS.toDouble(left) + JS.toDouble(right)));
+ break;
+ }
+ Object[] args = new Object[count];
+ while(--count >= 0) args[count] = t.pop();
+ if(args[0] instanceof String) {
+ StringBuffer sb = new StringBuffer(64);
+ for(int i=0;i<args.length;i++) sb.append(JS.toString(args[i]));
+ t.push(sb.toString());
+ } else {
+ int numStrings = 0;
+ for(int i=0;i<args.length;i++) if(args[i] instanceof String) numStrings++;
+ if(numStrings == 0) {
+ double d = 0.0;
+ for(int i=0;i<args.length;i++) d += JS.toDouble(args[i]);
+ t.push(new Double(d));
+ } else {
+ double d=0.0;
+ int i=0;
+ do {
+ d += JS.toDouble(args[i++]);
+ } while(!(args[i] instanceof String));
+ StringBuffer sb = new StringBuffer(64);
+ sb.append(JS.toString(new Double(d)));
+ while(i < args.length) sb.append(JS.toString(args[i++]));
+ t.push(sb.toString());
+ }
+ }
+ break;
+ }
default: {
Object right = t.pop();
Object left = t.pop();
switch(op[pc]) {
-
- case ADD: {
- Object l = left;
- Object r = right;
- if (l instanceof String || r instanceof String) {
- if (l == null) l = "null";
- if (r == null) r = "null";
- if (l instanceof Number && ((Number)l).doubleValue() == ((Number)l).longValue())
- l = new Long(((Number)l).longValue());
- if (r instanceof Number && ((Number)r).doubleValue() == ((Number)r).longValue())
- r = new Long(((Number)r).longValue());
- t.push(l.toString() + r.toString()); break;
- }
- t.push(new Double(JS.toDouble(l) + JS.toDouble(r))); break;
- }
case BITOR: t.push(new Long(JS.toLong(left) | JS.toLong(right))); break;
case BITXOR: t.push(new Long(JS.toLong(left) ^ JS.toLong(right))); break;