File Coverage

pl_inlined.c
Criterion Covered Total %
statement 5 5 100.0
branch 2 2 100.0
condition n/a
subroutine n/a
pod n/a
total 7 7 100.0


line stmt bran cond sub pod time code
1             #include "pl_util.h"
2             #include "pl_inlined.h"
3             #include "ppport.h"
4              
5             static struct {
6             const char* file_name;
7             const char* source;
8             } js_inlined[] = {
9             {
10             "c_eventloop.js", /* on my mac, this eval takes 574.697134 us avg */
11              
12             "/*\n"
13             " * C eventloop example (c_eventloop.c).\n"
14             " *\n"
15             " * Ecmascript code to initialize the exposed API (setTimeout() etc) when\n"
16             " * using the C eventloop.\n"
17             " *\n"
18             " * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Timers\n"
19             " */\n"
20             "\n"
21             "/*\n"
22             " * Timer API\n"
23             " */\n"
24             "\n"
25             "function setTimeout(func, delay) {\n"
26             " var cb_func;\n"
27             " var bind_args;\n"
28             " var timer_id;\n"
29             "\n"
30             " // Delay can be optional at least in some contexts, so tolerate that.\n"
31             " // https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/setTimeout\n"
32             " if (typeof delay !== 'number') {\n"
33             " if (typeof delay === 'undefined') {\n"
34             " delay = 0;\n"
35             " } else {\n"
36             " throw new TypeError('invalid delay');\n"
37             " }\n"
38             " }\n"
39             "\n"
40             " if (typeof func === 'string') {\n"
41             " // Legacy case: callback is a string.\n"
42             " cb_func = eval.bind(this, func);\n"
43             " } else if (typeof func !== 'function') {\n"
44             " throw new TypeError('callback is not a function/string');\n"
45             " } else if (arguments.length > 2) {\n"
46             " // Special case: callback arguments are provided.\n"
47             " bind_args = Array.prototype.slice.call(arguments, 2); // [ arg1, arg2, ... ]\n"
48             " bind_args.unshift(this); // [ global(this), arg1, arg2, ... ]\n"
49             " cb_func = func.bind.apply(func, bind_args);\n"
50             " } else {\n"
51             " // Normal case: callback given as a function without arguments.\n"
52             " cb_func = func;\n"
53             " }\n"
54             "\n"
55             " timer_id = EventLoop.createTimer(cb_func, delay, true /*oneshot*/);\n"
56             "\n"
57             " return timer_id;\n"
58             "}\n"
59             "\n"
60             "function clearTimeout(timer_id) {\n"
61             " if (typeof timer_id !== 'number') {\n"
62             " throw new TypeError('timer ID is not a number');\n"
63             " }\n"
64             " var success = EventLoop.deleteTimer(timer_id); /* retval ignored */\n"
65             "}\n"
66             "\n"
67             "function setInterval(func, delay) {\n"
68             " var cb_func;\n"
69             " var bind_args;\n"
70             " var timer_id;\n"
71             "\n"
72             " if (typeof delay !== 'number') {\n"
73             " if (typeof delay === 'undefined') {\n"
74             " delay = 0;\n"
75             " } else {\n"
76             " throw new TypeError('invalid delay');\n"
77             " }\n"
78             " }\n"
79             "\n"
80             " if (typeof func === 'string') {\n"
81             " // Legacy case: callback is a string.\n"
82             " cb_func = eval.bind(this, func);\n"
83             " } else if (typeof func !== 'function') {\n"
84             " throw new TypeError('callback is not a function/string');\n"
85             " } else if (arguments.length > 2) {\n"
86             " // Special case: callback arguments are provided.\n"
87             " bind_args = Array.prototype.slice.call(arguments, 2); // [ arg1, arg2, ... ]\n"
88             " bind_args.unshift(this); // [ global(this), arg1, arg2, ... ]\n"
89             " cb_func = func.bind.apply(func, bind_args);\n"
90             " } else {\n"
91             " // Normal case: callback given as a function without arguments.\n"
92             " cb_func = func;\n"
93             " }\n"
94             "\n"
95             " timer_id = EventLoop.createTimer(cb_func, delay, false /*oneshot*/);\n"
96             "\n"
97             " return timer_id;\n"
98             "}\n"
99             "\n"
100             "function clearInterval(timer_id) {\n"
101             " if (typeof timer_id !== 'number') {\n"
102             " throw new TypeError('timer ID is not a number');\n"
103             " }\n"
104             " EventLoop.deleteTimer(timer_id);\n"
105             "}\n"
106             "\n"
107             },
108             };
109              
110 262           void pl_register_inlined_functions(Duk* duk)
111             {
112 262           size_t j = 0;
113             dTHX;
114 524 100         for (j = 0; j < sizeof(js_inlined) / sizeof(js_inlined[0]); ++j) {
115 262           pl_eval(aTHX_ duk, js_inlined[j].source, js_inlined[j].file_name);
116             }
117 262           }