view nodejs/observer.cc @ 676:f264b50c469c

Bind subject of mouse_event to JS object of coord
author Thinker K.F. Li <thinker@branda.to>
date Fri, 06 Aug 2010 13:33:47 +0800
parents c643af2095c5
children 799c852b9065
line wrap: on
line source

#include <v8.h>
#include "mbfly_njs.h"

extern "C" {
#include <mb.h>
#include <string.h>
}

#ifndef ASSERT
#define ASSERT(x)
#endif

using namespace v8;

struct xnjsmb_observer_data {
    Persistent<Function> func;
};

static void
event_handler(event_t *evt, void *arg);

static observer_t *
_subject_add_event_observer(subject_t *subject, int type,
                            Handle<Function> func) {
    observer_t *observer;
    xnjsmb_observer_data *data;
    
    data = new xnjsmb_observer_data;
    if(data == NULL)
	return NULL;
    data->func = Persistent<Function>::New(func);
    observer = subject_add_event_observer(subject, type,
					  event_handler,
    	       				  data);

    return observer;
}

static void
_subject_remove_observer(subject_t *subject, observer_t *observer) {
    xnjsmb_observer_data *data;

    subject_remove_observer(subject, observer);
    data = (xnjsmb_observer_data *)observer->arg;
    delete data;
}

static Handle<Value>
xnjsmb_event_tgt_getter(Handle<Object> self, event_t *evt, const char **err) {
    Persistent<Object> *hdl;
    
    hdl = (Persistent<Object> *)
	mb_prop_get(&((mb_obj_t *)evt->tgt)->props,
		    PROP_JSOBJ);
    return Local<Object>::New(*hdl);
}

static void
xnjsmb_event_tgt_setter(Handle<Object> self, event_t *evt,
			Handle<Value> value, const char **err) {
    *err = "Not implemented";
}

static Handle<Value>
xnjsmb_event_cur_tgt_getter(Handle<Object> self, event_t *evt,
			    const char **err) {
    Persistent<Object> *hdl;
    
    hdl = (Persistent<Object> *)
	mb_prop_get(&((mb_obj_t *)evt->cur_tgt)->props,
		    PROP_JSOBJ);
    return Local<Object>::New(*hdl);
}

static void
xnjsmb_event_cur_tgt_setter(Handle<Object> self, event_t *evt,
			    Handle<Value> value, const char **err) {
    *err = "Not implemented";
}

/* This is the part of the code generated by gen_v8_binding.m4 */
#include "observer-inc.h"

static void
event_handler(event_t *evt, void *arg) {
    xnjsmb_observer_data *data = (xnjsmb_observer_data *)arg;
    Handle<Value> evt_obj;
    Handle<Value> func_args[1];

    evt_obj = xnjsmb_auto_event_new(evt);
    ASSERT(!evt_obj.IsEmpty());
    func_args[0] = evt_obj;
    data->func->Call(Context::GetCurrent()->Global(), 1, func_args);
}

Handle<Value>
export_xnjsmb_auto_subject_new(subject_t *subject) {
    Handle<Value> val;
    
    val = xnjsmb_auto_subject_new(subject);
    return val;
}

void
xnjsmb_observer_init(void) {
    xnjsmb_auto_observer_init();
    xnjsmb_auto_subject_init();
    xnjsmb_auto_event_init();
}