1 /* This should combine several functions into one, when they're already
2 * compiled into functions. Useful when we don't have their AST, e.g.
3 * to link several precompiled chunks into one.
5 * It currently doesn't work; meanwhile, one can use the original
6 * 'luac' executable, although it doesn't handle argument passing through
18 static int lua_combine( lua_State
* L
) {
19 int n
= lua_gettop( L
); /* Number of functions to combine */
21 return 1; /* Only one function, nothing to combine */
24 Proto
* f
= luaF_newproto( L
);
25 setptvalue2s( L
,L
->top
,f
);
27 f
->source
= luaS_newliteral( L
,"=(combiner)");
29 f
->is_vararg
= VARARG_ISVARARG
;
30 f
->code
= luaM_newvector(L
, pc
, Instruction
);
32 f
->p
= luaM_newvector( L
, n
, Proto
*);
34 for( i
= pc
= 0; i
< n
; i
++) {
35 int proto_idx
= i
-n
-1;
36 Proto
*p
= clvalue( L
->top
+ proto_idx
)->l
.p
;
38 f
->code
[pc
++] = CREATE_ABx( OP_CLOSURE
, 0, i
);
39 f
->code
[pc
++] = CREATE_ABx( OP_VARARG
, 1, 0);
40 f
->code
[pc
++] = CREATE_ABC( OP_CALL
, 0, 0, 1);
42 f
->code
[pc
++] = CREATE_ABC( OP_RETURN
, 0, 1, 0);
47 int luaopen_combine( lua_State
*L
) {
48 lua_pushcfunction( L
, lua_combine
);
49 lua_setglobal( L
, "combine");