1
0
mirror of https://github.com/janet-lang/janet synced 2024-12-25 16:00:27 +00:00

Add :export option to import

Also allow NULL ptr to janet_getfile for flags.
This commit is contained in:
Calvin Rose 2019-03-02 11:46:31 -05:00
parent efbb704247
commit 8cc2c964c1
3 changed files with 6 additions and 5 deletions

View File

@ -1687,18 +1687,20 @@ value, one key will be ignored."
table." table."
[env path & args] [env path & args]
(def {:as as (def {:as as
:prefix prefix} (table ;args)) :prefix prefix
:export ep} (table ;args))
(def newenv (require path ;args)) (def newenv (require path ;args))
(def prefix (or (and as (string as "/")) prefix (string path "/"))) (def prefix (or (and as (string as "/")) prefix (string path "/")))
(loop [[k v] :pairs newenv :when (not (v :private))] (loop [[k v] :pairs newenv :when (not (v :private))]
(def newv (table/setproto @{:private true} v)) (def newv (table/setproto @{:private (not ep)} v))
(put env (symbol prefix k) newv))) (put env (symbol prefix k) newv)))
(defmacro import (defmacro import
"Import a module. First requires the module, and then merges its "Import a module. First requires the module, and then merges its
symbols into the current environment, prepending a given prefix as needed. symbols into the current environment, prepending a given prefix as needed.
(use the :as or :prefix option to set a prefix). If no prefix is provided, (use the :as or :prefix option to set a prefix). If no prefix is provided,
use the name of the module as a prefix." use the name of the module as a prefix. One can also use :export true
to re-export the imported symbols."
[path & args] [path & args]
(def argm (map (fn [x] (def argm (map (fn [x]
(if (keyword? x) (if (keyword? x)

View File

@ -404,7 +404,7 @@ static const JanetReg io_cfuns[] = {
FILE *janet_getfile(const Janet *argv, int32_t n, int *flags) { FILE *janet_getfile(const Janet *argv, int32_t n, int *flags) {
IOFile *iof = janet_getabstract(argv, n, &cfun_io_filetype); IOFile *iof = janet_getabstract(argv, n, &cfun_io_filetype);
*flags = iof->flags; if (NULL != flags) *flags = iof->flags;
return iof->file; return iof->file;
} }

View File

@ -1256,7 +1256,6 @@ JANET_API int32_t janet_getargindex(const Janet *argv, int32_t n, int32_t length
JANET_API FILE *janet_getfile(const Janet *argv, int32_t n, int *flags); JANET_API FILE *janet_getfile(const Janet *argv, int32_t n, int *flags);
/* Marshal API */ /* Marshal API */
JANET_API void janet_marshal_int(JanetMarshalContext *ctx, int32_t value); JANET_API void janet_marshal_int(JanetMarshalContext *ctx, int32_t value);
JANET_API void janet_marshal_byte(JanetMarshalContext *ctx, uint8_t value); JANET_API void janet_marshal_byte(JanetMarshalContext *ctx, uint8_t value);