-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrjssurfaceinterface.h
136 lines (111 loc) · 4.08 KB
/
rjssurfaceinterface.h
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#pragma once
#include "rjscontext.h"
#define RJS_SURFACEINTERFACE_POINTER_PROPERTY "\xFFrjs_SurfaceInterface"
class RJSSurfaceInterface : public rengine::StandardSurfaceInterface
{
public:
RJSSurfaceInterface(RJSContext *rjs)
: m_context(rjs)
, m_surface(0)
{
assert(rjs);
}
~RJSSurfaceInterface()
{
printf("RJSSurfaceInterface::~RJSSurfaceInterface()\n");
}
rengine::Node *update(rengine::Node *old);
// std::cout << "(cpp) update called.." << std::endl;
// duk_context *ctx = m_context->scriptContext();
// bool ok = duk_get_global_string(ctx, "update");
// if (duk_pcall(ctx, 0) != 0) {
// std::cerr << "Call to 'update' returned with error state: '"
// << duk_safe_to_string(ctx, -1) << "'" << std::endl;
// return 0;
// }
// rengine::Node *node = 0;
// // if (duk_is_object(ctx, -1)) {
// // std::cout << " - return value is an object.." << std::endl;
// // duk_get_prop_string(ctx, -1, RENGINEJS_NODE_POINTER_PROPERTY);
// // if (duk_is_pointer(ctx, -1)) {
// // std::cout << " - return value has " << RENGINEJS_NODE_POINTER_PROPERTY << " value.." << std::endl;
// // node = (rengine::Node *) duk_get_pointer(ctx, -1);
// // std::cout << " - returned (Node *)" << node << std::endl;
// // }
// // }
void show() {
if (!m_surface)
m_surface = rengine::Backend::get()->createSurface(this);
m_surface->show();
}
private:
RJSContext *m_context;;
rengine::Surface *m_surface;
};
inline rengine::Node *RJSSurfaceInterface::update(rengine::Node *old)
{
printf("RJSSurfaceInterface(%p)::update(%p)\n", this, old);
// Resolve an 'update' function if present...
RJSLink *link = m_context->fromNative(this);
assert(link);
duk_context *ctx = m_context->scriptContext();
duk_push_heapptr(ctx, link->scriptValue());
duk_get_prop_string(ctx, -1, "onUpdate");
if (duk_is_function(ctx, -1)) {
duk_dup(ctx, -2);
duk_push_undefined(ctx);
duk_pcall_method(ctx, 1);
// ignore return value..
duk_pop(ctx);
} else {
// pop whatever was there which was not a function, usually undefined...
duk_pop(ctx);
}
// Then pop the heapptr..
duk_pop(ctx);
return 0;
};
inline duk_ret_t rjs_SurfaceInterface_finalize(duk_context *ctx)
{
duk_get_prop_string(ctx, 0, RJS_LINK_PROPERTY);
RJSLink *link = (RJSLink *) duk_require_pointer(ctx, -1);
RJSContext::from(ctx)->clear(link);
assert(link->ownership() == RJSLink::ScriptOwnership);
assert(link->nativeType() == RJSLink::SurfaceInterfaceType);
assert(link->nativeValue());
delete (RJSSurfaceInterface *) link->nativeValue();
return 1;
}
inline duk_ret_t rjs_SurfaceInterface_show(duk_context *ctx)
{
duk_push_this(ctx);
duk_get_prop_string(ctx, -1, RJS_LINK_PROPERTY);
RJSLink *link = (RJSLink *) duk_require_pointer(ctx, -1);
assert(link);
assert(link->nativeValue());
assert(link->nativeType() == RJSLink::SurfaceInterfaceType);
((RJSSurfaceInterface *) link->nativeValue())->show();
return 1;
}
inline duk_ret_t rjs_SurfaceInterface_constructor(duk_context *ctx)
{
if (!duk_is_constructor_call(ctx)) {
duk_push_string(ctx, "Function 'StandardSurfaceInterface' not called as a constructor");
duk_throw(ctx);
return 0;
}
RJSContext *rjs = RJSContext::from(ctx);
duk_push_this(ctx);
void *ptr = duk_get_heapptr(ctx, -1);
RJSSurfaceInterface *iface = new RJSSurfaceInterface(rjs);
RJSLink *link = rjs->create(iface, ptr, RJSLink::ScriptOwnership);
duk_push_pointer(ctx, link);
duk_put_prop_string(ctx, -2, RJS_LINK_PROPERTY);
duk_push_c_function(ctx, rjs_SurfaceInterface_finalize, 1);
duk_set_finalizer(ctx, -2);
duk_push_c_function(ctx, rjs_SurfaceInterface_show, 0);
duk_put_prop_string(ctx, -2, "show");
duk_pop(ctx);
duk_dump_context_stdout(ctx);
return 1;
}