Changeset 316 for trunk/ext

Show
Ignore:
Timestamp:
04/28/2008 02:17:36 (7 months ago)
Author:
slact
Message:

no more segfaults on load. no more leaks loading or dumping. added info table. dumping still a bit sketchy for high nesting levels

Location:
trunk/ext/lua
Files:
3 modified

Legend:

Unmodified
Added
Removed
  • trunk/ext/lua/syck.c

    r306 r316  
    11/* 
    2  * lsyck.c 
     2 * syck.c 
    33 * 
    44 * $Author$ 
     
    66 * 
    77 * Copyright (C) 2005 Zachary P. Landau <kapheine@divineinvasion.net> 
     8 * slact touched this in 2008. blame him. 
    89 */ 
    910 
     
    2324 
    2425struct parser_xtra { 
    25         lua_State *L; 
     26        lua_State *L; //syck parser thread state 
     27        lua_State *orig; //original API stack state 
    2628}; 
    2729 
     30/* 
     31static void stackDump (lua_State *L) { 
     32      int i; 
     33      int top = lua_gettop(L); 
     34      for (i = 1; i <= top; i++) {  // repeat for each level  
     35        int t = lua_type(L, i); 
     36                printf("%i:     ", (top-i+1)); 
     37                switch (t) { 
     38     
     39          case LUA_TSTRING:  // strings  
     40            printf("[string]"); 
     41            break; 
     42     
     43          case LUA_TBOOLEAN:  // booleans  
     44            printf(lua_toboolean(L, i) ? "true" : "false"); 
     45            break; 
     46     
     47          case LUA_TNUMBER:  // numbers  
     48            printf("%g", lua_tonumber(L, i)); 
     49            break; 
     50     
     51          default:  // other values  
     52            printf("%s", lua_typename(L, t)); 
     53            break; 
     54     
     55        } 
     56        printf("\n");  // put a separator  
     57      } 
     58      printf("\n----------\n");  // end the listing  
     59          } 
     60*/ 
    2861SYMID 
    2962lua_syck_parser_handler(SyckParser *p, SyckNode *n) 
     
    3467        int i; 
    3568 
     69        if(!lua_checkstack(bonus->L, 1)) 
     70                luaL_error(bonus->orig,"syck parser wanted too much stack space."); 
     71         
    3672        switch (n->kind) { 
    3773                case syck_str_kind: 
     
    5591                                o = lua_gettop(bonus->L); 
    5692                        } 
     93                        else if (strcmp(n->type_id, "float") == 0 || strcmp(n->type_id, "float#fix") == 0 || strcmp(n->type_id, "float#exp") == 0)  
     94                        { 
     95                                double f; 
     96                                syck_str_blow_away_commas(n); 
     97                                f = strtod(n->data.str->ptr, NULL); 
     98                                lua_pushnumber(bonus->L, f); 
     99                                o = lua_gettop(bonus->L); 
     100                        } 
    57101                        else if (strcmp(n->type_id, "int#hex") == 0) 
    58102                        { 
     
    65109                                long intVal = strtol(n->data.str->ptr, NULL, 10); 
    66110                                lua_pushnumber(bonus->L, intVal); 
    67                                 o = lua_gettop(bonus->L); 
     111                                o = lua_gettop(bonus->L);                  
    68112                        } 
    69113                        else 
     
    110154        struct emitter_xtra *bonus = (struct emitter_xtra *)e->bonus; 
    111155        int type = lua_type(bonus->L, -1); 
    112         char buf[30];           /* find a better way, if possible */ 
     156        char buf[32];           /* find a better way, if possible */ 
    113157         
    114158        switch (type) 
     
    125169                        break; 
    126170                case LUA_TNUMBER: 
    127                         /* should handle floats as well */ 
    128                         snprintf(buf, sizeof(buf), "%i", (int)lua_tonumber(bonus->L, -1)); 
     171                        ; lua_Number lnum; //is it an int or a float? 
     172                        lnum = lua_tonumber(bonus->L, -1); 
     173                        int asInt = lnum; 
     174                        if(asInt == lnum) 
     175                                snprintf(buf, sizeof(buf), "%i", asInt); 
     176                        else 
     177                        { 
     178                                snprintf(buf, sizeof(buf), "%f", lnum); 
     179                                /* Remove trailing zeroes after the decimal point */ 
     180                                int k; 
     181                                for (k = strlen(buf) - 1; buf[k] == '0' && buf[k - 1] != '.'; k--) 
     182                                        buf[k] = '\0'; 
     183                        } 
    129184                        syck_emit_scalar(e, "number", scalar_none, 0, 0, 0, buf, strlen(buf)); 
    130185                        break; 
     
    187242} 
    188243 
     244void lua_syck_error_handler(SyckParser *p, char *msg) 
     245{ 
     246        //struct parser_xtra *bonus = (struct parser_xtra *)p->bonus; 
     247        luaL_error(((struct parser_xtra *)p->bonus)->orig, "Error at [Line %d, Col %d]: %s\n", p->linect, p->cursor - p->lineptr, msg ); 
     248} 
     249 
     250 
    189251static int syck_load(lua_State *L) 
    190252{ 
     
    198260 
    199261        parser = syck_new_parser(); 
    200         parser->bonus = S_ALLOC_N(struct emitter_xtra, 1); 
     262        parser->bonus = S_ALLOC_N(struct parser_xtra, 1); 
    201263 
    202264        bonus = (struct parser_xtra *)parser->bonus; 
     265        bonus->orig = L; 
    203266        bonus->L = lua_newthread(L); 
    204267 
    205268        syck_parser_str(parser, (char *)lua_tostring(L, 1), lua_strlen(L, 1), NULL); 
    206269        syck_parser_handler(parser, lua_syck_parser_handler); 
     270        syck_parser_error_handler(parser, lua_syck_error_handler); 
    207271        v = syck_parse(parser); 
    208272        syck_lookup_sym(parser, v, (char **)&obj); 
     
    210274        syck_free_parser(parser); 
    211275 
     276        lua_pop(L,1); //pop the thread, we don't need it anymore. 
    212277        lua_xmove(bonus->L, L, 1); 
    213278 
     279        if ( parser->bonus != NULL ) 
     280                S_FREE( parser->bonus ); 
     281         
    214282        return 1; 
    215283} 
     284 
     285 
    216286 
    217287static int syck_dump(lua_State *L) 
     
    222292        emitter = syck_new_emitter(); 
    223293        emitter->bonus = S_ALLOC_N(struct emitter_xtra, 1); 
     294 
    224295 
    225296        bonus = (struct emitter_xtra *)emitter->bonus; 
     
    241312 
    242313        luaL_pushresult(&bonus->output); 
     314         
    243315        syck_free_emitter(emitter); 
     316         
     317        if (bonus != NULL ) 
     318                S_FREE( bonus ); 
     319         
    244320 
    245321        return 1; 
     
    252328}; 
    253329 
     330 
     331static void set_info (lua_State *L) 
     332{ 
     333// Assumes the table is on top of the stack. 
     334        lua_pushliteral (L, "_COPYRIGHT"); 
     335        lua_pushliteral (L, "Copyright (C) why the lucky stiff"); 
     336        lua_settable (L, -3); 
     337        lua_pushliteral (L, "_DESCRIPTION"); 
     338        lua_pushliteral (L, "YAML handling through the syck library"); 
     339        lua_settable (L, -3); 
     340        lua_pushliteral (L, "_VERSION"); 
     341        lua_pushliteral (L, "0.56"); 
     342        lua_settable (L, -3); 
     343} 
     344 
    254345LUALIB_API int luaopen_syck(lua_State *L) 
    255346{ 
    256347        luaL_openlib(L, "syck", sycklib, 0); 
     348        set_info(L); 
    257349        return 1; 
    258350} 
  • trunk/ext/lua/test.lua

    r306 r316  
    1313        assert_false(yaml.load("--- false")) 
    1414        assert_equal(10, yaml.load("--- 10")) 
     15        assert_equal(10.3321, yaml.load("--- 10.3321")) 
    1516        local t = yaml.load("--- \n- 5\n- 10\n- 15") 
    1617        assert_equal(5, t[1]) 
     
    4142        assert_equal("--- hey\n", yaml.dump("hey")) 
    4243        assert_equal("--- 5\n", yaml.dump(5)) 
     44        assert_equal("--- 5.9991\n", yaml.dump(5.9991)) 
    4345        assert_equal("--- true\n", yaml.dump(true)) 
    4446        assert_equal("--- false\n", yaml.dump(false)) 
     
    6971        assert_equal("hello", obj2[3]) 
    7072 
    71         os.remove(file) 
    72  
    7373        yaml.dump_file(obj, file) 
    7474        local obj2, err = yaml.load_file(file) 
  • trunk/ext/lua/yaml.lua

    r306 r316  
    3131        return ret 
    3232end 
     33 
     34setmetatable(yaml, {__index=syck})