OLD | NEW |
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
| 5 import 'dart:isolate'; |
| 6 |
5 patch bool debugger({bool when: true, | 7 patch bool debugger({bool when: true, |
6 String message}) native "Developer_debugger"; | 8 String message}) native "Developer_debugger"; |
7 | 9 |
8 patch Object inspect(Object object) native "Developer_inspect"; | 10 patch Object inspect(Object object) native "Developer_inspect"; |
9 | 11 |
10 patch void log(String message, | 12 patch void log(String message, |
11 {DateTime time, | 13 {DateTime time, |
12 int sequenceNumber, | 14 int sequenceNumber, |
13 int level: 0, | 15 int level: 0, |
14 String name: '', | 16 String name: '', |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
48 String name, | 50 String name, |
49 Zone zone, | 51 Zone zone, |
50 Object error, | 52 Object error, |
51 StackTrace stackTrace) native "Developer_log"; | 53 StackTrace stackTrace) native "Developer_log"; |
52 | 54 |
53 patch ServiceExtensionHandler _lookupExtension(String method) | 55 patch ServiceExtensionHandler _lookupExtension(String method) |
54 native "Developer_lookupExtension"; | 56 native "Developer_lookupExtension"; |
55 | 57 |
56 patch _registerExtension(String method, ServiceExtensionHandler handler) | 58 patch _registerExtension(String method, ServiceExtensionHandler handler) |
57 native "Developer_registerExtension"; | 59 native "Developer_registerExtension"; |
| 60 |
| 61 // This code is only invoked when there is no other Dart code on the stack. |
| 62 _runExtension(ServiceExtensionHandler handler, |
| 63 String method, |
| 64 List<String> parameterKeys, |
| 65 List<String> parameterValues, |
| 66 SendPort replyPort, |
| 67 Object id) { |
| 68 var parameters = {}; |
| 69 for (var i = 0; i < parameterKeys.length; i++) { |
| 70 parameters[parameterKeys[i]] = parameterValues[i]; |
| 71 } |
| 72 var response; |
| 73 try { |
| 74 response = handler(method, parameters); |
| 75 } catch (e, st) { |
| 76 var errorDetails = (st == null) ? '$e' : '$e\n$st'; |
| 77 response = new ServiceExtensionResponse.error( |
| 78 ServiceExtensionResponse.kExtensionError, |
| 79 errorDetails); |
| 80 _postResponse(replyPort, id, response); |
| 81 return; |
| 82 } |
| 83 if (response is! Future) { |
| 84 response = new ServiceExtensionResponse.error( |
| 85 ServiceExtensionResponse.kExtensionError, |
| 86 "Extension handler must return a Future"); |
| 87 _postResponse(replyPort, id, response); |
| 88 return; |
| 89 } |
| 90 response.catchError((e, st) { |
| 91 // Catch any errors eagerly and wrap them in a ServiceExtensionResponse. |
| 92 var errorDetails = (st == null) ? '$e' : '$e\n$st'; |
| 93 return new ServiceExtensionResponse.error( |
| 94 ServiceExtensionResponse.kExtensionError, |
| 95 errorDetails); |
| 96 }).then((response) { |
| 97 // Post the valid response or the wrapped error after verifying that |
| 98 // the response is a ServiceExtensionResponse. |
| 99 if (response is! ServiceExtensionResponse) { |
| 100 response = new ServiceExtensionResponse.error( |
| 101 ServiceExtensionResponse.kExtensionError, |
| 102 "Extension handler must complete to a ServiceExtensionResponse"); |
| 103 } |
| 104 _postResponse(replyPort, id, response); |
| 105 }).catchError((e, st) { |
| 106 // We do not expect any errors to occur in the .then or .catchError blocks |
| 107 // but, suppress them just in case. |
| 108 }); |
| 109 } |
| 110 |
| 111 // This code is only invoked by _runExtension. |
| 112 _postResponse(SendPort replyPort, |
| 113 Object id, |
| 114 ServiceExtensionResponse response) { |
| 115 assert(replyPort != null); |
| 116 if (id == null) { |
| 117 // No id -> no response. |
| 118 replyPort.send(null); |
| 119 return; |
| 120 } |
| 121 assert(id != null); |
| 122 StringBuffer sb = new StringBuffer(); |
| 123 sb.write('{"jsonrpc":"2.0",'); |
| 124 if (response._isError()) { |
| 125 sb.write('"error":'); |
| 126 } else { |
| 127 sb.write('"result":'); |
| 128 } |
| 129 sb.write('${response._toString()},'); |
| 130 if (id is String) { |
| 131 sb.write('"id":"$id"}'); |
| 132 } else { |
| 133 sb.write('"id":$id}'); |
| 134 } |
| 135 replyPort.send(sb.toString()); |
| 136 } |
OLD | NEW |