-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathluaport.c
1383 lines (1149 loc) · 25.4 KB
/
luaport.c
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
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
** Copyright (C) 2019-2020 Tilman M. Jaeschke. See Copyright Notice in LICENSE
** Copyright (C) 2005-2020 Mike Pall. See Copyright Notice in luajit.h
** Copyright (C) 1994-2008 Lua.org, PUC-Rio. See Copyright Notice in lua.h
*/
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <time.h>
#include <math.h>
#include <float.h>
#include <ei.h>
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>
#ifdef LUAP_JIT
#include <luajit.h>
#endif
#include "luaport.h"
#ifndef LUAP_BUFLEN
#define LUAP_BUFLEN 2048
#endif
#define LUAP_PACKET 4
#define LUAP_MTYPE "_mtype"
#define LUAP_TMAP 0
#define LUAP_TTUPLE 1
#define LUAP_TLIST 2
#define LUAP_EB_EXTRA 100
#define EXIT_FAIL_READ 200
#define EXIT_FAIL_WRITE 201
#define EXIT_FAIL_SIZE 202
#define EXIT_BAD_VERSION 210
#define EXIT_BAD_TUPLE 211
#define EXIT_BAD_ATOM 212
#define EXIT_BAD_FUNC 213
#define EXIT_BAD_ARGS 214
#define EXIT_BAD_CALL 215
#define EXIT_BAD_BINARY 216
#define EXIT_BAD_COMMAND 217
#define EXIT_BAD_CONFIG 218
#define EXIT_BAD_ANY 219
#define EXIT_CALL_READ 220
#define EXIT_CALL_VERSION 221
#define EXIT_CALL_RESULT 222
#define EXIT_PRINT_LEN 230
#define EXIT_PRINT_MALLOC 231
#define EXIT_PRINT_BUF 232
#define EXIT_EI_MALLOC 240
#ifdef LUAP_JIT
#define EXIT_FAIL_JIT 203
//from https://www.lua.org/source/5.1/lauxlib.c.html
#define abs_index(L, i) ((i) > 0 || (i) <= LUA_REGISTRYINDEX ? (i) : lua_gettop(L) + (i) + 1)
#ifndef LUAP_NOINT
#if defined(LUA_NUMBER_FLOAT)
#define luap_isinteger(n) (fabsf(rintf(n) - n) < FLT_EPSILON)
#elif defined(LUA_NUMBER_DOUBLE)
#define luap_isinteger(n) (fabs(rint(n) - n) < DBL_EPSILON)
//#elif defined(LUA_NUMBER_LONGDOUBLE)
//#define luap_isinteger(n) (fabsl(rintl(n) - n) < LDBL_EPSILON)
#else
#error "long double is not supported"
#endif
//from https://www.lua.org/source/5.1/luaconf.h.html
#if defined(LUA_NUMBER_DOUBLE) && !defined(LUA_ANSI) && !defined(__SSE2__) && (defined(__i386) || defined (_M_IX86) || defined(__i386__))
#if defined(_MSC_VER)
#define lua_number2int(i,d) __asm fld d __asm fistp i
#define lua_number2integer(i,n) lua_number2int(i, n)
#else
union luai_Cast { double l_d; long l_l; };
#define lua_number2int(i,d) { volatile union luai_Cast u; u.l_d = (d) + 6755399441055744.0; (i) = u.l_l; }
#define lua_number2integer(i,n) lua_number2int(i, n)
#endif
#else
#define lua_number2int(i,d) ((i)=(int)(d))
#define lua_number2integer(i,d) ((i)=(lua_Integer)(d))
#endif
#endif
#else
#if LUA_FLOAT_TYPE == LUA_FLOAT_LONGDOUBLE
#error "long double is not supported"
#endif
#endif
static size_t read4(const unsigned char *buf)
{
return buf[0] << 24 | buf[1] << 16 | buf[2] << 8 | buf[3];
}
/*static void write4(char *buf, int val)
{
buf[0] = (val >> 24) & 0xff;
buf[1] = (val >> 16) & 0xff;
buf[2] = (val >> 8) & 0xff;
buf[3] = val & 0xff;
}*/
static size_t swap4(size_t val)
{
return (val & 0xff000000) >> 24 | (val & 0x00ff0000) >> 8 | (val & 0x0000ff00) << 8 | (val & 0x000000ff) << 24;
}
static int read_bytes(char *buf, int len)
{
ssize_t bin, c = 0;
do
{
bin = read(STDIN_FILENO, buf + c, len - c);
if (bin < 0)
{
exit(EXIT_FAIL_READ);
}
else if (bin == 0)
{
return 0;
}
c += bin;
}
while (c < len);
return len;
}
static int write_bytes(char *buf, int len)
{
ssize_t bin, c = 0;
do
{
bin = write(STDOUT_FILENO, buf + c, len - c);
if (bin < 0)
{
exit(EXIT_FAIL_WRITE);
}
else if (bin == 0)
{
return 0;
}
c += bin;
}
while (c < len);
return len;
}
static int read_term(char *buf, int *index)
{
if (read_bytes(buf, LUAP_PACKET) != LUAP_PACKET)
{
return 0;
}
size_t len = read4((unsigned char *)buf);
if (len > LUAP_BUFLEN)
{
exit(EXIT_FAIL_SIZE);
}
*index = 0;
return read_bytes(buf, len);
}
static int write_term(ei_x_buff *eb)
{
size_t len = swap4(eb->index);
if (write_bytes((char *)&len, LUAP_PACKET) != LUAP_PACKET)
{
return 0;
}
len = write_bytes(eb->buff, eb->index);
eb->index = 0;
return len;
}
#ifdef LUAP_DEBUG
static void luap_printf(ei_x_buff *eb, const char *format, ...)
{
int len = 0;
char *buf = NULL;
va_list ap;
va_start(ap, format);
len = vsnprintf(buf, len, format, ap);
va_end(ap);
if (len < 0)
{
exit(EXIT_PRINT_LEN);
}
len++;
buf = malloc(len);
if (buf == NULL)
{
exit(EXIT_PRINT_MALLOC);
}
va_start(ap, format);
len = vsnprintf(buf, len, format, ap);
va_end(ap);
if (len < 0)
{
free(buf);
exit(EXIT_PRINT_BUF);
}
ei_x_encode_version(eb);
ei_x_encode_tuple_header(eb, 2);
ei_x_encode_atom(eb, "info");
ei_x_encode_string_len(eb, buf, len);
write_term(eb);
free(buf);
}
#endif
static void luap_setmetatype(lua_State *L, int index, int type)
{
if (type == LUAP_TMAP)
{
if (lua_getmetatable(L, index))
{
lua_pushfstring(L, LUAP_MTYPE);
lua_pushnil(L);
lua_rawset(L, -3);
lua_pop(L, 1);
}
}
else
{
#ifdef LUAP_JIT
index = abs_index(L, index);
#else
index = lua_absindex(L, index);
#endif
if (!lua_getmetatable(L, index))
{
lua_createtable(L, 0, 1);
}
lua_pushfstring(L, LUAP_MTYPE);
lua_pushinteger(L, type);
lua_rawset(L, -3);
lua_setmetatable(L, index);
}
}
static int luap_hasmetatype(lua_State *L, int index, int type)
{
if (!lua_istable(L, index))
{
return 0;
}
if (lua_getmetatable(L, index))
{
lua_pushstring(L, LUAP_MTYPE);
lua_rawget(L, -2);
if (lua_isnil(L, -1))
{
return type == LUAP_TMAP;
}
else
{
return type == (int)lua_tointeger(L, -1);
}
}
else
{
return type == LUAP_TMAP;
}
}
static int luap_ei_x_skip(ei_x_buff *eb, int len)
{
eb->index += len;
int buffsz = eb->index + LUAP_EB_EXTRA;
if (buffsz > eb->buffsz)
{
buffsz += LUAP_EB_EXTRA;
eb->buffsz = buffsz;
eb->buff = realloc(eb->buff, buffsz);
}
return eb->buff != NULL;
}
static int e2l_any(const char *buf, int *index, lua_State *L);
static int e2l_integer(const char *buf, int *index, lua_State *L)
{
long l;
if (ei_decode_long(buf, index, &l))
{
return -1;
}
lua_pushinteger(L, l);
return 0;
}
static int e2l_float(const char *buf, int *index, lua_State *L)
{
double d;
if (ei_decode_double(buf, index, &d))
{
return -1;
}
lua_pushnumber(L, d);
return 0;
}
static int e2l_binary(const char *buf, int *index, lua_State *L)
{
int type;
long size = 0;
ei_get_type(buf, index, &type, (int *)&size);
char bin[size];
if (ei_decode_binary(buf, index, bin, &size))
{
return -1;
}
lua_pushlstring(L, bin, size);
return 0;
}
static int e2l_atom(const char *buf, int *index, lua_State *L)
{
char atom[MAXATOMLEN];
if (ei_decode_atom(buf, index, atom))
{
return -1;
}
if (!strcmp(atom, "true"))
{
lua_pushboolean(L, 1);
}
else if (!strcmp(atom, "false"))
{
lua_pushboolean(L, 0);
}
else if (!strcmp(atom, "nil"))
{
lua_pushnil(L);
}
else
{
lua_pushstring(L, atom);
}
return 0;
}
static int e2l_list(const char *buf, int *index, lua_State *L)
{
int arity;
if (ei_decode_list_header(buf, index, &arity))
{
return -1;
}
lua_createtable(L, arity, 0);
luap_setmetatype(L, -1, LUAP_TLIST);
for (int i = 1; i <= arity; i++)
{
if (e2l_any(buf, index, L))
{
exit(EXIT_BAD_ANY);
}
lua_rawseti(L, -2, i);
}
ei_skip_term(buf, index);
return 0;
}
static int e2l_string(const char *buf, int *index, lua_State *L)
{
int type, size;
ei_get_type(buf, index, &type, &size);
char bin[size + 1];
if (ei_decode_string(buf, index, bin))
{
return -1;
}
lua_createtable(L, size, 0);
luap_setmetatype(L, -1, LUAP_TLIST);
for (int i = 0; i < size; i++)
{
lua_pushinteger(L, (unsigned char)bin[i]);
lua_rawseti(L, -2, i + 1);
}
return 0;
}
static int e2l_nil(const char *buf, int *index, lua_State *L)
{
lua_createtable(L, 0, 0);
luap_setmetatype(L, -1, LUAP_TLIST);
ei_skip_term(buf, index);
return 0;
}
#ifndef LUAP_USERTUPLE
static int e2l_tuple(const char *buf, int *index, lua_State *L)
{
int arity;
if (ei_decode_tuple_header(buf, index, &arity))
{
return -1;
}
lua_createtable(L, arity, 0);
luap_setmetatype(L, -1, LUAP_TTUPLE);
for (int i = 1; i <= arity; i++)
{
if (e2l_any(buf, index, L))
{
exit(EXIT_BAD_ANY);
}
lua_rawseti(L, -2, i);
}
return 0;
}
#else
static int e2l_tuple(const char *buf, int *index, lua_State *L)
{
int start = *index;
if (ei_skip_term(buf, index))
{
return -1;
}
int size = *index - start;
memcpy(lua_newuserdata(L, size), buf + start, size);
return 0;
}
#endif
static int e2l_map(const char *buf, int *index, lua_State *L)
{
int arity;
if (ei_decode_map_header(buf, index, &arity))
{
return -1;
}
lua_createtable(L, 0, arity);
for (int i = 0; i < arity; i++)
{
if (e2l_any(buf, index, L) || e2l_any(buf, index, L))
{
exit(EXIT_BAD_ANY);
}
lua_rawset(L, -3);
}
return 0;
}
static int e2l_global(const char *buf, int *index, lua_State *L)
{
int arity;
if (ei_decode_map_header(buf, index, &arity))
{
return -1;
}
for (int i = 0; i < arity; i++)
{
if (e2l_any(buf, index, L) || e2l_any(buf, index, L))
{
exit(EXIT_BAD_ANY);
}
lua_rawset(L, LUA_GLOBALSINDEX);
}
return 0;
}
/*static int e2l_ref(const char *buf, int *index, lua_State *L)
{
erlang_ref *ref = lua_newuserdata(L, sizeof(erlang_ref));
if (ei_decode_ref(buf, index, ref))
{
return -1;
}
return 0;
}*/
static int e2l_any(const char *buf, int *index, lua_State *L)
{
int type, size;
ei_get_type(buf, index, &type, &size);
switch (type)
{
case ERL_SMALL_INTEGER_EXT:
case ERL_INTEGER_EXT:
return e2l_integer(buf, index, L);
case ERL_FLOAT_EXT:
return e2l_float(buf, index, L);
case ERL_BINARY_EXT:
return e2l_binary(buf, index, L);
case ERL_ATOM_EXT:
return e2l_atom(buf, index, L);
case ERL_LIST_EXT:
return e2l_list(buf, index, L);
case ERL_STRING_EXT:
return e2l_string(buf, index, L);
case ERL_NIL_EXT:
return e2l_nil(buf, index, L);
case ERL_SMALL_TUPLE_EXT:
case ERL_LARGE_TUPLE_EXT:
return e2l_tuple(buf, index, L);
case ERL_MAP_EXT:
return e2l_map(buf, index, L);
/*case ERL_REFERENCE_EXT:
return e2l_ref(buf, index, L);*/
default:
return -1;
}
return 0;
}
static int e2l_args(const char *buf, int *index, lua_State *L, int *nargs)
{
int type;
ei_get_type(buf, index, &type, nargs);
if (type == ERL_LIST_EXT)
{
if (ei_decode_list_header(buf, index, nargs))
{
return -1;
}
for (int i = 0; i < *nargs; i++)
{
if (e2l_any(buf, index, L))
{
exit(EXIT_BAD_ANY);
}
}
ei_skip_term(buf, index);
return 0;
}
else if (type == ERL_STRING_EXT)
{
char bin[*nargs + 1];
if (ei_decode_string(buf, index, bin))
{
return -1;
}
for (int i = 0; i < *nargs; i++)
{
lua_pushinteger(L, (unsigned char)bin[i]);
}
return 0;
}
else if (type == ERL_NIL_EXT)
{
ei_skip_term(buf, index);
return 0;
}
return -1;
}
static int e2l_call(const char *buf, int *index, lua_State *L, int *nargs)
{
long ref;
if (ei_decode_long(buf, index, &ref))
{
return -1;
}
lua_rawgeti(L, LUA_REGISTRYINDEX, labs(ref));
if (ref > 0)
{
luaL_unref(L, LUA_REGISTRYINDEX, ref);
}
int top = lua_gettop(L);
#ifdef LUAP_JIT
*nargs = (int)lua_objlen(L, top);
#else
*nargs = (int)luaL_len(L, top);
#endif
for (int i = 1; i <= *nargs; i++)
{
lua_rawgeti(L, top, i);
}
(*nargs)--;
lua_remove(L, top);
return 0;
}
static void l2e_any(lua_State *L, int index, ei_x_buff *eb);
static void l2e_integer(lua_State *L, int index, ei_x_buff *eb)
{
lua_Integer i = lua_tointeger(L, index);
ei_x_encode_long(eb, i);
}
static void l2e_float(lua_State *L, int index, ei_x_buff *eb)
{
lua_Number n = lua_tonumber(L, index);
ei_x_encode_double(eb, n);
}
#ifndef LUAP_JIT
static void l2e_number(lua_State *L, int index, ei_x_buff *eb)
{
if (lua_isinteger(L, index))
{
l2e_integer(L, index, eb);
}
else
{
l2e_float(L, index, eb);
}
}
#elif !defined(LUAP_NOINT)
static void l2e_number_jit(lua_State *L, int index, ei_x_buff *eb)
{
lua_Number n = lua_tonumber(L, index);
if (luap_isinteger(n))
{
lua_Integer i;
lua_number2integer(i, n);
ei_x_encode_long(eb, i);
}
else
{
ei_x_encode_double(eb, n);
}
}
#endif
static void l2e_string_string(lua_State *L, int index, ei_x_buff *eb)
{
const char *str = lua_tostring(L, index);
ei_x_encode_string(eb, str);
}
static void l2e_string_binary(lua_State *L, int index, ei_x_buff *eb)
{
size_t len;
const char *str = lua_tolstring(L, index, &len);
ei_x_encode_binary(eb, str, len);
}
static void l2e_string_atom(lua_State *L, int index, ei_x_buff *eb)
{
const char *str = lua_tostring(L, index);
ei_x_encode_atom(eb, str);
}
static void l2e_table_list(lua_State *L, int index, ei_x_buff *eb)
{
#ifdef LUAP_JIT
int arity = lua_objlen(L, index);
#else
int arity = luaL_len(L, index);
#endif
if (arity > 0)
{
ei_x_encode_list_header(eb, arity);
for (int i = 1; i <= arity; i++)
{
lua_rawgeti(L, index, i);
l2e_any(L, -1, eb);
lua_pop(L, 1);
}
}
ei_x_encode_empty_list(eb);
}
static void l2e_table_tuple(lua_State *L, int index, ei_x_buff *eb)
{
#ifdef LUAP_JIT
int arity = lua_objlen(L, index);
#else
int arity = luaL_len(L, index);
#endif
ei_x_encode_tuple_header(eb, arity);
for (int i = 1; i <= arity; i++)
{
lua_rawgeti(L, index, i);
l2e_any(L, -1, eb);
lua_pop(L, 1);
}
}
static void l2e_table_map(lua_State *L, int index, ei_x_buff *eb)
{
#ifdef LUAP_JIT
index = abs_index(L, index);
#else
index = lua_absindex(L, index);
#endif
int eb_index = eb->index;
if (!luap_ei_x_skip(eb, 5))
{
exit(EXIT_EI_MALLOC);
}
lua_pushnil(L);
int arity = 0;
while (lua_next(L, index))
{
l2e_any(L, -2, eb);
l2e_any(L, -1, eb);
lua_pop(L, 1);
arity++;
}
ei_encode_map_header(eb->buff, &eb_index, arity);
}
static void l2e_table(lua_State *L, int index, ei_x_buff *eb)
{
if (luaL_getmetafield(L, index, LUAP_MTYPE))
{
int mtype = (int)lua_tointeger(L, -1);
lua_pop(L, 1);
if (mtype == LUAP_TLIST)
{
l2e_table_list(L, index, eb);
}
else if (mtype == LUAP_TTUPLE)
{
l2e_table_tuple(L, index, eb);
}
else
{
l2e_table_map(L, index, eb);
}
}
else
{
l2e_table_map(L, index, eb);
}
}
static void l2e_boolean(lua_State *L, int index, ei_x_buff *eb)
{
int boolean = lua_toboolean(L, index);
ei_x_encode_boolean(eb, boolean);
}
#ifdef LUAP_USERTUPLE
static void l2e_userdata(lua_State *L, int index, ei_x_buff *eb)
{
const char *buf = lua_touserdata(L, index);
#ifdef LUAP_JIT
int len = lua_objlen(L, index);
#else
int len = lua_rawlen(L, index);
#endif
ei_x_append_buf(eb, buf, len);
}
#endif
static void l2e_any(lua_State *L, int index, ei_x_buff *eb)
{
int type = lua_type(L, index);
switch (type)
{
case LUA_TNUMBER:
#ifdef LUAP_JIT
#ifdef LUAP_NOINT
l2e_float(L, index, eb);
#else
l2e_number_jit(L, index, eb);
#endif
#else
l2e_number(L, index, eb);
#endif
break;
case LUA_TSTRING:
l2e_string_binary(L, index, eb);
break;
case LUA_TTABLE:
l2e_table(L, index, eb);
break;
case LUA_TBOOLEAN:
l2e_boolean(L, index, eb);
break;
case LUA_TNIL:
ei_x_encode_atom(eb, "nil");
break;
#ifdef LUAP_USERTUPLE
case LUA_TUSERDATA:
l2e_userdata(L, index, eb);
break;
#endif
default:
luaL_error(L, "unsupported type \"%s\" at index %d", lua_typename(L, index), index);
}
}
static void l2e_args(lua_State *L, int index, ei_x_buff *eb)
{
int top = lua_gettop(L);
int arity = top - index + 1;
if (arity > 0)
{
ei_x_encode_list_header(eb, arity);
for (int i = index; i <= top; i++)
{
l2e_any(L, i, eb);
}
}
ei_x_encode_empty_list(eb);
}
static void l2e_call(lua_State *L, int index, ei_x_buff *eb)
{
luaL_checktype(L, index, LUA_TFUNCTION);
int top = lua_gettop(L);
int arity = top - index + 1;
lua_createtable(L, arity, 0);
lua_insert(L, index);
for (int i = arity; i >= 1; i--)
{
lua_rawseti(L, index, i);
}
int ref = luaL_ref(L, LUA_REGISTRYINDEX);
lua_pushinteger(L, ref);
ei_x_encode_long(eb, ref);
}
static void l2e_error(lua_State *L, int index, ei_x_buff *eb)
{
ei_x_encode_version(eb);
ei_x_encode_tuple_header(eb, 2);
ei_x_encode_atom(eb, "error");
l2e_string_string(L, index, eb);
}
static void l2e_ok(lua_State *L, int index, ei_x_buff *eb)
{
ei_x_encode_version(eb);
ei_x_encode_tuple_header(eb, 2);
ei_x_encode_atom(eb, "ok");
l2e_args(L, index, eb);
}
static void l2e_pcall(lua_State *L, int nargs, ei_x_buff *eb)
{
if (lua_pcall(L, nargs, LUA_MULTRET, 0))
{
l2e_error(L, 1, eb);
}
else
{
l2e_ok(L, 1, eb);
}
}
static int port_call(lua_State *L)
{
ei_x_buff *eb = lua_touserdata(L, lua_upvalueindex(2));
char *buf = lua_touserdata(L, lua_upvalueindex(3));
int *index = lua_touserdata(L, lua_upvalueindex(4));
ei_x_encode_version(eb);
ei_x_encode_tuple_header(eb, 3);
ei_x_encode_atom(eb, "call");
l2e_string_atom(L, lua_upvalueindex(1), eb);
l2e_args(L, 1, eb);
write_term(eb);
if (read_term(buf, index) <= 0)
{
exit(EXIT_CALL_READ);
}
int version;
if (ei_decode_version(buf, index, &version))
{
exit(EXIT_CALL_VERSION);
}
int nargs;
if (e2l_args(buf, index, L, &nargs))
{
exit(EXIT_CALL_RESULT);
}
return nargs;
}
static int port_call_index(lua_State *L)
{
lua_pushvalue(L, lua_upvalueindex(1));
lua_pushvalue(L, lua_upvalueindex(2));
lua_pushvalue(L, lua_upvalueindex(3));
lua_pushcclosure(L, &port_call, 4);
return 1;
}
static int port_cast(lua_State *L)
{
ei_x_buff *eb = lua_touserdata(L, lua_upvalueindex(2));
ei_x_encode_version(eb);
ei_x_encode_tuple_header(eb, 3);
ei_x_encode_atom(eb, "cast");
l2e_string_atom(L, lua_upvalueindex(1), eb);
l2e_args(L, 1, eb);
write_term(eb);
return 0;
}
static int port_cast_index(lua_State *L)
{