comparison src/luan/LuanClosure.java @ 1578:c922446f53aa

immutable threading
author Franklin Schmidt <fschmidt@gmail.com>
date Mon, 08 Feb 2021 14:16:19 -0700
parents 8fbcc4747091
children
comparison
equal deleted inserted replaced
1577:60e5c324adf9 1578:c922446f53aa
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 implements LuanMutable {
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; 10 private boolean immutable = false;
11 private Luan luan;
12 11
13 public LuanClosure(Pointer[] upValues,boolean javaOk,String sourceName) throws LuanException { 12 public LuanClosure(Pointer[] upValues,boolean javaOk,String sourceName) throws LuanException {
14 this.upValues = upValues; 13 this.upValues = upValues;
15 this.javaOk = javaOk; 14 this.javaOk = javaOk;
16 this.sourceName = sourceName; 15 this.sourceName = sourceName;
17 } 16 }
18 17
19 @Override public final LuanClosure shallowClone() { 18 @Override public final boolean isImmutable() {
20 check(); 19 return immutable;
21 try {
22 return (LuanClosure)clone();
23 } catch(CloneNotSupportedException e) {
24 throw new RuntimeException(e);
25 }
26 } 20 }
27 21
28 private void check() { 22 @Override public final void makeImmutable() {
29 if( cloner != null ) { 23 if(immutable)
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; 24 return;
50 case INCREMENTAL: 25 immutable = true;
51 clone.cloner = cloner; 26 LuanMutable.makeImmutable(upValues);
52 return;
53 }
54 }
55
56 private void completeClone(LuanClosure dc,LuanCloner cloner) {
57 LuanClosure clone = (LuanClosure)dc;
58 clone.upValues = (Pointer[])cloner.clone(upValues);
59 clone.luan = (Luan)cloner.clone(luan);
60 }
61
62 @Override public final void makeImmutable(LuanImmutabler immutabler) throws LuanException {
63 immutabler.makeImmutable(upValues);
64 } 27 }
65 28
66 @Override public final Object call(Luan luan,Object... args) throws LuanException { 29 @Override public final Object call(Luan luan,Object... args) throws LuanException {
67 check();
68 luan.push(this); 30 luan.push(this);
69 try { 31 try {
70 return doCall(luan,args); 32 return doCall(luan,args);
71 } catch(StackOverflowError e) { 33 } catch(StackOverflowError e) {
72 throw new LuanException( "stack overflow", e ); 34 throw new LuanException( "stack overflow", e );
73 } finally { 35 } finally {
74 luan.pop(); 36 luan.pop();
75 } 37 }
76 } 38 }
77 39
78 @Override public String toString() { 40 @Override public final String toString() {
79 return super.toString()+"="+sourceName; 41 return super.toString()+"="+sourceName;
80 } 42 }
81 43
82 public abstract Object doCall(Luan luan,Object[] args) throws LuanException; 44 public abstract Object doCall(Luan luan,Object[] args) throws LuanException;
83 } 45 }