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