Changeset 37

Show
Ignore:
Timestamp:
04/01/07 19:58:22 (22 months ago)
Author:
scott
Message:

Minor Cleanup, Sorted out IO errors

  • File.read() works, test passed.
  • Slightly refactored allocation of queue items:
    • This code still needs some refactoring, and in fact should probably live in a different component.
  • Fixed an unforeseen bug in Print() where data checking was inadequate.

This fixes #50.

Location:
trunk/sagot/src
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • trunk/sagot/src/sagot.c

    r35 r37  
    106106 
    107107    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 { 
    110112            return JS_FALSE; 
     113        } 
     114 
    111115        fprintf(stdout, "%s%s", i ? " " : "", JS_GetStringBytes(str)); 
    112116    } 
     117 
    113118    n++; 
    114119    if (n) 
    115120        fputc('\n', stdout); 
     121 
    116122    return JS_TRUE; 
    117123} 
  • trunk/sagot/src/sagot_glib_eventloop.c

    r36 r37  
    7474 
    7575struct 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; 
    8083}; 
     84 
     85struct 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 
     116void 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} 
    81124 
    82125gboolean sagot_eventloop_io_dispatch ( GIOChannel *source, GIOCondition 
     
    91134    fprintf(stderr, "got %X\n", data); 
    92135    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 never 
    106      * worked though, apparently you cannot bend the rules of ecmascript 
    107      * interfaces just because you're in spidermonkey unless you hack things up 
    108      * 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]); 
    119136 
    120137    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);  
    128141 
    129142    return FALSE; 
     
    136149    struct queue_item *qitem = (struct queue_item *) data; 
    137150 
    138     JS_RemoveRoot(qitem->cx, &qitem->args); 
    139  
    140     g_free(qitem->esid); 
    141     g_free(qitem); 
     151    queue_item_destroy(qitem); 
    142152} 
    143153 
     
    146156    GMainLoop *mainloop = (GMainLoop *) JS_GetPrivate(cx, obj); 
    147157 
    148     /* glib provides nice malloc/free warnings at runtime */ 
    149     struct queue_item *qitem = g_malloc(sizeof *qitem); 
    150  
    151158    if (argc < 2) { 
    152159        JS_ReportError(cx, "EventLoop.yield() requires 2 arguments"); 
     
    156163    if (JSVAL_IS_STRING(argv[1])) { 
    157164        jsval fun; 
     165        JSObject *parent = JSVAL_TO_OBJECT(argv[0]); 
    158166         
    159         JS_GetProperty(cx, JSVAL_TO_OBJECT(argv[0]),  
     167        JS_GetProperty(cx, parent,  
    160168                JS_GetStringBytes(JS_ValueToString(cx, argv[1])), &fun); 
    161169 
     
    170178             */ 
    171179 
    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            } 
    179189 
    180190            /* Store the source at the location of the pointer we're pointing 
     
    184194            g_idle_add_full(0, sagot_eventloop_dispatch, qitem, 
    185195                    sagot_eventloop_dispatch_cleanup); 
    186  
    187             JS_AddRoot(cx, &qitem->args); 
    188196 
    189197            return true; 
     
    243251         */ 
    244252        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; 
    245260         
    246261        /* 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 
    277265        fprintf(stderr, "watch put %X\n", qitem); 
    278266 
     267        /* TODO Make detach/cleanup */ 
    279268        /* Set the callback for this source, storing session data */ 
    280269        g_source_set_callback(source, sagot_eventloop_io_dispatch, qitem, NULL); 
     
    282271        /* Attach our event source to the context for the loop given to us */ 
    283272        g_source_attach( source, g_main_loop_get_context( mainloop )); 
    284  
    285         JS_AddRoot(cx, &qitem->args); 
    286273 
    287274        return true;