Ejemplo n.º 1
0
int main(int argc, const char *argv[])
{
	fake * fk = newfake();

	// 解析参数
	int parseret = parsearg(fk, argc, argv);
	if (parseret != 0)
	{
		return parseret;
	}

	if (g_iscompiled)
	{
		fksetargv(fk, argc - 1, argv + 1);
	}
	else
	{
		fksetargv(fk, argc - 2, argv + 2);
	}
	fkopenalllib(fk);
	fkseterrorfunc(fk, error_log);

	// for test
	fkreg(fk, "test_cfunc1", test_cfunc1);
	fkreg(fk, "new_test_class1", new_test_class1);
	fkreg(fk, "new_test_class2", new_test_class2);
	fkreg(fk, "delete_test_class1", delete_test_class1);
	fkreg(fk, "delete_test_class2", delete_test_class2);
	fkreg(fk, "test_memfunc1", &test_class1::test_memfunc1); 
	fkreg(fk, "test_memfunc1", &test_class2::test_memfunc1); 
	fkreg(fk, "test_memfunc2", &test_class2::test_memfunc2); 

	// 编译后文件
	if (g_iscompiled)
	{
		// 打开自己
		char * selftmpbuf = 0;
		int selfsize = 0;
		if (!readfile(g_selfname, 0, selftmpbuf, selfsize))
		{
			return -1;
		}

		int startpos = 0;
		int size = 0;
		memcpy(&startpos, g_replacebuff, sizeof(startpos));
		memcpy(&size, g_replacebuff + 4, sizeof(size));

		if (fkloadfunc(fk, selftmpbuf + startpos, size) == -1)
		{
			printf("load func fail\n");
			return -1;
		}

		free(selftmpbuf);
	}
	// 解析文件
	else if (!g_isload)
	{
		fkparse(fk, argv[1]);
		if (fkerror(fk))
		{
			return -1;
		}
	}
	// 读文件
	else
	{
		// 读文件
		char * tmpbuf = 0;
		int size = 0;
		if (!readfile(argv[1], 0, tmpbuf, size))
		{
			return -1;
		}

		if (fkloadfunc(fk, tmpbuf, size) == -1)
		{
			printf("load func fail\n");
			return -1;
		}

		free(tmpbuf);
		printf("load from %s ok, size %d\n", argv[1], size);
	}

	// 存文件
	if (g_issave)
	{
		int tmpsize = 1024 * 1024;
		char * tmpbuf = (char *)malloc(tmpsize);
		int size = fksavefunc(fk, tmpbuf, tmpsize);
		if (size == -1)
		{
			printf("save func fail\n");
			return -1;
		}

		const char * filename = "fake.bin";
		if (!writefile(filename, tmpbuf, size))
		{
			return -1;
		}
		free(tmpbuf);
		
		printf("save to %s ok, size %d\n", filename, size);
		
		return 0;
	}

	// 编译到一起
	if (g_iscompile)
	{
		int tmpsize = 1024 * 1024;
		char * tmpbuf = (char *)malloc(tmpsize);
		int size = fksavefunc(fk, tmpbuf, tmpsize);
		if (size == -1)
		{
			printf("compile func fail\n");
			return -1;
		}

		// 打开自己
		char * selftmpbuf = 0;
		int selfsize = 0;
		if (!readfile(g_selfname, size, selftmpbuf, selfsize))
		{
			return -1;
		}

		// 替换内存中的字符串
		bool isfind = false;
		for (int i = 0; i < selfsize; i++)
		{
			if (memcmp(selftmpbuf + i, g_replacebuff, strlen(g_replacebuff)) == 0)
			{
				memcpy(selftmpbuf + i, &selfsize, sizeof(selfsize));
				memcpy(selftmpbuf + i + 4, &size, sizeof(size));
				isfind = true;
				break;
			}
		}
		if (!isfind)
		{
			printf("replace %s pos fail\n", argv[0]);
			return -1;
		}

		// 复制后面的
		memcpy(selftmpbuf + selfsize, tmpbuf, size);
		free(tmpbuf);

		// 输出
		const char * filename = "out.exe";
		if (!writefile(filename, selftmpbuf, selfsize + size))
		{
			return -1;
		}
		free(selftmpbuf);

		printf("compile to %s ok, size %d\n", filename, selfsize + size);

		return 0;
	}

#ifndef WIN32
	if (g_isopengoogleprofile)
	{
		ProfilerStart("fake.prof");
	}
#endif

	uint32_t begintime = time(0);
	int fkret = 0;

	// run
	for (int i = 0; i < g_testnum; i++)
	{
		if (!g_isopenjit)
		{
			if (g_isstep)
			{
				fkret = fkdebugrun<int>(fk, "main");
			}
			else
			{
				fkret = fkrun<int>(fk, "main");
			}
		}
		else
		{
			fkret = fkrunjit<int>(fk, "main");
		}
	}
	
	uint32_t endtime = time(0);

#ifndef WIN32
	if (g_isopengoogleprofile)
	{
		ProfilerStop();
	}
#endif

	if (fkerror(fk))
	{
		return -1;
	}

	if (g_isopenprofile)
	{
		printf("\n%s", fkdumpprofile(fk));
	}

	if (g_isprintoutput)
	{
		printf("main return : %d, use time %d\n", fkret, endtime - begintime);
	}

	delfake(fk);

	return 0;
}
Ejemplo n.º 2
0
int main(int argc, const char *argv[])
{
    // ½âÎö²ÎÊý
    if (argc < 2)
    {
        printf("arg : [-hjp] file [arg]\n");
        return 0;
    }

    fake * fk = newfake();
    bool isopenprofile = false;
    bool isopenjit = false;
    if (argv[1][0] == '-')
    {
        if (strstr(argv[1], "h"))
        {
            printf("fakescript\n\narg : [-hjp] file [arg]\n-h help\n-j open jit\n-p open profile\n\nsample:./fake -jp a.fk\n\n");
            return 0;
        }

        if (strstr(argv[1], "j"))
        {
            fkopenjit(fk);
            isopenjit = true;
        }

        if (strstr(argv[1], "p"))
        {
            fkopenprofile(fk);
            isopenprofile = true;
        }
        argc--;
        argv++;
    }

    fksetargv(fk, argc - 2, argv + 2);
    fkopenalllib(fk);
    fkseterrorfunc(fk, error_log);
    fkparse(fk, argv[1]);
    if (fkerror(fk))
    {
        return 0;
    }

    if (!isopenjit)
    {
        fkrun<int>(fk, "main");
    }
    else
    {
        fkrunjit<int>(fk, "main");
    }
    if (fkerror(fk))
    {
        return 0;
    }

    if (isopenprofile)
    {
        printf("call profile:\n%s", fkdumpprofile(fk));
    }

    delfake(fk);

    return 0;
}