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

tarantool / luajit / 6035198545

31 Aug 2023 08:55AM UTC coverage: 88.225% (+0.4%) from 87.822%
6035198545

push

github

fckxorg
test: don't skip tool CLI flag for tarantool

That skipcond was introduced to overcome the obstacles
of LuaJIT's integration testing in Tarantool. Since
the required patch is now in the Tarantool master, this
skipcond is now unnecessary.

Related to tarantool/tarantool#5688

5340 of 5975 branches covered (0.0%)

Branch coverage included in aggregate %.

20495 of 23308 relevant lines covered (87.93%)

1297339.67 hits per line

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

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

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

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

113
static int docall(lua_State *L, int narg, int clear)
455✔
114
{
115
  int status;
455✔
116
  int base = lua_gettop(L) - narg;  /* function index */
455✔
117
  lua_pushcfunction(L, traceback);  /* push traceback function */
455✔
118
  lua_insert(L, base);  /* put it under chunk and args */
455✔
119
#if !LJ_TARGET_CONSOLE
120
  signal(SIGINT, laction);
455✔
121
#endif
122
  status = lua_pcall(L, narg, (clear ? 0 : LUA_MULTRET), base);
655✔
123
#if !LJ_TARGET_CONSOLE
124
  signal(SIGINT, SIG_DFL);
356✔
125
#endif
126
  lua_remove(L, base);  /* remove traceback function */
356✔
127
  /* force a complete garbage collection in case of errors */
128
  if (status != LUA_OK) lua_gc(L, LUA_GCCOLLECT, 0);
356✔
129
  return status;
356✔
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)
234✔
157
{
158
  int i;
234✔
159
  lua_createtable(L, argc - argf, argf);
234✔
160
  for (i = 0; i < argc; i++) {
1,517✔
161
    lua_pushstring(L, argv[i]);
1,049✔
162
    lua_rawseti(L, -2, i - argf);
1,049✔
163
  }
164
  lua_setglobal(L, "arg");
234✔
165
}
234✔
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)
199✔
174
{
175
  int status = luaL_loadbuffer(L, s, strlen(s), name) || docall(L, 0, 1);
199✔
176
  return report(L, status);
196✔
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)
183✔
271
{
272
  int status;
183✔
273
  const char *fname = argx[0];
183✔
274
  if (strcmp(fname, "-") == 0 && strcmp(argx[-1], "--") != 0)
183✔
275
    fname = NULL;  /* stdin */
4✔
276
  status = luaL_loadfile(L, fname);
183✔
277
  if (status == LUA_OK) {
183✔
278
    /* Fetch args from arg table. LUA_INIT or -e might have changed them. */
279
    int narg = 0;
182✔
280
    lua_getglobal(L, "arg");
182✔
281
    if (lua_istable(L, -1)) {
182✔
282
      do {
240✔
283
        narg++;
240✔
284
        lua_rawgeti(L, -narg, narg);
240✔
285
      } while (!lua_isnil(L, -1));
240✔
286
      lua_pop(L, 1);
182✔
287
      lua_remove(L, -narg);
182✔
288
      narg--;
182✔
289
    } else {
290
      lua_pop(L, 1);
×
291
    }
292
    status = docall(L, narg, 0);
182✔
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)
7✔
366
{
367
  lua_getglobal(L, "require");
7✔
368
  lua_pushstring(L, tool_name);
7✔
369
  if (lua_pcall(L, 1, 1, 0)) {
7✔
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");
7✔
379
  return report(L, lua_pcall(L, 1, 1, 0));
7✔
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)
240✔
436
{
437
  int i;
240✔
438
  for (i = 1; argv[i] != NULL; i++) {
517✔
439
    if (argv[i][0] != '-')  /* Not an option? */
486✔
440
      return i;
180✔
441
    switch (argv[i][1]) {  /* Check option. */
306✔
442
    case '-':
3✔
443
      notail(argv[i]);
3✔
444
      return i+1;
2✔
445
    case '\0':
446
      return i;
447
    case 'i':
4✔
448
      notail(argv[i]);
4✔
449
      *flags |= FLAGS_INTERACTIVE;
4✔
450
      /* fallthrough */
451
    case 'v':
8✔
452
      notail(argv[i]);
8✔
453
      *flags |= FLAGS_VERSION;
8✔
454
      break;
8✔
455
    case 't':
9✔
456
      *flags |= FLAGS_TOOL;
9✔
457
      if (argv[i][2] == '\0') return -1;
9✔
458
      if (argv[i + 1] == NULL) return -1;
8✔
459
      return i + 1;
8✔
460
    case 'e':
202✔
461
      *flags |= FLAGS_EXEC;
202✔
462
      /* fallthrough */
463
    case 'j':  /* LuaJIT extension */
263✔
464
    case 'l':
465
      *flags |= FLAGS_OPTION;
263✔
466
      if (argv[i][2] == '\0') {
263✔
467
        i++;
249✔
468
        if (argv[i] == NULL) return -1;
249✔
469
      }
470
      break;
471
    case 'O': break;  /* LuaJIT extension */
472
    case 'b':  /* LuaJIT extension */
9✔
473
      if (*flags) return -1;
9✔
474
      *flags |= FLAGS_EXEC;
9✔
475
      return i+1;
9✔
476
    case 'E':
1✔
477
      *flags |= FLAGS_NOENV;
1✔
478
      break;
1✔
479
    default: return -1;  /* invalid option */
480
    }
481
  }
482
  return i;
483
}
484

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

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

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

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

562
  LUAJIT_VERSION_SYM();  /* Linker-enforced version check. */
240✔
563

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

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

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

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

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

588
  if ((flags & FLAGS_VERSION)) print_version();
234✔
589

590
  s->status = runargs(L, argv, argn);
234✔
591
  if (s->status != LUA_OK) return 0;
226✔
592

593
  if (s->argc > argn) {
208✔
594
    s->status = handle_script(L, argv + argn);
183✔
595
    if (s->status != LUA_OK) return 0;
87✔
596
  }
597

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

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

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