aboutsummaryrefslogtreecommitdiff
path: root/src/lua.c
blob: ebddd032a53d7f4d988b43666e1e465484de88cd (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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
#include "lua.h"
#include <stdio.h>
#include "io.h"
#include <stdlib.h>
#include <string.h>
#include "types/str.h"
#include <stdint.h>
#include "types/parray.h"

static int ii = 0;
static int malloc_count = 0;

void* __malloc_(size_t N){
    printf("hi");
    malloc_count++;
    return (malloc)(N);
}

void __free_(void* p){
    malloc_count--;
    printf("%i\n",malloc_count);
    return (free)(p);
}

int _stream_read(lua_State* L){
  uint64_t len = 0;
  if(lua_gettop(L) > 1){
    len = lua_tointeger(L, 2);
  }

  lua_pushstring(L, "_read");
  lua_gettable(L, 1);
  stream_read_function rf = lua_touserdata(L, -1);

  lua_pushstring(L, "_state");
  lua_gettable(L, 1);
  void* state = lua_touserdata(L, -1);

  str* cont = str_init("");
  int ret = rf(len, &cont, &state);

  if(ret < 0){
    luaI_error(L, ret, "read error");
  }

  if(ret == 0){
    luaI_tsetb(L, 1, "more", 0);
  }
  
  lua_pushlstring(L, cont->c, cont->len);
  free(cont);
  return 1;
}

int _stream_file(lua_State* L){
  const int CHUNK_SIZE = 4096;
  uint64_t maxlen = 0;
  uint64_t totallen = 0;
  if(lua_gettop(L) > 2){
    maxlen = lua_tointeger(L, 3);
  }

  lua_pushstring(L, "_read");
  lua_gettable(L, 1);
  stream_read_function rf = lua_touserdata(L, -1);

  lua_pushstring(L, "_state");
  lua_gettable(L, 1);
  void* state = lua_touserdata(L, -1);

  const char* filename = lua_tostring(L, 2);
  FILE *f;
  f = fopen(filename, "w");
  if(f == NULL){
    luaI_error(L, -1, "unable to open file");
  }

  str* cont = str_init("");
  for(;;){
    int ret = rf(CHUNK_SIZE, &cont, &state);
    //printf("%s\n", cont->c);

    if(ret < 0){
      fclose(f);
      luaI_error(L, ret, "read error"); 
    }

    fwrite(cont->c, sizeof * cont->c, cont->len, f);
    totallen += cont->len;
    str_clear(cont);

    if(ret == 0 || totallen >= maxlen){
      if(ret == 0) {luaI_tsetb(L, 1, "more", 0);}
      break;
    }
  }

  fclose(f); 
  return 0;
}

int _stream_free(lua_State* L){
  lua_pushstring(L, "_free");
  lua_gettable(L, 1);
  void* rf = lua_touserdata(L, -1);

  lua_pushstring(L, "_state");
  lua_gettable(L, 1);
  void* state = lua_touserdata(L, -1);

  printf("call free\n");
  if(rf != NULL){
    printf("run free\n");
    ((stream_free_function)rf)(&state);
  }
  return 0;
}

void luaI_newstream(lua_State* L, stream_read_function readf, stream_free_function freef, void* state){
  lua_newtable(L);
  int tidx = lua_gettop(L);

  luaI_tsetlud(L, tidx, "_read", readf);
  luaI_tsetlud(L, tidx, "_free", freef);
  luaI_tsetlud(L, tidx, "_state", state);
  luaI_tsetcf(L, tidx, "read", _stream_read); 
  luaI_tsetcf(L, tidx, "close", _stream_free); 
  luaI_tsetb(L, tidx, "more", 1);
  luaI_tsetcf(L, tidx, "file", _stream_file);
  
  lua_newtable(L);
  int midx = lua_gettop(L);

  luaI_tsetcf(L, midx, "__gc", _stream_free);

  lua_pushvalue(L, midx);
  lua_setmetatable(L, tidx);

  lua_pushvalue(L, tidx);
}


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;
}

/**
 * @brief copy top element from src to dest
 *
 * @param {lua_State*} source
 * @param {lua_State*} dest
 * @param {void*} items already visited, use NULL
 * @param {int} whether or not to skip meta data
*/
void luaI_deepcopy(lua_State* src, lua_State* dest, enum deep_copy_flags flags){
    size_t len;
    //printf("%i\n",seen->len);
    int at, at2;
    //int *sp = malloc(1);
    //int *sp;
    char* s;
    void* whar;
    double n;
    int old_top = lua_gettop(src);
    int modi = 0;
    
    int type;
    switch(type = lua_type(src, -1)){
        case LUA_TNUMBER:
            n = lua_tonumber(src, -1);
            if(n == (uint64_t)n) lua_pushinteger(dest, lua_tonumber(src, -1));
            else lua_pushnumber(dest, n);
            break;
        case LUA_TSTRING:;
            size_t slen;
            const char* ss = lua_tolstring(src, -1, &slen);
            lua_pushlstring(dest, ss, slen);
            break;
        case LUA_TTABLE:
            modi = 1;
            lua_newtable(dest);
            at = lua_gettop(dest);
            at2 = lua_gettop(src);
            //printf("before\n"); 
            char* aauwu = calloc(sizeof * aauwu, 50);
            sprintf(aauwu, "%p", lua_topointer(src, at2));
            //lua_pushstring(dest, aauwu);
            //lua_gettable(dest, LUA_REGISTRYINDEX);
            lua_getfield(dest, LUA_REGISTRYINDEX, aauwu);
            if(lua_type(dest, -1) == LUA_TNIL){
              //printf("new %p\n", lua_topointer(src, at2));
              lua_pushstring(dest, aauwu);
              lua_pushvalue(dest, at);
              lua_settable(dest, LUA_REGISTRYINDEX);
              lua_pop(dest, 1);
            } else {
              //printf("use %p\n", lua_topointer(src, at2));
              //lua_pop(dest, 1);
              lua_remove(dest, -2);
              free(aauwu);
              return;
            }
            free(aauwu);
            //printf("after %i:%i\n", at, lua_gettop(dest));

            lua_pushnil(src);
            for(;lua_next(src, at2) != 0;){
                int first, second = first = lua_gettop(src);
                first -= 1;
                //this is a mess, skip if key is __gc (when SKIP_GC)
                //and skip _G (when SKIP__G)
                if(((flags & SKIP__G) && lua_type(src, first) == LUA_TSTRING
                  && strcmp("_G", lua_tostring(src, first)) == 0)
                  || ((flags & SKIP_GC) && lua_type(src, first) == LUA_TSTRING 
                  && strcmp("__gc", lua_tostring(src, first)) == 0)){
                  //printf("found %s\n", lua_tostring(src, first));
                  lua_pop(src, 1);
                  continue;
                }
                lua_pushvalue(src, first);
                //l_pprint(src);
                //lua_pop(src, 1);
                luaI_deepcopy(src, dest, flags);
         
                lua_pushvalue(src, second);
                luaI_deepcopy(src, dest, flags);
                lua_settable(dest, at);

                lua_pop(src, 3);
            }
            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);

          luaL_loadbuffer(dest, awa->c, awa->len, "fun");
          //lua_remove(dest, -2);
          str_free(awa);
          break;
        case LUA_TUSERDATA:
          modi = 1;
          size_t raw_len = lua_rawlen(src, -1);
          void* ud = lua_newuserdata(dest, raw_len);
          memcpy(ud, lua_touserdata(src, -1), raw_len);
          break;
        case LUA_TLIGHTUSERDATA:
          modi = 1;
          lua_pushlightuserdata(dest, lua_touserdata(src, -1)); 
          break;
        case LUA_TTHREAD:
          lua_pushnil(dest);
          break;
        default:
          printf("unknown type %i vs (old)%i\n",lua_type(src, -1), type);
          //abort();
          lua_pushnil(dest);
          break;
    }
    int tidx = lua_gettop(dest);
    int aa = lua_gettop(src);

    if(modi && !(flags & SKIP_META) && lua_getmetatable(src, -1)){
      luaI_deepcopy(src, dest, flags | IS_META | SKIP_META);
      lua_setmetatable(dest, tidx);

      lua_settop(dest, tidx);
    }
    lua_settop(src, old_top);
}

void luaI_deepcopy2(lua_State* src, lua_State* dest){
  switch(lua_type(src, -1)){
    case LUA_TNUMBER:
      lua_pushinteger(dest, lua_tointeger(src, -1));
      break;

    case LUA_TSTRING:;
      size_t size = 0;
      const char* str = lua_tolstring(src, -1, &size);
      lua_pushlstring(dest, str, size);
      break;
    
    case LUA_TTABLE:;
      const void* p = lua_topointer(src, -1);
      char* p_string = calloc(80, sizeof * p_string);
      sprintf(p_string, "%p", p);
      
      //lua_getfield(dest, LUA_REGISTRYINDEX, p_string);
      lua_pushstring(dest, p_string);
      lua_gettable(dest, LUA_REGISTRYINDEX);
      if(!lua_isnil(dest, -1)){
        break;
      }

      lua_pop(dest, 1);
      lua_pushstring(dest, p_string);
      lua_newtable(dest);
      //lua_setfield(dest, LUA_REGISTRYINDEX, p_string);
      //lua_getfield(dest, LUA_REGISTRYINDEX, p_string);
      lua_settable(dest, LUA_REGISTRYINDEX);

      lua_pushstring(dest, p_string);
      lua_gettable(dest, LUA_REGISTRYINDEX);

      free(p_string);

      int src_top = lua_gettop(src);
      int dest_top = lua_gettop(dest);

      lua_pushnil(src);
      for(;lua_next(src, src_top) != 0;){
        luaI_deepcopy2(src, dest); 
        lua_pop(src, 1);
        luaI_deepcopy2(src, dest);

        lua_settable(dest, dest_top);
      }
      break;

    default:
      lua_pushinteger(dest, 4);
      break;
  }
}

int env_table(lua_State* L, int provide_table){
  if(provide_table == 0){
    lua_newtable(L);
  }
  int tidx = lua_gettop(L);
 
  lua_Debug debug;
  for(int i = 0;; i++){
    if(lua_getstack(L, i, &debug) == 0) break;
    for(int idx = 1;; idx++){
      const char* name = lua_getlocal(L, &debug, idx);
      int val = lua_gettop(L);
      if(name == NULL) break;

      lua_pushstring(L, name);
      lua_gettable(L, tidx);
      //all temporary (non-local variables) should start with '('
      if(!lua_isnil(L, -1) || name[0] == '('){
        lua_pop(L, 2);
        continue;
      }

      luaI_tsetv(L, tidx, name, val);
      lua_pop(L, 2);
    }
  }

  //luaI_tseti(L, tidx, "hii", 234);

  return 1;
}

//top table is prioritized
void luaI_jointable(lua_State* L){
  int idx = lua_gettop(L) - 1;

  lua_pushnil(L);
  for(;lua_next(L, -2) != 0;){
    lua_pushvalue(L, -2);
    lua_pushvalue(L, -2);
    lua_settable(L, idx);
    lua_pop(L, 1);
  }

  lua_pushvalue(L, idx);
}

//copys all variables from state A to B, including locals (stored in _locals)
//populates _ENV the same as _G
void luaI_copyvars(lua_State* from, lua_State* to){
  lua_getglobal(from, "_G");
  luaI_deepcopy(from, to, SKIP_GC | SKIP__G);
  lua_set_global_table(to);

  env_table(from, 0);
  luaI_deepcopy(from, to, SKIP_GC);
  int idx = lua_gettop(to);
  lua_pushglobaltable(to);
  int tidx = lua_gettop(to);

  luaI_tsetv(to, idx, "_ENV", tidx);

  luaI_tsetv(to, tidx, "_locals", idx);
}

/**
 * @brief extracts a table to be global
 *
 * @param {lua_State*} source
*/
void lua_set_global_table(lua_State* L){
  lua_pushnil(L);
  for(;lua_next(L, -2) != 0;){
    if(lua_type(L, -2) != LUA_TSTRING){
      lua_pop(L, 1);
      continue;
    }

    //lua_pushstring(L, lua_tostring(L, -2));
    lua_setglobal(L, lua_tostring(L, -2));
  }
}

//returns a table where index is the name at that index
void lua_upvalue_key_table(lua_State* L, int fidx){
  lua_newtable(L);
  int tidx = lua_gettop(L);
  char* name;

  for(int i = 1; (name = (char*)lua_getupvalue(L, fidx, i)) != NULL; i++){
    int idx = lua_gettop(L);
    lua_pushinteger(L, lua_rawlen(L, tidx) + 1);
    lua_pushstring(L, name);
    lua_settable(L, tidx);
  }

  lua_pushvalue(L, tidx);
}

//sets each upvalue where the name exists in _locals table.
//if function was dumped it wont work if debug values are stripped
int lua_assign_upvalues(lua_State* L, int fidx){
  lua_getglobal(L, "_locals");
  int lidx = lua_gettop(L);

  lua_upvalue_key_table(L, fidx);

  lua_pushnil(L);
  for(;lua_next(L, -2) != 0;){
    lua_gettable(L, lidx);
    if(lua_isnil(L, -1)){
      lua_pop(L, 1);
    }
    lua_setupvalue(L, fidx, lua_tointeger(L, -2));
  }

  lua_pushvalue(L, fidx);

  return 0;
}