duk.xs

filter: Statementstmt 86.8% 305 / 351 Branchbran 66.8% 103 / 154 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 80.7% 408 / 505 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« util.c ↑ Summary duk_console.c »
1 #define PERL_NO_GET_CONTEXT /* we want efficiency */
2 #include "EXTERN.h"
3 #include "perl.h"
4 #include "XSUB.h"
5 #include "ppport.h"
6  
7 /*
8 * Duktape is an embeddable Javascript engine, with a focus on portability and
9 * compact footprint.
10 *
11 * http://duktape.org/index.html
12 */
13 #include "util.h"
14 #include "duktape.h"
15 #include "c_eventloop.h"
16 #include "duk_console.h"
17  
18 #define DUK_SLOT_CALLBACK "_perl_.callback"
19  
20 #define DUK_OPT_NAME_GATHER_STATS "gather_stats"
21 #define DUK_OPT_NAME_SAVE_MESSAGES "save_messages"
22  
23 #define DUK_OPT_FLAG_GATHER_STATS 0x01
24 #define DUK_OPT_FLAG_SAVE_MESSAGES 0x02
25  
26 /*
27 * This is our internal data structure. For now it only contains a pointer to
28 * a duktape context. We will add other stuff here.
29 */
30 typedef struct Duk {
31 duk_context* ctx;
32 int pagesize;
33 unsigned long flags;
34 HV* stats;
35 HV* msgs;
36 } Duk;
37  
38 typedef struct Stats {
39 double t0, t1;
40 double m0, m1;
41 } Stats;
42  
43 /*
44 * We use these two functions to convert back and forth between the Perl
45 * representation of an object and the JS one.
46 *
47 * Because data in Perl and JS can be nested (array of hashes of arrays of...),
48 * the functions are recursive.
49 *
50 * duk_to_perl: takes a JS value from a given position in the duktape stack,
51 * and creates the equivalent Perl value.
52 *
53 * perl_to_duk: takes a Perl value and leaves the equivalent JS value at the
54 * top of the duktape stack.
55 */
56 static SV* duk_to_perl(pTHX_ duk_context* ctx, int pos);
57 static int perl_to_duk(pTHX_ SV* value, duk_context* ctx);
58  
59 /*
60 * Native print callable from JS
61 */
62 4 static duk_ret_t native_print(duk_context* ctx)
63 {
64 4 duk_push_lstring(ctx, " ", 1);
65 4 duk_insert(ctx, 0);
66 4 duk_join(ctx, duk_get_top(ctx) - 1);
67 4 PerlIO_stdoutf("%s\n", duk_safe_to_string(ctx, -1));
68 4 return 0; // no return value
69 }
70  
71 /*
72 * Get JS compatible 'now' timestamp (millisecs since 1970).
73 */
74 1 static duk_ret_t native_now_ms(duk_context* ctx)
75 {
76 1 duk_push_number(ctx, (duk_double_t) (now_us() / 1000.0));
77 1 return 1; // return value at top
78 }
79  
80 16 static void save_stat(pTHX_ Duk* duk, const char* category, const char* name, double value)
81 {
82 16 STRLEN clen = strlen(category);
83 16 STRLEN nlen = strlen(name);
84 16 HV* data = 0;
85 16 SV** found = hv_fetch(duk->stats, category, clen, 0);
86 16 ▶ if (found) {
Branch: 1 branch
BranchTrueFalseTotal
if (found) { 14 2 16
87 14 SV* ref = SvRV(*found);
88 /* value not a valid hashref? bail out */
89 14 ▶ if (SvTYPE(ref) != SVt_PVHV) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvTYPE(ref) != SVt_PVHV) { 0 14 14
90 0 return;
91 }
92 14 data = (HV*) ref;
93 } else {
94 2 data = newHV();
95 2 SV* ref = newRV_noinc((SV*) data);
96 2 ▶ if (hv_store(duk->stats, category, clen, ref, 0)) {
Branch: 1 branch
BranchTrueFalseTotal
if (hv_store(duk->stats, category, clen, ref, 0)) { 2 0 2
97 2 SvREFCNT_inc(ref);
98 }
99 }
100  
101 16 SV* pvalue = sv_2mortal(newSVnv(value));
102 16 ▶ if (hv_store(data, name, nlen, pvalue, 0)) {
Branch: 1 branch
BranchTrueFalseTotal
if (hv_store(data, name, nlen, pvalue, 0)) { 16 0 16
103 16 SvREFCNT_inc(pvalue);
104 }
105 }
106  
107 8 static void save_msg(pTHX_ Duk* duk, const char* target, SV* message)
108 {
109 8 STRLEN tlen = strlen(target);
110 8 AV* data = 0;
111 8 int top = 0;
112 8 SV** found = hv_fetch(duk->msgs, target, tlen, 0);
113 8 ▶ if (found) {
Branch: 1 branch
BranchTrueFalseTotal
if (found) { 4 4 8
114 4 SV* ref = SvRV(*found);
115 /* value not a valid arrayref? bail out */
116 4 ▶ if (SvTYPE(ref) != SVt_PVAV) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvTYPE(ref) != SVt_PVAV) { 0 4 4
117 0 return;
118 }
119 4 data = (AV*) ref;
120 4 top = av_top_index(data);
121 } else {
122 4 data = newAV();
123 4 SV* ref = newRV_noinc((SV*) data);
124 4 ▶ if (hv_store(duk->msgs, target, tlen, ref, 0)) {
Branch: 1 branch
BranchTrueFalseTotal
if (hv_store(duk->msgs, target, tlen, ref, 0)) { 4 0 4
125 4 SvREFCNT_inc(ref);
126 }
127 4 top = -1;
128 }
129  
130 8 SV* pvalue = sv_2mortal(message);
131 8 ▶ if (av_store(data, ++top, pvalue)) {
Branch: 1 branch
BranchTrueFalseTotal
if (av_store(data, ++top, pvalue)) { 8 0 8
132 8 SvREFCNT_inc(pvalue);
133 }
134 else {
135 0 croak("Could not store message in target %*.*s\n", (int) tlen, (int) tlen, target);
136 }
137 }
138  
139 /*
140 * This is a generic dispatcher that allows calling any Perl function from JS,
141 * after it has been registered under a name in JS.
142 */
143 10 static duk_ret_t perl_caller(duk_context* ctx)
144 {
145 10 duk_idx_t j = 0;
146  
147 // get actual Perl CV stored as a function property
148 10 duk_push_current_function(ctx);
149 10 ▶ if (!duk_get_prop_lstring(ctx, -1, DUK_SLOT_CALLBACK, sizeof(DUK_SLOT_CALLBACK) - 1)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!duk_get_prop_lstring(ctx, -1, DUK_SLOT_CALLBACK, sizeof(DUK_SLOT_CALLBACK) - 1)) { 0 10 10
150 0 croak("Calling Perl handler for a non-Perl function\n");
151 }
152 10 SV* func = (SV*) duk_get_pointer(ctx, -1);
153 10 duk_pop_2(ctx); /* pop pointer and function */
154 10 ▶ if (func == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (func == 0) { 0 10 10
155 0 croak("Could not get value for property %s\n", DUK_SLOT_CALLBACK);
156 }
157  
158 // prepare Perl environment for calling the CV
159 dTHX;
160 10 dSP;
161 10 ENTER;
162 10 SAVETMPS;
163 10 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 10 10
164  
165 // pass in the stack each of the params we received
166 10 duk_idx_t nargs = duk_get_top(ctx);
167 23 ▶ for (j = 0; j < nargs; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < nargs; j++) { 13 10 23
168 13 SV* val = duk_to_perl(aTHX_ ctx, j);
169 13 ▶ mXPUSHs(val);
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHs(val); 0 13 13
170 }
171  
172 // call actual Perl CV, passing all params
173 10 PUTBACK;
174 10 call_sv(func, G_SCALAR | G_EVAL);
175 10 SPAGAIN;
176  
177 // get returned value from Perl and push its JS equivalent back in
178 // duktape's stack
179 10 SV* ret = POPs;
180 10 perl_to_duk(aTHX_ ret, ctx);
181  
182 // cleanup and return 1, indicating we are returning a value
183 10 PUTBACK;
184 10 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 10 0 10
185 10 LEAVE;
186 10 return 1;
187 }
188  
189 248 static SV* duk_to_perl(pTHX_ duk_context* ctx, int pos)
190 {
191 248 SV* ret = &PL_sv_undef; // return undef by default
192 248 switch (duk_get_type(ctx, pos)) {
193 32 case DUK_TYPE_NONE:
194 case DUK_TYPE_UNDEFINED:
195 case DUK_TYPE_NULL: {
196 32 break;
197 }
198 1 case DUK_TYPE_BOOLEAN: {
199 1 duk_bool_t val = duk_get_boolean(ctx, pos);
200 1 ret = newSViv(val);
201 1 break;
202 }
203 98 case DUK_TYPE_NUMBER: {
204 98 duk_double_t val = duk_get_number(ctx, pos);
205 98 ret = newSVnv(val); // JS numbers are always doubles
206 98 break;
207 }
208 19 case DUK_TYPE_STRING: {
209 19 duk_size_t clen = 0;
210 19 const char* cstr = duk_get_lstring(ctx, pos, &clen);
211 19 ret = newSVpvn(cstr, clen);
212 19 break;
213 }
214 97 case DUK_TYPE_OBJECT: {
215 97 ▶ if (duk_is_c_function(ctx, pos)) {
Branch: 1 branch
BranchTrueFalseTotal
if (duk_is_c_function(ctx, pos)) { 1 96 97
216 // if the JS function has a slot with the Perl callback,
217 // then we know we created it, so we return that
218 1 ▶ if (!duk_get_prop_lstring(ctx, -1, DUK_SLOT_CALLBACK, sizeof(DUK_SLOT_CALLBACK) - 1)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!duk_get_prop_lstring(ctx, -1, DUK_SLOT_CALLBACK, sizeof(DUK_SLOT_CALLBACK) - 1)) { 0 1 1
219 0 croak("JS object is an unrecognized function\n");
220 }
221 1 ret = (SV*) duk_get_pointer(ctx, -1);
222 1 duk_pop(ctx); // pop function
223 96 ▶ } else if (duk_is_array(ctx, pos)) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (duk_is_array(ctx, pos)) { 61 35 96
224 61 int array_top = duk_get_length(ctx, pos);
225 61 AV* values = newAV();
226 61 int j = 0;
227 176 ▶ for (j = 0; j < array_top; ++j) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < array_top; ++j) { 115 61 176
228 115 ▶ if (!duk_get_prop_index(ctx, pos, j)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!duk_get_prop_index(ctx, pos, j)) { 0 115 115
229 0 continue; // index doesn't exist => end of array
230 }
231 115 SV* nested = sv_2mortal(duk_to_perl(aTHX_ ctx, -1));
232 115 duk_pop(ctx); // value in current pos
233 115 ▶ if (!nested) {
Branch: 1 branch
BranchTrueFalseTotal
if (!nested) { 0 115 115
234 0 croak("Could not create Perl SV for array\n");
235 }
236 115 ▶ if (av_store(values, j, nested)) {
Branch: 1 branch
BranchTrueFalseTotal
if (av_store(values, j, nested)) { 115 0 115
237 115 SvREFCNT_inc(nested);
238 }
239 }
240 61 ret = newRV_noinc((SV*) values);
241 35 ▶ } else if (duk_is_object(ctx, pos)) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (duk_is_object(ctx, pos)) { 35 0 35
242 35 HV* values = newHV();
243 35 duk_enum(ctx, pos, 0);
244 60 ▶ while (duk_next(ctx, -1, 1)) { // get key and value
Branch: 1 branch
BranchTrueFalseTotal
while (duk_next(ctx, -1, 1)) { // get key and value 25 35 60
245 25 duk_size_t klen = 0;
246 25 const char* kstr = duk_get_lstring(ctx, -2, &klen);
247 25 SV* nested = sv_2mortal(duk_to_perl(aTHX_ ctx, -1));
248 25 duk_pop_2(ctx); // key and value
249 25 ▶ if (!nested) {
Branch: 1 branch
BranchTrueFalseTotal
if (!nested) { 0 25 25
250 0 croak("Could not create Perl SV for hash\n");
251 }
252 25 ▶ if (hv_store(values, kstr, klen, nested, 0)) {
Branch: 1 branch
BranchTrueFalseTotal
if (hv_store(values, kstr, klen, nested, 0)) { 25 0 25
253 25 SvREFCNT_inc(nested);
254 }
255 }
256 35 duk_pop(ctx); // iterator
257 35 ret = newRV_noinc((SV*) values);
258 } else {
259 0 croak("JS object with an unrecognized type\n");
260 }
261 97 break;
262 }
263 1 case DUK_TYPE_POINTER: {
264 1 ret = (SV*) duk_get_pointer(ctx, -1);
265 1 break;
266 }
267 0 case DUK_TYPE_BUFFER: {
268 0 croak("Don't know how to deal with a JS buffer\n");
269 break;
270 }
271 0 case DUK_TYPE_LIGHTFUNC: {
272 0 croak("Don't know how to deal with a JS lightfunc\n");
273 break;
274 }
275 0 default:
276 0 croak("Don't know how to deal with an undetermined JS object\n");
277 break;
278 }
279 248 return ret;
280 }
281  
282 135 static int perl_to_duk(pTHX_ SV* value, duk_context* ctx)
283 {
284 135 int ret = 1;
285 135 ▶ if (!SvOK(value)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(value)) { 3 132 135
286 3 duk_push_null(ctx);
287 132 ▶ } else if (SvIOK(value)) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (SvIOK(value)) { 61 71 132
288 61 int val = SvIV(value);
289 61 duk_push_int(ctx, val);
290 71 ▶ } else if (SvNOK(value)) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (SvNOK(value)) { 2 69 71
291 2 double val = SvNV(value);
292 2 duk_push_number(ctx, val);
293 69 ▶ } else if (SvPOK(value)) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (SvPOK(value)) { 9 60 69
294 9 STRLEN vlen = 0;
295 9 const char* vstr = SvPV_const(value, vlen);
296 9 duk_push_lstring(ctx, vstr, vlen);
297 60 ▶ } else if (SvROK(value)) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (SvROK(value)) { 60 0 60
298 60 SV* ref = SvRV(value);
299 60 ▶ if (SvTYPE(ref) == SVt_PVAV) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvTYPE(ref) == SVt_PVAV) { 41 19 60
300 41 AV* values = (AV*) ref;
301 41 duk_idx_t array_pos = duk_push_array(ctx);
302 41 int array_top = av_top_index(values);
303 41 int count = 0;
304 41 int j = 0;
305 120 ▶ for (j = 0; j <= array_top; ++j) { // yes, [0, array_top]
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j <= array_top; ++j) { // yes, [0, array_top] 79 41 120
306 79 SV** elem = av_fetch(values, j, 0);
307 79 ▶ if (!elem || !*elem) {
Branch: 2 branches
BranchTrueFalseTotal
if (!elem || !*elem) { 79 0 79
if (!elem || !*elem) { 79 0 79
308 break; // could not get element
309 }
310 79 ▶ if (!perl_to_duk(aTHX_ *elem, ctx)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!perl_to_duk(aTHX_ *elem, ctx)) { 0 79 79
311 0 croak("Could not create JS element for array\n");
312 }
313 79 ▶ if (!duk_put_prop_index(ctx, array_pos, count)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!duk_put_prop_index(ctx, array_pos, count)) { 0 79 79
314 0 croak("Could not push JS element for array\n");
315 }
316 79 ++count;
317 }
318 19 ▶ } else if (SvTYPE(ref) == SVt_PVHV) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (SvTYPE(ref) == SVt_PVHV) { 16 3 19
319 16 HV* values = (HV*) ref;
320 16 duk_idx_t hash_pos = duk_push_object(ctx);
321 16 hv_iterinit(values);
322 17 while (1) {
323 33 SV* value = 0;
324 33 I32 klen = 0;
325 33 char* kstr = 0;
326 33 HE* entry = hv_iternext(values);
327 33 ▶ if (!entry) {
Branch: 1 branch
BranchTrueFalseTotal
if (!entry) { 16 17 33
328 16 break; // no more hash keys
329 }
330 17 kstr = hv_iterkey(entry, &klen);
331 17 ▶ if (!kstr || klen < 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (!kstr || klen < 0) { 17 0 17
if (!kstr || klen < 0) { 0 17 17
332 0 continue; // invalid key
333 }
334 17 value = hv_iterval(values, entry);
335 17 ▶ if (!value) {
Branch: 1 branch
BranchTrueFalseTotal
if (!value) { 0 17 17
336 0 continue; // invalid value
337 }
338 17 ▶ if (!perl_to_duk(aTHX_ value, ctx)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!perl_to_duk(aTHX_ value, ctx)) { 0 17 17
339 0 croak("Could not create JS element for hash\n");
340 }
341 17 ▶ if (! duk_put_prop_lstring(ctx, hash_pos, kstr, klen)) {
Branch: 1 branch
BranchTrueFalseTotal
if (! duk_put_prop_lstring(ctx, hash_pos, kstr, klen)) { 0 17 17
342 0 croak("Could not push JS element for hash\n");
343 }
344 }
345 3 ▶ } else if (SvTYPE(ref) == SVt_PVCV) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (SvTYPE(ref) == SVt_PVCV) { 3 0 3
346 // use perl_caller as generic handler, but store the real callback
347 // in a slot, from where we can later retrieve it
348 3 duk_push_c_function(ctx, perl_caller, DUK_VARARGS);
349 3 SV* func = newSVsv(value);
350 3 ▶ if (!func) {
Branch: 1 branch
BranchTrueFalseTotal
if (!func) { 0 3 3
351 0 croak("Could not create copy of Perl callback\n");
352 }
353 3 duk_push_pointer(ctx, func);
354 3 ▶ if (! duk_put_prop_lstring(ctx, -2, DUK_SLOT_CALLBACK, sizeof(DUK_SLOT_CALLBACK) - 1)) {
Branch: 1 branch
BranchTrueFalseTotal
if (! duk_put_prop_lstring(ctx, -2, DUK_SLOT_CALLBACK, sizeof(DUK_SLOT_CALLBACK) - 1)) { 0 3 3
355 0 croak("Could not associate C dispatcher and Perl callback\n");
356 }
357 } else {
358 0 croak("Don't know how to deal with an undetermined Perl reference\n");
359 ret = 0;
360 }
361 } else {
362 0 croak("Don't know how to deal with an undetermined Perl object\n");
363 ret = 0;
364 }
365 135 return ret;
366 }
367  
368 30 static int set_global_or_property(pTHX_ duk_context* ctx, const char* name, SV* value)
369 {
370 30 ▶ if (sv_isobject(value)) {
Branch: 1 branch
BranchTrueFalseTotal
if (sv_isobject(value)) { 1 29 30
371 1 SV* obj = newSVsv(value);
372 1 duk_push_pointer(ctx, obj);
373 29 ▶ } else if (!perl_to_duk(aTHX_ value, ctx)) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (!perl_to_duk(aTHX_ value, ctx)) { 0 29 29
374 0 return 0;
375 }
376 30 int last_dot = -1;
377 30 int len = 0;
378 364 ▶ for (; name[len] != '\0'; ++len) {
Branch: 1 branch
BranchTrueFalseTotal
for (; name[len] != '\0'; ++len) { 334 30 364
379 334 ▶ if (name[len] == '.') {
Branch: 1 branch
BranchTrueFalseTotal
if (name[len] == '.') { 0 334 334
380 0 last_dot = len;
381 }
382 }
383 30 ▶ if (last_dot < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (last_dot < 0) { 30 0 30
384 30 ▶ if (!duk_put_global_lstring(ctx, name, len)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!duk_put_global_lstring(ctx, name, len)) { 0 30 30
385 0 croak("Could not save duk value for %s\n", name);
386 }
387 } else {
388 0 duk_push_lstring(ctx, name + last_dot + 1, len - last_dot - 1);
389 0 ▶ if (duk_peval_lstring(ctx, name, last_dot) != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (duk_peval_lstring(ctx, name, last_dot) != 0) { 0 0 0
390 0 croak("Could not eval JS object %*.*s: %s\n",
391 last_dot, last_dot, name, duk_safe_to_string(ctx, -1));
392 }
393 #if 0
394 duk_enum(ctx, -1, 0);
395 while (duk_next(ctx, -1, 0)) {
396 fprintf(stderr, "KEY [%s]\n", duk_get_string(ctx, -1));
397 duk_pop(ctx); /* pop_key */
398 }
399 #endif
400 // Have [value, key, object], need [object, key, value], hence swap
401 0 duk_swap(ctx, -3, -1);
402 0 duk_put_prop(ctx, -3);
403 0 duk_pop(ctx); // pop object
404 }
405 30 return 1;
406 }
407  
408 2014 static int session_dtor(pTHX_ SV* sv, MAGIC* mg)
409 {
410 UNUSED_ARG(sv);
411 2014 Duk* duk = (Duk*) mg->mg_ptr;
412 2014 duk_destroy_heap(duk->ctx);
413 2014 return 0;
414 }
415  
416 0 static void duk_fatal_error_handler(void* data, const char* msg)
417 {
418 UNUSED_ARG(data);
419 dTHX;
420 0 ▶ PerlIO_printf(PerlIO_stderr(), "duktape fatal error, aborting: %s\n", msg ? msg : "*NONE*");
Branch: 1 branch
BranchTrueFalseTotal
PerlIO_printf(PerlIO_stderr(), "duktape fatal error, aborting: %s\n", msg ? msg : "*NONE*"); 0 0 0
421 0 abort();
422 }
423  
424 2014 static int register_native_functions(Duk* duk)
425 {
426 static struct Data {
427 const char* name;
428 duk_c_function func;
429 } data[] = {
430 { "print" , native_print },
431 { "timestamp_ms", native_now_ms },
432 };
433 2014 duk_context* ctx = duk->ctx;
434 2014 int n = sizeof(data) / sizeof(data[0]);
435 2014 int j = 0;
436 6042 ▶ for (j = 0; j < n; ++j) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < n; ++j) { 4028 2014 6042
437 4028 duk_push_c_function(ctx, data[j].func, DUK_VARARGS);
438 4028 ▶ if (!duk_put_global_string(ctx, data[j].name)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!duk_put_global_string(ctx, data[j].name)) { 0 4028 4028
439 0 croak("Could not register native function %s\n", data[j].name);
440 }
441 }
442 2014 return n;
443 }
444 23 static int print_console_messages(duk_uint_t flags, void* data,
445 const char* fmt, va_list ap)
446 {
447 dTHX;
448  
449 UNUSED_ARG(data);
450 23 ▶ PerlIO* fp = (flags & DUK_CONSOLE_TO_STDERR) ? PerlIO_stderr() : PerlIO_stdout();
Branch: 1 branch
BranchTrueFalseTotal
PerlIO* fp = (flags & DUK_CONSOLE_TO_STDERR) ? PerlIO_stderr() : PerlIO_stdout(); 12 11 23
451 23 int ret = PerlIO_vprintf(fp, fmt, ap);
452 23 ▶ if (flags & DUK_CONSOLE_FLUSH) {
Branch: 1 branch
BranchTrueFalseTotal
if (flags & DUK_CONSOLE_FLUSH) { 23 0 23
453 23 PerlIO_flush(fp);
454 }
455 23 return ret;
456 }
457  
458  
459 8 static int save_console_messages(duk_uint_t flags, void* data,
460 const char* fmt, va_list ap)
461 {
462 dTHX;
463 8 Duk* duk = (Duk*) data;
464 8 ▶ const char* target = (flags & DUK_CONSOLE_TO_STDERR) ? "stderr" : "stdout";
Branch: 1 branch
BranchTrueFalseTotal
const char* target = (flags & DUK_CONSOLE_TO_STDERR) ? "stderr" : "stdout"; 5 3 8
465 8 SV* message = newSVpvs("");
466 va_list args_copy;
467 8 va_copy(args_copy, ap);
468 8 Perl_sv_vcatpvf(aTHX_ message, fmt, &args_copy);
469 8 save_msg(aTHX_ duk, target, message);
470 8 return SvCUR(message);
471  
472 }
473  
474 2014 static Duk* create_duktape_object(pTHX_ HV* opt)
475 {
476 2014 Duk* duk = (Duk*) malloc(sizeof(Duk));
477 2014 memset(duk, 0, sizeof(Duk));
478  
479 2014 duk->pagesize = getpagesize();
480 2014 duk->stats = newHV();
481 2014 duk->msgs = newHV();
482  
483 2014 duk->ctx = duk_create_heap(0, 0, 0, 0, duk_fatal_error_handler);
484 2014 ▶ if (!duk->ctx) {
Branch: 1 branch
BranchTrueFalseTotal
if (!duk->ctx) { 0 2014 2014
485 0 croak("Could not create duk heap\n");
486 }
487  
488 2014 register_native_functions(duk);
489  
490 // Register our event loop dispatcher, otherwise calls to
491 // dispatch_function_in_event_loop will not work.
492 2014 eventloop_register(duk->ctx);
493  
494 // initialize console object
495 2014 duk_console_init(duk->ctx, DUK_CONSOLE_PROXY_WRAPPER | DUK_CONSOLE_FLUSH);
496  
497 2014 ▶ if (opt) {
Branch: 1 branch
BranchTrueFalseTotal
if (opt) { 5 2009 2014
498 5 hv_iterinit(opt);
499 5 while (1) {
500 10 SV* value = 0;
501 10 I32 klen = 0;
502 10 char* kstr = 0;
503 10 HE* entry = hv_iternext(opt);
504 10 ▶ if (!entry) {
Branch: 1 branch
BranchTrueFalseTotal
if (!entry) { 5 5 10
505 5 break; // no more hash keys
506 }
507 5 kstr = hv_iterkey(entry, &klen);
508 5 ▶ if (!kstr || klen < 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (!kstr || klen < 0) { 5 0 5
if (!kstr || klen < 0) { 0 5 5
509 5 continue; // invalid key
510 }
511 5 value = hv_iterval(opt, entry);
512 5 ▶ if (!value) {
Branch: 1 branch
BranchTrueFalseTotal
if (!value) { 0 5 5
513 0 continue; // invalid value
514 }
515 5 ▶ if (memcmp(kstr, DUK_OPT_NAME_GATHER_STATS, klen) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (memcmp(kstr, DUK_OPT_NAME_GATHER_STATS, klen) == 0) { 2 3 5
516 2 duk->flags |= SvTRUE(value) ? DUK_OPT_FLAG_GATHER_STATS : 0;
517 2 continue;
518 }
519 3 ▶ if (memcmp(kstr, DUK_OPT_NAME_SAVE_MESSAGES, klen) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (memcmp(kstr, DUK_OPT_NAME_SAVE_MESSAGES, klen) == 0) { 3 0 3
520 3 ▶ duk->flags |= SvTRUE(value) ? DUK_OPT_FLAG_SAVE_MESSAGES : 0;
Branch: 1 branch
BranchTrueFalseTotal
duk->flags |= SvTRUE(value) ? DUK_OPT_FLAG_SAVE_MESSAGES : 0; 2 1 3
521 3 continue;
522 }
523 0 croak("Unknown option %*.*s\n", (int) klen, (int) klen, kstr);
524 }
525 }
526  
527 2014 ▶ if (duk->flags & DUK_OPT_FLAG_SAVE_MESSAGES) {
Branch: 1 branch
BranchTrueFalseTotal
if (duk->flags & DUK_OPT_FLAG_SAVE_MESSAGES) { 2 2012 2014
528 2 duk_console_register_handler(save_console_messages, duk);
529 }
530 else {
531 2012 duk_console_register_handler(print_console_messages, duk);
532 }
533  
534 2014 return duk;
535 }
536  
537 189 static void stats_start(pTHX_ Duk* duk, Stats* stats)
538 {
539 189 ▶ if (!(duk->flags & DUK_OPT_FLAG_GATHER_STATS)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!(duk->flags & DUK_OPT_FLAG_GATHER_STATS)) { 181 8 189
540 181 return;
541 }
542 8 stats->t0 = now_us();
543 8 stats->m0 = total_memory_pages() * duk->pagesize;
544 }
545  
546 189 static void stats_stop(pTHX_ Duk* duk, Stats* stats, const char* name)
547 {
548 189 ▶ if (!(duk->flags & DUK_OPT_FLAG_GATHER_STATS)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!(duk->flags & DUK_OPT_FLAG_GATHER_STATS)) { 181 8 189
549 181 return;
550 }
551 8 stats->t1 = now_us();
552 8 stats->m1 = total_memory_pages() * duk->pagesize;
553  
554 8 save_stat(aTHX_ duk, name, "elapsed_us", stats->t1 - stats->t0);
555 8 save_stat(aTHX_ duk, name, "memory_bytes", stats->m1 - stats->m0);
556 }
557  
558 4 static int run_function_in_event_loop(Duk* duk, const char* func)
559 {
560 4 duk_context* ctx = duk->ctx;
561  
562 // Start a zero timer which will call our function from the event loop.
563 4 duk_int_t rc = 0;
564 char js[256];
565 4 int len = sprintf(js, "setTimeout(function() { %s(); }, 0);", func);
566 4 rc = duk_peval_lstring(ctx, js, len);
567 4 ▶ if (rc != DUK_EXEC_SUCCESS) {
Branch: 1 branch
BranchTrueFalseTotal
if (rc != DUK_EXEC_SUCCESS) { 0 4 4
568 0 croak("Could not eval JS event loop dispatcher %*.*s: %d - %s\n",
569 len, len, js, rc, duk_safe_to_string(ctx, -1));
570 }
571 4 duk_pop(ctx);
572  
573 // Launch eventloop; this call only returns after the eventloop terminates.
574 4 rc = duk_safe_call(ctx, eventloop_run, duk, 0 /*nargs*/, 1 /*nrets*/);
575 4 ▶ if (rc != DUK_EXEC_SUCCESS) {
Branch: 1 branch
BranchTrueFalseTotal
if (rc != DUK_EXEC_SUCCESS) { 0 4 4
576 0 croak("JS event loop run failed: %d - %s\n",
577 rc, duk_safe_to_string(ctx, -1));
578 }
579 4 duk_pop(ctx);
580  
581 4 return 0;
582 }
583  
584 static MGVTBL session_magic_vtbl = { .svt_free = session_dtor };
585  
586 MODULE = JavaScript::Duktape::XS PACKAGE = JavaScript::Duktape::XS
587 PROTOTYPES: DISABLE
588  
589 #################################################################
590  
591 Duk*
592 new(char* CLASS, HV* opt = NULL)
593 CODE:
594 UNUSED_ARG(opt);
595 2014 RETVAL = create_duktape_object(aTHX_ opt);
596 OUTPUT: RETVAL
597  
598 HV*
599 get_stats(Duk* duk)
600 CODE:
601 9 RETVAL = duk->stats;
602 OUTPUT: RETVAL
603  
604 void
605 reset_stats(Duk* duk)
606 PPCODE:
607 0 duk->stats = newHV();
608  
609 HV*
610 get_msgs(Duk* duk)
611 CODE:
612 3 RETVAL = duk->msgs;
613 OUTPUT: RETVAL
614  
615 void
616 reset_msgs(Duk* duk)
617 PPCODE:
618 2 duk->msgs = newHV();
619  
620 SV*
621 get(Duk* duk, const char* name)
622 PREINIT:
623 35 duk_context* ctx = 0;
624 Stats stats;
625 CODE:
626 35 ctx = duk->ctx;
627 35 RETVAL = &PL_sv_undef; // return undef by default
628 35 stats_start(aTHX_ duk, &stats);
629 35 ▶ if (duk_get_global_string(ctx, name)) {
Branch: 1 branch
BranchTrueFalseTotal
if (duk_get_global_string(ctx, name)) { 35 0 35
630 35 RETVAL = duk_to_perl(aTHX_ ctx, -1);
631 35 duk_pop(ctx);
632 }
633 35 stats_stop(aTHX_ duk, &stats, "get");
634 OUTPUT: RETVAL
635  
636 int
637 set(Duk* duk, const char* name, SV* value)
638 PREINIT:
639 30 ▶ duk_context* ctx = 0;
Branch: 1 branch
BranchTrueFalseTotal
duk_context* ctx = 0; 30 0 30
640 Stats stats;
641 CODE:
642 30 ctx = duk->ctx;
643 30 stats_start(aTHX_ duk, &stats);
644 30 RETVAL = set_global_or_property(aTHX_ ctx, name, value);
645 30 stats_stop(aTHX_ duk, &stats, "set");
646 OUTPUT: RETVAL
647  
648 SV*
649 eval(Duk* duk, const char* js, const char* file = 0)
650 PREINIT:
651 60 duk_context* ctx = 0;
652 Stats stats;
653 60 duk_uint_t flags = 0;
654 60 duk_int_t rc = 0;
655 CODE:
656 60 ctx = duk->ctx;
657  
658 /* flags |= DUK_COMPILE_STRICT; */
659  
660 60 stats_start(aTHX_ duk, &stats);
661 60 ▶ if (!file) {
Branch: 1 branch
BranchTrueFalseTotal
if (!file) { 56 4 60
662 56 rc = duk_pcompile_string(ctx, flags, js);
663 }
664 else {
665 4 duk_push_string(ctx, file);
666 4 rc = duk_pcompile_string_filename(ctx, flags, js);
667 }
668 60 stats_stop(aTHX_ duk, &stats, "compile");
669  
670 60 ▶ if (rc != DUK_EXEC_SUCCESS) {
Branch: 1 branch
BranchTrueFalseTotal
if (rc != DUK_EXEC_SUCCESS) { 0 60 60
671 0 croak("JS could not compile code: %s\n", duk_safe_to_string(ctx, -1));
672 }
673  
674 60 stats_start(aTHX_ duk, &stats);
675 60 rc = duk_pcall(ctx, 0);
676 60 stats_stop(aTHX_ duk, &stats, "run");
677 60 check_duktape_call_for_errors(rc, ctx);
678  
679 60 RETVAL = duk_to_perl(aTHX_ ctx, -1);
680 60 duk_pop(ctx);
681 OUTPUT: RETVAL
682  
683 SV*
684 dispatch_function_in_event_loop(Duk* duk, const char* func)
685 PREINIT:
686 Stats stats;
687 CODE:
688 4 stats_start(aTHX_ duk, &stats);
689 4 RETVAL = newSViv(run_function_in_event_loop(duk, func));
690 4 stats_stop(aTHX_ duk, &stats, "dispatch");
691 OUTPUT: RETVAL
« util.c ↑ Summary duk_console.c »