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 http://www.eclipse.org/legal/epl-v10.html 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 <yokolet@gmail.com> 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 java.io.File;
 import java.io.Reader;
 import java.io.Writer;
 import org.jruby.Ruby;

Author(s):
Yoko Harada <yokolet@gmail.com>
 
     private RubyRuntimeAdapter adapter = JavaEmbedUtils.newRuntimeAdapter();
     private ScriptingContainer container;
 
     public EmbedRubyRuntimeAdapterImpl(ScriptingContainer container) {
         this. = container;
     }
 
     public EmbedEvalUnit parse(String scriptint... lines) {
         if (script == null) {
             return null;
         }
         boolean unicode_escape = false;
         if (obj != null && obj instanceof Boolean) {
             unicode_escape = (Boolean)obj;
         }
         if (unicode_escape) {
             InputStream istream = new ReaderInputStream(new StringReader(script));
             return runParser(istreamnulllines);
         } else {
             return runParser(scriptnulllines);
         }
     }
 
     public EmbedEvalUnit parse(Reader readerString filenameint... lines) {
         if (reader != null) {
             InputStream istream = new ReaderInputStream(reader);
            return runParser(istreamfilenamelines);
        } else {
            return null;
        }
    }
    public EmbedEvalUnit parse(PathType typeString filenameint... lines) {
        if (filename == null) {
            return null;
        }
        if (type == null) {
            type = .;
        }
        InputStream istream = null;
        try {
            switch (type) {
                case :
                    istream = new FileInputStream(filename);
                    break;
                case :
                    String basedir = (String.getAttribute(.);
                    if (basedir == null) {
                        basedir = SystemPropertyCatcher.getBaseDir();
                    }
                    String absolutePath = basedir + . + filename;
                    istream = new FileInputStream(absolutePath);
                    break;
                case :
                    istream = .getProvider().getRuntime().getJRubyClassLoader().getResourceAsStream(filename);
                    break;
            }
            return parse(istreamfilenamelines);
        } catch (FileNotFoundException e) {
            throw new ParseFailedException(e);
        } finally {
            if (istream != null) {
                try {istream.close();} catch (IOException ioe) {}
            }
        }
    }
    public EmbedEvalUnit parse(InputStream istreamString filenameint... lines) {
        if (istream != null) {
            return runParser(istreamfilenamelines);
        } else {
            return null;
        }
    }
    private EmbedEvalUnit runParser(Object inputString filenameint... lines) {
        if (input == null) {
            return null;
        }
        if (filename == null || filename.length() == 0) {
            filename = .getScriptFilename();
        }
        Ruby runtime = .getProvider().getRuntime();
        IAccessor d = new ValueAccessor(RubyString.newString(runtimefilename));
        runtime.getGlobalVariables().define("$PROGRAM_NAME"d..);
        runtime.getGlobalVariables().define("$0"d..);
        int line = 0;
        if (lines != null && lines.length > 0) {
            line = lines[0];
        }
        try {
            ManyVarsDynamicScope scope  = null;
            boolean sharing_variables = true;
            if (obj != null && obj instanceof Boolean && ((Booleanobj) == false) {
                sharing_variables = false;
            }
            if (sharing_variables) {
                scope = getManyVarsDynamicScope(, 0);
            }
            Node node = null;
            if (input instanceof String) {
                node = runtime.parseEval((String)inputfilenamescopeline);
            } else {
                node = runtime.parseFile((InputStream)inputfilenamescopeline);
            }
            CompileMode compileMode = runtime.getInstanceConfig().getCompileMode();
            if (compileMode == .) {
                // we pass in an ASTInspector that will force heap variables, so
                // it uses our shared scope
                ASTInspector inspector = new ASTInspector();
                inspector.setFlag(.);
                Script script = runtime.tryCompile(nodeinspector);
                if (script != null) {
                    return new EmbedEvalUnitImpl(nodescopescript);
                } else {
                    return new EmbedEvalUnitImpl(nodescope);
                }
            }
            return new EmbedEvalUnitImpl(nodescope);
        } catch (RaiseException e) {
            runtime.printError(e.getException());
            throw new ParseFailedException(e.getMessage(), e);
        } catch (Throwable e) {
            throw new ParseFailedException(e);
        } finally {
            try {
                if (input instanceof InputStream) {
                    ((InputStream)input).close();
                }
            } catch (IOException ex) {
                throw new ParseFailedException(ex);
            }
        }
    }
    static ManyVarsDynamicScope getManyVarsDynamicScope(ScriptingContainer containerint depth) {
        ManyVarsDynamicScope scope;
        StaticScopeFactory scopeFactory = container.getProvider().getRuntime().getStaticScopeFactory();
        // root our parsing scope with a dummy scope
        StaticScope topStaticScope = scopeFactory.newLocalScope(null);
        topStaticScope.setModule(container.getProvider().getRuntime().getObject());
        DynamicScope currentScope = new ManyVarsDynamicScope(topStaticScopenull);
        String[] names4Injection = container.getVarMap().getLocalVarNames();
        StaticScope evalScope = names4Injection == null || names4Injection.length == 0 ?
                scopeFactory.newEvalScope(currentScope.getStaticScope()) :
                scopeFactory.newEvalScope(currentScope.getStaticScope(), names4Injection);
        scope = new ManyVarsDynamicScope(evalScopecurrentScope);
        // JRUBY-5501: ensure we've set up a cref for the scope too
        scope.getStaticScope().determineModule();
        
        return scope;
    }
    public IRubyObject eval(Ruby runtimeString script) {
        return .eval(runtimescript);
    }
    public EvalUnit parse(Ruby runtimeString scriptString filenameint lineNumber) {
        return .parse(runtimescriptfilenamelineNumber);
    }
    public EvalUnit parse(Ruby runtimeInputStream istreamString filenameint lineNumber) {
        return .parse(runtimeistreamfilenamelineNumber);
    }
New to GrepCode? Check out our FAQ X