| ... | ... | @@ -59,34 +59,37 @@ pub fn openPath(allocator: *Allocator, dir: fs.Dir, sub_path: []const u8, option |
| 59 | 59 | |
| 60 | 60 | try file.writeAll(&(spec.magic ++ spec.version)); |
| 61 | 61 | |
| 62 | | wasm.base = .{ |
| 63 | | .tag = .wasm, |
| 64 | | .options = options, |
| 65 | | .file = file, |
| 66 | | .allocator = allocator, |
| 67 | | }; |
| 68 | | |
| 69 | 62 | // TODO: this should vary depending on the section and be less arbitrary |
| 70 | 63 | const size = 1024; |
| 71 | 64 | const offset = @sizeOf(@TypeOf(spec.magic ++ spec.version)); |
| 72 | 65 | |
| 73 | | wasm.types = try Types.init(file, offset, size); |
| 74 | | wasm.funcs = try Funcs.init(file, offset + size, size, offset + 3 * size, size); |
| 75 | | wasm.exports = try Exports.init(file, offset + 2 * size, size); |
| 76 | | try file.setEndPos(offset + 4 * size); |
| 77 | | |
| 78 | | wasm.sections = [_]*Section{ |
| 79 | | &wasm.types.typesec.section, |
| 80 | | &wasm.funcs.funcsec, |
| 81 | | &wasm.exports.exportsec, |
| 82 | | &wasm.funcs.codesec.section, |
| 66 | wasm.* = .{ |
| 67 | .base = .{ |
| 68 | .tag = .wasm, |
| 69 | .options = options, |
| 70 | .file = file, |
| 71 | .allocator = allocator, |
| 72 | }, |
| 73 | |
| 74 | .types = try Types.init(file, offset, size), |
| 75 | .funcs = try Funcs.init(file, offset + size, size, offset + 3 * size, size), |
| 76 | .exports = try Exports.init(file, offset + 2 * size, size), |
| 77 | |
| 78 | // These must be ordered as they will appear in the output file |
| 79 | .sections = [_]*Section{ |
| 80 | &wasm.types.typesec.section, |
| 81 | &wasm.funcs.funcsec, |
| 82 | &wasm.exports.exportsec, |
| 83 | &wasm.funcs.codesec.section, |
| 84 | }, |
| 83 | 85 | }; |
| 84 | 86 | |
| 87 | try file.setEndPos(offset + 4 * size); |
| 88 | |
| 85 | 89 | return &wasm.base; |
| 86 | 90 | } |
| 87 | 91 | |
| 88 | 92 | pub fn deinit(self: *Wasm) void { |
| 89 | | if (self.base.file) |f| f.close(); |
| 90 | 93 | self.types.deinit(); |
| 91 | 94 | self.funcs.deinit(); |
| 92 | 95 | } |
| ... | ... | @@ -112,7 +115,8 @@ pub fn updateDecl(self: *Wasm, module: *Module, decl: *Module.Decl) !void { |
| 112 | 115 | |
| 113 | 116 | decl.fn_link.wasm = .{ .typeidx = typeidx, .funcidx = funcidx }; |
| 114 | 117 | |
| 115 | | try self.exports.writeAll(module); |
| 118 | // TODO: we should be more smart and set this only when needed |
| 119 | self.exports.dirty = true; |
| 116 | 120 | } |
| 117 | 121 | |
| 118 | 122 | pub fn updateDeclExports( |
| ... | ... | @@ -121,11 +125,7 @@ pub fn updateDeclExports( |
| 121 | 125 | decl: *const Module.Decl, |
| 122 | 126 | exports: []const *Module.Export, |
| 123 | 127 | ) !void { |
| 124 | | // TODO: updateDeclExports() may currently be called before updateDecl, |
| 125 | | // presumably due to a bug. For now just rely on the following call |
| 126 | | // being made in updateDecl(). |
| 127 | | |
| 128 | | //try self.exports.writeAll(module); |
| 128 | self.exports.dirty = true; |
| 129 | 129 | } |
| 130 | 130 | |
| 131 | 131 | pub fn freeDecl(self: *Wasm, decl: *Module.Decl) void { |
| ... | ... | @@ -138,7 +138,9 @@ pub fn freeDecl(self: *Wasm, decl: *Module.Decl) void { |
| 138 | 138 | } |
| 139 | 139 | } |
| 140 | 140 | |
| 141 | | pub fn flush(self: *Wasm) !void {} |
| 141 | pub fn flush(self: *Wasm, module: *Module) !void { |
| 142 | if (self.exports.dirty) try self.exports.writeAll(module); |
| 143 | } |
| 142 | 144 | |
| 143 | 145 | /// This struct describes the location of a named section + custom section |
| 144 | 146 | /// padding in the output file. This is all the data we need to allow for |
| ... | ... | @@ -373,11 +375,14 @@ const Exports = struct { |
| 373 | 375 | /// Size in bytes of the contents of the section. Does not include |
| 374 | 376 | /// the "header" containing the section id and this value. |
| 375 | 377 | contents_size: u32, |
| 378 | /// If this is true, then exports will be rewritten on flush() |
| 379 | dirty: bool, |
| 376 | 380 | |
| 377 | 381 | fn init(file: fs.File, offset: u64, initial_size: u64) !Exports { |
| 378 | 382 | return Exports{ |
| 379 | 383 | .exportsec = (try VecSection.init(spec.exports_id, file, offset, initial_size)).section, |
| 380 | 384 | .contents_size = 5, |
| 385 | .dirty = false, |
| 381 | 386 | }; |
| 382 | 387 | } |
| 383 | 388 | |
| ... | ... | @@ -410,6 +415,8 @@ const Exports = struct { |
| 410 | 415 | |
| 411 | 416 | for (module.decl_exports.entries.items) |entry| |
| 412 | 417 | for (entry.value) |e| try writeExport(writer, e); |
| 418 | |
| 419 | self.dirty = false; |
| 413 | 420 | } |
| 414 | 421 | |
| 415 | 422 | /// Return the total number of bytes an export will take. |