My favorites
|
Sign in
lotrepls
Many languages, one REPL (in the browser)
Project Home
Downloads
Issues
Source
Checkout
|
Browse
|
Changes
|
r7
Source path:
svn
/
trunk
/
src
/
com
/
google
/
lotrepls
/
server
/
interpreters
/
ScalaInterpreter.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/*
* Copyright 2009 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.google.lotrepls.server.interpreters;
import java.io.ByteArrayOutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.net.URL;
import javax.servlet.ServletContext;
import scala.tools.nsc.Settings;
/**
* A Scala Interpreter.
*
*/
public class ScalaInterpreter implements Interpreter {
private ByteArrayOutputStream interpOutStream;
private scala.tools.nsc.Interpreter interp;
public void init(ServletContext context) {
String rtJarPath = getJarFromResource(getClass().getResource(
"/java/lang/Object.class"));
String scalaLibPath = getJarFromResource(getClass().getResource(
"/scala/Array.class"));
interpOutStream = new ByteArrayOutputStream();
Settings settings = new Settings();
String codebasePath = rtJarPath + " " + scalaLibPath;
settings.Xcodebase().value_$eq(codebasePath.toString());
settings.classpath().value_$eq("");
settings.bootclasspath().value_$eq("");
settings.outdir().value_$eq(".");
settings.extdirs().value_$eq("");
interp = new scala.tools.nsc.Interpreter(settings, new PrintWriter(
new OutputStreamWriter(interpOutStream))) {
@Override
public ClassLoader parentClassLoader() {
return Thread.currentThread().getContextClassLoader();
}
};
}
public Context createContext() {
return new Ctx();
}
public String execute(Context context, String script) throws Exception {
StringBuffer strBuffer = new StringBuffer();
interp.reset();
interpOutStream.reset();
PrintStream output = new PrintStream(interpOutStream);
// We set System.out and System.err such that we capture anything
// that runs during interpretation (Such as a System.out.println
// call). The _only_ reason this doesn't fail due to concurrency,
// is because we don't get concurrent requests to a single JVM.
PrintStream out = System.out;
PrintStream err = System.err;
System.setOut(output);
System.setErr(output);
try {
// Eval the user text
interp.interpret(script);
} finally {
System.setOut(out);
System.setErr(err);
}
output.flush();
strBuffer.append(new String(interpOutStream.toByteArray(), "UTF-8"));
return interpOutStream.toString();
}
/**
* Returns a URL to a Jar file in which a resource from that Jar file is
* located. This is the easiest way that we can locate certain Jar files.
*/
private static String getJarFromResource(URL resource) {
String path = resource.toString();
int indexOfFile = path.indexOf("file:");
int indexOfSeparator = path.lastIndexOf('!');
return path.substring(indexOfFile, indexOfSeparator);
}
// NB: not serializable
static class Ctx implements Context {
}
}
Show details
Hide details
Change log
r3
by jam...@google.com on Apr 11, 2009
Diff
Add source for deps.
Go to:
/trunk/COPYING
/trunk/build-gwt.xml
/trunk/build.xml
/trunk/deps/bsh/bsh-2.0b4-src.jar
...clojure/clojure_20090320_src.zip
....1-20090327.130819-1-sources.jar
.../jruby/jruby-src-1.2.0RC2.tar.gz
/trunk/deps/jruby/jruby.jar
...deps/jscheme/jscheme-7.2-src.tgz
/trunk/deps/rhino/js.jar
...nk/deps/rhino/rhino1_7R2_src.zip
/trunk/src
/trunk/src/com
/trunk/src/com/google
/trunk/src/com/google/lotrepls
...google/lotrepls/LotREPLs.gwt.xml
...k/src/com/google/lotrepls/client
...repls/client/CommandHistory.java
...trepls/client/CommandPrompt.java
...le/lotrepls/client/LotREPLs.java
...ls/client/LotREPLsResources.java
...le/lotrepls/client/gaeAndGwt.gif
...k/src/com/google/lotrepls/server
...epls/server/LotREPLsApiImpl.java
...gle/lotrepls/server/interpreters
...reters/BeanShellInterpreter.java
...rpreters/ClojureInterpreter.java
...server/interpreters/Context.java
...erpreters/GroovyInterpreter.java
...er/interpreters/Interpreter.java
...rpreters/InterpreterFactory.java
...eters/JavaScriptInterpreter.java
.../interpreters/PCInterpreter.java
...erpreters/PythonInterpreter.java
...nterpreters/RubyInterpreter.java
...terpreters/ScalaInterpreter.java
...erpreters/SchemeInterpreter.java
...k/src/com/google/lotrepls/shared
...shared/InterpreterException.java
...epls/shared/InterpreterType.java
...lotrepls/shared/LotREPLsApi.java
...pls/shared/LotREPLsApiAsync.java
Project members,
sign in
to write a code review
Older revisions
All revisions of this file
File info
Size: 3418 bytes, 111 lines
View raw file