Skip to content

Commit

Permalink
struct: correct the field alignment logic
Browse files Browse the repository at this point in the history
Fixes the failing tests and TooTallNate#1 :)
  • Loading branch information
tjfontaine authored and TooTallNate committed Sep 26, 2012
1 parent 4ce0cfb commit da85dac
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions lib/struct.js
Original file line number Diff line number Diff line change
Expand Up @@ -272,12 +272,13 @@ function recalc (struct) {

function addType (type) {
var offset = struct.size
var left = offset % struct.alignment
var align = type.indirection === 1 ? type.alignment : ref.alignof.pointer
var padding = (align - (offset % align)) % align
var size = type.indirection === 1 ? type.size : ref.sizeof.pointer

if (size > left) {
offset += left
}
offset += padding

assert.equal(offset % align, 0, "offset should align")

// adjust the "size" of the struct type
struct.size = offset + size
Expand Down

0 comments on commit da85dac

Please sign in to comment.