|
| 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 'package:kernel/ast.dart'; |
| 6 | +import 'package:wasm_builder/wasm_builder.dart'; |
| 7 | + |
| 8 | +import 'util.dart'; |
| 9 | + |
| 10 | +class DynamicModuleExportRepository |
| 11 | + extends MetadataRepository<DynamicModuleExports> { |
| 12 | + static const repositoryTag = 'wasm.dynamic-modules.exports'; |
| 13 | + |
| 14 | + @override |
| 15 | + final String tag = repositoryTag; |
| 16 | + |
| 17 | + @override |
| 18 | + final Map<TreeNode, DynamicModuleExports> mapping = {}; |
| 19 | + |
| 20 | + @override |
| 21 | + DynamicModuleExports readFromBinary(_, BinarySource source) { |
| 22 | + return DynamicModuleExports._readFromBinary(source); |
| 23 | + } |
| 24 | + |
| 25 | + @override |
| 26 | + void writeToBinary(DynamicModuleExports exports, Node node, BinarySink sink) { |
| 27 | + exports._writeToBinary(sink); |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +class DynamicModuleExports { |
| 32 | + final Map<int, String> _callableNames = {}; |
| 33 | + final Map<int, String> _constantNames = {}; |
| 34 | + final Map<int, String> _constantInitializerNames = {}; |
| 35 | + |
| 36 | + DynamicModuleExports(); |
| 37 | + |
| 38 | + factory DynamicModuleExports._readFromBinary(BinarySource source) { |
| 39 | + void readMap(Map<int, String> map) { |
| 40 | + final length = source.readUInt30(); |
| 41 | + for (int i = 0; i < length; i++) { |
| 42 | + final id = source.readUInt30(); |
| 43 | + final name = source.readStringReference(); |
| 44 | + map[id] = name; |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + final exports = DynamicModuleExports(); |
| 49 | + readMap(exports._callableNames); |
| 50 | + readMap(exports._constantNames); |
| 51 | + readMap(exports._constantInitializerNames); |
| 52 | + return exports; |
| 53 | + } |
| 54 | + |
| 55 | + void _writeToBinary(BinarySink sink) { |
| 56 | + void writeMap(Map<int, String> map) { |
| 57 | + sink.writeUInt30(map.length); |
| 58 | + map.forEach((id, name) { |
| 59 | + sink.writeUInt30(id); |
| 60 | + sink.writeStringReference(name); |
| 61 | + }); |
| 62 | + } |
| 63 | + |
| 64 | + writeMap(_callableNames); |
| 65 | + writeMap(_constantNames); |
| 66 | + writeMap(_constantInitializerNames); |
| 67 | + } |
| 68 | + |
| 69 | + String? getCallableName(int callableReferenceId) => |
| 70 | + _callableNames[callableReferenceId]; |
| 71 | + |
| 72 | + String? getConstantName(int constantId) => _constantNames[constantId]; |
| 73 | + |
| 74 | + String? getConstantInitializerName(int constantId) => |
| 75 | + _constantInitializerNames[constantId]; |
| 76 | +} |
| 77 | + |
| 78 | +/// A generator for export names (when minification is enabled). |
| 79 | +/// |
| 80 | +/// For simplicity and readability of generated names, we just use the base64 |
| 81 | +/// encoding of an integer counter. |
| 82 | +class ExportNamer { |
| 83 | + final bool minify; |
| 84 | + |
| 85 | + ExportNamer(this.minify); |
| 86 | + |
| 87 | + final Set<String> _reservedNames = {}; |
| 88 | + |
| 89 | + /// Mark a name as reserved by the `wasm:export` or `wasm:weak-export` |
| 90 | + /// annotations so that it will not be generated as a minified export name. |
| 91 | + void reserveName(String name) { |
| 92 | + if (!minify) return; |
| 93 | + final added = _reservedNames.add(name); |
| 94 | + assert(added, "Name '$name' is already reserved"); |
| 95 | + } |
| 96 | + |
| 97 | + int _nameCounter = 0; |
| 98 | + |
| 99 | + String _getExportName(String name) { |
| 100 | + if (!minify) return name; |
| 101 | + do { |
| 102 | + name = intToBase64(_nameCounter++); |
| 103 | + } while (_reservedNames.contains(name)); |
| 104 | + return name; |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +/// Manages exporting entities in a minification-aware way. |
| 109 | +/// |
| 110 | +/// The [Exporter] stores associations between entities exported from a dynamic |
| 111 | +/// main module and their export names in [dynamicModuleExports] so that dynamic |
| 112 | +/// submodules can import these entities with those names. |
| 113 | +/// |
| 114 | +/// The `export___` methods add relevant associations to [dynamicModuleExports] |
| 115 | +/// and add the given exportable to the Wasm exports of the specified module |
| 116 | +/// under the computed export name. |
| 117 | +class Exporter { |
| 118 | + final ExportNamer _namer; |
| 119 | + |
| 120 | + Exporter(this._namer); |
| 121 | + |
| 122 | + final DynamicModuleExports dynamicModuleExports = DynamicModuleExports(); |
| 123 | + |
| 124 | + void exportCallable(ModuleBuilder module, String name, |
| 125 | + FunctionBuilder function, int callableReferenceId) { |
| 126 | + dynamicModuleExports._callableNames[callableReferenceId] = |
| 127 | + _export(module, name, function); |
| 128 | + } |
| 129 | + |
| 130 | + void exportConstant(ModuleBuilder module, String name, GlobalBuilder constant, |
| 131 | + int constantId) { |
| 132 | + dynamicModuleExports._constantNames[constantId] = |
| 133 | + _export(module, name, constant); |
| 134 | + } |
| 135 | + |
| 136 | + void exportConstantInitializer(ModuleBuilder module, String name, |
| 137 | + FunctionBuilder initializer, int constantId) { |
| 138 | + dynamicModuleExports._constantInitializerNames[constantId] = |
| 139 | + _export(module, name, initializer); |
| 140 | + } |
| 141 | + |
| 142 | + String _export(ModuleBuilder module, String name, Exportable exportable) { |
| 143 | + final exportName = _namer._getExportName(name); |
| 144 | + module.exports.export(exportName, exportable); |
| 145 | + return exportName; |
| 146 | + } |
| 147 | +} |
0 commit comments