1
0
mirror of https://github.com/janet-lang/janet synced 2024-11-29 03:19:54 +00:00

Add several configurable options - #379

This commit is contained in:
Calvin Rose 2020-05-09 12:00:01 -05:00
parent 6b986fecb0
commit b1f76139a7
9 changed files with 62 additions and 42 deletions

View File

@ -2575,6 +2575,9 @@
(def- importers {'import true 'import* true 'use true 'dofile true 'require true}) (def- importers {'import true 'import* true 'use true 'dofile true 'require true})
# conditional compilation for reduced os
(def- getenv-alias (if-let [entry (in _env 'os/getenv)] (entry :value) (fn [&])))
(defn cli-main (defn cli-main
"Entrance for the Janet CLI tool. Call this functions with the command line "Entrance for the Janet CLI tool. Call this functions with the command line
arguments as an array or tuple of strings to invoke the CLI interface." arguments as an array or tuple of strings to invoke the CLI interface."
@ -2592,8 +2595,8 @@
(var *debug* false) (var *debug* false)
(var *compile-only* false) (var *compile-only* false)
(if-let [jp (os/getenv "JANET_PATH")] (setdyn :syspath jp)) (if-let [jp (getenv-alias "JANET_PATH")] (setdyn :syspath jp))
(if-let [jp (os/getenv "JANET_HEADERPATH")] (setdyn :headerpath jp)) (if-let [jp (getenv-alias "JANET_HEADERPATH")] (setdyn :headerpath jp))
# Flag handlers # Flag handlers
(def handlers (def handlers
@ -2708,7 +2711,7 @@
(put _env 'is-safe-def nil) (put _env 'is-safe-def nil)
(put _env 'safe-forms nil) (put _env 'safe-forms nil)
(put _env 'importers nil) (put _env 'importers nil)
(put _env 'getenv-alias nil)
### ###
### ###

View File

@ -45,15 +45,20 @@
/* #define JANET_NO_DOCSTRINGS */ /* #define JANET_NO_DOCSTRINGS */
/* #define JANET_NO_SOURCEMAPS */ /* #define JANET_NO_SOURCEMAPS */
/* #define JANET_REDUCED_OS */ /* #define JANET_REDUCED_OS */
/* #define JANET_NO_PROCESSES */
/* Other settings */
/* #define JANET_NO_ASSEMBLER */ /* #define JANET_NO_ASSEMBLER */
/* #define JANET_NO_PEG */ /* #define JANET_NO_PEG */
/* #define JANET_NO_NET */ /* #define JANET_NO_NET */
/* #define JANET_NO_TYPED_ARRAY */ /* #define JANET_NO_TYPED_ARRAY */
/* #define JANET_NO_INT_TYPES */ /* #define JANET_NO_INT_TYPES */
/* Other settings */
/* #define JANET_NO_PRF */ /* #define JANET_NO_PRF */
/* #define JANET_NO_UTC_MKTIME */
/* #define JANET_NO_SYMLINKS */
/* #define JANET_OUT_OF_MEMORY do { printf("janet out of memory\n"); exit(1); } while (0) */ /* #define JANET_OUT_OF_MEMORY do { printf("janet out of memory\n"); exit(1); } while (0) */
/* #define JANET_EXIT(msg) do { printf("C assert failed executing janet: %s\n", msg); exit(1); } while (0) */
/* #define JANET_TOP_LEVEL_SIGNAL(msg) call_my_function((msg), stderr) */
/* #define JANET_RECURSION_GUARD 1024 */ /* #define JANET_RECURSION_GUARD 1024 */
/* #define JANET_MAX_PROTO_DEPTH 200 */ /* #define JANET_MAX_PROTO_DEPTH 200 */
/* #define JANET_MAX_MACRO_EXPAND 200 */ /* #define JANET_MAX_MACRO_EXPAND 200 */

View File

@ -27,6 +27,15 @@
#include "fiber.h" #include "fiber.h"
#endif #endif
JANET_NO_RETURN static void janet_top_level_signal(const char *msg) {
#ifdef JANET_TOP_LEVEL_SIGNAL
JANET_TOP_LEVEL_SIGNAL(msg);
#else
fputs(msg, stdout);
exit(1);
#endif
}
void janet_signalv(JanetSignal sig, Janet message) { void janet_signalv(JanetSignal sig, Janet message) {
if (janet_vm_return_reg != NULL) { if (janet_vm_return_reg != NULL) {
*janet_vm_return_reg = message; *janet_vm_return_reg = message;
@ -37,8 +46,8 @@ void janet_signalv(JanetSignal sig, Janet message) {
longjmp(*janet_vm_jmp_buf, sig); longjmp(*janet_vm_jmp_buf, sig);
#endif #endif
} else { } else {
fputs((const char *)janet_formatc("janet top level signal - %v\n", message), stdout); const char *str = (const char *)janet_formatc("janet top level signal - %v\n", message);
exit(1); janet_top_level_signal(str);
} }
} }

View File

@ -533,7 +533,7 @@ void *janet_srealloc(void *mem, size_t size) {
if (i == 0) break; if (i == 0) break;
} }
} }
janet_exit("invalid janet_srealloc"); JANET_EXIT("invalid janet_srealloc");
} }
void janet_sfinalizer(void *mem, JanetScratchFinalizer finalizer) { void janet_sfinalizer(void *mem, JanetScratchFinalizer finalizer) {
@ -554,5 +554,5 @@ void janet_sfree(void *mem) {
if (i == 0) break; if (i == 0) break;
} }
} }
janet_exit("invalid janet_sfree"); JANET_EXIT("invalid janet_sfree");
} }

View File

@ -99,14 +99,7 @@ static Janet makef(FILE *f, int flags) {
} }
/* Open a process */ /* Open a process */
#ifdef __EMSCRIPTEN__ #ifndef JANET_NO_PROCESSES
static Janet cfun_io_popen(int32_t argc, Janet *argv) {
(void) argc;
(void) argv;
janet_panic("not implemented on this platform");
return janet_wrap_nil();
}
#else
static Janet cfun_io_popen(int32_t argc, Janet *argv) { static Janet cfun_io_popen(int32_t argc, Janet *argv) {
janet_arity(argc, 1, 2); janet_arity(argc, 1, 2);
const uint8_t *fname = janet_getstring(argv, 0); const uint8_t *fname = janet_getstring(argv, 0);
@ -655,6 +648,7 @@ static const JanetReg io_cfuns[] = {
"for the relative number of bytes to seek in the file. n may be a real " "for the relative number of bytes to seek in the file. n may be a real "
"number to handle large files of more the 4GB. Returns the file handle.") "number to handle large files of more the 4GB. Returns the file handle.")
}, },
#ifndef JANET_NO_PROCESSES
{ {
"file/popen", cfun_io_popen, "file/popen", cfun_io_popen,
JDOC("(file/popen path &opt mode)\n\n" JDOC("(file/popen path &opt mode)\n\n"
@ -663,6 +657,7 @@ static const JanetReg io_cfuns[] = {
"process can be read from the file. In :w mode, the stdin of the process " "process can be read from the file. In :w mode, the stdin of the process "
"can be written to. Returns the new file.") "can be written to. Returns the new file.")
}, },
#endif
{NULL, NULL, NULL} {NULL, NULL, NULL}
}; };

View File

@ -182,18 +182,9 @@ static Janet os_exit(int32_t argc, Janet *argv) {
return janet_wrap_nil(); return janet_wrap_nil();
} }
#ifdef JANET_REDUCED_OS #ifndef JANET_REDUCED_OS
/* Provide a dud os/getenv so boot.janet and init.janet work, but nothing else */
static Janet os_getenv(int32_t argc, Janet *argv) {
(void) argv;
janet_arity(argc, 1, 2);
return janet_wrap_nil();
}
#else
/* Provide full os functionality */
#ifndef JANET_NO_PROCESSES
/* Get env for os_execute */ /* Get env for os_execute */
static char **os_execute_env(int32_t argc, const Janet *argv) { static char **os_execute_env(int32_t argc, const Janet *argv) {
char **envp = NULL; char **envp = NULL;
@ -444,6 +435,8 @@ static Janet os_shell(int32_t argc, Janet *argv) {
: janet_wrap_boolean(stat); : janet_wrap_boolean(stat);
} }
#endif /* JANET_NO_PROCESSES */
static Janet os_environ(int32_t argc, Janet *argv) { static Janet os_environ(int32_t argc, Janet *argv) {
(void) argv; (void) argv;
janet_fixarity(argc, 0); janet_fixarity(argc, 0);
@ -764,8 +757,8 @@ static Janet os_mktime(int32_t argc, Janet *argv) {
t = mktime(&t_info); t = mktime(&t_info);
} else { } else {
/* utc time */ /* utc time */
#ifdef __sun #ifdef JANET_NO_UTC_MKTIME
janet_panic("os/mktime UTC not supported on Solaris"); janet_panic("os/mktime UTC not supported on this platform");
return janet_wrap_nil(); return janet_wrap_nil();
#else #else
t = timegm(&t_info); t = timegm(&t_info);
@ -779,6 +772,12 @@ static Janet os_mktime(int32_t argc, Janet *argv) {
return janet_wrap_number((double)t); return janet_wrap_number((double)t);
} }
#ifdef JANET_NO_SYMLINKS
#define j_symlink link
#else
#define j_symlink symlink
#endif
static Janet os_link(int32_t argc, Janet *argv) { static Janet os_link(int32_t argc, Janet *argv) {
janet_arity(argc, 2, 3); janet_arity(argc, 2, 3);
#ifdef JANET_WINDOWS #ifdef JANET_WINDOWS
@ -789,7 +788,7 @@ static Janet os_link(int32_t argc, Janet *argv) {
#else #else
const char *oldpath = janet_getcstring(argv, 0); const char *oldpath = janet_getcstring(argv, 0);
const char *newpath = janet_getcstring(argv, 1); const char *newpath = janet_getcstring(argv, 1);
int res = ((argc == 3 && janet_truthy(argv[2])) ? symlink : link)(oldpath, newpath); int res = ((argc == 3 && janet_truthy(argv[2])) ? j_symlink : link)(oldpath, newpath);
if (-1 == res) janet_panicf("%s: %s -> %s", strerror(errno), oldpath, newpath); if (-1 == res) janet_panicf("%s: %s -> %s", strerror(errno), oldpath, newpath);
return janet_wrap_nil(); return janet_wrap_nil();
#endif #endif
@ -805,12 +804,14 @@ static Janet os_symlink(int32_t argc, Janet *argv) {
#else #else
const char *oldpath = janet_getcstring(argv, 0); const char *oldpath = janet_getcstring(argv, 0);
const char *newpath = janet_getcstring(argv, 1); const char *newpath = janet_getcstring(argv, 1);
int res = symlink(oldpath, newpath); int res = j_symlink(oldpath, newpath);
if (-1 == res) janet_panicf("%s: %s -> %s", strerror(errno), oldpath, newpath); if (-1 == res) janet_panicf("%s: %s -> %s", strerror(errno), oldpath, newpath);
return janet_wrap_nil(); return janet_wrap_nil();
#endif #endif
} }
#undef j_symlink
static Janet os_mkdir(int32_t argc, Janet *argv) { static Janet os_mkdir(int32_t argc, Janet *argv) {
janet_fixarity(argc, 1); janet_fixarity(argc, 1);
const char *path = janet_getcstring(argv, 0); const char *path = janet_getcstring(argv, 0);
@ -1264,11 +1265,6 @@ static const JanetReg os_cfuns[] = {
"\t:netbsd\n" "\t:netbsd\n"
"\t:posix - A POSIX compatible system (default)") "\t:posix - A POSIX compatible system (default)")
}, },
{
"os/getenv", os_getenv,
JDOC("(os/getenv variable &opt dflt)\n\n"
"Get the string value of an environment variable.")
},
{ {
"os/arch", os_arch, "os/arch", os_arch,
JDOC("(os/arch)\n\n" JDOC("(os/arch)\n\n"
@ -1287,6 +1283,11 @@ static const JanetReg os_cfuns[] = {
JDOC("(os/environ)\n\n" JDOC("(os/environ)\n\n"
"Get a copy of the os environment table.") "Get a copy of the os environment table.")
}, },
{
"os/getenv", os_getenv,
JDOC("(os/getenv variable &opt dflt)\n\n"
"Get the string value of an environment variable.")
},
{ {
"os/dir", os_dir, "os/dir", os_dir,
JDOC("(os/dir dir &opt array)\n\n" JDOC("(os/dir dir &opt array)\n\n"
@ -1377,6 +1378,7 @@ static const JanetReg os_cfuns[] = {
JDOC("(os/readlink path)\n\n" JDOC("(os/readlink path)\n\n"
"Read the contents of a symbolic link. Does not work on Windows.\n") "Read the contents of a symbolic link. Does not work on Windows.\n")
}, },
#ifndef JANET_NO_PROCESSES
{ {
"os/execute", os_execute, "os/execute", os_execute,
JDOC("(os/execute args &opts flags env)\n\n" JDOC("(os/execute args &opts flags env)\n\n"
@ -1394,6 +1396,7 @@ static const JanetReg os_cfuns[] = {
JDOC("(os/shell str)\n\n" JDOC("(os/shell str)\n\n"
"Pass a command string str directly to the system shell.") "Pass a command string str directly to the system shell.")
}, },
#endif
{ {
"os/setenv", os_setenv, "os/setenv", os_setenv,
JDOC("(os/setenv variable value)\n\n" JDOC("(os/setenv variable value)\n\n"

View File

@ -145,7 +145,7 @@ void janetc_regalloc_free(JanetcRegisterAllocator *ra, int32_t reg) {
int32_t janetc_regalloc_temp(JanetcRegisterAllocator *ra, JanetcRegisterTemp nth) { int32_t janetc_regalloc_temp(JanetcRegisterAllocator *ra, JanetcRegisterTemp nth) {
int32_t oldmax = ra->max; int32_t oldmax = ra->max;
if (ra->regtemps & (1 << nth)) { if (ra->regtemps & (1 << nth)) {
janet_exit("regtemp already allocated"); JANET_EXIT("regtemp already allocated");
} }
ra->regtemps |= 1 << nth; ra->regtemps |= 1 << nth;
int32_t reg = janetc_regalloc_1(ra); int32_t reg = janetc_regalloc_1(ra);

View File

@ -32,9 +32,9 @@
#include <errno.h> #include <errno.h>
/* Handle runtime errors */ /* Handle runtime errors */
#ifndef janet_exit #ifndef JANET_EXIT
#include <stdio.h> #include <stdio.h>
#define janet_exit(m) do { \ #define JANET_EXIT(m) do { \
fprintf(stderr, "C runtime error at line %d in file %s: %s\n",\ fprintf(stderr, "C runtime error at line %d in file %s: %s\n",\
__LINE__,\ __LINE__,\
__FILE__,\ __FILE__,\
@ -44,7 +44,7 @@
#endif #endif
#define janet_assert(c, m) do { \ #define janet_assert(c, m) do { \
if (!(c)) janet_exit((m)); \ if (!(c)) JANET_EXIT((m)); \
} while (0) } while (0)
/* What to do when out of memory */ /* What to do when out of memory */

View File

@ -132,6 +132,11 @@ extern "C" {
#define JANET_NO_DYNAMIC_MODULES #define JANET_NO_DYNAMIC_MODULES
#endif #endif
/* Check sun */
#ifdef __sun
#define JANET_NO_UTC_MKTIME
#endif
/* Define how global janet state is declared */ /* Define how global janet state is declared */
#ifdef JANET_SINGLE_THREADED #ifdef JANET_SINGLE_THREADED
#define JANET_THREAD_LOCAL #define JANET_THREAD_LOCAL