1
0
mirror of https://github.com/janet-lang/janet synced 2024-11-25 09:47:17 +00:00

Merge branch 'master' into net

This commit is contained in:
Calvin Rose 2020-04-19 13:38:51 -05:00
commit 42c257d0fc
6 changed files with 73 additions and 10 deletions

View File

@ -2,6 +2,7 @@
All notable changes to this project will be documented in this file. All notable changes to this project will be documented in this file.
## Unreleased - ??? ## Unreleased - ???
- Allow sending pointers and C functions across threads via `thread/send`.
- Fix bug in `getline`. - Fix bug in `getline`.
- Add `sh-rule` and `sh-phony` to jpm's dialect of Janet. - Add `sh-rule` and `sh-phony` to jpm's dialect of Janet.
- Change C api's `janet_formatb` -> `janet_formatbv`. - Change C api's `janet_formatb` -> `janet_formatbv`.
@ -22,6 +23,7 @@ All notable changes to this project will be documented in this file.
- Add os/perm-string - Add os/perm-string
- Add :octal-permissions option for os/stat. - Add :octal-permissions option for os/stat.
- Add `jpm repl` subcommand, as well as `post-deps` macro in project.janet files. - Add `jpm repl` subcommand, as well as `post-deps` macro in project.janet files.
- Various bug fixes.
## 1.8.1 - 2020-03-31 ## 1.8.1 - 2020-03-31
- Fix bugs for big endian systems - Fix bugs for big endian systems

View File

@ -2236,14 +2236,14 @@
(defn require (defn require
"Require a module with the given name. Will search all of the paths in "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 module/paths. Returns the new environment
returned from compiling and running the file." returned from compiling and running the file."
[path & args] [path & args]
(def [fullpath mod-kind] (module/find path)) (def [fullpath mod-kind] (module/find path))
(unless fullpath (error mod-kind)) (unless fullpath (error mod-kind))
(if-let [check (in module/cache fullpath)] (if-let [check (in module/cache fullpath)]
check check
(if-let [check2 (module/loading fullpath)] (if (module/loading fullpath)
(error (string "circular dependency " fullpath " detected")) (error (string "circular dependency " fullpath " detected"))
(do (do
(def loader (if (keyword? mod-kind) (module/loaders mod-kind) mod-kind)) (def loader (if (keyword? mod-kind) (module/loaders mod-kind) mod-kind))

View File

@ -500,10 +500,15 @@ static Janet janet_core_signal(int32_t argc, Janet *argv) {
sig = JANET_SIGNAL_USER0 + s; sig = JANET_SIGNAL_USER0 + s;
} else { } else {
JanetKeyword kw = janet_getkeyword(argv, 0); JanetKeyword kw = janet_getkeyword(argv, 0);
if (!janet_cstrcmp(kw, "yield")) sig = JANET_SIGNAL_YIELD; if (!janet_cstrcmp(kw, "yield")) {
if (!janet_cstrcmp(kw, "error")) sig = JANET_SIGNAL_ERROR; sig = JANET_SIGNAL_YIELD;
if (!janet_cstrcmp(kw, "debug")) sig = JANET_SIGNAL_DEBUG; } else if (!janet_cstrcmp(kw, "error")) {
janet_panicf("unknown signal, expected :yield, :error, or :debug, got %v", argv[0]); sig = JANET_SIGNAL_ERROR;
} else if (!janet_cstrcmp(kw, "debug")) {
sig = JANET_SIGNAL_DEBUG;
} else {
janet_panicf("unknown signal, expected :yield, :error, or :debug, got %v", argv[0]);
}
} }
Janet payload = argc == 2 ? argv[1] : janet_wrap_nil(); Janet payload = argc == 2 ? argv[1] : janet_wrap_nil();
janet_signalv(sig, payload); janet_signalv(sig, payload);

View File

@ -61,7 +61,9 @@ enum {
LB_ABSTRACT, /* 217 */ LB_ABSTRACT, /* 217 */
LB_REFERENCE, /* 218 */ LB_REFERENCE, /* 218 */
LB_FUNCENV_REF, /* 219 */ LB_FUNCENV_REF, /* 219 */
LB_FUNCDEF_REF /* 220 */ LB_FUNCDEF_REF, /* 220 */
LB_UNSAFE_CFUNCTION, /* 221 */
LB_UNSAFE_POINTER /* 222 */
} LeadBytes; } LeadBytes;
/* Helper to look inside an entry in an environment */ /* Helper to look inside an entry in an environment */
@ -563,9 +565,25 @@ static void marshal_one(MarshalState *st, Janet x, int flags) {
marshal_one_fiber(st, janet_unwrap_fiber(x), flags + 1); marshal_one_fiber(st, janet_unwrap_fiber(x), flags + 1);
return; return;
} }
case JANET_CFUNCTION: {
if (!(flags & JANET_MARSHAL_UNSAFE)) goto no_registry;
MARK_SEEN();
pushbyte(st, LB_UNSAFE_CFUNCTION);
JanetCFunction cfn = janet_unwrap_cfunction(x);
pushbytes(st, (uint8_t *) &cfn, sizeof(JanetCFunction));
return;
}
case JANET_POINTER: {
if (!(flags & JANET_MARSHAL_UNSAFE)) goto no_registry;
MARK_SEEN();
pushbyte(st, LB_UNSAFE_POINTER);
void *ptr = janet_unwrap_pointer(x);
pushbytes(st, (uint8_t *) &ptr, sizeof(void *));
return;
}
no_registry:
default: { default: {
janet_panicf("no registry value and cannot marshal %p", x); janet_panicf("no registry value and cannot marshal %p", x);
return;
} }
} }
#undef MARK_SEEN #undef MARK_SEEN
@ -1293,6 +1311,42 @@ static const uint8_t *unmarshal_one(
} }
return data; return data;
} }
case LB_UNSAFE_POINTER: {
MARSH_EOS(st, data + sizeof(void *));
data++;
if (!(flags & JANET_MARSHAL_UNSAFE)) {
janet_panicf("unsafe flag not given, "
"will not unmarshal raw pointer at index %d",
(int)(data - st->start));
}
union {
void *ptr;
uint8_t bytes[sizeof(void *)];
} u;
memcpy(u.bytes, data, sizeof(void *));
data += sizeof(void *);
*out = janet_wrap_pointer(u.ptr);
janet_v_push(st->lookup, *out);
return data;
}
case LB_UNSAFE_CFUNCTION: {
MARSH_EOS(st, data + sizeof(JanetCFunction));
data++;
if (!(flags & JANET_MARSHAL_UNSAFE)) {
janet_panicf("unsafe flag not given, "
"will not unmarshal function pointer at index %d",
(int)(data - st->start));
}
union {
JanetCFunction ptr;
uint8_t bytes[sizeof(JanetCFunction)];
} u;
memcpy(u.bytes, data, sizeof(JanetCFunction));
data += sizeof(JanetCFunction);
*out = janet_wrap_cfunction(u.ptr);
janet_v_push(st->lookup, *out);
return data;
}
default: { default: {
janet_panicf("unknown byte %x at index %d", janet_panicf("unknown byte %x at index %d",
*data, *data,

View File

@ -329,7 +329,7 @@ int janet_thread_send(JanetThread *thread, Janet msg, double timeout) {
msgbuf->count = 0; msgbuf->count = 0;
/* Start panic zone */ /* Start panic zone */
janet_marshal(msgbuf, msg, thread->encode, 0); janet_marshal(msgbuf, msg, thread->encode, JANET_MARSHAL_UNSAFE);
/* End panic zone */ /* End panic zone */
mailbox->messageNext = (mailbox->messageNext + 1) % mailbox->messageCapacity; mailbox->messageNext = (mailbox->messageNext + 1) % mailbox->messageCapacity;
@ -379,7 +379,7 @@ int janet_thread_receive(Janet *msg_out, double timeout) {
const uint8_t *nextItem = NULL; const uint8_t *nextItem = NULL;
Janet item = janet_unmarshal( Janet item = janet_unmarshal(
msgbuf->data, msgbuf->count, msgbuf->data, msgbuf->count,
0, janet_thread_get_decode(), &nextItem); JANET_MARSHAL_UNSAFE, janet_thread_get_decode(), &nextItem);
*msg_out = item; *msg_out = item;
/* Cleanup */ /* Cleanup */

View File

@ -1323,6 +1323,8 @@ typedef JanetBuildConfig(*JanetModconf)(void);
JANET_API JanetModule janet_native(const char *name, JanetString *error); JANET_API JanetModule janet_native(const char *name, JanetString *error);
/* Marshaling */ /* Marshaling */
#define JANET_MARSHAL_UNSAFE 0x20000
JANET_API void janet_marshal( JANET_API void janet_marshal(
JanetBuffer *buf, JanetBuffer *buf,
Janet x, Janet x,