mirror of
https://github.com/janet-lang/janet
synced 2024-11-24 17:27:18 +00:00
Add images.
Images are precompiled libraries. They can be created programmatically via the `write-image` function and then loaded with `require` or `import`. They can also be run by the command line tool - you must specify the path to the image without the .jimage extension.
This commit is contained in:
parent
bdf03b4706
commit
9e6b1d1b16
@ -2,6 +2,9 @@
|
||||
All notable changes to this project will be documented in this file.
|
||||
|
||||
## 0.4.0 - ??
|
||||
- `write-image` function creates pre compiled images for janet. These images
|
||||
link to the core library. They can be loaded via require.
|
||||
- Add bracketed tuples as tuple constructor.
|
||||
- Add partition function to core library.
|
||||
- Pre-compile core library into an image for faster startup.
|
||||
- Add methods to parser values that mirror the api.
|
||||
|
@ -1081,6 +1081,33 @@ value, one key will be ignored."
|
||||
(if (not= i len) (array/push ret (tuple/slice ind i)))
|
||||
ret)
|
||||
|
||||
###
|
||||
###
|
||||
### IO Helpers
|
||||
###
|
||||
###
|
||||
|
||||
(defn slurp
|
||||
"Read all data from a file with name path
|
||||
and then close the file."
|
||||
[path]
|
||||
(def f (file/open path :r))
|
||||
(if-not f (error (string "could not open file " path)))
|
||||
(def contents (file/read f :all))
|
||||
(file/close f)
|
||||
contents)
|
||||
|
||||
(defn spit
|
||||
"Write contents to a file at path.
|
||||
Can optionally append to the file."
|
||||
[path contents mode &]
|
||||
(default mode :w)
|
||||
(def f (file/open path mode))
|
||||
(if-not f (error (string "could not open file " path " with mode " mode)))
|
||||
(file/write f contents)
|
||||
(file/close f)
|
||||
nil)
|
||||
|
||||
###
|
||||
###
|
||||
### Pattern Matching
|
||||
@ -1556,8 +1583,14 @@ value, one key will be ignored."
|
||||
@["./:all:.:native:"
|
||||
"./:all:/:name:.:native:"
|
||||
":sys:/:all:.:native:"
|
||||
":sys:/:all:/:name:.:native:"
|
||||
":all:"])
|
||||
":sys:/:all:/:name:.:native:"])
|
||||
|
||||
(def module/image-paths
|
||||
"See doc for module/paths"
|
||||
@["./:all:.jimage"
|
||||
"./:all:.:name:.jimage"
|
||||
":sys:/:all:.jimage"
|
||||
":sys:/:all:/:name:.jimage"])
|
||||
|
||||
(var module/*syspath*
|
||||
"The path where globally installed libraries are located.
|
||||
@ -1621,23 +1654,39 @@ value, one key will be ignored."
|
||||
(if exit-on-error (os/exit 1))))
|
||||
:source modpath})
|
||||
(file/close f)
|
||||
(table/setproto newenv nil)
|
||||
(put module/loading modpath false)
|
||||
(put module/cache modpath newenv)
|
||||
(put module/cache path newenv)
|
||||
newenv)
|
||||
(do
|
||||
# Try native module
|
||||
(def n (find fexists (module/find path module/native-paths)))
|
||||
(if (not n)
|
||||
(error (string "could not open file for module " path)))
|
||||
(def e (make-env))
|
||||
(native n e)
|
||||
(put module/cache n e)
|
||||
(put module/cache path e)
|
||||
e))))
|
||||
(if-let [imgpath (find fexists (module/find path module/image-paths))]
|
||||
(do
|
||||
# Try image
|
||||
(def imgsource (slurp imgpath))
|
||||
(def img (unmarshal imgsource (env-lookup *env*)))
|
||||
img)
|
||||
(do
|
||||
# Try native module
|
||||
(def n (find fexists (module/find path module/native-paths)))
|
||||
(if (not n)
|
||||
(error (string "could not open file for module " path)))
|
||||
(def e (make-env))
|
||||
(native n e)
|
||||
(put module/cache n e)
|
||||
(put module/cache path e)
|
||||
e)))))
|
||||
|
||||
(put _env 'fexists nil)
|
||||
|
||||
(defn write-image
|
||||
"Create an image from the file at path. Writes the output
|
||||
image to a file at out."
|
||||
[path out]
|
||||
(def env (require path))
|
||||
(def img (marshal env (invert (env-lookup _env))))
|
||||
(spit out img)
|
||||
img)
|
||||
|
||||
(defn import*
|
||||
"Import a module into a given environment table. This is the
|
||||
functional form of (import ...) that expects and explicit environment
|
||||
@ -1702,26 +1751,5 @@ value, one key will be ignored."
|
||||
(put symbol-set k true))
|
||||
(sort (keys symbol-set)))
|
||||
|
||||
(defn slurp
|
||||
"Read all data from a file with name path
|
||||
and then close the file."
|
||||
[path]
|
||||
(def f (file/open path :r))
|
||||
(if-not f (error (string "could not open file " path)))
|
||||
(def contents (file/read f :all))
|
||||
(file/close f)
|
||||
contents)
|
||||
|
||||
(defn spit
|
||||
"Write contents to a file at path.
|
||||
Can optionally append to the file."
|
||||
[path contents mode &]
|
||||
(default mode :w)
|
||||
(def f (file/open path mode))
|
||||
(if-not f (error (string "could not open file " path " with mode " mode)))
|
||||
(file/write f contents)
|
||||
(file/close f)
|
||||
nil)
|
||||
|
||||
# Use dynamic *env* from now on
|
||||
(put _env '_env nil)
|
||||
|
Loading…
Reference in New Issue
Block a user