[dart2wasm] Make commandline JS runner (run_wasm.js) initialize location.href

By initializing `location.href` in the commandline JS runners we allow
programs running in D8 to use `Uri.base` (which depends on
`location.href`).

That in return will allow running some Dart programs in D8 that would
otherwise not run (e.g. code using `package:test/test.dart` - which uses
`Uri.base`)

TEST=tests/web/wasm/location_href_test.dart

Change-Id: Ie219f8d9ece3b92f2442200539557f116d2c84ab
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/363700
Reviewed-by: Ömer Ağacan <omersa@google.com>
Commit-Queue: Martin Kustermann <kustermann@google.com>
This commit is contained in:
Martin Kustermann 2024-04-19 12:40:23 +00:00 committed by Commit Queue
parent 98808385d0
commit 149db7eced
2 changed files with 38 additions and 8 deletions

View file

@ -64,6 +64,14 @@ if (isD8) {
delete performance.measure;
}
var args = (isD8 || isJSC) ? arguments : scriptArgs;
var dartArgs = [];
const argsSplit = args.indexOf("--");
if (argsSplit != -1) {
dartArgs = args.slice(argsSplit + 1);
args = args.slice(0, argsSplit);
}
// 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.
@ -347,6 +355,9 @@ if (isD8) {
self.clearInterval = cancelTimer;
self.queueMicrotask = addTask;
self.location = {}
self.location.href = 'file://' + args[wasmArg];
// Signals `Stopwatch._initTicker` to use `Date.now` to get ticks instead of
// `performance.now`, as it's not available in d8.
self.dartUseDateNowForTicks = true;
@ -356,14 +367,6 @@ if (isD8) {
// unfortunately d8 does not return a failed error code if an unhandled
// exception occurs asynchronously in an ES module.
const main = async () => {
var args = (isD8 || isJSC) ? arguments : scriptArgs;
var dartArgs = [];
const argsSplit = args.indexOf("--");
if (argsSplit != -1) {
dartArgs = args.slice(argsSplit + 1);
args = args.slice(0, argsSplit);
}
const dart2wasm = await import(args[jsRuntimeArg]);
/// Returns whether the `js-string` built-in is supported.

View file

@ -0,0 +1,27 @@
// Copyright (c) 2024, 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.
import 'dart:js_interop';
import 'package:expect/expect.dart';
import 'package:expect/config.dart';
@JS('location.href')
external JSString? get locationHref;
void main() {
final String? nullableHref = locationHref?.toDart;
Expect.isNotNull(nullableHref);
final String href = nullableHref!;
print('location.href = $href');
if (isBrowserConfiguration) {
Expect.isTrue(href.startsWith('http://'));
Expect.isTrue(href.contains('/test.html'));
} else {
Expect.isTrue(href.startsWith('file://'));
Expect.isTrue(href.endsWith('.wasm'));
}
}