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 NativeBoolean 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 NativeBoolean(String name, int n, int len) {
043: super (name, len);
044:
045: this .n = n;
046: }
047:
048: /**
049: * Creates the initial native Boolean object
050: */
051: static ESObject create(Global resin) {
052: Native nativeBool = new NativeBoolean("Boolean", NEW, 1);
053: ESWrapper boolProto = new ESWrapper("Boolean", resin.objProto,
054: ESBoolean.FALSE);
055: NativeWrapper bool = new NativeWrapper(resin, nativeBool,
056: boolProto, ESThunk.BOOL_THUNK);
057: resin.boolProto = boolProto;
058:
059: put(boolProto, "toString", TO_STRING, 0, DONT_ENUM);
060: put(boolProto, "valueOf", VALUE_OF, 0, DONT_ENUM);
061:
062: bool.setClean();
063: boolProto.setClean();
064:
065: return bool;
066: }
067:
068: private static void put(ESObject obj, String name, int n, int len,
069: int flags) {
070: obj.put(name, new NativeBoolean(name, n, len), flags);
071: }
072:
073: public ESBase call(Call eval, int length) throws Throwable {
074: switch (n) {
075: case NEW:
076: if (length == 0)
077: return ESBoolean.FALSE;
078: else
079: return ESBoolean.create(eval.getArg(0).toBoolean());
080:
081: case TO_STRING:
082: try {
083: return ((ESBase) ((ESWrapper) eval.getArg(-1)).value)
084: .toStr();
085: } catch (ClassCastException e) {
086: if (eval.getArg(-1) instanceof ESBoolean)
087: return eval.getArg(-1);
088: if (eval.getArg(-1) instanceof ESThunk)
089: return ((ESBase) ((ESWrapper) ((ESThunk) eval
090: .getArg(-1)).getObject()).value).toStr();
091:
092: throw new ESException(
093: "toString expected boolean object");
094: }
095:
096: case VALUE_OF:
097: try {
098: return (ESBase) ((ESWrapper) eval.getArg(-1)).value;
099: } catch (ClassCastException e) {
100: if (eval.getArg(-1) instanceof ESBoolean)
101: return eval.getArg(-1);
102: if (eval.getArg(-1) instanceof ESThunk)
103: return (ESBase) ((ESWrapper) ((ESThunk) eval
104: .getArg(-1)).getObject()).value;
105:
106: throw new ESException("valueOf expected boolean object");
107: }
108:
109: default:
110: throw new RuntimeException("Unknown object function");
111: }
112: }
113:
114: public ESBase construct(Call eval, int length) throws Throwable {
115: if (n != NEW)
116: return super .construct(eval, length);
117:
118: ESBase value;
119:
120: if (length == 0)
121: value = ESBoolean.FALSE;
122: else
123: value = ESBoolean.create(eval.getArg(0).toBoolean());
124:
125: return value.toObject();
126: }
127: }
|