# HG changeset patch # User Thinker K.F. Li # Date 1275882454 -28800 # Node ID ce7a35abcb0dd0e4657d0ff30a7dca05fe01ebe3 # Parent ef078d7c57b427f7940d3cbb0b81647b4f57c694 Function to instantiate coord for Javascript diff -r ef078d7c57b4 -r ce7a35abcb0d nodejs/X_supp_njs.c --- a/nodejs/X_supp_njs.c Mon Jun 07 11:42:45 2010 +0800 +++ b/nodejs/X_supp_njs.c Mon Jun 07 11:47:34 2010 +0800 @@ -10,19 +10,12 @@ #include #include "mb_X_supp.h" #include "mb_tools.h" +#include "X_supp_njs.h" #ifndef ASSERT #define ASSERT(x) #endif -typedef struct _njs_runtime { - ev_io iowatcher; - ev_timer tmwatcher; - int enable_io; - int enable_timer; - void *xrt; -} njs_runtime_t; - static void timer_cb(EV_P_ ev_timer *tmwatcher, int revent); /*! \brief Register next timeout with libev. diff -r ef078d7c57b4 -r ce7a35abcb0d nodejs/X_supp_njs.h --- a/nodejs/X_supp_njs.h Mon Jun 07 11:42:45 2010 +0800 +++ b/nodejs/X_supp_njs.h Mon Jun 07 11:47:34 2010 +0800 @@ -1,8 +1,15 @@ #ifndef __X_SUPP_NJS_H_ #define __X_SUPP_NJS_H_ -struct _njs_runtime; -typedef struct _njs_runtime njs_runtime_t; +#include + +typedef struct _njs_runtime { + ev_io iowatcher; + ev_timer tmwatcher; + int enable_io; + int enable_timer; + void *xrt; +} njs_runtime_t; extern void X_njs_MB_init_handle_connection(njs_runtime_t *rt); extern void X_njs_MB_free(njs_runtime_t *rt); diff -r ef078d7c57b4 -r ce7a35abcb0d nodejs/coord.cc --- a/nodejs/coord.cc Mon Jun 07 11:42:45 2010 +0800 +++ b/nodejs/coord.cc Mon Jun 07 11:47:34 2010 +0800 @@ -8,6 +8,12 @@ #include "X_supp_njs.h" } +#include "mbfly_njs.h" + +#ifndef ASSERT +#define ASSERT(x) +#endif + using namespace v8; static Handle @@ -16,15 +22,12 @@ Handle self; coord_t *coord; co_aix v; - Handle exc; - if(index < 0 || index >= 6) { - exc = Exception::Error(String::New("Invalid index")); - return ThrowException(exc); - } + if(index < 0 || index >= 6) + THROW("Invalid index"); self = info.This(); - coord = (coord_t *)External::Unwrap(self->Get(String::New("_njs_coord"))); + coord = (coord_t *)UNWRAP(self); v = coord_get_matrix(coord)[index]; return Number::New(v); @@ -36,28 +39,84 @@ HandleScope scope; Handle self; + Handle js_rt; redraw_man_t *rdman; coord_t *coord; co_aix v; - Handle exc; - if(index < 0 || index >= 6) { - exc = Exception::Error(String::New("Invalid index")); - return ThrowException(exc); - } - if(!value->IsNumber()) { - exc = Exception::Error(String::New("Invalid value")); - return ThrowException(exc); - } + if(index < 0 || index >= 6) + THROW("Invalid Index"); + if(!value->IsNumber()) + THROW("Invalid value"); self = info.This(); - coord = (coord_t *)External::Unwrap(self->Get(String::New("_njs_coord"))); + coord = (coord_t *)UNWRAP(self); v = value->NumberValue(); coord_get_matrix(coord)[index] = v; - rdman = (redraw_man_t *) - External::Unwrap(self->Get(String::New("_njs_rdman"))); + js_rt = GET(self, "mbrt")->ToObject(); + rdman = xnjsmb_rt_rdman(js_rt); rdman_coord_changed(rdman, coord); return value; } + +static Persistent coord_obj_temp; + +static void +xnjsmb_init_temp(void) { + coord_obj_temp = Persistent(ObjectTemplate::New()); + coord_obj_temp->SetIndexedPropertyHandler(xnjsmb_coord_get_index, + xnjsmb_coord_set_index); + coord_obj_temp->SetInternalFieldCount(1); +} + +/*! \brief Create a coord object associated with the rdman of the runtime. + * + * Two internal fields, coord and rdman. + */ +Handle +xnjsmb_coord_new(const Arguments &args) { + HandleScope scope; + Handle js_rt; + Handle coord_obj, parent_obj; + njs_runtime_t *rt; + redraw_man_t *rdman; + coord_t *coord, *parent = NULL; + int argc; + static int init_temp = 0; + + if(!init_temp) { + xnjsmb_init_temp(); + init_temp = 1; + } + + argc = args.Length(); + if(argc > 1) + THROW("Too many arguments (> 1)"); + + js_rt = args.This(); + rt = (njs_runtime_t *)UNWRAP(js_rt); + rdman = X_njs_MB_rdman(rt); + + if(argc == 1) { + parent_obj = args[0]->ToObject(); + parent = (coord_t *)UNWRAP(parent_obj); + } + + /* + * Set Javascript object + */ + coord = rdman_coord_new(rdman, parent); + ASSERT(coord != NULL); + + coord_obj = coord_obj_temp->NewInstance(); + ASSERT(coord_obj != NULL); + WRAP(coord_obj, coord); + + if(parent != NULL) + SET(coord_obj, "parent", parent_obj); + SET(coord_obj, "mbrt", js_rt); + + return coord_obj; +}