コード例 #1
0
ファイル: kpr.c プロジェクト: basuke/kinomajs
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;
				}
			}
		}
コード例 #2
0
ファイル: FskEnvironment.c プロジェクト: giapdangle/kinomajs
char *FskEnvironmentApply(char *input)
{
	FskErr err;
	char *p;
	char *out = NULL;
	char *start, *end;
	UInt32 outCount = 0;
	char tempName[256];
	UInt32 nameLen;
	char *value;
	Boolean doApply = false;

	if (NULL == input)
		goto bail;

	if ('"' == *input) {
		// special case for string literal - useful when there is leading or trailing white space or [ or ] in the string
		UInt32 len = FskStrLen(input);
		if ('"' == input[len - 1]) {
			err = FskMemPtrNew(len - 1, &out);
			BAIL_IF_ERR(err);
			out[len - 2] = 0;
			FskMemMove(out, input + 1, len - 2);
			goto bail;
		}
	}

	// scan
	p = input;
	while (true) {
		start = FskStrChr(p, '[');
		if (NULL == start) {
			outCount += FskStrLen(p);
			break;
		}
		outCount += start - p;
		end = FskStrChr(start + 1, ']');
		if (NULL == end) {
			outCount += FskStrLen(start);
			break;
		}

		nameLen = end - start - 1;
		if (nameLen > (sizeof(tempName) - 1))
			goto bail;
		FskMemMove(tempName, start + 1, nameLen);
		tempName[nameLen] = 0;

		value = FskEnvironmentGet(tempName);
		outCount += FskStrLen(value);

		doApply = true;
		p = end + 1;
	}

	if (!doApply)
		goto bail;

	// replace
	err = FskMemPtrNew(outCount + 1, &out);
	BAIL_IF_ERR(err);

	out[0] = 0;
	p = input;
	while (true) {
		start = FskStrChr(p, '[');
		if (NULL == start) {
			FskStrCat(out, p);
			break;
		}
		outCount += start - p;
		end = FskStrChr(start + 1, ']');
		if (NULL == end) {
			FskStrCat(out, start);
			break;
		}

		FskStrNCat(out, p, start - p);

		nameLen = end - start - 1;
		FskMemMove(tempName, start + 1, nameLen);
		tempName[nameLen] = 0;
		FskStrCat(out, FskEnvironmentGet(tempName));

		p = end + 1;
	}

bail:
	return out;
}