duk.xs

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