Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
**** BEGIN LICENSE BLOCK ***** Version: EPL 1.0/GPL 2.0/LGPL 2.1 The contents of this file are subject to the Eclipse Public License Version 1.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the specific language governing rights and limitations under the License. Copyright (C) 2009-2010 Yoko Harada <> Alternatively, the contents of this file may be used under the terms of either of the GNU General Public License Version 2 or later (the "GPL"), or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), in which case the provisions of the GPL or the LGPL are applicable instead of those above. If you wish to allow use of your version of this file only under the terms of either the GPL or the LGPL, and not to allow others to use your version of this file under the terms of the EPL, indicate your decision by deleting the provisions above and replace them with the notice and other provisions required by the GPL or the LGPL. If you do not delete the provisions above, a recipient may use your version of this file under the terms of any one of the EPL, the GPL or the LGPL. **** END LICENSE BLOCK *****
 package org.jruby.embed.internal;
 import org.jruby.Ruby;
Implementation of org.jruby.javasupport.JavaEmbedUtils.EvalUnit for embeddiing. This class is created when a Ruby script has been parsed. Once parsed, the script is ready to run many times without parsing.

Users do not instantiate explicitly. Instead, they can get the instance by parsing Ruby script by parse method of org.jruby.embed.ScriptingContainer.

Yoko Harada <>
 public class EmbedEvalUnitImpl implements EmbedEvalUnit {
     private ScriptingContainer container;
     private Node node;
     private ManyVarsDynamicScope scope;
     private Script script;
     public EmbedEvalUnitImpl(ScriptingContainer containerNode nodeManyVarsDynamicScope scope) {
     public EmbedEvalUnitImpl(ScriptingContainer containerNode nodeManyVarsDynamicScope scopeScript script) {
         this. = container;
         this. = node;
         this. = scope;
         this. = script;

Returns a root node of parsed Ruby script.

a root node of parsed Ruby script
     public Node getNode() {
         return ;

Returns a ManyVarsDynamicScope used to parse a script. A returned value is used to inject Ruby's local variable when script is evaluated.

a scope to refer local variables
     public ManyVarsDynamicScope getScope() {
         return ;

Evaluates a Ruby script, which has been parsed before.

results of executing this evaluation unit
     public IRubyObject run() {
         if ( == null &&  == null) {
             return null;
        Ruby runtime = .getProvider().getRuntime();
        BiVariableMap vars = .getVarMap();
        boolean sharing_variables = true;
        if (obj != null && obj instanceof Boolean && ((Booleanobj) == false) {
            sharing_variables = false;
        // Keep reference to current context to prevent it being collected.
        ThreadContext threadContext = runtime.getCurrentContext();
        try {
            if (sharing_variables) {
                vars.inject(, 0, null);
            IRubyObject ret;
            CompileMode mode = runtime.getInstanceConfig().getCompileMode();
            if (mode == .) {
                ret = runtime.runScriptBody();
            } else {
                ret = runtime.runInterpreter();
            if (sharing_variables) {
            return ret;
        } catch (RaiseException e) {
            // handle exits as simple script termination
            if (e.getException() instanceof RubySystemExit) {
                return ((RubySystemExit)e.getException()).status();
            throw new EvalFailedException(e.getMessage(), e);
        } catch (StackOverflowError soe) {
            throw runtime.newSystemStackError("stack level too deep"soe);
        } catch (Throwable e) {
            throw new EvalFailedException(e);
        } finally {
            if (sharing_variables) {
            /* Below lines doesn't work. Neither does classCache.flush(). How to clear cache?
            ClassCache classCache = JavaEmbedUtils.createClassCache(getRuntime().getClassLoader());
New to GrepCode? Check out our FAQ X