comparison src/luan/LuanClosure.java @ 1335:e0cf0d108a77

major cleanup
author Franklin Schmidt <fschmidt@gmail.com>
date Thu, 14 Feb 2019 03:10:45 -0700
parents 25746915a241
children 643cf1c37723
comparison
equal deleted inserted replaced
1334:c88b486a9511 1335:e0cf0d108a77
1 package luan; 1 package luan;
2 2
3 import luan.impl.Pointer; 3 import luan.impl.Pointer;
4 4
5 5
6 public abstract class LuanClosure extends LuanFunction implements LuanCloneable, Cloneable { 6 public abstract class LuanClosure extends LuanFunction {
7 public Pointer[] upValues; 7 public Pointer[] upValues;
8 public boolean javaOk; 8 public boolean javaOk;
9 public final String sourceName; 9 public final String sourceName;
10 private LuanCloner cloner;
11 10
12 public LuanClosure(int nUpValues,boolean javaOk,String sourceName) throws LuanException { 11 public LuanClosure(Luan luan,int nUpValues,boolean javaOk,String sourceName) throws LuanException {
12 super(luan);
13 this.upValues = new Pointer[nUpValues]; 13 this.upValues = new Pointer[nUpValues];
14 this.javaOk = javaOk; 14 this.javaOk = javaOk;
15 this.sourceName = sourceName; 15 this.sourceName = sourceName;
16 } 16 }
17 17
18 @Override public LuanClosure shallowClone() { 18 @Override protected void completeClone(LuanFunction dc,LuanCloner cloner) {
19 check(); 19 LuanClosure clone = (LuanClosure)dc;
20 try { 20 clone.upValues = (Pointer[])cloner.clone(upValues);
21 return (LuanClosure)clone(); 21 super.completeClone(dc,cloner);
22 } catch(CloneNotSupportedException e) {
23 throw new RuntimeException(e);
24 }
25 } 22 }
26 23
27 @Override public void deepenClone(LuanCloneable dc,LuanCloner cloner) { 24 @Override public final Object call(Object[] args) throws LuanException {
28 LuanClosure clone = (LuanClosure)dc; 25 Luan luan = luan();
29 switch( cloner.type ) {
30 case COMPLETE:
31 clone.upValues = (Pointer[])cloner.clone(upValues);
32 return;
33 case INCREMENTAL:
34 clone.cloner = cloner;
35 clone.upValues = upValues;
36 return;
37 }
38 }
39
40 private void check() {
41 if( cloner != null ) {
42 upValues = (Pointer[])cloner.clone(upValues);
43 cloner = null;
44 }
45 }
46
47 @Override public final Object call(Luan luan,Object[] args) throws LuanException {
48 if( luan.isLocked )
49 throw new RuntimeException("luan is locked");
50 check();
51 luan.push(this); 26 luan.push(this);
52 try { 27 try {
53 return doCall(luan,args); 28 return doCall(luan,args);
54 } catch(StackOverflowError e) { 29 } catch(StackOverflowError e) {
55 throw new LuanException( "stack overflow" ); 30 throw new LuanException( "stack overflow" );
56 } finally { 31 } finally {
57 luan.pop(); 32 luan.pop();
58 } 33 }
59 } 34 }
60 35
36 @Override public String toString() {
37 return super.toString()+"="+sourceName;
38 }
39
61 public abstract Object doCall(Luan luan,Object[] args) throws LuanException; 40 public abstract Object doCall(Luan luan,Object[] args) throws LuanException;
62 } 41 }