forked from gonzus/JavaScript-V8-XS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pl_inlined.cc
142 lines (136 loc) · 5.11 KB
/
pl_inlined.cc
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#include "pl_eval.h"
#include "pl_inlined.h"
#include "ppport.h"
typedef int (*BeforeCB)(V8Context* ctx);
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"
},
{
"log_cyclic.js",
"function JSON_stringify_with_cycles(obj) {\n"
" function getCircularReplacer() {\n"
" const seen = new WeakMap();\n"
" let count = 0;\n"
" return (key, value) => {\n"
" if (typeof value === \"object\" && value !== null) {\n"
" if (seen.has(value)) {\n"
" return \"<cycle\" + seen.get(value) + \">\";\n"
" }\n"
" seen.set(value, count++);\n"
" }\n"
" return value;\n"
" };\n"
" };\n"
"\n"
" return JSON.stringify(obj, getCircularReplacer());\n"
"};\n"
"\n"
},
};
void pl_register_inlined_functions(V8Context* ctx)
{
size_t j = 0;
dTHX;
for (j = 0; j < sizeof(js_inlined) / sizeof(js_inlined[0]); ++j) {
pl_eval(aTHX_ ctx, js_inlined[j].source, js_inlined[j].file_name);
}
}