-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathConsole.cpp
More file actions
169 lines (144 loc) · 4.26 KB
/
Console.cpp
File metadata and controls
169 lines (144 loc) · 4.26 KB
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
#include "Console.h"
#include "js_native_api_types.h"
#include <iostream>
#include <string>
namespace charon {
void Console::init(napi_env env) {
napi_value global, Console, console;
napi_get_global(env, &global);
const napi_property_descriptor properties[] = {
{
.utf8name = "log",
.name = nullptr,
.method = log,
.getter = nullptr,
.setter = nullptr,
.value = nullptr,
.attributes = napi_default,
.data = (void *)kConsoleStreamLog,
},
{
.utf8name = "error",
.name = nullptr,
.method = log,
.getter = nullptr,
.setter = nullptr,
.value = nullptr,
.attributes = napi_default,
.data = (void *)kConsoleStreamError,
},
{
.utf8name = "warn",
.name = nullptr,
.method = log,
.getter = nullptr,
.setter = nullptr,
.value = nullptr,
.attributes = napi_default,
.data = (void *)kConsoleStreamWarn,
},
};
napi_define_class(env, "Console", NAPI_AUTO_LENGTH, Console::constructor,
nullptr, 3, properties, &Console);
napi_new_instance(env, Console, 0, nullptr, &console);
const napi_property_descriptor globalProperties[] = {
{
.utf8name = "console",
.name = nullptr,
.method = nullptr,
.getter = nullptr,
.setter = nullptr,
.value = console,
.attributes = napi_default,
.data = nullptr,
},
{
.utf8name = "Console",
.name = nullptr,
.method = nullptr,
.getter = nullptr,
.setter = nullptr,
.value = Console,
.attributes = napi_default,
.data = nullptr,
},
};
napi_define_properties(env, global, 2, globalProperties);
}
napi_value Console::constructor(napi_env env, napi_callback_info cbinfo) {
napi_value thisArg;
napi_get_cb_info(env, cbinfo, nullptr, nullptr, &thisArg, nullptr);
return thisArg;
}
napi_value Console::log(napi_env env, napi_callback_info cbinfo) {
size_t argc = 0;
ConsoleStream stream;
void *data = nullptr;
napi_get_cb_info(env, cbinfo, &argc, nullptr, nullptr, &data);
stream = ConsoleStream((unsigned long)data);
napi_value argv[argc];
napi_get_cb_info(env, cbinfo, &argc, argv, nullptr, nullptr);
napi_value global, Symbol, SymbolFor, symbolDescription, symbol;
napi_get_global(env, &global);
napi_get_named_property(env, global, "Symbol", &Symbol);
napi_get_named_property(env, Symbol, "for", &SymbolFor);
napi_create_string_utf8(env, "nodejs.util.inspect.custom", NAPI_AUTO_LENGTH,
&symbolDescription);
napi_call_function(env, global, SymbolFor, 1, &symbolDescription, &symbol);
std::string string;
for (size_t i = 0; i < argc; i++) {
napi_valuetype type;
napi_typeof(env, argv[i], &type);
bool hasSymbol = false;
if (type == napi_object || type == napi_function) {
napi_has_property(env, argv[i], symbol, &hasSymbol);
}
napi_value argstr = nullptr;
if (hasSymbol) {
napi_value fn;
napi_get_property(env, argv[i], symbol, &fn);
napi_call_function(env, argv[i], fn, 0, nullptr, &argstr);
} else {
napi_coerce_to_string(env, argv[i], &argstr);
}
size_t length = 0;
napi_get_value_string_utf8(env, argstr, nullptr, 0, &length);
char *strbuf = (char *)malloc(length + 2);
napi_get_value_string_utf8(env, argstr, strbuf, length + 2, &length);
strbuf[length] = i >= (argc - 1) ? '\0' : ' ';
strbuf[length + 1] = '\0';
string += strbuf;
}
// TODO(dj): what if we made this pretty?
std::string log;
log += "[JS]";
switch (stream) {
case kConsoleStreamLog:
// log += "LOG";
break;
case kConsoleStreamError:
log += " ERROR";
break;
case kConsoleStreamWarn:
log += " WARN";
break;
}
log += " ";
log += string;
log += "\n";
switch (stream) {
case kConsoleStreamLog:
std::cout << log;
break;
case kConsoleStreamError:
std::cerr << log;
break;
case kConsoleStreamWarn:
std::cerr << log;
break;
}
napi_value undefined;
napi_get_undefined(env, &undefined);
return undefined;
}
} // namespace charon