mirror of
git://git.tartarus.org/simon/puzzles.git
synced 2025-04-21 16:05:44 -07:00
js: pass preferences file from JS to C on the heap, not the stack
The C stack used by Emscripten is quite small, so passing more than a few klilobytes of data on it tends to cause an overflow. Current versions of puzzles will only generate tiny preferences files, but this might change in future and in any case Puzzles shouldn't crash just because the preferences in local storage have got corrupted. To fix this, we now have JavaScript allocate a suitable amount of C heap memory using malloc() and stick the preferences file in there. This could plausibly fail if the preferences file were really big, but that's unlikely since browsers generally limit an origin to about 5 MB of local storage. In any case, if malloc() does fail, we'll just ignore the preferences file, which is probably the right thing to do.
This commit is contained in:
@ -49,6 +49,9 @@ set(emcc_export_list
|
|||||||
_rescale_puzzle
|
_rescale_puzzle
|
||||||
# Callback for loading user preferences
|
# Callback for loading user preferences
|
||||||
_prefs_load_callback
|
_prefs_load_callback
|
||||||
|
# Functions for allocating and freeing C memory
|
||||||
|
_malloc
|
||||||
|
_free
|
||||||
# Main program, run at initialisation time
|
# Main program, run at initialisation time
|
||||||
_main)
|
_main)
|
||||||
|
|
||||||
|
@ -827,7 +827,13 @@ mergeInto(LibraryManager.library, {
|
|||||||
var prefsdata =
|
var prefsdata =
|
||||||
localStorage.getItem(location.pathname + " preferences");
|
localStorage.getItem(location.pathname + " preferences");
|
||||||
if (prefsdata !== undefined && prefsdata !== null) {
|
if (prefsdata !== undefined && prefsdata !== null) {
|
||||||
prefs_load_callback(me, prefsdata);
|
var lenbytes = lengthBytesUTF8(prefsdata) + 1;
|
||||||
|
var dest = _malloc(lenbytes);
|
||||||
|
if (dest != 0) {
|
||||||
|
stringToUTF8(prefsdata, dest, lenbytes);
|
||||||
|
prefs_load_callback(me, dest);
|
||||||
|
_free(dest);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
// Log the error but otherwise pretend the settings were
|
// Log the error but otherwise pretend the settings were
|
||||||
|
@ -693,7 +693,7 @@ function initPuzzle() {
|
|||||||
['number','number']);
|
['number','number']);
|
||||||
timer_callback = Module.cwrap('timer_callback', 'void', ['number']);
|
timer_callback = Module.cwrap('timer_callback', 'void', ['number']);
|
||||||
prefs_load_callback = Module.cwrap('prefs_load_callback', 'void',
|
prefs_load_callback = Module.cwrap('prefs_load_callback', 'void',
|
||||||
['number','string']);
|
['number','number']);
|
||||||
|
|
||||||
if (resizable_div !== null) {
|
if (resizable_div !== null) {
|
||||||
var resize_handle = document.getElementById("resizehandle");
|
var resize_handle = document.getElementById("resizehandle");
|
||||||
|
Reference in New Issue
Block a user