1 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 (); 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 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 82 { 83 fun.setProperty(name, value); 84 85 super.setProperty(name, value); 86 } 87 88 public ESBase toPrimitive(int hint) throws Throwable 89 { 90 return fun.toStr(); 91 } 92 93 public ESBase call(Call eval, int length) throws Throwable 94 { 95 return fun.call(eval, length); 96 } 97 98 public ESBase construct(Call eval, int length) throws Throwable 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 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 |