Mercurial > MadButterfly
comparison nodejs/observer.cc @ 651:aa52883534fc
Code generated by generator can be built
author | Thinker K.F. Li <thinker@branda.to> |
---|---|
date | Sat, 31 Jul 2010 13:28:48 +0800 |
parents | |
children | d58251f1bf0b |
comparison
equal
deleted
inserted
replaced
650:123e7eeff10c | 651:aa52883534fc |
---|---|
1 #include <v8.h> | |
2 #include "mbfly_njs.h" | |
3 | |
4 extern "C" { | |
5 #include <mb.h> | |
6 #include <string.h> | |
7 } | |
8 | |
9 #ifndef ASSERT | |
10 #define ASSERT(x) | |
11 #endif | |
12 | |
13 using namespace v8; | |
14 | |
15 struct xnjsmb_observer_data { | |
16 Persistent<Function> func; | |
17 }; | |
18 | |
19 static void | |
20 event_handler(event_t *evt, void *arg); | |
21 | |
22 static observer_t * | |
23 _subject_add_event_observer(subject_t *subject, int type, | |
24 Handle<Function> func) { | |
25 observer_t *observer; | |
26 xnjsmb_observer_data *data; | |
27 | |
28 data = new xnjsmb_observer_data; | |
29 if(data == NULL) | |
30 return NULL; | |
31 data->func = Persistent<Function>::New(func); | |
32 observer = subject_add_event_observer(subject, type, | |
33 event_handler, | |
34 data); | |
35 | |
36 return observer; | |
37 } | |
38 | |
39 static void | |
40 _subject_remove_observer(subject_t *subject, observer_t *observer) { | |
41 xnjsmb_observer_data *data; | |
42 | |
43 subject_remove_observer(subject, observer); | |
44 data = (xnjsmb_observer_data *)observer->arg; | |
45 delete data; | |
46 } | |
47 | |
48 #include "observer-inc.h" | |
49 | |
50 static void | |
51 event_handler(event_t *evt, void *arg) { | |
52 xnjsmb_observer_data *data = (xnjsmb_observer_data *)arg; | |
53 Handle<Value> evt_obj; | |
54 Handle<Value> func_args[1]; | |
55 | |
56 evt_obj = xnjsmb_event_new(evt); | |
57 ASSERT(!evt_obj.IsEmpty()); | |
58 func_args[0] = evt_obj; | |
59 data->func->Call(Context::GetCurrent()->Global(), 1, func_args); | |
60 } | |
61 |