forked from gonzus/JavaScript-V8-XS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pl_console.cc
234 lines (210 loc) · 7.25 KB
/
pl_console.cc
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
#include <stdarg.h>
#include <stdio.h>
#include <v8.h>
#include "pl_util.h"
#include "pl_console.h"
#define NEED_newRV_noinc_GLOBAL
#include "ppport.h"
#define CONSOLE_FLUSH 0x01
#define CONSOLE_TARGET_STDOUT 0x10
#define CONSOLE_TARGET_STDERR 0x20
static void save_msg(pTHX_ V8Context* ctx, const char* target, SV* message)
{
STRLEN tlen = strlen(target);
AV* data = 0;
int top = 0;
SV* pvalue = 0;
SV** found = hv_fetch(ctx->msgs, target, tlen, 0);
if (found) {
SV* ref = SvRV(*found);
/* value not a valid arrayref? bail out */
if (SvTYPE(ref) != SVt_PVAV) {
return;
}
data = (AV*) ref;
top = av_top_index(data);
} else {
SV* ref = 0;
data = newAV();
ref = newRV_noinc((SV*) data);
if (hv_store(ctx->msgs, target, tlen, ref, 0)) {
SvREFCNT_inc(ref);
}
top = -1;
}
pvalue = sv_2mortal(message);
if (av_store(data, ++top, pvalue)) {
SvREFCNT_inc(pvalue);
}
else {
croak("Could not store message in target %*.*s\n", (int) tlen, (int) tlen, target);
}
}
static int console_output_line(pTHX_ V8Context* ctx, SV* message, unsigned int flags)
{
STRLEN mlen = 0;
char* mstr = SvPV(message, mlen);
if (ctx->flags & V8_OPT_FLAG_SAVE_MESSAGES) {
const char* target = (flags & CONSOLE_TARGET_STDERR) ? "stderr" : "stdout";
save_msg(aTHX_ ctx, target, message);
}
else {
PerlIO* fp = (flags & CONSOLE_TARGET_STDERR) ? PerlIO_stderr() : PerlIO_stdout();
PerlIO_printf(fp, "%s\n", mstr);
if (flags & CONSOLE_FLUSH) {
PerlIO_flush(fp);
}
}
return mlen;
}
static int console_output(const FunctionCallbackInfo<Value>& args, unsigned int flags, const char* preamble = 0, bool stack = false, int start = 0)
{
dTHX;
Isolate* isolate = args.GetIsolate();
HandleScope handle_scope(isolate);
Local<External> v8_val = Local<External>::Cast(args.Data());
V8Context* ctx = (V8Context*) v8_val->Value();
Local<Context> context = Local<Context>::New(isolate, *ctx->persistent_context);
Context::Scope context_scope(context);
SV* message = newSVpvs("");
bool separate = false;
if (preamble) {
Perl_sv_catpv(aTHX_ message, preamble);
Perl_sv_catpvf(aTHX_ message, ":");
separate = true;
}
Local<Object> global = context->Global();
Local<Name> to_str_nam = String::NewFromUtf8(isolate, "JSON_stringify_with_cycles", NewStringType::kNormal).ToLocalChecked();
Local<Value> to_str_val;
if (!global->Get(context, to_str_nam).ToLocal(&to_str_val)) {
return 0;
}
if (to_str_val->IsFunction()) {
Local<Function> to_str_fun = Local<Function>::Cast(to_str_val);
Local<Value> sargs[1];
for (int j = start; j < args.Length(); j++) {
/* add separator if necessary */
if (separate) {
Perl_sv_catpvf(aTHX_ message, " ");
}
separate = true;
/* for non-objects, just get their value as string */
if (!args[j]->IsObject()) {
String::Utf8Value str(isolate, args[j]);
Perl_sv_catpvf(aTHX_ message, "%s", *str);
continue;
}
/* convert each object arg to JSON */
sargs[0] = args[j];
Local<Value> ret;
if (!to_str_fun->Call(context, global, 1, sargs).ToLocal(&ret)) {
continue;
}
Local<String> json = Local<String>::Cast(ret);
String::Utf8Value str(isolate, json);
Perl_sv_catpvf(aTHX_ message, "%s", *str);
}
}
if (stack) {
#if 0
/* TODO: generate a stack trace */
v8_inspector::V8Inspector* inspector = new v8_inspector::V8InspectorImpl::V8InspectorImpl();
inspector->captureStackTrace();
#endif
}
return console_output_line(aTHX_ ctx, message, flags);
}
static void console_assert(const FunctionCallbackInfo<Value>& args)
{
if (args.Length() < 1) {
return;
}
Local<Boolean> v8_silent = Local<Boolean>::Cast(args[0]);
bool silent = v8_silent->Value();
if (silent) {
return;
}
console_output(args, CONSOLE_TARGET_STDOUT | CONSOLE_FLUSH, "AssertionError", true, 1);
}
static void console_log(const FunctionCallbackInfo<Value>& args)
{
console_output(args, CONSOLE_TARGET_STDOUT | CONSOLE_FLUSH);
}
static void console_debug(const FunctionCallbackInfo<Value>& args)
{
console_output(args, CONSOLE_TARGET_STDOUT | CONSOLE_FLUSH);
}
static void console_trace(const FunctionCallbackInfo<Value>& args)
{
console_output(args, CONSOLE_TARGET_STDOUT | CONSOLE_FLUSH, "Trace", true);
}
static void console_info(const FunctionCallbackInfo<Value>& args)
{
console_output(args, CONSOLE_TARGET_STDOUT | CONSOLE_FLUSH);
}
static void console_warn(const FunctionCallbackInfo<Value>& args)
{
console_output(args, CONSOLE_TARGET_STDERR | CONSOLE_FLUSH);
}
static void console_error(const FunctionCallbackInfo<Value>& args)
{
console_output(args, CONSOLE_TARGET_STDERR | CONSOLE_FLUSH, "Error", true);
}
static void console_exception(const FunctionCallbackInfo<Value>& args)
{
console_output(args, CONSOLE_TARGET_STDERR | CONSOLE_FLUSH, "Error", true);
}
static void console_dir(const FunctionCallbackInfo<Value>& args)
{
console_output(args, CONSOLE_TARGET_STDERR | CONSOLE_FLUSH);
}
int pl_register_console_functions(V8Context* ctx)
{
typedef void (*Handler)(const FunctionCallbackInfo<Value>& args);
static struct Data {
const char* name;
Handler func;
} data[] = {
{ "console.assert" , console_assert },
{ "console.log" , console_log },
{ "console.debug" , console_debug },
{ "console.trace" , console_trace },
{ "console.info" , console_info },
{ "console.warn" , console_warn },
{ "console.error" , console_error },
{ "console.exception", console_exception },
{ "console.dir" , console_dir },
};
HandleScope handle_scope(ctx->isolate);
Local<Context> context = Local<Context>::New(ctx->isolate, *ctx->persistent_context);
Context::Scope context_scope(context);
Local<Value> v8_ctx = External::New(ctx->isolate, ctx);
int n = sizeof(data) / sizeof(data[0]);
int c = 0;
for (int j = 0; j < n; ++j) {
Local<Object> object;
Local<Value> slot;
bool found = find_parent(ctx, data[j].name, context, object, slot, true);
if (!found) {
pl_show_error(ctx, "could not create parent for %s", data[j].name);
continue;
}
Local<FunctionTemplate> ft = FunctionTemplate::New(ctx->isolate, data[j].func, v8_ctx);
Local<Function> v8_func = ft->GetFunction(context).ToLocalChecked();
if (!object->Set(context, slot, v8_func).IsNothing()) {
continue;
}
++c;
}
return c;
}
int pl_show_error(V8Context* ctx, const char* fmt, ...)
{
dTHX;
SV* message = newSVpvs("");
va_list ap;
va_start(ap, fmt);
Perl_sv_vcatpvf(aTHX_ message, fmt, &ap);
va_end(ap);
return console_output_line(aTHX_ ctx, message, CONSOLE_TARGET_STDERR | CONSOLE_FLUSH);
}