[dart2wasm] Ensure we never emit string import names that are longer than 100_000 bytes

D8 currently allows longer import names bot JSShell and JSC do
issue a validation error if the name is larger than 100_000.

This fixes e.g. StarryStrings benchmarks on JSC/JSShell

TEST=tests/web/wasm/long_string_import_test.dart

Change-Id: I93df5d0e3014f6157750511fe486677e9b9413d7
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/450922
Reviewed-by: Ömer Ağacan <omersa@google.com>
Commit-Queue: Martin Kustermann <kustermann@google.com>
This commit is contained in:
Martin Kustermann
2025-09-23 04:02:01 -07:00
committed by Commit Queue
parent dfac9f1ded
commit 520607a6a7
3 changed files with 18 additions and 2 deletions
+10 -1
View File
@@ -2,6 +2,8 @@
// 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:convert';
import 'package:kernel/ast.dart';
import 'package:kernel/class_hierarchy.dart'
show ClassHierarchy, ClassHierarchySubtypes, ClosedWorldClassHierarchy;
@@ -1866,7 +1868,14 @@ class Translator with KernelNodes {
return false;
}
if (hasUnpairedSurrogate(s)) {
// Maximum length in bytes of an import name (JSC & JSShell will issue a
// wasm validation error if we import names larger than this).
const maxStringBytes = 100_000;
// A code unit of Dart string can take up max 3 bytes, we use it as first
// condition to avoid `utf8.encode()` in most situations.
final stringInBytesIsToLarge = (s.length * 3) > maxStringBytes &&
utf8.encode(s).length > maxStringBytes;
if (hasUnpairedSurrogate(s) || stringInBytesIsToLarge) {
// Unpaired surrogates can't be encoded as UTF-8, import them from JS
// runtime.
final i = internalizedStringsForJSRuntime.length;
+1 -1
View File
@@ -2,7 +2,7 @@ name: dart2wasm
# This package is not intended for consumption on pub.dev. DO NOT publish.
publish_to: none
environment:
sdk: ^3.5.0
sdk: ^3.6.0
resolution: workspace
File diff suppressed because one or more lines are too long