From de59c57e48e533a2d4657cb5dc57db319012059e Mon Sep 17 00:00:00 2001 From: Calvin Rose Date: Sat, 19 May 2018 21:29:22 -0400 Subject: [PATCH] Make native modules easier to import. --- natives/sqlite3/Makefile | 2 +- src/compiler/boot.dst | 26 +++++++++++++++++--------- 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/natives/sqlite3/Makefile b/natives/sqlite3/Makefile index 6674a5b3..6a491a81 100644 --- a/natives/sqlite3/Makefile +++ b/natives/sqlite3/Makefile @@ -19,7 +19,7 @@ # IN THE SOFTWARE. CFLAGS=-std=c99 -Wall -Wextra -I../../src/include -O2 -DSQLITE_THREADSAFE=0 -DSQLITE_OMIT_LOAD_EXTENSION -shared -fpic -TARGET=libdstsqlite3.so +TARGET=sqlite3.so sqlite-autoconf-3230100/sqlite3.%: curl https://www.sqlite.org/2018/sqlite-autoconf-3230100.tar.gz | tar -xvz diff --git a/src/compiler/boot.dst b/src/compiler/boot.dst index 4df5d219..e97bfabc 100644 --- a/src/compiler/boot.dst +++ b/src/compiler/boot.dst @@ -984,29 +984,37 @@ environment is needed, use run-context." (def module.native-paths @[ "./?.so" + "./?/??.so" "./dst_modules/?.so" + "./dst_modules/?/??.so" "/usr/local/dst/0.0.0/?.so" + "/usr/local/dst/0.0.0/?/??.so" ]) +(defn module.find + [path paths] + (def parts (string.split "." path)) + (def last (get parts (- (length parts) 1))) + (def normname (string.replace-all "." "/" path)) + (array.push + (map (fn [x] + (def y (string.replace "??" last x)) + (string.replace "?" normname y)) + paths) + path)) + (def require "Require a module with the given name. Will search all of the paths in module.paths, then the path as a raw file path. Returns the new environment returned from compiling and running the file." (do - (defn transform - [path paths] - (def normname (string.replace-all "." "/" path)) - (array.push - (map (fn [x] (string.replace "?" normname x)) paths) - path)) - (defn check-mod [f testpath] (if f f (file.open testpath))) (defn find-mod [path] - (def paths (transform path module.paths)) + (def paths (module.find path module.paths)) (reduce check-mod nil paths)) (defn check-native @@ -1016,7 +1024,7 @@ returned from compiling and running the file." (if f (do (file.close f) testpath))))) (defn find-native [path] - (def paths (transform path module.native-paths)) + (def paths (module.find path module.native-paths)) (reduce check-native nil paths)) (def cache @{})