1 28 29 package com.caucho.es; 30 31 import com.caucho.util.Exit; 32 33 37 final public class ScriptClosure { 38 private Script script; 39 private Global resin; 40 private ESGlobal global; 41 42 ScriptClosure(Global resin, ESGlobal global, Script script) 43 { 44 this.script = script; 45 this.resin = resin; 46 this.global = global; 47 } 48 49 56 public boolean isModified() 57 { 58 return script.isModified(); 59 } 60 61 67 public synchronized Object call(String name) 68 throws Throwable 69 { 70 Call call = resin.getCall(); 71 call.caller = call; 72 call.top = 1; 73 call.setArg(-1, global); 74 75 Object value = call(getFunction(name), call, 0); 76 77 resin.freeCall(call); 78 79 return value; 80 } 81 82 91 public synchronized Object call(String name, Object a) 92 throws Throwable 93 { 94 Call call = resin.getCall(); 95 call.caller = call; 96 call.top = 1; 97 call.setArg(-1, global); 98 call.setArg(0, resin.objectWrap(a)); 99 100 Object value = call(getFunction(name), call, 1); 101 102 resin.freeCall(call); 103 104 return value; 105 } 106 107 117 public synchronized Object call(String name, Object a, Object b) 118 throws Throwable 119 { 120 Call call = resin.getCall(); 121 call.caller = call; 122 call.top = 1; 123 call.setArg(-1, global); 124 call.setArg(0, resin.objectWrap(a)); 125 call.setArg(1, resin.objectWrap(b)); 126 127 Object value = call(getFunction(name), call, 2); 128 129 resin.freeCall(call); 130 131 return value; 132 } 133 134 145 public synchronized Object call(String name, Object a, Object b, Object c) 146 throws Throwable 147 { 148 Call call = resin.getCall(); 149 call.caller = call; 150 call.top = 1; 151 call.setArg(-1, global); 152 call.setArg(0, resin.objectWrap(a)); 153 call.setArg(1, resin.objectWrap(b)); 154 call.setArg(2, resin.objectWrap(c)); 155 156 Object value = call(getFunction(name), call, 3); 157 158 resin.freeCall(call); 159 160 return value; 161 } 162 163 175 public synchronized Object call(String name, Object a, Object b, 176 Object c, Object d) 177 throws Throwable 178 { 179 Call call = resin.getCall(); 180 call.caller = call; 181 call.top = 1; 182 call.setArg(-1, global); 183 call.setArg(0, resin.objectWrap(a)); 184 call.setArg(1, resin.objectWrap(b)); 185 call.setArg(2, resin.objectWrap(c)); 186 call.setArg(3, resin.objectWrap(d)); 187 188 Object value = call(getFunction(name), call, 4); 189 190 resin.freeCall(call); 191 192 return value; 193 } 194 195 204 public synchronized Object call(String name, Object []args) 205 throws Throwable 206 { 207 Call call = resin.getCall(); 208 call.caller = call; 209 call.setArg(0, global); 210 call.top = 1; 211 for (int i = 0; i < args.length; i++) 212 call.setArg(i, resin.objectWrap(args[i])); 213 214 Object value = call(getFunction(name), call, args.length); 215 216 resin.freeCall(call); 217 218 return value; 219 } 220 221 private ESClosure getFunction(String name) 222 throws Throwable 223 { 224 ESBase fun = global.hasProperty(ESString.create(name)); 225 226 if (fun == null) 227 throw new ESException("no such function `" + name + "'"); 228 229 if (! (fun instanceof ESClosure)) 230 throw new ESException(name + " should be function: " + fun); 231 232 return (ESClosure) fun; 233 } 234 235 private Object call(ESClosure closure, Call call, int length) 236 throws Throwable 237 { 238 int scopeLength = closure.scopeLength; 239 call.scopeLength = scopeLength; 240 for (int i = 0; i < scopeLength; i++) 241 call.scope[i] = closure.scope[i]; 242 243 247 248 boolean doExit = Exit.addExit(); 249 Global old = resin.begin(); 250 try { 251 ESBase value = closure.call(call, length); 252 253 return value == null ? null : value.toJavaObject(); 254 } finally { 255 resin.end(old); 256 if (doExit) 257 Exit.exit(); 258 } 259 } 260 261 267 public synchronized Object getProperty(String name) 268 { 269 try { 270 ESBase object = global.getProperty(name); 271 272 return object.toJavaObject(); 273 } catch (Throwable e) { 274 return null; 275 } 276 } 277 278 284 public synchronized void setProperty(String name, Object value) 285 { 286 try { 287 global.setProperty(name, resin.objectWrap(value)); 288 } catch (Throwable e) { 289 } 290 } 291 } 292 | Popular Tags |