Exemplo n.º 1
0
FskErr
FskSSLHandshake(void *a, FskNetSocketCreatedCallback callback, void *refCon, Boolean initiate)
{
	FskSSL *fssl = a;
	FskErr err = kFskErrNone;

	fssl->socketCallback = callback;
	fssl->callbackData = refCon;

	xsBeginHost(fssl->vm->the);

	xsTry {
		xsVars(1);

		/* set session._hostData = ssl, just for callback */
		xsVar(0) = xsNewHostObject(NULL);
		xsSetHostData(xsVar(0), fssl);
		xsSet(fssl->ssl, xsID("_hostData"), xsVar(0));
		xsVar(0) = xsNewHostFunction(xs_handshake_finished_callback, 2);
		xsSet(fssl->ssl, xsID("_hostCallback"), xsVar(0));
		xsCall3_noResult(fssl->ssl, xsID("handshake"), fssl->socket, xsVar(0), initiate ? xsTrue : xsFalse);
	} xsCatch {
		if (xsHas(xsException, xsID("code")))
			err = xsToInteger(xsGet(xsException, xsID("code")));
		if (err == kFskErrNone)
			err = kFskErrOperationFailed;
	}

	xsEndHost(fssl->vm->the);
	return err;
}
Exemplo n.º 2
0
void fxParse(xsMachine* the, void* theStream, xsGetter theGetter, xsStringValue thePath, xsIntegerValue theLine, xsFlag theFlag)
{
	xsStreamGetter streamGetter;
	xsSlot* stack = the->stack;
	xsIntegerValue c = xsToInteger(stack[0]);
	stack[c] = xsGet(xsGet(xsGlobal, xsID("xs")), xsID("infoset"));
	stack[c - 1] = xsNewHostObject(NULL);
	streamGetter.stream = theStream;
	streamGetter.getter = theGetter;
	xsSetHostData(stack[c - 1], &streamGetter);
	if (thePath && theLine)
		stack[c - 2] = xsCall3(stack[c], xsID("scan"), stack[c - 1], xsString(thePath), xsInteger(theLine));
	else if (thePath)
		stack[c - 2] = xsCall2(stack[c], xsID("scan"), stack[c - 1], xsString(thePath));
	else
		stack[c - 2] = xsCall1(stack[c], xsID("scan"), stack[c - 1]);
	stack[c] = xsCall2(stack[c], xsID("parse"), stack[c - 2], xsInteger(theFlag));
	the->stack = stack + c;
}
Exemplo n.º 3
0
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;
}
Exemplo n.º 4
0
void KPR_shell_execute(xsMachine* the)
{
	xsStringValue application = NULL;
	xsStringValue command = NULL;
	xsStringValue directory = NULL;
	xsStringValue environment = NULL;
	xsStringValue string;
	xsIntegerValue length;
	KprShellExec exec = NULL;
	STARTUPINFO si;
	SECURITY_ATTRIBUTES sa; 
	xsVars(5);
	xsTry {
		application = getenv("COMSPEC");
		if (!application)
			xsThrowIfFskErr(kFskErrOperationFailed);
		
		string = xsToString(xsArg(0));
		length = FskStrLen(string) + 1;
		xsThrowIfFskErr(FskMemPtrNew(3 + length, &command));
		memcpy(command, "/c ", 3);
		memcpy(command + 3, string, length);

		if (xsFindString(xsArg(1), xsID_directory, &string)) {
			length = FskStrLen(string) + 1;
			xsThrowIfFskErr(FskMemPtrNew(length, &directory));
			memcpy(directory, string, length);
		}
		if (xsFindResult(xsArg(1), xsID_environment)) {
			xsIntegerValue total = 0, length;
			xsVar(1) = xsEnumerate(xsResult);
			for (;;) {
				xsVar(2) = xsCall0(xsVar(1), xsID("next"));
				xsVar(3) = xsGet(xsVar(2), xsID("done"));
				if (xsTest(xsVar(3)))
					break;
				xsVar(3) = xsGet(xsVar(2), xsID("value"));
				xsVar(4) = xsGetAt(xsResult, xsVar(3));
				total += FskStrLen(xsToString(xsVar(3)));
				total++;
				total += FskStrLen(xsToString(xsVar(4)));
				total++;
			}
			total++;
			xsThrowIfFskErr(FskMemPtrNew(total, &environment));
			total = 0;
			xsVar(1) = xsEnumerate(xsResult);
			for (;;) {
				xsVar(2) = xsCall0(xsVar(1), xsID("next"));
				xsVar(3) = xsGet(xsVar(2), xsID("done"));
				if (xsTest(xsVar(3)))
					break;
				xsVar(3) = xsGet(xsVar(2), xsID("value"));
				xsVar(4) = xsGetAt(xsResult, xsVar(3));
				string = xsToString(xsVar(3));
				length = FskStrLen(string);
				memcpy(environment + total, string, length);
				total += length;
				environment[total++] = '=';
				string = xsToString(xsVar(4));
				length = FskStrLen(string);
				memcpy(environment + total, string, length);
				total += length;
				environment[total++] = 0;
			}
			environment[total++] = 0;
		}
		
		xsThrowIfFskErr(FskMemPtrNewClear(sizeof(KprShellExecRecord), &exec));
		xsVar(0) = xsNewHostObject(KPR_shell_execute_destructor);
		exec->the = the;
		exec->slot = xsVar(0);
		xsSetHostData(xsVar(0), exec);
		
		xsResult = xsNewHostFunction(KPR_shell_execute_cancel, 0);
		xsNewHostProperty(xsVar(0), xsID_cancel, xsResult, xsDefault, xsDontDelete | xsDontEnum | xsDontSet);
		if (xsFindResult(xsArg(1), xsID_callback)) {
			xsNewHostProperty(xsVar(0), xsID_callback, xsResult, xsDefault, xsDontDelete | xsDontEnum | xsDontSet);
		}
		else {
			xsNewHostProperty(xsVar(0), xsID_callback, xsNull, xsDefault, xsDontDelete | xsDontEnum | xsDontSet);
		}
		if (xsFindResult(xsArg(1), xsID_stderr)) {
			xsNewHostProperty(xsVar(0), xsID_stderr, xsResult, xsDefault, xsDontDelete | xsDontEnum | xsDontSet);
		}
		else {
			xsNewHostProperty(xsVar(0), xsID_stderr, xsNull, xsDefault, xsDontDelete | xsDontEnum | xsDontSet);
		}
		if (xsFindResult(xsArg(1), xsID_stdout)) {
			xsNewHostProperty(xsVar(0), xsID_stdout, xsResult, xsDefault, xsDontDelete | xsDontEnum | xsDontSet);
		}
		else {
			xsNewHostProperty(xsVar(0), xsID_stdout, xsNull, xsDefault, xsDontDelete | xsDontEnum | xsDontSet);
		}
		
		sa.nLength = sizeof(SECURITY_ATTRIBUTES); 
		sa.bInheritHandle = TRUE; 
		sa.lpSecurityDescriptor = NULL; 
		if (!CreatePipe(&(exec->hReadPipe), &(exec->hWritePipe), &sa, 0))
			xsThrowIfFskErr(kFskErrOperationFailed);
		if (!SetHandleInformation(exec->hReadPipe, HANDLE_FLAG_INHERIT, 0))
			xsThrowIfFskErr(kFskErrOperationFailed);
		ZeroMemory(&si, sizeof(si));
		si.cb = sizeof(STARTUPINFO); 
		si.hStdError = exec->hWritePipe;
		si.hStdOutput = exec->hWritePipe;
		si.hStdInput = GetStdHandle(STD_INPUT_HANDLE);
		si.dwFlags = STARTF_USESTDHANDLES;
		if (!CreateProcess(application, command, NULL, NULL, TRUE, CREATE_NO_WINDOW, environment, directory, &si, &(exec->pi)))
			xsThrowIfFskErr(kFskErrOperationFailed);
			
		xsRemember(exec->slot);
		exec->usage++;
		FskThreadPostCallback(KprHTTPGetThread(), (FskThreadCallback)KPR_shell_execute_async, exec, NULL, NULL, NULL);
		FskMemPtrDispose(environment);
		FskMemPtrDispose(directory);
		FskMemPtrDispose(command);
	}
	xsCatch {
		if (exec) {
			if (exec->pi.hProcess)
				CloseHandle(exec->pi.hProcess);
			if (exec->pi.hThread)
				CloseHandle(exec->pi.hThread);
			if (exec->hWritePipe)
				CloseHandle(exec->hWritePipe);
			if (exec->hReadPipe)
				CloseHandle(exec->hReadPipe);
			FskMemPtrDispose(exec);
		}
		FskMemPtrDispose(environment);
		FskMemPtrDispose(directory);
		FskMemPtrDispose(command);
		xsThrow(xsException);
	}
}
Exemplo n.º 5
0
static xsSlot _xsHostData(xsMachine *the, void *ptr)
{
	the->scratch = xsNewHostObject(NULL);
	xsSetHostData(the->scratch, ptr);
	return the->scratch;
}