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: class NativeWrapper extends ESObject {
032: static ESId CONSTRUCTOR = ESId.intern("constructor");
033: static ESId LENGTH = ESId.intern("length");
034: static ESId PROTOTYPE = ESId.intern("prototype");
035:
036: Native fun;
037: Native constructor;
038:
039: NativeWrapper(Global resin, Native fun, ESObject proto, int thunk) {
040: super ("Function", resin.funProto);
041:
042: if (proto == null)
043: throw new RuntimeException();
044:
045: this .fun = fun;
046: className = "Function";
047:
048: fun.newN = fun.n;
049: constructor = fun;
050:
051: proto.put(CONSTRUCTOR, new ESThunk(thunk), DONT_ENUM);
052:
053: put(PROTOTYPE, new ESThunk(thunk - 1), DONT_ENUM | DONT_DELETE
054: | READ_ONLY);
055:
056: put(LENGTH, ESNumber.create(fun.length), DONT_ENUM
057: | DONT_DELETE | READ_ONLY);
058: }
059:
060: protected NativeWrapper() {
061: }
062:
063: public ESBase typeof() throws ESException {
064: return ESString.create("function");
065: }
066:
067: public ESBase getProperty(ESString name) throws Throwable {
068: ESBase value = fun.getProperty(name);
069:
070: if (value != esEmpty && value != null)
071: return value;
072: else {
073: value = super .getProperty(name);
074:
075: return value;
076: }
077: }
078:
079: public void setProperty(ESString name, ESBase value)
080: throws Throwable {
081: fun.setProperty(name, value);
082:
083: super .setProperty(name, value);
084: }
085:
086: public ESBase toPrimitive(int hint) throws Throwable {
087: return fun.toStr();
088: }
089:
090: public ESBase call(Call eval, int length) throws Throwable {
091: return fun.call(eval, length);
092: }
093:
094: public ESBase construct(Call eval, int length) throws Throwable {
095: if (constructor != null)
096: return constructor.construct(eval, length);
097: else
098: return super .construct(eval, length);
099: }
100:
101: protected void copy(Object obj) {
102: NativeWrapper dup = (NativeWrapper) obj;
103:
104: super .copy(dup);
105: dup.fun = fun;
106: dup.constructor = constructor;
107: }
108:
109: ESObject resinCopy() {
110: NativeWrapper dup = (NativeWrapper) dup();
111:
112: copy(dup);
113:
114: return dup;
115: }
116:
117: protected ESObject dup() {
118: return new NativeWrapper();
119: }
120: }
|