@@ -117,10 +117,12 @@ function Struct () {
117
117
StructType . toString = toString
118
118
StructType . fields = { }
119
119
120
+ var opt = ( arguments . length > 0 && arguments [ 1 ] ) ? arguments [ 1 ] : { } ;
120
121
// Setup the ref "type" interface. The constructor doubles as the "type" object
121
122
StructType . size = 0
122
123
StructType . alignment = 0
123
124
StructType . indirection = 1
125
+ StructType . isPacked = opt . packed ? Boolean ( opt . packed ) : false
124
126
StructType . get = get
125
127
StructType . set = set
126
128
@@ -245,7 +247,11 @@ function recalc (struct) {
245
247
if ( type . indirection > 1 ) {
246
248
alignment = ref . alignof . pointer
247
249
}
248
- struct . alignment = Math . max ( struct . alignment , alignment )
250
+ if ( struct . isPacked ) {
251
+ struct . alignment = Math . min ( struct . alignment || alignment , alignment )
252
+ } else {
253
+ struct . alignment = Math . max ( struct . alignment , alignment )
254
+ }
249
255
} )
250
256
251
257
// second loop through sets the `offset` property on each "field"
@@ -270,12 +276,14 @@ function recalc (struct) {
270
276
function addType ( type ) {
271
277
var offset = struct . size
272
278
var align = type . indirection === 1 ? type . alignment : ref . alignof . pointer
273
- var padding = ( align - ( offset % align ) ) % align
279
+ var padding = struct . isPacked ? 0 : ( align - ( offset % align ) ) % align
274
280
var size = type . indirection === 1 ? type . size : ref . sizeof . pointer
275
281
276
282
offset += padding
277
283
278
- assert . equal ( offset % align , 0 , "offset should align" )
284
+ if ( ! struct . isPacked ) {
285
+ assert . equal ( offset % align , 0 , "offset should align" )
286
+ }
279
287
280
288
// adjust the "size" of the struct type
281
289
struct . size = offset + size
0 commit comments