@@ -2318,27 +2318,25 @@ function Runtime() {
2318
2318
toNative = identityTransform ;
2319
2319
}
2320
2320
2321
+ const [ totalSize , fieldOffsets ] = fieldTypes . reduce ( function ( result , t ) {
2322
+ const [ previousOffset , offsets ] = result ;
2323
+
2324
+ const { size} = t ;
2325
+ const offset = align ( previousOffset , size ) ;
2326
+ offsets . push ( offset ) ;
2327
+
2328
+ return [ offset + size , offsets ] ;
2329
+ } , [ 0 , [ ] ] ) ;
2330
+
2321
2331
return {
2322
- type : fieldTypes . map ( function ( t ) {
2323
- return t . type ;
2324
- } ) ,
2325
- size : fieldTypes . reduce ( function ( totalSize , t ) {
2326
- return totalSize + t . size ;
2327
- } , 0 ) ,
2332
+ type : fieldTypes . map ( t => t . type ) ,
2333
+ size : totalSize ,
2328
2334
read : function ( address ) {
2329
- let source = address ;
2330
- return fieldTypes . map ( ( type , index ) => {
2331
- const result = type . read ( source ) ;
2332
- source = source . add ( type . size ) ;
2333
- return result ;
2334
- } ) ;
2335
+ return fieldTypes . map ( ( type , index ) => type . read ( address . add ( fieldOffsets [ index ] ) ) ) ;
2335
2336
} ,
2336
2337
write : function ( address , values ) {
2337
- let target = address ;
2338
2338
values . forEach ( ( value , index ) => {
2339
- const type = fieldTypes [ index ] ;
2340
- type . write ( target , value ) ;
2341
- target = target . add ( type . size ) ;
2339
+ fieldTypes [ index ] . write ( address . add ( fieldOffsets [ index ] ) , value ) ;
2342
2340
} ) ;
2343
2341
} ,
2344
2342
fromNative : fromNative ,
@@ -2545,6 +2543,11 @@ function Runtime() {
2545
2543
function identityTransform ( v ) {
2546
2544
return v ;
2547
2545
}
2546
+
2547
+ function align ( value , boundary ) {
2548
+ const remainder = value % boundary ;
2549
+ return ( remainder === 0 ) ? value : value + ( boundary - remainder ) ;
2550
+ }
2548
2551
}
2549
2552
2550
2553
let _api = null ;
0 commit comments