Mercurial Hosting > luan
comparison core/src/luan/impl/Closure.java @ 670:58ebfec6178b
all luan now compiles
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Tue, 12 Apr 2016 01:05:57 -0600 |
parents | 7780cafca27f |
children | d3e5414bdf4c |
comparison
equal
deleted
inserted
replaced
669:e320488819b6 | 670:58ebfec6178b |
---|---|
7 import luan.DeepCloner; | 7 import luan.DeepCloner; |
8 import luan.DeepCloneable; | 8 import luan.DeepCloneable; |
9 | 9 |
10 | 10 |
11 public abstract class Closure extends LuanFunction implements DeepCloneable, Cloneable { | 11 public abstract class Closure extends LuanFunction implements DeepCloneable, Cloneable { |
12 private final int stackSize; | 12 public Pointer[] upValues; |
13 private final int numArgs; | |
14 private final boolean isVarArg; | |
15 private UpValue[] upValues; | |
16 | 13 |
17 public Closure(LuanStateImpl luan,int stackSize,int numArgs,boolean isVarArg,UpValue.Getter[] upValueGetters) throws LuanException { | 14 public Closure(int nUpValues) throws LuanException { |
18 this.stackSize = stackSize; | 15 this.upValues = new Pointer[nUpValues]; |
19 this.numArgs = numArgs; | |
20 this.isVarArg = isVarArg; | |
21 this.upValues = new UpValue[upValueGetters.length]; | |
22 for( int i=0; i<upValues.length; i++ ) { | |
23 upValues[i] = upValueGetters[i].get(luan); | |
24 } | |
25 } | 16 } |
26 | 17 |
27 @Override public Closure shallowClone() { | 18 @Override public Closure shallowClone() { |
28 try { | 19 try { |
29 return (Closure)clone(); | 20 return (Closure)clone(); |
31 throw new RuntimeException(e); | 22 throw new RuntimeException(e); |
32 } | 23 } |
33 } | 24 } |
34 | 25 |
35 @Override public void deepenClone(DeepCloneable clone,DeepCloner cloner) { | 26 @Override public void deepenClone(DeepCloneable clone,DeepCloner cloner) { |
36 ((Closure)clone).upValues = (UpValue[])cloner.deepClone(upValues); | 27 ((Closure)clone).upValues = (Pointer[])cloner.deepClone(upValues); |
37 } | 28 } |
38 | 29 |
39 UpValue[] upValues() { | 30 @Override public final Object call(LuanState ls,Object[] args) throws LuanException { |
40 return upValues; | 31 LuanStateImpl luan = (LuanStateImpl)ls; |
32 Closure old = luan.closure; | |
33 luan.closure = this; | |
34 try { | |
35 return doCall(luan,args); | |
36 } finally { | |
37 luan.closure = old; | |
38 } | |
41 } | 39 } |
42 | 40 |
43 @Override public Object call(LuanState ls,Object[] args) throws LuanException { | 41 public abstract Object doCall(LuanState luan,Object[] args) throws LuanException; |
44 LuanStateImpl luan = (LuanStateImpl)ls; | |
45 Object[] varArgs = null; | |
46 if( isVarArg ) { | |
47 if( args.length > numArgs ) { | |
48 varArgs = new Object[ args.length - numArgs ]; | |
49 for( int i=0; i<varArgs.length; i++ ) { | |
50 varArgs[i] = args[numArgs+i]; | |
51 } | |
52 } else { | |
53 varArgs = LuanFunction.NOTHING; | |
54 } | |
55 } | |
56 Object[] stack = luan.newFrame(this,stackSize,varArgs); | |
57 final int n = Math.min(args.length,numArgs); | |
58 for( int i=0; i<n; i++ ) { | |
59 stack[i] = args[i]; | |
60 } | |
61 try { | |
62 return run(luan); | |
63 } catch(StackOverflowError e) { | |
64 throw new LuanException( "stack overflow", e ); | |
65 } finally { | |
66 luan.popFrame(); | |
67 } | |
68 } | |
69 | |
70 public abstract Object run(LuanStateImpl luan) throws LuanException; | |
71 } | 42 } |