1 29 package org.jruby.util; 30 31 import java.io.BufferedInputStream ; 32 import java.io.IOException ; 33 import java.io.InputStream ; 34 35 import org.ablaf.ast.IAstDecoder; 36 import org.jruby.Ruby; 37 import org.jruby.ast.Node; 38 import org.jruby.ast.util.RubyAstMarshal; 39 import org.jruby.runtime.load.Library; 40 import org.jruby.runtime.load.LoadServiceResource; 41 42 45 public class PreparsedScript implements Library { 46 private LoadServiceResource resource; 47 48 public PreparsedScript(LoadServiceResource resource) { 49 this.resource = resource; 50 } 51 52 public void load(Ruby runtime) throws IOException { 53 runtime.loadNode("preparsed", getNode(runtime), false); 54 } 55 56 private Node getNode(Ruby runtime) throws IOException { 57 InputStream in; 58 try { 59 in = new BufferedInputStream (resource.getURL().openStream()); 60 } catch (IOException e) { 61 throw runtime.newIOError("Resource not found: " + resource.getName()); 62 } 63 IAstDecoder decoder = RubyAstMarshal.getInstance().openDecoder(in); 64 try { 65 return decoder.readNode(); 66 } finally { 67 decoder.close(); 68 } 69 } 70 } 71 | Popular Tags |