aboutsummaryrefslogtreecommitdiff
path: root/src/zjs_console.c
blob: 339b9e5611ac8925d581bcbe1fb71da7630a356b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
// Copyright (c) 2016-2017, Intel Corporation.

#ifdef BUILD_MODULE_CONSOLE

#include "zjs_common.h"
#include "zjs_error.h"
#include "zjs_util.h"
#ifdef ZJS_LINUX_BUILD
#include "zjs_linux_port.h"
#else
#include "zjs_zephyr_port.h"
#endif

#define MAX_STR_LENGTH   256

#ifdef ZJS_LINUX_BUILD
#define STDERR_PRINT(s, ...) fprintf(stderr, s, __VA_ARGS__)
#define STDOUT_PRINT(s, ...) fprintf(stdout, s, __VA_ARGS__)
#else
#define STDERR_PRINT(s, ...) ZJS_PRINT(s, __VA_ARGS__)
#define STDOUT_PRINT(s, ...) ZJS_PRINT(s, __VA_ARGS__)
#endif

#define IS_NUMBER 0
#define IS_INT    1
#define IS_UINT   2

static jerry_value_t gbl_time_obj;

static int is_int(jerry_value_t val) {
    int ret = 0;
    double n = jerry_get_number_value(val);
    ret = (n - (int) n == 0);
    if (ret) {
        // Integer type
        if (n < 0) {
            return IS_INT;
        } else {
            return IS_UINT;
        }
    } else {
        return IS_NUMBER;
    }
}

static bool value2str(const jerry_value_t value, char *buf, int maxlen,
                      bool quotes)
{
    // requires: buf has at least maxlen characters
    //  effects: writes a string representation of the value to buf; when
    //             processing a string value, puts quotes around it if quotes
    //             is true
    //  returns: true if the representation was complete or false if it
    //             was abbreviated
    if (jerry_value_is_array(value)) {
        uint32_t len = jerry_get_array_length(value);
        sprintf(buf, "[Array - length %lu]", len);
        return false;
    }
    else if (jerry_value_is_boolean(value)) {
        uint8_t val = jerry_get_boolean_value(value);
        sprintf(buf, (val) ? "true" : "false");
    }
    else if (jerry_value_is_function(value)) {
        sprintf(buf, "[Function]");
    }
    else if (jerry_value_is_number(value)) {
        int type = is_int(value);
        if (type == IS_NUMBER) {
#ifdef ZJS_PRINT_FLOATS
            double num = jerry_get_number_value(value);
            sprintf(buf, "%f", num);
#else
            int32_t num = (int32_t)jerry_get_number_value(value);
            sprintf(buf, "[Float ~%li]", num);
#endif
        } else if (type == IS_UINT) {
            uint32_t num = (uint32_t)jerry_get_number_value(value);
            sprintf(buf, "%lu", num);
        } else if (type == IS_INT) {
            int32_t num = (int32_t)jerry_get_number_value(value);
            // Linux and Zephyr print int32_t's differently if %li is used
#ifdef ZJS_LINUX_BUILD
            sprintf(buf, "%i", num);
#else
            sprintf(buf, "%li", num);
#endif
        }
    }
    else if (jerry_value_is_null(value)) {
        sprintf(buf, "null");
    }
    // NOTE: important that checks for function and array were above this
    else if (jerry_value_is_object(value)) {
        sprintf(buf, "[Object]");
    }
    else if (jerry_value_is_string(value)) {
        jerry_size_t size = jerry_get_string_size(value);
        if (size >= maxlen) {
            sprintf(buf, "[String - length %lu]", size);
        }
        else {
            char buffer[++size];
            zjs_copy_jstring(value, buffer, &size);
            if (quotes) {
                sprintf(buf, "\"%s\"", buffer);
            }
            else {
                sprintf(buf, "%s", buffer);
            }
        }
    }
    else if (jerry_value_is_undefined(value)) {
        sprintf(buf, "undefined");
    }
    else {
        // should never get this
        sprintf(buf, "UNKNOWN");
    }
    return true;
}

static void print_value(const jerry_value_t value, FILE *out, bool deep,
                        bool quotes)
{
    char buf[MAX_STR_LENGTH];
    if (!value2str(value, buf, MAX_STR_LENGTH, quotes) && deep) {
        if (jerry_value_is_array(value)) {
            uint32_t len = jerry_get_array_length(value);
            fprintf(out, "[");
            for (int i = 0; i < len; i++) {
                if (i) {
                    fprintf(out, ", ");
                }
                jerry_value_t element = jerry_get_property_by_index(value, i);
                print_value(element, out, false, true);
                jerry_release_value(element);
            }
            fprintf(out, "]");
        }
    }
    else {
        fprintf(out, "%s", buf);
    }
}

static jerry_value_t do_print(const jerry_value_t function_obj,
                              const jerry_value_t this,
                              const jerry_value_t argv[],
                              const jerry_length_t argc,
                              FILE* out)
{
    for (int i = 0; i < argc; i++) {
        if (i) {
            // insert spaces between arguments
            fprintf(out, " ");
        }
        print_value(argv[i], out, true, false);
    }
    fprintf(out, "\n");
    return ZJS_UNDEFINED;
}

static jerry_value_t console_log(const jerry_value_t function_obj,
                                 const jerry_value_t this,
                                 const jerry_value_t argv[],
                                 const jerry_length_t argc)
{
    return do_print(function_obj, this, argv, argc, stdout);
}

static jerry_value_t console_error(const jerry_value_t function_obj,
                                   const jerry_value_t this,
                                   const jerry_value_t argv[],
                                   const jerry_length_t argc)
{
    return do_print(function_obj, this, argv, argc, stderr);
}

static jerry_value_t console_time(const jerry_value_t function_obj,
                                  const jerry_value_t this,
                                  const jerry_value_t argv[],
                                  const jerry_length_t argc)
{
    if (argc < 1 || !jerry_value_is_string(argv[0])) {
        return TYPE_ERROR("invalid argument");
    }

    uint32_t start = zjs_port_timer_get_uptime();

    jerry_value_t num = jerry_create_number(start);
    jerry_set_property(gbl_time_obj, argv[0], num);
    jerry_release_value(num);
    return ZJS_UNDEFINED;
}

static jerry_value_t console_time_end(const jerry_value_t function_obj,
                                      const jerry_value_t this,
                                      const jerry_value_t argv[],
                                      const jerry_length_t argc)
{
    if (argc < 1 || !jerry_value_is_string(argv[0])) {
        return TYPE_ERROR("invalid argument");
    }

    jerry_value_t num = jerry_get_property(gbl_time_obj, argv[0]);
    jerry_delete_property(gbl_time_obj, argv[0]);

    if (!jerry_value_is_number(num)) {
        jerry_release_value(num);
        return TYPE_ERROR("unexpected value");
    }

    uint32_t start = (uint32_t)jerry_get_number_value(num);
    uint32_t milli = zjs_port_timer_get_uptime() - start;
    jerry_release_value(num);

    char *label = zjs_alloc_from_jstring(argv[0], NULL);
    const char *const_label = "unknown";
    if (label) {
        const_label = label;
    }

    ZJS_PRINT("%s: %lums\n", const_label, milli);
    zjs_free(label);
    return ZJS_UNDEFINED;
}

static jerry_value_t console_assert(const jerry_value_t function_obj,
                                    const jerry_value_t this,
                                    const jerry_value_t argv[],
                                    const jerry_length_t argc)
{
    char message[MAX_STR_LENGTH];
    if (!jerry_value_is_boolean(argv[0])) {
        return TYPE_ERROR("invalid parameter");
    }
    bool b = jerry_get_boolean_value(argv[0]);
    if (!b) {
        if (argc > 1) {
            value2str(argv[1], message, MAX_STR_LENGTH, false);
            return zjs_custom_error("AssertionError", message);
        } else {
            return zjs_custom_error("AssertionError", "console.assert");
        }
    }
    return ZJS_UNDEFINED;
}

void zjs_console_init(void)
{
    jerry_value_t console = jerry_create_object();
    zjs_obj_add_function(console, console_log, "log");
    zjs_obj_add_function(console, console_log, "info");
    zjs_obj_add_function(console, console_error, "error");
    zjs_obj_add_function(console, console_error, "warn");
    zjs_obj_add_function(console, console_time, "time");
    zjs_obj_add_function(console, console_time_end, "timeEnd");
    zjs_obj_add_function(console, console_assert, "assert");

    jerry_value_t global_obj = jerry_get_global_object();
    zjs_set_property(global_obj, "console", console);

    jerry_release_value(console);
    jerry_release_value(global_obj);

    // initialize the time object
    gbl_time_obj = jerry_create_object();
}

void zjs_console_cleanup()
{
    jerry_release_value(gbl_time_obj);
}

#endif  // BUILD_MODULE_CONSOLE