1
0
mirror of https://github.com/rkd77/elinks.git synced 2024-06-21 00:25:37 +00:00

[mujs] localStorage

This commit is contained in:
Witold Filipczyk 2022-08-07 17:47:21 +02:00
parent 8b4726a072
commit a59ca3fb26
5 changed files with 208 additions and 2 deletions

View File

@ -27,6 +27,7 @@
#include "ecmascript/mujs.h"
#include "ecmascript/mujs/console.h"
#include "ecmascript/mujs/history.h"
#include "ecmascript/mujs/localstorage.h"
#include "ecmascript/mujs/navigator.h"
#include "ecmascript/mujs/screen.h"
#include "ecmascript/mujs/unibar.h"
@ -86,6 +87,7 @@ mujs_get_interpreter(struct ecmascript_interpreter *interpreter)
mjs_navigator_init(interpreter, J);
mjs_history_init(interpreter, J);
mjs_console_init(interpreter, J);
mjs_localstorage_init(interpreter, J);
return J;
#if 0

View File

@ -0,0 +1,194 @@
/* The quickjs localstorage object implementation. */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "elinks.h"
#include "bfu/dialog.h"
#include "cache/cache.h"
#include "config/home.h"
#include "cookies/cookies.h"
#include "dialogs/menu.h"
#include "dialogs/status.h"
#include "document/html/frames.h"
#include "document/document.h"
#include "document/forms.h"
#include "document/view.h"
#include "ecmascript/ecmascript.h"
#include "ecmascript/localstorage-db.h"
#include "ecmascript/mujs.h"
#include "ecmascript/mujs/localstorage.h"
#include "intl/libintl.h"
#include "main/select.h"
#include "osdep/newwin.h"
#include "osdep/sysname.h"
#include "protocol/http/http.h"
#include "protocol/uri.h"
#include "session/history.h"
#include "session/location.h"
#include "session/session.h"
#include "session/task.h"
#include "terminal/tab.h"
#include "terminal/terminal.h"
#include "util/conv.h"
#include "util/memory.h"
#include "util/string.h"
#include "viewer/text/draw.h"
#include "viewer/text/form.h"
#include "viewer/text/link.h"
#include "viewer/text/vs.h"
#include <time.h>
#include "document/renderer.h"
#include "document/refresh.h"
#include "terminal/screen.h"
/* IMPLEMENTS READ FROM STORAGE USING SQLITE DATABASE */
static char *
readFromStorage(const char *key)
{
char *val;
if (local_storage_ready==0)
{
db_prepare_structure(local_storage_filename);
local_storage_ready=1;
}
val = db_query_by_key(local_storage_filename, key);
//DBG("Read: %s %s %s",local_storage_filename, key, val);
return val;
}
static void
removeFromStorage(const char *key)
{
if (local_storage_ready==0)
{
db_prepare_structure(local_storage_filename);
local_storage_ready=1;
}
db_delete_from(local_storage_filename, key);
}
/* IMPLEMENTS SAVE TO STORAGE USING SQLITE DATABASE */
static void
saveToStorage(const char *key, const char *val)
{
if (local_storage_ready==0) {
db_prepare_structure(local_storage_filename);
local_storage_ready=1;
}
int rows_affected=0;
rows_affected=db_update_set(local_storage_filename, key, val);
if (rows_affected==0) {
rows_affected=db_insert_into(local_storage_filename, key, val);
}
// DBG(log, "UPD ROWS: %d KEY: %s VAL: %s",rows_affected,key,val);
}
static void
mjs_localstorage_getitem(js_State *J)
{
#ifdef ECMASCRIPT_DEBUG
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
#endif
const char *key = js_tostring(J, 1);
if (!key) {
js_pushnull(J);
return;
}
char *val = readFromStorage(key);
if (!val) {
js_pushnull(J);
return;
}
// TODO possible memleak
js_pushstring(J, val);
}
static void
mjs_localstorage_removeitem(js_State *J)
{
#ifdef ECMASCRIPT_DEBUG
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
#endif
const char *key = js_tostring(J, 1);
if (key) {
removeFromStorage(key);
}
js_pushundefined(J);
}
static void
mjs_localstorage_setitem(js_State *J)
{
#ifdef ECMASCRIPT_DEBUG
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
#endif
struct ecmascript_interpreter *interpreter = (struct ecmascript_interpreter *)js_touserdata(J, 0, "localStorage");
const char *key_str = js_tostring(J, 1);
const char *val_str = js_tostring(J, 2);
if (!key_str || !val_str)
{
js_pushundefined(J);
return;
}
saveToStorage(key_str, val_str);
#ifdef CONFIG_LEDS
set_led_value(interpreter->vs->doc_view->session->status.ecmascript_led, 'J');
#endif
js_pushundefined(J);
}
static void
mjs_localstorage_toString(js_State *J)
{
#ifdef ECMASCRIPT_DEBUG
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
#endif
js_pushstring(J, "[localstorage object]");
}
int
mjs_localstorage_init(struct ecmascript_interpreter *interpreter, js_State *J)
{
js_getglobal(J, "Object");
js_getproperty(J, -1, "prototype");
js_newuserdata(J, "localStorage", interpreter, NULL);
js_newcfunction(J, mjs_localstorage_getitem, "localStorage.prototype.getItem", 1);
js_defproperty(J, -2, "getItem", JS_DONTENUM);
js_newcfunction(J, mjs_localstorage_removeitem, "localStorage.prototype.removeItem", 1);
js_defproperty(J, -2, "removeItem", JS_DONTENUM);
js_newcfunction(J, mjs_localstorage_setitem, "localStorage.prototype.setItem", 2);
js_defproperty(J, -2, "setItem", JS_DONTENUM);
js_newcfunction(J, mjs_localstorage_toString, "localStorage.prototype.toString", 0);
js_defproperty(J, -2, "toString", JS_DONTENUM);
js_defglobal(J, "localStorage", JS_DONTENUM);
return 0;
}

View File

@ -0,0 +1,10 @@
#ifndef EL__ECMASCRIPT_MUJS_LOCALSTORAGE_H
#define EL__ECMASCRIPT_MUJS_LOCALSTORAGE_H
#include <mujs.h>
struct ecmascript_interpreter;
int mjs_localstorage_init(struct ecmascript_interpreter *interpreter, js_State *J);
#endif

View File

@ -1,3 +1,3 @@
#srcs += files('attr.cpp', 'attributes.cpp', 'collection.cpp', 'console.cpp', 'document.cpp', 'element.cpp', 'form.cpp', 'forms.cpp', 'heartbeat.cpp', 'history.cpp', 'implementation.cpp',
#'input.cpp', 'localstorage.cpp', 'location.cpp', 'navigator.cpp', 'nodelist.cpp', 'screen.cpp', 'unibar.cpp', 'window.cpp')
srcs += files('console.cpp', 'history.cpp', 'navigator.cpp', 'screen.cpp', 'unibar.cpp', 'window.cpp')
srcs += files('console.cpp', 'history.cpp', 'localstorage.cpp', 'navigator.cpp', 'screen.cpp', 'unibar.cpp', 'window.cpp')

View File

@ -4,7 +4,7 @@
console.log("---= LOCAL STORAGE TEST =---");
console.log("|``````````````````````````|");
localStorage.setItem("foo","bar");
foo=localStorage.getItem("foo");
var foo=localStorage.getItem("foo");
console.log(". foo: "+foo);
localStorage.setItem("foo","off");
foo=localStorage.getItem("foo");