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 function. The global object is represented as one so
033: * the top level script can be called.
034: */
035: class Native extends ESBase {
036: static ESId CONSTRUCTOR = ESId.intern("constructor");
037: static ESId LENGTH = ESId.intern("length");
038: static ESId PROTOTYPE = ESId.intern("prototype");
039: static final int NEW = 1;
040:
041: String name;
042: ESString id;
043: ESString[] formals;
044: int length;
045: protected int n;
046: protected int newN;
047:
048: protected Native(String name, int len) {
049: prototype = esBase;
050: this .name = name;
051: this .length = len;
052: className = "Function";
053: id = ESId.intern(name);
054: }
055:
056: /**
057: * Null constructor
058: */
059: public ESBase getProperty(ESString key) {
060: if (key.equals(LENGTH))
061: return ESNumber.create(length);
062: else
063: return esEmpty;
064: }
065:
066: public ESBase delete(ESString key) {
067: return ESBoolean.create(false);
068: }
069:
070: public ESBase typeof() throws ESException {
071: return ESString.create("function");
072: }
073:
074: public ESString toStr() {
075: return ESString.create(decompile());
076: }
077:
078: private String decompile() {
079: StringBuffer sbuf = new StringBuffer();
080:
081: sbuf.append("function ");
082: sbuf.append(name);
083: sbuf.append("(");
084: for (int i = 0; formals != null && i < formals.length; i++) {
085: if (i != 0)
086: sbuf.append(", ");
087: sbuf.append(formals[i]);
088: }
089:
090: sbuf.append(") ");
091:
092: sbuf.append("{ ");
093: sbuf.append("[native code]");
094: sbuf.append(" }");
095:
096: return sbuf.toString();
097: }
098:
099: public double toNum() {
100: return 0.0 / 0.0;
101: }
102:
103: public boolean toBoolean() {
104: return true;
105: }
106:
107: public ESObject toObject() {
108: throw new RuntimeException();
109: //return new NativeWrapper(Global.getGlobal(), this);
110: }
111:
112: public ESBase construct(Call eval, int length) throws Throwable {
113: if (n != newN)
114: return super .construct(eval, length);
115:
116: try {
117: return (ESBase) call(eval, length);
118: } catch (ClassCastException e) {
119: throw new ESException("cannot create " + name);
120: }
121: }
122:
123: protected Native create(String name, int n, int len) {
124: throw new RuntimeException("create not specialized");
125: }
126: }
|