Skip to content

Commit 564bd1b

Browse files
authored
Yj dartdevembedder 2488 (#2531)
* Reenabled tests in frontend_server_ddc_and_canary_evaluate_test.dart * added new line * updated changelog * updated changelog * skip running test if indexBaseMode == IndexBaseMode.base && Platform.isWindows * addressing comments * formatted code * addressed comment - move _dartRuntimeDebugger to be initialize as member field * updated getter for dartRuntimeDebugger * renamed file
1 parent e3863bb commit 564bd1b

12 files changed

+136
-30
lines changed

dwds/CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
to use the provided `name` in a `ModuleMetadata`. Metadata provided by DDC
1212
when using the library bundle format does not provide a useful bundle name.
1313
- Migrate to `package:web` v1.1.0.
14+
- Added support for some debugging APIs with the DDC library bundle format. - [#2488](https://github.com/dart-lang/webdev/issues/2488)
1415

1516
## 24.1.0
1617

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
// Copyright (c) 2024, 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:dwds/src/loaders/strategy.dart';
6+
7+
class DartRuntimeDebugger {
8+
final LoadStrategy _loadStrategy;
9+
final bool _useLibraryBundleExpression;
10+
11+
DartRuntimeDebugger({
12+
required LoadStrategy loadStrategy,
13+
required bool useLibraryBundleExpression,
14+
}) : _loadStrategy = loadStrategy,
15+
_useLibraryBundleExpression = useLibraryBundleExpression;
16+
17+
String _generateJsExpression(
18+
String ddcExpression,
19+
String libraryBundleExpression,
20+
) {
21+
return _useLibraryBundleExpression
22+
? libraryBundleExpression
23+
: ddcExpression;
24+
}
25+
26+
String _wrapWithSdkLoader(String args, String functionCall) {
27+
return '''
28+
function($args) {
29+
const sdk = ${_loadStrategy.loadModuleSnippet}("dart_sdk");
30+
const dart = sdk.dart;
31+
return dart.$functionCall;
32+
}
33+
''';
34+
}
35+
36+
String _wrapWithBundleLoader(String args, String functionCall) {
37+
return '''
38+
function($args) {
39+
return dartDevEmbedder.debugger.$functionCall;
40+
}
41+
''';
42+
}
43+
44+
String _buildExpression(
45+
String args,
46+
String ddcFunction,
47+
String libraryBundleFunction,
48+
) {
49+
return _generateJsExpression(
50+
_wrapWithSdkLoader(args, ddcFunction),
51+
_wrapWithBundleLoader(args, libraryBundleFunction),
52+
);
53+
}
54+
55+
String getObjectMetadataJsExpression() {
56+
return _buildExpression(
57+
'arg',
58+
'getObjectMetadata(arg)',
59+
'getObjectMetadata(arg)',
60+
);
61+
}
62+
63+
String getObjectFieldNamesJsExpression() {
64+
return _buildExpression(
65+
'',
66+
'getObjectFieldNames(this)',
67+
'getObjectFieldNames(this)',
68+
);
69+
}
70+
71+
String getFunctionMetadataJsExpression() {
72+
return _buildExpression(
73+
'',
74+
'getFunctionMetadata(this)',
75+
'getFunctionName(this)',
76+
);
77+
}
78+
79+
String getSubRangeJsExpression() {
80+
return _buildExpression(
81+
'offset, count',
82+
'getSubRange(this, offset, count)',
83+
'getSubRange(this, offset, count)',
84+
);
85+
}
86+
}

dwds/lib/src/debugging/inspector.dart

+2-7
Original file line numberDiff line numberDiff line change
@@ -643,13 +643,8 @@ class AppInspector implements AppInspectorInterface {
643643
// If this is a List, just call sublist. If it's a Map, get the entries, but
644644
// avoid doing a toList on a large map using skip/take to get the section we
645645
// want. To make those alternatives easier in JS, pass both count and end.
646-
final expression = '''
647-
function (offset, count) {
648-
const sdk = ${globalToolConfiguration.loadStrategy.loadModuleSnippet}("dart_sdk");
649-
const dart = sdk.dart;
650-
return dart.getSubRange(this, offset, count);
651-
}
652-
''';
646+
final expression = globalToolConfiguration.loadStrategy.dartRuntimeDebugger
647+
.getSubRangeJsExpression();
653648

654649
return await jsCallFunctionOn(receiver, expression, args);
655650
}

dwds/lib/src/debugging/instance.dart

+3-3
Original file line numberDiff line numberDiff line change
@@ -772,9 +772,9 @@ class InstanceHelper extends Domain {
772772
//
773773
// For maps and lists it's more complicated. Treat the actual SDK versions
774774
// of these as special.
775-
final fieldNameExpression =
776-
_jsRuntimeFunctionCall('getObjectFieldNames(this)');
777-
775+
final fieldNameExpression = globalToolConfiguration
776+
.loadStrategy.dartRuntimeDebugger
777+
.getObjectFieldNamesJsExpression();
778778
final result = await inspector.jsCallFunctionOn(
779779
remoteObject,
780780
fieldNameExpression,

dwds/lib/src/debugging/metadata/class.dart

+3-7
Original file line numberDiff line numberDiff line change
@@ -152,13 +152,9 @@ class ClassMetaDataHelper {
152152
/// Returns null if the [remoteObject] is not a Dart class.
153153
Future<ClassMetaData?> metaDataFor(RemoteObject remoteObject) async {
154154
try {
155-
final evalExpression = '''
156-
function(arg) {
157-
const sdk = ${globalToolConfiguration.loadStrategy.loadModuleSnippet}('dart_sdk');
158-
const dart = sdk.dart;
159-
return dart.getObjectMetadata(arg);
160-
}
161-
''';
155+
final evalExpression = globalToolConfiguration
156+
.loadStrategy.dartRuntimeDebugger
157+
.getObjectMetadataJsExpression();
162158

163159
final result = await _inspector.jsCallFunctionOn(
164160
remoteObject,

dwds/lib/src/debugging/metadata/function.dart

+3-7
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,9 @@ class FunctionMetaData {
1717
RemoteDebugger remoteDebugger,
1818
RemoteObject remoteObject,
1919
) async {
20-
final evalExpression = '''
21-
function() {
22-
const sdk = ${globalToolConfiguration.loadStrategy.loadModuleSnippet}('dart_sdk');
23-
const dart = sdk.dart;
24-
return dart.getFunctionMetadata(this);
25-
}
26-
''';
20+
final evalExpression = globalToolConfiguration
21+
.loadStrategy.dartRuntimeDebugger
22+
.getFunctionMetadataJsExpression();
2723

2824
final response = await remoteDebugger.sendCommand(
2925
'Runtime.callFunctionOn',

dwds/lib/src/loaders/ddc.dart

+7
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import 'dart:convert';
66

7+
import 'package:dwds/src/debugging/dart_runtime_debugger.dart';
78
import 'package:dwds/src/debugging/metadata/provider.dart';
89
import 'package:dwds/src/loaders/strategy.dart';
910
import 'package:dwds/src/readers/asset_reader.dart';
@@ -163,6 +164,12 @@ class DdcStrategy extends LoadStrategy {
163164
@override
164165
String get loadModuleSnippet => 'dart_library.import';
165166

167+
@override
168+
late final DartRuntimeDebugger dartRuntimeDebugger = DartRuntimeDebugger(
169+
loadStrategy: this,
170+
useLibraryBundleExpression: false,
171+
);
172+
166173
@override
167174
BuildSettings get buildSettings => _buildSettings;
168175

dwds/lib/src/loaders/ddc_library_bundle.dart

+7
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import 'dart:convert';
66

7+
import 'package:dwds/src/debugging/dart_runtime_debugger.dart';
78
import 'package:dwds/src/debugging/metadata/provider.dart';
89
import 'package:dwds/src/loaders/ddc.dart';
910
import 'package:dwds/src/loaders/strategy.dart';
@@ -140,6 +141,12 @@ class DdcLibraryBundleStrategy extends LoadStrategy {
140141
"function() { throw new Error('LoadStrategy.loadModuleSnippet is used. "
141142
"This is currently unsupported in the DDC library bundle format.'); }";
142143

144+
@override
145+
late final DartRuntimeDebugger dartRuntimeDebugger = DartRuntimeDebugger(
146+
loadStrategy: this,
147+
useLibraryBundleExpression: true,
148+
);
149+
143150
@override
144151
BuildSettings get buildSettings => _buildSettings;
145152

dwds/lib/src/loaders/require.dart

+8-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import 'dart:convert';
66

7+
import 'package:dwds/src/debugging/dart_runtime_debugger.dart';
78
import 'package:dwds/src/debugging/metadata/provider.dart';
89
import 'package:dwds/src/loaders/strategy.dart';
910
import 'package:dwds/src/readers/asset_reader.dart';
@@ -164,6 +165,12 @@ class RequireStrategy extends LoadStrategy {
164165
@override
165166
String get loadModuleSnippet => 'require';
166167

168+
@override
169+
late final DartRuntimeDebugger dartRuntimeDebugger = DartRuntimeDebugger(
170+
loadStrategy: this,
171+
useLibraryBundleExpression: false,
172+
);
173+
167174
/// Require JS config for ddc.
168175
///
169176
/// Sets the base url to `/` so that all modules can be loaded using absolute
@@ -180,7 +187,7 @@ $_baseUrlScript;
180187
require.config({
181188
baseUrl: baseUrl,
182189
waitSeconds: 0,
183-
paths: modulePaths
190+
paths: modulePaths
184191
});
185192
const modulesGraph = new Map();
186193
requirejs.onResourceLoad = function (context, map, depArray) {

dwds/lib/src/loaders/strategy.dart

+4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import 'dart:typed_data';
66

7+
import 'package:dwds/src/debugging/dart_runtime_debugger.dart';
78
import 'package:dwds/src/debugging/metadata/provider.dart';
89
import 'package:dwds/src/readers/asset_reader.dart';
910
import 'package:dwds/src/services/expression_compiler.dart';
@@ -43,6 +44,9 @@ abstract class LoadStrategy {
4344
/// argument which is the module name to load.
4445
String get loadModuleSnippet;
4546

47+
/// Provides a runtime debugger for the Dart runtime.
48+
DartRuntimeDebugger get dartRuntimeDebugger;
49+
4650
/// The relative root path for library paths. The current directory will be
4751
/// used if this is not overridden.
4852
String? get libraryRoot => null;

dwds/test/fixtures/fakes.dart

+7
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import 'dart:async';
66

77
import 'package:dwds/asset_reader.dart';
8+
import 'package:dwds/src/debugging/dart_runtime_debugger.dart';
89
import 'package:dwds/src/debugging/execution_context.dart';
910
import 'package:dwds/src/debugging/inspector.dart';
1011
import 'package:dwds/src/debugging/instance.dart';
@@ -362,6 +363,12 @@ class FakeStrategy extends LoadStrategy {
362363
@override
363364
String get loadModuleSnippet => '';
364365

366+
@override
367+
late final DartRuntimeDebugger dartRuntimeDebugger = DartRuntimeDebugger(
368+
loadStrategy: this,
369+
useLibraryBundleExpression: false,
370+
);
371+
365372
@override
366373
ReloadConfiguration get reloadConfiguration => ReloadConfiguration.none;
367374

dwds/test/frontend_server_ddc_and_canary_evaluate_test.dart dwds/test/frontend_server_ddc_library_bundle_evaluate_test.dart

+5-5
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
@Timeout(Duration(minutes: 5))
88
library;
99

10+
import 'dart:io';
11+
1012
import 'package:dwds/expression_compiler.dart';
1113
import 'package:test/test.dart';
1214
import 'package:test_common/test_sdk_configuration.dart';
@@ -41,11 +43,9 @@ void main() async {
4143
debug: debug,
4244
);
4345
},
44-
// TODO(#2488): Restore the skip argument below, related to
45-
// https://github.com/dart-lang/sdk/issues/49277, once
46-
// https://github.com/dart-lang/webdev/issues/2488 is resolved.
47-
// skip: indexBaseMode == IndexBaseMode.base && Platform.isWindows,
48-
skip: 'https://github.com/dart-lang/webdev/issues/2488',
46+
skip: indexBaseMode == IndexBaseMode.base && Platform.isWindows
47+
? 'Skipped on Windows when indexBaseMode is base. See issue: https://github.com/dart-lang/sdk/issues/49277'
48+
: null,
4949
);
5050
}
5151
});

0 commit comments

Comments
 (0)