Mercurial Hosting > luan
comparison src/luan/LuanClosure.java @ 1563:8fbcc4747091
remove LuanFunction.luan
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Mon, 09 Nov 2020 01:37:57 -0700 |
parents | e1a13e707bf3 |
children | c922446f53aa |
comparison
equal
deleted
inserted
replaced
1562:b89212fd04b5 | 1563:8fbcc4747091 |
---|---|
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 { | 6 public abstract class LuanClosure extends LuanFunction implements LuanCloneable, Cloneable { |
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 private Luan luan; | |
10 | 12 |
11 public LuanClosure(Luan luan,Pointer[] upValues,boolean javaOk,String sourceName) throws LuanException { | 13 public LuanClosure(Pointer[] upValues,boolean javaOk,String sourceName) throws LuanException { |
12 super(luan); | |
13 this.upValues = upValues; | 14 this.upValues = upValues; |
14 this.javaOk = javaOk; | 15 this.javaOk = javaOk; |
15 this.sourceName = sourceName; | 16 this.sourceName = sourceName; |
16 } | 17 } |
17 | 18 |
18 @Override protected void completeClone(LuanFunction dc,LuanCloner cloner) { | 19 @Override public final LuanClosure shallowClone() { |
20 check(); | |
21 try { | |
22 return (LuanClosure)clone(); | |
23 } catch(CloneNotSupportedException e) { | |
24 throw new RuntimeException(e); | |
25 } | |
26 } | |
27 | |
28 private void check() { | |
29 if( cloner != null ) { | |
30 completeClone(this,cloner); | |
31 cloner = null; | |
32 } | |
33 } | |
34 | |
35 private void checkLuan(Luan luan) { | |
36 check(); | |
37 if( this.luan==null ) { | |
38 this.luan = luan; | |
39 } else if( this.luan != luan ) { | |
40 throw new RuntimeException("wrong luan"); | |
41 } | |
42 } | |
43 | |
44 @Override public final void deepenClone(LuanCloneable dc,LuanCloner cloner) { | |
45 LuanClosure clone = (LuanClosure)dc; | |
46 switch( cloner.type ) { | |
47 case COMPLETE: | |
48 completeClone(clone,cloner); | |
49 return; | |
50 case INCREMENTAL: | |
51 clone.cloner = cloner; | |
52 return; | |
53 } | |
54 } | |
55 | |
56 private void completeClone(LuanClosure dc,LuanCloner cloner) { | |
19 LuanClosure clone = (LuanClosure)dc; | 57 LuanClosure clone = (LuanClosure)dc; |
20 clone.upValues = (Pointer[])cloner.clone(upValues); | 58 clone.upValues = (Pointer[])cloner.clone(upValues); |
21 super.completeClone(dc,cloner); | 59 clone.luan = (Luan)cloner.clone(luan); |
22 } | 60 } |
23 | 61 |
24 @Override public void makeImmutable(LuanImmutabler immutabler) throws LuanException { | 62 @Override public final void makeImmutable(LuanImmutabler immutabler) throws LuanException { |
25 immutabler.makeImmutable(upValues); | 63 immutabler.makeImmutable(upValues); |
26 super.makeImmutable(immutabler); | |
27 } | 64 } |
28 | 65 |
29 @Override public final Object call(Object... args) throws LuanException { | 66 @Override public final Object call(Luan luan,Object... args) throws LuanException { |
30 Luan luan = luan(); | 67 check(); |
31 luan.push(this); | 68 luan.push(this); |
32 try { | 69 try { |
33 return doCall(luan,args); | 70 return doCall(luan,args); |
34 } catch(StackOverflowError e) { | 71 } catch(StackOverflowError e) { |
35 throw new LuanException( "stack overflow", e ); | 72 throw new LuanException( "stack overflow", e ); |