1
1
package sk .kottman .androlua ;
2
2
3
- import java .io .BufferedReader ;
4
- import java .io .ByteArrayOutputStream ;
5
- import java .io .IOException ;
6
- import java .io .InputStream ;
7
- import java .io .InputStreamReader ;
8
- import java .io .PrintStream ;
9
- import java .io .StringWriter ;
3
+ import java .io .*;
10
4
import java .net .ServerSocket ;
11
5
import java .net .Socket ;
12
6
18
12
import android .app .Activity ;
19
13
import android .content .res .AssetManager ;
20
14
import android .os .Bundle ;
15
+ import android .util .*;
21
16
import android .os .Handler ;
22
17
import android .text .method .ScrollingMovementMethod ;
23
18
import android .view .View ;
@@ -33,9 +28,13 @@ public class Main extends Activity implements OnClickListener,
33
28
private final static int LISTEN_PORT = 3333 ;
34
29
35
30
Button execute ;
36
- EditText source ;
37
- TextView status ;
38
- LuaState L ;
31
+
32
+ // public so we can play with these from Lua
33
+ public EditText source ;
34
+ public TextView status ;
35
+ public LuaState L ;
36
+
37
+ final StringBuilder output = new StringBuilder ();
39
38
40
39
Handler handler ;
41
40
ServerThread serverThread ;
@@ -61,7 +60,7 @@ public void onCreate(Bundle savedInstanceState) {
61
60
62
61
source = (EditText ) findViewById (R .id .source );
63
62
source .setOnLongClickListener (this );
64
- source .setText ("require 'greet' \n greet.hello('world' )\n " );
63
+ source .setText ("require 'import' \n print(Math:sin(2.3) )\n " );
65
64
66
65
status = (TextView ) findViewById (R .id .statusText );
67
66
status .setMovementMethod (ScrollingMovementMethod .getInstance ());
@@ -78,17 +77,25 @@ public void onCreate(Bundle savedInstanceState) {
78
77
JavaFunction print = new JavaFunction (L ) {
79
78
@ Override
80
79
public int execute () throws LuaException {
81
- StringBuilder sb = new StringBuilder ();
82
80
for (int i = 2 ; i <= L .getTop (); i ++) {
83
81
int type = L .type (i );
84
- String val = L .toString (i );
82
+ String stype = L .typeName (type );
83
+ String val = null ;
84
+ if (stype .equals ("userdata" )) {
85
+ Object obj = L .toJavaObject (i );
86
+ if (obj != null )
87
+ val = obj .toString ();
88
+ } else if (stype .equals ("boolean" )) {
89
+ val = L .toBoolean (i ) ? "true" : "false" ;
90
+ } else {
91
+ val = L .toString (i );
92
+ }
85
93
if (val == null )
86
- val = L . typeName ( type );
87
- sb .append (val );
88
- sb .append ("\t " );
94
+ val = stype ;
95
+ output .append (val );
96
+ output .append ("\t " );
89
97
}
90
- sb .append ("\n " );
91
- status .append (sb .toString ());
98
+ output .append ("\n " );
92
99
return 0 ;
93
100
}
94
101
};
@@ -159,15 +166,34 @@ public void run() {
159
166
Socket client = server .accept ();
160
167
BufferedReader in = new BufferedReader (
161
168
new InputStreamReader (client .getInputStream ()));
169
+ final PrintWriter out = new PrintWriter (client .getOutputStream ());
162
170
String line = null ;
163
171
while (!stopped && (line = in .readLine ()) != null ) {
164
- final String s = line ;
165
- handler .post (new Runnable () {
166
- @ Override
167
- public void run () {
168
- source .append (s + "\n " );
169
- }
170
- });
172
+ final String s = line .replace ('\001' , '\n' );
173
+ if (s .startsWith ("--mod:" )) {
174
+ int i1 = s .indexOf (':' ), i2 = s .indexOf ('\n' );
175
+ String mod = s .substring (i1 +1 ,i2 );
176
+ String file = getFilesDir ()+"/" +mod .replace ('.' , '/' )+".lua" ;
177
+ FileWriter fw = new FileWriter (file );
178
+ fw .write (s );
179
+ fw .close ();
180
+ // package.loaded[mod] = nil
181
+ L .getGlobal ("package" );
182
+ L .getField (-1 , "loaded" );
183
+ L .pushNil ();
184
+ L .setField (-2 , mod );
185
+ out .println ("wrote " + file + "\n " );
186
+ out .flush ();
187
+ } else {
188
+ handler .post (new Runnable () {
189
+ public void run () {
190
+ String res = safeEvalLua (s );
191
+ res = res .replace ('\n' , '\001' );
192
+ out .println (res );
193
+ out .flush ();
194
+ }
195
+ });
196
+ }
171
197
}
172
198
}
173
199
server .close ();
@@ -183,13 +209,19 @@ public void run() {
183
209
}
184
210
});
185
211
}
186
- }
187
-
188
- @ Override
189
- public void onClick (View view ) {
190
- String src = source .getText ().toString ();
212
+ }
191
213
192
- status .setText ("" );
214
+ String safeEvalLua (String src ) {
215
+ String res = null ;
216
+ try {
217
+ res = evalLua (src );
218
+ } catch (LuaException e ) {
219
+ res = e .getMessage ()+"\n " ;
220
+ }
221
+ return res ;
222
+ }
223
+
224
+ String evalLua (String src ) throws LuaException {
193
225
L .setTop (0 );
194
226
int ok = L .LloadString (src );
195
227
if (ok == 0 ) {
@@ -198,14 +230,28 @@ public void onClick(View view) {
198
230
L .remove (-2 );
199
231
L .insert (-2 );
200
232
ok = L .pcall (0 , 0 , -2 );
201
- if (ok == 0 ) {
202
- status .append ("Finished succesfully" );
203
- return ;
233
+ if (ok == 0 ) {
234
+ String res = output .toString ();
235
+ output .setLength (0 );
236
+ return res ;
204
237
}
205
238
}
239
+ throw new LuaException (errorReason (ok ) + ": " + L .toString (-1 ));
240
+ //return null;
241
+
242
+ }
243
+
244
+ public void onClick (View view ) {
245
+ String src = source .getText ().toString ();
246
+ status .setText ("" );
247
+ try {
248
+ String res = evalLua (src );
249
+ status .append (res );
250
+ status .append ("Finished succesfully" );
251
+ } catch (LuaException e ) {
252
+ Toast .makeText (this , e .getMessage (), Toast .LENGTH_LONG ).show ();
253
+ }
206
254
207
- String reason = errorReason (ok );
208
- Toast .makeText (this , reason + ": " + L .toString (-1 ), Toast .LENGTH_LONG ).show ();
209
255
}
210
256
211
257
private String errorReason (int error ) {
@@ -222,7 +268,6 @@ private String errorReason(int error) {
222
268
return "Unknown error " + error ;
223
269
}
224
270
225
- @ Override
226
271
public boolean onLongClick (View view ) {
227
272
source .setText ("" );
228
273
return true ;
0 commit comments