[dart2wasm] Implement dart:developer in dart2wasm

* We migrate the `dart:developer` implementation of dart2js to
static interop.

* We make dart2wasm use the same implementation as dart2js.

Closes https://github.com/dart-lang/sdk/issues/54991

Change-Id: I7873edc7e804500c8eca878367d9045c98a1c2e8
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/354101
Reviewed-by: Srujan Gaddam <srujzs@google.com>
Commit-Queue: Martin Kustermann <kustermann@google.com>
This commit is contained in:
Martin Kustermann 2024-02-26 11:23:29 +00:00 committed by Commit Queue
parent e0758f98bd
commit c625797f8a
6 changed files with 61 additions and 89 deletions

View file

@ -57,6 +57,13 @@ const isD8 = (typeof readbuffer === "function");
const isJSC = (typeof readFile === "function");
const isJSShell = (typeof readRelativeToScript === "function");
if (isD8) {
// D8's performance.measure is API incompatible with the browser version.
//
// (see also dart2js's `sdk/**/js_runtime/lib/preambles/d8.js`)
delete performance.measure;
}
// d8's `setTimeout` doesn't work as expected (it doesn't wait before calling
// the callback), and d8 also doesn't have `setInterval` and `queueMicrotask`.
// So we define our own event loop with these functions.

View file

@ -5,8 +5,9 @@
// Patch file for dart:developer library.
import 'dart:_internal' show patch;
import 'dart:_foreign_helper' show JS;
import 'dart:async' show Zone;
import 'dart:js_interop';
import 'dart:isolate';
// These values must be kept in sync with developer/timeline.dart.
@ -18,12 +19,55 @@ const int _flowBeginPatch = 9;
const int _flowStepPatch = 10;
const int _flowEndPatch = 11;
@JS('debugger')
external void _jsDebugger();
@JS('performance')
external JSAny? get _jsPerformance;
@JS('JSON')
external JSAny? get _jsJSON;
extension type _JSPerformance(JSObject performance) {
@JS('measure')
external JSAny? get _measureMethod;
@JS('mark')
external JSAny? get _markMethod;
external void measure(
JSString measureName, JSString startMark, JSString endMark);
external void mark(JSString markName, JSObject markOptions);
}
extension type _JSJSON(JSObject performance) {
external JSObject parse(JSString string);
}
_JSPerformance? _performance = (() {
final value = _jsPerformance;
if (value.isA<JSObject>()) {
final performance = _JSPerformance(value as JSObject);
if (performance._measureMethod != null && performance._markMethod != null) {
return performance;
}
}
return null;
})();
_JSJSON _json = (() {
final value = _jsJSON;
if (value.isA<JSObject>()) {
return value as _JSJSON;
}
throw UnsupportedError('Missing JSON.parse() support');
})();
@patch
@pragma('dart2js:tryInline')
bool debugger({bool when = true, String? message}) {
if (when) {
JS('', 'debugger');
}
if (when) _jsDebugger();
return when;
}
@ -69,9 +113,7 @@ void _postEvent(String eventKind, String eventData) {
@patch
bool _isDartStreamEnabled() {
// Timeline requires performance.measure API.
return JS('bool', r'typeof performance !== "undefined"') &&
JS('bool', r'typeof performance.measure !== "undefined"');
return _performance != null;
}
@patch
@ -156,23 +198,17 @@ void _reportTaskEvent(
_incrementEventCount(currentEventName);
currentEventName = _postfixWithCount(currentEventName);
}
final markOptions = JS('', '{detail: JSON.parse(#)}', argumentsAsJson);
// Start by creating a mark event.
JS('', 'performance.mark(#, #)', currentEventName, markOptions);
_performance!.mark(currentEventName.toJS, _json.parse(argumentsAsJson.toJS));
// If it's an end event, then create a measurement from the most recent begin
// event with the same name.
if (isEndEvent) {
final beginEventName = _createEventName(
taskId: taskId, name: name, isBeginEvent: true, isEndEvent: false);
JS(
'',
'performance.measure(#, #, #)',
name,
_postfixWithCount(beginEventName),
currentEventName,
);
_performance!.measure(name.toJS, _postfixWithCount(beginEventName).toJS,
currentEventName.toJS);
_decrementEventCount(beginEventName);
}
}

View file

@ -1,70 +0,0 @@
// Copyright (c) 2022, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
// This is a stub implementation of `dart:developer`.
import "dart:_internal" show patch;
import "dart:async" show Zone;
// Stubs for `developer.dart`.
@patch
bool debugger({bool when = true, String? message}) => when;
@patch
Object? inspect(Object? object) => object;
@patch
void log(String message,
{DateTime? time,
int? sequenceNumber,
int level = 0,
String name = '',
Zone? zone,
Object? error,
StackTrace? stackTrace}) {}
@patch
int get reachabilityBarrier => 0;
@patch
bool get extensionStreamHasListener => false;
@patch
void _postEvent(String eventKind, String eventData) {}
@patch
ServiceExtensionHandler? _lookupExtension(String method) => null;
@patch
_registerExtension(String method, ServiceExtensionHandler handler) {}
// Stubs for `timeline.dart`.
@patch
bool _isDartStreamEnabled() => false;
@patch
int _getTraceClock() => _traceClock++;
int _traceClock = 0;
@patch
int _getNextTaskId() => 0;
@patch
void _reportTaskEvent(
int taskId, int flowId, int type, String name, String argumentsAsJson) {}
@patch
abstract final class NativeRuntime {
@patch
static String? get buildId => null;
@patch
static void writeHeapSnapshotToFile(String filepath) =>
throw UnsupportedError(
"Generating heap snapshots is not supported on the wasm.");
}

View file

@ -307,7 +307,7 @@
"developer": {
"uri": "developer/developer.dart",
"patches": [
"_internal/wasm/lib/developer.dart"
"_internal/js_runtime/lib/developer_patch.dart"
]
},
"ffi": {

View file

@ -247,7 +247,7 @@ wasm_common:
developer:
uri: developer/developer.dart
patches:
- _internal/wasm/lib/developer.dart
- _internal/js_runtime/lib/developer_patch.dart
ffi:
uri: "ffi/ffi.dart"
patches:

View file

@ -9,7 +9,6 @@ convert/utf85_test: Skip # Pass, Slow Issue 12644.
[ $compiler == dart2wasm ]
async/stream_periodic3_test: Skip # Flaky, issue 50901
developer/*: SkipByDesign
fix_data_tests/*: SkipByDesign
html/*: SkipByDesign # dart:html not supported on dart2wasm
isolate/*: SkipByDesign