Skip to content

Commit 7dd8aed

Browse files
mkustermannCommit Queue
authored andcommitted
[dart2wasm] Make imported globals/functions have nicer name
When a function/global is used cross-module then the implementation will export from defining module and import from using module. => Make the imported function/global have the same local name as the defining one. => This only influences the names in the name section which are stripped for deployed modules. Change-Id: I770233f5e7c5d08050e8189402e7294b88212651 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/458840 Reviewed-by: Ömer Ağacan <omersa@google.com> Commit-Queue: Ömer Ağacan <omersa@google.com>
1 parent fc8b3d7 commit 7dd8aed

File tree

8 files changed

+83
-26
lines changed

8 files changed

+83
-26
lines changed

pkg/dart2wasm/lib/translator.dart

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2807,8 +2807,10 @@ class WasmFunctionImporter extends _WasmImporter<w.BaseFunction> {
28072807
@override
28082808
w.BaseFunction _import(w.ModuleBuilder importingModule,
28092809
w.BaseFunction definition, String moduleName, String importName) {
2810-
return importingModule.functions
2810+
final function = importingModule.functions
28112811
.import(moduleName, importName, definition.type, definition.name);
2812+
function.functionName = definition.functionName;
2813+
return function;
28122814
}
28132815
}
28142816

@@ -2818,8 +2820,10 @@ class WasmGlobalImporter extends _WasmImporter<w.Global> {
28182820
@override
28192821
w.Global _import(w.ModuleBuilder importingModule, w.Global definition,
28202822
String moduleName, String importName) {
2821-
return importingModule.globals
2822-
.import(moduleName, importName, definition.type);
2823+
final global =
2824+
importingModule.globals.import(moduleName, importName, definition.type);
2825+
global.globalName = definition.globalName;
2826+
return global;
28232827
}
28242828
}
28252829

pkg/dart2wasm/test/ir_tests/deferred.constant.wat

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@
66
(field $field0 i32)
77
(field $field1 (mut i32))
88
(field $fun (ref $#Closure-0-1)))))
9-
(type $type246 <...>)
10-
(type $type249 <...>)
11-
(type $type252 <...>)
12-
(table $static1-0 (export "static1-0") 1 (ref null $type252))
13-
(table $static2-0 (export "static2-0") 1 (ref null $type246))
14-
(table $static3-0 (export "static3-0") 1 (ref null $type249))
9+
(type $type245 <...>)
10+
(type $type248 <...>)
11+
(type $type251 <...>)
12+
(table $static1-0 (export "static1-0") 1 (ref null $type251))
13+
(table $static2-0 (export "static2-0") 1 (ref null $type245))
14+
(table $static3-0 (export "static3-0") 1 (ref null $type248))
1515
(func $print (param $var0 (ref null $#Top)) (result (ref null $#Top)) <...>)
1616
(func $"modMainUseH0 <noInline>"
1717
i64.const 0

pkg/dart2wasm/test/ir_tests/deferred.constant_module1.wat

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@
2222
(type $BoxedInt <...>)
2323
(func $print (import "module0" "func5") (param (ref null $#Top)) (result (ref null $#Top)))
2424
(func $JSStringImpl._interpolate (import "module0" "func6") (param (ref $Array<Object?>)) (result (ref $JSStringImpl)))
25-
(global $module0.global0 (import "module0" "global0") (ref $JSStringImpl))
26-
(global $module0.global5 (import "module0" "global5") (ref $_InterfaceType))
27-
(global $module0.global7 (import "module0" "global7") (ref $_FunctionType))
25+
(global $"C21 \")\"" (import "module0" "global0") (ref $JSStringImpl))
26+
(global $"C28 _InterfaceType" (import "module0" "global5") (ref $_InterfaceType))
27+
(global $"C455 _FunctionType" (import "module0" "global7") (ref $_FunctionType))
2828
(global $S.globalH1Bar< (import "S" "globalH1Bar<") (ref extern))
2929
(global $global7 (ref $#Vtable-1-1) <...>)
3030
(global $global4 (ref $#DummyStruct) <...>)
@@ -73,7 +73,7 @@
7373
local.get $var0
7474
global.get $"C464 \">(\""
7575
local.get $var1
76-
global.get $module0.global0
76+
global.get $"C21 \")\""
7777
array.new_fixed $Array<Object?> 5
7878
call $JSStringImpl._interpolate
7979
call $print
@@ -119,13 +119,13 @@
119119
global.set $"C460 globalH1Foo tear-off"
120120
local.get $var0
121121
end $label1
122-
global.get $module0.global5
122+
global.get $"C28 _InterfaceType"
123123
struct.new $#InstantiationContext-1-1
124124
ref.func $"dynamic call entry"
125125
ref.func $"#dummy function (ref struct) -> (ref null #Top)"
126126
ref.func $"instantiation constant trampoline"
127127
struct.new $#Vtable-0-1
128-
global.get $module0.global7
128+
global.get $"C455 _FunctionType"
129129
struct.new $#Closure-0-1
130130
local.tee $var1
131131
global.set $"C461 InstantiationConstant(globalH1Foo<int>)"

pkg/dart2wasm/test/ir_tests/deferred.constant_module2.wat

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
(type $Array<Object?> <...>)
55
(type $JSStringImpl <...>)
66
(type $Array<_Type> <...>)
7-
(type $BoxedInt <...>)
87
(type $_FunctionType <...>)
98
(type $#ClosureBase <...>)
109
(type $#Vtable-0-1 <...>)
@@ -15,7 +14,7 @@
1514
(field $fun (ref $#Closure-0-1)))))
1615
(type $#DummyStruct <...>)
1716
(func $print (import "module0" "func5") (param (ref null $#Top)) (result (ref null $#Top)))
18-
(global $module0.global7 (import "module0" "global7") (ref $_FunctionType))
17+
(global $"C455 _FunctionType" (import "module0" "global7") (ref $_FunctionType))
1918
(global $S.globalH0Foo (import "S" "globalH0Foo") (ref extern))
2019
(global $global6 (ref $#Vtable-0-1) <...>)
2120
(global $global3 (ref $#DummyStruct) <...>)
@@ -33,18 +32,10 @@
3332
call $print
3433
)
3534
(func $"globalH0Foo tear-off dynamic call entry" (param $var0 (ref $#ClosureBase)) (param $var1 (ref $Array<_Type>)) (param $var2 (ref $Array<Object?>)) (param $var3 (ref $Array<Object?>)) (result (ref null $#Top))
36-
local.get $var2
37-
i32.const 0
38-
array.get $Array<Object?>
39-
ref.cast $BoxedInt
40-
drop
4135
global.get $"C467 \"globalH0Foo\""
4236
call $print
4337
)
4438
(func $"globalH0Foo tear-off trampoline" (param $var0 (ref struct)) (param $var1 (ref null $#Top)) (result (ref null $#Top))
45-
local.get $var1
46-
ref.cast $BoxedInt
47-
drop
4839
global.get $"C467 \"globalH0Foo\""
4940
call $print
5041
)
@@ -60,7 +51,7 @@
6051
i32.const 0
6152
global.get $global3
6253
global.get $global6
63-
global.get $module0.global7
54+
global.get $"C455 _FunctionType"
6455
struct.new $#Closure-0-1
6556
local.tee $var0
6657
global.set $"C465 globalH0Foo tear-off"
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright (c) 2025, 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+
// functionFilter=Foo
6+
// compilerOption=--enable-deferred-loading
7+
// compilerOption=--no-minify
8+
9+
import 'import_name.h.0.dart' deferred as h0;
10+
11+
void main() async {
12+
await h0.loadLibrary();
13+
h0.deferredFoo();
14+
}
15+
16+
@pragma('wasm:never-inline')
17+
void mainFoo() {
18+
print('hello world');
19+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// Copyright (c) 2025, 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+
import 'import_name.dart';
6+
7+
@pragma('wasm:never-inline')
8+
void deferredFoo() {
9+
mainFoo();
10+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
(module $module0
2+
(type $#Top (struct
3+
(field $field0 i32)))
4+
(type $Object (sub $#Top (struct
5+
(field $field0 i32)
6+
(field $field1 (mut i32)))))
7+
(type $JSStringImpl (sub final $Object (struct
8+
(field $field0 i32)
9+
(field $field1 (mut i32))
10+
(field $_ref externref))))
11+
(global $"S.hello world" (import "S" "hello world") (ref extern))
12+
(global $"C367 \"hello world\"" (ref $JSStringImpl)
13+
(i32.const 4)
14+
(i32.const 0)
15+
(global.get $"S.hello world")
16+
(struct.new $JSStringImpl))
17+
(func $print (param $var0 (ref $#Top)) <...>)
18+
(func $"mainFoo <noInline>" (export "func0") (result (ref null $#Top))
19+
global.get $"C367 \"hello world\""
20+
call $print
21+
ref.null none
22+
)
23+
)
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
(module $module1
2+
(type $#Top (struct
3+
(field $field0 i32)))
4+
(func $"mainFoo <noInline>" (import "module0" "func0") (result (ref null $#Top)))
5+
(func $"deferredFoo <noInline>" (result (ref null $#Top))
6+
call $"mainFoo <noInline>"
7+
drop
8+
ref.null none
9+
)
10+
)

0 commit comments

Comments
 (0)