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
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
|
#ifdef _WIN32 //add -lws2_32
#include <winsock2.h>
//#define socklen_t __socklen_t
//#define close closesocket
typedef int socklen_t;
#else
#include <sys/socket.h>
#include <arpa/inet.h>
#define closesocket close
#endif
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include "net.h"
#include "lua.h"
#include "io.h"
#include "table.h"
#include "i_str.h"
#include "parray.h"
#define max_con 200
#define BUFFER_SIZE 2048
static int ports[65535] = { 0 };
static parray_t* paths = NULL;
struct lchar {
char* c;
int len;
char req[20];
};
struct sarray_t {
struct lchar** cs;
int len;
};
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t lua_mutex = PTHREAD_MUTEX_INITIALIZER;
size_t recv_full_buffer(int client_fd, char** _buffer, int* header_eof){
char* buffer = malloc(BUFFER_SIZE * sizeof * buffer);
memset(buffer, 0, BUFFER_SIZE);
char* header;
size_t len = 0;
*header_eof = -1;
int n;
for(;;){
n = recv(client_fd, buffer + len, BUFFER_SIZE, 0);
if(*header_eof == -1 && (header = strstr(buffer, "\r\n\r\n")) != NULL){
*header_eof = header - buffer;
}
if(n != BUFFER_SIZE) break;
len += BUFFER_SIZE;
buffer = realloc(buffer, len + BUFFER_SIZE);
memset(buffer + len, 0, BUFFER_SIZE);
}
//buffer[len - 1] = 0;
*_buffer = buffer;
return len + BUFFER_SIZE;
}
int parse_header(char* buffer, int header_eof, str*** _table, int* _len){
if(header_eof == -1) return -1;
char add[] = {0,0};
int lines = 3;
for(int i = 0; i != header_eof; i++) lines += buffer[i] == '\n';
str** table = malloc(sizeof ** table * lines * 2);
table[0] = str_init("Request");// table[1] = str_init("Post|Get");
table[2] = str_init("Path");// table[3] = str_init("/");
table[4] = str_init("Version");// table[5] = str_init("HTTP/1.1");
str* current = str_init("");
int ins = 1;
int oi = 0;
for(; oi != header_eof; oi++){
add[0] = buffer[oi];
if(buffer[oi] == '\n') break;
if(buffer[oi] == ' '){
table[ins] = str_init(current->c);
ins += 2;
str_clear(current);
} else str_push(current, add);
}
current->c[current->len - 1] = 0;
table[ins] = str_init(current->c);
str_clear(current);
int tlen = 6;
int key = 1;
for(int i = oi + 1; i != header_eof; i++){
if(key && buffer[i]==':' || !key && buffer[i]=='\n') {
if(!key) current->c[current->len - 1] = 0;
table[tlen] = str_init(current->c);
str_clear(current);
tlen++;
i+=key;
key = !key;
continue;
}
add[0] = buffer[i];
str_push(current, add);
}
table[tlen] = str_init(current->c);
tlen++;
str_free(current);
*_len = tlen / 2;
*_table = table;
return 0;
}
int stable_key(str** table, char* target, int flen){
for(int i = 0; i != flen * 2; i+=2){
if(strcmp(table[i]->c,target) == 0){
return i + 1;
}
}
return -1;
}
void http_build(str** _dest, int code, char* code_det, char* header_vs, char* content){
/**dest = str_init(
"HTTP/1.1 404 Not Found\r\n"
"Content-Type: text/plain\r\n"
"\r\n"
"test"
);*/
char* dest = malloc(BUFFER_SIZE);
memset(dest, 0, BUFFER_SIZE);
sprintf(dest,
"HTTP/1.1 %i %s\r\n"
"%s"
"\r\n"
, code, code_det, header_vs);
*_dest = str_init(dest);
str_push(*_dest, content);
free(dest);
}
typedef struct {
int fd;
int port;
lua_State* L;
struct sockaddr_in cli;
} thread_arg_struct;
void http_code(int code, char* code_det){
//this was done with a script btw
switch(code){
case 100: sprintf(code_det,"Continue"); break;
case 101: sprintf(code_det,"Switching Protocols"); break;
case 102: sprintf(code_det,"Processing"); break;
case 103: sprintf(code_det,"Early Hints"); break;
case 200: sprintf(code_det,"OK"); break;
case 201: sprintf(code_det,"Created"); break;
case 202: sprintf(code_det,"Accepted"); break;
case 203: sprintf(code_det,"Non-Authoritative Information"); break;
case 204: sprintf(code_det,"No Content"); break;
case 205: sprintf(code_det,"Reset Content"); break;
case 206: sprintf(code_det,"Partial Content"); break;
case 207: sprintf(code_det,"Multi-Status"); break;
case 208: sprintf(code_det,"Already Reported"); break;
case 226: sprintf(code_det,"IM Used"); break;
case 300: sprintf(code_det,"Multiple Choices"); break;
case 301: sprintf(code_det,"Moved Permanently"); break;
case 302: sprintf(code_det,"Found"); break;
case 303: sprintf(code_det,"See Other"); break;
case 304: sprintf(code_det,"Not Modified"); break;
case 307: sprintf(code_det,"Temporary Redirect"); break;
case 308: sprintf(code_det,"Permanent Redirect"); break;
case 400: sprintf(code_det,"Bad Request"); break;
case 401: sprintf(code_det,"Unauthorized"); break;
case 402: sprintf(code_det,"Payment Required"); break;
case 403: sprintf(code_det,"Forbidden"); break;
case 404: sprintf(code_det,"Not Found"); break;
case 405: sprintf(code_det,"Method Not Allowed"); break;
case 406: sprintf(code_det,"Not Acceptable"); break;
case 407: sprintf(code_det,"Proxy Authentication Required"); break;
case 408: sprintf(code_det,"Request Timeout"); break;
case 409: sprintf(code_det,"Conflict"); break;
case 410: sprintf(code_det,"Gone"); break;
case 411: sprintf(code_det,"Length Required"); break;
case 412: sprintf(code_det,"Precondition Failed"); break;
case 413: sprintf(code_det,"Content Too Large"); break;
case 414: sprintf(code_det,"URI Too Long"); break;
case 415: sprintf(code_det,"Unsupported Media Type"); break;
case 416: sprintf(code_det,"Range Not Satisfiable"); break;
case 417: sprintf(code_det,"Expectation Failed"); break;
case 418: sprintf(code_det,"I'm a teapot"); break;
case 421: sprintf(code_det,"Misdirected Request"); break;
case 422: sprintf(code_det,"Unprocessable Content"); break;
case 423: sprintf(code_det,"Locked"); break;
case 424: sprintf(code_det,"Failed Dependency"); break;
case 425: sprintf(code_det,"Too Early"); break;
case 426: sprintf(code_det,"Upgrade Required"); break;
case 428: sprintf(code_det,"Precondition Required"); break;
case 429: sprintf(code_det,"Too Many Requests"); break;
case 431: sprintf(code_det,"Request Header Fields Too Large"); break;
case 451: sprintf(code_det,"Unavailable For Legal Reasons"); break;
case 500: sprintf(code_det,"Internal Server Error"); break;
case 501: sprintf(code_det,"Not Implemented"); break;
case 502: sprintf(code_det,"Bad Gateway"); break;
case 503: sprintf(code_det,"Service Unavailable"); break;
case 504: sprintf(code_det,"Gateway Timeout"); break;
case 505: sprintf(code_det,"HTTP Version Not Supported"); break;
case 506: sprintf(code_det,"Variant Also Negotiates"); break;
case 507: sprintf(code_det,"Insufficient Storage"); break;
case 508: sprintf(code_det,"Loop Detected"); break;
case 510: sprintf(code_det,"Not Extended"); break;
case 511: sprintf(code_det,"Network Authentication Required"); break;
default: sprintf(code_det,"unknown");
}
}
void i_write_header(lua_State* L, int header_top, str** _resp, char* content){
str* resp;
lua_pushvalue(L, header_top);
str* header_vs = str_init("");
lua_pushnil(L);
for(;lua_next(L, header_top) != 0;){
char* key = (char*)luaL_checklstring(L, -2, NULL);
if(strcmp(key, "Code") != 0){
str_push(header_vs, key);
str_push(header_vs, ": ");
str_push(header_vs, (char*)luaL_checklstring(L, -1, NULL));
str_push(header_vs, "\r\n");
}
lua_pop(L, 1);
}
lua_pushvalue(L, header_top);
lua_pushstring(L, "Code");
lua_gettable(L, header_top);
int code = luaL_checkinteger(L, -1);
char code_det[50] = {0};
http_code(code, code_det);
http_build(&resp, code, code_det, header_vs->c, content);
str_free(header_vs);
*_resp = resp;
}
int l_write(lua_State* L){
int res_idx = 1;
lua_pushvalue(L, 1);
lua_pushstring(L, "_request");
lua_gettable(L, -2);
int head = strcmp(luaL_checkstring(L, -1), "HEAD") == 0;
lua_pushvalue(L, res_idx);
lua_pushstring(L, "client_fd");
lua_gettable(L, res_idx);
int client_fd = luaL_checkinteger(L, -1);
if(client_fd <= 0)
p_fatal("client fd already closed\n");
char* content = (char*)luaL_checkstring(L, 2);
lua_pushvalue(L, res_idx);
lua_pushstring(L, "header");
lua_gettable(L, -2);
int header_top = lua_gettop(L);
lua_pushstring(L, "_sent");
lua_gettable(L, -2);
str* resp;
if(lua_isnil(L, -1)){
if(head) i_write_header(L, header_top, &resp, "");
else i_write_header(L, header_top, &resp, content);
lua_pushvalue(L, header_top);
lua_pushstring(L, "_sent");
lua_pushinteger(L, 1);
lua_settable(L, -3);
} else {
if(head) return 0;
resp = str_init(content);
}
send(client_fd, resp->c, resp->len, 0);
str_free(resp);
return 0;
}
int l_send(lua_State* L){
int res_idx = 1;
lua_pushvalue(L, res_idx);
lua_pushstring(L, "client_fd");
lua_gettable(L, res_idx);
int client_fd = luaL_checkinteger(L, -1);
if(client_fd <= 0)
p_fatal("client fd already closed\n");
char* content = (char*)luaL_checkstring(L, 2);
lua_pushvalue(L, res_idx);
lua_pushstring(L, "header");
lua_gettable(L, -2);
int header = lua_gettop(L);
str* resp;
lua_pushvalue(L, 1);
lua_pushstring(L, "_request");
lua_gettable(L, -2);
if(strcmp(luaL_checkstring(L, -1), "HEAD") == 0){
i_write_header(L, header, &resp, "");
} else
i_write_header(L, header, &resp, content);
send(client_fd, resp->c, resp->len, 0);
lua_pushstring(L, "client_fd");
lua_pushinteger(L, -1);
lua_settable(L, res_idx);
closesocket(client_fd);
str_free(resp);
return 0;
}
int l_close(lua_State* L){
int res_idx = 1;
lua_pushvalue(L, res_idx);
lua_pushstring(L, "client_fd");
lua_gettable(L, res_idx);
int client_fd = luaL_checkinteger(L, -1);
if(client_fd <= 0)
p_fatal("client fd already closed\n");
return 0;
lua_pushstring(L, "client_fd");
lua_pushinteger(L, -1);
lua_settable(L, res_idx);
closesocket(client_fd);
return 0;
}
volatile size_t threads = 0;
void* handle_client(void *_arg){
//pthread_mutex_lock(&mutex);
thread_arg_struct* args = (thread_arg_struct*)_arg;
int client_fd = args->fd;
char* buffer;
char dummy[2] = {0, 0};
int header_eof;
//create state for this thread
lua_State* L = luaL_newstate();
luaL_openlibs(L);
pthread_mutex_lock(&mutex);
int old_top = lua_gettop(args->L);
lua_getglobal(args->L, "_G");
i_dcopy(args->L, L, NULL);
lua_settop(args->L, old_top);
//l_pprint(L);
lua_setglobal(L, "_G");
pthread_mutex_unlock(&mutex);
//read full request
size_t bytes_received = recv_full_buffer(client_fd, &buffer, &header_eof);
//if the buffer, yknow exists
if(bytes_received > 0){
str** table;
int len = 0;
//checks for a valid header
if(parse_header(buffer, header_eof, &table, &len) != -1){
int k = stable_key(table, "Path", len);
int R = stable_key(table, "Request", len);
char portc[10] = {0};
sprintf(portc, "%i", args->port);
str* aa = str_init(portc);
if(table[k]->c[table[k]->len - 1] != '/') str_push(table[k], "/");
str_push(aa, table[k]->c);
//if(aa->c[aa->len - 1] != '/') str_push(aa, "/");
void* v = parray_find(paths, aa->c);
str_free(aa);
if(v == NULL){
str* resp;
http_build(&resp, 404, "Not Found","text/html", "<h1>404</h1>");
send(client_fd, resp->c, resp->len, 0);
str_free(resp);
} else {
lua_newtable(L);
int req_idx = lua_gettop(L);
lua_newtable(L);
int res_idx = lua_gettop(L);
for(int i = 0; i != len * 2; i+=2){
//printf("'%s' :: '%s'\n",table[i]->c, table[i+1]->c);
luaI_tsets(L, req_idx, table[i]->c, table[i+1]->c);
}
luaI_tsets(L, req_idx, "ip", inet_ntoa(args->cli.sin_addr));
//functions
luaI_tsetcf(L, res_idx, "send", l_send);
luaI_tsetcf(L, res_idx, "write", l_write);
luaI_tsetcf(L, res_idx, "close", l_close);
//values
luaI_tseti(L, res_idx, "client_fd", client_fd);
luaI_tsets(L, res_idx, "_request", table[R]->c);
//header table
lua_newtable(L);
int header_idx = lua_gettop(L);
luaI_tseti(L, header_idx, "Code", 200);
luaI_tsets(L, header_idx, "Content-Type", "text/html");
luaI_tsetv(L, res_idx, "header", header_idx);
//the function(s)
//get all function that kinda match
parray_t* owo = (parray_t*)v;
uint64_t passes = 0;
for(int i = 0; i != owo->len; i++){
//though these are arrays of arrays we have to iterate *again*
struct sarray_t* awa = (struct sarray_t*)owo->P[i].value;
for(int z = 0; z != awa->len; z++){
char* path;
struct lchar* wowa = awa->cs[z];
if(strcmp(wowa->req, "all") == 0 || strcmp(wowa->req, table[R]->c) == 0 ||
(strcmp(table[R]->c, "HEAD") && strcmp(wowa->req, "GET"))){
luaI_tseti(L, res_idx, "passes", passes);
passes++;
luaL_loadbuffer(L, wowa->c, wowa->len, wowa->c);
int func = lua_gettop(L);
lua_pushvalue(L, func); // push function call
lua_pushvalue(L, res_idx); //push methods related to dealing with the request
lua_pushvalue(L, req_idx); //push info about the request
//call the function
lua_call(L, 2, 0);
}
}
}
parray_lclear(owo); //dont free the rest
lua_pushstring(L, "client_fd");
lua_gettable(L, res_idx);
client_fd = luaL_checkinteger(L, -1);
}
}
for(int i = 0; i != len * 2; i++){
str_free(table[i]);
}
free(table);
}
if(client_fd > 0) closesocket(client_fd);
free(args);
free(buffer);
lua_close(L);
pthread_mutex_lock(&mutex);
threads--;
pthread_mutex_unlock(&mutex);
return NULL;
}
int start_serv(lua_State* L, int port){
//need these on windows for sockets (stupid)
#ifdef _WIN32
WSADATA Data;
WSAStartup(MAKEWORD(2, 2), &Data);
#endif
int server_fd;
struct sockaddr_in server_addr;
//open the socket
if((server_fd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
p_fatal("error opening socket\n");
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = INADDR_ANY;
server_addr.sin_port = htons(port);
//bind to port
if(bind(server_fd, (struct sockaddr*)&server_addr, sizeof(server_addr)) < 0)
p_fatal("failed to bind to port\n");
if(listen(server_fd, max_con) < 0)
p_fatal("failed to listen\n");
/*
lua_rawgeti(L, LUA_REGISTRYINDEX, ports[port]);
lua_pushstring(L, "/");
lua_gettable(L, -2);
lua_pushstring(L, "fn");
lua_gettable(L, -2);
int aa = lua_gettop(L);*/
if (pthread_mutex_init(&mutex, NULL) != 0)
p_fatal("mutex init failed\n");
if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR, (const char*)&(int){1}, sizeof(int)) < 0)
p_fatal("SO_REUSEADDR refused\n");
for(;;){
struct sockaddr_in client_addr;
socklen_t client_addr_len = sizeof(client_addr);
int* client_fd = malloc(sizeof(int));
if((*client_fd = accept(server_fd, (struct sockaddr*)&client_addr, &client_addr_len)) < 0)
p_fatal("failed to accept\n");
//printf("%s\n",inet_ntoa(client_addr.sin_addr));
//printf("%i\n",threads);
//open a state to call shit, should be somewhat thread safe
thread_arg_struct* args = malloc(sizeof * args);
args->fd = *client_fd;
args->port = port;
args->cli = client_addr;
args->L = L;
pthread_mutex_lock(&mutex);
threads++;
pthread_mutex_unlock(&mutex);
//send request to handle_client()
pthread_t thread_id;
pthread_create(&thread_id, NULL, handle_client, (void*)args);
pthread_detach(thread_id);
free(client_fd);
}
}
int l_req_com(lua_State* L, char* req){
lua_pushstring(L, "port");
lua_gettable(L, 1);
int port = luaL_checkinteger(L, -1);
char portc[10] = {0};
sprintf(portc, "%i", port);//, lua_tostring(L, 2));
str* portss = str_init(portc);
str_push(portss, (char*)lua_tostring(L, 2));
if(portss->c[portss->len - 1] != '/') str_push(portss, "/");
struct lchar* awa;
lua_getglobal(L, "string");
lua_pushstring(L, "dump");
lua_gettable(L, -2);
lua_pushvalue(L, 3);
lua_call(L, 1, 1);
size_t len;
char* a = (char*)luaL_checklstring(L, -1, &len);
awa = malloc(len + 1);
awa->c = a;
awa->len = len;
strcpy(awa->req, req);
//printf("%s\n",awa->req);
if(paths == NULL)
paths = parray_init();
//please free this
void* v_old_paths = parray_get(paths, portss->c);
struct sarray_t* old_paths;
if(v_old_paths == NULL){
old_paths = malloc(sizeof * old_paths);
old_paths->len = 0;
old_paths->cs = malloc(sizeof * old_paths->cs);
} else old_paths = (struct sarray_t*)v_old_paths;
old_paths->len++;
old_paths->cs = realloc(old_paths->cs, sizeof * old_paths->cs * old_paths->len);
old_paths->cs[old_paths->len - 1] = awa;
parray_set(paths, portss->c, (void*)old_paths);
str_free(portss);
return 1;
}
#define gen_reqs(T)\
int l_##T##q (lua_State* L){\
l_req_com(L, #T);\
return 1;\
}
gen_reqs(GET);
gen_reqs(HEAD);
gen_reqs(POST);
gen_reqs(PUT);
gen_reqs(DELETE);
gen_reqs(CONNECT);
gen_reqs(OPTIONS);
gen_reqs(TRACE);
gen_reqs(PATCH);
gen_reqs(all); //non standard lol, like expressjs use keyword :3
int l_lock(lua_State* L){
pthread_mutex_lock(&lua_mutex);
return 0;
}
int l_unlock(lua_State* L){
pthread_mutex_unlock(&lua_mutex);
return 0;
}
int l_listen(lua_State* L){
lua_State* src = luaL_newstate();
lua_State* dest = luaL_newstate();
if(lua_gettop(L) != 2) {
printf("not enough args");
abort();
}
if(lua_type(L, 1) != LUA_TFUNCTION) {
printf("expected a function at arg 1");
abort();
}
int port = luaL_checkinteger(L, 2);
lua_newtable(L);
int mt = lua_gettop(L);
luaI_tsetcf(L, mt, "GET", l_GETq);
luaI_tsetcf(L, mt, "HEAD", l_HEADq);
luaI_tsetcf(L, mt, "POST", l_POSTq);
luaI_tsetcf(L, mt, "PUT", l_PUTq);
luaI_tsetcf(L, mt, "DELETE", l_DELETEq);
luaI_tsetcf(L, mt, "CONNECT", l_CONNECTq);
luaI_tsetcf(L, mt, "OPTIONS", l_OPTIONSq);
luaI_tsetcf(L, mt, "TRACE", l_TRACEq);
luaI_tsetcf(L, mt, "PATCH", l_PATCHq);
luaI_tsetcf(L, mt, "all", l_allq);
lua_pushstring(L, "lock");
lua_pushcfunction(L, l_lock);
lua_settable(L, -3);
lua_pushstring(L, "unlock");
lua_pushcfunction(L, l_unlock);
lua_settable(L, -3);
lua_pushstring(L, "port");
lua_pushvalue(L, 2);
lua_settable(L, -3);
lua_pushvalue(L, 1); //the function
lua_pushvalue(L, -2); //the server table
lua_pcall(L, 1, 0, 0);
start_serv(L, port);
return 0;
}
void* hh(void* _L){
lua_State* L = (lua_State*)_L;
lua_call(L, 0, 0);
return NULL;
}
int l_spawn(lua_State* L){
lua_getglobal(L, "string");
lua_pushstring(L, "dump");
lua_gettable(L, -2);
lua_pushvalue(L, 1);
lua_call(L, 1, 1);
size_t len;
char* a = (char*)luaL_checklstring(L, -1, &len);
//luaL_loadbuffer(L, a, len, a);
//lua_call(L,0,0);
lua_State* sL = luaL_newstate();
luaL_openlibs(sL);
requiref(sL, "_G", luaopen_base, 0);
requiref(sL, "package", luaopen_package, 1);
lua_pushlstring(sL, a, len);
char* b = (char*)luaL_checklstring(sL, -1, &len);
luaL_loadbuffer(sL, b, len, b);
//l_pprint(L);
pthread_t thread_id;
pthread_create(&thread_id, NULL, hh, (void*)sL);
pthread_detach(thread_id);
return 0;
}
|