From 5abf13c4a2d347fa68859c519393156b9994bcff Mon Sep 17 00:00:00 2001 From: Eric House Date: Tue, 16 Feb 2021 15:08:58 -0800 Subject: [PATCH] new files to make EM_JS stuff sharable --- xwords4/wasm/Makefile | 2 +- xwords4/wasm/wasmasm.c | 40 ++++++++++++++++++++++++++++++++++++++++ xwords4/wasm/wasmasm.h | 26 ++++++++++++++++++++++++++ xwords4/wasm/wasmdutil.c | 37 +------------------------------------ 4 files changed, 68 insertions(+), 37 deletions(-) create mode 100644 xwords4/wasm/wasmasm.c create mode 100644 xwords4/wasm/wasmasm.h diff --git a/xwords4/wasm/Makefile b/xwords4/wasm/Makefile index 990f6af7a..3575489f7 100644 --- a/xwords4/wasm/Makefile +++ b/xwords4/wasm/Makefile @@ -20,7 +20,7 @@ include ../common/config.mk DEBS = cmake -INPUTS = main.c wasmdict.c wasmutls.c wasmdraw.c wasmutil.c wasmdutil.c ${COMMONSRC} +INPUTS = main.c wasmdict.c wasmutls.c wasmdraw.c wasmutil.c wasmdutil.c wasmasm.c ${COMMONSRC} DEFINES += -DXWFEATURE_CHAT DEFINES += -DXWFEATURE_BONUSALL diff --git a/xwords4/wasm/wasmasm.c b/xwords4/wasm/wasmasm.c new file mode 100644 index 000000000..e7b40a582 --- /dev/null +++ b/xwords4/wasm/wasmasm.c @@ -0,0 +1,40 @@ +/* -*- 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 + +#include "wasmasm.h" + +EM_JS(const char*, get_stored_value, (const char* key), { + 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); + }); diff --git a/xwords4/wasm/wasmasm.h b/xwords4/wasm/wasmasm.h new file mode 100644 index 000000000..c9e8ae37b --- /dev/null +++ b/xwords4/wasm/wasmasm.h @@ -0,0 +1,26 @@ +/* -*- compile-command: "cd ../wasm && make main.html -j3"; -*- */ +/* + * 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. + */ + +#ifndef _WASMASM_H_ +#define _WASMASM_H_ + +const char* get_stored_value( const char* key ); +void set_stored_value(const char* key, const char* val); + +#endif diff --git a/xwords4/wasm/wasmdutil.c b/xwords4/wasm/wasmdutil.c index de2158ab6..5c136ce06 100644 --- a/xwords4/wasm/wasmdutil.c +++ b/xwords4/wasm/wasmdutil.c @@ -25,6 +25,7 @@ #include "main.h" #include "dbgutil.h" #include "LocalizedStrIncludes.h" +#include "wasmasm.h" typedef struct _WasmDUtilCtxt { XW_DUtilCtxt super; @@ -162,42 +163,6 @@ base16Decode( uint8_t* decodeBuf, int len, const char* str ) XP_ASSERT( offset == len ); } -/* static void testBase16() */ -/* { */ -/* const XP_U8 testBuf[] = {0x03, 0x04, 0x78, 0xF8, 0x99 }; */ - -/* XP_UCHAR chars[(VSIZE(testBuf) * 2) + 1]; */ -/* base16Encode( testBuf, VSIZE(testBuf), chars, sizeof(chars) ); */ - -/* int len = XP_STRLEN( chars ); */ -/* XP_U8 decodeBuf[len / 2]; */ -/* base16Decode( decodeBuf, VSIZE(decodeBuf), chars ); */ - -/* XP_ASSERT( 0 == XP_MEMCMP( testBuf, decodeBuf, VSIZE(testBuf) ) ); */ -/* } */ - -EM_JS(const char*, get_stored_value, (const char* key), { - var result = null; - var jsKey = UTF8ToString(key); - var jsString = localStorage.getItem(jsKey); - if ( jsString != null ) { - var lengthBytes = lengthBytesUTF8(jsString)+1; - var 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, clear_stored, (), { */ -/* localStorage.clear(); */ -/* }); */ - static void wasm_dutil_loadPtr( XW_DUtilCtxt* duc, XWEnv xwe, const XP_UCHAR* key, const XP_UCHAR* keySuffix, void* data, XP_U16* lenp )