Skip to content

Commit e775fa0

Browse files
committed
Add support for 'dart.library.X' environment variables in dart2js.
[email protected], [email protected] Committed: 369899c Reverted: b80994d Review URL: https://codereview.chromium.org/1404183002 .
1 parent f70bef4 commit e775fa0

File tree

5 files changed

+277
-1
lines changed

5 files changed

+277
-1
lines changed

pkg/compiler/lib/src/apiimpl.dart

+28-1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,14 @@ import 'script.dart';
3333
const bool forceIncrementalSupport =
3434
const bool.fromEnvironment('DART2JS_EXPERIMENTAL_INCREMENTAL_SUPPORT');
3535

36+
/// For every 'dart:' library, a corresponding environment variable is set
37+
/// to "true". The environment variable's name is the concatenation of
38+
/// this prefix and the name (without the 'dart:'.
39+
///
40+
/// For example 'dart:html' has the environment variable 'dart.library.html' set
41+
/// to "true".
42+
const String dartLibraryEnvironmentPrefix = 'dart.library.';
43+
3644
/// Locations of the platform descriptor files relative to the library root.
3745
const String _clientPlatform = "lib/dart_client.platform";
3846
const String _serverPlatform = "lib/dart_server.platform";
@@ -580,7 +588,26 @@ class CompilerImpl extends Compiler {
580588
}
581589
}
582590

583-
fromEnvironment(String name) => environment[name];
591+
fromEnvironment(String name) {
592+
assert(invariant(NO_LOCATION_SPANNABLE,
593+
sdkLibraries != null, message: "setupSdk() has not been run"));
594+
595+
var result = environment[name];
596+
if (result != null || environment.containsKey(name)) return result;
597+
if (!name.startsWith(dartLibraryEnvironmentPrefix)) return null;
598+
599+
String libraryName = name.substring(dartLibraryEnvironmentPrefix.length);
600+
if (sdkLibraries.containsKey(libraryName)) {
601+
// Dart2js always "supports" importing 'dart:mirrors' but will abort
602+
// the compilation at a later point if the backend doesn't support
603+
// mirrors. In this case 'mirrors' should not be in the environment.
604+
if (name == dartLibraryEnvironmentPrefix + 'mirrors') {
605+
return backend.supportsReflection ? "true" : null;
606+
}
607+
return "true";
608+
}
609+
return null;
610+
}
584611

585612
Uri lookupLibraryUri(String libraryName) {
586613
assert(invariant(NO_LOCATION_SPANNABLE,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
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+
/// Check that 'dart:' libraries have their corresponding dart.library.X
6+
/// environment variable set.
7+
8+
import "dart:io";
9+
10+
import "dart:async";
11+
12+
import "memory_source_file_helper.dart";
13+
14+
import "package:async_helper/async_helper.dart";
15+
16+
import 'package:expect/expect.dart' show
17+
Expect;
18+
19+
import 'package:compiler/src/elements/elements.dart' show
20+
LibraryElement;
21+
22+
import 'package:compiler/src/null_compiler_output.dart' show
23+
NullCompilerOutput;
24+
25+
import 'package:compiler/compiler_new.dart' show
26+
CompilerInput,
27+
CompilerDiagnostics;
28+
29+
import 'package:sdk_library_metadata/libraries.dart' show
30+
LibraryInfo;
31+
32+
const clientPlatform = r'''
33+
[dart-spec]
34+
spec: 3rd edition.
35+
36+
[features]
37+
# No extra features
38+
39+
[libraries]
40+
mock.client: mock1.dart
41+
mock.shared: mock3.dart
42+
collection: collection/collection.dart
43+
html: html/dart2js/html_dart2js.dart
44+
''';
45+
46+
const serverPlatform = r'''
47+
[dart-spec]
48+
spec: 3rd edition.
49+
50+
[features]
51+
# No extra features
52+
53+
[libraries]
54+
mock.server: mock2.dart
55+
mock.shared: mock3.dart
56+
collection: collection/collection.dart
57+
io: io/io.dart
58+
''';
59+
60+
class DummyCompilerInput implements CompilerInput {
61+
const DummyCompilerInput();
62+
63+
readFromUri(uri) async {
64+
if (uri.toString().endsWith("dart_client.platform")) {
65+
return clientPlatform;
66+
} else if (uri.toString().endsWith("dart_server.platform")) {
67+
return serverPlatform;
68+
} else {
69+
throw "should not be needed $uri";
70+
}
71+
}
72+
}
73+
74+
class DummyCompilerDiagnostics implements CompilerDiagnostics {
75+
const DummyCompilerDiagnostics();
76+
77+
report(code, uri, begin, end, text, kind) {
78+
throw "should not be needed";
79+
}
80+
}
81+
82+
class CustomCompiler extends CompilerImpl {
83+
CustomCompiler(
84+
options,
85+
environment)
86+
: super(
87+
const DummyCompilerInput(),
88+
const NullCompilerOutput(),
89+
const DummyCompilerDiagnostics(),
90+
Uri.base.resolve("sdk/"),
91+
null,
92+
options,
93+
environment);
94+
}
95+
96+
runTest() async {
97+
var compiler = new CustomCompiler(
98+
[],
99+
{});
100+
101+
await compiler.setupSdk();
102+
103+
// Core libraries are always present.
104+
Expect.equals("true", compiler.fromEnvironment("dart.library.collection"));
105+
// Non-existing entries in the environment return 'null'.
106+
Expect.isNull(compiler.fromEnvironment("not in env"));
107+
// Check for client libraries (default if there are no flags to the compiler).
108+
Expect.equals("true", compiler.fromEnvironment("dart.library.mock.client"));
109+
Expect.equals("true", compiler.fromEnvironment("dart.library.html"));
110+
// Check for shared libraries..
111+
Expect.equals("true", compiler.fromEnvironment("dart.library.mock.shared"));
112+
// Check server libraries are not present.
113+
Expect.equals(null, compiler.fromEnvironment("dart.library.mock.server"));
114+
Expect.equals(null, compiler.fromEnvironment("dart.library.io"));
115+
116+
compiler = new CustomCompiler(
117+
['--categories=Server'],
118+
{});
119+
120+
await compiler.setupSdk();
121+
122+
// Core libraries are always present.
123+
Expect.equals("true", compiler.fromEnvironment("dart.library.collection"));
124+
// Non-existing entries in the environment return 'null'.
125+
Expect.isNull(compiler.fromEnvironment("not in env"));
126+
// Check client libraries are not present.
127+
Expect.equals(null, compiler.fromEnvironment("dart.library.mock.client"));
128+
Expect.equals(null, compiler.fromEnvironment("dart.library.html"));
129+
// Check for shared libraries..
130+
Expect.equals("true", compiler.fromEnvironment("dart.library.mock.shared"));
131+
// Check for server libraries.
132+
Expect.equals("true", compiler.fromEnvironment("dart.library.mock.server"));
133+
Expect.equals("true", compiler.fromEnvironment("dart.library.io"));
134+
135+
// Check that user-defined env-variables win.
136+
compiler = new CustomCompiler(
137+
[],
138+
{'dart.library.collection': "false",
139+
'dart.library.mock.client': "foo"});
140+
141+
await compiler.setupSdk();
142+
143+
Expect.equals("false", compiler.fromEnvironment("dart.library.collection"));
144+
Expect.equals("foo", compiler.fromEnvironment("dart.library.mock.client"));
145+
}
146+
147+
main() {
148+
asyncStart();
149+
runTest().then((_) {
150+
asyncEnd();
151+
});
152+
}

tests/language/language.status

+3
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,10 @@ async_star_regression_2238_test: CompileTimeError, RuntimeError # drt only runti
3838
async_star_cancel_while_paused_test: RuntimeError
3939
async_star_await_pauses_test: Skip # Times out. Issue 23996
4040

41+
library_env_test: RuntimeError
42+
4143
[ ($compiler == none || $compiler == precompiler) && ($runtime == vm || $runtime == dart_precompiled) ]
44+
4245
class_keyword_test/02: MissingCompileTimeError # Issue 13627
4346
unicode_bom_test: Fail # Issue 16067
4447
vm/debug_break_enabled_vm_test/01: Crash, OK # Expected to hit breakpoint.

tests/language/language_dart2js.status

+7
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,13 @@ try_catch_on_syntax_test/11: Fail # Issue 19823
1515

1616
call_function_apply_test: RuntimeError # Issue 23873
1717

18+
# The following tests are supposed to fail.
19+
# When run for testing dart2js supports all dart:X libraries (because it
20+
# uses '--categories=all').
21+
library_env_test/has_no_html_support: RuntimeError, OK
22+
library_env_test/has_no_io_support: RuntimeError, OK
23+
library_env_test/has_no_mirror_support: RuntimeError, OK
24+
1825
[ $compiler == dart2js && $runtime == jsshell ]
1926
await_for_test: Skip # Jsshell does not provide periodic timers, Issue 7728
2027
async_star_test: RuntimeError # Jsshell does not provide non-zero timers, Issue 7728

tests/language/library_env_test.dart

+87
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
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+
import 'package:expect/expect.dart';
6+
7+
main() {
8+
const NOT_PRESENT = null;
9+
10+
Expect.isTrue(const bool.fromEnvironment("dart.library.async"));
11+
Expect.isTrue(const bool.fromEnvironment("dart.library.collection"));
12+
Expect.isTrue(const bool.fromEnvironment("dart.library.convert"));
13+
Expect.isTrue(const bool.fromEnvironment("dart.library.core"));
14+
Expect.isTrue(const bool.fromEnvironment("dart.library.typed_data"));
15+
16+
17+
bool hasHtmlSupport;
18+
hasHtmlSupport = true; /// has_html_support: ok
19+
hasHtmlSupport = false; /// has_no_html_support: ok
20+
21+
if (hasHtmlSupport != null) {
22+
bool expectedResult = hasHtmlSupport ? true : NOT_PRESENT;
23+
24+
Expect.equals(expectedResult,
25+
const bool.fromEnvironment("dart.library.html",
26+
defaultValue: NOT_PRESENT));
27+
Expect.equals(expectedResult,
28+
const bool.fromEnvironment("dart.library.indexed_db",
29+
defaultValue: NOT_PRESENT));
30+
Expect.equals(expectedResult,
31+
const bool.fromEnvironment("dart.library.svg",
32+
defaultValue: NOT_PRESENT));
33+
Expect.equals(expectedResult,
34+
const bool.fromEnvironment("dart.library.web_audio",
35+
defaultValue: NOT_PRESENT));
36+
Expect.equals(expectedResult,
37+
const bool.fromEnvironment("dart.library.web_gl",
38+
defaultValue: NOT_PRESENT));
39+
Expect.equals(expectedResult,
40+
const bool.fromEnvironment("dart.library.web_sql",
41+
defaultValue: NOT_PRESENT));
42+
}
43+
44+
bool hasIoSupport;
45+
hasIoSupport = true; /// has_io_support: ok
46+
hasIoSupport = false; /// has_no_io_support: ok
47+
48+
if (hasIoSupport != null) {
49+
bool expectedResult = hasIoSupport ? true : NOT_PRESENT;
50+
51+
Expect.equals(expectedResult,
52+
const bool.fromEnvironment("dart.library.io",
53+
defaultValue: NOT_PRESENT));
54+
Expect.equals(expectedResult,
55+
const bool.fromEnvironment("dart.library.developer",
56+
defaultValue: NOT_PRESENT));
57+
}
58+
59+
bool hasMirrorSupport;
60+
hasMirrorSupport = true; /// has_mirror_support: ok
61+
hasMirrorSupport = false; /// has_no_mirror_support: ok
62+
63+
if (hasMirrorSupport != null) {
64+
bool expectedResult = hasMirrorSupport ? true : NOT_PRESENT;
65+
66+
Expect.equals(expectedResult,
67+
const bool.fromEnvironment("dart.library.mirrors",
68+
defaultValue: NOT_PRESENT));
69+
}
70+
71+
Expect.equals(NOT_PRESENT,
72+
const bool.fromEnvironment("dart.library.XYZ",
73+
defaultValue: NOT_PRESENT));
74+
Expect.equals(NOT_PRESENT,
75+
const bool.fromEnvironment("dart.library.Collection",
76+
defaultValue: NOT_PRESENT));
77+
Expect.equals(NOT_PRESENT,
78+
const bool.fromEnvironment("dart.library.converT",
79+
defaultValue: NOT_PRESENT));
80+
Expect.equals(NOT_PRESENT,
81+
const bool.fromEnvironment("dart.library.",
82+
defaultValue: NOT_PRESENT));
83+
Expect.equals(NOT_PRESENT,
84+
const bool.fromEnvironment("dart.library.core ",
85+
defaultValue: NOT_PRESENT));
86+
87+
}

0 commit comments

Comments
 (0)