Imported Upstream version 1.46.0
[platform/upstream/nghttp2.git] / third-party / mruby / src / print.c
1 /*
2 ** print.c - Kernel.#p
3 **
4 ** See Copyright Notice in mruby.h
5 */
6
7 #include <mruby.h>
8 #include <mruby/string.h>
9 #include <mruby/variable.h>
10 #include <mruby/error.h>
11 #include <string.h>
12
13 #ifndef MRB_DISABLE_STDIO
14 static void
15 printcstr(const char *str, size_t len, FILE *stream)
16 {
17   if (str) {
18     fwrite(str, len, 1, stream);
19     putc('\n', stream);
20   }
21 }
22
23 static void
24 printstr(mrb_value obj, FILE *stream)
25 {
26   if (mrb_string_p(obj)) {
27     printcstr(RSTRING_PTR(obj), RSTRING_LEN(obj), stream);
28   }
29 }
30 #else
31 # define printcstr(str, len, stream) (void)0
32 # define printstr(obj, stream) (void)0
33 #endif
34
35 void
36 mrb_core_init_printabort(void)
37 {
38   static const char *str = "Failed mruby core initialization";
39   printcstr(str, strlen(str), stdout);
40 }
41
42 MRB_API void
43 mrb_p(mrb_state *mrb, mrb_value obj)
44 {
45   if (mrb_type(obj) == MRB_TT_EXCEPTION && mrb_obj_ptr(obj) == mrb->nomem_err) {
46     static const char *str = "Out of memory";
47     printcstr(str, strlen(str), stdout);
48   }
49   else {
50     printstr(mrb_inspect(mrb, obj), stdout);
51   }
52 }
53
54 MRB_API void
55 mrb_print_error(mrb_state *mrb)
56 {
57   mrb_print_backtrace(mrb);
58 }
59
60 MRB_API void
61 mrb_show_version(mrb_state *mrb)
62 {
63   printstr(mrb_const_get(mrb, mrb_obj_value(mrb->object_class), mrb_intern_lit(mrb, "MRUBY_DESCRIPTION")), stdout);
64 }
65
66 MRB_API void
67 mrb_show_copyright(mrb_state *mrb)
68 {
69   printstr(mrb_const_get(mrb, mrb_obj_value(mrb->object_class), mrb_intern_lit(mrb, "MRUBY_COPYRIGHT")), stdout);
70 }