xwords/xwords4/wasm/wasmasm.c

74 lines
2.3 KiB
C
Raw Normal View History

2021-02-16 15:08:58 -08:00
/* -*- compile-command: "cd ../wasm && make -j3 MEMDEBUG=TRUE install"; -*- */
/*
* Copyright 2021 by Eric House (xwords@eehouse.org). All rights reserved.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include <emscripten.h>
#include <string.h>
#include <stdlib.h>
2021-02-16 15:08:58 -08:00
#include "wasmasm.h"
#include "comtypes.h"
2021-02-16 15:08:58 -08:00
EM_JS(const char*, _get_stored_value, (const char* key), {
2021-02-16 15:08:58 -08:00
var result = null;
var jsKey = UTF8ToString(key);
var jsString = localStorage.getItem(jsKey);
if ( jsString != null ) {
var lengthBytes = lengthBytesUTF8(jsString)+1;
result = _malloc(lengthBytes);
stringToUTF8(jsString, result, lengthBytes);
}
return result;
});
EM_JS(void, set_stored_value, (const char* key, const char* val), {
var jsKey = UTF8ToString(key);
var jsVal = UTF8ToString(val);
var jsString = localStorage.setItem(jsKey, jsVal);
});
EM_JS(void, remove_stored_value, (const char* key), {
var jsKey = UTF8ToString(key);
var jsString = localStorage.removeItem(jsKey);
});
EM_JS(bool, have_stored_value, (const char* key), {
let jsKey = UTF8ToString(key);
let jsVal = localStorage.getItem(jsKey);
let result = null !== jsVal;
return result;
});
bool
get_stored_value( const char* key, char out[], size_t* len )
{
bool success = false;
const char* tmp = _get_stored_value( key );
if ( !!tmp ) {
size_t slen = 1 + strlen(tmp);
if ( !!out && slen <= *len ) {
memcpy( out, tmp, slen );
success = true;
}
*len = slen;
free( (void*)tmp );
}
return success;
}