| ... | ... | @@ -150,10 +150,10 @@ nav_fixups: std.ArrayListUnmanaged(NavFixup) = .empty, |
| 150 | 150 | symbol_table: std.AutoArrayHashMapUnmanaged(String, void) = .empty, |
| 151 | 151 | |
| 152 | 152 | /// When importing objects from the host environment, a name must be supplied. |
| 153 | | /// LLVM uses "env" by default when none is given. This would be a good default for Zig |
| 154 | | /// to support existing code. |
| 155 | | /// TODO: Allow setting this through a flag? |
| 156 | | host_name: String, |
| 153 | /// LLVM uses "env" by default when none is given. |
| 154 | /// This value is passed to object files since wasm tooling conventions provides |
| 155 | /// no way to specify the module name in the symbol table. |
| 156 | object_host_name: OptionalString, |
| 157 | 157 | |
| 158 | 158 | /// Memory section |
| 159 | 159 | memories: std.wasm.Memory = .{ .limits = .{ |
| ... | ... | @@ -737,7 +737,7 @@ const DebugSection = struct {}; |
| 737 | 737 | |
| 738 | 738 | pub const FunctionImport = extern struct { |
| 739 | 739 | flags: SymbolFlags, |
| 740 | | module_name: String, |
| 740 | module_name: OptionalString, |
| 741 | 741 | source_location: SourceLocation, |
| 742 | 742 | resolution: Resolution, |
| 743 | 743 | type: FunctionType.Index, |
| ... | ... | @@ -862,7 +862,7 @@ pub const FunctionImport = extern struct { |
| 862 | 862 | return index.key(wasm).*; |
| 863 | 863 | } |
| 864 | 864 | |
| 865 | | pub fn moduleName(index: Index, wasm: *const Wasm) String { |
| 865 | pub fn moduleName(index: Index, wasm: *const Wasm) OptionalString { |
| 866 | 866 | return index.value(wasm).module_name; |
| 867 | 867 | } |
| 868 | 868 | |
| ... | ... | @@ -888,7 +888,7 @@ pub const Function = extern struct { |
| 888 | 888 | |
| 889 | 889 | pub const GlobalImport = extern struct { |
| 890 | 890 | flags: SymbolFlags, |
| 891 | | module_name: String, |
| 891 | module_name: OptionalString, |
| 892 | 892 | source_location: SourceLocation, |
| 893 | 893 | resolution: Resolution, |
| 894 | 894 | |
| ... | ... | @@ -1009,7 +1009,7 @@ pub const GlobalImport = extern struct { |
| 1009 | 1009 | return index.key(wasm).*; |
| 1010 | 1010 | } |
| 1011 | 1011 | |
| 1012 | | pub fn moduleName(index: Index, wasm: *const Wasm) String { |
| 1012 | pub fn moduleName(index: Index, wasm: *const Wasm) OptionalString { |
| 1013 | 1013 | return index.value(wasm).module_name; |
| 1014 | 1014 | } |
| 1015 | 1015 | |
| ... | ... | @@ -1114,7 +1114,7 @@ pub const TableImport = extern struct { |
| 1114 | 1114 | return index.key(wasm).*; |
| 1115 | 1115 | } |
| 1116 | 1116 | |
| 1117 | | pub fn moduleName(index: Index, wasm: *const Wasm) String { |
| 1117 | pub fn moduleName(index: Index, wasm: *const Wasm) OptionalString { |
| 1118 | 1118 | return index.value(wasm).module_name; |
| 1119 | 1119 | } |
| 1120 | 1120 | }; |
| ... | ... | @@ -1604,7 +1604,7 @@ pub const ZcuImportIndex = enum(u32) { |
| 1604 | 1604 | return wasm.getExistingString(name_slice).?; |
| 1605 | 1605 | } |
| 1606 | 1606 | |
| 1607 | | pub fn moduleName(index: ZcuImportIndex, wasm: *const Wasm) String { |
| 1607 | pub fn moduleName(index: ZcuImportIndex, wasm: *const Wasm) OptionalString { |
| 1608 | 1608 | const zcu = wasm.base.comp.zcu.?; |
| 1609 | 1609 | const ip = &zcu.intern_pool; |
| 1610 | 1610 | const nav_index = index.ptr(wasm).*; |
| ... | ... | @@ -1613,8 +1613,8 @@ pub const ZcuImportIndex = enum(u32) { |
| 1613 | 1613 | .@"extern" => |*ext| ext, |
| 1614 | 1614 | else => unreachable, |
| 1615 | 1615 | }; |
| 1616 | | const lib_name = ext.lib_name.toSlice(ip) orelse return wasm.host_name; |
| 1617 | | return wasm.getExistingString(lib_name).?; |
| 1616 | const lib_name = ext.lib_name.toSlice(ip) orelse return .none; |
| 1617 | return wasm.getExistingString(lib_name).?.toOptional(); |
| 1618 | 1618 | } |
| 1619 | 1619 | |
| 1620 | 1620 | pub fn functionType(index: ZcuImportIndex, wasm: *Wasm) FunctionType.Index { |
| ... | ... | @@ -1639,8 +1639,8 @@ pub const ZcuImportIndex = enum(u32) { |
| 1639 | 1639 | } |
| 1640 | 1640 | }; |
| 1641 | 1641 | |
| 1642 | | /// 0. Index into `object_function_imports`. |
| 1643 | | /// 1. Index into `imports`. |
| 1642 | /// 0. Index into `Wasm.object_function_imports`. |
| 1643 | /// 1. Index into `Wasm.imports`. |
| 1644 | 1644 | pub const FunctionImportId = enum(u32) { |
| 1645 | 1645 | _, |
| 1646 | 1646 | |
| ... | ... | @@ -1695,7 +1695,7 @@ pub const FunctionImportId = enum(u32) { |
| 1695 | 1695 | }; |
| 1696 | 1696 | } |
| 1697 | 1697 | |
| 1698 | | pub fn moduleName(id: FunctionImportId, wasm: *const Wasm) String { |
| 1698 | pub fn moduleName(id: FunctionImportId, wasm: *const Wasm) OptionalString { |
| 1699 | 1699 | return switch (unpack(id, wasm)) { |
| 1700 | 1700 | inline .object_function_import, .zcu_import => |i| i.moduleName(wasm), |
| 1701 | 1701 | }; |
| ... | ... | @@ -1706,6 +1706,24 @@ pub const FunctionImportId = enum(u32) { |
| 1706 | 1706 | inline .object_function_import, .zcu_import => |i| i.functionType(wasm), |
| 1707 | 1707 | }; |
| 1708 | 1708 | } |
| 1709 | |
| 1710 | /// Asserts not emitting an object, and `Wasm.import_symbols` is false. |
| 1711 | pub fn undefinedAllowed(id: FunctionImportId, wasm: *const Wasm) bool { |
| 1712 | assert(!wasm.import_symbols); |
| 1713 | assert(wasm.base.comp.config.output_mode != .Obj); |
| 1714 | return switch (unpack(id, wasm)) { |
| 1715 | .object_function_import => |i| { |
| 1716 | const import = i.value(wasm); |
| 1717 | return import.flags.binding == .strong and import.module_name != .none; |
| 1718 | }, |
| 1719 | .zcu_import => |i| { |
| 1720 | const zcu = wasm.base.comp.zcu.?; |
| 1721 | const ip = &zcu.intern_pool; |
| 1722 | const ext = ip.getNav(i.ptr(wasm).*).toExtern(ip).?; |
| 1723 | return !ext.is_weak_linkage and ext.lib_name != .none; |
| 1724 | }, |
| 1725 | }; |
| 1726 | } |
| 1709 | 1727 | }; |
| 1710 | 1728 | |
| 1711 | 1729 | /// 0. Index into `object_global_imports`. |
| ... | ... | @@ -1760,7 +1778,7 @@ pub const GlobalImportId = enum(u32) { |
| 1760 | 1778 | }; |
| 1761 | 1779 | } |
| 1762 | 1780 | |
| 1763 | | pub fn moduleName(id: GlobalImportId, wasm: *const Wasm) String { |
| 1781 | pub fn moduleName(id: GlobalImportId, wasm: *const Wasm) OptionalString { |
| 1764 | 1782 | return switch (unpack(id, wasm)) { |
| 1765 | 1783 | inline .object_global_import, .zcu_import => |i| i.moduleName(wasm), |
| 1766 | 1784 | }; |
| ... | ... | @@ -2082,7 +2100,7 @@ pub fn createEmpty( |
| 2082 | 2100 | |
| 2083 | 2101 | .entry_name = undefined, |
| 2084 | 2102 | .dump_argv_list = .empty, |
| 2085 | | .host_name = undefined, |
| 2103 | .object_host_name = .none, |
| 2086 | 2104 | .preloaded_strings = undefined, |
| 2087 | 2105 | }; |
| 2088 | 2106 | if (use_llvm and comp.config.have_zcu) { |
| ... | ... | @@ -2090,7 +2108,7 @@ pub fn createEmpty( |
| 2090 | 2108 | } |
| 2091 | 2109 | errdefer wasm.base.destroy(); |
| 2092 | 2110 | |
| 2093 | | wasm.host_name = try wasm.internString("env"); |
| 2111 | if (options.object_host_name) |name| wasm.object_host_name = (try wasm.internString(name)).toOptional(); |
| 2094 | 2112 | |
| 2095 | 2113 | inline for (@typeInfo(PreloadedStrings).@"struct".fields) |field| { |
| 2096 | 2114 | @field(wasm.preloaded_strings, field.name) = try wasm.internString(field.name); |
| ... | ... | @@ -2162,7 +2180,7 @@ fn parseObject(wasm: *Wasm, obj: link.Input.Object) !void { |
| 2162 | 2180 | var ss: Object.ScratchSpace = .{}; |
| 2163 | 2181 | defer ss.deinit(gpa); |
| 2164 | 2182 | |
| 2165 | | const object = try Object.parse(wasm, file_contents, obj.path, null, wasm.host_name, &ss, obj.must_link, gc_sections); |
| 2183 | const object = try Object.parse(wasm, file_contents, obj.path, null, wasm.object_host_name, &ss, obj.must_link, gc_sections); |
| 2166 | 2184 | wasm.objects.appendAssumeCapacity(object); |
| 2167 | 2185 | } |
| 2168 | 2186 | |
| ... | ... | @@ -2201,7 +2219,7 @@ fn parseArchive(wasm: *Wasm, obj: link.Input.Object) !void { |
| 2201 | 2219 | try wasm.objects.ensureUnusedCapacity(gpa, offsets.count()); |
| 2202 | 2220 | for (offsets.keys()) |file_offset| { |
| 2203 | 2221 | const contents = file_contents[file_offset..]; |
| 2204 | | const object = try archive.parseObject(wasm, contents, obj.path, wasm.host_name, &ss, obj.must_link, gc_sections); |
| 2222 | const object = try archive.parseObject(wasm, contents, obj.path, wasm.object_host_name, &ss, obj.must_link, gc_sections); |
| 2205 | 2223 | wasm.objects.appendAssumeCapacity(object); |
| 2206 | 2224 | } |
| 2207 | 2225 | } |
| ... | ... | @@ -2313,6 +2331,7 @@ pub fn updateNav(wasm: *Wasm, pt: Zcu.PerThread, nav_index: InternPool.Nav.Index |
| 2313 | 2331 | assert(!wasm.navs_exe.contains(nav_index)); |
| 2314 | 2332 | } |
| 2315 | 2333 | const name = try wasm.internString(ext.name.toSlice(ip)); |
| 2334 | if (ext.lib_name.toSlice(ip)) |ext_name| _ = try wasm.internString(ext_name); |
| 2316 | 2335 | try wasm.imports.ensureUnusedCapacity(gpa, 1); |
| 2317 | 2336 | if (ip.isFunctionType(nav.typeOf(ip))) { |
| 2318 | 2337 | try wasm.function_imports.ensureUnusedCapacity(gpa, 1); |