aboutsummaryrefslogtreecommitdiff
path: root/src/lua.c
blob: 91c667bbceb5428046958c7da2484ddb601f9a53 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#include "lua.h"
#include <stdio.h>
#include "io.h"
#include <stdlib.h>
#include <string.h>
#include "i_str.h"
#include "parray.h"

static int ii = 0;

int writer(lua_State *L, const void* p, size_t sz, void* ud){
    char o[2] = {0};
    for (int i =0; i<sz; i++){
        //printf("%c\n",((char*)p)[i]);
        o[0] = ((char*)p)[i];
        str_pushl((str*)ud, o, 1);
        //printf("%s\n",((str*)ud)->c);
    }
    
    return 0;
}
void i_dcopy(lua_State* src, lua_State* dest, void* _seen){
    parray_t* seen = (parray_t*)_seen;
    int wnull = seen == NULL;
    if(wnull) seen = parray_init();
    size_t len;
    //printf("%i\n",seen->len);
    int at, at2;
    //int *sp = malloc(1);
    //int *sp;
    char* s;
    void* whar;
    int old_top = lua_gettop(src);
    //printf("%i\n",ii++);
    switch(lua_type(src, -1)){
        case LUA_TNUMBER:
            lua_pushnumber(dest, luaL_checknumber(src, -1));
            break;
        case LUA_TSTRING:
            s = (char*)luaL_checklstring(src, -1, &len);
            lua_pushlstring(dest, s, len);
            break;
        case LUA_TTABLE:
            lua_newtable(dest);
            at = lua_gettop(dest);
            at2 = lua_gettop(src);
            char aauwu[50] = {0};
            sprintf(aauwu, "%p", lua_topointer(src, at2));

            int* sp = malloc(1);
            whar = parray_get(seen, aauwu);
            if( whar != NULL){
                //printf("%s\n",lua_tostring(src, at2 - 1));
                //printf("WHAR\n");
                lua_pop(dest, 1);
                lua_rawgeti(dest, LUA_REGISTRYINDEX, *(int*)whar);

                //lua_pushnumber(dest, 23);
                /*int abb = lua_gettop(src);
                l_pprint(src);
                lua_settop(src, abb);
                abb = lua_gettop(dest);
                printf("\n**\n");
                l_pprint(dest);
                lua_settop(dest, abb);
                printf("used %i\n",*(int*)whar);*/
                return;
            }
            //lua_pushinteger(dest, 55);
            int r = luaL_ref(dest, LUA_REGISTRYINDEX);
            lua_rawgeti(dest, LUA_REGISTRYINDEX, r);
            *sp = r;
            parray_set(seen, aauwu, sp);
            //printf("saved %i\n", *sp);

            //for(int i = 0; i != seen->len; i++){
            //    printf("%i ", *(int*)seen->P[i].value);
            //}
            lua_pushnil(src);
            for(;lua_next(src, at2) != 0;){
                lua_pushvalue(src, -2);
                int a = lua_gettop(src);
                //l_pprint(src);
                lua_settop(src, a);
                i_dcopy(src, dest, seen);

                lua_pushvalue(src, -2);
                i_dcopy(src, dest, seen);
                
                lua_settable(dest, at);
                lua_pop(src, 3);
            }
            //lua_settop(dest, at);
            break;
        case LUA_TFUNCTION:
            if(lua_iscfunction(src, old_top)){
                //kinda silly
                lua_pushcfunction(dest, lua_tocfunction(src, -1));
                break;
            }
            
            str* awa = str_init("");
            lua_dump(src, writer, (void*)awa, 0);
            //l_pprint(src);
            //lua_pcall(src, 1, 1, 0);
            //l_pprint(src);
            //lua_settop(src, oo);
            //lua_pop(src, 6);

            //s = (char*)luaL_checklstring(src, -1, &len);
            lua_pushlstring(dest, awa->c, awa->len);
            //for(int i = 0; i != awa->len; i++) printf("%i : %c\n",i, awa->c[i]);
            luaL_loadbuffer(dest, awa->c, awa->len, awa->c);
            lua_remove(dest, -2);
            str_free(awa);
            //lua_pushvalue(dest, -1);
            break;
        case LUA_TUSERDATA:
            //printf("aww\n");
            //lua_pushnumber(dest, 8);
            lua_pushlightuserdata(dest, lua_touserdata(src, -1));
            break;
        default:
            printf("%i\n",lua_type(src, -1));
            lua_pushnil(dest);
            break;
    }
    if(wnull) parray_clear(seen, 1);
    //lua_settop(src, old_top);
    _seen = seen;
}