Mercurial Hosting > luan
comparison src/luan/interp/Closure.java @ 48:64ecb7a3aad7
rename Lua to Luan
git-svn-id: https://luan-java.googlecode.com/svn/trunk@49 21e917c8-12df-6dd8-5cb6-c86387c605b9
author | fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9> |
---|---|
date | Fri, 28 Dec 2012 03:29:12 +0000 |
parents | src/luan/interp/LuaClosure.java@e3624b7cd603 |
children | 8ede219cd111 |
comparison
equal
deleted
inserted
replaced
47:659c7139e903 | 48:64ecb7a3aad7 |
---|---|
1 package luan.interp; | |
2 | |
3 import luan.LuanFunction; | |
4 import luan.LuanState; | |
5 import luan.LuanElement; | |
6 import luan.LuanException; | |
7 | |
8 | |
9 final class Closure extends LuanFunction { | |
10 private final Chunk chunk; | |
11 final UpValue[] upValues; | |
12 private final static UpValue[] NO_UP_VALUES = new UpValue[0]; | |
13 | |
14 Closure(Chunk chunk,LuanStateImpl lua) { | |
15 this.chunk = chunk; | |
16 UpValue.Getter[] upValueGetters = chunk.upValueGetters; | |
17 if( upValueGetters.length==0 ) { | |
18 upValues = NO_UP_VALUES; | |
19 } else { | |
20 upValues = new UpValue[upValueGetters.length]; | |
21 for( int i=0; i<upValues.length; i++ ) { | |
22 upValues[i] = upValueGetters[i].get(lua); | |
23 } | |
24 } | |
25 } | |
26 | |
27 public Object[] call(LuanState lua,Object[] args) throws LuanException { | |
28 return call(this,(LuanStateImpl)lua,args); | |
29 } | |
30 | |
31 private static Object[] call(Closure closure,LuanStateImpl lua,Object[] args) throws LuanException { | |
32 while(true) { | |
33 Chunk chunk = closure.chunk; | |
34 Object[] varArgs = null; | |
35 if( chunk.isVarArg ) { | |
36 if( args.length > chunk.numArgs ) { | |
37 varArgs = new Object[ args.length - chunk.numArgs ]; | |
38 for( int i=0; i<varArgs.length; i++ ) { | |
39 varArgs[i] = args[chunk.numArgs+i]; | |
40 } | |
41 } else { | |
42 varArgs = LuanFunction.EMPTY_RTN; | |
43 } | |
44 } | |
45 Object[] stack = lua.newFrame(closure,chunk.stackSize,varArgs); | |
46 final int n = Math.min(args.length,chunk.numArgs); | |
47 for( int i=0; i<n; i++ ) { | |
48 stack[i] = args[i]; | |
49 } | |
50 Object[] returnValues; | |
51 Closure tailFn; | |
52 try { | |
53 chunk.block.eval(lua); | |
54 } catch(ReturnException e) { | |
55 } finally { | |
56 returnValues = lua.returnValues; | |
57 closure = lua.tailFn; | |
58 lua.popFrame(); | |
59 } | |
60 if( closure == null ) | |
61 return returnValues; | |
62 args = returnValues; | |
63 } | |
64 } | |
65 | |
66 } |