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

Merge branch 'master' of github.com:janet-lang/janet

This commit is contained in:
Calvin Rose 2021-12-04 13:03:33 -06:00
commit d4c6643311
2 changed files with 23 additions and 0 deletions

View File

@ -483,6 +483,19 @@ static Janet cfun_io_print_impl_x(int32_t argc, Janet *argv, int newline,
janet_buffer_push_u8(buf, '\n'); janet_buffer_push_u8(buf, '\n');
return janet_wrap_nil(); return janet_wrap_nil();
} }
case JANET_FUNCTION: {
/* Special case function */
JanetFunction *fun = janet_unwrap_function(x);
JanetBuffer *buf = janet_buffer(0);
for (int32_t i = offset; i < argc; ++i) {
janet_to_string_b(buf, argv[i]);
}
if (newline)
janet_buffer_push_u8(buf, '\n');
Janet args[1] = { janet_wrap_buffer(buf) };
janet_call(fun, 1, args);
return janet_wrap_nil();
}
case JANET_NIL: case JANET_NIL:
f = dflt_file; f = dflt_file;
if (f == NULL) janet_panic("cannot print to nil"); if (f == NULL) janet_panic("cannot print to nil");

View File

@ -168,6 +168,16 @@
(assert (= (string out-buf) "Hello\nhi") "print and prin to buffer 1") (assert (= (string out-buf) "Hello\nhi") "print and prin to buffer 1")
(assert (= (string err-buf) "Sup\nnot much.") "eprint and eprin to buffer 1") (assert (= (string err-buf) "Sup\nnot much.") "eprint and eprin to buffer 1")
# Printing to functions
(def out-buf @"")
(defn prepend [x]
(with-dyns [:out out-buf]
(prin "> " x)))
(with-dyns [:out prepend]
(print "Hello world"))
(assert (= (string out-buf) "> Hello world\n") "print to buffer via function")
(assert (= (string '()) (string [])) "empty bracket tuple literal") (assert (= (string '()) (string [])) "empty bracket tuple literal")
# with-vars # with-vars