Skip to content

Commit 07797a9

Browse files
committed
getValue: Only cast BigInt to Number when loading a poiner type.
Fixes: #17322
1 parent e91ea21 commit 07797a9

File tree

6 files changed

+27
-16
lines changed

6 files changed

+27
-16
lines changed

src/library_getvalue.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ var LibraryMemOps = {
1212
* @param {string} type
1313
*/`,
1414
$setValue: function(ptr, value, type = 'i8') {
15-
if (type.endsWith('*')) type = '{{{ POINTER_WASM_TYPE }}}';
15+
if (type.endsWith('*')) type = '*';
1616
switch (type) {
1717
case 'i1': {{{ makeSetValue('ptr', '0', 'value', 'i1') }}}; break;
1818
case 'i8': {{{ makeSetValue('ptr', '0', 'value', 'i8') }}}; break;
@@ -21,6 +21,7 @@ var LibraryMemOps = {
2121
case 'i64': {{{ makeSetValue('ptr', '0', 'value', 'i64') }}}; break;
2222
case 'float': {{{ makeSetValue('ptr', '0', 'value', 'float') }}}; break;
2323
case 'double': {{{ makeSetValue('ptr', '0', 'value', 'double') }}}; break;
24+
case '*': {{{ makeSetValue('ptr', '0', 'value', '*') }}}; break;
2425
default: abort('invalid type for setValue: ' + type);
2526
}
2627
},
@@ -31,7 +32,7 @@ var LibraryMemOps = {
3132
* @param {string} type
3233
*/`,
3334
$getValue: function(ptr, type = 'i8') {
34-
if (type.endsWith('*')) type = '{{{ POINTER_WASM_TYPE }}}';
35+
if (type.endsWith('*')) type = '*';
3536
switch (type) {
3637
case 'i1': return {{{ makeGetValue('ptr', '0', 'i1') }}};
3738
case 'i8': return {{{ makeGetValue('ptr', '0', 'i8') }}};
@@ -40,6 +41,7 @@ var LibraryMemOps = {
4041
case 'i64': return {{{ makeGetValue('ptr', '0', 'i64') }}};
4142
case 'float': return {{{ makeGetValue('ptr', '0', 'float') }}};
4243
case 'double': return {{{ makeGetValue('ptr', '0', 'double') }}};
44+
case '*': return {{{ makeGetValue('ptr', '0', '*') }}};
4345
default: abort('invalid type for getValue: ' + type);
4446
}
4547
return null;

src/parseTools.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,7 @@ function makeGetValue(ptr, pos, type, noNeedFirst, unsigned, ignore, align) {
411411

412412
const slab = getHeapForType(type);
413413
let ret = slab + '[' + getHeapOffset(offset, type) + ']';
414-
if (slab.substr(slab.length - 2) == '64') {
414+
if (MEMORY64 && isPointerType(type)) {
415415
ret = `Number(${ret})`;
416416
}
417417
return ret;

tests/core/test_getValue_setValue.cpp

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,29 @@
33
// University of Illinois/NCSA Open Source License. Both these licenses can be
44
// found in the LICENSE file.
55

6-
#include<emscripten.h>
6+
#include <emscripten.h>
77

88
int main() {
99
char buffer_char[8] = { 'x' };
10-
void* buffer_ptr[] = { (void*)0x12345678 };
11-
#ifdef DIRECT
10+
int buffer_ptr[] = { 0x12345678, 42 };
1211
EM_ASM({
12+
#ifdef DIRECT
1313
out('i32: ' + getValue($0, 'i32'));
1414
setValue($0, 1234, 'i32');
1515
out('i32: ' + getValue($0, 'i32'));
16-
out('ptr: 0x' + getValue($1, '*').toString(16));
17-
}, buffer_char, buffer_ptr);
16+
i64 = getValue($1, 'i64');
17+
ptr = getValue($1, '*');
18+
out('i64: 0x' + i64.toString(16) + ' ' + typeof(i64));
19+
out('ptr: 0x' + ptr.toString(16) + ' ' + typeof(ptr));
1820
#else
19-
EM_ASM({
2021
out('i32: ' + getValue($0, 'i32'));
2122
Module['setValue']($0, 1234, 'i32');
2223
out('i32: ' + Module['getValue']($0, 'i32'));
23-
out('ptr: 0x' + Module['getValue']($1, 'i32').toString(16));
24-
}, buffer_char, buffer_ptr);
24+
i64 = Module['getValue']($1, 'i64');
25+
ptr = Module['getValue']($1, '*');
26+
out('i64: 0x' + i64.toString(16) + ' ' + typeof(i64));
27+
out('ptr: 0x' + ptr.toString(16) + ' ' + typeof(ptr));
2528
#endif
29+
}, buffer_char, buffer_ptr);
2630
}
2731

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
i32: 120
22
i32: 1234
3-
ptr: 0x12345678
3+
i64: 0x12345678 number
4+
ptr: 0x12345678 number
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
i32: 120
2+
i32: 1234
3+
i64: 0x2a12345678 bigint
4+
ptr: 0x2a12345678 number

tests/test_core.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7007,10 +7007,10 @@ def test_dyncall_specific(self, *args):
70077007

70087008
def test_getValue_setValue(self):
70097009
# these used to be exported, but no longer are by default
7010-
def test(output_prefix='', args=None, assert_returncode=0):
7011-
src = test_file('core/test_getValue_setValue.cpp')
7012-
expected = test_file('core/test_getValue_setValue' + output_prefix + '.out')
7013-
self.do_run_from_file(src, expected, assert_returncode=assert_returncode, emcc_args=args)
7010+
def test(out_suffix='', args=None, assert_returncode=0):
7011+
if not out_suffix and self.is_wasm64():
7012+
out_suffix = '64'
7013+
self.do_run_in_out_file_test('core/test_getValue_setValue.cpp', out_suffix=out_suffix, assert_returncode=assert_returncode, emcc_args=args)
70147014

70157015
# see that direct usage (not on module) works. we don't export, but the use
70167016
# keeps it alive through JSDCE

0 commit comments

Comments
 (0)