comparison nodejs/coord.cc @ 560:ce7a35abcb0d Android_Skia

Function to instantiate coord for Javascript
author Thinker K.F. Li <thinker@branda.to>
date Mon, 07 Jun 2010 11:47:34 +0800
parents 0ca8437a91fa
children 1b6402f07cd4
comparison
equal deleted inserted replaced
559:ef078d7c57b4 560:ce7a35abcb0d
6 #include "mb_X_supp.h" 6 #include "mb_X_supp.h"
7 #include "mb_tools.h" 7 #include "mb_tools.h"
8 #include "X_supp_njs.h" 8 #include "X_supp_njs.h"
9 } 9 }
10 10
11 #include "mbfly_njs.h"
12
13 #ifndef ASSERT
14 #define ASSERT(x)
15 #endif
16
11 using namespace v8; 17 using namespace v8;
12 18
13 static Handle<Value> 19 static Handle<Value>
14 xnjsmb_coord_get_index(uint32_t index, const AccessorInfo &info) { 20 xnjsmb_coord_get_index(uint32_t index, const AccessorInfo &info) {
15 HandleScope scope; 21 HandleScope scope;
16 Handle<Object> self; 22 Handle<Object> self;
17 coord_t *coord; 23 coord_t *coord;
18 co_aix v; 24 co_aix v;
19 Handle<Value> exc;
20 25
21 if(index < 0 || index >= 6) { 26 if(index < 0 || index >= 6)
22 exc = Exception::Error(String::New("Invalid index")); 27 THROW("Invalid index");
23 return ThrowException(exc);
24 }
25 28
26 self = info.This(); 29 self = info.This();
27 coord = (coord_t *)External::Unwrap(self->Get(String::New("_njs_coord"))); 30 coord = (coord_t *)UNWRAP(self);
28 v = coord_get_matrix(coord)[index]; 31 v = coord_get_matrix(coord)[index];
29 32
30 return Number::New(v); 33 return Number::New(v);
31 } 34 }
32 35
34 xnjsmb_coord_set_index(uint32_t index, Local<Value> value, 37 xnjsmb_coord_set_index(uint32_t index, Local<Value> value,
35 const AccessorInfo &info) { 38 const AccessorInfo &info) {
36 39
37 HandleScope scope; 40 HandleScope scope;
38 Handle<Object> self; 41 Handle<Object> self;
42 Handle<Object> js_rt;
39 redraw_man_t *rdman; 43 redraw_man_t *rdman;
40 coord_t *coord; 44 coord_t *coord;
41 co_aix v; 45 co_aix v;
42 Handle<Value> exc;
43 46
44 if(index < 0 || index >= 6) { 47 if(index < 0 || index >= 6)
45 exc = Exception::Error(String::New("Invalid index")); 48 THROW("Invalid Index");
46 return ThrowException(exc); 49 if(!value->IsNumber())
47 } 50 THROW("Invalid value");
48 if(!value->IsNumber()) {
49 exc = Exception::Error(String::New("Invalid value"));
50 return ThrowException(exc);
51 }
52 51
53 self = info.This(); 52 self = info.This();
54 coord = (coord_t *)External::Unwrap(self->Get(String::New("_njs_coord"))); 53 coord = (coord_t *)UNWRAP(self);
55 v = value->NumberValue(); 54 v = value->NumberValue();
56 coord_get_matrix(coord)[index] = v; 55 coord_get_matrix(coord)[index] = v;
57 56
58 rdman = (redraw_man_t *) 57 js_rt = GET(self, "mbrt")->ToObject();
59 External::Unwrap(self->Get(String::New("_njs_rdman"))); 58 rdman = xnjsmb_rt_rdman(js_rt);
60 rdman_coord_changed(rdman, coord); 59 rdman_coord_changed(rdman, coord);
61 60
62 return value; 61 return value;
63 } 62 }
63
64 static Persistent<ObjectTemplate> coord_obj_temp;
65
66 static void
67 xnjsmb_init_temp(void) {
68 coord_obj_temp = Persistent<ObjectTemplate>(ObjectTemplate::New());
69 coord_obj_temp->SetIndexedPropertyHandler(xnjsmb_coord_get_index,
70 xnjsmb_coord_set_index);
71 coord_obj_temp->SetInternalFieldCount(1);
72 }
73
74 /*! \brief Create a coord object associated with the rdman of the runtime.
75 *
76 * Two internal fields, coord and rdman.
77 */
78 Handle<Value>
79 xnjsmb_coord_new(const Arguments &args) {
80 HandleScope scope;
81 Handle<Object> js_rt;
82 Handle<Object> coord_obj, parent_obj;
83 njs_runtime_t *rt;
84 redraw_man_t *rdman;
85 coord_t *coord, *parent = NULL;
86 int argc;
87 static int init_temp = 0;
88
89 if(!init_temp) {
90 xnjsmb_init_temp();
91 init_temp = 1;
92 }
93
94 argc = args.Length();
95 if(argc > 1)
96 THROW("Too many arguments (> 1)");
97
98 js_rt = args.This();
99 rt = (njs_runtime_t *)UNWRAP(js_rt);
100 rdman = X_njs_MB_rdman(rt);
101
102 if(argc == 1) {
103 parent_obj = args[0]->ToObject();
104 parent = (coord_t *)UNWRAP(parent_obj);
105 }
106
107 /*
108 * Set Javascript object
109 */
110 coord = rdman_coord_new(rdman, parent);
111 ASSERT(coord != NULL);
112
113 coord_obj = coord_obj_temp->NewInstance();
114 ASSERT(coord_obj != NULL);
115 WRAP(coord_obj, coord);
116
117 if(parent != NULL)
118 SET(coord_obj, "parent", parent_obj);
119 SET(coord_obj, "mbrt", js_rt);
120
121 return coord_obj;
122 }