Exemple #1
0
	static int lookforfunc(lua_State *L, const char *path, const char *sym) {
		void *reg = clib::get(L, path);
		if (reg == NULL) {
			reg = loaddll(path);
			if (reg == NULL) {
				pusherror(L);
				return 1;
			}
			clib::add(L, path, reg);
		}
		if (*sym == '*') { 
			lua_pushboolean(L, 1);
			return 0;
		}
		else {
			lua_CFunction f = (lua_CFunction)GetProcAddress((HMODULE)reg, sym);
			if (f == NULL) {
				pusherror(L);
				return 2;
			}
			lua_pushcfunction(L, f); 
			return 0; 
		}
	}
Exemple #2
0
static void
compile_and_run(void)
{
    char cmdbuf[256];
    int ret;
    const char * cc = CC;
    const char * copt = "";
    const char * pic_cmd = "";
    if (opt_level >= 3)
	copt = " -O3";

    if (cc_cmd) cc = cc_cmd;

#ifdef DEFAULT_PIC
    if (pic_opt<0) pic_opt = DEFAULT_PIC;
#endif
    switch(pic_opt) {
    case 0: pic_cmd = ""; break;
    case 1: pic_cmd = " -fpic"; break;
    case 2: pic_cmd = " -fPIC"; break;
    }

    if (in_one) {
	if (verbose)
	    fprintf(stderr,
		"Running C Code using \"%s%s%s -shared\" and dlopen().\n",
		cc,pic_cmd,copt);

	sprintf(cmdbuf, "%s%s%s -shared -o %s %s",
		    cc, pic_cmd, copt, dl_name, ccode_name);
	ret = system(cmdbuf);
    } else {
	if (verbose)
	    fprintf(stderr,
		"Running C Code using \"%s%s%s\", link -shared and dlopen().\n",
		cc,pic_cmd,copt);

	/* Like this so that ccache has a good path and distinct compile. */
	sprintf(cmdbuf, "cd %s; %s%s%s -c -o %s %s",
		tmpdir, cc, pic_cmd, copt, BFBASE".o", BFBASE".c");
	ret = system(cmdbuf);

	if (ret != -1) {
	    sprintf(cmdbuf, "cd %s; %s%s -shared -o %s %s",
		    tmpdir, cc, pic_cmd, dl_name, BFBASE".o");
	    ret = system(cmdbuf);
	}
    }

    if (ret == -1) {
	perror("Calling C compiler failed");
	exit(1);
    }
    if (WIFEXITED(ret)) {
	if (WEXITSTATUS(ret)) {
	    fprintf(stderr, "Compile failed.\n");
	    exit(WEXITSTATUS(ret));
	}
    } else {
	if (WIFSIGNALED(ret)) {
	    if( WTERMSIG(ret) != SIGINT && WTERMSIG(ret) != SIGQUIT)
		fprintf(stderr, "Killed by SIGNAL %d.\n", WTERMSIG(ret));
	    exit(1);
	}
	perror("Abnormal exit");
	exit(1);
    }

    loaddll(dl_name);

    if (!leave_temps) {
	unlink(ccode_name);
	unlink(dl_name);
	unlink(obj_name);
	rmdir(tmpdir);
    }

#ifndef __STRICT_ANSI__
    if (verbose>1)
	fprintf(stderr, "Calling function loaded at address %p\n", (void*) runfunc);
#endif

    start_runclock();
    (*runfunc)();
    finish_runclock(&run_time, &io_time);

    dlclose(handle);
}