Permalink
Browse files

Merge remote branch 'origin/master'

  • Loading branch information...
2 parents 915ca47 + b4072d7 commit 51e8ba2b4d8680cfc83a81f221c0aa12b9d7f88d Danny Yoo committed Apr 25, 2011
@@ -0,0 +1,72 @@
+package org.wescheme.project;
+
+import java.io.IOException;
+import java.io.OutputStreamWriter;
+import java.io.UnsupportedEncodingException;
+import java.io.InputStream;
+import java.io.BufferedInputStream;
+import java.net.HttpURLConnection;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.net.URLEncoder;
+
+import org.wescheme.project.Compiler.BadCompilationResult;
+import org.wescheme.project.Compiler.CompilationResult;
+import org.wescheme.project.Compiler.GoodCompilationResult;
+
+public class AndroidPackager {
+
+ private static final String compilationServletURL = "http://moby-compiler.cs.brown.edu/servlets/standalone.ss";
+
+ /*
+ * compile: SourceCode -> ObjectCode
+ * Compiles the source code, using an external compilation server, and returns the compiled code.
+ */
+ public static CompilationResult compile(String programName, String programSource){
+ System.out.println("compile");
+ try {
+ URL url = new URL(compilationServletURL);
+
+ String data = "name=" + URLEncoder.encode(programName, "UTF-8") +
+ "&format=json"+
+ "&program=" + URLEncoder.encode(programSource, "UTF-8");
+
+ HttpURLConnection conn = (HttpURLConnection) url.openConnection();
+ conn.setDoOutput(true);
+ OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
+ wr.write(data);
+ wr.flush();
+
+ if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
+ // FIXME: the content here is json and we need to parse it.
+ String compiledCode = readStream(conn.getInputStream());
+ return new GoodCompilationResult(compiledCode);
+ } else {
+ String errorMessage = readStream(conn.getErrorStream());
+ return new BadCompilationResult(errorMessage);
+ }
+ } catch (MalformedURLException e) {
+ throw new RuntimeException(e);
+ } catch (UnsupportedEncodingException e) {
+ throw new RuntimeException(e);
+ } catch (IOException e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ private static String readStream(InputStream stream) {
+ BufferedInputStream bs = new BufferedInputStream(stream);
+ int nextChar;
+ StringBuilder builder = new StringBuilder();
+ try {
+ while ((nextChar = bs.read()) != -1) {
+ builder.append((char) nextChar);
+ }
+ return builder.toString();
+ } catch (IOException e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+
+}
@@ -11,6 +11,7 @@
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
+import java.util.Set;
import java.util.HashSet;
public class Compiler extends HttpServlet
@@ -22,6 +23,7 @@
public interface CompilationResult {
boolean isBad();
String getCompiledCode();
+ Set<String> getPermissions();
String getErrorMessage();
}
@@ -33,6 +35,7 @@ public GoodCompilationResult(String compiledCode) {
public boolean isBad() { return false; }
public String getCompiledCode() { return this.compiledCode; }
public String getErrorMessage() { return null; }
+ public Set<String> getPermissions() { return new HashSet<String>(); }
}
public static class BadCompilationResult implements CompilationResult {
@@ -43,6 +46,7 @@ public BadCompilationResult(String errorMessage) {
public boolean isBad() { return true; }
public String getCompiledCode() { throw new UnsupportedOperationException(); }
public String getErrorMessage() { return this.errorMessage; }
+ public Set<String> getPermissions() { throw new UnsupportedOperationException(); }
}
@@ -52,7 +56,10 @@ public static ObjectCode compile(SourceCode src) {
if (result.isBad()) {
throw new RuntimeException(result.getErrorMessage());
} else {
- return new ObjectCode(result.getCompiledCode(), new HashSet<String>(), false);
+ return new ObjectCode(
+ result.getCompiledCode(),
+ result.getPermissions(),
+ false);
}
}
@@ -76,6 +83,7 @@ public static CompilationResult compile(String programName, String programSource
if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
String compiledCode = readStream(conn.getInputStream());
+ // FIXME: use format json, grab the set of permissions too!
return new GoodCompilationResult(compiledCode);
} else {
String errorMessage = readStream(conn.getErrorStream());
@@ -0,0 +1,32 @@
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
+
+<html>
+<head>
+
+ <!-- war/js/mzscheme-vm/support.js -->
+ <script type="text/javascript" charset="utf-8" src="support.js"></script>
+ <!-- war/js/mzscheme-vm/evaluator.js -->
+ <script type="text/javascript" charset="utf-8" src="evaluator.js"></script>
+ <!-- war/js/mzscheme-vm/collections.js -->
+ <script type="text/javascript" charset="utf-8" src="collections.js"></script>
+
+ <!-- The program's bytecode will be written here, under the variable program.bytecode -->
+ <script type="text/javascript" charset="utf-8" src="program.js"></script>
+
+ <!-- war/android-packager/main.js -->
+ <script type="text/javascript" charset="utf-8" src="main.js"></script>
+</head>
+
+<body id="body" onload="main.onload();"
+ onunload="main.onunload();">
+
+<div id="loading">
+ Loading; please wait.
+</div>
+
+<div style="border-style: solid; border-width: thin">
+ <div id="jsworld-div"></div>
+</div>
+
+</body>
+</html>
@@ -0,0 +1,90 @@
+var main = {};
+
+
+(function() {
+
+ main.onload = function() {
+ document.getElementById("loading").style['display'] = "none";
+
+ var onSuccess = function() {}
+ var onFail = function(err) { reportError(err); }
+ evaluator.executeCompiledProgram(program.bytecode,
+ onSuccess,
+ onFail);
+ };
+
+ main.onunload = function() {
+ evaluator.requestBreak();
+ };
+
+
+
+ //////////////////////////////////////////////////////////////////////
+
+ var evaluator = new Evaluator(
+ { write: function(x) { writeToInteractions(x) },
+ writeError: function(err) { reportError(err) },
+ });
+
+ evaluator.makeToplevelNode = function() {
+ var jsworldDiv = document.getElementById("jsworld-div");
+
+ var dom = document.createElement("div");
+ var innerDom = document.createElement("div");
+ dom.appendChild(innerDom);
+ jsworldDiv.appendChild(dom);
+
+ return innerDom;
+ };
+
+
+ var writeToInteractions = function(thing) {
+ var interactionsDiv = document.body;
+ if (typeof thing === 'string' || typeof thing === 'number') {
+ var dom = document.createElement('div');
+ dom.style['white-space'] = 'pre';
+ dom.appendChild(document.createTextNode(thing + ''));
+ interactionsDiv.appendChild(dom);
+ } else {
+ interactionsDiv.appendChild(thing);
+ // IE Hack for excanvas elements.
+ // See: http://www.lrbabe.com/?p=104
+ // The issue is that if we use excanvas, and it has
+ // vml, things don't render if the element hasn't been
+ // attached to the dom. Ugly stuff.
+ if (/MSIE/.test(navigator.userAgent) && !window.opera) {
+ setTimeout(function() { thing.outerHTML = thing.outerHTML; },
+ 0);
+ }
+ }
+ };
+
+
+ var reportError = function(exn) {
+ // Under google-chrome, this will produce a nice error stack
+ // trace that we can deal with.
+ if (typeof(console) !== 'undefined' && console.log &&
+ exn && exn.stack) {
+ console.log(exn.stack);
+ }
+
+ var domElt = document.createElement('div');
+ domElt.style['color'] = 'red';
+ domElt.appendChild(document.createTextNode(evaluator.getMessageFromExn(exn)+""));
+
+ var stacktrace = evaluator.getTraceFromExn(exn);
+ for (var i = 0; i < stacktrace.length; i++) {
+ domElt.appendChild(document.createElement("br"));
+ domElt.appendChild(document.createTextNode(
+ "in " + stacktrace[i].id +
+ ", at offset " + stacktrace[i].offset +
+ ", line " + stacktrace[i].line +
+ ", column " + stacktrace[i].column +
+ ", span " + stacktrace[i].span));
+ };
+
+ writeToInteractions(domElt);
+ };
+
+
+})();
Oops, something went wrong.

0 comments on commit 51e8ba2

Please sign in to comment.