summaryrefslogtreecommitdiff
path: root/javascript/duktape/Window.bnd
blob: 4121ac6ca6f60eabcd55e4828a2373fb85935105 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/* 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;
	prologue %{
#include "utils/nsurl.h"
#include "desktop/browser.h"
#include "render/html.h"
#include "render/html_internal.h"
%};
}

init Window("struct browser_window *" win, "struct html_content *" htmlc)
%{
	/* element window */
	priv->win = win;
	priv->htmlc = htmlc;
	LOG("win=%p htmlc=%p", priv->win, priv->htmlc);

	LOG("URL is %s", nsurl_access(browser_window_get_url(priv->win)));
%}

prototype Window()
%{
#define EXPOSE(v) \
	duk_get_global_string(ctx, #v); \
	duk_put_prop_string(ctx, 0, #v)
	/* steal undefined */
	EXPOSE(undefined);
	EXPOSE(eval);
	EXPOSE(Object);
	EXPOSE(parseInt);
	EXPOSE(parseFloat);
	EXPOSE(Array);
	EXPOSE(Date);
	EXPOSE(RegExp);
	EXPOSE(Math);
#undef EXPOSE
%}

getter Window::document()
%{
	LOG("priv=%p", priv);
	dom_document *doc = priv->htmlc->document;
	dukky_push_node(ctx, (struct dom_node *)doc);
	return 1;
%}

getter Window::window()
%{
	duk_push_this(ctx);
	return 1;
%}

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) {
			duk_error(ctx, DUK_ERR_ERROR, "Unable to create console object");
			return 0;
		}
		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) {
			duk_error(ctx, DUK_ERR_ERROR, "Unable to create location object");
			return 0;
		}
		duk_dup(ctx, -1);
		duk_put_prop_string(ctx, -3, MAGIC(Location));
	}
	return 1;
%}