pl_native.c

filter: Statementstmt 94.4% 17 / 18 Branchbran 75.0% 3 / 4 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 90.9% 20 / 22 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« pl_console.c ↑ Summary blib/lib/JavaScript/Duktape/XS.pm »
1 #include "pl_util.h"
2 #include "pl_native.h"
3  
4 /*
5 * Native print callable from JS
6 */
7 4 static duk_ret_t native_print(duk_context* ctx)
8 {
9 4 duk_push_lstring(ctx, " ", 1);
10 4 duk_insert(ctx, 0);
11 4 duk_join(ctx, duk_get_top(ctx) - 1);
12 4 PerlIO_stdoutf("%s\n", duk_safe_to_string(ctx, -1));
13 4 return 0; /* no return value */
14 }
15  
16 /*
17 * Get JS compatible 'now' timestamp (millisecs since 1970).
18 */
19 1 static duk_ret_t native_now_ms(duk_context* ctx)
20 {
21 1 duk_push_number(ctx, (duk_double_t) (now_us() / 1000.0));
22 1 return 1; /* return value at top */
23 }
24  
25 373 int pl_register_native_functions(Duk* duk)
26 {
27 static struct Data {
28 const char* name;
29 duk_c_function func;
30 } data[] = {
31 { "print" , native_print },
32 { "timestamp_ms", native_now_ms },
33 };
34 373 duk_context* ctx = duk->ctx;
35 373 int n = sizeof(data) / sizeof(data[0]);
36 373 int j = 0;
37 1119 ▶ for (j = 0; j < n; ++j) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < n; ++j) { 746 373 1119
38 746 duk_push_c_function(ctx, data[j].func, DUK_VARARGS);
39 746 ▶ if (!duk_put_global_string(ctx, data[j].name)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!duk_put_global_string(ctx, data[j].name)) { 0 746 746
40 0 croak("Could not register native function %s\n", data[j].name);
41 }
42 }
43 373 return n;
44 }
« pl_console.c ↑ Summary blib/lib/JavaScript/Duktape/XS.pm »