Skip to content

Commit

Permalink
Add ScriptEngine.FILENAME handling to three eval methods. Also, fix l…
Browse files Browse the repository at this point in the history
…oad path setup.
  • Loading branch information
yokolet committed Sep 16, 2010
1 parent 9bc0a8d commit f00eb88
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 5 deletions.
9 changes: 6 additions & 3 deletions src/org/jruby/embed/jsr223/JRubyEngine.java
Expand Up @@ -84,6 +84,7 @@ public Object eval(String script, ScriptContext context) throws ScriptException
throw new NullPointerException("either script or context is null");
}
setContext(context);
container.setScriptFilename(Utils.getFilename(this));
try {
EmbedEvalUnit unit = container.parse(script, Utils.getLineNumber(this));
IRubyObject ret = unit.run();
Expand Down Expand Up @@ -129,7 +130,7 @@ public Object eval(Reader reader, ScriptContext context) throws ScriptException
throw new NullPointerException("either reader or context is null");
}
setContext(context);
String filename = (String) context.getAttribute(ScriptEngine.FILENAME);
String filename = Utils.getFilename(this);
try {
EmbedEvalUnit unit = container.parse(reader, filename, Utils.getLineNumber(this));
IRubyObject ret = unit.run();
Expand All @@ -147,6 +148,7 @@ public Object eval(String script) throws ScriptException {
if (script == null) {
throw new NullPointerException("script is null");
}
container.setScriptFilename(Utils.getFilename(this));
try {
EmbedEvalUnit unit = container.parse(script, Utils.getLineNumber(this));
IRubyObject ret = unit.run();
Expand All @@ -164,7 +166,7 @@ public Object eval(Reader reader) throws ScriptException {
if (reader == null) {
throw new NullPointerException("reader is null");
}
String filename = (String) getContext().getAttribute(ScriptEngine.FILENAME);
String filename = Utils.getFilename(this);
try {
EmbedEvalUnit unit = container.parse(reader, filename, Utils.getLineNumber(this));
IRubyObject ret = unit.run();
Expand All @@ -183,6 +185,7 @@ public Object eval(String script, Bindings bindings) throws ScriptException {
throw new NullPointerException("either script or bindings is null");
}
getContext().setBindings(bindings, ScriptContext.ENGINE_SCOPE);
container.setScriptFilename(Utils.getFilename(this));
try {
EmbedEvalUnit unit = container.parse(script, Utils.getLineNumber(this));
IRubyObject ret = unit.run();
Expand All @@ -201,7 +204,7 @@ public Object eval(Reader reader, Bindings bindings) throws ScriptException {
throw new NullPointerException("either reader or bindings is null");
}
getContext().setBindings(bindings, ScriptContext.ENGINE_SCOPE);
String filename = (String) getContext().getAttribute(ScriptEngine.FILENAME);
String filename = Utils.getFilename(this);
try {
EmbedEvalUnit unit = container.parse(reader, filename, Utils.getLineNumber(this));
IRubyObject ret = unit.run();
Expand Down
5 changes: 5 additions & 0 deletions src/org/jruby/embed/jsr223/Utils.java
Expand Up @@ -53,4 +53,9 @@ static int getLineNumber(ScriptEngine engine) {
return 0;
}

static String getFilename(ScriptEngine engine) {
Object filename = engine.getContext().getAttribute(ScriptEngine.FILENAME);
return filename != null ? (String)filename : "<script>";
}

}
19 changes: 17 additions & 2 deletions src/org/jruby/embed/util/SystemPropertyCatcher.java
Expand Up @@ -192,11 +192,26 @@ private static String findFromJar(Object instance) throws URISyntaxException {
*/
public static List<String> findLoadPaths() {
String paths = System.getProperty(PropertyName.CLASSPATH.toString());
List<String> loadPaths = new ArrayList<String>();
if (paths == null) {
paths = System.getProperty("java.class.path");
}
if (paths == null) return new ArrayList<String>();
else return Arrays.asList(paths.split(File.pathSeparator));
if (paths == null) return loadPaths;
String[] possiblePaths = paths.split(File.pathSeparator);
String[] prefixes = {"file", "url"};
for (int i=0; i<possiblePaths.length; i++) {
int startIndex = i;
for (int j=0; j < prefixes.length; j++) {
if (prefixes[j].equals(possiblePaths[i]) && i < possiblePaths.length - 1) {
loadPaths.add(possiblePaths[i] + ":" + possiblePaths[++i]);
break;
}
}
if (startIndex == i) {
loadPaths.add(possiblePaths[i]);
}
}
return loadPaths;
}

/**
Expand Down

0 comments on commit f00eb88

Please sign in to comment.