KickJava   Java API By Example, From Geeks To Geeks.

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


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 function. The global object is represented as one so
33  * the top level script can be called.
34  */

35 class Native extends ESBase {
36   static ESId CONSTRUCTOR = ESId.intern("constructor");
37   static ESId LENGTH = ESId.intern("length");
38   static ESId PROTOTYPE = ESId.intern("prototype");
39   static final int NEW = 1;
40
41   String JavaDoc name;
42   ESString id;
43   ESString []formals;
44   int length;
45   protected int n;
46   protected int newN;
47
48   protected Native(String JavaDoc name, int len)
49   {
50     prototype = esBase;
51     this.name = name;
52     this.length = len;
53     className = "Function";
54     id = ESId.intern(name);
55   }
56
57   /**
58    * Null constructor
59    */

60   public ESBase getProperty(ESString key)
61   {
62     if (key.equals(LENGTH))
63       return ESNumber.create(length);
64     else
65       return esEmpty;
66   }
67
68   public ESBase delete(ESString key)
69   {
70     return ESBoolean.create(false);
71   }
72
73   public ESBase typeof() throws ESException
74   {
75     return ESString.create("function");
76   }
77
78   public ESString toStr()
79   {
80     return ESString.create(decompile());
81   }
82
83   private String JavaDoc decompile()
84   {
85     StringBuffer JavaDoc sbuf = new StringBuffer JavaDoc();
86
87     sbuf.append("function ");
88     sbuf.append(name);
89     sbuf.append("(");
90     for (int i = 0; formals != null && i < formals.length; i++) {
91       if (i != 0)
92         sbuf.append(", ");
93       sbuf.append(formals[i]);
94     }
95
96     sbuf.append(") ");
97
98     sbuf.append("{ ");
99     sbuf.append("[native code]");
100     sbuf.append(" }");
101
102     return sbuf.toString();
103   }
104
105   public double toNum()
106   {
107     return 0.0/0.0;
108   }
109
110   public boolean toBoolean()
111   {
112     return true;
113   }
114
115   public ESObject toObject()
116   {
117     throw new RuntimeException JavaDoc();
118     //return new NativeWrapper(Global.getGlobal(), this);
119
}
120
121   public ESBase construct(Call eval, int length) throws Throwable JavaDoc
122   {
123     if (n != newN)
124       return super.construct(eval, length);
125
126     try {
127       return (ESBase) call(eval, length);
128     } catch (ClassCastException JavaDoc e) {
129       throw new ESException("cannot create " + name);
130     }
131   }
132
133   protected Native create(String JavaDoc name, int n, int len)
134   {
135     throw new RuntimeException JavaDoc("create not specialized");
136   }
137 }
138
Popular Tags