From da85dac1c8a98540813ff083836a967cacd303ff Mon Sep 17 00:00:00 2001 From: Timothy J Fontaine Date: Wed, 26 Sep 2012 13:18:32 -0700 Subject: [PATCH] struct: correct the field alignment logic Fixes the failing tests and #1 :) --- lib/struct.js | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/lib/struct.js b/lib/struct.js index c72d24d..1108941 100644 --- a/lib/struct.js +++ b/lib/struct.js @@ -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