예제 #1
0
void* fxMapArchive(txString base, txCallbackAt callbackAt)
{
	int error = 0;
	txArchive* archive = C_NULL;
#if mxWindows
#else
	struct stat statbuf;
#endif
	txString p;
	Atom atom;
	int c, i;
	txString* symbol;
	txScript* script;

	archive = c_malloc(sizeof(txArchive));
	bailElse(archive);
	c_memset(archive, 0, sizeof(txArchive));
	c_strcpy(archive->base, base);
#if mxWindows
	archive->file = CreateFile(base, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
	bailElse(archive->file != INVALID_HANDLE_VALUE);
	archive->size = GetFileSize(archive->file, &archive->size);
	bailElse(archive->size != INVALID_FILE_SIZE);
	archive->mapping = CreateFileMapping(archive->file, NULL, PAGE_READONLY, 0, (SIZE_T)archive->size, NULL);
	bailElse(archive->mapping != INVALID_HANDLE_VALUE);
	archive->address = MapViewOfFile(archive->mapping, FILE_MAP_READ, 0, 0, (SIZE_T)archive->size);
	bailElse(archive->address != NULL);
#else
	archive->fd = open(base, O_RDONLY);
	bailElse(archive->fd >= 0);
	fstat(archive->fd, &statbuf);
	archive->size = statbuf.st_size;
	archive->address = mmap(NULL, archive->size, PROT_READ, MAP_SHARED, archive->fd, 0);
	bailElse(archive->address != MAP_FAILED);
#endif
	p = archive->address;
	p = fxMapAtom(p, &atom);
	bailAssert(atom.atomType == XS_ATOM_ARCHIVE);
	
	p = fxMapAtom(p, &atom);
	bailAssert(atom.atomType == XS_ATOM_VERSION);
	bailAssert(*p++ == XS_MAJOR_VERSION);
	bailAssert(*p++ == XS_MINOR_VERSION);
	bailAssert(*p++ == XS_PATCH_VERSION);
	if ((*p++) && !callbackAt) {
		char* dot;
		dot = c_strrchr(archive->base, '.');
		if (dot)
			*dot = 0;
		#if mxWindows
			c_strcat(archive->base, ".dll");
			archive->library = LoadLibrary(archive->base);
			bailElse(archive->library);
			callbackAt = (txCallbackAt)GetProcAddress(archive->library, "xsHostModuleAt");
			bailElse(callbackAt);
		#else
			c_strcat(archive->base, ".so");
			archive->library = dlopen(archive->base, RTLD_NOW);
			if (!archive->library)
        		fprintf(stderr, "%s\n", dlerror());
			bailElse(archive->library);
			callbackAt = (txCallbackAt)dlsym(archive->library, "xsHostModuleAt");
			bailElse(callbackAt);
		#endif
		archive->keys = callbackAt(0);
	}
	
	p = fxMapAtom(p, &atom);
	bailAssert(atom.atomType == XS_ATOM_SYMBOLS);
	c = archive->symbolCount = ntohs(*((unsigned short*)p));
	p += 2;
	if (c) {
		symbol = archive->symbols = c_malloc(c * sizeof(txString));
		for (i = 0; i < c; i++) {
			*symbol = p;
			p += c_strlen(p) + 1;
			symbol++;
		}
	}
	
	p = fxMapAtom(p, &atom);
	bailAssert(atom.atomType == XS_ATOM_PATHS);
	c = archive->scriptCount = ntohs(*((unsigned short*)p));
	p += 2;
	script = archive->scripts = c_malloc(c * sizeof(txScript));
	c_memset(script, 0, c * sizeof(txScript));
	for (i = 1; i <= c; i++) {
		if (callbackAt)
			script->callback = callbackAt(i);
		script->path = p;
		p += c_strlen(p) + 1;
		script++;
	}
	
	script = archive->scripts;
	for (i = 0; i < c; i++) {
		p = fxMapAtom(p, &atom);
		bailAssert(atom.atomType == XS_ATOM_CODE);
		script->codeSize = atom.atomSize - sizeof(atom);
		script->codeBuffer = (txS1*)p;
		p += script->codeSize;
		script++;
	}
	
	p = c_strrchr(archive->base, mxSeparator);
	if (p) {
		p++;
		*p = 0;
		archive->baseLength = p - archive->base;
	}
bail:
	if (error) {
		fxUnmapArchive(archive);
		archive = C_NULL;
	}
	return archive;
}
예제 #2
0
파일: xsr6.c 프로젝트: basuke/kinomajs
int main(int argc, char* argv[]) 
{
	xsCreation creation_mc = {
		25*1024,	/* initial chunk size */
		2048,		/* incremental chunk size */
		50*1024/16,	/* initial heap count	-- will be calculated later */
		128,		/* incremental heap count	-- wasting 16 bytes / allocation */
		650,		/* stack count */
		2048+1024,	/* key count */
		97,		/* name modulo */
		127,		/* symbol modulo */
	};
	xsCreation creation_tool = {
		128 * 1024 * 1024, 	/* initialChunkSize */
		16 * 1024 * 1024, 	/* incrementalChunkSize */
		8 * 1024 * 1024, 		/* initialHeapCount */
		1 * 1024 * 1024, 		/* incrementalHeapCount */
		4096, 		/* stackCount */
		4096*3, 		/* keyCount */
		1993, 		/* nameModulo */
		127 		/* symbolModulo */
	};
	xsCreation* creation = &creation_tool;
	int error = 0;
	int argi = 1;
	void* archive = NULL;
	xsBooleanValue program = 0;
	xsBooleanValue xsbug = 0;
	xsMachine* machine;
	char path[PATH_MAX];
	char modulePath[PATH_MAX];
	xsStringValue extension;
	xsStringValue slash;
	xsStringValue name;
	int size;
	if (argi < argc) {
		if (!strcmp(argv[argi], "-a")) {
			argi++;
			if (argi < argc) {
                if (realpath(argv[argi], path)) {
                	archive = fxMapArchive(path, NULL);
                    if (!archive) {
                        fprintf(stderr, "# invalid archive: %s\n", path);
                        return 1;
                    }
                    if (strstr(path, "mc.xsa")) {
                  		creation = &creation_mc;
                    }
                }
				else {
					fprintf(stderr, "# archive not found: %s\n", argv[argi]);
					return 1;
				}
				argi++;
			}
		}
	}
	while (argi < argc) {
		if (!strcmp(argv[argi], "-p")) {
			program = 1;
			argi++;
		}
		else if (!strcmp(argv[argi], "-x")) {
			xsbug = 1;
			argi++;
		}
		else
			break;
	}
	gargc = argc;
	gargi = argi;
	gargv = argv;
	machine = xsCreateMachine(creation, archive, "xsr6", NULL);
	xsBeginHost(machine);
	{
		xsVars(2);
		{
			xsTry {
				xsVar(0) = xsNewInstanceOf(xsObjectPrototype);
				if (xsbug)
					xsVar(1) = xsNewHostFunction(console_log_xsbug, 0);
				else
					xsVar(1) = xsNewHostFunction(console_log, 0);
				xsSet(xsVar(0), xsID("log"), xsVar(1));
				xsSet(xsGlobal, xsID("console"), xsVar(0));

				xsVar(0) = xsNewHostObject(weakTest);
				xsVar(1) = xsNewHostConstructor(WeakTest, 1, xsVar(0));
				xsSet(xsGlobal, xsID("WeakTest"), xsVar(1));
				xsVar(1) = xsNewHostFunction(gc, 0);
				xsSet(xsGlobal, xsID("gc"), xsVar(1));

				xsResult = xsModulePaths();
				if (archive) {
					slash = strrchr(path, mxSeparator);
					if (slash) {
						*(slash + 1) = 0;
						xsCall1(xsResult, xsID("add"), xsString(path));
					}
				}
				realpath(argv[0], modulePath);
				slash = strrchr(modulePath, mxSeparator);
				if (slash) {
					strcpy(slash + 1, "modules");
					size = c_strlen(modulePath);
					modulePath[size++] = mxSeparator;
					modulePath[size] = 0;
					xsCall1(xsResult, xsID("add"), xsString(modulePath));
				}

				if (argi == argc) {
					fprintf(stderr, "# no module, no program\n");
					error = 1;
				}	
				else if (program) {
					xsVar(0) = xsNewHostFunction(print, 0);
					xsSet(xsGlobal, xsID("print"), xsVar(0));
					//xsStartProfiling();
					while (argi < argc) {
						if (argv[argi][0] != '-') {
							xsElseError(realpath(argv[argi], path));
							strcpy(modulePath, path);
							slash = strrchr(modulePath, mxSeparator);
							*(slash + 1) = 0;
							xsCall1(xsResult, xsID("add"), xsString(modulePath));
							strcat(modulePath, "modules/");
							xsCall1(xsResult, xsID("add"), xsString(modulePath));
							fxRunProgram(the, path);
						}
						argi++;
					}
					fxRunLoop(the);
					//xsStopProfiling();
				}
				else {
					xsVar(0) = xsNewInstanceOf(xsObjectPrototype);
					xsVar(1) = xsNewHostFunction(process_cwd, 0);
					xsSet(xsVar(0), xsID("cwd"), xsVar(1));
				#ifdef mxDebug
					xsSet(xsVar(0), xsID("debug"), xsTrue);
				#else
					xsSet(xsVar(0), xsID("debug"), xsFalse);
				#endif
					xsVar(1) = xsNewHostFunction(process_execArgv, 0);
					xsSet(xsVar(0), xsID("execArgv"), xsVar(1));
					xsVar(1) = xsNewHostFunction(process_getenv, 0);
					xsSet(xsVar(0), xsID("getenv"), xsVar(1));
					xsVar(1) = xsNewHostFunction(process_then, 1);
					xsSet(xsVar(0), xsID("then"), xsVar(1));
					xsSet(xsVar(0), xsID("platform"), xsString("darwin"));
					xsSet(xsGlobal, xsID("process"), xsVar(0));
		
					strcpy(path, argv[argi]);
					slash = strrchr(path, mxSeparator);
					if (slash) {
						*slash = 0;
						realpath(path, modulePath);
						size = c_strlen(modulePath);
						modulePath[size++] = mxSeparator;
						modulePath[size] = 0;
						xsCall1(xsResult, xsID("add"), xsString(modulePath));
						strcat(modulePath, "modules");
						size = c_strlen(modulePath);
						modulePath[size++] = mxSeparator;
						modulePath[size] = 0;
						xsCall1(xsResult, xsID("add"), xsString(modulePath));
						name = slash + 1;
					
					}
					else {
						realpath(".", modulePath);
						size = c_strlen(modulePath);
						modulePath[size++] = mxSeparator;
						modulePath[size] = 0;
						xsCall1(xsResult, xsID("add"), xsString(modulePath));
						strcat(modulePath, "modules");
						size = c_strlen(modulePath);
						modulePath[size++] = mxSeparator;
						modulePath[size] = 0;
						xsCall1(xsResult, xsID("add"), xsString(modulePath));
						name = path;
					}
					extension = strrchr(name, '.');
					if (extension)
						*extension = 0;
					//xsStartProfiling();
					fxRunModule(the, name);
					//xsStopProfiling();
				}
			}
			xsCatch {
				xsStringValue message = xsToString(xsException);
				fprintf(stderr, "### %s\n", message);
				error = 1;
			}
		}
	}
	xsEndHost(the);
	xsDeleteMachine(machine);
	fxUnmapArchive(archive);
	if (!error && process_then_parameters) {
	#if mxWindows
		error =_spawnvp(_P_WAIT, process_then_parameters[0], process_then_parameters);
		if (error < 0)
			fprintf(stderr, "### Cannot execute %s!\n", process_then_parameters[0]);
	#else
		execvp(process_then_parameters[0], process_then_parameters);
	#endif
	}
	return error;
}
예제 #3
0
void* fxMapArchive(txString base, txCallbackAt callbackAt)
{
	FskErr err = kFskErrNone;
	txArchive* archive = NULL;
	txString p;
	Atom atom;
	int c, i;
	txString* symbol;
	txScript* script;

	bailIfError(FskMemPtrNewClear(sizeof(txArchive), &archive));
	c_strcpy(archive->base, base);
	bailIfError(FskFileMap(base, (unsigned char**)&(archive->address), &(archive->size), 0, &(archive->map)));

	p = archive->address;
	p = fxMapAtom(p, &atom);
	bailAssert(atom.atomType == XS_ATOM_ARCHIVE);
	
	p = fxMapAtom(p, &atom);
	bailAssert(atom.atomType == XS_ATOM_VERSION);
	bailAssert(*p++ == XS_MAJOR_VERSION);
	bailAssert(*p++ == XS_MINOR_VERSION);
	bailAssert(*p++ == XS_PATCH_VERSION);
	if ((*p++) && !callbackAt) {
		char* dot;
		dot = c_strrchr(archive->base, '.');
		if (dot)
			*dot = 0;
		#if mxWindows
			c_strcat(archive->base, ".dll");
		#else
			c_strcat(archive->base, ".so");
		#endif
		bailIfError(FskLibraryLoad(&(archive->library), archive->base));
		bailIfError(FskLibraryGetSymbolAddress(archive->library, "xsHostModuleAt", &callbackAt));
		archive->keys = callbackAt(0);
	}

	p = fxMapAtom(p, &atom);
	bailAssert(atom.atomType == XS_ATOM_SYMBOLS);
	mxDecode2(p, archive->symbolCount);
	c = archive->symbolCount;
	if (c) {
		bailIfError(FskMemPtrNew(c * sizeof(txString), &(archive->symbols)));
		symbol = archive->symbols;
		for (i = 0; i < c; i++) {
			*symbol = p;
			p += c_strlen(p) + 1;
			symbol++;
		}
	}

	p = fxMapAtom(p, &atom);
	bailAssert(atom.atomType == XS_ATOM_PATHS);
	mxDecode2(p, archive->scriptCount);
	c = archive->scriptCount;
	bailIfError(FskMemPtrNewClear(c * sizeof(txScript), &(archive->scripts)));
	script = archive->scripts;
	for (i = 1; i <= c; i++) {
		if (callbackAt)
			script->callback = callbackAt(i);
		script->path = p;
		bailIfNULL(script->path);
		p += c_strlen(p) + 1;
		script++;
	}
	
	script = archive->scripts;
	for (i = 0; i < c; i++) {
		p = fxMapAtom(p, &atom);
		bailAssert(atom.atomType == XS_ATOM_CODE);
		script->codeSize = atom.atomSize - sizeof(atom);
		script->codeBuffer = (txS1*)p;
		p += script->codeSize;
		script++;
	}
	
	p = c_strrchr(archive->base, '/');
	if (p) {
		p++;
		*p = 0;
		archive->baseLength = p - archive->base;
	}
bail:
	if (err) {
		fxUnmapArchive(archive);
		archive = NULL;
	}
	return archive;
}