diff src/luan/LuanTable.java @ 1563:8fbcc4747091

remove LuanFunction.luan
author Franklin Schmidt <fschmidt@gmail.com>
date Mon, 09 Nov 2020 01:37:57 -0700
parents b89212fd04b5
children 364859d29ff5
line wrap: on
line diff
--- a/src/luan/LuanTable.java	Sun Nov 08 16:50:59 2020 -0700
+++ b/src/luan/LuanTable.java	Mon Nov 09 01:37:57 2020 -0700
@@ -20,6 +20,7 @@
 	public LuanClosure closure;
 	private LuanCloner cloner;
 	private boolean immutable = false;
+	private Luan luan;
 
 	public LuanTable() {}
 
@@ -99,11 +100,21 @@
 		}
 	}
 
+	private void checkLuan(Luan luan) {
+		check();
+		if( this.luan==null ) {
+			this.luan = luan;
+		} else if( this.luan != luan ) {
+			throw new RuntimeException("wrong luan");
+		}
+	}
+
 	private void completeClone(LuanTable clone,LuanCloner cloner) {
 		clone.map = cloner.clone(map);
 		clone.list = (List)cloner.clone(list);
 		clone.metatable = (LuanTable)cloner.clone(metatable);
 		clone.closure = (LuanClosure)cloner.clone(closure);
+		clone.luan = (Luan)cloner.clone(luan);
 	}
 
 	@Override public void makeImmutable(LuanImmutabler immutabler) throws LuanException {
@@ -133,12 +144,12 @@
 		return map!=null ? map : Collections.emptyMap();
 	}
 
-	public String toStringLuan() throws LuanException {
+	public String toStringLuan(Luan luan) throws LuanException {
 		Object h = getHandler("__to_string");
 		if( h == null )
 			return rawToString();
 		LuanFunction fn = Luan.checkFunction(h);
-		return Luan.checkString( Luan.first( fn.call(this) ) );
+		return Luan.checkString( Luan.first( fn.call(luan,this) ) );
 	}
 
 	public String rawToString() {
@@ -146,6 +157,7 @@
 	}
 
 	public Object get(Luan luan,Object key) throws LuanException {
+		//checkLuan(luan);
 		Object value = rawGet(key);
 		if( value != null )
 			return value;
@@ -154,7 +166,7 @@
 			return null;
 		if( h instanceof LuanFunction ) {
 			LuanFunction fn = (LuanFunction)h;
-			return Luan.first(fn.call(this,key));
+			return Luan.first(fn.call(luan,this,key));
 		}
 		return luan.index(h,key);
 	}
@@ -179,6 +191,7 @@
 	}
 
 	public void put(Luan luan,Object key,Object value) throws LuanException {
+		//checkLuan(luan);
 		Object h = getHandler("__new_index");
 		if( h==null || rawGet(key)!=null ) {
 			rawPut(key,value);
@@ -186,7 +199,7 @@
 		}
 		if( h instanceof LuanFunction ) {
 			LuanFunction fn = (LuanFunction)h;
-			fn.call(this,key,value);
+			fn.call(luan,this,key,value);
 			return;
 		}
 		if( h instanceof LuanTable ) {
@@ -295,11 +308,11 @@
 		Collections.sort(list(),cmp);
 	}
 
-	public int length() throws LuanException {
+	public int length(Luan luan) throws LuanException {
 		Object h = getHandler("__len");
 		if( h != null ) {
 			LuanFunction fn = Luan.checkFunction(h);
-			return (Integer)Luan.first(fn.call(this));
+			return (Integer)Luan.first(fn.call(luan,this));
 		}
 		return rawLength();
 	}
@@ -309,8 +322,8 @@
 		return list==null ? 0 : list.size();
 	}
 
-	public Iterable<Map.Entry> iterable() throws LuanException {
-		final Iterator<Map.Entry> iter = iterator();
+	public Iterable<Map.Entry> iterable(Luan luan) throws LuanException {
+		final Iterator<Map.Entry> iter = iterator(luan);
 		return new Iterable<Map.Entry>() {
 			public Iterator<Map.Entry> iterator() {
 				return iter;
@@ -327,16 +340,16 @@
 		};
 	}
 
-	public Iterator<Map.Entry> iterator() throws LuanException {
+	public Iterator<Map.Entry> iterator(final Luan luan) throws LuanException {
 		if( getHandler("__pairs") == null )
 			return rawIterator();
-		final LuanFunction fn = pairs();
+		final LuanFunction fn = pairs(luan);
 		return new Iterator<Map.Entry>() {
 			private Map.Entry<Object,Object> next = getNext();
 
 			private Map.Entry<Object,Object> getNext() {
 				try {
-					Object obj = fn.call();
+					Object obj = fn.call(luan);
 					if( obj==null )
 						return null;
 					Object[] a = (Object[])obj;
@@ -364,12 +377,12 @@
 		};
 	}
 
-	public LuanFunction pairs() throws LuanException {
+	public LuanFunction pairs(Luan luan) throws LuanException {
 		Object h = getHandler("__pairs");
 		if( h != null ) {
 			if( h instanceof LuanFunction ) {
 				LuanFunction fn = (LuanFunction)h;
-				Object obj = Luan.first(fn.call(this));
+				Object obj = Luan.first(fn.call(luan,this));
 				if( !(obj instanceof LuanFunction) )
 					throw new LuanException( "metamethod __pairs should return function but returned " + Luan.type(obj) );
 				return (LuanFunction)obj;
@@ -380,10 +393,10 @@
 	}
 
 	private LuanFunction rawPairs() {
-		return new LuanFunction(false) {  // ???
+		return new LuanFunction() {
 			final Iterator<Map.Entry> iter = rawIterator();
 
-			@Override public Object[] call(Object[] args) {
+			@Override public Object[] call(Luan luan,Object[] args) {
 				if( !iter.hasNext() )
 					return LuanFunction.NOTHING;
 				Map.Entry<Object,Object> entry = iter.next();
@@ -473,7 +486,7 @@
 	}
 
 	public boolean isSet() throws LuanException {
-		for( Map.Entry<Object,Object> entry : iterable() ) {
+		for( Map.Entry<Object,Object> entry : rawIterable() ) {
 			if( !entry.getValue().equals(Boolean.TRUE) )
 				return false;
 		}
@@ -482,7 +495,7 @@
 
 	public Set<Object> asSet() throws LuanException {
 		Set<Object> set = new HashSet<Object>();
-		for( Map.Entry<Object,Object> entry : iterable() ) {
+		for( Map.Entry<Object,Object> entry : rawIterable() ) {
 			set.add(entry.getKey());
 		}
 		return set;
@@ -490,7 +503,7 @@
 
 	public Map<Object,Object> asMap() throws LuanException {
 		Map<Object,Object> map = newMap();
-		for( Map.Entry<Object,Object> entry : iterable() ) {
+		for( Map.Entry<Object,Object> entry : rawIterable() ) {
 			map.put(entry.getKey(),entry.getValue());
 		}
 		return map;
@@ -536,13 +549,13 @@
 		Object h = getHandler("__gc");
 		if( h != null ) {
 			LuanFunction fn = Luan.checkFunction(h);
-			fn.call(this);
+			fn.call(new Luan(),this);  // ??? should be immutable
 		}
 		super.finalize();
 	}
 
-	public LuanFunction fn(Luan luan,String fnName) throws LuanException {
-		return (LuanFunction)get(luan,fnName);
+	public LuanFunction fn(String fnName) throws LuanException {
+		return (LuanFunction)rawGet(fnName);
 	}
 
 	public static void debug(LuanTable table) {