KickJava   Java API By Example, From Geeks To Geeks.

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


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 NativeBoolean 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 NativeBoolean(String JavaDoc name, int n, int len)
43   {
44     super(name, len);
45
46     this.n = n;
47   }
48
49   /**
50    * Creates the initial native Boolean object
51    */

52   static ESObject create(Global resin)
53   {
54     Native nativeBool = new NativeBoolean("Boolean", NEW, 1);
55     ESWrapper boolProto = new ESWrapper("Boolean", resin.objProto,
56                                         ESBoolean.FALSE);
57     NativeWrapper bool = new NativeWrapper(resin, nativeBool,
58                                            boolProto, ESThunk.BOOL_THUNK);
59     resin.boolProto = boolProto;
60
61     put(boolProto, "toString", TO_STRING, 0, DONT_ENUM);
62     put(boolProto, "valueOf", VALUE_OF, 0, DONT_ENUM);
63
64     bool.setClean();
65     boolProto.setClean();
66
67     return bool;
68   }
69
70   private static void put(ESObject obj, String JavaDoc name, int n, int len,
71                           int flags)
72   {
73     obj.put(name, new NativeBoolean(name, n, len), flags);
74   }
75
76   public ESBase call(Call eval, int length) throws Throwable JavaDoc
77   {
78     switch (n) {
79     case NEW:
80       if (length == 0)
81         return ESBoolean.FALSE;
82       else
83         return ESBoolean.create(eval.getArg(0).toBoolean());
84
85     case TO_STRING:
86       try {
87         return ((ESBase) ((ESWrapper) eval.getArg(-1)).value).toStr();
88       } catch (ClassCastException JavaDoc e) {
89         if (eval.getArg(-1) instanceof ESBoolean)
90           return eval.getArg(-1);
91         if (eval.getArg(-1) instanceof ESThunk)
92           return ((ESBase) ((ESWrapper) ((ESThunk) eval.getArg(-1)).getObject()).value).toStr();
93
94         throw new ESException("toString expected boolean object");
95       }
96
97     case VALUE_OF:
98       try {
99         return (ESBase) ((ESWrapper) eval.getArg(-1)).value;
100       } catch (ClassCastException JavaDoc e) {
101         if (eval.getArg(-1) instanceof ESBoolean)
102           return eval.getArg(-1);
103         if (eval.getArg(-1) instanceof ESThunk)
104           return (ESBase) ((ESWrapper) ((ESThunk) eval.getArg(-1)).getObject()).value;
105
106         throw new ESException("valueOf expected boolean object");
107       }
108
109     default:
110       throw new RuntimeException JavaDoc("Unknown object function");
111     }
112   }
113
114   public ESBase construct(Call eval, int length) throws Throwable JavaDoc
115   {
116     if (n != NEW)
117       return super.construct(eval, length);
118
119     ESBase value;
120
121     if (length == 0)
122       value = ESBoolean.FALSE;
123     else
124       value = ESBoolean.create(eval.getArg(0).toBoolean());
125
126     return value.toObject();
127   }
128 }
129
Popular Tags