Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(458)

Side by Side Diff: compiler/java/com/google/dart/runner/V8Launcher.java

Issue 9466041: Removes dependency on v8 from dartc testing (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Updated date, copyright notices, build.xml Created 8 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file.
4
5 package com.google.dart.runner;
6
7 import com.google.common.collect.Lists;
8
9 import java.io.BufferedReader;
10 import java.io.File;
11 import java.io.FileWriter;
12 import java.io.IOException;
13 import java.io.InputStream;
14 import java.io.InputStreamReader;
15 import java.io.PrintStream;
16 import java.io.PrintWriter;
17 import java.io.StringWriter;
18 import java.util.ArrayList;
19 import java.util.Arrays;
20 import java.util.List;
21
22 /**
23 * Runs a given JS-script in d8 (part of V8).
24 */
25 public class V8Launcher implements JavaScriptLauncher {
26 private static class Drainer implements Runnable {
27 private final InputStream stream;
28 private final List<String> lines;
29 private final PrintStream out;
30
31 public Drainer(InputStream stream, PrintStream out, List<String> lines) {
32 this.stream = stream;
33 this.lines = lines;
34 this.out = out;
35 }
36
37 @Override
38 public void run() {
39 BufferedReader r = new BufferedReader(new InputStreamReader(stream));
40 String str;
41 try {
42 while ((str = r.readLine()) != null) {
43 if (lines != null) {
44 lines.add(str);
45 }
46 out.println(str);
47 }
48 } catch (IOException e) {
49 throw new AssertionError(e);
50 }
51 }
52 }
53
54 private static final String D8_ENVIRONMENT_VARIABLE = "D8_EXEC";
55
56 private static final String EOL = System.getProperty("line.separator");
57
58 public V8Launcher() {
59 }
60
61 @Override
62 public void execute(String jsScript, String sourceName, String[] args, RunnerO ptions options,
63 PrintStream stdout, PrintStream stderr)
64 throws RunnerError {
65 if (!isConfigured()) {
66 throw new RunnerError("Please set the " + D8_ENVIRONMENT_VARIABLE + " envi ronment variable.");
67 }
68 File sourceFile;
69 try {
70 sourceFile = writeTempFile(sourceName, jsScript);
71 } catch (IOException e) {
72 throw new RunnerError(e);
73 }
74 try {
75 ArrayList<String> command = new ArrayList<String>();
76 command.add(v8Executable().getAbsolutePath());
77 command.add(sourceFile.getAbsolutePath());
78 if (options.shouldProfile()) {
79 command.add("--prof");
80 }
81 command.add("--");
82 command.addAll(Arrays.asList(args));
83 int exitValue;
84 Process p = null;
85 List<String> stdOutLines = Lists.newArrayList();
86 Thread stdOutDrain = null;
87 Thread stdErrDrain = null;
88 try {
89 p = Runtime.getRuntime().exec(command.toArray(new String[0]));
90 // TODO(floitsch): we should handle timeouts (but how long should we wai t?).
91 stdOutDrain = new Thread(new Drainer(p.getInputStream(), stdout, stdOutL ines));
92 stdErrDrain = new Thread(new Drainer(p.getErrorStream(), stderr, null));
93 stdOutDrain.start();
94 stdErrDrain.start();
95 try {
96 exitValue = p.waitFor();
97 } catch (InterruptedException e) {
98 throw new RuntimeException(e);
99 }
100 } catch (IOException e) {
101 throw new RunnerError(e);
102 } finally {
103 try {
104 if (stdOutDrain != null) {
105 stdOutDrain.join();
106 }
107 if (stdErrDrain != null) {
108 stdErrDrain.join();
109 }
110 } catch (InterruptedException e) {
111 throw new RuntimeException(e);
112 }
113 try {
114 p.getInputStream().close();
115 p.getOutputStream().close();
116 p.getOutputStream().close();
117 } catch (IOException e) {
118 throw new RunnerError(e);
119 }
120 p.destroy();
121 }
122 if (exitValue != 0) {
123 StringWriter stringWriter = new StringWriter();
124 PrintWriter out = new PrintWriter(stringWriter);
125 if (options.verbose()) {
126 out.println(jsScript);
127 }
128 out.println("Execution failed.");
129
130 out.println("V8 execution returned non-zero exit-code: " + p.exitValue() );
131 out.flush();
132 throw new RunnerError(stringWriter.toString());
133 }
134 } finally {
135 sourceFile.delete();
136 }
137 }
138
139 private File writeTempFile(String name, String content) throws IOException {
140 // The first argument to createTempFile must be at least three characters lo ng, and be a
141 // valid file-name.
142 name = name.replace('/', '_');
143 File file = File.createTempFile("dart_" + name, ".js");
144 FileWriter writer = new FileWriter(file);
145 try {
146 writer.write(content);
147 } finally {
148 writer.close();
149 }
150 return file;
151 }
152
153 private static File v8Executable() {
154 String d8Path = System.getProperty("com.google.dart.runner.d8",
155 System.getenv(D8_ENVIRONMENT_VARIABLE));
156 if (d8Path == null) {
157 String testSrcDir = System.getenv("TEST_SRCDIR");
158 if (testSrcDir == null) {
159 return null;
160 }
161 return new File(new File(new File(new File(testSrcDir, "google3"),
162 "third_party"), "v8"), "d8");
163 } else {
164 return new File(d8Path);
165 }
166 }
167
168 /**
169 * @return true if the D8_EXEC environment variable is correctly set up.
170 */
171 public static boolean isConfigured() {
172 File file = v8Executable();
173 if (file == null) {
174 return false;
175 }
176 return file.canExecute();
177 }
178 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698