comparison core/src/luan/Luan.java @ 647:8e8c30b72e9b

move methods from LuanState to Luan
author Franklin Schmidt <fschmidt@gmail.com>
date Tue, 29 Mar 2016 20:39:14 -0600
parents 6cc2f047019b
children d3e5414bdf4c
comparison
equal deleted inserted replaced
646:cdc70de628b5 647:8e8c30b72e9b
83 s = s.replace("\"","\\\""); 83 s = s.replace("\"","\\\"");
84 s = s.replace("\'","\\'"); 84 s = s.replace("\'","\\'");
85 return s; 85 return s;
86 } 86 }
87 87
88
89 // from LuanState
90
91 public static Boolean checkBoolean(Object obj) throws LuanException {
92 if( obj instanceof Boolean )
93 return (Boolean)obj;
94 throw new LuanException("attempt to use a " + Luan.type(obj) + " value as a boolean" );
95 }
96
97 public static String checkString(Object obj) throws LuanException {
98 if( obj instanceof String )
99 return (String)obj;
100 throw new LuanException("attempt to use a " + Luan.type(obj) + " value as a string" );
101 }
102
103 public static LuanFunction checkFunction(Object obj) throws LuanException {
104 if( obj instanceof LuanFunction )
105 return (LuanFunction)obj;
106 throw new LuanException("attempt to call a " + Luan.type(obj) + " value" );
107 }
108
109 public static boolean isLessThan(LuanState luan,Object o1,Object o2) throws LuanException {
110 if( o1 instanceof Number && o2 instanceof Number ) {
111 Number n1 = (Number)o1;
112 Number n2 = (Number)o2;
113 return n1.doubleValue() < n2.doubleValue();
114 }
115 if( o1 instanceof String && o2 instanceof String ) {
116 String s1 = (String)o1;
117 String s2 = (String)o2;
118 return s1.compareTo(s2) < 0;
119 }
120 LuanFunction fn = getBinHandler("__lt",o1,o2);
121 if( fn != null )
122 return checkBoolean( Luan.first(fn.call(luan,new Object[]{o1,o2})) );
123 throw new LuanException( "attempt to compare " + Luan.type(o1) + " with " + Luan.type(o2) );
124 }
125
126 public static LuanFunction getBinHandler(String op,Object o1,Object o2) throws LuanException {
127 if( o1 instanceof LuanTable ) {
128 LuanFunction f1 = getHandlerFunction(op,(LuanTable)o1);
129 if( f1 != null )
130 return f1;
131 }
132 return o2 instanceof LuanTable ? getHandlerFunction(op,(LuanTable)o2) : null;
133 }
134
135 public static LuanFunction getHandlerFunction(String op,LuanTable t) throws LuanException {
136 Object f = t.getHandler(op);
137 if( f == null )
138 return null;
139 return checkFunction(f);
140 }
141
142
143
88 private Luan() {} // never 144 private Luan() {} // never
89 } 145 }