Example #1
0
FskErr KprApplicationNew(KprApplication* it, char* url, char* id, Boolean breakOnStart, Boolean breakOnExceptions)
{
	KprCoordinatesRecord coordinates = { kprLeftRight, kprTopBottom, 0, 0, 0, 0, 0, 0 };
	xsAllocation allocation = {
		2 * 1024 * 1024,
		1024 * 1024,
		64 * 1024,
		8 * 1024,
		2048,
		16000,
		1993
	};
	FskErr err = kFskErrNone;
	KprApplication self;
	
	bailIfError(FskMemPtrNewClear(sizeof(KprApplicationRecord), it));
	self = *it;
	FskInstrumentedItemNew(self, NULL, &KprApplicationInstrumentation);
	self->dispatch = &KprApplicationDispatchRecord;
	self->flags = kprContainer | kprClip | kprVisible;
	KprContentInitialize((KprContent)self, &coordinates, NULL, NULL);
	bailIfError(KprURLMerge(gShell->url, url, &self->url));	
	if (id) {
		self->id = FskStrDoCopy(id);	
		bailIfNULL(self->id);
	}
	self->the = xsAliasMachine(&allocation, gShell->root, self->url, self);
	if (!self->the) 
		BAIL(kFskErrMemFull);
	FskInstrumentedItemSendMessageNormal(self, kprInstrumentedContentCreateMachine, self);
	xsBeginHost(self->the);
	xsResult = xsNewHostFunction(KPR_include, 1);
	xsSet(xsResult, xsID("uri"), xsString(self->url));
	xsNewHostProperty(xsGlobal, xsID("include"), xsResult, xsDontDelete | xsDontSet, xsDontScript | xsDontDelete | xsDontSet);
	xsResult = xsNewHostFunction(KPR_require, 1);
	xsSet(xsResult, xsID("uri"), xsString(self->url));
	xsNewHostProperty(xsGlobal, xsID("require"), xsResult, xsDontDelete | xsDontSet, xsDontScript | xsDontDelete | xsDontSet);
	xsResult = xsNewInstanceOf(xsGet(xsGet(xsGlobal, xsID("KPR")), xsID("application")));
	self->slot = xsResult;
	xsSetHostData(xsResult, self);
	(void)xsCall1(xsGet(xsGlobal, xsID("Object")), xsID("seal"), xsResult);
	xsNewHostProperty(xsGlobal, xsID("application"), xsResult, xsDontDelete | xsDontSet, xsDontScript | xsDontDelete | xsDontSet);
	xsNewHostProperty(xsGlobal, xsID("shell"), xsNull, xsDontDelete | xsDontSet, xsDontScript | xsDontDelete | xsDontSet);
	if (breakOnStart)
		xsDebugger();
    if (breakOnExceptions)
		(void)xsCall1(xsGet(xsGet(xsGlobal, xsID("xs")), xsID("debug")), xsID("setBreakOnException"), xsBoolean(breakOnExceptions));
	(void)xsCall1(xsGlobal, xsID("include"), xsString(self->url));
	xsEndHost(self->the);
	KprContentChainPrepend(&gShell->applicationChain, self, 0, NULL);
bail:
	return err;
}
Example #2
0
void KPR_system_alert(xsMachine* the)
{
	int argc = xsToInteger(xsArgc);
	MSGBOXPARAMSW params;
	xsStringValue string;
	xsIntegerValue result;
	xsVars(1);
	params.cbSize = sizeof(params);
	params.hwndOwner = NULL;
	params.hInstance = FskMainGetHInstance();
	params.lpszText = NULL;
	params.lpszCaption = xsStringToWideString("Kinoma Code");
	params.dwStyle = MB_ICONSTOP;
	params.dwContextHelpId = 0;
	params.lpfnMsgBoxCallback = NULL;
	params.dwLanguageId = MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT);
	if ((argc > 0) && xsTest(xsArg(0))) {
		if (xsFindString(xsArg(0), xsID_type, &string)) {
			if (!FskStrCompare(string, "about"))
				params.dwStyle = MB_ICONINFORMATION;
			else if (!FskStrCompare(string, "stop"))
				params.dwStyle = MB_ICONSTOP;
			else if (!FskStrCompare(string, "note"))
				params.dwStyle = MB_ICONEXCLAMATION;
		}
		if (xsFindResult(xsArg(0), xsID_prompt)) {
			xsVar(0) = xsResult;
		}
		if (xsFindResult(xsArg(0), xsID_info)) {
			xsVar(0) = xsCall1(xsVar(0), xsID("concat"), xsString("\n\n"));
			xsVar(0) = xsCall1(xsVar(0), xsID("concat"), xsResult);
		}
		params.lpszText = xsStringToWideString(xsToString(xsVar(0)));
		if (xsFindResult(xsArg(0), xsID_buttons)) {
			if (xsIsInstanceOf(xsResult, xsArrayPrototype)) {
				xsIntegerValue c = xsToInteger(xsGet(xsResult, xsID_length));
				if (c == 3)
					params.dwStyle |= MB_YESNOCANCEL;
				else if (c == 2)
					params.dwStyle |= MB_OKCANCEL;
				else
					params.dwStyle |= MB_OK;
			}
		}
	}
	result = MessageBoxIndirectW(&params);
	if (params.lpszText)
		CoTaskMemFree((LPVOID *)params.lpszText);
	if (params.lpszCaption)
		CoTaskMemFree((LPVOID *)params.lpszCaption);
	if ((argc > 1) && xsTest(xsArg(1)))
		(void)xsCallFunction1(xsArg(1), xsNull, ((result == IDYES) || (result == IDOK)) ? xsTrue : (result == IDNO) ? xsFalse : xsUndefined);
}
Example #3
0
void KprDebugMachineDataReader(FskThreadDataHandler handler UNUSED, FskThreadDataSource source UNUSED, void *refCon)
{
	FskErr err = kFskErrNone;
	KprDebugMachine self = refCon;
	KprDebug debug = self->debug;
	int size;

	err = FskNetSocketRecvTCP(self->socket, self->buffer, XS_BUFFER_COUNT, &size);
	if (size > 0) {
		xsBeginHostSandboxCode(debug->the, debug->code);
		xsVars(4);
		KprDebugMachineParse(self, self->buffer, size);
		if (self->done) {
			self->state = XS_HEADER_STATE;
			self->dataIndex = 0;
			
			if (self->broken) {
				xsCall0(xsGet(xsGlobal, xsID_KPR), xsID_gotoFront);
			}
			else {
				xsCall1(debug->slot, xsID_go, xsString(self->address));
				KprDebugMachineCallback(self, "gone");
			}
		}
		xsEndHostSandboxCode();
	}
	if (err) {
		KprDebugMachineDispose(self);
	}
}
Example #4
0
void KprMessageScriptTargetTransform(void* it, KprMessage message, xsMachine* machine)
{
	KprMessageScriptTarget self = it;
	if (message->error) return;
	xsBeginHostSandboxCode(machine, NULL);
	{
		xsVars(2);
		{
			xsTry {
				void* data;
				UInt32 size; 
				KprMessageGetResponseBody(message, &data, &size);
				if (data && size) {
					xsIndex id = xsID(self->name);
					if (xsHas(xsGlobal, id)) {
						xsVar(0) = xsGet(xsGlobal, id);
						xsVar(1) = xsNewInstanceOf(xsChunkPrototype);
						xsSetHostData(xsVar(1), data);
						xsSetHostDestructor(xsVar(1) , NULL);
						xsSet(xsVar(1), xsID("length"), xsInteger(size));
						xsResult = xsCall1(xsVar(0), xsID("parse"), xsVar(1));	
						self->result = xsMarshall(xsResult);
					}
				}
			}
			xsCatch {
			}
		}
	}
	xsEndHostSandboxCode();
}
Example #5
0
void xs_parse(xsMachine* the)
{
	xsIntegerValue c = xsToInteger(xsArgc);
	xsVars(2);
	if (c < 1)
		xsSyntaxError("no buffer");
	xsVar(0) = xsGet(xsGet(xsGlobal, xsID_xs), xsID_infoset);
	if (c > 3)
		xsVar(1) = xsCall3(xsVar(0), xsID_scan, xsArg(0), xsArg(2), xsArg(3));
	else if (c > 2)
		xsVar(1) = xsCall2(xsVar(0), xsID_scan, xsArg(0), xsArg(2));
	else if (c > 1)
		xsVar(1) = xsCall1(xsVar(0), xsID_scan, xsArg(0));
	else
		xsVar(1) = xsCall1(xsVar(0), xsID_scan, xsArg(0));
	if (c > 1)
		xsResult = xsCall2(xsVar(0), xsID_parse, xsVar(1), xsArg(1));
	else
		xsResult = xsCall1(xsVar(0), xsID_parse, xsVar(1));
}
Example #6
0
void KPR_host_set_debugging(xsMachine* the)
{
#ifdef mxDebug
	KprHost self = xsGetHostData(xsThis);
	KprApplication application = (KprApplication)self->first;
	Boolean flag = xsToBoolean(xsArg(0));
	xsBeginHost(application->the);
	{
		(void)xsCall1(xsGet(xsGet(xsGlobal, xsID("xs")), xsID("debug")), xsID("setConnected"), xsBoolean(flag));
	}
	xsEndHost(application->the);
#endif
}
Example #7
0
xsBooleanValue fxFindModuleLoadAsDirectory(xsMachine* the, xsStringValue base, xsStringValue name, xsIndex* id)
{
	char node[1024];
	UInt32 nodeSize;
	xsStringValue slash;
	FskErr err;
	FskFileMapping map = NULL;
	xsBooleanValue result = 0;
	xsStringValue path = NULL;
	unsigned char* data;
	FskInt64 size;
	xsSlot slot;
	
	nodeSize = sizeof(node);
	FskStrNCopy(node, base, nodeSize);
	slash = FskStrRChr(node, '/');
	if (slash)
		*slash = 0;
	FskStrNCat(node, "/", nodeSize);
	FskStrNCat(node, name, nodeSize);
	if (FskStrTail(node, "/") != 0)
		FskStrNCat(node, "/", nodeSize);
	FskStrNCat(node, "package.json", nodeSize);
	err = KprURLToPath(node, &path);
	if (err == kFskErrNone) {
		err = FskFileMap(path, &data, &size, 0, &map);
		if (err == kFskErrNone) {
			xsTry {
				slot = xsNewInstanceOf(xsChunkPrototype);
				xsSetHostData(slot, data);
				xsSetHostDestructor(slot, NULL);
				xsSet(slot, xsID("length"), xsInteger(size));
				slot = xsCall1(xsGet(xsGlobal, xsID("JSON")), xsID("parse"), slot);
			}
			xsCatch {
				slot = xsUndefined;
			}
			if (xsTest(slot)) {
				slot = xsGet(slot, xsID("main"));
				if (xsTest(slot)) {
					FskStrNCopy(node, name, nodeSize);
					if (FskStrTail(node, "/") != 0)
						FskStrNCat(node, "/", nodeSize);
					FskStrNCat(node, xsToString(slot), nodeSize);
					if (fxFindModuleLoadAsFile(the, base, node, id))
						result = 1;
				}
			}
		}
Example #8
0
int main(int argc, char* argv[]) 
{
	xsAllocation anAllocation = {
		2048 * 1024, /* initialChunkSize */
		512 * 1024, /* incrementalChunkSize */
		1000 * 1000, /* initialHeapCount */	
		50 * 1000, /* incrementalHeapCount */
		1024, /* stackCount */
		8192, /* symbolCount */
		1993 /* symbolModulo */
	};
	xsMachine* aMachine;
	int argi;
	int result = 1;

	for (argi = 0; argi < argc; argi++){
		result = strncmp(argv[argi], "--help", strlen("--help"));
		if(!result) {
			printHelp();
			exit(0);
		}
	}
	
	result = 1;

	gxProperties = calloc(mxPropertyModulo, sizeof(xsSlot));
	if (gxProperties) {
		aMachine = xsNewMachine(&anAllocation, &kprconfigGrammar, NULL);
		if (aMachine) {
			xsBeginHost(aMachine);
			{
				xsTry {
					xsResult = xsNewInstanceOf(xsArrayPrototype);
					for (argi = 0; argi < argc; argi++)
						xsSet(xsResult, argi, xsString(argv[argi]));
					(void)xsCall1(xsGlobal, xsID("main"), xsResult);
					result = 0;
				}
				xsCatch {
					char* aMessage = xsToString(xsGet(xsException, xsID("message")));
					fprintf(stderr, "### %s\n", aMessage);
				}
			}
			xsEndHost(aMachine);
			xsDeleteMachine(aMachine);
		}
		else
			fprintf(stderr, "### Cannot allocate machine!\n");
	}
Example #9
0
void xsToolSearchProperty(xsMachine* the)
{
	xsStringValue aString;
	xsIntegerValue aSum;

	aString = xsToString(xsArg(0));
	aSum = 0;
	while(*aString != 0) {
		aSum = (aSum << 1) + *aString++;
	}
	aSum &= 0x7FFFFFFF;
	xsResult = gxProperties[aSum % mxPropertyModulo];
	if (xsTypeOf(xsResult) == xsReferenceType)
		xsResult = xsCall1(xsResult, xsID("searchProperty"), xsArg(0));
}
Example #10
0
void KPR_debug_get_machines(xsMachine *the)
{
	KprDebug self = xsGetHostData(xsThis);
	KprDebugMachine machine = NULL;
	xsEnterSandbox();
	xsVars(1);
	{
		xsResult = xsNewInstanceOf(xsArrayPrototype);
		for (machine = self->machine; machine; machine = machine->next) {
			xsVar(0) = xsNewInstanceOf(xsObjectPrototype);
			xsNewHostProperty(xsVar(0), xsID("address"), xsString(machine->address), xsDefault, xsDontScript);
			if (machine->title)
				xsNewHostProperty(xsVar(0), xsID("title"), xsString(machine->title), xsDefault, xsDontScript);
			xsCall1(xsResult, xsID_push, xsVar(0));
		}
	}
	xsLeaveSandbox();
}
Example #11
0
void KPR_host_set_profilingDirectory(xsMachine* the)
{
	KprHost self = xsGetHostData(xsThis);
	KprApplication application = (KprApplication)self->first;
	char* directory = NULL;
	if (xsTest(xsArg(0))) {
		xsThrowIfFskErr(KprURLToPath(xsToString(xsArg(0)), &directory));
	}
	xsBeginHost(application->the);
	{
		if (directory) {
			xsResult = xsString(directory);
			FskMemPtrDispose(directory);
		}
		(void)xsCall1(xsGet(xsGlobal, xsID("xs")), xsID("setProfilingDirectory"), xsResult);
	}
	xsEndHost(application->the);
}
Example #12
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;
}
Example #13
0
FskErr
FskSSLClose(void *a)
{
	FskSSL *fssl = a;
	FskErr err = kFskErrNone;

	if (fssl->skt == NULL)
		return kFskErrOperationFailed;

	xsBeginHost(fssl->vm->the);
	xsTry {
		xsResult = xsCall1(fssl->ssl, xsID("close"), fssl->socket);
	} xsCatch {
		if (xsHas(xsException, xsID("code")))
			err = xsToInteger(xsGet(xsException, xsID("code")));
		if (err == kFskErrNone)
			err = kFskErrOperationFailed;
	}
	xsEndHost(fssl->vm->the);
	return err;
}
Example #14
0
int main(int argc, char* argv[])
{
    xsAllocation anAllocation = {
        2048 * 1024, /* initialChunkSize */
        512 * 1024, /* incrementalChunkSize */
        1000 * 1000, /* initialHeapCount */
        50 * 1000, /* incrementalHeapCount */
        64 * 1024, /* stackCount */
        4096, /* symbolCount */
        1993 /* symbolModulo */
    };
    xsMachine* aMachine;
    int argi;
    int result = 1;

    aMachine = xsNewMachine(&anAllocation, &kpr2jsGrammar, NULL);
    if (aMachine) {
        xsBeginHost(aMachine);
        {
            xsTry {
                xsResult = xsNewInstanceOf(xsArrayPrototype);
                for (argi = 0; argi < argc; argi++)
                    xsSet(xsResult, argi, xsString(argv[argi]));
                (void)xsCall1(xsGlobal, xsID("main"), xsResult);
                result = 0;
            }
            xsCatch {
                char* aMessage = xsToString(xsGet(xsException, xsID("message")));
                if (*aMessage)
                    fprintf(stderr, "### %s\n", aMessage);
                else
                    fprintf(stderr, "### XML error(s)\n");
            }
        }
        xsEndHost(aMachine);
        xsDeleteMachine(aMachine);
    }
    else
Example #15
0
FskErr
FskSSLFlush(void *a)
{
	FskSSL *fssl = a;
	FskErr err = kFskErrNone;

	if (fssl->skt == NULL)
		return kFskErrOperationFailed;

	xsBeginHost(fssl->vm->the);
	xsTry {
		xsVars(1);
		xsCall1_noResult(fssl->socket, xsID("attachData"), xsHostData(fssl->skt));
		xsResult = xsCall1(fssl->ssl, xsID("flush"), fssl->socket);
		err = xsToBoolean(xsResult) ? kFskErrNone: kFskErrNoData;
	} xsCatch {
		if (xsHas(xsException, xsID("code")))
			err = xsToInteger(xsGet(xsException, xsID("code")));
		if (err == kFskErrNone)
			err = kFskErrOperationFailed;
	}
	xsEndHost(fssl->vm->the);
	return err;
}
Example #16
0
void console_log(xsMachine* the)
{
	xsIntegerValue c = xsToInteger(xsArgc), i;
	xsBooleanValue space = 0;
	xsVars(4);
	console_log_depth++;
	for (i = 0; i < c; i++) {
		if (space)
			fprintf(stdout,  " ");
		else
			space = 1;
		switch (xsTypeOf(xsArg(i))) {
		case xsUndefinedType:
		case xsNullType:
		case xsBooleanType:
		case xsIntegerType:
		case xsNumberType:
			fprintf(stdout,  "%s", xsToString(xsArg(i)));
			break;
		case xsSymbolType:
			xsResult = xsCall1(xsGlobal, xsID("String"), xsArg(i));
			fprintf(stdout,  "%s", xsToString(xsResult));
			break;
		case xsStringType:
		case xsStringXType:
			if ((console_log_depth == 1) && (i == 0))
				fprintf(stdout,  "%s", xsToString(xsArg(i)));
			else
				fprintf(stdout,  "'%s'", xsToString(xsArg(i)));
			break;
		case xsReferenceType:
			if (console_log_depth < 3) {
				xsBooleanValue comma = 0;
				if (xsHas(xsArg(i), xsID("length"))) {
					xsIntegerValue length = xsToInteger(xsGet(xsArg(i), xsID("length"))), index;
					fprintf(stdout,  "[");
					for (index = 0; index < length; index++) {
						if (comma)
							fprintf(stdout,  ",");
						else
							comma = 1;
						fprintf(stdout,  " ");
						if (xsHas(xsArg(i), (xsIndex)index)) {
							xsVar(1) = xsGet(xsArg(i), (xsIndex)index);
							fxPush(xsVar(1));
							fxPushCount(the, 1);
							fxPush(xsThis);
							fxPush(xsFunction);
							fxCall(the);
						}
					}
					fprintf(stdout,  " ]");
				}
				else {
					fprintf(stdout,  "{");
					xsVar(0) = xsEnumerate(xsArg(i));
					for (;;) {
						xsVar(1) = xsCall0(xsVar(0), xsID("next"));
						if (xsTest(xsGet(xsVar(1), xsID("done"))))
							break;
						xsVar(2) = xsGet(xsVar(1), xsID("value"));
						xsVar(3) = xsGetAt(xsArg(i), xsVar(2));
						if (comma)
							fprintf(stdout,  ",");
						else
							comma = 1;
						fprintf(stdout,  " %s: ", xsToString(xsVar(2)));
						fxPush(xsVar(3));
						fxPushCount(the, 1);
						fxPush(xsThis);
						fxPush(xsFunction);
						fxCall(the);
					}
					fprintf(stdout,  " }");
				}
			}
			else
				fprintf(stdout,  "%s", xsToString(xsArg(i)));
			break;
		}
	}
	console_log_depth--;
	if (!console_log_depth)
		fprintf(stdout,  "\n");
}
Example #17
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;
}
Example #18
0
void KprFILEServiceInvoke(KprService service UNUSED, KprMessage message)
{
	FskErr err = kFskErrNone;
	char* path = NULL;
	FskFileInfo info;
	FskDirectoryIterator iterator = NULL;
	char* pathName = NULL;
	char* sniff = NULL;
	FskFile fref = NULL;
	char *name = NULL;
	if (KprMessageContinue(message)) {
		if (!message->method || FskStrCompare(message->method, "GET")) {
			bailIfError(KprURLToPath(message->url, &path));
			bailIfError(FskFileGetFileInfo(path, &info));
			if (kFskDirectoryItemIsDirectory == info.filetype) {
				unsigned char buffer[4096];
				UInt32 itemType;
				double date;
				UInt32 size;
				xsBeginHostSandboxCode(gFILEService.machine, NULL);
				{
					xsVars(3);
					{
						xsTry {
							xsVar(0) = xsGet(xsGlobal, xsID("Files"));
							xsResult = xsNewInstanceOf(xsArrayPrototype);
							bailIfError(FskDirectoryIteratorNew(path, &iterator, 0));
							while (kFskErrNone == FskDirectoryIteratorGetNext(iterator, &name, &itemType)) {
								if (name[0] == '.') {
									FskMemPtrDisposeAt(&name);
									continue;
								}
								if (kFskDirectoryItemIsFile == itemType) {
									pathName = FskStrDoCat(path, name);
									bailIfError(FskFileGetFileInfo(pathName, &info));
									bailIfError(FskFileOpen(pathName, kFskFilePermissionReadOnly, &fref));
									bailIfError(FskFileRead(fref, sizeof(buffer), buffer, &size));
									FskFileClose(fref);
									fref = NULL;
									if (kFskErrNone == FskMediaPlayerSniffForMIME(buffer, size, NULL, pathName, &sniff)) {
									}
									else if (kFskErrNone == FskImageDecompressSniffForMIME(buffer, size, NULL, pathName, &sniff)) {
									}
									FskMemPtrDispose(pathName);
									pathName = NULL;
								}
								xsVar(1) = xsNewInstanceOf(xsObjectPrototype);
								xsNewHostProperty(xsVar(1), xsID("name"), xsString(name), xsDefault, xsDontScript);
								xsNewHostProperty(xsVar(1), xsID("path"), xsString(name), xsDefault, xsDontScript);
								FskStrCopy((char*)buffer, message->url);
								FskStrCat((char*)buffer, name);
								if (kFskDirectoryItemIsDirectory == itemType) {
									xsVar(2) = xsGet(xsVar(0), xsID("directoryType"));
									FskStrCat((char*)buffer, "/");
								}
								else if (kFskDirectoryItemIsFile == itemType) {
									xsVar(2) = xsGet(xsVar(0), xsID("fileType"));
									if (info.fileModificationDate > info.fileCreationDate)
										date = info.fileModificationDate * 1000.0;	
									else
										date = info.fileCreationDate * 1000.0;
									xsNewHostProperty(xsVar(1), xsID("date"), xsNumber(date), xsDefault, xsDontScript);
									xsNewHostProperty(xsVar(1), xsID("size"), xsNumber(info.filesize), xsDefault, xsDontScript);
									if (sniff) {
										xsNewHostProperty(xsVar(1), xsID("mime"), xsString(sniff), xsDefault, xsDontScript);
										FskMemPtrDispose(sniff);
										sniff = NULL;
									}
								}
								else
									xsVar(2) = xsGet(xsVar(0), xsID("linkType"));
								xsNewHostProperty(xsVar(1), xsID("type"), xsVar(2), xsDefault, xsDontScript);
								xsNewHostProperty(xsVar(1), xsID("url"), xsString((char*)buffer), xsDefault, xsDontScript);
								(void)xsCall1(xsResult, xsID("push"), xsVar(1));
								FskMemPtrDisposeAt(&name);
							}
							xsResult = xsCall1(xsGet(xsGlobal, xsID("JSON")), xsID("stringify"), xsResult);
							message->response.body = FskStrDoCopy(xsToString(xsResult));
							message->response.size = FskStrLen(message->response.body);
							KprMessageTransform(message, gFILEService.machine);
						}
						xsCatch {
						}
					}
				}
				xsEndHostSandboxCode();
			}
			else if (kFskDirectoryItemIsFile == info.filetype) {
Example #19
0
void
xs_console_log(xsMachine* the)
{
	xsIntegerValue c = xsToInteger(xsArgc), i;
	xsBooleanValue space = 0;
	xsBooleanValue comma = 0;
	xsBooleanValue nonl = 0;
	int options = 0;
	FILE *console_out = stdout;

	xsVars(4);
	console_log_depth++;
	for (i = 0; i < c; i++) {
		if (space && !nonl)
			CONSOLE_OUT("\n");
		else
			space = 1;
		switch (xsTypeOf(xsArg(i))) {
		case xsUndefinedType:
		case xsNullType:
		case xsBooleanType:
		case xsIntegerType:
		case xsNumberType:
			CONSOLE_OUT("%s", xsToString(xsArg(i)));
			break;
		case xsSymbolType:
			xsResult = xsCall1(xsGlobal, xsID("String"), xsArg(i));
			CONSOLE_OUT("%s", xsToString(xsResult));
			break;
		case xsStringType:
		case xsStringXType:
			if (i == 0 || options) {
				const char *opt = xsToString(xsArg(i));
				options = 0;
				if (strcmp(opt, "-n") == 0) {
					nonl++;
					options++;
				}
				else if (strcmp(opt, "-stderr") == 0) {
					console_out = stderr;
					options++;
				}
				else if (strcmp(opt, "-stdout") == 0) {
					console_out = stdout;
					options++;
				}
				else if (strncmp(opt, "-", 1) == 0)	/* end of the option args */
					break;
				if (options) {
					space = 0;
					break;
				}
			}
			if (console_log_depth == 1)
				CONSOLE_OUT("%s", xsToString(xsArg(i)));
			else
				CONSOLE_OUT("\"%s\"", xsToString(xsArg(i)));
			break;
		case xsReferenceType:
			if (xsIsInstanceOf(xsArg(i), xsFunctionPrototype)) {
				CONSOLE_OUT("function(...) { ... }");
			}
			else if (xsIsInstanceOf(xsArg(i), xsArrayPrototype)) {
				xsGet(xsVar(0), xsArg(i), xsID("length"));
				xsIntegerValue length = xsToInteger(xsVar(0)), index;
				CONSOLE_OUT("[\n");
				for (index = 0; index < length; index++) {
					xsGet(xsVar(1), xsArg(i), (xsIndex)index);
					if (comma)
						CONSOLE_OUT(",\n");
					else
						comma = 1;
					indent(the, console_out, 0);
					fxPush(xsVar(1));
					fxPushCount(the, 1);
					fxPush(xsThis);
					fxPush(xsFunction);
					fxCall(the);
				}
				CONSOLE_OUT("\n"); indent(the, console_out, -1); CONSOLE_OUT("]");
			}
			else {
				CONSOLE_OUT( "{\n");
				xsVar(0) = xsEnumerate(xsArg(i));
				for (;;) {
					xsVar(1) = xsCall0(xsVar(0), xsID("next"));
					xsGet(xsVar(2), xsVar(1), xsID("done"));
					if (xsTest(xsVar(2)))
						break;
					xsGet(xsVar(2), xsVar(1), xsID("value"));
					xsGetAt(xsVar(3), xsArg(i), xsVar(2));
					if (comma)
						CONSOLE_OUT( ",\n");
					else
						comma = 1;
					indent(the, console_out, 0); CONSOLE_OUT("%s: ", xsToString(xsVar(2)));
					fxPush(xsVar(3));
					fxPushCount(the, 1);
					fxPush(xsThis);
					fxPush(xsFunction);
					fxCall(the);
				}
				CONSOLE_OUT("\n"); indent(the, console_out, -1); CONSOLE_OUT("}");
			}
			break;
		}
	}
	console_log_depth--;
	if (!console_log_depth && !nonl)
		CONSOLE_OUT("\n");
}
Example #20
0
void xsToolListDirectory(xsMachine* the)
{
#if mxWindows
	xsStringValue path, name = NULL;
	UINT32 length, index;
	UINT16 *pathW = NULL;
	HANDLE findHandle = INVALID_HANDLE_VALUE;
	WIN32_FIND_DATAW findData;

	xsTry {
		xsVars(1);
		xsResult = xsNewInstanceOf(xsArrayPrototype);
	
		path = xsToString(xsArg(0));
		length = strlen(path);
		pathW = malloc((length + 3) * 2);
		xsElseError(pathW);
		MultiByteToWideChar(CP_UTF8, 0, path, length + 1, pathW, length + 1);
		for (index = 0; index < length; index++) {
			if (pathW[index] == '/')
				pathW[index] = '\\';
		}
		pathW[length] = '\\';
		pathW[length + 1] = '*';
		pathW[length + 2] = 0;
		findHandle = FindFirstFileW(pathW, &findData);
		if (findHandle != INVALID_HANDLE_VALUE) {
			do {
				if ((findData.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN) ||
					!wcscmp(findData.cFileName, L".") ||
					!wcscmp(findData.cFileName, L".."))
					continue;
				length = wcslen(findData.cFileName);
				name = malloc((length + 1) * 2);
				xsElseError(name);
				WideCharToMultiByte(CP_UTF8, 0, findData.cFileName, length + 1, name, length + 1, NULL, NULL);
				xsVar(0) = xsNewInstanceOf(xsObjectPrototype);
				xsSet(xsVar(0), xsID("name"), xsString(name));
				if (findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
					xsSet(xsVar(0), xsID("directory"), xsTrue);
				else
					xsSet(xsVar(0), xsID("directory"), xsFalse);
				xsCall1(xsResult, xsID("push"), xsVar(0));
				free(name);
				name = NULL;
			} while (FindNextFileW(findHandle, &findData));
		}
	}
	xsCatch {
	}
	if (name)
		free(name);
	if (findHandle != INVALID_HANDLE_VALUE)
		FindClose(findHandle);
	if (pathW)
		free(pathW);
#else
    DIR* dir;
	char path[1024];
	int length;

	xsVars(1);
	xsResult = xsNewInstanceOf(xsArrayPrototype);
	dir = opendir(xsToStringBuffer(xsArg(0), path, sizeof(path) - 1));
	length = strlen(path);
	path[length] = '/';
	length++;
	if (dir) {
		struct dirent *ent;
		while ((ent = readdir(dir))) {
			struct stat a_stat;
			if (ent->d_name[0] == '.')
				continue;
			strcpy(path + length, ent->d_name);
			if (!stat(path, &a_stat)) {
				xsVar(0) = xsNewInstanceOf(xsObjectPrototype);
				xsSet(xsVar(0), xsID("name"), xsString(ent->d_name));
				if (S_ISDIR(a_stat.st_mode))
					xsSet(xsVar(0), xsID("directory"), xsTrue);
				else
					xsSet(xsVar(0), xsID("directory"), xsFalse);
				(void)xsCall1(xsResult, xsID("push"), xsVar(0));
			}
		}
		closedir(dir);
	}
#endif
}