Exemplo n.º 1
0
static int pmain(lua_State* L)
{
 struct Smain* s = (struct Smain*)lua_touserdata(L, 1);
 int argc=s->argc;
 char** argv=s->argv;
 const Proto* f;
 int i;
 if (!lua_checkstack(L,argc)) fatal("too many input files");
 for (i=0; i<argc; i++)
 {
  const char* filename=IS("-") ? NULL : argv[i];
  if (luaL_loadfile(L,filename)!=0) fatal(lua_tostring(L,-1));
 }
 f=combine(L,argc);
 if (listing) luaU_print(f,listing>1);
 if (dumping)
 {
  FILE* D= (output==NULL) ? stdout : fopen(output,"wb");
  if (D==NULL) cannot("open");
  lua_lock(L);
  luaU_dump(L,f,writer,D,stripping);
  lua_unlock(L);
  if (ferror(D)) cannot("write");
  if (fclose(D)) cannot("close");
 }
 return 0;
}
Exemplo n.º 2
0
static int LS_dump( LuaState* state )
{
	LuaStack args(state);
	const char* inFileName = args[1].GetString();
	const char* outFileName = args[2].GetString();
	if (luaL_loadfile(*state, inFileName) != 0)
	{
		state->PushBoolean(false);
		return 1;
	}

	FILE* D = fopen(outFileName, "wb");
	if (!D)
	{
		state->PushBoolean(false);
		return 1;
	}
	
	Proto* f = combine(*state, 1);
//	if (D==NULL) cannot(output,"open","out");
//	if (stripping) strip(L,f);
	luaU_dump(*state, f, writer, D);
	fclose(D);

	state->PushBoolean(true);
	return 1;
}
Exemplo n.º 3
0
int main(int argc, char* argv[])
{
 lua_State* L;
 Proto* f;
 int i=doargs(argc,argv);
 argc-=i; argv+=i;
 if (argc<=0) usage("no input files given",NULL);
 L=lua_open();
 luaB_opentests(L);
 for (i=0; i<argc; i++)
 {
  const char* filename=IS("-") ? NULL : argv[i];
  if (luaL_loadfile(L,filename)!=0) fatal(lua_tostring(L,-1));
 }
 f=combine(L,argc);
 if (listing) luaU_print(f);
 if (dumping)
 {
  FILE* D=fopen(output,"wb");
  if (D==NULL) cannot(output,"open","out");
  if (stripping) strip(L,f);
  luaU_dump(L,f,writer,D);
  if (ferror(D)) cannot(output,"write","out");
  fclose(D);
 }
 lua_close(L);
 return 0;
}
Exemplo n.º 4
0
ByteCode *TypeCompiler::generateByteCode(Proto *proto)
{
    utArray<unsigned char> bc;
    // always include debug info
    luaU_dump(L, proto, luaByteCodewriter, &bc, 0);
    return ByteCode::encode64(bc);
}
Exemplo n.º 5
0
int main(int argc, char* argv[]) {
    const char *input_luac, *opcodes_def;
    int i;
    lua_State* L;
    Proto* f;
    FILE* D;

    gargc = argc;
    gargv = argv;
    i = doargs(argc,argv);
    argc -= i;
    argv += i;

    if (argc < 1) {
        usage("need 1 arguments at least", NULL);
    }

    L = lua_open();
    glstate = L;
    //luaB_opentests(L);

    input_luac = argv[0];
    if (luaL_loadfile(L, input_luac) != 0) {
        fatal(lua_tostring(L, -1));
    }
    f = toproto(L, -1);

    if (cmp_gen_lua || cmp_gen_luac) {
        Proto* input_proto = f;
        Proto* allopcodes_proto;
        const char* buff = cmp_gen_lua ? allopcodes_lua : allopcodes_luac;
        int bufflen = cmp_gen_lua ? allopcodes_lua_len : allopcodes_luac_len;
        if (luaL_loadbuffer(L, buff, bufflen, "allopcodes.lua") != 0) {
            fatal(lua_tostring(L, -1));
        }
        allopcodes_proto = toproto(L, -1);
        CompareAndGenOpcodes(input_proto, allopcodes_proto);
    } else {
        opcodes_def = (argv[1]) ? (argv[1]) : OPCODES_TXT;
        if (!generateOp2op(opcodes_def)) {
            fprintf(stderr, "opcodes.txt file: %s format error!", opcodes_def);
            return EXIT_FAILURE;
        }
        swapOpCode(f);

        D = (output == NULL) ? stdout : fopen(output, "wb");
        if (D == NULL) cannot("open", output);
        lua_lock(L);
        luaU_dump(L, f, writer, D, 0);
        lua_unlock(L);
        if (ferror(D)) cannot("write", output);
        if (fclose(D)) cannot("close", output);

        printf("%s generated!\n", output);
    }

    lua_close(L);
    return EXIT_SUCCESS;
}
Exemplo n.º 6
0
// Lua: compile(filename) -- compile lua file into lua bytecode, and save to .lc
static int node_compile( lua_State* L )
{
  Proto* f;
  int file_fd = FS_OPEN_OK - 1;
  size_t len;
  const char *fname = luaL_checklstring( L, 1, &len );
  if ( len > FS_NAME_MAX_LENGTH )
    return luaL_error(L, "filename too long");

  char output[FS_NAME_MAX_LENGTH];
  c_strcpy(output, fname);
  // check here that filename end with ".lua".
  if (len < 4 || (c_strcmp( output + len - 4, ".lua") != 0) )
    return luaL_error(L, "not a .lua file");

  output[c_strlen(output) - 2] = 'c';
  output[c_strlen(output) - 1] = '\0';
  NODE_DBG(output);
  NODE_DBG("\n");
  if (luaL_loadfsfile(L, fname) != 0) {
    return luaL_error(L, lua_tostring(L, -1));
  }

  f = toproto(L, -1);

  int stripping = 1;      /* strip debug information? */

  file_fd = fs_open(output, fs_mode2flag("w+"));
  if (file_fd < FS_OPEN_OK)
  {
    return luaL_error(L, "cannot open/write to file");
  }

  lua_lock(L);
  int result = luaU_dump(L, f, writer, &file_fd, stripping);
  lua_unlock(L);

  if (fs_flush(file_fd) < 0) {   // result codes aren't propagated by flash_fs.h
    // overwrite Lua error, like writer() does in case of a file io error
    result = 1;
  }
  fs_close(file_fd);
  file_fd = FS_OPEN_OK - 1;

  if (result == LUA_ERR_CC_INTOVERFLOW) {
    return luaL_error(L, "value too big or small for target integer type");
  }
  if (result == LUA_ERR_CC_NOTINTEGER) {
    return luaL_error(L, "target lua_Number is integral but fractional value found");
  }
  if (result == 1) {    // result status generated by writer() or fs_flush() fail
    return luaL_error(L, "writing to file failed");
  }

  return 0;
}
Exemplo n.º 7
0
static int file_compile( lua_State* L )
{
  Proto* f;
  int file_fd = FILE_NOT_OPENED;
  size_t len;
  const char *fname = luaL_checklstring( L, 1, &len );
  if ( len > SPIFFS_OBJ_NAME_LEN )
    return luaL_error(L, "filename too long");

  char output[SPIFFS_OBJ_NAME_LEN];
  strcpy(output, fname);
  // check here that filename end with ".lua".
  if (len < 4 || (strcmp( output + len - 4, ".lua") != 0) )
    return luaL_error(L, "not a .lua file");

  output[strlen(output) - 2] = 'c';
  output[strlen(output) - 1] = '\0';
  
  if (luaL_loadfile(L, fname) != 0) {
    return luaL_error(L, lua_tostring(L, -1));
  }

  f = toproto(L, -1);

  int stripping = 1;      /* strip debug information? */

  file_fd = SPIFFS_open(&fs,(char*)output,mode2flag("w+"),0);
  if (file_fd < FILE_NOT_OPENED)
  {
    return luaL_error(L, "cannot open/write to file");
  }

  lua_lock(L);
  int result = luaU_dump(L, f, writer, &file_fd, stripping);
  lua_unlock(L);

  SPIFFS_fflush(&fs,file_fd);
  SPIFFS_close(&fs,file_fd);
  file_fd =FILE_NOT_OPENED;

  if (result == LUA_ERR_CC_INTOVERFLOW) {
    return luaL_error(L, "value too big or small for target integer type");
  }
  if (result == LUA_ERR_CC_NOTINTEGER) {
    return luaL_error(L, "target lua_Number is integral but fractional value found");
  }

  return 0;
}
Exemplo n.º 8
0
const char* luaC_complieN(lua_State* L, const char* szDesFile, int count)
{
	const Proto* f;
	FILE* D;
	if (!lua_checkstack(L, count))
	{
		return "too much files, can not compile once";
	}
	f = combine(L, count);


	D = fopen_k(szDesFile, "wb");
	lua_lock(L);
	luaU_dump(L, f, writer, D, 0);
	lua_unlock(L);
	fclose(D);

	return 0;
}
Exemplo n.º 9
0
const char* luaC_complie(lua_State* L, const char* szSrcFile, const char* szDesFile)
{
	const Proto* f;
	FILE* D;
	if (luaL_loadfile(L,szSrcFile)!=0)
	{
		return lua_tostring(L, -1);
	}
	f = combine(L, 1);


	D = fopen_k(szDesFile, "wb");
	lua_lock(L);
	luaU_dump(L, f, writer, D, 0);
	lua_unlock(L);
	fclose(D);
	
	return 0;
}
Exemplo n.º 10
0
int lua_dumpSourceCode(lua_State* L, const char *sourceCode, const char* outputPath)
{
    if (luaL_loadstring(L, sourceCode) != LUA_OK)
    {
        fatal(lua_tostring(L,-1));
    }
    
    const Proto* f = combine(L, 1);
    if (listing)
    {
        luaU_print(f,listing>1);
    }
    
    if (dumping)
    {
        FILE* D = fopen(outputPath, "wb");
        
        if (D==NULL)
        {
            printf(" error to open file!\n");
        }
        
        lua_lock(L);
        
        luaU_dump(L,f,writer,D,stripping);
        
        lua_unlock(L);
        
        if (ferror(D))
        {
            printf("error in write!\n");
        }
        
        if (fclose(D))
        {
            printf("error in close!\n");
        }
    }
    
    return 0;
}
Exemplo n.º 11
0
static int pmain(LuaThread* L)
{
 int argc=(int)lua_tointeger(L,1);
 char** argv=(char**)lua_touserdata(L,2);
 const LuaProto* f;
 int i;
 if (!lua_checkstack(L,argc)) fatal("too many input files");
 for (i=0; i<argc; i++)
 {
  const char* filename=IS("-") ? NULL : argv[i];
  if (luaL_loadfile(L,filename)!=LUA_OK) fatal(lua_tostring(L,-1));
 }
 f=combine(L,argc);
 if (listing) luaU_print(f,listing>1);
 if (dumping)
 {
  FILE* D= (output==NULL) ? stdout : fopen(output,"wb");
  if (D==NULL) cannot("open");
  luaU_dump(L,f,writer,D,stripping);
  if (ferror(D)) cannot("write");
  if (fclose(D)) cannot("close");
 }
 return 0;
}
Exemplo n.º 12
0
static int pmain(lua_State* L) {
  struct Smain* s = (struct Smain*)lua_touserdata(L, 1);
  int argc=s->argc;
  char** argv=s->argv;
  Proto* f;
  int scripts=0;
  int i;
  if (!lua_checkstack(L,argc)) fatal("too many input files");
  lua_gc(L, LUA_GCSTOP, 0);  /* stop collector during initialization */
  luaL_openlibs(L);  /* open libraries */
  lua_gc(L, LUA_GCRESTART, 0);
  /* compile each script from command line into a Lua function. */
  for (i=0; i<argc; i++) {
    const char* filename=IS("-") ? NULL : argv[i];
    if(IS("-L")) break;
    if (luaL_loadfile(L,filename)!=0) fatal(lua_tostring(L,-1));
    scripts++;
  }
  /* compile each preload library from the command line into a Lua function. */
  for (i=0; i<preloads; i++) {
    char* filename=preload_libs[i];
    char* p;
    /* try loading library as if it is a normal file. */
    if (luaL_loadfile(L,filename)!=0) {
      /* try pre-loading library with 'require' module loading system. */
      lua_getglobal(L, "require");
      lua_pushstring(L, filename);
      lua_pushboolean(L, 1);
      lua_call(L, 2, 1);
      if (lua_iscfunction(L, -1)) { /* make sure it is not a C-Function. */
        lua_pop(L, 1);
        lua_pushfstring(L, "\nCan't preload C module: '%s'\n", filename);
        lua_concat(L, 2);  /* accumulate with error from luaL_findfile */
        fatal(lua_tostring(L,-1));
      }
      if (!lua_isfunction(L, -1)) { /* did we get an error? */
        lua_pushliteral(L, "\n");
        lua_concat(L, 3);  /* accumulate with error from luaL_findfile */
        fatal(lua_tostring(L,-1));
      } else {
        lua_remove(L, -2); /* remove error from luaL_findfile. */
      }
    } else {
      /* convert filename into package name. */
      p= filename + strlen(filename);
      for(;p >= filename; p--) {
        if(p[0] == '.') { /* Remove file extension. */
          p[0] = '\0';
          continue;
        }
        if(p[0] == '/') { /* Remove file path. */
          preload_libs[i] = p+1;
          break;
        }
      }
    }
  }
  /* generate a new Lua function to combine all of the compiled scripts. */
  f=combine(L, scripts);
  if (listing) luaU_print(f,listing>1);
  if (c_code && !parse_only) {
    FILE* D= (output==NULL) ? stdout : fopen(output,"wb");
    if (D==NULL) cannot("open");
    lua_lock(L);
    slua_dumper_dump(D, output, L, f, stripping);
    lua_unlock(L);
    if (ferror(D)) cannot("write");
    if (fclose(D)) cannot("close");
  }
  if (dumping && !parse_only) {
    FILE* D= (output==NULL) ? stdout : fopen(output,"wb");
    if (D==NULL) cannot("open");
    lua_lock(L);
    luaU_dump(L,f,writer,D,stripping);
    lua_unlock(L);
    if (ferror(D)) cannot("write");
    if (fclose(D)) cannot("close");
  }
  return 0;
}
Exemplo n.º 13
0
int main(int argc, char* argv[]) {
	lua_State* L;
	const char *destfile, *destnum, *srcfile, *srcnum;
	Proto *fdestroot, *fsrcroot, *fparent, *fdest, *fsrc;
	int cdest, csrc;
	char *realdestnum = NULL, *realsrcnum = NULL;
	FILE* D;
	int diff, i;

	gargc = argc;
	gargv = argv;
	i = doargs(argc,argv);
	argc -= i;
	argv += i;

	if (printfuncnum && argc < 1) {
		usage("need 1 arguments for -pn at least", NULL);
	}
	if (argc < 4) {
		usage("need 4 arguments at least", NULL);
	}

	L = lua_open();
	glstate = L;
	luaB_opentests(L);

	destfile = argv[0];
	destnum = argv[1];
	if (luaL_loadfile(L, destfile) != 0) {
		fatal(lua_tostring(L, -1));
	}
	fdestroot = toproto(L, -1);

	if (printfuncnum) {
		printf("%d\n",0);
		printFuncStructure(fdestroot, "  0_");
		lua_close(L);
		return EXIT_SUCCESS;
	}

	realdestnum = (char*)calloc(strlen(destnum)+1, sizeof(char));
	fparent = findParentFunction(fdestroot, destnum, &cdest, realdestnum);
	if (cdest < 0) {
		if (realdestnum) { free(realdestnum); realdestnum = NULL; }
		fatal("cannot find dest function");
	}
	if (fparent == NULL) {
		fdest = fdestroot;
	} else {
		fdest = fparent->p[cdest];
	}

	srcfile = argv[2];
	srcnum = argv[3];
	if (luaL_loadfile(L, srcfile) != 0) {
		fatal(lua_tostring(L, -1));
	}
	fsrcroot = toproto(L, -1);
	realsrcnum = (char*)calloc(strlen(srcnum)+1, sizeof(char));
	fsrc = findParentFunction(fsrcroot, srcnum, &csrc, realsrcnum);
	if (csrc < 0) {
		if (realdestnum) { free(realdestnum); realdestnum = NULL; }
		if (realsrcnum) { free(realsrcnum); realsrcnum = NULL; }
		fatal("cannot find src function");
	}
	if (fsrc == NULL) {
		fsrc = fsrcroot;
	} else {
		fsrc = fsrc->p[csrc];
	}

	if (!replace_sub){
		if (fparent == NULL) {
			if (realdestnum) { free(realdestnum); realdestnum = NULL; }
			if (realsrcnum) { free(realsrcnum); realsrcnum = NULL; }
			fatal("cannot use root as dest function");
		}
		fprintf(stderr, "Replacing %s %s with %s %s ...\n", destfile, realdestnum, srcfile, realsrcnum);
		diff = replaceFunction(fparent, cdest, fsrc);
		fprintf(stderr, "1 function replaced ok.\n");
	} else {
		fprintf(stderr, "Replacing sub functions of %s %s with those of %s %s ...\n", destfile, realdestnum, srcfile, realsrcnum);
		diff = replaceSubFunctions(fdest, fsrc);
		fprintf(stderr, "%d function replaced ok.", MIN(fdest->sizep, fsrc->sizep));
		if (fdest->sizep != fsrc->sizep) {
			fprintf(stderr, " The dest has %d sub functions, but the src has %d . Please have a check.\n", fdest->sizep, fsrc->sizep);
		} else {
			fprintf(stderr, "\n");
		}
	}

	if (realdestnum) { free(realdestnum); realdestnum = NULL; }
	if (realsrcnum) { free(realsrcnum); realsrcnum = NULL; }

	if (strict == 1 && diff > 0){
		fatal("strip mode on and incompatible functions found, stop writing output.");
	}

	D = (output == NULL) ? stdout : fopen(output, "wb");
	if (D == NULL) cannot("open");
	lua_lock(L);
	luaU_dump(L, fdestroot, writer, D, 0);
	lua_unlock(L);
	if (ferror(D)) cannot("write");
	if (fclose(D)) cannot("close");

	printf("%s generated!\n", output);

	lua_close(L);
	return EXIT_SUCCESS;
}