001: /*
002: * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.es;
030:
031: /**
032: * JavaScript object
033: */
034: class NativeNumber extends Native {
035: static final int NEW = 1;
036: static final int TO_STRING = NEW + 1;
037: static final int VALUE_OF = TO_STRING + 1;
038:
039: /**
040: * Create a new object based on a prototype
041: */
042: private NativeNumber(String name, int n, int len) {
043: super (name, len);
044:
045: this .n = n;
046: }
047:
048: /**
049: * Creates the native Object object
050: */
051: static ESObject create(Global resin) {
052: Native nativeNum = new NativeNumber("Number", NEW, 1);
053: ESWrapper numProto = new ESWrapper("Number", resin.objProto,
054: ESNumber.create(0));
055: NativeWrapper num = new NativeWrapper(resin, nativeNum,
056: numProto, ESThunk.NUM_THUNK);
057: resin.numProto = numProto;
058:
059: int flags = DONT_ENUM;
060: int allflags = (DONT_ENUM | DONT_DELETE | READ_ONLY);
061:
062: numProto.put(ESId.intern("toString"), new NativeNumber(
063: "toString", TO_STRING, 0), flags);
064: numProto.put(ESId.intern("valueOf"), new NativeNumber(
065: "valueOf", VALUE_OF, 0), flags);
066:
067: num.put("length", ESNumber.create(1), allflags);
068: num.put("MAX_VALUE", ESNumber.create(Double.MAX_VALUE),
069: allflags);
070: num.put("MIN_VALUE", ESNumber.create(Double.MIN_VALUE),
071: allflags);
072: num.put("NaN", ESNumber.create(0.0 / 0.0), allflags);
073: num.put("NEGATIVE_INFINITY", ESNumber.create(-1.0 / 0.0),
074: allflags);
075: num.put("POSITIVE_INFINITY", ESNumber.create(1.0 / 0.0),
076: allflags);
077:
078: numProto.setClean();
079: num.setClean();
080:
081: return num;
082: }
083:
084: public ESBase call(Call eval, int length) throws Throwable {
085: ESBase argThis;
086:
087: switch (n) {
088: case NEW:
089: if (length == 0)
090: return ESNumber.create(0);
091: else
092: return ESNumber.create(eval.getArg(0).toNum());
093:
094: case TO_STRING:
095: try {
096: return ((ESBase) ((ESWrapper) eval.getArg(-1)).value)
097: .toStr();
098: } catch (ClassCastException e) {
099: if (eval.getArg(-1) instanceof ESNumber)
100: return eval.getArg(-1);
101: if (eval.getArg(-1) instanceof ESThunk)
102: return ((ESBase) ((ESWrapper) ((ESThunk) eval
103: .getArg(-1)).getObject()).value).toStr();
104:
105: throw new ESException("toString expected number object");
106: }
107:
108: case VALUE_OF:
109: try {
110: return (ESBase) ((ESWrapper) eval.getArg(-1)).value;
111: } catch (ClassCastException e) {
112: if (eval.getArg(-1) instanceof ESNumber)
113: return eval.getArg(-1);
114: if (eval.getArg(-1) instanceof ESThunk)
115: return (ESBase) ((ESWrapper) ((ESThunk) eval
116: .getArg(-1)).getObject()).value;
117:
118: throw new ESException("valueOf expected number object");
119: }
120:
121: default:
122: throw new RuntimeException("Unknown object function");
123: }
124: }
125:
126: public ESBase construct(Call eval, int length) throws Throwable {
127: if (n != NEW)
128: return super .construct(eval, length);
129:
130: ESBase value;
131:
132: if (length == 0)
133: value = ESNumber.create(0);
134: else
135: value = ESNumber.create(eval.getArg(0).toNum());
136:
137: return value.toObject();
138: }
139: }
|