Mercurial > MadButterfly
comparison tools/gen_v8_binding.m4 @ 648:e13dbfd37eab
JS binding generator and observer definition with the generator
author | Thinker K.F. Li <thinker@branda.to> |
---|---|
date | Thu, 29 Jul 2010 01:02:29 +0800 |
parents | |
children | 2030e5d9f7f7 |
comparison
equal
deleted
inserted
replaced
644:c9f8c7e6f57e | 648:e13dbfd37eab |
---|---|
1 changequote(`[', `]')dnl | |
2 include([foreach.m4])dnl | |
3 divert([-1])dnl | |
4 | |
5 define([UNQUOTE], [$*]) | |
6 | |
7 define([QUOTE], [[[$*]]]) | |
8 | |
9 define([COUNT],[ifelse([$*],[],0,[$#])]) | |
10 | |
11 define([IMPORT],[define([$1],[$2$1(]$[]@[)])]) | |
12 | |
13 define([PROJ_PREFIX], [xnjsmb_]) | |
14 | |
15 define([START_ACCESSOR], [dnl | |
16 divert([-1])dnl | |
17 define([INT], [ | |
18 static Handle<Value> | |
19 ]PROJ_PREFIX[]STRUCT_NAME[_get_$][1(Local<String> property, const AccessorInfo &info) { | |
20 return Integer::New(data->$][1); | |
21 } | |
22 | |
23 static void | |
24 ]PROJ_PREFIX[]STRUCT_NAME[_set_$][1(Local<String> property, | |
25 Local<Value> value, | |
26 const AccessorInfo &info) { | |
27 Handle<Object> self = info.This(); | |
28 STRUCT_TYPE *data; | |
29 | |
30 data = (STRUCT_TYPE *)UNWRAP(self); | |
31 data->$][1 = value->Int32Value(); | |
32 } | |
33 ]) | |
34 define([OBJ], [ | |
35 static Handle<Value> | |
36 ]PROJ_PREFIX[]STRUCT_NAME[_get_$][1(Local<String> property, const AccessorInfo &info) { | |
37 return ]PROJ_PREFIX[$][2_new(data->$][1); | |
38 } | |
39 | |
40 static void | |
41 ]PROJ_PREFIX[]STRUCT_NAME[_set_$][1(Local<String> property, | |
42 Local<Value> value, | |
43 const AccessorInfo &info) { | |
44 Handle<Object> self = info.This(); | |
45 Handle<Object> obj; | |
46 $][2 *v; | |
47 STRUCT_TYPE *data; | |
48 | |
49 data = UNWRAP(self); | |
50 obj = value->ToObject(); | |
51 v = ($][2 *)UNWRAP(obj); | |
52 data->$][1 = v; | |
53 } | |
54 ]) | |
55 define([STR], [ | |
56 static Handle<Value> | |
57 ]PROJ_PREFIX[]STRUCT_NAME[_get_$][1(Local<String> property, const AccessorInfo &info) { | |
58 return String::New(data->$][1); | |
59 } | |
60 | |
61 static void | |
62 ]PROJ_PREFIX[]STRUCT_NAME[_set_$][1(Local<String> property, | |
63 Local<Value> value, | |
64 const AccessorInfo &info) { | |
65 Handle<Object> self = info.This(); | |
66 STRUCT_TYPE *data; | |
67 | |
68 data = (STRUCT_TYPE *)UNWRAP(self); | |
69 String::Utf8Value utf8(value->ToString()); | |
70 free(data->$][1); | |
71 data->$][1 = strdup(*utf8); | |
72 } | |
73 ]) | |
74 divert([])dnl | |
75 ]) | |
76 | |
77 define([STOP_ACCESSOR], [dnl | |
78 divert([-1])dnl | |
79 undefine([INT]) | |
80 undefine([OBJ]) | |
81 undefine([STR]) | |
82 divert([])dnl | |
83 ]) | |
84 | |
85 define([SET_ACCESSSOR], [dnl | |
86 define([INT], [$][1])dnl | |
87 define([OBJ], [$][1])dnl | |
88 define([STR], [$][1])dnl | |
89 inst_temp->SetAccessor(String::New("$1"), | |
90 PROJ_PREFIX[]STRUCT_NAME[]_get_[]$1, | |
91 PROJ_PREFIX[]STRUCT_NAME[]_set_[]$1); | |
92 undefine([INT])dnl | |
93 undefine([OBJ])dnl | |
94 undefine([STR])dnl | |
95 ]) | |
96 | |
97 define([START_METHOD_ARG_VAR], [dnl | |
98 define([INT], [dnl | |
99 int $][1];)dnl | |
100 define([OBJ], [dnl | |
101 $][2 *$][1];)dnl | |
102 define([STR], [dnl | |
103 char *$][1];)dnl | |
104 define([FUNC], [ | |
105 Handle<Function> $][1];)dnl | |
106 ]) | |
107 | |
108 define([START_METHOD_ARG_TYPE_CHK], [dnl | |
109 define([INT], [ || | |
110 !args[[i++]]->IsInt32()])dnl | |
111 define([OBJ], [ || | |
112 !args[[i++]]->IsObject()])dnl | |
113 define([STR], [ || | |
114 !args[[i++]]->IsString()])dnl | |
115 define([FUNC], [ || | |
116 !args[[i++]]->IsFunction()])dnl | |
117 ]) | |
118 | |
119 define([START_METHOD_ARG_ASSIGN], [dnl | |
120 define([INT], [ | |
121 $][1 = args[[i++]]->Int32Value();])dnl | |
122 define([OBJ], [ | |
123 $][1 = ($][2 *)UNWRAP(args[[i++]]->ToObject());])dnl | |
124 define([STR], [ | |
125 $][1 = strdup(*String::Utf8Value(args[[i++]]->ToString()));])dnl | |
126 define([FUNC], [ | |
127 $][1 = Function::Cast(args[[i++]]);])dnl | |
128 ]) | |
129 | |
130 define([START_METHOD_ARG_PASS], [dnl | |
131 define([INT], [$][1])dnl | |
132 define([OBJ], [$][1])dnl | |
133 define([STR], [$][1])dnl | |
134 define([FUNC], [$][1])dnl | |
135 ]) | |
136 | |
137 define([STOP_METHOD_ARG], [dnl | |
138 undefine([INT])dnl | |
139 undefine([OBJ])dnl | |
140 undefine([STR])dnl | |
141 undefine([FUNC])dnl | |
142 ]) | |
143 | |
144 define([START_METHOD], [dnl | |
145 define([METHOD], [ | |
146 static Handle<Value> | |
147 PROJ_PREFIX[]STRUCT_NAME[]_$][1(const Arguments &args) { | |
148 int i; | |
149 int argc = args.Length(); | |
150 Handle<Object> self = args.This(); | |
151 STRUCT_TYPE *_self = (STRUCT_TYPE *)UNWRAP(self); | |
152 foreach([ITER], $][3, [START_METHOD_ARG_VAR[]ITER[]STOP_METHOD_ARG]) | |
153 | |
154 if(argc != $][4) | |
155 THROW("Invalid number of arguments (!=$][4)"); | |
156 i = 0; | |
157 if(0]dnl | |
158 [foreach([ITER], $][3, [START_METHOD_ARG_TYPE_CHK[]ITER[]STOP_METHOD_ARG])) | |
159 THROW("Invalid argument type"); | |
160 | |
161 i = 0; | |
162 foreach([ITER], $][3, [START_METHOD_ARG_ASSIGN[]ITER[]STOP_METHOD_ARG])) | |
163 | |
164 $][2(_self[]foreach([ITER], $][3, [START_METHOD_ARG_PASS[], ITER[]STOP_METHOD_ARG])); | |
165 } | |
166 ])dnl | |
167 ]) | |
168 | |
169 define([STOP_METHOD], [undefine([METHOD])]) | |
170 | |
171 define([SET_METHOD], [dnl | |
172 define([METHOD], [dnl | |
173 SET(proto_temp, "$][1", | |
174 FunctionTemplate::New(PROJ_PREFIX[]STRUCT_NAME[]_$][1)); | |
175 ])dnl | |
176 $1[]dnl | |
177 undefine([METHOD])dnl | |
178 ]) | |
179 | |
180 define([STRUCT], [dnl | |
181 define([STRUCT_NAME], [$1])dnl | |
182 define([STRUCT_TYPE], [$2])dnl | |
183 [ | |
184 /* ************************************************** | |
185 * STRUCT: $1 | |
186 * Generated by gen_v8_binding.m4 | |
187 */ | |
188 static Handle<Value> | |
189 ]PROJ_PREFIX[$1(const Arguments &args) { | |
190 } | |
191 | |
192 static Persistent<FunctionTemplate> ]PROJ_PREFIX[$1][_temp; | |
193 | |
194 static Handle<Value> | |
195 ]PROJ_PREFIX[$1][_new($2 *data) { | |
196 Handle<Object> obj; | |
197 Handle<Function> func; | |
198 | |
199 func = ]PROJ_PREFIX[$1][_temp->GetFunction(); | |
200 obj = func->NewInstance(); | |
201 WRAP(obj, data); | |
202 | |
203 return obj; | |
204 } | |
205 ]dnl | |
206 foreach([ITER], ($3), [START_ACCESSOR ITER STOP_ACCESSOR])dnl | |
207 foreach([ITER], ($4), [START_METHOD ITER STOP_METHOD])dnl | |
208 [ | |
209 static void | |
210 ]PROJ_PREFIX[$1][_init(void) { | |
211 Handle<FunctionTemplate> func_temp; | |
212 Handle<ObjectTemplate> inst_temp; | |
213 Handle<ObjectTemplate> proto_temp; | |
214 | |
215 func_temp = FunctionTemplate::New(]PROJ_PREFIX[$1); | |
216 func_temp->SetClassName("]STRUCT_NAME["); | |
217 inst_temp = func_temp->InstanceTemplate(); | |
218 inst_temp->SetInternalFieldCount(1); | |
219 ] | |
220 foreach([ITER], ($3), [SET_ACCESSSOR(ITER)])dnl | |
221 inst_temp = func_temp->InstanceTemplate(); | |
222 | |
223 foreach([ITER], ($4), [SET_METHOD(ITER)])dnl | |
224 | |
225 PROJ_PREFIX[$1][_temp = Persistent<FunctionTemplate>::New(func_temp); | |
226 }]dnl | |
227 ]) | |
228 divert([])dnl |