#include "./test.h"
void ensureCapacity(Path* path, size_t capacity)
{
capacity++;
if (path->capacity >= capacity) return;
size_t newCapacity = 16;
while (newCapacity < capacity) newCapacity *= 2;
path->chars = (char*)realloc(path->chars, newCapacity);
path->capacity = newCapacity;
}
void appendSlice(Path* path, Slice slice)
{
size_t length = slice.end - slice.start;
ensureCapacity(path, path->length + length);
memcpy(path->chars + path->length, slice.start, length);
path->length += length;
path->chars[path->length] = '\0';
}
void pathAppendString(Path* path, const char* string)
{
Slice slice;
slice.start = string;
slice.end = string + strlen(string);
appendSlice(path, slice);
}
inline static bool isSeparator(char c)
{
if (c == '/') return true;
#ifdef _WIN32
if (c == '\\') return true;
#endif
return false;
}
#ifdef _WIN32
inline static bool isDriveLetter(char c)
{
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
}
#endif
inline static size_t absolutePrefixLength(const char* path)
{
#ifdef _WIN32
if (isDriveLetter(path[0]) && path[1] == ':')
{
if (isSeparator(path[2]))
{
return 3;
} else {
return 2;
}
}
#endif
if (isSeparator(path[0])) return 1;
return 0;
}
PathType pathType(const char* path)
{
if (absolutePrefixLength(path) > 0) return PATH_TYPE_ABSOLUTE;
if ((path[0] == '.' && isSeparator(path[1])) ||
(path[0] == '.' && path[1] == '.' && isSeparator(path[2])))
{
return PATH_TYPE_RELATIVE;
}
return PATH_TYPE_SIMPLE;
}
Path* pathNew(const char* string)
{
Path* path = (Path*)malloc(sizeof(Path));
path->chars = (char*)malloc(1);
path->chars[0] = '\0';
path->length = 0;
path->capacity = 0;
pathAppendString(path, string);
return path;
}
void pathFree(Path* path)
{
if (path->chars) free(path->chars);
free(path);
}
void pathDirName(Path* path)
{
for (size_t i = path->length - 1; i < path->length; i--)
{
if (isSeparator(path->chars[i]))
{
path->length = i;
path->chars[i] = '\0';
return;
}
}
path->length = 0;
path->chars[0] = '\0';
}
void pathRemoveExtension(Path* path)
{
for (size_t i = path->length - 1; i < path->length; i--)
{
if (isSeparator(path->chars[i])) return;
if (path->chars[i] == '.')
{
path->length = i;
path->chars[path->length] = '\0';
}
}
}
void pathAppendChar(Path* path, char c)
{
ensureCapacity(path, path->length + 1);
path->chars[path->length++] = c;
path->chars[path->length] = '\0';
}
void pathJoin(Path* path, const char* string)
{
if (path->length > 0 && !isSeparator(path->chars[path->length - 1]))
{
pathAppendChar(path, '/');
}
pathAppendString(path, string);
}
void pathNormalize(Path* path)
{
Slice components[MAX_COMPONENTS];
int numComponents = 0;
char* start = path->chars;
char* end = path->chars;
int leadingDoubles = 0;
for (;;)
{
if (*end == '\0' || isSeparator(*end))
{
if (start != end)
{
size_t length = end - start;
if (length == 1 && start[0] == '.')
{
}
else if (length == 2 && start[0] == '.' && start[1] == '.')
{
if (numComponents > 0)
{
numComponents--;
}
else
{
leadingDoubles++;
}
}
else
{
if (numComponents >= MAX_COMPONENTS)
{
fprintf(stderr, "Path cannot have more than %d path components.\n",
MAX_COMPONENTS);
exit(1);
}
components[numComponents].start = start;
components[numComponents].end = end;
numComponents++;
}
}
while (*end != '\0' && isSeparator(*end)) end++;
start = end;
if (*end == '\0') break;
}
end++;
}
bool needsSeparator = false;
Path* result = pathNew("");
size_t prefixLength = absolutePrefixLength(path->chars);
if (prefixLength > 0)
{
Slice slice;
slice.start = path->chars;
slice.end = path->chars + prefixLength;
appendSlice(result, slice);
}
else if (leadingDoubles > 0)
{
for (int i = 0; i < leadingDoubles; i++)
{
if (needsSeparator) pathAppendChar(result, '/');
pathAppendString(result, "..");
needsSeparator = true;
}
}
else if (path->chars[0] == '.' && isSeparator(path->chars[1]))
{
pathAppendChar(result, '.');
needsSeparator = true;
}
for (int i = 0; i < numComponents; i++)
{
if (needsSeparator) pathAppendChar(result, '/');
appendSlice(result, components[i]);
needsSeparator = true;
}
if (result->length == 0) pathAppendChar(result, '.');
free(path->chars);
path->capacity = result->capacity;
path->chars = result->chars;
path->length = result->length;
free(result);
}
char* pathToString(Path* path)
{
char* string = (char*)malloc(path->length + 1);
memcpy(string, path->chars, path->length);
string[path->length] = '\0';
return string;
}
char* readFile(const char* path)
{
FILE* file = fopen(path, "rb");
if (file == NULL) return NULL;
fseek(file, 0L, SEEK_END);
size_t fileSize = ftell(file);
rewind(file);
char* buffer = (char*)malloc(fileSize + 1);
if (buffer == NULL)
{
fprintf(stderr, "Could not read file \"%s\".\n", path);
exit(WREN_EX_IOERR);
}
size_t bytesRead = fread(buffer, 1, fileSize, file);
if (bytesRead < fileSize)
{
fprintf(stderr, "Could not read file \"%s\".\n", path);
exit(WREN_EX_IOERR);
}
buffer[bytesRead] = '\0';
fclose(file);
return buffer;
}
void vm_write(WrenVM* vm, const char* text)
{
printf("%s", text);
}
void reportError(WrenVM* vm, WrenErrorType type,
const char* module, int line, const char* message)
{
switch (type)
{
case WREN_ERROR_COMPILE:
fprintf(stderr, "[%s line %d] %s\n", module, line, message);
break;
case WREN_ERROR_RUNTIME:
fprintf(stderr, "%s\n", message);
break;
case WREN_ERROR_STACK_TRACE:
fprintf(stderr, "[%s line %d] in %s\n", module, line, message);
break;
}
}
void readModuleComplete(WrenVM* vm, const char* module, WrenLoadModuleResult result)
{
if (result.source) {
free((void*)result.source);
result.source = NULL;
}
}
WrenLoadModuleResult readModule(WrenVM* vm, const char* module)
{
Path* filePath = pathNew(module);
pathAppendString(filePath, ".wren");
char* source = readFile(filePath->chars);
pathFree(filePath);
WrenLoadModuleResult result;
result.source = source;
result.onComplete = readModuleComplete;
return result;
}
const char* resolveModule(WrenVM* vm, const char* importer, const char* module)
{
if (pathType(module) == PATH_TYPE_SIMPLE) return module;
Path* path = pathNew(importer);
pathDirName(path);
pathJoin(path, module);
pathNormalize(path);
char* resolved = pathToString(path);
pathFree(path);
return resolved;
}
bool isModuleAnAPITest(const char* module)
{
if(strncmp(module, "test/api", 8) == 0) return true;
if(strncmp(module, "test/benchmark", 14) == 0) return true;
return false;
}
WrenInterpretResult runFile(WrenVM* vm, const char* path)
{
char* source = readFile(path);
if (source == NULL)
{
fprintf(stderr, "Could not find file \"%s\".\n", path);
exit(WREN_EX_NOINPUT);
}
Path* module = pathNew(path);
if (pathType(module->chars) == PATH_TYPE_SIMPLE)
{
Path* relative = pathNew(".");
pathJoin(relative, path);
pathFree(module);
module = relative;
}
pathRemoveExtension(module);
WrenInterpretResult result = wrenInterpret(vm, module->chars, source);
pathFree(module);
free(source);
return result;
}
int handle_args(int argc, const char* argv[])
{
if (argc < 2)
{
printf("This is a Wren test runner.\nUsage: wren_test [file]\n");
return WREN_EX_USAGE;
}
if (argc == 2 && strcmp(argv[1], "--version") == 0)
{
printf("wren_test is running on Wren version %s\n", WREN_VERSION_STRING);
return 1;
}
return 0;
}