Skip to content

Commit 22c0cef

Browse files
committed
Server now returns result on same socket; improved printing logic
1 parent 500a873 commit 22c0cef

File tree

1 file changed

+82
-37
lines changed

1 file changed

+82
-37
lines changed

src/sk/kottman/androlua/Main.java

+82-37
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,6 @@
11
package sk.kottman.androlua;
22

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.*;
104
import java.net.ServerSocket;
115
import java.net.Socket;
126

@@ -18,6 +12,7 @@
1812
import android.app.Activity;
1913
import android.content.res.AssetManager;
2014
import android.os.Bundle;
15+
import android.util.*;
2116
import android.os.Handler;
2217
import android.text.method.ScrollingMovementMethod;
2318
import android.view.View;
@@ -33,9 +28,13 @@ public class Main extends Activity implements OnClickListener,
3328
private final static int LISTEN_PORT = 3333;
3429

3530
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();
3938

4039
Handler handler;
4140
ServerThread serverThread;
@@ -61,7 +60,7 @@ public void onCreate(Bundle savedInstanceState) {
6160

6261
source = (EditText) findViewById(R.id.source);
6362
source.setOnLongClickListener(this);
64-
source.setText("require 'greet'\ngreet.hello('world')\n");
63+
source.setText("require 'import'\nprint(Math:sin(2.3))\n");
6564

6665
status = (TextView) findViewById(R.id.statusText);
6766
status.setMovementMethod(ScrollingMovementMethod.getInstance());
@@ -78,17 +77,25 @@ public void onCreate(Bundle savedInstanceState) {
7877
JavaFunction print = new JavaFunction(L) {
7978
@Override
8079
public int execute() throws LuaException {
81-
StringBuilder sb = new StringBuilder();
8280
for (int i = 2; i <= L.getTop(); i++) {
8381
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+
}
8593
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");
8997
}
90-
sb.append("\n");
91-
status.append(sb.toString());
98+
output.append("\n");
9299
return 0;
93100
}
94101
};
@@ -159,15 +166,34 @@ public void run() {
159166
Socket client = server.accept();
160167
BufferedReader in = new BufferedReader(
161168
new InputStreamReader(client.getInputStream()));
169+
final PrintWriter out = new PrintWriter(client.getOutputStream());
162170
String line = null;
163171
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+
}
171197
}
172198
}
173199
server.close();
@@ -183,13 +209,19 @@ public void run() {
183209
}
184210
});
185211
}
186-
}
187-
188-
@Override
189-
public void onClick(View view) {
190-
String src = source.getText().toString();
212+
}
191213

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 {
193225
L.setTop(0);
194226
int ok = L.LloadString(src);
195227
if (ok == 0) {
@@ -198,14 +230,28 @@ public void onClick(View view) {
198230
L.remove(-2);
199231
L.insert(-2);
200232
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;
204237
}
205238
}
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+
}
206254

207-
String reason = errorReason(ok);
208-
Toast.makeText(this, reason + ": " + L.toString(-1), Toast.LENGTH_LONG).show();
209255
}
210256

211257
private String errorReason(int error) {
@@ -222,7 +268,6 @@ private String errorReason(int error) {
222268
return "Unknown error " + error;
223269
}
224270

225-
@Override
226271
public boolean onLongClick(View view) {
227272
source.setText("");
228273
return true;

0 commit comments

Comments
 (0)