comparison core/src/luan/LuanBit.java @ 404:d55e873e1f0d

metatables now only apply to tables
author Franklin Schmidt <fschmidt@gmail.com>
date Wed, 29 Apr 2015 07:04:40 -0600
parents 2456ef7ada02
children 91af5337b9ae
comparison
equal deleted inserted replaced
403:637f7ad85654 404:d55e873e1f0d
86 throw exception( "attempt to use a " + Luan.type(obj) + " as a boolean" ); 86 throw exception( "attempt to use a " + Luan.type(obj) + " as a boolean" );
87 } 87 }
88 } 88 }
89 89
90 public String toString(Object obj) throws LuanException { 90 public String toString(Object obj) throws LuanException {
91 LuanFunction fn = getHandlerFunction("__tostring",obj); 91 if( obj instanceof LuanTable ) {
92 if( fn != null ) 92 LuanFunction fn = getHandlerFunction("__tostring",(LuanTable)obj);
93 return checkString( Luan.first( call(fn,"__tostring",new Object[]{obj}) ) ); 93 if( fn != null )
94 return checkString( Luan.first( call(fn,"__tostring",new Object[]{obj}) ) );
95 }
94 return Luan.toString(obj); 96 return Luan.toString(obj);
95 } 97 }
96 98
97 public String repr(Object obj) throws LuanException { 99 public String repr(Object obj) throws LuanException {
98 LuanFunction fn = getHandlerFunction("__repr",obj); 100 if( obj instanceof LuanTable ) {
99 if( fn != null ) 101 LuanFunction fn = getHandlerFunction("__repr",(LuanTable)obj);
100 return checkString( Luan.first( call(fn,"__repr",new Object[]{obj}) ) ); 102 if( fn != null )
103 return checkString( Luan.first( call(fn,"__repr",new Object[]{obj}) ) );
104 }
101 String repr = Luan.repr(obj); 105 String repr = Luan.repr(obj);
102 if( repr==null ) 106 if( repr==null )
103 throw exception( "value '" + obj + "' doesn't support repr()" ); 107 throw exception( "value '" + obj + "' doesn't support repr()" );
104 return repr; 108 return repr;
105 } 109 }
106 110
107 public LuanFunction getHandlerFunction(String op,Object obj) throws LuanException { 111 public LuanFunction getHandlerFunction(String op,LuanTable t) throws LuanException {
108 Object f = luan.getHandler(op,obj); 112 Object f = luan.getHandler(op,t);
109 if( f == null ) 113 if( f == null )
110 return null; 114 return null;
111 return checkFunction(f); 115 return checkFunction(f);
112 } 116 }
113 117
114 public LuanFunction getBinHandler(String op,Object o1,Object o2) throws LuanException { 118 public LuanFunction getBinHandler(String op,Object o1,Object o2) throws LuanException {
115 LuanFunction f1 = getHandlerFunction(op,o1); 119 if( o1 instanceof LuanTable ) {
116 if( f1 != null ) 120 LuanFunction f1 = getHandlerFunction(op,(LuanTable)o1);
117 return f1; 121 if( f1 != null )
118 return getHandlerFunction(op,o2); 122 return f1;
123 }
124 return o2 instanceof LuanTable ? getHandlerFunction(op,(LuanTable)o2) : null;
119 } 125 }
120 126
121 public boolean isLessThan(Object o1,Object o2) throws LuanException { 127 public boolean isLessThan(Object o1,Object o2) throws LuanException {
122 if( o1 instanceof Number && o2 instanceof Number ) { 128 if( o1 instanceof Number && o2 instanceof Number ) {
123 Number n1 = (Number)o1; 129 Number n1 = (Number)o1;