KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > es > NativeNumber


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.es;
30
31 /**
32  * JavaScript object
33  */

34 class NativeNumber extends Native {
35   static final int NEW = 1;
36   static final int TO_STRING = NEW + 1;
37   static final int VALUE_OF = TO_STRING + 1;
38
39   /**
40    * Create a new object based on a prototype
41    */

42   private NativeNumber(String JavaDoc name, int n, int len)
43   {
44     super(name, len);
45
46     this.n = n;
47   }
48
49   /**
50    * Creates the native Object object
51    */

52   static ESObject create(Global resin)
53   {
54     Native nativeNum = new NativeNumber("Number", NEW, 1);
55     ESWrapper numProto = new ESWrapper("Number", resin.objProto,
56                                        ESNumber.create(0));
57     NativeWrapper num = new NativeWrapper(resin, nativeNum, numProto,
58                                           ESThunk.NUM_THUNK);
59     resin.numProto = numProto;
60
61     int flags = DONT_ENUM;
62     int allflags = (DONT_ENUM|DONT_DELETE|READ_ONLY);
63
64     numProto.put(ESId.intern("toString"),
65                  new NativeNumber("toString", TO_STRING, 0),
66                  flags);
67     numProto.put(ESId.intern("valueOf"),
68                  new NativeNumber("valueOf", VALUE_OF, 0),
69                  flags);
70
71     num.put("length", ESNumber.create(1), allflags);
72     num.put("MAX_VALUE", ESNumber.create(Double.MAX_VALUE), allflags);
73     num.put("MIN_VALUE", ESNumber.create(Double.MIN_VALUE), allflags);
74     num.put("NaN", ESNumber.create(0.0/0.0), allflags);
75     num.put("NEGATIVE_INFINITY", ESNumber.create(-1.0/0.0), allflags);
76     num.put("POSITIVE_INFINITY", ESNumber.create(1.0/0.0), allflags);
77
78     numProto.setClean();
79     num.setClean();
80
81     return num;
82   }
83
84   public ESBase call(Call eval, int length) throws Throwable JavaDoc
85   {
86     ESBase argThis;
87
88     switch (n) {
89     case NEW:
90       if (length == 0)
91         return ESNumber.create(0);
92       else
93         return ESNumber.create(eval.getArg(0).toNum());
94
95     case TO_STRING:
96       try {
97         return ((ESBase) ((ESWrapper) eval.getArg(-1)).value).toStr();
98       } catch (ClassCastException JavaDoc e) {
99         if (eval.getArg(-1) instanceof ESNumber)
100           return eval.getArg(-1);
101         if (eval.getArg(-1) instanceof ESThunk)
102           return ((ESBase) ((ESWrapper) ((ESThunk) eval.getArg(-1)).getObject()).value).toStr();
103
104         throw new ESException("toString expected number object");
105       }
106
107     case VALUE_OF:
108       try {
109         return (ESBase) ((ESWrapper) eval.getArg(-1)).value;
110       } catch (ClassCastException JavaDoc e) {
111         if (eval.getArg(-1) instanceof ESNumber)
112           return eval.getArg(-1);
113         if (eval.getArg(-1) instanceof ESThunk)
114           return (ESBase) ((ESWrapper) ((ESThunk) eval.getArg(-1)).getObject()).value;
115
116         throw new ESException("valueOf expected number object");
117       }
118
119     default:
120       throw new RuntimeException JavaDoc("Unknown object function");
121     }
122   }
123
124   public ESBase construct(Call eval, int length) throws Throwable JavaDoc
125   {
126     if (n != NEW)
127       return super.construct(eval, length);
128
129     ESBase value;
130
131     if (length == 0)
132       value = ESNumber.create(0);
133     else
134       value = ESNumber.create(eval.getArg(0).toNum());
135
136     return value.toObject();
137   }
138 }
139
Popular Tags