|
| 1 | +// Copyright (c) 2020, 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 | +part of dds; |
| 6 | + |
| 7 | +/// A helper class which handles `evaluate` and `evaluateInFrame` calls by |
| 8 | +/// potentially forwarding compilation requests to an external compilation |
| 9 | +/// service like Flutter Tools. |
| 10 | +class _ExpressionEvaluator { |
| 11 | + _ExpressionEvaluator(this.dds); |
| 12 | + |
| 13 | + Future<Map<String, dynamic>> execute(json_rpc.Parameters parameters) async { |
| 14 | + final isolateId = parameters['isolateId'].asString; |
| 15 | + final expression = parameters['expression'].asString; |
| 16 | + Map<String, dynamic> buildScopeResponse; |
| 17 | + |
| 18 | + try { |
| 19 | + buildScopeResponse = await _buildScope(parameters); |
| 20 | + } on json_rpc.RpcException catch (e) { |
| 21 | + throw _RpcErrorCodes.buildRpcException( |
| 22 | + _RpcErrorCodes.kExpressionCompilationError, |
| 23 | + data: e.data, |
| 24 | + ); |
| 25 | + } |
| 26 | + String kernelBase64; |
| 27 | + try { |
| 28 | + kernelBase64 = |
| 29 | + await _compileExpression(isolateId, expression, buildScopeResponse); |
| 30 | + } on json_rpc.RpcException catch (e) { |
| 31 | + throw _RpcErrorCodes.buildRpcException( |
| 32 | + _RpcErrorCodes.kExpressionCompilationError, |
| 33 | + data: e.data, |
| 34 | + ); |
| 35 | + } |
| 36 | + return await _evaluateCompiledExpression( |
| 37 | + parameters, isolateId, kernelBase64); |
| 38 | + } |
| 39 | + |
| 40 | + Future<Map<String, dynamic>> _buildScope( |
| 41 | + json_rpc.Parameters parameters) async { |
| 42 | + final params = _setupParams(parameters); |
| 43 | + params['isolateId'] = parameters['isolateId'].asString; |
| 44 | + if (parameters['scope'].asMapOr(null) != null) { |
| 45 | + params['scope'] = parameters['scope'].asMap; |
| 46 | + } |
| 47 | + return await dds._vmServiceClient.sendRequest( |
| 48 | + '_buildExpressionEvaluationScope', |
| 49 | + params, |
| 50 | + ); |
| 51 | + } |
| 52 | + |
| 53 | + Future<String> _compileExpression(String isolateId, String expression, |
| 54 | + Map<String, dynamic> buildScopeResponseResult) async { |
| 55 | + _DartDevelopmentServiceClient externalClient = |
| 56 | + dds.clientManager.findFirstClientThatHandlesService( |
| 57 | + 'compileExpression', |
| 58 | + ); |
| 59 | + |
| 60 | + final compileParams = <String, dynamic>{ |
| 61 | + 'isolateId': isolateId, |
| 62 | + 'expression': expression, |
| 63 | + 'definitions': buildScopeResponseResult['param_names'], |
| 64 | + 'typeDefinitions': buildScopeResponseResult['type_params_names'], |
| 65 | + 'libraryUri': buildScopeResponseResult['libraryUri'], |
| 66 | + 'isStatic': buildScopeResponseResult['isStatic'], |
| 67 | + }; |
| 68 | + |
| 69 | + final klass = buildScopeResponseResult['klass']; |
| 70 | + if (klass != null) { |
| 71 | + compileParams['klass'] = klass; |
| 72 | + } |
| 73 | + // TODO(bkonyi): handle service disappeared case? |
| 74 | + try { |
| 75 | + if (externalClient != null) { |
| 76 | + return (await externalClient.sendRequest( |
| 77 | + 'compileExpression', |
| 78 | + compileParams, |
| 79 | + ))['result']['kernelBytes']; |
| 80 | + } else { |
| 81 | + // Fallback to compiling using the kernel service. |
| 82 | + return (await dds._vmServiceClient.sendRequest( |
| 83 | + '_compileExpression', |
| 84 | + compileParams, |
| 85 | + ))['kernelBytes']; |
| 86 | + } |
| 87 | + } on json_rpc.RpcException catch (e) { |
| 88 | + throw _RpcErrorCodes.buildRpcException( |
| 89 | + _RpcErrorCodes.kExpressionCompilationError, |
| 90 | + data: e.data, |
| 91 | + ); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + Future<Map<String, dynamic>> _evaluateCompiledExpression( |
| 96 | + json_rpc.Parameters parameters, |
| 97 | + String isolateId, |
| 98 | + String kernelBase64, |
| 99 | + ) async { |
| 100 | + final params = _setupParams(parameters); |
| 101 | + params['isolateId'] = isolateId; |
| 102 | + params['kernelBytes'] = kernelBase64; |
| 103 | + params['disableBreakpoints'] = |
| 104 | + parameters['disableBreakpoints'].asBoolOr(false); |
| 105 | + if (parameters['scope'].asMapOr(null) != null) { |
| 106 | + params['scope'] = parameters['scope'].asMap; |
| 107 | + } |
| 108 | + return await dds._vmServiceClient.sendRequest( |
| 109 | + '_evaluateCompiledExpression', |
| 110 | + params, |
| 111 | + ); |
| 112 | + } |
| 113 | + |
| 114 | + Map<String, dynamic> _setupParams(json_rpc.Parameters parameters) { |
| 115 | + if (parameters.method == 'evaluateInFrame') { |
| 116 | + return <String, dynamic>{ |
| 117 | + 'frameIndex': parameters['frameIndex'].asInt, |
| 118 | + }; |
| 119 | + } else { |
| 120 | + assert(parameters.method == 'evaluate'); |
| 121 | + return <String, dynamic>{ |
| 122 | + 'targetId': parameters['targetId'].asString, |
| 123 | + }; |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + final _DartDevelopmentService dds; |
| 128 | +} |
0 commit comments