- Timestamp:
- 04/01/07 19:58:22 (22 months ago)
- Location:
- trunk/sagot/src
- Files:
-
- 2 modified
-
sagot.c (modified) (1 diff)
-
sagot_glib_eventloop.c (modified) (9 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/sagot/src/sagot.c
r35 r37 106 106 107 107 for (i = n = 0; i < argc; i++) { 108 str = JS_ValueToString(cx, argv[i]); 109 if (!str) 108 if (!JSVAL_IS_NULL(argv[i]) && JSVAL_IS_STRING(argv[i])) { 109 str = JS_ValueToString(cx, argv[i]); 110 } 111 else { 110 112 return JS_FALSE; 113 } 114 111 115 fprintf(stdout, "%s%s", i ? " " : "", JS_GetStringBytes(str)); 112 116 } 117 113 118 n++; 114 119 if (n) 115 120 fputc('\n', stdout); 121 116 122 return JS_TRUE; 117 123 } -
trunk/sagot/src/sagot_glib_eventloop.c
r36 r37 74 74 75 75 struct queue_item { 76 int *esid; 77 JSObject *args; 78 uintN argc; 79 JSContext *cx; 76 JSContext *cx; 77 78 uintN argc; 79 jsval *argv; 80 81 JSObject *parent; 82 JSFunction *callback; 80 83 }; 84 85 struct queue_item * queue_item_new (JSContext *cx, uintN argc, jsval *argv, 86 JSObject *parent, JSFunction *callback) { 87 88 struct queue_item *qitem = g_malloc(sizeof *qitem); 89 90 /* Mad syntax errors ... why? 91 *qitem = (struct queue_item *) { 92 .cx = cx, 93 .argc = argc, 94 .argv = OBJECT_TO_JSVAL(JS_NewArrayObject(cx, argc, argv)), 95 .parent = parent, 96 .callback = callback 97 }; 98 */ 99 100 qitem->cx = cx; 101 qitem->argc = argc; 102 qitem->argv = OBJECT_TO_JSVAL(JS_NewArrayObject(cx, argc, argv)); 103 qitem->parent = parent; 104 qitem->callback = callback; 105 106 /* Make sure callback is protected */ 107 JS_AddRoot(cx, parent); 108 JS_AddRoot(cx, callback); 109 110 /* Make sure arguments are protected */ 111 JS_AddRoot(cx, qitem->argv); 112 113 return qitem; 114 } 115 116 void queue_item_destroy (struct queue_item *qitem) { 117 JS_RemoveRoot(qitem->cx, qitem->parent); 118 JS_RemoveRoot(qitem->cx, qitem->callback); 119 120 JS_RemoveRoot(qitem->cx, qitem->argv); 121 122 g_free(qitem); 123 } 81 124 82 125 gboolean sagot_eventloop_io_dispatch ( GIOChannel *source, GIOCondition … … 91 134 fprintf(stderr, "got %X\n", data); 92 135 struct queue_item *qitem = (struct queue_item *) data; 93 JSContext *cx = qitem->cx;94 uintN argc = qitem->argc;95 JSObject *arglist = qitem->args;96 jsval argv[argc];97 98 for (int i = 0; i < argc; i++) {99 JS_GetElement(cx, arglist, i, &argv[i]);100 }101 102 /* We use the first two parameters, and we don't hand them off.. */103 argc -= 2;104 105 /* Used to try to use the parent of argv[0], which was a function. It never106 * worked though, apparently you cannot bend the rules of ecmascript107 * interfaces just because you're in spidermonkey unless you hack things up108 * a bit ;-) */109 JSObject *parent;110 111 if (JSVAL_IS_OBJECT(argv[0])) {112 parent = JSVAL_TO_OBJECT(argv[0]);113 }114 else {115 parent = JS_GetParent(cx, JSVAL_TO_OBJECT(argv[1]));116 }117 118 JSFunction *function = JS_ValueToFunction(cx, argv[1]);119 136 120 137 jsval rval; 121 jsval args[argc]; 122 123 for (int i = 0; i < argc; i++) { 124 args[i] = argv[i + 2]; 125 } 126 127 JS_CallFunction(cx, parent, function, argc, args, &rval); 138 139 JS_CallFunction(qitem->cx, qitem->parent, qitem->callback, qitem->argc, 140 qitem->argv, &rval); 128 141 129 142 return FALSE; … … 136 149 struct queue_item *qitem = (struct queue_item *) data; 137 150 138 JS_RemoveRoot(qitem->cx, &qitem->args); 139 140 g_free(qitem->esid); 141 g_free(qitem); 151 queue_item_destroy(qitem); 142 152 } 143 153 … … 146 156 GMainLoop *mainloop = (GMainLoop *) JS_GetPrivate(cx, obj); 147 157 148 /* glib provides nice malloc/free warnings at runtime */149 struct queue_item *qitem = g_malloc(sizeof *qitem);150 151 158 if (argc < 2) { 152 159 JS_ReportError(cx, "EventLoop.yield() requires 2 arguments"); … … 156 163 if (JSVAL_IS_STRING(argv[1])) { 157 164 jsval fun; 165 JSObject *parent = JSVAL_TO_OBJECT(argv[0]); 158 166 159 JS_GetProperty(cx, JSVAL_TO_OBJECT(argv[0]),167 JS_GetProperty(cx, parent, 160 168 JS_GetStringBytes(JS_ValueToString(cx, argv[1])), &fun); 161 169 … … 170 178 */ 171 179 172 argv[1] = fun; 173 *qitem = (struct queue_item) { 174 .esid = g_malloc(sizeof(int)), 175 .args = JS_NewArrayObject(cx, argc, argv), 176 .argc = argc, 177 .cx = cx, 178 }; 180 argv += 2; 181 argc -= 2; 182 183 struct queue_item *qitem = queue_item_new(cx, argc, argv, parent, 184 JS_ValueToFunction(cx, fun)); 185 186 if (qitem == NULL) { 187 JS_ReportError(cx, "Unable to allocate memory"); 188 } 179 189 180 190 /* Store the source at the location of the pointer we're pointing … … 184 194 g_idle_add_full(0, sagot_eventloop_dispatch, qitem, 185 195 sagot_eventloop_dispatch_cleanup); 186 187 JS_AddRoot(cx, &qitem->args);188 196 189 197 return true; … … 243 251 */ 244 252 GSource *source = g_io_create_watch(channel, JSVAL_TO_INT(argv[1])); 253 254 255 JSFunction *function = JS_ValueToFunction(cx, fun); 256 JSObject *parent = JSVAL_TO_OBJECT(argv[2]); 257 258 argc -= 3; 259 *argv += 3; 245 260 246 261 /* Create a new execution item... */ 247 struct queue_item *qitem = g_malloc(sizeof *qitem); 248 249 fprintf(stderr, "qitem alloced as %X\n", qitem); 250 251 /* Now that we got what we wanted out of the argv...let's re-arrange it 252 */ 253 argv[1] = fun; 254 argv[2] = argv[3]; 255 argv[3] = argv[0]; 256 257 argv++; 258 argc--; 259 260 *qitem = (struct queue_item) { 261 .esid = g_malloc(sizeof(int)), 262 .args = JS_NewArrayObject(cx, argc, argv), 263 .argc = argc, 264 .cx = cx, 265 }; 266 267 fprintf(stderr, "qitem post initialization is %X\n", qitem); 268 269 /* Move the first two items over, creating an empty spot for the 270 * channel as an argument...heh, this is a bunch of work just to reuse 271 * that dispatch code. 272 qitem->args[0] = qitem->args[1]; 273 qitem->args[1] = qitem->args[2]; 274 qitem->args[2] = argv[0]; 275 */ 276 262 struct queue_item *qitem = queue_item_new(cx, argc, argv, parent, 263 function); 264 277 265 fprintf(stderr, "watch put %X\n", qitem); 278 266 267 /* TODO Make detach/cleanup */ 279 268 /* Set the callback for this source, storing session data */ 280 269 g_source_set_callback(source, sagot_eventloop_io_dispatch, qitem, NULL); … … 282 271 /* Attach our event source to the context for the loop given to us */ 283 272 g_source_attach( source, g_main_loop_get_context( mainloop )); 284 285 JS_AddRoot(cx, &qitem->args);286 273 287 274 return true;