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

tarantool / luajit / 6026444715

30 Aug 2023 02:43PM UTC coverage: 87.808% (-0.01%) from 87.821%
6026444715

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 in 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 these changes.

Closes tarantool/tarantool#5688

5343 of 6008 branches covered (0.0%)

Branch coverage included in aggregate %.

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

20506 of 23430 relevant lines covered (87.52%)

883672.17 hits per line

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

91.93
/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)
9✔
83
{
84
  if (pname) { fputs(pname, stderr); fputc(':', stderr); fputc(' ', stderr); }
9✔
85
  fputs(msg, stderr); fputc('\n', stderr);
9✔
86
  fflush(stderr);
9✔
87
}
9✔
88

89
static int report(lua_State *L, int status)
507✔
90
{
91
  if (status && !lua_isnil(L, -1)) {
507✔
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;
507✔
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)
435✔
114
{
115
  int status;
435✔
116
  int base = lua_gettop(L) - narg;  /* function index */
435✔
117
  lua_pushcfunction(L, traceback);  /* push traceback function */
435✔
118
  lua_insert(L, base);  /* put it under chunk and args */
435✔
119
#if !LJ_TARGET_CONSOLE
120
  signal(SIGINT, laction);
435✔
121
#endif
122
  status = lua_pcall(L, narg, (clear ? 0 : LUA_MULTRET), base);
626✔
123
#if !LJ_TARGET_CONSOLE
124
  signal(SIGINT, SIG_DFL);
347✔
125
#endif
126
  lua_remove(L, base);  /* remove traceback function */
347✔
127
  /* force a complete garbage collection in case of errors */
128
  if (status != LUA_OK) lua_gc(L, LUA_GCCOLLECT, 0);
347✔
129
  return status;
347✔
130
}
131

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

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

156
static void createargtable(lua_State *L, char **argv, int argc, int argf)
224✔
157
{
158
  int i;
224✔
159
  lua_createtable(L, argc - argf, argf);
224✔
160
  for (i = 0; i < argc; i++) {
1,459✔
161
    lua_pushstring(L, argv[i]);
1,011✔
162
    lua_rawseti(L, -2, i - argf);
1,011✔
163
  }
164
  lua_setglobal(L, "arg");
224✔
165
}
224✔
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)
133✔
187
{
188
  const char *p;
133✔
189
  lua_getfield(L, LUA_GLOBALSINDEX, firstline ? "_PROMPT" : "_PROMPT2");
242✔
190
  p = lua_tostring(L, -1);
133✔
191
  if (p == NULL) p = firstline ? LUA_PROMPT : LUA_PROMPT2;
133✔
192
  fputs(p, stdout);
133✔
193
  fflush(stdout);
133✔
194
  lua_pop(L, 1);  /* remove global */
133✔
195
}
133✔
196

197
static int incomplete(lua_State *L, int status)
128✔
198
{
199
  if (status == LUA_ERRSYNTAX) {
128✔
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)
133✔
212
{
213
  char buf[LUA_MAXINPUT];
133✔
214
  write_prompt(L, firstline);
133✔
215
  if (fgets(buf, LUA_MAXINPUT, stdin)) {
133✔
216
    size_t len = strlen(buf);
128✔
217
    if (len > 0 && buf[len-1] == '\n')
128✔
218
      buf[len-1] = '\0';
126✔
219
    if (firstline && buf[0] == '=')
128✔
220
      lua_pushfstring(L, "return %s", buf+1);
5✔
221
    else
222
      lua_pushstring(L, buf);
123✔
223
    return 1;
128✔
224
  }
225
  return 0;
226
}
227

228
static int loadline(lua_State *L)
24✔
229
{
230
  int status;
24✔
231
  lua_settop(L, 0);
24✔
232
  if (!pushline(L, 1))
24✔
233
    return -1;  /* no input */
234
  for (;;) {  /* repeat until gets a complete line */
237✔
235
    status = luaL_loadbuffer(L, lua_tostring(L, 1), lua_strlen(L, 1), "=stdin");
128✔
236
    if (!incomplete(L, status)) break;  /* cannot try to add lines? */
128✔
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 */
19✔
244
  return status;
19✔
245
}
246

247
static void dotty(lua_State *L)
5✔
248
{
249
  int status;
5✔
250
  const char *oldprogname = progname;
5✔
251
  progname = NULL;
5✔
252
  while ((status = loadline(L)) != -1) {
29✔
253
    if (status == LUA_OK) status = docall(L, 0, 0);
19✔
254
    report(L, status);
19✔
255
    if (status == LUA_OK && lua_gettop(L) > 0) {  /* any result to print? */
19✔
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 */
5✔
265
  fputs("\n", stdout);
5✔
266
  fflush(stdout);
5✔
267
  progname = oldprogname;
5✔
268
}
5✔
269

270
static int handle_script(lua_State *L, char **argx)
173✔
271
{
272
  int status;
173✔
273
  const char *fname = argx[0];
173✔
274
  if (strcmp(fname, "-") == 0 && strcmp(argx[-1], "--") != 0)
173✔
275
    fname = NULL;  /* stdin */
4✔
276
  status = luaL_loadfile(L, fname);
173✔
277
  if (status == LUA_OK) {
173✔
278
    /* Fetch args from arg table. LUA_INIT or -e might have changed them. */
279
    int narg = 0;
172✔
280
    lua_getglobal(L, "arg");
172✔
281
    if (lua_istable(L, -1)) {
172✔
282
      do {
230✔
283
        narg++;
230✔
284
        lua_rawgeti(L, -narg, narg);
230✔
285
      } while (!lua_isnil(L, -1));
230✔
286
      lua_pop(L, 1);
172✔
287
      lua_remove(L, -narg);
172✔
288
      narg--;
172✔
289
    } else {
290
      lua_pop(L, 1);
×
291
    }
292
    status = docall(L, narg, 0);
172✔
293
  }
294
  return report(L, status);
88✔
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) {
×
372
      if (!strncmp(msg, "module ", 7))
×
373
        msg = "unknown luaJIT command or tools not installed";
×
374
      l_message(progname, msg);
×
375
    }
376
    return 1;
×
377
  }
378
  lua_getglobal(L, "arg");
8✔
379
  return report(L, lua_pcall(L, 1, 1, 0));
8✔
380
}
381

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

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

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

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

428
#define FLAGS_INTERACTIVE        1
429
#define FLAGS_VERSION                2
430
#define FLAGS_EXEC                4
431
#define FLAGS_OPTION                8
432
#define FLAGS_NOENV                16
433
#define FLAGS_TOOL                32
434

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

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

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

546
static struct Smain {
547
  char **argv;
548
  int argc;
549
  int status;
550
} smain;
551

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

561
  LUAJIT_VERSION_SYM();  /* Linker-enforced version check. */
230✔
562

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

570
  if ((flags & FLAGS_NOENV)) {
224✔
571
    lua_pushboolean(L, 1);
1✔
572
    lua_setfield(L, LUA_REGISTRYINDEX, "LUA_NOENV");
1✔
573
  }
574

575
  /* Stop collector during library initialization. */
576
  lua_gc(L, LUA_GCSTOP, 0);
224✔
577
  luaL_openlibs(L);
224✔
578
  lua_gc(L, LUA_GCRESTART, -1);
224✔
579

580
  createargtable(L, argv, s->argc, (flags & FLAGS_TOOL) ? argn - 1 : argn);
224✔
581

582
  if (!(flags & FLAGS_NOENV)) {
224✔
583
    s->status = handle_luainit(L);
223✔
584
    if (s->status != LUA_OK) return 0;
223✔
585
  }
586

587
  if ((flags & FLAGS_VERSION)) print_version();
224✔
588

589
  s->status = runargs(L, argv, argn);
224✔
590
  if (s->status != LUA_OK) return 0;
215✔
591

592
  if (s->argc > argn) {
198✔
593
    s->status = handle_script(L, argv + argn);
173✔
594
    if (s->status != LUA_OK) return 0;
88✔
595
  }
596

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

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

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