~ubuntu-branches/ubuntu/trusty/luajit/trusty

« back to all changes in this revision

Viewing changes to src/lib_aux.c

  • Committer: Package Import Robot
  • Author(s): Enrico Tassi
  • Date: 2012-11-03 14:07:56 UTC
  • mfrom: (1.2.1) (15.1.1 experimental)
  • Revision ID: package-import@ubuntu.com-20121103140756-z0zcnyrwqlvuc2m5
Tags: 2.0.0+dfsg-1
New upstream release

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
2
** Auxiliary library for the Lua/C API.
3
 
** Copyright (C) 2005-2011 Mike Pall. See Copyright Notice in luajit.h
 
3
** Copyright (C) 2005-2012 Mike Pall. See Copyright Notice in luajit.h
4
4
**
5
5
** Major parts taken verbatim or adapted from the Lua interpreter.
6
6
** Copyright (C) 1994-2008 Lua.org, PUC-Rio. See Copyright Notice in lua.h
19
19
#include "lj_obj.h"
20
20
#include "lj_err.h"
21
21
#include "lj_state.h"
 
22
#include "lj_trace.h"
22
23
#include "lj_lib.h"
23
24
 
 
25
#if LJ_TARGET_POSIX
 
26
#include <sys/wait.h>
 
27
#endif
 
28
 
 
29
/* -- I/O error handling -------------------------------------------------- */
 
30
 
 
31
LUALIB_API int luaL_fileresult(lua_State *L, int stat, const char *fname)
 
32
{
 
33
  if (stat) {
 
34
    setboolV(L->top++, 1);
 
35
    return 1;
 
36
  } else {
 
37
    int en = errno;  /* Lua API calls may change this value. */
 
38
    setnilV(L->top++);
 
39
    if (fname)
 
40
      lua_pushfstring(L, "%s: %s", fname, strerror(en));
 
41
    else
 
42
      lua_pushfstring(L, "%s", strerror(en));
 
43
    setintV(L->top++, en);
 
44
    lj_trace_abort(G(L));
 
45
    return 3;
 
46
  }
 
47
}
 
48
 
 
49
LUALIB_API int luaL_execresult(lua_State *L, int stat)
 
50
{
 
51
  if (stat != -1) {
 
52
#if LJ_TARGET_POSIX
 
53
    if (WIFSIGNALED(stat)) {
 
54
      stat = WTERMSIG(stat);
 
55
      setnilV(L->top++);
 
56
      lua_pushliteral(L, "signal");
 
57
    } else {
 
58
      if (WIFEXITED(stat))
 
59
        stat = WEXITSTATUS(stat);
 
60
      if (stat == 0)
 
61
        setboolV(L->top++, 1);
 
62
      else
 
63
        setnilV(L->top++);
 
64
      lua_pushliteral(L, "exit");
 
65
    }
 
66
#else
 
67
    if (stat == 0)
 
68
      setboolV(L->top++, 1);
 
69
    else
 
70
      setnilV(L->top++);
 
71
    lua_pushliteral(L, "exit");
 
72
#endif
 
73
    setintV(L->top++, stat);
 
74
    return 3;
 
75
  }
 
76
  return luaL_fileresult(L, 0, NULL);
 
77
}
 
78
 
24
79
/* -- Module registration ------------------------------------------------- */
25
80
 
26
81
LUALIB_API const char *luaL_findtable(lua_State *L, int idx,
233
288
  }
234
289
}
235
290
 
236
 
/* -- Load Lua code ------------------------------------------------------- */
237
 
 
238
 
typedef struct FileReaderCtx {
239
 
  FILE *fp;
240
 
  char buf[LUAL_BUFFERSIZE];
241
 
} FileReaderCtx;
242
 
 
243
 
static const char *reader_file(lua_State *L, void *ud, size_t *size)
244
 
{
245
 
  FileReaderCtx *ctx = (FileReaderCtx *)ud;
246
 
  UNUSED(L);
247
 
  if (feof(ctx->fp)) return NULL;
248
 
  *size = fread(ctx->buf, 1, sizeof(ctx->buf), ctx->fp);
249
 
  return *size > 0 ? ctx->buf : NULL;
250
 
}
251
 
 
252
 
LUALIB_API int luaL_loadfile(lua_State *L, const char *filename)
253
 
{
254
 
  FileReaderCtx ctx;
255
 
  int status;
256
 
  const char *chunkname;
257
 
  if (filename) {
258
 
    ctx.fp = fopen(filename, "rb");
259
 
    if (ctx.fp == NULL) {
260
 
      lua_pushfstring(L, "cannot open %s: %s", filename, strerror(errno));
261
 
      return LUA_ERRFILE;
262
 
    }
263
 
    chunkname = lua_pushfstring(L, "@%s", filename);
264
 
  } else {
265
 
    ctx.fp = stdin;
266
 
    chunkname = "=stdin";
267
 
  }
268
 
  status = lua_load(L, reader_file, &ctx, chunkname);
269
 
  if (ferror(ctx.fp)) {
270
 
    L->top -= filename ? 2 : 1;
271
 
    lua_pushfstring(L, "cannot read %s: %s", chunkname+1, strerror(errno));
272
 
    if (filename)
273
 
      fclose(ctx.fp);
274
 
    return LUA_ERRFILE;
275
 
  }
276
 
  if (filename) {
277
 
    L->top--;
278
 
    copyTV(L, L->top-1, L->top);
279
 
    fclose(ctx.fp);
280
 
  }
281
 
  return status;
282
 
}
283
 
 
284
 
typedef struct StringReaderCtx {
285
 
  const char *str;
286
 
  size_t size;
287
 
} StringReaderCtx;
288
 
 
289
 
static const char *reader_string(lua_State *L, void *ud, size_t *size)
290
 
{
291
 
  StringReaderCtx *ctx = (StringReaderCtx *)ud;
292
 
  UNUSED(L);
293
 
  if (ctx->size == 0) return NULL;
294
 
  *size = ctx->size;
295
 
  ctx->size = 0;
296
 
  return ctx->str;
297
 
}
298
 
 
299
 
LUALIB_API int luaL_loadbuffer(lua_State *L, const char *buf, size_t size,
300
 
                               const char *name)
301
 
{
302
 
  StringReaderCtx ctx;
303
 
  ctx.str = buf;
304
 
  ctx.size = size;
305
 
  return lua_load(L, reader_string, &ctx, name);
306
 
}
307
 
 
308
 
LUALIB_API int luaL_loadstring(lua_State *L, const char *s)
309
 
{
310
 
  return luaL_loadbuffer(L, s, strlen(s), s);
311
 
}
312
 
 
313
291
/* -- Default allocator and panic function -------------------------------- */
314
292
 
315
293
static int panic(lua_State *L)
316
294
{
317
 
  fprintf(stderr, "PANIC: unprotected error in call to Lua API (%s)\n",
318
 
          lua_tostring(L, -1));
 
295
  const char *s = lua_tostring(L, -1);
 
296
  fputs("PANIC: unprotected error in call to Lua API (", stderr);
 
297
  fputs(s ? s : "?", stderr);
 
298
  fputc(')', stderr); fputc('\n', stderr);
 
299
  fflush(stderr);
319
300
  return 0;
320
301
}
321
302
 
366
347
LUA_API lua_State *lua_newstate(lua_Alloc f, void *ud)
367
348
{
368
349
  UNUSED(f); UNUSED(ud);
369
 
  fprintf(stderr, "Must use luaL_newstate() for 64 bit target\n");
 
350
  fputs("Must use luaL_newstate() for 64 bit target\n", stderr);
370
351
  return NULL;
371
352
}
372
353
#endif