line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
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
|
262
|
|
|
|
|
|
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
|
262
|
|
|
|
|
|
duk_context* ctx = duk->ctx; |
35
|
262
|
|
|
|
|
|
int n = sizeof(data) / sizeof(data[0]); |
36
|
262
|
|
|
|
|
|
int j = 0; |
37
|
786
|
100
|
|
|
|
|
for (j = 0; j < n; ++j) { |
38
|
524
|
|
|
|
|
|
duk_push_c_function(ctx, data[j].func, DUK_VARARGS); |
39
|
524
|
50
|
|
|
|
|
if (!duk_put_global_string(ctx, data[j].name)) { |
40
|
0
|
|
|
|
|
|
croak("Could not register native function %s\n", data[j].name); |
41
|
|
|
|
|
|
|
} |
42
|
|
|
|
|
|
|
} |
43
|
262
|
|
|
|
|
|
return n; |
44
|
|
|
|
|
|
|
} |