Skip to content

Commit 403dafe

Browse files
committed
Refactor makeHEAPView to use getHeapOffset. NFC
The code for calculating the offsets was being duplicated here.
1 parent 4b9a5f7 commit 403dafe

File tree

1 file changed

+26
-18
lines changed

1 file changed

+26
-18
lines changed

src/parseTools.js

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -297,14 +297,17 @@ function getNativeTypeSize(type) {
297297

298298
function getHeapOffset(offset, type) {
299299
const sz = getNativeTypeSize(type);
300-
const shifts = Math.log(sz) / Math.LN2;
300+
if (sz == 1) {
301+
return offset;
302+
}
301303
if (MEMORY64 == 1) {
302-
return `((${offset})/${2 ** shifts})`;
303-
} else if (CAN_ADDRESS_2GB) {
304+
return `((${offset})/${sz})`;
305+
}
306+
const shifts = Math.log(sz) / Math.LN2;
307+
if (CAN_ADDRESS_2GB) {
304308
return `((${offset})>>>${shifts})`;
305-
} else {
306-
return `((${offset})>>${shifts})`;
307309
}
310+
return `((${offset})>>${shifts})`;
308311
}
309312

310313
function ensureDot(value) {
@@ -427,19 +430,24 @@ function makeSetValueImpl(ptr, pos, value, type) {
427430
}
428431

429432
function makeHEAPView(which, start, end) {
430-
const size = parseInt(which.replace('U', '').replace('F', '')) / 8;
431-
const shift = Math.log2(size);
432-
let mod = '';
433-
if (size != 1) {
434-
if (MEMORY64) {
435-
mod = '/' + size;
436-
} else if (CAN_ADDRESS_2GB) {
437-
mod = '>>>' + shift;
438-
} else {
439-
mod = '>>' + shift;
440-
}
441-
}
442-
return `HEAP${which}.subarray((${start})${mod}, (${end})${mod})`;
433+
// The makeHEAPView, for legacuy reason takes a heap suffix
434+
// rather than heap "type" that used by other APIs here.
435+
type = {
436+
'8': 'i8',
437+
'U8': 'u8',
438+
'16': 'i16',
439+
'U16': 'u16',
440+
'32': 'i32',
441+
'U32': 'u32',
442+
'64': 'i64',
443+
'U64': 'u64',
444+
'F32': 'float',
445+
'F64': 'double',
446+
}[which];
447+
const heap = getHeapForType(type);
448+
start = getHeapOffset(start, type);
449+
end = getHeapOffset(end, type);
450+
return `${heap}.subarray((${start}), ${end})`;
443451
}
444452

445453
// Given two values and an operation, returns the result of that operation.

0 commit comments

Comments
 (0)