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