1
0
mirror of https://github.com/rkd77/elinks.git synced 2025-02-02 15:09:23 -05:00

[mujs] console.c

This commit is contained in:
Witold Filipczyk 2023-04-12 17:19:10 +02:00
parent 9a3ffecf65
commit e0ad0c64d6
5 changed files with 98 additions and 2 deletions

View File

@ -1,6 +1,6 @@
top_builddir=../../../..
include $(top_builddir)/Makefile.config
OBJS = attr.o attributes.o collection.o mapa.obj
OBJS = attr.o attributes.o collection.o console.o mapa.obj
include $(top_srcdir)/Makefile.lib

View File

@ -0,0 +1,86 @@
/* The MuJS console object implementation. */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "elinks.h"
#include "ecmascript/ecmascript.h"
#include "ecmascript/mujs.h"
#include "ecmascript/mujs/console.h"
#define DEBUG 0
static void
mjs_console_log_common(js_State *J, const char *str, const char *log_filename)
{
#ifdef ECMASCRIPT_DEBUG
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
#endif
struct ecmascript_interpreter *interpreter = (struct ecmascript_interpreter *)js_getcontext(J);
assert(interpreter);
if (log_filename && get_opt_bool("ecmascript.enable_console_log", NULL) && str)
{
FILE *f = fopen(log_filename, "a");
if (f)
{
fprintf(f, "%s\n", str);
fclose(f);
}
}
js_pushundefined(J);
}
static void
mjs_console_log(js_State *J)
{
#ifdef ECMASCRIPT_DEBUG
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
#endif
const char *str = js_tostring(J, 1);
mjs_console_log_common(J, str, console_log_filename);
}
static void
mjs_console_error(js_State *J)
{
#ifdef ECMASCRIPT_DEBUG
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
#endif
const char *str = js_tostring(J, 1);
mjs_console_log_common(J, str, console_error_filename);
}
static void
mjs_console_toString(js_State *J)
{
#ifdef ECMASCRIPT_DEBUG
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
#endif
js_pushstring(J, "[console object]");
}
int
mjs_console_init(js_State *J)
{
js_newobject(J);
{
addmethod(J, "console.log", mjs_console_log, 1);
addmethod(J, "console.error", mjs_console_error, 1);
addmethod(J, "console.toString", mjs_console_toString, 0);
}
js_defglobal(J, "console", JS_DONTENUM);
return 0;
}

View File

@ -1 +1 @@
srcs += files('attr.c', 'attributes.c', 'collection.c', 'mapa.cpp')
srcs += files('attr.c', 'attributes.c', 'collection.c', 'console.c', 'mapa.cpp')

View File

@ -30,6 +30,7 @@
#include "document/refresh.h"
#include "terminal/screen.h"
#ifndef CONFIG_LIBDOM
#define DEBUG 0
static void
@ -99,3 +100,4 @@ mjs_console_init(js_State *J)
return 0;
}
#endif

View File

@ -3,6 +3,14 @@
#include <mujs.h>
#ifdef __cplusplus
extern "C" {
#endif
int mjs_console_init(js_State *J);
#ifdef __cplusplus
}
#endif
#endif