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