aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFraser Adams <fraser.adams@blueyonder.co.uk>2014-04-24 08:41:41 +0100
committerFraser Adams <fraser.adams@blueyonder.co.uk>2014-04-24 08:41:41 +0100
commite59333c486a97132698a1232ffdb1f5ae048d2b6 (patch)
treed068317527d5bfe435348172f6f2a76cfc0dfb18
parent55ba58508e65f70aed38ec57b78b46a83ecf9433 (diff)
Committing the actual change now
-rwxr-xr-xemscripten.py16
-rw-r--r--src/runtime.js6
-rw-r--r--tests/test_core.py22
3 files changed, 41 insertions, 3 deletions
diff --git a/emscripten.py b/emscripten.py
index c8122cb9..13492ba8 100755
--- a/emscripten.py
+++ b/emscripten.py
@@ -477,7 +477,7 @@ def emscript(infile, settings, outfile, libraries=[], compiler_engine=None,
if '_rand' in exported_implemented_functions or '_srand' in exported_implemented_functions:
basic_vars += ['___rand_seed']
- asm_runtime_funcs = ['stackAlloc', 'stackSave', 'stackRestore', 'setThrew'] + ['setTempRet%d' % i for i in range(10)]
+ asm_runtime_funcs = ['stackAlloc', 'stackSave', 'stackRestore', 'setThrew'] + ['setTempRet%d' % i for i in range(10)] + ['getTempRet%d' % i for i in range(10)]
# function tables
function_tables = ['dynCall_' + table for table in last_forwarded_json['Functions']['tables']]
function_tables_impls = []
@@ -632,6 +632,10 @@ function setTempRet%d(value) {
value = value|0;
tempRet%d = value;
}
+''' % (i, i) for i in range(10)]) + ''.join(['''
+function getTempRet%d() {
+ return tempRet%d|0;
+}
''' % (i, i) for i in range(10)])] + [PostSets.js + '\n'] + funcs_js + ['''
%s
@@ -647,6 +651,7 @@ function setTempRet%d(value) {
Runtime.stackAlloc = function(size) { return asm['stackAlloc'](size) };
Runtime.stackSave = function() { return asm['stackSave']() };
Runtime.stackRestore = function(top) { asm['stackRestore'](top) };
+Runtime.getTempRet0 = asm['getTempRet0'];
''')
# Set function table masks
@@ -1056,7 +1061,7 @@ def emscript_fast(infile, settings, outfile, libraries=[], compiler_engine=None,
if '_rand' in exported_implemented_functions or '_srand' in exported_implemented_functions:
basic_vars += ['___rand_seed']
- asm_runtime_funcs = ['stackAlloc', 'stackSave', 'stackRestore', 'setThrew'] + ['setTempRet%d' % i for i in range(10)]
+ asm_runtime_funcs = ['stackAlloc', 'stackSave', 'stackRestore', 'setThrew'] + ['setTempRet%d' % i for i in range(10)] + ['getTempRet%d' % i for i in range(10)]
# function tables
function_tables = ['dynCall_' + table for table in last_forwarded_json['Functions']['tables']]
function_tables_impls = []
@@ -1202,7 +1207,7 @@ function copyTempDouble(ptr) {
HEAP8[tempDoublePtr+3|0] = HEAP8[ptr+3|0];
HEAP8[tempDoublePtr+4|0] = HEAP8[ptr+4|0];
HEAP8[tempDoublePtr+5|0] = HEAP8[ptr+5|0];
- HEAP8[tempDoublePtr+6|0] = HEAP8[ptr+6|0];
+ HEAP8[tempDoublePtr+6|0] = HEAP8[ptr+6|0];f
HEAP8[tempDoublePtr+7|0] = HEAP8[ptr+7|0];
}
''' + ''.join(['''
@@ -1210,6 +1215,10 @@ function setTempRet%d(value) {
value = value|0;
tempRet%d = value;
}
+''' % (i, i) for i in range(10)]) + ''.join(['''
+function getTempRet%d() {
+ return tempRet%d|0;
+}
''' % (i, i) for i in range(10)])] + funcs_js + ['''
%s
@@ -1225,6 +1234,7 @@ function setTempRet%d(value) {
Runtime.stackAlloc = function(size) { return asm['stackAlloc'](size) };
Runtime.stackSave = function() { return asm['stackSave']() };
Runtime.stackRestore = function(top) { asm['stackRestore'](top) };
+Runtime.getTempRet0 = asm['getTempRet0'];
''')
# Set function table masks
diff --git a/src/runtime.js b/src/runtime.js
index 63610d3b..3974295d 100644
--- a/src/runtime.js
+++ b/src/runtime.js
@@ -96,6 +96,12 @@ function unInline(name_, params) {
}
var Runtime = {
+ // When a 64 bit long is returned from a compiled function the least significant
+ // 32 bit word is passed in the return value, but the most significant 32 bit
+ // word is placed in tempRet0. This provides an accessor for that value.
+ getTempRet0: function() {
+ return tempRet0;
+ },
stackSave: function() {
return STACKTOP;
},
diff --git a/tests/test_core.py b/tests/test_core.py
index 1b116c4a..97462cf9 100644
--- a/tests/test_core.py
+++ b/tests/test_core.py
@@ -6499,6 +6499,28 @@ def process(filename):
if self.emcc_args is None: return self.skip('needs emcc')
self.do_run_from_file(path_from_root('tests', 'test_locale.c'), path_from_root('tests', 'test_locale.out'))
+ def test_64bit_return_value(self):
+ # This test checks that the most significant 32 bits of a 64 bit long are correctly made available
+ # to native JavaScript applications that wish to interact with compiled code returning 64 bit longs.
+ # The MS 32 bits should be available in Runtime.getTempRet0() even when compiled with -O2 --closure 1
+ # Run with ./runner.py test_64bit_return_value
+
+ # Compile test.c and wrap it in a native JavaScript binding so we can call our compiled function from JS.
+ Popen([PYTHON, EMCC, path_from_root('tests', 'return64bit', 'test.c'), '--pre-js', path_from_root('tests', 'return64bit', 'testbindstart.js'), '--pre-js', path_from_root('tests', 'return64bit', 'testbind.js'), '--post-js', path_from_root('tests', 'return64bit', 'testbindend.js'), '-s', 'EXPORTED_FUNCTIONS=["_test"]', '-o', 'test.js', '-O2', '--closure', '1'], stdout=PIPE, stderr=PIPE).communicate()
+
+ # Simple test program to load the test.js binding library and call the binding to the
+ # C function returning the 64 bit long.
+ open(os.path.join(self.get_dir(), 'testrun.js'), 'w').write('''
+ var test = require("./test.js");
+ test.runtest();
+ ''')
+
+ # Run the test and confirm the output is as expected.
+ if NODE_JS in JS_ENGINES:
+ out = run_js('testrun.js', engine=NODE_JS, full_output=True)
+ assert "low = 5678" in out
+ assert "high = 1234" in out
+
# Generate tests for everything
def make_run(fullname, name=-1, compiler=-1, embetter=0, quantum_size=0,
typed_arrays=0, emcc_args=None, env=None):