From 2cb627f3ba79bfce98e4cf6ab4b2e8029e8cb09e Mon Sep 17 00:00:00 2001 From: Jo-Philipp Wich Date: Fri, 28 Jan 2022 20:15:13 +0100 Subject: program: rename bytecode load/write functions, track path of executed file Extend source objects with a `runpath` field which contains the original path of the source being executed by the VM. When instantiating source objects from file paths, the `runpath` will be set to the `filename`. When instantiating source buffers using `uc_source_new_buffer()`, the runpath is initially unset. A new function `uc_source_runpath_set()` can be used to adjust the runtime path being associated with a source object. Extend bytecode loading logic to set the source buffer runtime path to the precompiled bytecode file path being loaded and executed. This is required for `sourcepath()` and relative paths in `include()` to function correctly when executing precompiled programs. Finally rename `uc_program_from_file()` and `uc_program_to_file()` to `uc_program_load()` and `uc_program_write()` respectively since the load part now operates on an `uc_source_t` input buffer instead of a plain `FILE *` handle. Adjust users of these API functions accordingly. Signed-off-by: Jo-Philipp Wich --- source.c | 13 +++++++++++++ 1 file changed, 13 insertions(+) (limited to 'source.c') diff --git a/source.c b/source.c index 3b35b70..f2bd8bf 100644 --- a/source.c +++ b/source.c @@ -34,6 +34,7 @@ uc_source_new_file(const char *path) src->fp = fp; src->buffer = NULL; src->filename = strcpy((char *)src + ALIGN(sizeof(*src)), path); + src->runpath = src->filename; src->usecount = 1; @@ -113,6 +114,9 @@ uc_source_put(uc_source_t *source) return; } + if (source->runpath != source->filename) + free(source->runpath); + uc_vector_clear(&source->lineinfo); fclose(source->fp); free(source->buffer); @@ -211,3 +215,12 @@ uc_source_line_update(uc_source_t *source, size_t off) } } } + +void +uc_source_runpath_set(uc_source_t *source, const char *runpath) +{ + if (source->runpath != source->filename) + free(source->runpath); + + source->runpath = xstrdup(runpath); +} -- cgit v1.2.3