OLD | NEW |
| (Empty) |
1 // Copyright (c) 2015, 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 /// V8 runner support used by dartdevrun. | |
6 | |
7 import 'dart:async'; | |
8 import 'dart:io'; | |
9 | |
10 import 'package:path/path.dart'; | |
11 import 'package:pub_semver/pub_semver.dart' show Version; | |
12 | |
13 import '../options.dart' show CompilerOptions; | |
14 import 'runtime_utils.dart' show getRuntimeFileAlias; | |
15 | |
16 _parseV8Version(String version) => | |
17 new Version.parse(version.split('.').getRange(0, 3).join('.')); | |
18 | |
19 final _MIN_SUPPORTED_V8_VERSION = _parseV8Version("4.5.103.30"); | |
20 | |
21 /// TODO(ochafik): Move to dart_library.js | |
22 const _GLOBALS = r''' | |
23 if (typeof global == 'undefined') var global = this; | |
24 if (typeof alert == 'undefined') var alert = x => console.log(`ALERT: ${x}`); | |
25 if (typeof console == 'undefined') var console = { log: print, error: print }; | |
26 '''; | |
27 | |
28 /// Runner for v8-based JS interpreter binaries. | |
29 abstract class V8Runner { | |
30 final CompilerOptions _options; | |
31 V8Runner._(this._options); | |
32 | |
33 factory V8Runner(CompilerOptions options) { | |
34 String bin = options.runnerOptions.v8Binary; | |
35 switch (basename(bin)) { | |
36 case "iojs": | |
37 case "node": | |
38 return new _NodeRunner(options).._checkVersion(); | |
39 case "d8": | |
40 return new _D8Runner(options).._checkVersion(); | |
41 default: | |
42 throw new UnsupportedError("Unknown v8-based binary: $bin"); | |
43 } | |
44 } | |
45 | |
46 List<String> get _v8VersionArgs; | |
47 List<String> _getLoadStatements(List<File> files); | |
48 | |
49 String get _v8Binary => _options.runnerOptions.v8Binary; | |
50 | |
51 Future<Process> start(List<File> files, String startStatement) => | |
52 Process.start( | |
53 _v8Binary, | |
54 [ | |
55 "--harmony", | |
56 "-e", | |
57 _GLOBALS + _getLoadStatements(files).join() + startStatement | |
58 ], | |
59 workingDirectory: _options.codegenOptions.outputDir); | |
60 | |
61 /// Throws if the v8 version of this runner is not supported, or if the runner | |
62 /// is not in the path. | |
63 void _checkVersion() { | |
64 ProcessResult result = Process.runSync(_v8Binary, _v8VersionArgs); | |
65 if (result.exitCode != 0) { | |
66 throw new StateError("Failed to run $_v8Binary: ${result.stderr}"); | |
67 } | |
68 String v8Version = result.stdout.trim(); | |
69 if (_parseV8Version(v8Version).compareTo(_MIN_SUPPORTED_V8_VERSION) < 0) { | |
70 throw new StateError( | |
71 "V8 version $v8Version in $_v8Binary does not meet the required " | |
72 "minimum $_MIN_SUPPORTED_V8_VERSION."); | |
73 } | |
74 } | |
75 } | |
76 | |
77 /// Runner for d8 (see https://developers.google.com/v8/build). | |
78 class _D8Runner extends V8Runner { | |
79 _D8Runner(options) : super._(options); | |
80 | |
81 @override | |
82 get _v8VersionArgs => ['-e', 'print(version())']; | |
83 | |
84 @override | |
85 _getLoadStatements(List<File> files) => | |
86 files.map((file) => 'load("${file.path}");'); | |
87 } | |
88 | |
89 /// Runner for iojs (see https://iojs.org/download/next-nightly/) and node. | |
90 class _NodeRunner extends V8Runner { | |
91 _NodeRunner(options) : super._(options); | |
92 | |
93 @override | |
94 get _v8VersionArgs => ['-p', 'process.versions.v8']; | |
95 | |
96 @override | |
97 _getLoadStatements(List<File> files) => files.map((file) { | |
98 String alias = getRuntimeFileAlias(_options, file); | |
99 return (alias != null ? 'var $alias = ' : '') + | |
100 'require("${file.path}");'; | |
101 }); | |
102 } | |
OLD | NEW |