1
0
mirror of https://github.com/rkd77/elinks.git synced 2024-12-04 14:46:47 -05:00

[console] console.error and fixes in jsval_to_string

This commit is contained in:
Witold Filipczyk 2021-09-07 17:31:45 +02:00
parent b8ddbeef0f
commit 5737a4d345
4 changed files with 31 additions and 14 deletions

View File

@ -81,6 +81,7 @@ static int interpreter_count;
static INIT_LIST_OF(struct string_list_item, allowed_urls); static INIT_LIST_OF(struct string_list_item, allowed_urls);
static INIT_LIST_OF(struct string_list_item, disallowed_urls); static INIT_LIST_OF(struct string_list_item, disallowed_urls);
char *console_error_filename;
char *console_log_filename; char *console_log_filename;
char *local_storage_filename; char *local_storage_filename;
@ -542,13 +543,11 @@ init_ecmascript_module(struct module *module)
{ {
read_url_list(); read_url_list();
if (elinks_home) {
/* ecmascript console log */ /* ecmascript console log */
if (elinks_home) {
console_log_filename = straconcat(elinks_home, "/console.log", NULL); console_log_filename = straconcat(elinks_home, "/console.log", NULL);
} console_error_filename = straconcat(elinks_home, "/console.err", NULL);
/* ecmascript local storage db location */ /* ecmascript local storage db location */
if (elinks_home) {
local_storage_filename = straconcat(elinks_home, "/elinks_ls.db", NULL); local_storage_filename = straconcat(elinks_home, "/elinks_ls.db", NULL);
} }
} }

View File

@ -119,6 +119,7 @@ int get_ecmascript_enable(struct ecmascript_interpreter *interpreter);
void check_for_rerender(struct ecmascript_interpreter *interpreter, const char* text); void check_for_rerender(struct ecmascript_interpreter *interpreter, const char* text);
extern char *console_error_filename;
extern char *console_log_filename; extern char *console_log_filename;
extern char *local_storage_filename; extern char *local_storage_filename;

View File

@ -3,6 +3,7 @@
#include <jsapi.h> #include <jsapi.h>
#include <jsfriendapi.h> #include <jsfriendapi.h>
#include <js/Conversions.h>
#include "util/string.h" #include "util/string.h"
@ -47,8 +48,7 @@ static inline char *
jsval_to_string(JSContext *ctx, JS::HandleValue hvp) jsval_to_string(JSContext *ctx, JS::HandleValue hvp)
{ {
/* Memory must be freed in caller */ /* Memory must be freed in caller */
JSString *st = JS::ToString(ctx, hvp);
JSString *st = hvp.toString();
JS::RootedString rst(ctx, st); JS::RootedString rst(ctx, st);
JS::UniqueChars utf8chars = JS_EncodeStringToUTF8(ctx, rst); JS::UniqueChars utf8chars = JS_EncodeStringToUTF8(ctx, rst);

View File

@ -73,23 +73,22 @@ console_get_property(JSContext *ctx, JS::HandleObject hobj, JS::HandleId hid, JS
return true; return true;
} }
static bool console_error(JSContext *ctx, unsigned int argc, JS::Value *vp);
static bool console_log(JSContext *ctx, unsigned int argc, JS::Value *vp); static bool console_log(JSContext *ctx, unsigned int argc, JS::Value *vp);
const spidermonkeyFunctionSpec console_funcs[] = { const spidermonkeyFunctionSpec console_funcs[] = {
{ "log", console_log, 2 }, { "log", console_log, 1 },
{ "error", console_error, 1 },
{ NULL } { NULL }
}; };
static bool static bool
console_log(JSContext *ctx, unsigned int argc, JS::Value *vp) console_log_common(JSContext *ctx, unsigned int argc, JS::Value *vp, const char *log_filename)
{ {
#ifdef ECMASCRIPT_DEBUG
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
#endif
struct ecmascript_interpreter *interpreter = JS_GetContextPrivate(ctx); struct ecmascript_interpreter *interpreter = JS_GetContextPrivate(ctx);
JS::CallArgs args = CallArgsFromVp(argc, vp); JS::CallArgs args = CallArgsFromVp(argc, vp);
if (argc != 1 || !console_log_filename) if (argc != 1 || !log_filename)
{ {
args.rval().setBoolean(false); args.rval().setBoolean(false);
return(true); return(true);
@ -99,7 +98,7 @@ console_log(JSContext *ctx, unsigned int argc, JS::Value *vp)
{ {
unsigned char *key = jsval_to_string(ctx, args[0]); unsigned char *key = jsval_to_string(ctx, args[0]);
FILE *f = fopen(console_log_filename, "a"); FILE *f = fopen(log_filename, "a");
if (f) if (f)
{ {
@ -112,3 +111,21 @@ console_log(JSContext *ctx, unsigned int argc, JS::Value *vp)
args.rval().setBoolean(true); args.rval().setBoolean(true);
return(true); return(true);
} }
static bool
console_log(JSContext *ctx, unsigned int argc, JS::Value *vp)
{
#ifdef ECMASCRIPT_DEBUG
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
#endif
return console_log_common(ctx, argc, vp, console_log_filename);
}
static bool
console_error(JSContext *ctx, unsigned int argc, JS::Value *vp)
{
#ifdef ECMASCRIPT_DEBUG
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
#endif
return console_log_common(ctx, argc, vp, console_error_filename);
}