From 66b89a493fce2c11b49be4ca5441b43997c505f3 Mon Sep 17 00:00:00 2001 From: Bob Nystrom Date: Sun, 13 Sep 2015 22:29:47 -0700 Subject: [PATCH] Combine io.c and vm.c. (This is mainly to free up io.c as a built in module.) --- project/xcode/wren.xcodeproj/project.pbxproj | 6 -- script/wren.mk | 3 +- src/cli/io.c | 101 ------------------ src/cli/io.h | 25 ----- src/cli/main.c | 1 - src/cli/vm.c | 103 ++++++++++++++++++- test/api/main.c | 1 - 7 files changed, 100 insertions(+), 140 deletions(-) delete mode 100644 src/cli/io.c delete mode 100644 src/cli/io.h diff --git a/project/xcode/wren.xcodeproj/project.pbxproj b/project/xcode/wren.xcodeproj/project.pbxproj index 8689aff2..5e493348 100644 --- a/project/xcode/wren.xcodeproj/project.pbxproj +++ b/project/xcode/wren.xcodeproj/project.pbxproj @@ -24,7 +24,6 @@ 29512C811B91F8EB008C10E6 /* libuv.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 29512C801B91F8EB008C10E6 /* libuv.a */; }; 29512C821B91F901008C10E6 /* libuv.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 29512C801B91F8EB008C10E6 /* libuv.a */; }; 2986F6D71ACF93BA00BCE26C /* wren_primitive.c in Sources */ = {isa = PBXBuildFile; fileRef = 2986F6D51ACF93BA00BCE26C /* wren_primitive.c */; }; - 29C8A92F1AB71C1C00DEC81D /* io.c in Sources */ = {isa = PBXBuildFile; fileRef = 29C8A92E1AB71C1C00DEC81D /* io.c */; }; 29C8A9331AB71FFF00DEC81D /* vm.c in Sources */ = {isa = PBXBuildFile; fileRef = 29C8A9311AB71FFF00DEC81D /* vm.c */; }; 29DE39531AC3A50A00987D41 /* wren_meta.c in Sources */ = {isa = PBXBuildFile; fileRef = 29DE39511AC3A50A00987D41 /* wren_meta.c */; }; /* End PBXBuildFile section */ @@ -82,8 +81,6 @@ 2986F6D51ACF93BA00BCE26C /* wren_primitive.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = wren_primitive.c; path = ../../src/vm/wren_primitive.c; sourceTree = ""; }; 2986F6D61ACF93BA00BCE26C /* wren_primitive.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = wren_primitive.h; path = ../../src/vm/wren_primitive.h; sourceTree = ""; }; 29AB1F061816E3AD004B501E /* wren */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = wren; sourceTree = BUILT_PRODUCTS_DIR; }; - 29C8A92E1AB71C1C00DEC81D /* io.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = io.c; path = ../../src/cli/io.c; sourceTree = ""; }; - 29C8A9301AB71C3300DEC81D /* io.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = io.h; path = ../../src/cli/io.h; sourceTree = ""; }; 29C8A9311AB71FFF00DEC81D /* vm.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = vm.c; path = ../../src/cli/vm.c; sourceTree = ""; }; 29C8A9321AB71FFF00DEC81D /* vm.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = vm.h; path = ../../src/cli/vm.h; sourceTree = ""; }; 29D009A61B7E3993000CE58C /* main.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = main.c; path = ../../test/api/main.c; sourceTree = ""; }; @@ -160,8 +157,6 @@ 29205CA91AB4E67B0073018D /* cli */ = { isa = PBXGroup; children = ( - 29C8A9301AB71C3300DEC81D /* io.h */, - 29C8A92E1AB71C1C00DEC81D /* io.c */, 29205C8E1AB4E5C90073018D /* main.c */, 291647C61BA5EC5E006142EE /* modules.h */, 291647C51BA5EC5E006142EE /* modules.c */, @@ -301,7 +296,6 @@ 29205C9D1AB4E6430073018D /* wren_utils.c in Sources */, 29205C9E1AB4E6430073018D /* wren_value.c in Sources */, 29205C9F1AB4E6430073018D /* wren_vm.c in Sources */, - 29C8A92F1AB71C1C00DEC81D /* io.c in Sources */, 29DE39531AC3A50A00987D41 /* wren_meta.c in Sources */, 29205C8F1AB4E5C90073018D /* main.c in Sources */, ); diff --git a/script/wren.mk b/script/wren.mk index 29c37f00..0824c3c9 100644 --- a/script/wren.mk +++ b/script/wren.mk @@ -153,8 +153,7 @@ lib/lib$(WREN).$(SHARED_EXT): $(VM_OBJECTS) # Test executable. $(BUILD_DIR)/test/$(WREN): $(TEST_OBJECTS) $(MODULE_OBJECTS) $(VM_OBJECTS) \ - $(BUILD_DIR)/cli/io.o $(BUILD_DIR)/cli/modules.o $(BUILD_DIR)/cli/vm.o \ - $(LIBUV) + $(BUILD_DIR)/cli/modules.o $(BUILD_DIR)/cli/vm.o $(LIBUV) @ printf "%10s %-30s %s\n" $(CC) $@ "$(C_OPTIONS)" @ mkdir -p $(BUILD_DIR)/test @ $(CC) $(CFLAGS) $^ -o $@ -lm $(LIBUV_LIBS) diff --git a/src/cli/io.c b/src/cli/io.c deleted file mode 100644 index 43326db0..00000000 --- a/src/cli/io.c +++ /dev/null @@ -1,101 +0,0 @@ -#include -#include -#include - -#include "io.h" -#include "modules.h" - -char const* rootDirectory = NULL; - -// Reads the contents of the file at [path] and returns it as a heap allocated -// string. -// -// Returns `NULL` if the path could not be found. Exits if it was found but -// could not be read. -char* readFile(const char* path) -{ - FILE* file = fopen(path, "rb"); - if (file == NULL) return NULL; - - // Find out how big the file is. - fseek(file, 0L, SEEK_END); - size_t fileSize = ftell(file); - rewind(file); - - // Allocate a buffer for it. - char* buffer = (char*)malloc(fileSize + 1); - if (buffer == NULL) - { - fprintf(stderr, "Could not read file \"%s\".\n", path); - exit(74); - } - - // Read the entire file. - size_t bytesRead = fread(buffer, sizeof(char), fileSize, file); - if (bytesRead < fileSize) - { - fprintf(stderr, "Could not read file \"%s\".\n", path); - exit(74); - } - - // Terminate the string. - buffer[bytesRead] = '\0'; - - fclose(file); - return buffer; -} - -void setRootDirectory(const char* path) -{ - rootDirectory = path; -} - -char* wrenFilePath(const char* name) -{ - // The module path is relative to the root directory and with ".wren". - size_t rootLength = rootDirectory == NULL ? 0 : strlen(rootDirectory); - size_t nameLength = strlen(name); - size_t pathLength = rootLength + nameLength + 5; - char* path = (char*)malloc(pathLength + 1); - - if (rootDirectory != NULL) - { - memcpy(path, rootDirectory, rootLength); - } - - memcpy(path + rootLength, name, nameLength); - memcpy(path + rootLength + nameLength, ".wren", 5); - path[pathLength] = '\0'; - - return path; -} - -char* readModule(WrenVM* vm, const char* module) -{ - char* source = readBuiltInModule(module); - if (source != NULL) return source; - - // First try to load the module with a ".wren" extension. - char* modulePath = wrenFilePath(module); - char* moduleContents = readFile(modulePath); - free(modulePath); - - if (moduleContents != NULL) return moduleContents; - - // If no contents could be loaded treat the module name as specifying a - // directory and try to load the "module.wren" file in the directory. - size_t moduleLength = strlen(module); - size_t moduleDirLength = moduleLength + 7; - char* moduleDir = (char*)malloc(moduleDirLength + 1); - memcpy(moduleDir, module, moduleLength); - memcpy(moduleDir + moduleLength, "/module", 7); - moduleDir[moduleDirLength] = '\0'; - - char* moduleDirPath = wrenFilePath(moduleDir); - free(moduleDir); - - moduleContents = readFile(moduleDirPath); - free(moduleDirPath); - - return moduleContents; -} diff --git a/src/cli/io.h b/src/cli/io.h deleted file mode 100644 index d60d6021..00000000 --- a/src/cli/io.h +++ /dev/null @@ -1,25 +0,0 @@ -#ifndef io_h -#define io_h - -#include "wren.h" - -// Simple IO functions. - -// Reads the contents of the file at [path] and returns it as a heap allocated -// string. -// -// Returns `NULL` if the path could not be found. Exits if it was found but -// could not be read. -char* readFile(const char* path); - -// Sets the root directory that modules are resolved relative to. -void setRootDirectory(const char* path); - -// Attempts to read the source for [module] relative to the current root -// directory. -// -// Returns it if found, or NULL if the module could not be found. Exits if the -// module was found but could not be read. -char* readModule(WrenVM* vm, const char* module); - -#endif diff --git a/src/cli/main.c b/src/cli/main.c index cb2a7b99..beb2f2f5 100644 --- a/src/cli/main.c +++ b/src/cli/main.c @@ -1,7 +1,6 @@ #include #include -#include "io.h" #include "vm.h" #include "wren.h" diff --git a/src/cli/vm.c b/src/cli/vm.c index 90073411..52f9d0e2 100644 --- a/src/cli/vm.c +++ b/src/cli/vm.c @@ -1,7 +1,6 @@ #include #include -#include "io.h" #include "modules.h" #include "vm.h" #include "scheduler.h" @@ -9,12 +8,108 @@ #define MAX_LINE_LENGTH 1024 // TODO: Something less arbitrary. // The single VM instance that the CLI uses. -WrenVM* vm; +static WrenVM* vm; static WrenBindForeignMethodFn bindMethodFn = NULL; static WrenBindForeignClassFn bindClassFn = NULL; -uv_loop_t* loop; +static uv_loop_t* loop; + +static char const* rootDirectory = NULL; + +// Reads the contents of the file at [path] and returns it as a heap allocated +// string. +// +// Returns `NULL` if the path could not be found. Exits if it was found but +// could not be read. +static char* readFile(const char* path) +{ + FILE* file = fopen(path, "rb"); + if (file == NULL) return NULL; + + // Find out how big the file is. + fseek(file, 0L, SEEK_END); + size_t fileSize = ftell(file); + rewind(file); + + // Allocate a buffer for it. + char* buffer = (char*)malloc(fileSize + 1); + if (buffer == NULL) + { + fprintf(stderr, "Could not read file \"%s\".\n", path); + exit(74); + } + + // Read the entire file. + size_t bytesRead = fread(buffer, sizeof(char), fileSize, file); + if (bytesRead < fileSize) + { + fprintf(stderr, "Could not read file \"%s\".\n", path); + exit(74); + } + + // Terminate the string. + buffer[bytesRead] = '\0'; + + fclose(file); + return buffer; +} + +// Converts the module [name] to a file path. +static char* wrenFilePath(const char* name) +{ + // The module path is relative to the root directory and with ".wren". + size_t rootLength = rootDirectory == NULL ? 0 : strlen(rootDirectory); + size_t nameLength = strlen(name); + size_t pathLength = rootLength + nameLength + 5; + char* path = (char*)malloc(pathLength + 1); + + if (rootDirectory != NULL) + { + memcpy(path, rootDirectory, rootLength); + } + + memcpy(path + rootLength, name, nameLength); + memcpy(path + rootLength + nameLength, ".wren", 5); + path[pathLength] = '\0'; + + return path; +} + +// Attempts to read the source for [module] relative to the current root +// directory. +// +// Returns it if found, or NULL if the module could not be found. Exits if the +// module was found but could not be read. +static char* readModule(WrenVM* vm, const char* module) +{ + char* source = readBuiltInModule(module); + if (source != NULL) return source; + + // First try to load the module with a ".wren" extension. + char* modulePath = wrenFilePath(module); + char* moduleContents = readFile(modulePath); + free(modulePath); + + if (moduleContents != NULL) return moduleContents; + + // If no contents could be loaded treat the module name as specifying a + // directory and try to load the "module.wren" file in the directory. + size_t moduleLength = strlen(module); + size_t moduleDirLength = moduleLength + 7; + char* moduleDir = (char*)malloc(moduleDirLength + 1); + memcpy(moduleDir, module, moduleLength); + memcpy(moduleDir + moduleLength, "/module", 7); + moduleDir[moduleDirLength] = '\0'; + + char* moduleDirPath = wrenFilePath(moduleDir); + free(moduleDir); + + moduleContents = readFile(moduleDirPath); + free(moduleDirPath); + + return moduleContents; +} // Binds foreign methods declared in either built in modules, or the injected // API test modules. @@ -89,7 +184,7 @@ void runFile(const char* path) root = (char*)malloc(lastSlash - path + 2); memcpy(root, path, lastSlash - path + 1); root[lastSlash - path + 1] = '\0'; - setRootDirectory(root); + rootDirectory = root; } char* source = readFile(path); diff --git a/test/api/main.c b/test/api/main.c index 117b53c7..a7b7f4c7 100644 --- a/test/api/main.c +++ b/test/api/main.c @@ -1,7 +1,6 @@ #include #include -#include "io.h" #include "vm.h" #include "wren.h"