Mercurial > hg > graal-jvmci-8
comparison graal/com.oracle.truffle.ruby.nodes/src/com/oracle/truffle/ruby/nodes/core/SystemNode.java @ 13514:0fbee3eb71f0
Ruby: import project.
author | Chris Seaton <chris.seaton@oracle.com> |
---|---|
date | Mon, 06 Jan 2014 17:12:09 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
13513:64a23ce736a0 | 13514:0fbee3eb71f0 |
---|---|
1 /* | |
2 * Copyright (c) 2013 Oracle and/or its affiliates. All rights reserved. This | |
3 * code is released under a tri EPL/GPL/LGPL license. You can use it, | |
4 * redistribute it and/or modify it under the terms of the: | |
5 * | |
6 * Eclipse Public License version 1.0 | |
7 * GNU General Public License version 2 | |
8 * GNU Lesser General Public License version 2.1 | |
9 */ | |
10 package com.oracle.truffle.ruby.nodes.core; | |
11 | |
12 import java.io.*; | |
13 | |
14 import com.oracle.truffle.api.*; | |
15 import com.oracle.truffle.api.frame.*; | |
16 import com.oracle.truffle.api.nodes.*; | |
17 import com.oracle.truffle.ruby.nodes.*; | |
18 import com.oracle.truffle.ruby.runtime.*; | |
19 | |
20 /** | |
21 * Represents an expression that is evaluated by running it as a system command via forking and | |
22 * execing, and then taking stdout as a string. | |
23 */ | |
24 @NodeInfo(shortName = "system") | |
25 public class SystemNode extends RubyNode { | |
26 | |
27 @Child protected RubyNode child; | |
28 | |
29 public SystemNode(RubyContext context, SourceSection sourceSection, RubyNode child) { | |
30 super(context, sourceSection); | |
31 this.child = adoptChild(child); | |
32 } | |
33 | |
34 @Override | |
35 public Object execute(VirtualFrame frame) { | |
36 final RubyContext context = getContext(); | |
37 | |
38 final String command = child.execute(frame).toString(); | |
39 | |
40 Process process; | |
41 | |
42 try { | |
43 // We need to run via bash to get the variable and other expansion we expect | |
44 process = Runtime.getRuntime().exec(new String[]{"bash", "-c", command}); | |
45 } catch (IOException e) { | |
46 throw new RuntimeException(e); | |
47 } | |
48 | |
49 final InputStream stdout = process.getInputStream(); | |
50 final BufferedReader reader = new BufferedReader(new InputStreamReader(stdout)); | |
51 | |
52 final StringBuilder resultBuilder = new StringBuilder(); | |
53 | |
54 String line; | |
55 | |
56 // TODO(cs): this isn't great for binary output | |
57 | |
58 try { | |
59 while ((line = reader.readLine()) != null) { | |
60 resultBuilder.append(line); | |
61 resultBuilder.append("\n"); | |
62 } | |
63 } catch (IOException e) { | |
64 throw new RuntimeException(e); | |
65 } | |
66 | |
67 return context.makeString(resultBuilder.toString()); | |
68 } | |
69 } |