Changeset 35
- Timestamp:
- 03/28/07 04:38:17 (22 months ago)
- Location:
- trunk/sagot
- Files:
-
- 12 modified
-
config.h.in (modified) (1 diff)
-
configure.in (modified) (1 diff)
-
src/Makefile.am (modified) (1 diff)
-
src/sagot.c (modified) (2 diffs)
-
src/sagot_glib_eventcontext.c (modified) (2 diffs)
-
src/sagot_glib_eventcontext.h (modified) (1 diff)
-
src/sagot_glib_eventloop.c (modified) (8 diffs)
-
src/sagot_glib_eventloop.h (modified) (1 diff)
-
src/sagot_glib_filechannel.c (modified) (2 diffs)
-
src/sagot_glib_filechannel.h (modified) (1 diff)
-
src/sagot_glib_iochannel.c (modified) (10 diffs)
-
src/sagot_glib_iochannel.h (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
trunk/sagot/config.h.in
r20 r35 1 1 /* config.h.in. Generated from configure.in by autoheader. */ 2 3 /* backtrace_symbols check */ 4 #undef HAVE_BACKTRACE 2 5 3 6 /* Define to 1 if you have the <inttypes.h> header file. */ -
trunk/sagot/configure.in
r23 r35 15 15 AC_SUBST(DEPS_CFLAGS) 16 16 AC_SUBST(DEPS_LIBS) 17 AC_CHECK_FUNC(backtrace_symbols, [AC_DEFINE(HAVE_BACKTRACE, 1, [backtrace_symbols check])]) 17 18 18 19 AC_OUTPUT([ -
trunk/sagot/src/Makefile.am
r23 r35 3 3 sagot_glib_iochannel.c sagot_glib_filechannel.c 4 4 sagot_LDADD = $(DEPS_LIBS) 5 AM_CFLAGS = - -std=c99$(DEPS_CFLAGS)5 AM_CFLAGS = -std=c99 -pedantic -Wall -W -O2 $(DEPS_CFLAGS) -
trunk/sagot/src/sagot.c
r23 r35 14 14 #include <sys/stat.h> 15 15 #include <unistd.h> 16 #include <errno.h> 16 17 18 #include "debug.h" 17 19 #include "sagot_glib.h" 18 20 … … 155 157 sagot_InitIOChannelClass(cx, global); 156 158 159 char *filename = argv[1]; 160 157 161 struct stat statr; 158 162 159 if (!stat( argv[1], &statr)) {160 FILE *script = fopen( argv[1], "r");161 /* Go C99! */ 163 if (!stat(filename, &statr)) { 164 FILE *script = fopen(filename, "r"); 165 162 166 char *code = (char *) malloc(statr.st_size); 163 // char script[statr.st_size]; .. we might as well store it in heap space 164 // rather than stack space...165 read(fileno(script), code, statr.st_size);167 168 fread(code, statr.st_size, 1, script); 169 fclose(script); 166 170 167 171 jsval rval; 168 172 169 173 JSScript *bytecode = JS_CompileScript(cx, global, code, statr.st_size, 170 argv[1], 0);174 filename, 0); 171 175 172 176 /* precompiling the code should increase the speed a lot */ 173 177 JS_ExecuteScript(cx, global, bytecode, &rval); 178 } 179 else { 180 printf("Cannot open %s: %s\n", filename, strerror(errno)); 174 181 } 175 182 -
trunk/sagot/src/sagot_glib_eventcontext.c
r5 r35 25 25 }; 26 26 27 /* Not quite sure how this works yet 27 28 static JSFunctionSpec eventcontext_methods_static[] = { 28 // Not quite sure how this works yet 29 // {"default", sagot_eventcontext_default, 0,0,0}, 29 {"default", sagot_eventcontext_default, 0,0,0}, 30 30 }; 31 */ 31 32 32 33 /* … … 68 69 JSObject *proto = JS_InitClass(cx, object, NULL, &eventcontext_class, 69 70 EventContext, 0, NULL, eventcontext_methods, NULL, 70 eventcontext_methods_static);71 NULL); 71 72 72 73 return !proto ? NULL : proto; -
trunk/sagot/src/sagot_glib_eventcontext.h
r17 r35 21 21 JSObject * sagot_InitEventContextClass (JSContext *cx, JSObject *object); 22 22 JSClass eventcontext_class; 23 static JSFunctionSpec eventcontext_methods[ ];24 static JSFunctionSpec eventcontext_methods_static[];23 static JSFunctionSpec eventcontext_methods[3]; 24 //static JSFunctionSpec eventcontext_methods_static[0]; -
trunk/sagot/src/sagot_glib_eventloop.c
r16 r35 28 28 {"quit", sagot_eventloop_quit, 0,0,0 }, 29 29 {"yield", sagot_eventloop_yield, 1,0,0 }, 30 {"watch", sagot_eventloop_watch, 3,0,0 }, 30 31 }; 31 32 32 static JSFunctionSpec eventloop_methods_static[] = { 33 }; 33 //static JSFunctionSpec eventloop_methods_static[0]; 34 34 35 35 /* Create a new main loop. If we aren't provided a context, use the default … … 112 112 JSFunction *function = JS_ValueToFunction(cx, argv[1]); 113 113 114 jsval *rval;114 jsval rval; 115 115 jsval args[argc]; 116 116 … … 119 119 } 120 120 121 JS_CallFunction(cx, parent, function, argc, args, rval);121 JS_CallFunction(cx, parent, function, argc, args, &rval); 122 122 123 123 return FALSE; 124 124 } 125 125 126 /** 127 * Cleanup data (post-dispatch) 128 */ 126 129 void sagot_eventloop_dispatch_cleanup (void *data) { 127 130 struct queue_item *qitem = (struct queue_item *) data; … … 138 141 139 142 /* glib provides nice malloc/free warnings at runtime */ 140 struct queue_item *qitem = g_malloc(sizeof (struct queue_item));143 struct queue_item *qitem = g_malloc(sizeof *qitem); 141 144 142 145 if (argc < 2) { … … 146 149 147 150 if (JSVAL_IS_STRING(argv[1])) { 151 jsval fun; 152 148 153 JS_GetProperty(cx, JSVAL_TO_OBJECT(argv[0]), 149 JS_GetStringBytes(JS_ValueToString(cx, argv[1])), &argv[1]); 150 151 if (JS_ObjectIsFunction(cx, JSVAL_TO_OBJECT(argv[1]))) { 154 JS_GetStringBytes(JS_ValueToString(cx, argv[1])), &fun); 155 156 if (fun == JSVAL_VOID) { 157 JS_ReportError(cx, "EventLoop.yield(): Unable to look up method"); 158 } 159 else if (JS_ObjectIsFunction(cx, JSVAL_TO_OBJECT(fun))) { 152 160 /* This might be kind of slow, think about creating a source 153 161 * specifically for these vents, attaching the source to the loop, and … … 156 164 */ 157 165 166 argv[1] = fun; 158 167 *qitem = (struct queue_item) { 159 168 .esid = g_malloc(sizeof(int)), … … 174 183 } 175 184 } 176 177 JS_ReportError(cx, "argument 1 to enqueue() must be a string"); 178 179 return false; 185 else { 186 JS_ReportError(cx, "argument 1 to enqueue() must be a string"); 187 return false; 188 } 189 } 190 191 static JSBool sagot_eventloop_watch (JSContext *cx, JSObject *obj, uintN argc, 192 jsval *argv, jsval *rval) { 193 GMainLoop *mainloop = JS_GetPrivate(cx, obj); 194 195 if (argc < 4) { 196 JS_ReportError(cx, "EventLoop.watch() requires 4 arguments"); 197 return false; 198 } 199 200 if (!JSVAL_IS_OBJECT(argv[0])) { 201 JS_ReportError(cx, "EventLoop.watch() argument 1 must be an iochannel"); 202 return false; 203 } 204 205 if (!JSVAL_IS_INT(argv[1])) { 206 JS_ReportError(cx, "EventLoop.watch() argument 2 must be a number"); 207 return false; 208 } 209 210 jsval fun; 211 212 if (JSVAL_IS_STRING(argv[3])) { 213 JS_GetProperty(cx, JSVAL_TO_OBJECT(argv[2]), 214 JS_GetStringBytes(JS_ValueToString(cx, argv[3])), &fun); 215 } 216 else { 217 JS_ReportError(cx, "BUGNUTS!"); 218 } 219 220 if (fun == JSVAL_VOID) { 221 JS_ReportError(cx, "EventLoop.watch(): Unable to look up method"); 222 return false; 223 } 224 else if (!JS_ObjectIsFunction(cx, JSVAL_TO_OBJECT(fun))) { 225 JS_ReportError(cx, "Unable to locate function in supplied object"); 226 return false; 227 } 228 229 GIOChannel *channel = JS_GetPrivate(cx, JSVAL_TO_OBJECT(argv[0])); 230 231 if (channel) { 232 /* Create a new event source... TODO we may want to explore only having 233 * a few event sources per context, and attaching new descriptors to 234 * this source. Although this will likely be more complicated, it may 235 * be faster. 236 */ 237 GSource *source = g_io_create_watch(channel, JSVAL_TO_INT(argv[1])); 238 239 /* Create a new execution item... */ 240 struct queue_item *qitem = g_malloc(sizeof *qitem); 241 242 /* Now that we got what we wanted out of the argv...let's re-arrange it 243 */ 244 argv[1] = fun; 245 argv[2] = argv[3]; 246 argv[3] = argv[0]; 247 248 argv++; 249 argc--; 250 251 *qitem = (struct queue_item) { 252 .esid = g_malloc(sizeof(int)), 253 .args = JS_NewArrayObject(cx, argc, argv), 254 .argc = argc, 255 .cx = cx, 256 }; 257 258 /* Move the first two items over, creating an empty spot for the 259 * channel as an argument...heh, this is a bunch of work just to reuse 260 * that dispatch code. 261 qitem->args[0] = qitem->args[1]; 262 qitem->args[1] = qitem->args[2]; 263 qitem->args[2] = argv[0]; 264 */ 265 266 /* Set the callback for this source, storing session data */ 267 g_source_set_callback(source, sagot_eventloop_dispatch, qitem, 268 sagot_eventloop_dispatch_cleanup); 269 270 /* Attach our event source to the context for the loop given to us */ 271 g_source_attach( source, g_main_loop_get_context( mainloop )); 272 273 JS_AddRoot(cx, &qitem->args); 274 275 return true; 276 } 277 else { 278 JS_ReportError(cx, "EventLoop.watch() argument 1 must be an iochannel"); 279 return false; 280 } 180 281 } 181 282 … … 184 285 JSObject *proto = JS_InitClass(cx, object, NULL, &eventloop_class, 185 286 EventLoop, 1, NULL, eventloop_methods, NULL, 186 eventloop_methods_static);287 NULL); 187 288 188 289 return !proto ? NULL : proto; -
trunk/sagot/src/sagot_glib_eventloop.h
r17 r35 24 24 static JSBool sagot_eventloop_yield (JSContext *cx, JSObject *obj, uintN 25 25 argc, jsval *argv, jsval *rval); 26 static JSBool sagot_eventloop_watch (JSContext *cx, JSObject *obj, uintN 27 argc, jsval *argv, jsval *rval); 26 28 JSObject * sagot_InitEventLoopClass (JSContext *cx, JSObject *object); -
trunk/sagot/src/sagot_glib_filechannel.c
r24 r35 31 31 static JSPropertySpec filechannel_properties[] = { 32 32 {"fd", 1, JSPROP_READONLY, sagot_iochannel_fd_get, NULL }, 33 };34 35 static JSFunctionSpec filechannel_methods_static[] = {36 33 }; 37 34 … … 83 80 JSObject *proto = JS_InitClass(cx, object, NULL, &filechannel_class, 84 81 FileChannel, 2, NULL, filechannel_methods, NULL, 85 filechannel_methods_static);82 NULL); 86 83 87 84 return !proto ? NULL : proto; -
trunk/sagot/src/sagot_glib_filechannel.h
r23 r35 12 12 13 13 static JSClass filechannel_class; 14 static JSFunctionSpec filechannel_methods[ ];15 static JSPropertySpec filechannel_properties[ ];16 static JSFunctionSpec filechannel_methods_static[]; 17 static JSPropertySpec filechannel_properties_static[]; 14 static JSFunctionSpec filechannel_methods[4]; 15 static JSPropertySpec filechannel_properties[1]; 16 // static JSFunctionSpec filechannel_methods_static[0]; Currently none 17 // static JSPropertySpec filechannel_properties_static[0]; Currently none 18 18 JSObject * sagot_InitFileChannelClass (JSContext *cx, JSObject *object); -
trunk/sagot/src/sagot_glib_iochannel.c
r24 r35 35 35 }; 36 36 37 JSFunctionSpec iochannel_methods_static[] = { 38 }; 37 // JSFunctionSpec iochannel_methods_static[0]; /* NONE */ 39 38 40 39 … … 57 56 * Create a glib unix channel. 58 57 */ 59 bool IOChannel (JSContext *cx, JSObject *object, uintN argc,58 JSBool IOChannel (JSContext *cx, JSObject *object, uintN argc, 60 59 jsval *argv, jsval *rval) { 61 60 GIOChannel *channel = NULL; … … 96 95 } 97 96 98 bool sagot_iochannel_fd_get (JSContext *cx, JSObject *object, jsval id,97 JSBool sagot_iochannel_fd_get (JSContext *cx, JSObject *object, jsval id, 99 98 jsval *vp) { 100 99 /* This could be sped up a bit by storing the fd as a jsval and rooting it, … … 107 106 } 108 107 109 bool sagot_iochannel_gettype (JSContext *cx, JSObject *object, jsval id,108 JSBool sagot_iochannel_gettype (JSContext *cx, JSObject *object, jsval id, 110 109 jsval *vp) { 111 110 /* This is kind of a hack...but it works... ehh ;-) */ … … 114 113 } 115 114 116 bool sagot_iochannel_read (JSContext *cx, JSObject *object, uintN argc,115 JSBool sagot_iochannel_read (JSContext *cx, JSObject *object, uintN argc, 117 116 jsval *argv, jsval *rval) { 118 117 GIOChannel *channel = (GIOChannel *) JS_GetPrivate(cx, object); … … 145 144 } 146 145 147 bool sagot_iochannel_readline (JSContext *cx, JSObject *self, uintN argc,146 JSBool sagot_iochannel_readline (JSContext *cx, JSObject *self, uintN argc, 148 147 jsval *argv, jsval *rval) { 149 148 GIOChannel *channel = JS_GetPrivate(cx, self); … … 166 165 } 167 166 168 bool sagot_iochannel_getc (JSContext *cx, JSObject *self, uintN argc,167 JSBool sagot_iochannel_getc (JSContext *cx, JSObject *self, uintN argc, 169 168 jsval *argv, jsval *rval) { 170 169 GIOChannel *channel = JS_GetPrivate(cx, self); … … 184 183 } 185 184 186 bool sagot_iochannel_close (JSContext *cx, JSObject *self, uintN argc,185 JSBool sagot_iochannel_close (JSContext *cx, JSObject *self, uintN argc, 187 186 jsval *argv, jsval *rval) { 188 187 GIOChannel *channel = JS_GetPrivate(cx, self); … … 195 194 } 196 195 197 bool sagot_iochannel_watch (JSContext *cx, JSObject *self, uintN argc,196 JSBool sagot_iochannel_watch (JSContext *cx, JSObject *self, uintN argc, 198 197 jsval *argv, jsval *rval) { 199 198 GIOChannel *channel = JS_GetPrivate(cx, self); … … 209 208 JSObject *proto = JS_InitClass(cx, object, NULL, &iochannel_class, 210 209 IOChannel, 2, iochannel_properties, iochannel_methods, 211 iochannel_properties_static, iochannel_methods_static);210 iochannel_properties_static, NULL); 212 211 213 212 return !proto ? NULL : proto; -
trunk/sagot/src/sagot_glib_iochannel.h
r24 r35 20 20 21 21 /* Constructor */ 22 extern bool IOChannel (JSContext *cx, JSObject *object, uintN argc, jsval *argv,22 extern JSBool IOChannel (JSContext *cx, JSObject *object, uintN argc, jsval *argv, 23 23 jsval *rval); 24 24 25 extern bool sagot_iochannel_fd_get (JSContext *cx, JSObject *object, jsval id,25 extern JSBool sagot_iochannel_fd_get (JSContext *cx, JSObject *object, jsval id, 26 26 jsval *vp); 27 27 28 extern bool sagot_iochannel_gettype (JSContext *cx, JSObject *object, jsval id,28 extern JSBool sagot_iochannel_gettype (JSContext *cx, JSObject *object, jsval id, 29 29 jsval *vp); 30 30 31 extern bool sagot_iochannel_read (JSContext *cx, JSObject *object, uintN argc,31 extern JSBool sagot_iochannel_read (JSContext *cx, JSObject *object, uintN argc, 32 32 jsval *argv, jsval *rval); 33 33 34 extern bool sagot_iochannel_readline (JSContext *cx, JSObject *self, uintN argc,34 extern JSBool sagot_iochannel_readline (JSContext *cx, JSObject *self, uintN argc, 35 35 jsval *argv, jsval *rval); 36 36 37 extern bool sagot_iochannel_getc (JSContext *cx, JSObject *self, uintN argc,37 extern JSBool sagot_iochannel_getc (JSContext *cx, JSObject *self, uintN argc, 38 38 jsval *argv, jsval *rval); 39 39 40 extern bool sagot_iochannel_close (JSContext *cx, JSObject *self, uintN argc,40 extern JSBool sagot_iochannel_close (JSContext *cx, JSObject *self, uintN argc, 41 41 jsval *argv, jsval *rval); 42 42 43 extern bool sagot_iochannel_watch (JSContext *cx, JSObject *self, uintN argc,43 extern JSBool sagot_iochannel_watch (JSContext *cx, JSObject *self, uintN argc, 44 44 jsval *argv, jsval *rval); 45 45