Permalink
Browse files

struct: correct the field alignment logic

Fixes the failing tests and #1 :)
  • Loading branch information...
tjfontaine authored and TooTallNate committed Sep 26, 2012
1 parent 4ce0cfb commit da85dac1c8a98540813ff083836a967cacd303ff
Showing with 5 additions and 4 deletions.
  1. +5 −4 lib/struct.js
View
@@ -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

0 comments on commit da85dac

Please sign in to comment.