netsurf/content/handlers/javascript/duktape/Window.bnd

145 lines
3.1 KiB
Plaintext
Raw Normal View History

2015-08-09 14:26:41 +03:00
/* Window binding for browser using duktape and libdom
*
* Copyright 2015 Vincent Sanders <vince@netsurf-browser.org>
*
* This file is part of NetSurf, http://www.netsurf-browser.org/
*
* Released under the terms of the MIT License,
* http://www.opensource.org/licenses/mit-license
*/
class Window {
private struct browser_window * win;
private struct html_content * htmlc;
2015-08-09 14:26:41 +03:00
prologue %{
#include "utils/nsurl.h"
#include "netsurf/browser_window.h"
#include "content/hlcache.h"
#include "html/html.h"
#include "html/html_internal.h"
2015-08-09 14:26:41 +03:00
%};
};
2015-08-09 14:26:41 +03:00
init Window(struct browser_window *win, struct html_content *htmlc)
2015-08-09 14:26:41 +03:00
%{
/* element window */
priv->win = win;
priv->htmlc = htmlc;
2017-09-04 01:19:11 +03:00
NSLOG(netsurf, INFO, "win=%p htmlc=%p", priv->win, priv->htmlc);
2015-08-09 14:26:41 +03:00
2017-09-04 01:19:11 +03:00
NSLOG(netsurf, INFO,
"URL is %s", nsurl_access(browser_window_access_url(priv->win)));
2015-08-09 14:26:41 +03:00
%}
prototype Window()
%{
#define EXPOSE(v) \
duk_get_global_string(ctx, #v); \
duk_put_prop_string(ctx, 0, #v)
2015-08-09 14:26:41 +03:00
/* steal undefined */
EXPOSE(undefined);
EXPOSE(eval);
EXPOSE(Object);
2015-08-10 21:09:58 +03:00
EXPOSE(parseInt);
EXPOSE(parseFloat);
EXPOSE(Array);
EXPOSE(Date);
EXPOSE(RegExp);
EXPOSE(Math);
2015-10-31 22:11:05 +03:00
EXPOSE(Function);
EXPOSE(Proxy);
EXPOSE(String);
#undef EXPOSE
2015-08-09 14:26:41 +03:00
%}
getter Window::document()
%{
JS_LOG("priv=%p", priv);
2015-08-09 14:26:41 +03:00
dom_document *doc = priv->htmlc->document;
dukky_push_node(ctx, (struct dom_node *)doc);
return 1;
%}
2015-08-09 18:20:09 +03:00
getter Window::window()
2015-08-09 14:26:41 +03:00
%{
2015-08-09 18:20:09 +03:00
duk_push_this(ctx);
2015-08-09 14:26:41 +03:00
return 1;
2015-08-10 21:09:58 +03:00
%}
getter Window::console()
%{
duk_push_this(ctx);
duk_get_prop_string(ctx, -1, MAGIC(Console));
if (duk_is_undefined(ctx, -1)) {
duk_pop(ctx);
if (dukky_create_object(ctx, PROTO_NAME(CONSOLE), 0) != DUK_EXEC_SUCCESS) {
return duk_error(ctx, DUK_ERR_ERROR, "Unable to create console object");
2015-08-10 21:09:58 +03:00
}
duk_dup(ctx, -1);
duk_put_prop_string(ctx, -3, MAGIC(Console));
}
return 1;
%}
getter Window::location()
%{
duk_push_this(ctx);
duk_get_prop_string(ctx, -1, MAGIC(Location));
if (duk_is_undefined(ctx, -1)) {
duk_pop(ctx);
duk_push_pointer(ctx, llcache_handle_get_url(priv->htmlc->base.llcache));
if (dukky_create_object(ctx, PROTO_NAME(LOCATION), 1) != DUK_EXEC_SUCCESS) {
return duk_error(ctx, DUK_ERR_ERROR, "Unable to create location object");
}
duk_dup(ctx, -1);
duk_put_prop_string(ctx, -3, MAGIC(Location));
}
return 1;
%}
2015-10-05 17:46:22 +03:00
getter Window::navigator()
%{
duk_push_this(ctx);
duk_get_prop_string(ctx, -1, MAGIC(Navigator));
if (duk_is_undefined(ctx, -1)) {
duk_pop(ctx);
if (dukky_create_object(ctx,
PROTO_NAME(NAVIGATOR),
0) != DUK_EXEC_SUCCESS) {
return duk_error(ctx,
2015-10-05 17:46:22 +03:00
DUK_ERR_ERROR,
"Unable to create navigator object");
}
duk_dup(ctx, -1);
duk_put_prop_string(ctx, -3, MAGIC(Navigator));
}
return 1;
%}
getter Window::name()
%{
const char *name;
browser_window_get_name(priv->win, &name);
duk_push_string(ctx, name);
return 1;
%}
setter Window::name()
%{
const char *name;
name = duk_to_string(ctx, -1);
browser_window_set_name(priv->win, name);
return 0;
%}
2015-10-31 12:23:56 +03:00
method Window::alert()
%{
duk_size_t msg_len;
const char *msg = duk_safe_to_lstring(ctx, 0, &msg_len);
2017-09-04 01:19:11 +03:00
NSLOG(netsurf, INFO, "JS ALERT: %*s", (int)msg_len, msg);
2015-10-31 12:23:56 +03:00
return 0;
%}