• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

tarantool / luajit / 5953029851

23 Aug 2023 02:56PM UTC coverage: 87.838% (+0.02%) from 87.821%
5953029851

push

github

fckxorg
tools: add cli flag to run profile dump parsers

It is really inconvenient to use a standalone shell script to parse
binary dumps from profilers. That is why this commit introduces a
CLI flag for tools to the LuaJIT, so now it is possible to parse
a memprof dump as simple as:
```
luajit -tm memprof.bin
luajit -tm --leak-only memprof.bin
```

And the sysprof too:
```
luajit -ts sysprof.bin
luajit -ts --split sysprof.bin
```

The `luajit-parse-memprof` and `luajit-parse-sysprof` are purged
as a result of this changes.

Closes tarantool/tarantool#5688

5344 of 6007 branches covered (88.96%)

Branch coverage included in aggregate %.

19 of 19 new or added lines in 1 file covered. (100.0%)

20511 of 23428 relevant lines covered (87.55%)

877087.5 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

92.55
/src/luajit.c
1
/*
2
** LuaJIT frontend. Runs commands, scripts, read-eval-print (REPL) etc.
3
** Copyright (C) 2005-2017 Mike Pall. See Copyright Notice in luajit.h
4
**
5
** Major portions taken verbatim or adapted from the Lua interpreter.
6
** Copyright (C) 1994-2008 Lua.org, PUC-Rio. See Copyright Notice in lua.h
7
*/
8

9
#include <stdio.h>
10
#include <stdlib.h>
11
#include <string.h>
12

13
#define luajit_c
14

15
#include "lua.h"
16
#include "lauxlib.h"
17
#include "lualib.h"
18
#include "luajit.h"
19

20
#include "lj_arch.h"
21

22
#if LJ_TARGET_POSIX
23
#include <unistd.h>
24
#define lua_stdin_is_tty()        isatty(0)
25
#elif LJ_TARGET_WINDOWS
26
#include <io.h>
27
#ifdef __BORLANDC__
28
#define lua_stdin_is_tty()        isatty(_fileno(stdin))
29
#else
30
#define lua_stdin_is_tty()        _isatty(_fileno(stdin))
31
#endif
32
#else
33
#define lua_stdin_is_tty()        1
34
#endif
35

36
#if !LJ_TARGET_CONSOLE
37
#include <signal.h>
38
#endif
39

40
static lua_State *globalL = NULL;
41
static const char *progname = LUA_PROGNAME;
42

43
#if !LJ_TARGET_CONSOLE
44
static void lstop(lua_State *L, lua_Debug *ar)
×
45
{
46
  (void)ar;  /* unused arg. */
×
47
  lua_sethook(L, NULL, 0, 0);
×
48
  /* Avoid luaL_error -- a C hook doesn't add an extra frame. */
49
  luaL_where(L, 0);
×
50
  lua_pushfstring(L, "%sinterrupted!", lua_tostring(L, -1));
×
51
  lua_error(L);
×
52
}
×
53

54
static void laction(int i)
×
55
{
56
  signal(i, SIG_DFL); /* if another SIGINT happens before lstop,
×
57
                         terminate process (default action) */
58
  lua_sethook(globalL, lstop, LUA_MASKCALL | LUA_MASKRET | LUA_MASKCOUNT, 1);
×
59
}
×
60
#endif
61

62
static void print_usage(void)
6✔
63
{
64
  fputs("usage: ", stderr);
6✔
65
  fputs(progname, stderr);
6✔
66
  fputs(" [options]... [script [args]...].\n"
6✔
67
  "Available options are:\n"
68
  "  -e chunk  Execute string " LUA_QL("chunk") ".\n"
69
  "  -l name   Require library " LUA_QL("name") ".\n"
70
  "  -b ...    Save or list bytecode.\n"
71
  "  -j cmd    Perform LuaJIT control command.\n"
72
  "  -O[opt]   Control LuaJIT optimizations.\n"
73
  "  -i        Enter interactive mode after executing " LUA_QL("script") ".\n"
74
  "  -v        Show version information.\n"
75
  "  -t<cmd>   Execute tool.\n"
76
  "  -E        Ignore environment variables.\n"
77
  "  --        Stop handling options.\n"
78
  "  -         Execute stdin and stop handling options.\n", stderr);
79
  fflush(stderr);
6✔
80
}
6✔
81

82
static void l_message(const char *pname, const char *msg)
8✔
83
{
84
  if (pname) { fputs(pname, stderr); fputc(':', stderr); fputc(' ', stderr); }
8✔
85
  fputs(msg, stderr); fputc('\n', stderr);
8✔
86
  fflush(stderr);
8✔
87
}
8✔
88

89
static int report(lua_State *L, int status)
504✔
90
{
91
  if (status && !lua_isnil(L, -1)) {
504✔
92
    const char *msg = lua_tostring(L, -1);
7✔
93
    if (msg == NULL) msg = "(error object is not a string)";
7✔
94
    l_message(progname, msg);
7✔
95
    lua_pop(L, 1);
7✔
96
  }
97
  return status;
504✔
98
}
99

100
static int traceback(lua_State *L)
3✔
101
{
102
  if (!lua_isstring(L, 1)) { /* Non-string error object? Try metamethod. */
3✔
103
    if (lua_isnoneornil(L, 1) ||
2✔
104
        !luaL_callmeta(L, 1, "__tostring") ||
1✔
105
        !lua_isstring(L, -1))
×
106
      return 1;  /* Return non-string error object. */
1✔
107
    lua_remove(L, 1);  /* Replace object by result of __tostring metamethod. */
×
108
  }
109
  luaL_traceback(L, L, lua_tostring(L, 1), 1);
2✔
110
  return 1;
2✔
111
}
112

113
static int docall(lua_State *L, int narg, int clear)
433✔
114
{
115
  int status;
433✔
116
  int base = lua_gettop(L) - narg;  /* function index */
433✔
117
  lua_pushcfunction(L, traceback);  /* push traceback function */
433✔
118
  lua_insert(L, base);  /* put it under chunk and args */
433✔
119
#if !LJ_TARGET_CONSOLE
120
  signal(SIGINT, laction);
433✔
121
#endif
122
  status = lua_pcall(L, narg, (clear ? 0 : LUA_MULTRET), base);
622✔
123
#if !LJ_TARGET_CONSOLE
124
  signal(SIGINT, SIG_DFL);
345✔
125
#endif
126
  lua_remove(L, base);  /* remove traceback function */
345✔
127
  /* force a complete garbage collection in case of errors */
128
  if (status != LUA_OK) lua_gc(L, LUA_GCCOLLECT, 0);
345✔
129
  return status;
345✔
130
}
131

132
static void print_version(void)
8✔
133
{
134
  fputs(LUAJIT_VERSION " -- " LUAJIT_COPYRIGHT ". " LUAJIT_URL "\n", stdout);
8✔
135
}
8✔
136

137
static void print_jit_status(lua_State *L)
4✔
138
{
139
  int n;
4✔
140
  const char *s;
4✔
141
  lua_getfield(L, LUA_REGISTRYINDEX, "_LOADED");
4✔
142
  lua_getfield(L, -1, "jit");  /* Get jit.* module table. */
4✔
143
  lua_remove(L, -2);
4✔
144
  lua_getfield(L, -1, "status");
4✔
145
  lua_remove(L, -2);
4✔
146
  n = lua_gettop(L);
4✔
147
  lua_call(L, 0, LUA_MULTRET);
4✔
148
  fputs(lua_toboolean(L, n) ? "JIT: ON" : "JIT: OFF", stdout);
4✔
149
  for (n++; (s = lua_tostring(L, n)); n++) {
60✔
150
    putc(' ', stdout);
56✔
151
    fputs(s, stdout);
56✔
152
  }
153
  putc('\n', stdout);
4✔
154
}
4✔
155

156
static void createargtable(lua_State *L, char **argv, int argc, int argf)
223✔
157
{
158
  int i;
223✔
159
  lua_createtable(L, argc - argf, argf);
223✔
160
  for (i = 0; i < argc; i++) {
1,454✔
161
    lua_pushstring(L, argv[i]);
1,008✔
162
    lua_rawseti(L, -2, i - argf);
1,008✔
163
  }
164
  lua_setglobal(L, "arg");
223✔
165
}
223✔
166

167
static int dofile(lua_State *L, const char *name)
1✔
168
{
169
  int status = luaL_loadfile(L, name) || docall(L, 0, 1);
1✔
170
  return report(L, status);
1✔
171
}
172

173
static int dostring(lua_State *L, const char *s, const char *name)
188✔
174
{
175
  int status = luaL_loadbuffer(L, s, strlen(s), name) || docall(L, 0, 1);
188✔
176
  return report(L, status);
185✔
177
}
178

179
static int dolibrary(lua_State *L, const char *name)
57✔
180
{
181
  lua_getglobal(L, "require");
57✔
182
  lua_pushstring(L, name);
57✔
183
  return report(L, docall(L, 1, 1));
57✔
184
}
185

186
static void write_prompt(lua_State *L, int firstline)
131✔
187
{
188
  const char *p;
131✔
189
  lua_getfield(L, LUA_GLOBALSINDEX, firstline ? "_PROMPT" : "_PROMPT2");
240✔
190
  p = lua_tostring(L, -1);
131✔
191
  if (p == NULL) p = firstline ? LUA_PROMPT : LUA_PROMPT2;
131✔
192
  fputs(p, stdout);
131✔
193
  fflush(stdout);
131✔
194
  lua_pop(L, 1);  /* remove global */
131✔
195
}
131✔
196

197
static int incomplete(lua_State *L, int status)
127✔
198
{
199
  if (status == LUA_ERRSYNTAX) {
127✔
200
    size_t lmsg;
109✔
201
    const char *msg = lua_tolstring(L, -1, &lmsg);
109✔
202
    const char *tp = msg + lmsg - (sizeof(LUA_QL("<eof>")) - 1);
109✔
203
    if (strstr(msg, LUA_QL("<eof>")) == tp) {
109✔
204
      lua_pop(L, 1);
109✔
205
      return 1;
109✔
206
    }
207
  }
208
  return 0;  /* else... */
209
}
210

211
static int pushline(lua_State *L, int firstline)
131✔
212
{
213
  char buf[LUA_MAXINPUT];
131✔
214
  write_prompt(L, firstline);
131✔
215
  if (fgets(buf, LUA_MAXINPUT, stdin)) {
131✔
216
    size_t len = strlen(buf);
127✔
217
    if (len > 0 && buf[len-1] == '\n')
127✔
218
      buf[len-1] = '\0';
125✔
219
    if (firstline && buf[0] == '=')
127✔
220
      lua_pushfstring(L, "return %s", buf+1);
5✔
221
    else
222
      lua_pushstring(L, buf);
122✔
223
    return 1;
127✔
224
  }
225
  return 0;
226
}
227

228
static int loadline(lua_State *L)
22✔
229
{
230
  int status;
22✔
231
  lua_settop(L, 0);
22✔
232
  if (!pushline(L, 1))
22✔
233
    return -1;  /* no input */
234
  for (;;) {  /* repeat until gets a complete line */
236✔
235
    status = luaL_loadbuffer(L, lua_tostring(L, 1), lua_strlen(L, 1), "=stdin");
127✔
236
    if (!incomplete(L, status)) break;  /* cannot try to add lines? */
127✔
237
    if (!pushline(L, 0))  /* no more input? */
109✔
238
      return -1;
239
    lua_pushliteral(L, "\n");  /* add a new line... */
109✔
240
    lua_insert(L, -2);  /* ...between the two lines */
109✔
241
    lua_concat(L, 3);  /* join them */
109✔
242
  }
243
  lua_remove(L, 1);  /* remove line */
18✔
244
  return status;
18✔
245
}
246

247
static void dotty(lua_State *L)
4✔
248
{
249
  int status;
4✔
250
  const char *oldprogname = progname;
4✔
251
  progname = NULL;
4✔
252
  while ((status = loadline(L)) != -1) {
26✔
253
    if (status == LUA_OK) status = docall(L, 0, 0);
18✔
254
    report(L, status);
18✔
255
    if (status == LUA_OK && lua_gettop(L) > 0) {  /* any result to print? */
18✔
256
      lua_getglobal(L, "print");
5✔
257
      lua_insert(L, 1);
5✔
258
      if (lua_pcall(L, lua_gettop(L)-1, 0, 0) != 0)
5✔
259
        l_message(progname,
×
260
          lua_pushfstring(L, "error calling " LUA_QL("print") " (%s)",
261
                              lua_tostring(L, -1)));
262
    }
263
  }
264
  lua_settop(L, 0);  /* clear stack */
4✔
265
  fputs("\n", stdout);
4✔
266
  fflush(stdout);
4✔
267
  progname = oldprogname;
4✔
268
}
4✔
269

270
static int handle_script(lua_State *L, char **argx)
172✔
271
{
272
  int status;
172✔
273
  const char *fname = argx[0];
172✔
274
  if (strcmp(fname, "-") == 0 && strcmp(argx[-1], "--") != 0)
172✔
275
    fname = NULL;  /* stdin */
4✔
276
  status = luaL_loadfile(L, fname);
172✔
277
  if (status == LUA_OK) {
172✔
278
    /* Fetch args from arg table. LUA_INIT or -e might have changed them. */
279
    int narg = 0;
171✔
280
    lua_getglobal(L, "arg");
171✔
281
    if (lua_istable(L, -1)) {
171✔
282
      do {
229✔
283
        narg++;
229✔
284
        lua_rawgeti(L, -narg, narg);
229✔
285
      } while (!lua_isnil(L, -1));
229✔
286
      lua_pop(L, 1);
171✔
287
      lua_remove(L, -narg);
171✔
288
      narg--;
171✔
289
    } else {
290
      lua_pop(L, 1);
×
291
    }
292
    status = docall(L, narg, 0);
171✔
293
  }
294
  return report(L, status);
87✔
295
}
296

297
/* Load add-on module. */
298
static int loadjitmodule(lua_State *L)
10✔
299
{
300
  lua_getglobal(L, "require");
10✔
301
  lua_pushliteral(L, "jit.");
10✔
302
  lua_pushvalue(L, -3);
10✔
303
  lua_concat(L, 2);
10✔
304
  if (lua_pcall(L, 1, 1, 0)) {
10✔
305
    const char *msg = lua_tostring(L, -1);
1✔
306
    if (msg && !strncmp(msg, "module ", 7))
1✔
307
      goto nomodule;
1✔
308
    return report(L, 1);
×
309
  }
310
  lua_getfield(L, -1, "start");
9✔
311
  if (lua_isnil(L, -1)) {
9✔
312
  nomodule:
×
313
    l_message(progname,
1✔
314
              "unknown luaJIT command or jit.* modules not installed");
315
    return 1;
1✔
316
  }
317
  lua_remove(L, -2);  /* Drop module table. */
9✔
318
  return 0;
9✔
319
}
320

321
/* Run command with options. */
322
static int runcmdopt(lua_State *L, const char *opt)
10✔
323
{
324
  int narg = 0;
10✔
325
  if (opt && *opt) {
10✔
326
    for (;;) {  /* Split arguments. */
10✔
327
      const char *p = strchr(opt, ',');
8✔
328
      narg++;
8✔
329
      if (!p) break;
8✔
330
      if (p == opt)
2✔
331
        lua_pushnil(L);
×
332
      else
333
        lua_pushlstring(L, opt, (size_t)(p - opt));
2✔
334
      opt = p + 1;
2✔
335
    }
336
    if (*opt)
6✔
337
      lua_pushstring(L, opt);
6✔
338
    else
339
      lua_pushnil(L);
×
340
  }
341
  return report(L, lua_pcall(L, narg, 0, 0));
10✔
342
}
343

344
/* JIT engine control command: try jit library first or load add-on module. */
345
static int dojitcmd(lua_State *L, const char *cmd)
4✔
346
{
347
  const char *opt = strchr(cmd, '=');
4✔
348
  lua_pushlstring(L, cmd, opt ? (size_t)(opt - cmd) : strlen(cmd));
4✔
349
  lua_getfield(L, LUA_REGISTRYINDEX, "_LOADED");
4✔
350
  lua_getfield(L, -1, "jit");  /* Get jit.* module table. */
4✔
351
  lua_remove(L, -2);
4✔
352
  lua_pushvalue(L, -2);
4✔
353
  lua_gettable(L, -2);  /* Lookup library function. */
4✔
354
  if (!lua_isfunction(L, -1)) {
4✔
355
    lua_pop(L, 2);  /* Drop non-function and jit.* table, keep module name. */
1✔
356
    if (loadjitmodule(L))
1✔
357
      return 1;
358
  } else {
359
    lua_remove(L, -2);  /* Drop jit.* table. */
3✔
360
  }
361
  lua_remove(L, -2);  /* Drop module name. */
3✔
362
  return runcmdopt(L, opt ? opt+1 : opt);
3✔
363
}
364

365
static int runtoolcmd(lua_State *L, const char *tool_name)
8✔
366
{
367
  lua_getglobal(L, "require");
8✔
368
  lua_pushstring(L, tool_name);
8✔
369
  if (lua_pcall(L, 1, 1, 0)) {
8✔
370
    const char *msg = lua_tostring(L, -1);
×
371
    if (msg && !strncmp(msg, "module ", 7))
×
372
      l_message(progname,
×
373
        "unknown luaJIT command or tools not installed");
374
    return 1;
×
375
  }
376
  lua_getglobal(L, "arg");
8✔
377
  return report(L, lua_pcall(L, 1, 1, 0));
8✔
378
}
379

380
static int dotoolcmd(lua_State *L, const char *cmd)
381
{
382
  switch (cmd[0]) {
383
  case 'm':
384
    return runtoolcmd(L, "memprof");
385
  case 's':
386
    return runtoolcmd(L, "sysprof");
387
  default:
388
    break;
389
  }
390
  return -1;
391
}
392

393
/* Optimization flags. */
394
static int dojitopt(lua_State *L, const char *opt)
7✔
395
{
396
  lua_getfield(L, LUA_REGISTRYINDEX, "_LOADED");
7✔
397
  lua_getfield(L, -1, "jit.opt");  /* Get jit.opt.* module table. */
7✔
398
  lua_remove(L, -2);
7✔
399
  lua_getfield(L, -1, "start");
7✔
400
  lua_remove(L, -2);
7✔
401
  return runcmdopt(L, opt);
7✔
402
}
403

404
/* Save or list bytecode. */
405
static int dobytecode(lua_State *L, char **argv)
9✔
406
{
407
  int narg = 0;
9✔
408
  lua_pushliteral(L, "bcsave");
9✔
409
  if (loadjitmodule(L))
9✔
410
    return 1;
411
  if (argv[0][2]) {
9✔
412
    narg++;
4✔
413
    argv[0][1] = '-';
4✔
414
    lua_pushstring(L, argv[0]+1);
4✔
415
  }
416
  for (argv++; *argv != NULL; narg++, argv++)
29✔
417
    lua_pushstring(L, *argv);
20✔
418
  report(L, lua_pcall(L, narg, 0, 0));
9✔
419
  return -1;
9✔
420
}
421

422
/* check that argument has no extra characters at the end */
423
#define notail(x)        {if ((x)[2] != '\0') return -1;}
424

425
#define FLAGS_INTERACTIVE        1
426
#define FLAGS_VERSION                2
427
#define FLAGS_EXEC                4
428
#define FLAGS_OPTION                8
429
#define FLAGS_NOENV                16
430
#define FLAGS_TOOL                32
431

432
static int collectargs(char **argv, int *flags)
229✔
433
{
434
  int i;
229✔
435
  for (i = 1; argv[i] != NULL; i++) {
495✔
436
    if (argv[i][0] != '-')  /* Not an option? */
465✔
437
      return i;
169✔
438
    switch (argv[i][1]) {  /* Check option. */
296✔
439
    case '-':
3✔
440
      notail(argv[i]);
3✔
441
      return i+1;
2✔
442
    case '\0':
443
      return i;
444
    case 'i':
4✔
445
      notail(argv[i]);
4✔
446
      *flags |= FLAGS_INTERACTIVE;
4✔
447
      /* fallthrough */
448
    case 'v':
8✔
449
      notail(argv[i]);
8✔
450
      *flags |= FLAGS_VERSION;
8✔
451
      break;
8✔
452
    case 't':
10✔
453
      *flags |= FLAGS_TOOL;
10✔
454
      if (argv[i][2] == '\0') return -1;
10✔
455
      if (argv[i + 1] == NULL) return -1;
9✔
456
      return i + 1;
9✔
457
    case 'e':
191✔
458
      *flags |= FLAGS_EXEC;
191✔
459
    case 'j':  /* LuaJIT extension */
252✔
460
    case 'l':
461
      *flags |= FLAGS_OPTION;
252✔
462
      if (argv[i][2] == '\0') {
252✔
463
        i++;
238✔
464
        if (argv[i] == NULL) return -1;
238✔
465
      }
466
      break;
467
    case 'O': break;  /* LuaJIT extension */
468
    case 'b':  /* LuaJIT extension */
9✔
469
      if (*flags) return -1;
9✔
470
      *flags |= FLAGS_EXEC;
9✔
471
      return i+1;
9✔
472
    case 'E':
1✔
473
      *flags |= FLAGS_NOENV;
1✔
474
      break;
1✔
475
    default: return -1;  /* invalid option */
476
    }
477
  }
478
  return i;
479
}
480

481
static int runargs(lua_State *L, char **argv, int argn)
223✔
482
{
483
  int i;
223✔
484
  for (i = 1; i < argn; i++) {
482✔
485
    if (argv[i] == NULL) continue;
285✔
486
    lua_assert(argv[i][0] == '-');
285✔
487
    switch (argv[i][1]) {
285✔
488
    case 'e': {
188✔
489
      const char *chunk = argv[i] + 2;
188✔
490
      if (*chunk == '\0') chunk = argv[++i];
188✔
491
      lua_assert(chunk != NULL);
188✔
492
      if (dostring(L, chunk, "=(command line)") != 0)
188✔
493
        return 1;
494
      break;
495
      }
496
    case 'l': {
57✔
497
      const char *filename = argv[i] + 2;
57✔
498
      if (*filename == '\0') filename = argv[++i];
57✔
499
      lua_assert(filename != NULL);
57✔
500
      if (dolibrary(L, filename))
57✔
501
        return 1;
502
      break;
503
      }
504
    case 'j': {  /* LuaJIT extension. */
4✔
505
      const char *cmd = argv[i] + 2;
4✔
506
      if (*cmd == '\0') cmd = argv[++i];
4✔
507
      lua_assert(cmd != NULL);
4✔
508
      if (dojitcmd(L, cmd))
4✔
509
        return 1;
510
      break;
511
      }
512
    case 't': { /* Tarantool's fork extension. */
9✔
513
      const char *cmd = argv[i] + 2;
9✔
514
      return dotoolcmd(L, cmd) != LUA_OK;
9✔
515
    }
516
    case 'O':  /* LuaJIT extension. */
7✔
517
      if (dojitopt(L, argv[i] + 2))
7✔
518
        return 1;
519
      break;
520
    case 'b':  /* LuaJIT extension. */
9✔
521
      return dobytecode(L, argv+i);
9✔
522
    default: break;
523
    }
524
  }
525
  return LUA_OK;
526
}
527

528
static int handle_luainit(lua_State *L)
222✔
529
{
530
#if LJ_TARGET_CONSOLE
531
  const char *init = NULL;
532
#else
533
  const char *init = getenv(LUA_INIT);
222✔
534
#endif
535
  if (init == NULL)
222✔
536
    return LUA_OK;
537
  else if (init[0] == '@')
×
538
    return dofile(L, init+1);
×
539
  else
540
    return dostring(L, init, "=" LUA_INIT);
×
541
}
542

543
static struct Smain {
544
  char **argv;
545
  int argc;
546
  int status;
547
} smain;
548

549
static int pmain(lua_State *L)
229✔
550
{
551
  struct Smain *s = &smain;
229✔
552
  char **argv = s->argv;
229✔
553
  int argn;
229✔
554
  int flags = 0;
229✔
555
  globalL = L;
229✔
556
  if (argv[0] && argv[0][0]) progname = argv[0];
229✔
557

558
  LUAJIT_VERSION_SYM();  /* Linker-enforced version check. */
229✔
559

560
  argn = collectargs(argv, &flags);
229✔
561
  if (argn < 0) {  /* Invalid args? */
229✔
562
    print_usage();
6✔
563
    s->status = 1;
6✔
564
    return 0;
6✔
565
  }
566

567
  if ((flags & FLAGS_NOENV)) {
223✔
568
    lua_pushboolean(L, 1);
1✔
569
    lua_setfield(L, LUA_REGISTRYINDEX, "LUA_NOENV");
1✔
570
  }
571

572
  /* Stop collector during library initialization. */
573
  lua_gc(L, LUA_GCSTOP, 0);
223✔
574
  luaL_openlibs(L);
223✔
575
  lua_gc(L, LUA_GCRESTART, -1);
223✔
576

577
  createargtable(L, argv, s->argc, (flags & FLAGS_TOOL) ? argn - 1 : argn);
223✔
578

579
  if (!(flags & FLAGS_NOENV)) {
223✔
580
    s->status = handle_luainit(L);
222✔
581
    if (s->status != LUA_OK) return 0;
222✔
582
  }
583

584
  if ((flags & FLAGS_VERSION)) print_version();
223✔
585

586
  s->status = runargs(L, argv, argn);
223✔
587
  if (s->status != LUA_OK) return 0;
214✔
588

589
  if (s->argc > argn) {
197✔
590
    s->status = handle_script(L, argv + argn);
172✔
591
    if (s->status != LUA_OK) return 0;
87✔
592
  }
593

594
  if ((flags & FLAGS_INTERACTIVE)) {
111✔
595
    print_jit_status(L);
4✔
596
    dotty(L);
4✔
597
  } else if (s->argc == argn && !(flags & (FLAGS_EXEC|FLAGS_VERSION))) {
107✔
598
    if (lua_stdin_is_tty()) {
1✔
599
      print_version();
×
600
      print_jit_status(L);
×
601
      dotty(L);
×
602
    } else {
603
      dofile(L, NULL);  /* Executes stdin as a file. */
1✔
604
    }
605
  }
606
  return 0;
607
}
608

609
int main(int argc, char **argv)
229✔
610
{
611
  int status;
229✔
612
  lua_State *L = lua_open();
229✔
613
  if (L == NULL) {
229✔
614
    l_message(argv[0], "cannot create state: not enough memory");
×
615
    return EXIT_FAILURE;
×
616
  }
617
  smain.argc = argc;
229✔
618
  smain.argv = argv;
229✔
619
  status = lua_cpcall(L, pmain, NULL);
229✔
620
  report(L, status);
135✔
621
  lua_close(L);
135✔
622
  return (status || smain.status > 0) ? EXIT_FAILURE : EXIT_SUCCESS;
135✔
623
}
624

STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc