KickJava   Java API By Example, From Geeks To Geeks.

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


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 class NativeWrapper extends ESObject {
32   static ESId CONSTRUCTOR = ESId.intern("constructor");
33   static ESId LENGTH = ESId.intern("length");
34   static ESId PROTOTYPE = ESId.intern("prototype");
35
36   Native fun;
37   Native constructor;
38
39   NativeWrapper(Global resin, Native fun, ESObject proto, int thunk)
40   {
41     super("Function", resin.funProto);
42
43     if (proto == null)
44       throw new RuntimeException JavaDoc();
45
46     this.fun = fun;
47     className = "Function";
48
49     fun.newN = fun.n;
50     constructor = fun;
51
52     proto.put(CONSTRUCTOR, new ESThunk(thunk), DONT_ENUM);
53
54     put(PROTOTYPE, new ESThunk(thunk - 1), DONT_ENUM|DONT_DELETE|READ_ONLY);
55
56     put(LENGTH, ESNumber.create(fun.length), DONT_ENUM|DONT_DELETE|READ_ONLY);
57   }
58
59   protected NativeWrapper()
60   {
61   }
62
63   public ESBase typeof() throws ESException
64   {
65     return ESString.create("function");
66   }
67
68   public ESBase getProperty(ESString name) throws Throwable JavaDoc
69   {
70     ESBase value = fun.getProperty(name);
71
72     if (value != esEmpty && value != null)
73       return value;
74     else {
75       value = super.getProperty(name);
76
77       return value;
78     }
79   }
80
81   public void setProperty(ESString name, ESBase value) throws Throwable JavaDoc
82   {
83     fun.setProperty(name, value);
84
85     super.setProperty(name, value);
86   }
87
88   public ESBase toPrimitive(int hint) throws Throwable JavaDoc
89   {
90     return fun.toStr();
91   }
92
93   public ESBase call(Call eval, int length) throws Throwable JavaDoc
94   {
95     return fun.call(eval, length);
96   }
97
98   public ESBase construct(Call eval, int length) throws Throwable JavaDoc
99   {
100     if (constructor != null)
101       return constructor.construct(eval, length);
102     else
103       return super.construct(eval, length);
104   }
105
106   protected void copy(Object JavaDoc obj)
107   {
108     NativeWrapper dup = (NativeWrapper) obj;
109
110     super.copy(dup);
111     dup.fun = fun;
112     dup.constructor = constructor;
113   }
114
115   ESObject resinCopy()
116   {
117     NativeWrapper dup = (NativeWrapper) dup();
118
119     copy(dup);
120
121     return dup;
122   }
123
124   protected ESObject dup()
125   {
126     return new NativeWrapper();
127   }
128 }
129
130
Popular Tags