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

Add extra argument to (native) to allow for passing

in custom environment to add stuff to.
This commit is contained in:
Calvin Rose 2019-01-05 23:37:10 -05:00
parent b535c91ee1
commit b626e73d19
2 changed files with 9 additions and 4 deletions

View File

@ -1623,7 +1623,7 @@ value, one key will be ignored."
(def n (find-native path)) (def n (find-native path))
(if (not n) (if (not n)
(error (string "could not open file for module " path))) (error (string "could not open file for module " path)))
(native n))))))) (native n (make-env))))))))
(defn import* (defn import*
"Import a module into a given environment table. This is the "Import a module into a given environment table. This is the

View File

@ -67,10 +67,15 @@ JanetModule janet_native(const char *name, const uint8_t **error) {
static Janet janet_core_native(int32_t argc, Janet *argv) { static Janet janet_core_native(int32_t argc, Janet *argv) {
JanetModule init; JanetModule init;
JanetTable *env = janet_table(0); janet_arity(argc, 1, 2);
janet_fixarity(argc, 1);
const uint8_t *path = janet_getstring(argv, 0); const uint8_t *path = janet_getstring(argv, 0);
const uint8_t *error = NULL; const uint8_t *error = NULL;
JanetTable *env;
if (argc == 2) {
env = janet_gettable(argv, 1);
} else {
env = janet_table(0);
}
init = janet_native((const char *)path, &error); init = janet_native((const char *)path, &error);
if (!init) { if (!init) {
janet_panicf("could not load native %S: %S", path, error); janet_panicf("could not load native %S: %S", path, error);
@ -245,7 +250,7 @@ static Janet janet_core_hash(int32_t argc, Janet *argv) {
static const JanetReg cfuns[] = { static const JanetReg cfuns[] = {
{"native", janet_core_native, {"native", janet_core_native,
"(native path)\n\n" "(native path [,env])\n\n"
"Load a native module from the given path. The path " "Load a native module from the given path. The path "
"must be an absolute or relative path on the file system, and is " "must be an absolute or relative path on the file system, and is "
"usually a .so file on Unix systems, and a .dll file on Windows. " "usually a .so file on Unix systems, and a .dll file on Windows. "