Debian Codegraph @

vendor/mruby-source/src/print.c

raw
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 <mruby/presym.h>
12 #include <string.h>
13
14 #ifndef MRB_NO_STDIO
15 static void
16 printcstr(const char *str, size_t len, FILE *stream)
17 {
18 if (str) {
19 fwrite(str, len, 1, stream);
20 putc('\n', stream);
21 }
22 }
23
24 static void
25 printstr(mrb_value obj, FILE *stream)
26 {
27 if (mrb_string_p(obj)) {
28 printcstr(RSTRING_PTR(obj), RSTRING_LEN(obj), stream);
29 }
30 }
31
32 void
33 mrb_core_init_printabort(void)
34 {
35 static const char *str = "Failed mruby core initialization";
36 printcstr(str, strlen(str), stdout);
37 }
38
39 MRB_API void
40 mrb_p(mrb_state *mrb, mrb_value obj)
41 {
42 if (mrb_type(obj) == MRB_TT_EXCEPTION && mrb_obj_ptr(obj) == mrb->nomem_err) {
43 static const char *str = "Out of memory";
44 printcstr(str, strlen(str), stdout);
45 }
46 else {
47 printstr(mrb_inspect(mrb, obj), stdout);
48 }
49 }
50
51
52 MRB_API void
53 mrb_show_version(mrb_state *mrb)
54 {
55 printstr(mrb_const_get(mrb, mrb_obj_value(mrb->object_class), MRB_SYM(MRUBY_DESCRIPTION)), stdout);
56 }
57
58 MRB_API void
59 mrb_show_copyright(mrb_state *mrb)
60 {
61 printstr(mrb_const_get(mrb, mrb_obj_value(mrb->object_class), MRB_SYM(MRUBY_COPYRIGHT)), stdout);
62 }
63
64 #else
65 void
66 mrb_core_init_printabort(void)
67 {
68 }
69
70 MRB_API void
71 mrb_p(mrb_state *mrb, mrb_value obj)
72 {
73 }
74
75 MRB_API void
76 mrb_show_version(mrb_state *mrb)
77 {
78 }
79
80 MRB_API void
81 mrb_show_copyright(mrb_state *mrb)
82 {
83 }
84 #endif