Skip to content

Commit

Permalink
Use dart.library.html to distinguish dart2wasm from dart2js/ddc in co…
Browse files Browse the repository at this point in the history
…nditional imports

Users of packages that have specialized code for the VM (which supports
FFI) use conditional imports based on `dart.library.ffi`. We don't want
the VM-specific code to be used for web in dart2wasm.

As a result we're going to make `dart.library.ffi` be false in
coditional imports (as well as in
`const bool.fromEnvironment('dart.library.ffi')`.

Issue dart-lang/sdk#55948
Issue flutter/flutter#149984
  • Loading branch information
mkustermann committed Jun 10, 2024
1 parent 1f3938d commit 1eb2c0c
Show file tree
Hide file tree
Showing 5 changed files with 5 additions and 9 deletions.
2 changes: 1 addition & 1 deletion lib/web_ui/lib/src/engine/renderer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import 'dart:math' as math;
import 'dart:typed_data';

import 'package:ui/src/engine.dart';
import 'package:ui/src/engine/skwasm/skwasm_stub.dart' if (dart.library.ffi) 'package:ui/src/engine/skwasm/skwasm_impl.dart';
import 'package:ui/src/engine/skwasm/skwasm_impl.dart' if (dart.library.html) 'package:ui/src/engine/skwasm/skwasm_stub.dart';
import 'package:ui/ui.dart' as ui;
import 'package:ui/ui_web/src/ui_web.dart' as ui_web;

Expand Down
5 changes: 1 addition & 4 deletions lib/web_ui/lib/ui_web/src/ui_web/browser_detection.dart
Original file line number Diff line number Diff line change
Expand Up @@ -195,10 +195,7 @@ class BrowserDetection {
bool get isEdge => userAgent.contains('Edg/');

/// Whether we are running from a wasm module compiled with dart2wasm.
///
/// Note: Currently the ffi library is available from dart2wasm but not dart2js
/// or dartdevc.
bool get isWasm => const bool.fromEnvironment('dart.library.ffi');
bool get isWasm => const bool.fromEnvironment('dart.tool.dart2wasm');
}

/// A short-hand accessor to the [BrowserDetection.instance] singleton.
Expand Down
3 changes: 1 addition & 2 deletions lib/web_ui/test/ui/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
import 'dart:async';

import 'package:ui/src/engine.dart';
import 'package:ui/src/engine/skwasm/skwasm_stub.dart'
if (dart.library.ffi) 'package:ui/src/engine/skwasm/skwasm_impl.dart';
import 'package:ui/src/engine/skwasm/skwasm_impl.dart' if (dart.library.html) 'package:ui/src/engine/skwasm/skwasm_stub.dart';
import 'package:ui/ui.dart';

import '../common/rendering.dart';
Expand Down
2 changes: 1 addition & 1 deletion web_sdk/sdk_rewriter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ const Set<String> rootLibraryNames = <String>{
};

final Map<Pattern, String> extraImportsMap = <Pattern, String>{
RegExp('skwasm_(stub|impl)'): "import 'dart:_skwasm_stub' if (dart.library.ffi) 'dart:_skwasm_impl';",
RegExp('skwasm_(stub|impl)'): "import 'dart:_skwasm_impl' if (dart.library.html) 'dart:_skwasm_stub';",
'ui_web': "import 'dart:ui_web' as ui_web;",
'engine': "import 'dart:_engine';",
'web_unicode': "import 'dart:_web_unicode';",
Expand Down
2 changes: 1 addition & 1 deletion web_sdk/test/sdk_rewriter_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ void printSomething() {
test('gets correct extra imports', () {
// Root libraries.
expect(getExtraImportsForLibrary('engine'), <String>[
"import 'dart:_skwasm_stub' if (dart.library.ffi) 'dart:_skwasm_impl';",
"import 'dart:_skwasm_impl' if (dart.library.html) 'dart:_skwasm_stub';",
"import 'dart:ui_web' as ui_web;",
"import 'dart:_web_unicode';",
"import 'dart:_web_test_fonts';",
Expand Down

0 comments on commit 1eb2c0c

Please sign in to comment.