fixes #31575, DDC kernel backend can now compile the SDK to JS

Also adds this to the build script, so we can eventually replace the existing SDK compile step with this new one.

Change-Id: Iba0720df5bbab302d2fdd0b5aeeb182de152cc98
Reviewed-on: https://dart-review.googlesource.com/32504
Commit-Queue: Jenny Messerly <jmesserly@google.com>
Reviewed-by: Vijay Menon <vsm@google.com>
This commit is contained in:
Jenny Messerly 2018-01-05 11:17:20 -08:00 committed by commit-bot@chromium.org
parent e34aae20ce
commit 303d71f53f
4 changed files with 26 additions and 14 deletions

View file

@ -398,7 +398,7 @@ class ProgramCompiler
if (bootstrap) _emitLibraryProcedures(library);
library.classes.forEach(_emitClass);
_moduleItems.addAll(library.typedefs.map(_emitTypedef));
library.typedefs.forEach(_emitTypedef);
if (bootstrap) {
_moduleItems.add(_emitInternalSdkFields(library.fields));
} else {
@ -1884,7 +1884,7 @@ class ProgramCompiler
var jsMethods = <JS.Method>[];
if (field.isStatic) return jsMethods;
var name = getAnnotationName(field, isJSName) ?? field.name;
var name = getAnnotationName(field, isJSName) ?? field.name.name;
// Generate getter
var fn = new JS.Fun([], js.statement('{ return this.#; }', [name]));
var method = new JS.Method(_declareMemberName(field), fn, isGetter: true);
@ -2012,16 +2012,18 @@ class ProgramCompiler
return js.statement('# = #;', [_emitTopLevelName(c), jsTypeName]);
}
JS.Statement _emitTypedef(Typedef t) {
void _emitTypedef(Typedef t) {
var body = _callHelper(
'typedef(#, () => #)', [js.string(t.name, "'"), _emitType(t.type)]);
JS.Statement result;
if (t.typeParameters.isNotEmpty) {
return _defineClassTypeArguments(
result = _defineClassTypeArguments(
t, t.typeParameters, js.statement('const # = #;', [t.name, body]));
} else {
return js.statement('# = #;', [_emitTopLevelName(t), body]);
result = js.statement('# = #;', [_emitTopLevelName(t), body]);
}
_moduleItems.add(result);
}
/// Treat dart:_runtime fields as safe to eagerly evaluate.
@ -2057,11 +2059,7 @@ class ProgramCompiler
for (var field in fields) {
var name = field.name.name;
var access = _emitStaticMemberName(name);
accessors.add(new JS.Method(
access,
js.call('function() { return #; }',
_visitInitializer(field.initializer, field.annotations))
as JS.Fun,
accessors.add(new JS.Method(access, _emitStaticFieldInitializer(field),
isGetter: true));
// TODO(jmesserly): currently uses a dummy setter to indicate writable.
@ -2077,6 +2075,20 @@ class ProgramCompiler
return _callHelperStatement('defineLazy(#, { # });', [objExpr, accessors]);
}
JS.Fun _emitStaticFieldInitializer(Field field) {
var savedLetVariables = _letVariables;
_letVariables = [];
var body = [
new JS.Return(_visitInitializer(field.initializer, field.annotations))
];
_initTempVars(body);
_letVariables = savedLetVariables;
return new JS.Fun([], new JS.Block(body));
}
JS.PropertyAccess _emitTopLevelName(NamedNode n, {String suffix: ''}) {
return _emitJSInterop(n) ?? _emitTopLevelNameNoInterop(n, suffix: suffix);
}

View file

@ -49,7 +49,7 @@ Future main(List<String> args) async {
if (generateJS) {
var jsModule = compileToJSModule(program, [], [], {});
var jsPath = path.join(path.basename(outputPath), 'dart_sdk.kernel.js');
var jsPath = path.join(path.dirname(outputPath), 'dart_sdk.kernel.js');
new File(jsPath)
.writeAsStringSync(jsProgramToCode(jsModule, ModuleFormat.es6).code);
}

View file

@ -190,7 +190,6 @@ call_non_method_field_test/01: MissingCompileTimeError
call_non_method_field_test/02: MissingCompileTimeError
call_with_no_such_method_test: CompileTimeError # Issue 31402 Error: A value of type '#lib1::F' can't be assigned to a variable of type 'dart.core::Function'.
callable_test/none: CompileTimeError
cascade_on_static_field_test: Crash
check_member_static_test/01: MissingCompileTimeError
check_member_static_test/02: MissingCompileTimeError
class_cycle_test/02: MissingCompileTimeError
@ -681,7 +680,6 @@ type_variable_bounds_test/05: MissingCompileTimeError
type_variable_bounds_test/06: MissingCompileTimeError
type_variable_bounds_test/08: MissingCompileTimeError
type_variable_bounds_test/11: MissingCompileTimeError
typedef_class_in_other_file_test: Crash
void_block_return_test/00: MissingCompileTimeError
void_type_callbacks_test/none: CompileTimeError
wrong_number_type_arguments_test/01: MissingCompileTimeError

View file

@ -353,7 +353,7 @@ compiled_action("dartdevc_test_pkg") {
]
}
# Compiles the DDC SDK's kernel summary.
# Compiles the DDC SDK's kernel summary and JS code.
compiled_action("dartdevc_sdk_kernel_summary") {
tool = "../../runtime/bin:dart"
@ -367,10 +367,12 @@ compiled_action("dartdevc_sdk_kernel_summary") {
outputs = [
"$target_gen_dir/ddc_sdk.dill",
"$target_gen_dir/dart_sdk.kernel.js"
]
args = [
rebase_path("../../pkg/dev_compiler/tool/kernel_sdk.dart"),
"--generate-javascript",
rebase_path("$target_gen_dir/ddc_sdk.dill"),
]
}