lj_load.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*
  2. ** Load and dump code.
  3. ** Copyright (C) 2005-2014 Mike Pall. See Copyright Notice in luajit.h
  4. */
  5. #include <errno.h>
  6. #include <stdio.h>
  7. #define lj_load_c
  8. #define LUA_CORE
  9. #include "lua.h"
  10. #include "lauxlib.h"
  11. #include "lj_obj.h"
  12. #include "lj_gc.h"
  13. #include "lj_err.h"
  14. #include "lj_str.h"
  15. #include "lj_func.h"
  16. #include "lj_frame.h"
  17. #include "lj_vm.h"
  18. #include "lj_lex.h"
  19. #include "lj_bcdump.h"
  20. #include "lj_parse.h"
  21. /* -- Load Lua source code and bytecode ----------------------------------- */
  22. static TValue *cpparser(lua_State *L, lua_CFunction dummy, void *ud)
  23. {
  24. LexState *ls = (LexState *)ud;
  25. GCproto *pt;
  26. GCfunc *fn;
  27. int bc;
  28. UNUSED(dummy);
  29. cframe_errfunc(L->cframe) = -1; /* Inherit error function. */
  30. bc = lj_lex_setup(L, ls);
  31. if (ls->mode && !strchr(ls->mode, bc ? 'b' : 't')) {
  32. setstrV(L, L->top++, lj_err_str(L, LJ_ERR_XMODE));
  33. lj_err_throw(L, LUA_ERRSYNTAX);
  34. }
  35. pt = bc ? lj_bcread(ls) : lj_parse(ls);
  36. fn = lj_func_newL_empty(L, pt, tabref(L->env));
  37. /* Don't combine above/below into one statement. */
  38. setfuncV(L, L->top++, fn);
  39. return NULL;
  40. }
  41. LUA_API int lua_loadx(lua_State *L, lua_Reader reader, void *data,
  42. const char *chunkname, const char *mode)
  43. {
  44. LexState ls;
  45. int status;
  46. ls.rfunc = reader;
  47. ls.rdata = data;
  48. ls.chunkarg = chunkname ? chunkname : "?";
  49. ls.mode = mode;
  50. lj_str_initbuf(&ls.sb);
  51. status = lj_vm_cpcall(L, NULL, &ls, cpparser);
  52. lj_lex_cleanup(L, &ls);
  53. lj_gc_check(L);
  54. return status;
  55. }
  56. LUA_API int lua_load(lua_State *L, lua_Reader reader, void *data,
  57. const char *chunkname)
  58. {
  59. return lua_loadx(L, reader, data, chunkname, NULL);
  60. }
  61. typedef struct FileReaderCtx {
  62. FILE *fp;
  63. char buf[LUAL_BUFFERSIZE];
  64. } FileReaderCtx;
  65. static const char *reader_file(lua_State *L, void *ud, size_t *size)
  66. {
  67. FileReaderCtx *ctx = (FileReaderCtx *)ud;
  68. UNUSED(L);
  69. if (feof(ctx->fp)) return NULL;
  70. *size = fread(ctx->buf, 1, sizeof(ctx->buf), ctx->fp);
  71. return *size > 0 ? ctx->buf : NULL;
  72. }
  73. LUALIB_API int luaL_loadfilex(lua_State *L, const char *filename,
  74. const char *mode)
  75. {
  76. FileReaderCtx ctx;
  77. int status;
  78. const char *chunkname;
  79. if (filename) {
  80. ctx.fp = fopen(filename, "rb");
  81. if (ctx.fp == NULL) {
  82. lua_pushfstring(L, "cannot open %s: %s", filename, strerror(errno));
  83. return LUA_ERRFILE;
  84. }
  85. chunkname = lua_pushfstring(L, "@%s", filename);
  86. } else {
  87. ctx.fp = stdin;
  88. chunkname = "=stdin";
  89. }
  90. status = lua_loadx(L, reader_file, &ctx, chunkname, mode);
  91. if (ferror(ctx.fp)) {
  92. L->top -= filename ? 2 : 1;
  93. lua_pushfstring(L, "cannot read %s: %s", chunkname+1, strerror(errno));
  94. if (filename)
  95. fclose(ctx.fp);
  96. return LUA_ERRFILE;
  97. }
  98. if (filename) {
  99. L->top--;
  100. copyTV(L, L->top-1, L->top);
  101. fclose(ctx.fp);
  102. }
  103. return status;
  104. }
  105. LUALIB_API int luaL_loadfile(lua_State *L, const char *filename)
  106. {
  107. return luaL_loadfilex(L, filename, NULL);
  108. }
  109. typedef struct StringReaderCtx {
  110. const char *str;
  111. size_t size;
  112. } StringReaderCtx;
  113. static const char *reader_string(lua_State *L, void *ud, size_t *size)
  114. {
  115. StringReaderCtx *ctx = (StringReaderCtx *)ud;
  116. UNUSED(L);
  117. if (ctx->size == 0) return NULL;
  118. *size = ctx->size;
  119. ctx->size = 0;
  120. return ctx->str;
  121. }
  122. LUALIB_API int luaL_loadbufferx(lua_State *L, const char *buf, size_t size,
  123. const char *name, const char *mode)
  124. {
  125. StringReaderCtx ctx;
  126. ctx.str = buf;
  127. ctx.size = size;
  128. return lua_loadx(L, reader_string, &ctx, name, mode);
  129. }
  130. LUALIB_API int luaL_loadbuffer(lua_State *L, const char *buf, size_t size,
  131. const char *name)
  132. {
  133. return luaL_loadbufferx(L, buf, size, name, NULL);
  134. }
  135. LUALIB_API int luaL_loadstring(lua_State *L, const char *s)
  136. {
  137. return luaL_loadbuffer(L, s, strlen(s), s);
  138. }
  139. /* -- Dump bytecode ------------------------------------------------------- */
  140. LUA_API int lua_dump(lua_State *L, lua_Writer writer, void *data)
  141. {
  142. cTValue *o = L->top-1;
  143. api_check(L, L->top > L->base);
  144. if (tvisfunc(o) && isluafunc(funcV(o)))
  145. return lj_bcwrite(L, funcproto(funcV(o)), writer, data, 0);
  146. else
  147. return 1;
  148. }