|
| 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 | +} |
0 commit comments