Skip to content

Commit

Permalink
Fix for JRUBY-4944
Browse files Browse the repository at this point in the history
  • Loading branch information
yokolet committed Sep 18, 2010
1 parent efe6e1b commit 2d90a9a
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 51 deletions.
2 changes: 1 addition & 1 deletion src/org/jruby/embed/jsr223/JRubyBindings.java
Expand Up @@ -170,7 +170,7 @@ public Set keySet() {
keys.add(entry.getKey());
}
}
keys.addAll(container.getVarMap().keySet());
if (container.getVarMap().keySet() != null) keys.addAll(container.getVarMap().keySet());
return keys;
}

Expand Down
82 changes: 32 additions & 50 deletions src/org/jruby/embed/jsr223/JRubyEngine.java
Expand Up @@ -33,6 +33,7 @@
import java.io.PrintWriter;
import java.io.Reader;
import java.io.Writer;
import java.util.Set;
import javax.script.Bindings;
import javax.script.Compilable;
import javax.script.CompiledScript;
Expand Down Expand Up @@ -83,7 +84,12 @@ public Object eval(String script, ScriptContext context) throws ScriptException
if (script == null || context == null) {
throw new NullPointerException("either script or context is null");
}
setContext(context);
return evaluate(context, script);
}

private Object evaluate(ScriptContext context, String script) throws ScriptException {
if (this.context != context) setContext(context);
injectGlobalBinsings(context);
container.setScriptFilename(Utils.getFilename(this));
try {
EmbedEvalUnit unit = container.parse(script, Utils.getLineNumber(this));
Expand All @@ -98,6 +104,21 @@ public Object eval(String script, ScriptContext context) throws ScriptException
}
}

private void injectGlobalBinsings(ScriptContext context) {
Bindings globalMap = context.getBindings(ScriptContext.GLOBAL_SCOPE);
if (globalMap == null) return;

Set<String> engineKeys = (Set<String>)context.getBindings(ScriptContext.ENGINE_SCOPE).keySet();
Set<String> globalKeys = globalMap.keySet();
for (String key : globalKeys) {
if (engineKeys.contains(key)) continue;
Object value = context.getBindings(ScriptContext.GLOBAL_SCOPE).get(key);
if (value != null) {
container.put(key, value);
}
}
}

private ScriptException wrapException(Exception e) {
if (e.getCause() instanceof Exception) {
Writer w = container.getErrorWriter();
Expand Down Expand Up @@ -129,7 +150,12 @@ public Object eval(Reader reader, ScriptContext context) throws ScriptException
if (reader == null || context == null) {
throw new NullPointerException("either reader or context is null");
}
setContext(context);
return evaluate(context, reader);
}

private Object evaluate(ScriptContext context, Reader reader) throws ScriptException {
if (this.context != context) setContext(context);
injectGlobalBinsings(context);
String filename = Utils.getFilename(this);
try {
EmbedEvalUnit unit = container.parse(reader, filename, Utils.getLineNumber(this));
Expand All @@ -148,74 +174,30 @@ 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();
return JavaEmbedUtils.rubyToJava(ret);
} catch (Exception e) {
throw wrapException(e);
} finally {
if(isTerminationOn()) {
container.terminate();
}
}
return evaluate(this.context, script);
}

public Object eval(Reader reader) throws ScriptException {
if (reader == null) {
throw new NullPointerException("reader is null");
}
String filename = Utils.getFilename(this);
try {
EmbedEvalUnit unit = container.parse(reader, filename, Utils.getLineNumber(this));
IRubyObject ret = unit.run();
return JavaEmbedUtils.rubyToJava(ret);
} catch (Exception e) {
throw wrapException(e);
} finally {
if(isTerminationOn()) {
container.terminate();
}
}
return evaluate(this.context, reader);
}

public Object eval(String script, Bindings bindings) throws ScriptException {
if (script == null || bindings == null) {
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();
return JavaEmbedUtils.rubyToJava(ret);
} catch (Exception e) {
throw wrapException(e);
} finally {
if(isTerminationOn()) {
container.terminate();
}
}
return evaluate(this.context, script);
}

public Object eval(Reader reader, Bindings bindings) throws ScriptException {
if (reader == null || bindings == null) {
throw new NullPointerException("either reader or bindings is null");
}
getContext().setBindings(bindings, ScriptContext.ENGINE_SCOPE);
String filename = Utils.getFilename(this);
try {
EmbedEvalUnit unit = container.parse(reader, filename, Utils.getLineNumber(this));
IRubyObject ret = unit.run();
return JavaEmbedUtils.rubyToJava(ret);
} catch (Exception e) {
throw wrapException(e);
} finally {
if(isTerminationOn()) {
container.terminate();
}
}
return evaluate(this.context, reader);
}

public Object get(String key) {
Expand Down

0 comments on commit 2d90a9a

Please sign in to comment.