Skip to content

Commit

Permalink
Address review and fix convertPtrToIdx on non-byte shifts
Browse files Browse the repository at this point in the history
  • Loading branch information
juj committed Jun 14, 2023
1 parent 8353f65 commit 8809d51
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 18 deletions.
4 changes: 1 addition & 3 deletions src/embind/embind.js
Original file line number Diff line number Diff line change
Expand Up @@ -718,9 +718,7 @@ var LibraryEmbind = {
// assumes 4-byte alignment
var base = _malloc({{{ POINTER_SIZE }}} + length + 1);
var ptr = base + {{{ POINTER_SIZE }}};
#if CAN_ADDRESS_2GB
ptr >>>= 0;
#endif
{{{ convertPtrToIdx('ptr') }}};
{{{ makeSetValue('base', '0', 'length', SIZE_TYPE) }}};
if (stdStringIsUTF8 && valueIsOfTypeString) {
stringToUTF8(value, ptr, length + 1);
Expand Down
11 changes: 2 additions & 9 deletions src/parseTools.js
Original file line number Diff line number Diff line change
Expand Up @@ -876,13 +876,6 @@ function isIntLiteral(x) {
return /^\d+$/.test(x);
}

// Returns the given Number (either literal or a variable) casted to a BigInt.
function toBigInt(number) {
// Code size optimization: if 'number' is a literal, use the short literal syntax '42n'.
// Otherwise must use a longer cast to BigInt().
return isIntLiteral(number) ? `${number}n` : `BigInt(${number})`;
}

// Generates an expression that tests whether a pointer (either a BigInt or a signed int32 JS Number) is aligned to the given byte multiple.
function isPtrAligned(ptr, alignment) {
return `(${parensGuard(ptr)} % ${idxToPtr(alignment)} == 0)`;
Expand Down Expand Up @@ -928,8 +921,8 @@ function ptrToIdx(ptr, accessShift = 0) {
// {{{ convertPtrToIdx(ptr, as) }}} is equal to 'ptr = {{{ ptrToIdx(ptr, as) }}}, i.e. it assigns
// in-place. This can be used to avoid generating redundant "ptr = ptr;" assignments in JS code.
function convertPtrToIdx(ptr, accessShift = 0) {
if (MEMORY64 || MAXIMUM_MEMORY > MAX_MEMORY_2GB) return `${ptr} = ${ptrToIdx(ptr, accessShift)}`;
return ''; // No conversion needed in 2GB mode.
if (MEMORY64 || MAXIMUM_MEMORY > MAX_MEMORY_2GB || accessShift != 0) return `${ptr} = ${ptrToIdx(ptr, accessShift)}`;
return ''; // No conversion of byte addresses needed in 2GB mode.
}

// Given an index (unsigned Number) to one of the HEAP* arrays, converts it to an appropriate Wasm pointer
Expand Down
8 changes: 2 additions & 6 deletions src/runtime_safe_heap.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@ var SAFE_HEAP_COUNTER = 0;

/** @param {number|boolean=} isFloat */
function SAFE_HEAP_STORE(dest, value, bytes, isFloat) {
#if CAN_ADDRESS_2GB
dest >>>= 0;
#endif
{{{ convertPtrToIdx('dest') }}};
#if SAFE_HEAP_LOG
out('SAFE_HEAP store: ' + [dest, value, bytes, isFloat, SAFE_HEAP_COUNTER++]);
#endif
Expand Down Expand Up @@ -56,9 +54,7 @@ function SAFE_HEAP_STORE_D(dest, value, bytes) {

/** @param {number|boolean=} isFloat */
function SAFE_HEAP_LOAD(dest, bytes, unsigned, isFloat) {
#if CAN_ADDRESS_2GB
dest >>>= 0;
#endif
{{{ convertPtrToIdx('dest') }}};
if (dest <= 0) abort(`segmentation fault loading ${bytes} bytes from address ${dest}`);
#if SAFE_HEAP == 1
if (dest % bytes !== 0) abort(`alignment error loading from address ${dest}, which was expected to be aligned to a multiple of ${bytes}`);
Expand Down

0 comments on commit 8809d51

Please sign in to comment.