visaljs 0.0.8 → 0.0.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +6 -3
- package/THIRD_PARTY_LICENSES +1 -1
- package/cli.js +2782 -770
- package/node/include/js_node.h +1 -0
- package/node/prelude/_http_agent.js +5 -0
- package/node/prelude/_http_server.js +7 -0
- package/node/prelude/dns.js +7 -1
- package/node/prelude/events.js +7 -3
- package/node/prelude/http.js +693 -285
- package/node/prelude/https.js +14 -3
- package/node/prelude/internal/events_extras.js +2 -5
- package/node/prelude/internal/features.js +17 -0
- package/node/prelude/internal/http_agent.js +88 -2
- package/node/prelude/internal/http_chars.ts +22 -0
- package/node/prelude/internal/http_symbols.js +3 -0
- package/node/prelude/internal/net_bound.js +123 -0
- package/node/prelude/internal/net_onread.js +73 -0
- package/node/prelude/internal/net_tos.js +54 -0
- package/node/prelude/internal/perf_observe.js +7 -1
- package/node/prelude/internal/tls/wrap.js +2 -1
- package/node/prelude/module.js +42 -18
- package/node/prelude/net.js +212 -61
- package/node/prelude/perf_hooks.js +4 -0
- package/node/src/host_http.c +151 -21
- package/node/src/host_net.c +75 -9
- package/node/src/host_sys.c +9 -0
- package/package.json +1 -1
- package/prebuilt/win32-x64/boringssl-ce573858472b7fc9.a +0 -0
- package/prebuilt/win32-x64/brotli-20d86a61f30baeb3.a +0 -0
- package/prebuilt/win32-x64/uv-b11bae65d7fced82.a +0 -0
- package/prebuilt/win32-x64/zlib-4ce2317a4c3f5fb5.a +0 -0
- package/prebuilt/win32-x64/zstd-015315ac7c784c73.a +0 -0
- package/runtime/include/js_atoms.h +4 -0
- package/runtime/include/js_buffer.h +185 -0
- package/runtime/include/js_builtins.h +53 -0
- package/runtime/include/js_exception.h +3 -0
- package/runtime/include/js_function.h +1 -1
- package/runtime/include/js_object.h +21 -3
- package/runtime/include/js_ops.h +27 -0
- package/runtime/include/js_runtime.h +1 -0
- package/runtime/include/js_web.h +3 -3
- package/runtime/include/jscontext.h +15 -0
- package/runtime/src/atoms.c +37 -3
- package/runtime/src/builtins.c +17 -4
- package/runtime/src/events.c +19 -0
- package/runtime/src/function.c +37 -10
- package/runtime/src/gc.c +7 -5
- package/runtime/src/internal.h +4 -4
- package/runtime/src/iterator.c +5 -1
- package/runtime/src/object.c +42 -6
- package/runtime/src/regexp.c +569 -12
- package/runtime/src/shape.c +68 -1
- package/runtime/src/typedarray.c +23 -0
- package/runtime/src/weak.c +95 -24
|
@@ -10,7 +10,9 @@
|
|
|
10
10
|
#include <stdint.h>
|
|
11
11
|
#include <string.h>
|
|
12
12
|
|
|
13
|
+
#include "js_convert.h"
|
|
13
14
|
#include "js_object.h"
|
|
15
|
+
#include "js_ops.h"
|
|
14
16
|
|
|
15
17
|
#define JS_BUFFER_RESIZABLE 0x01u
|
|
16
18
|
#define JS_BUFFER_DETACHED 0x02u
|
|
@@ -76,4 +78,187 @@ int js_typed_array_type(JSValue v);
|
|
|
76
78
|
/* A new, zeroed Uint8Array of `length` bytes; its bytes are js_buffer_source_bytes'. */
|
|
77
79
|
JS_WARN_UNUSED JSValue js_new_uint8_array(JSContext *ctx, size_t length);
|
|
78
80
|
|
|
81
|
+
/* ---- Generated code: a typed array's elements inline ------------------------------------------ */
|
|
82
|
+
|
|
83
|
+
/* The bytes of element i of a typed array generated code may read or write inline: a view of a
|
|
84
|
+
* fixed-size buffer (it can't shrink below the view, so i < length is in bounds), not detached,
|
|
85
|
+
* not immutable (for a write). NULL for anything else, which takes js_get_elem / js_set_elem. */
|
|
86
|
+
static JS_FAST_PATH uint8_t *js_ta_inline_elem(JSContext *ctx, JSValue obj, int32_t i, bool write) {
|
|
87
|
+
if (!js_is_object(obj) || js_as_object(obj)->shape->cls != ctx->rt->typed_array_class) return NULL;
|
|
88
|
+
const JSTypedArray *ta = (const JSTypedArray *)js_as_object(obj);
|
|
89
|
+
const JSArrayBuffer *b = js_as_array_buffer(ta->buffer);
|
|
90
|
+
uint32_t bad = JS_BUFFER_DETACHED | JS_BUFFER_RESIZABLE | (write ? JS_BUFFER_IMMUTABLE : 0);
|
|
91
|
+
if (ta->length_tracking || i < 0 || (size_t)i >= ta->length || (b->flags & bad)) return NULL;
|
|
92
|
+
return b->data + ta->byte_offset;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/* obj[i]: a dense array's element or a typed array's (Int8, Uint8, Int32, Float64) inline. */
|
|
96
|
+
static JS_FAST_PATH JSValue js_get_index_ta(JSContext *ctx, JSValue obj, JSValue key) {
|
|
97
|
+
if (JS_LIKELY(js_is_int32(key)) && !js_is_array(obj)) {
|
|
98
|
+
int32_t i = js_as_int32(key);
|
|
99
|
+
const uint8_t *p = js_ta_inline_elem(ctx, obj, i, false);
|
|
100
|
+
if (p) switch (((const JSTypedArray *)js_as_object(obj))->type) {
|
|
101
|
+
case JS_TA_UINT8: case JS_TA_UINT8C: return js_int32(p[i]);
|
|
102
|
+
case JS_TA_INT8: return js_int32((int8_t)p[i]);
|
|
103
|
+
case JS_TA_INT32: { int32_t v; memcpy(&v, p + 4 * (size_t)i, 4); return js_int32(v); }
|
|
104
|
+
case JS_TA_FLOAT64: { double v; memcpy(&v, p + 8 * (size_t)i, 8); return js_number(v); }
|
|
105
|
+
default: break;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
return js_get_index(ctx, obj, key);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/* obj[i] = v: the same, for a Number v (no ToNumber code to run). */
|
|
112
|
+
static JS_FAST_PATH int js_set_index_ta(JSContext *ctx, JSValue obj, JSValue key, JSValue v, bool strict) {
|
|
113
|
+
if (JS_LIKELY(js_is_int32(key)) && js_is_number(v) && !js_is_array(obj)) {
|
|
114
|
+
int32_t i = js_as_int32(key);
|
|
115
|
+
uint8_t *p = js_ta_inline_elem(ctx, obj, i, true);
|
|
116
|
+
if (p) switch (((const JSTypedArray *)js_as_object(obj))->type) {
|
|
117
|
+
case JS_TA_UINT8: case JS_TA_INT8: p[i] = (uint8_t)(js_is_int32(v) ? js_as_int32(v) : js_double_to_int32(js_as_number(v))); return 1;
|
|
118
|
+
case JS_TA_INT32: { int32_t x = js_is_int32(v) ? js_as_int32(v) : js_double_to_int32(js_as_number(v)); memcpy(p + 4 * (size_t)i, &x, 4); return 1; }
|
|
119
|
+
case JS_TA_FLOAT64: { double x = js_as_number(v); memcpy(p + 8 * (size_t)i, &x, 8); return 1; }
|
|
120
|
+
default: break;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
return js_set_index(ctx, obj, key, v, strict);
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/* The same with a double key (an unboxed index): an integral one is the int32 key (-0 too: "0"). */
|
|
127
|
+
static inline JSValue js_get_index_ta_f64(JSContext *ctx, JSValue obj, double key) {
|
|
128
|
+
int32_t i = (int32_t)key;
|
|
129
|
+
if (key >= -2147483648.0 && key <= 2147483647.0 && (double)i == key) return js_get_index_ta(ctx, obj, js_int32(i));
|
|
130
|
+
return js_get_index_f64(ctx, obj, key);
|
|
131
|
+
}
|
|
132
|
+
static inline int js_set_index_ta_f64(JSContext *ctx, JSValue obj, double key, JSValue v, bool strict) {
|
|
133
|
+
int32_t i = (int32_t)key;
|
|
134
|
+
if (key >= -2147483648.0 && key <= 2147483647.0 && (double)i == key) return js_set_index_ta(ctx, obj, js_int32(i), v, strict);
|
|
135
|
+
return js_set_index_f64(ctx, obj, key, v, strict);
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/* A typed array's elements as generated code holds them (the typed-arrays pass): taken once,
|
|
139
|
+
* for an array nothing else can reach, so nothing detaches or resizes its buffer. Empty (length
|
|
140
|
+
* 0) when the value isn't a typed array of that type: every access then takes the generic path. */
|
|
141
|
+
typedef struct JSTAView {
|
|
142
|
+
uint8_t *base;
|
|
143
|
+
size_t length;
|
|
144
|
+
int type; /* the element type it's for: a slow path that may have run code takes it again */
|
|
145
|
+
} JSTAView;
|
|
146
|
+
|
|
147
|
+
static inline JSTAView js_ta_view(JSContext *ctx, JSValue obj, int type) {
|
|
148
|
+
JSTAView v = { NULL, 0, type };
|
|
149
|
+
if (!js_is_object(obj) || js_as_object(obj)->shape->cls != ctx->rt->typed_array_class) return v;
|
|
150
|
+
const JSTypedArray *ta = (const JSTypedArray *)js_as_object(obj);
|
|
151
|
+
const JSArrayBuffer *b = js_as_array_buffer(ta->buffer);
|
|
152
|
+
if (ta->type != type || ta->length_tracking || (b->flags & (JS_BUFFER_DETACHED | JS_BUFFER_RESIZABLE | JS_BUFFER_IMMUTABLE)) || !b->data) return v;
|
|
153
|
+
v.base = b->data + ta->byte_offset;
|
|
154
|
+
v.length = ta->length;
|
|
155
|
+
return v;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/* Whether v is a typed array of that element type (a typed function's typed array parameter). */
|
|
159
|
+
static inline bool js_ta_is(JSContext *ctx, JSValue v, int type) {
|
|
160
|
+
return js_is_object(v) && js_as_object(v)->shape->cls == ctx->rt->typed_array_class && ((const JSTypedArray *)js_as_object(v))->type == type;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/* The index a key names when it's in the view, else false. */
|
|
164
|
+
static JS_FAST_PATH bool js_tav_index(const JSTAView *v, JSValue key, size_t *i) {
|
|
165
|
+
if (!js_is_int32(key) || js_as_int32(key) < 0 || (size_t)js_as_int32(key) >= v->length) return false;
|
|
166
|
+
*i = (size_t)js_as_int32(key);
|
|
167
|
+
return true;
|
|
168
|
+
}
|
|
169
|
+
static JS_FAST_PATH bool js_tav_index_d(const JSTAView *v, double key, size_t *i) {
|
|
170
|
+
if (!(key >= 0 && key < (double)v->length) || (double)(size_t)key != key) return false;
|
|
171
|
+
*i = (size_t)key;
|
|
172
|
+
return true;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/* Each element type's read and write (a Number, converted as the spec's NumericToRawBytes). */
|
|
176
|
+
#define JS_TAV_ACCESS(name, ctype, box, unbox) \
|
|
177
|
+
static JS_FAST_PATH JSValue js_tav_get_##name(JSContext *ctx, JSTAView *v, JSValue obj, JSValue key) { \
|
|
178
|
+
size_t i; \
|
|
179
|
+
if (JS_LIKELY(js_tav_index(v, key, &i))) { ctype x; memcpy(&x, v->base + i * sizeof x, sizeof x); return box(x); } \
|
|
180
|
+
JSValue r = js_get_elem(ctx, obj, key); \
|
|
181
|
+
*v = js_ta_view(ctx, obj, v->type); /* a getter may have run */ \
|
|
182
|
+
return r; \
|
|
183
|
+
} \
|
|
184
|
+
static JS_FAST_PATH JSValue js_tav_get_##name##_d(JSContext *ctx, JSTAView *v, JSValue obj, double key) { \
|
|
185
|
+
size_t i; \
|
|
186
|
+
if (JS_LIKELY(js_tav_index_d(v, key, &i))) { ctype x; memcpy(&x, v->base + i * sizeof x, sizeof x); return box(x); } \
|
|
187
|
+
JSValue r = js_get_elem(ctx, obj, js_number(key)); \
|
|
188
|
+
*v = js_ta_view(ctx, obj, v->type); \
|
|
189
|
+
return r; \
|
|
190
|
+
} \
|
|
191
|
+
static JS_FAST_PATH JSValue js_tav_get_##name##_i(JSContext *ctx, JSTAView *v, JSValue obj, int32_t key) { \
|
|
192
|
+
if (JS_LIKELY((uint32_t)key < v->length)) { ctype x; memcpy(&x, v->base + (size_t)(uint32_t)key * sizeof x, sizeof x); return box(x); } \
|
|
193
|
+
JSValue r = js_get_elem(ctx, obj, js_int32(key)); \
|
|
194
|
+
*v = js_ta_view(ctx, obj, v->type); \
|
|
195
|
+
return r; \
|
|
196
|
+
} \
|
|
197
|
+
static JS_FAST_PATH int js_tav_set_##name##_i(JSContext *ctx, JSTAView *v, JSValue obj, int32_t key, JSValue val, bool strict) { \
|
|
198
|
+
if (JS_LIKELY(js_is_number(val) && (uint32_t)key < v->length)) { ctype x = unbox(val); memcpy(v->base + (size_t)(uint32_t)key * sizeof x, &x, sizeof x); return 1; } \
|
|
199
|
+
int r = js_set_elem(ctx, obj, js_int32(key), val, strict); \
|
|
200
|
+
*v = js_ta_view(ctx, obj, v->type); \
|
|
201
|
+
return r; \
|
|
202
|
+
} \
|
|
203
|
+
static JS_FAST_PATH JSValue js_tav_get_##name##_l(JSContext *ctx, JSTAView *v, JSValue obj, int64_t key) { \
|
|
204
|
+
if (JS_LIKELY((uint64_t)key < v->length)) { ctype x; memcpy(&x, v->base + (size_t)key * sizeof x, sizeof x); return box(x); } \
|
|
205
|
+
JSValue r = js_get_elem(ctx, obj, js_number((double)key)); \
|
|
206
|
+
*v = js_ta_view(ctx, obj, v->type); \
|
|
207
|
+
return r; \
|
|
208
|
+
} \
|
|
209
|
+
static JS_FAST_PATH int js_tav_set_##name##_l(JSContext *ctx, JSTAView *v, JSValue obj, int64_t key, JSValue val, bool strict) { \
|
|
210
|
+
if (JS_LIKELY(js_is_number(val) && (uint64_t)key < v->length)) { ctype x = unbox(val); memcpy(v->base + (size_t)key * sizeof x, &x, sizeof x); return 1; } \
|
|
211
|
+
int r = js_set_elem(ctx, obj, js_number((double)key), val, strict); \
|
|
212
|
+
*v = js_ta_view(ctx, obj, v->type); \
|
|
213
|
+
return r; \
|
|
214
|
+
} \
|
|
215
|
+
static JS_FAST_PATH int js_tav_set_##name(JSContext *ctx, JSTAView *v, JSValue obj, JSValue key, JSValue val, bool strict) { \
|
|
216
|
+
size_t i; \
|
|
217
|
+
if (JS_LIKELY(js_is_number(val) && js_tav_index(v, key, &i))) { ctype x = unbox(val); memcpy(v->base + i * sizeof x, &x, sizeof x); return 1; } \
|
|
218
|
+
int r = js_set_elem(ctx, obj, key, val, strict); /* valueOf, a setter: they may have run */ \
|
|
219
|
+
*v = js_ta_view(ctx, obj, v->type); \
|
|
220
|
+
return r; \
|
|
221
|
+
} \
|
|
222
|
+
static JS_FAST_PATH int js_tav_set_##name##_d(JSContext *ctx, JSTAView *v, JSValue obj, double key, JSValue val, bool strict) { \
|
|
223
|
+
size_t i; \
|
|
224
|
+
if (JS_LIKELY(js_is_number(val) && js_tav_index_d(v, key, &i))) { ctype x = unbox(val); memcpy(v->base + i * sizeof x, &x, sizeof x); return 1; } \
|
|
225
|
+
int r = js_set_elem(ctx, obj, js_number(key), val, strict); \
|
|
226
|
+
*v = js_ta_view(ctx, obj, v->type); \
|
|
227
|
+
return r; \
|
|
228
|
+
}
|
|
229
|
+
static JS_FAST_PATH int32_t js_tav_to_int32(JSValue v) { return js_is_int32(v) ? js_as_int32(v) : js_double_to_int32(js_as_number(v)); }
|
|
230
|
+
#define JS_TAV_INT(x) js_int32((int32_t)(x))
|
|
231
|
+
JS_TAV_ACCESS(i8, int8_t, JS_TAV_INT, (int8_t)js_tav_to_int32)
|
|
232
|
+
JS_TAV_ACCESS(u8, uint8_t, JS_TAV_INT, (uint8_t)js_tav_to_int32)
|
|
233
|
+
JS_TAV_ACCESS(i32, int32_t, JS_TAV_INT, js_tav_to_int32)
|
|
234
|
+
JS_TAV_ACCESS(f64, double, js_number, js_as_number)
|
|
235
|
+
|
|
236
|
+
/* A Float64Array's element as a double, for typed code ("use static": its type is number):
|
|
237
|
+
* past the end, undefined as a number (NaN). False with an exception pending. And a store of a
|
|
238
|
+
* double, boxed only on the slow path. */
|
|
239
|
+
static inline bool js_tav_num_slow(JSContext *ctx, JSTAView *v, JSValue obj, JSValue key, double *out) {
|
|
240
|
+
JSValue r = js_get_elem(ctx, obj, key);
|
|
241
|
+
*v = js_ta_view(ctx, obj, v->type);
|
|
242
|
+
if (js_is_exception(r)) return false;
|
|
243
|
+
if (js_is_number(r)) { *out = js_as_number(r); return true; }
|
|
244
|
+
return js_to_number(ctx, out, r) == 0;
|
|
245
|
+
}
|
|
246
|
+
#define JS_TAV_F64(suffix, ktype, in_view, boxed_key) \
|
|
247
|
+
static JS_FAST_PATH bool js_tav_num_f64##suffix(JSContext *ctx, JSTAView *v, JSValue obj, ktype key, double *out) { \
|
|
248
|
+
size_t i; \
|
|
249
|
+
if (JS_LIKELY(in_view)) { memcpy(out, v->base + i * 8, 8); return true; } \
|
|
250
|
+
return js_tav_num_slow(ctx, v, obj, boxed_key, out); \
|
|
251
|
+
} \
|
|
252
|
+
static JS_FAST_PATH int js_tav_put_f64##suffix(JSContext *ctx, JSTAView *v, JSValue obj, ktype key, double x, bool strict) { \
|
|
253
|
+
size_t i; \
|
|
254
|
+
if (JS_LIKELY(in_view)) { memcpy(v->base + i * 8, &x, 8); return 1; } \
|
|
255
|
+
int r = js_set_elem(ctx, obj, boxed_key, js_number(x), strict); \
|
|
256
|
+
*v = js_ta_view(ctx, obj, v->type); \
|
|
257
|
+
return r; \
|
|
258
|
+
}
|
|
259
|
+
JS_TAV_F64(, JSValue, js_tav_index(v, key, &i), key)
|
|
260
|
+
JS_TAV_F64(_d, double, js_tav_index_d(v, key, &i), js_number(key))
|
|
261
|
+
JS_TAV_F64(_i, int32_t, ((i = (size_t)(uint32_t)key) < v->length), js_int32(key))
|
|
262
|
+
JS_TAV_F64(_l, int64_t, ((i = (size_t)(uint64_t)key) < v->length), js_number((double)key))
|
|
263
|
+
|
|
79
264
|
#endif /* JS_BUFFER_H */
|
|
@@ -211,6 +211,59 @@ static inline JSValue js_call_char_code_at(JSContext *ctx, JSValue f, JSValue s,
|
|
|
211
211
|
return js_call_site(ctx, f, s, 1, &i, site);
|
|
212
212
|
}
|
|
213
213
|
|
|
214
|
+
/* s.charCodeAt(i) in typed code ("use static"), which trusts the built-ins to be the standard
|
|
215
|
+
* ones (char_code_at): s is a string; its unit read inline, else (a rope, an index out of
|
|
216
|
+
* range) the built-in's answer. */
|
|
217
|
+
static inline double js_string_char_code_at(JSContext *ctx, JSValue s, double i) {
|
|
218
|
+
JSStrView v;
|
|
219
|
+
if (JS_LIKELY(i >= 0 && i < 4294967295.0 && js_string_units(s, &v) && (uint32_t)i < v.length && (double)(uint32_t)i == i))
|
|
220
|
+
return (double)js_view_at(v, (uint32_t)i);
|
|
221
|
+
JSValue index = js_number(i);
|
|
222
|
+
return js_as_number(js_core_string_proto_char_code_at(ctx, JS_UNDEFINED, s, 1, &index, JS_UNDEFINED));
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
/* s.charCodeAt(i) in typed code for an index the optimizer proved in s: the unit, an int32 (a
|
|
226
|
+
* rope not yet flattened takes the built-in). */
|
|
227
|
+
static inline int32_t js_string_unit_at(JSContext *ctx, JSValue s, uint32_t i) {
|
|
228
|
+
JSStrView v;
|
|
229
|
+
if (JS_LIKELY(js_string_units(s, &v))) return (int32_t)js_view_at(v, i);
|
|
230
|
+
return (int32_t)js_string_char_code_at(ctx, s, (double)i);
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
/* Function.prototype.apply and call: generated code tells them by their entry. */
|
|
234
|
+
JSValue js_function_proto_apply(JSContext *ctx, JSValue callee, JSValue this_val, uint32_t argc, const JSValue *argv, JSValue new_target);
|
|
235
|
+
JSValue js_function_proto_call(JSContext *ctx, JSValue callee, JSValue this_val, uint32_t argc, const JSValue *argv, JSValue new_target);
|
|
236
|
+
|
|
237
|
+
/* f.apply(t, args), `m` the value of f.apply: when m is the built-in and f callable, f is called
|
|
238
|
+
* without the built-in in between, with a short dense array's elements copied to the stack (its
|
|
239
|
+
* elements are its own data properties: reading them runs no code; copied, as f may change the
|
|
240
|
+
* array). Anything else is the call of m. */
|
|
241
|
+
static inline JSValue js_call_apply(JSContext *ctx, JSValue m, JSValue f, JSValue t, JSValue args, const char *site) {
|
|
242
|
+
if (JS_LIKELY(js_is_function(m) && js_as_function(m)->entry == js_function_proto_apply && js_is_callable(f))) {
|
|
243
|
+
if (js_is_nullish(args)) return js_call(ctx, f, t, 0, NULL);
|
|
244
|
+
if (js_is_array(args)) {
|
|
245
|
+
JSObject *a = js_as_object(args);
|
|
246
|
+
JSElements *e = a->elements;
|
|
247
|
+
uint32_t n = js_array_length(a);
|
|
248
|
+
JSValue copy[8];
|
|
249
|
+
if (e->kind == JS_ELEMENTS_DENSE && e->length == n && n <= 8) {
|
|
250
|
+
uint32_t i = 0;
|
|
251
|
+
while (i < n && !js_is_empty(e->data[i])) copy[i] = e->data[i], i++;
|
|
252
|
+
if (i == n) return js_call(ctx, f, t, n, copy);
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
return js_call_site(ctx, m, f, 2, (const JSValue[]){ t, args }, site);
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
/* f.call(t, …), `m` the value of f.call: f called directly when m is the built-in and f callable.
|
|
260
|
+
* argv: t, then the arguments. */
|
|
261
|
+
static inline JSValue js_call_call(JSContext *ctx, JSValue m, JSValue f, uint32_t argc, const JSValue *argv, const char *site) {
|
|
262
|
+
if (JS_LIKELY(js_is_function(m) && js_as_function(m)->entry == js_function_proto_call && js_is_callable(f)))
|
|
263
|
+
return js_call(ctx, f, argc ? argv[0] : JS_UNDEFINED, argc ? argc - 1 : 0, argc ? argv + 1 : NULL);
|
|
264
|
+
return js_call_site(ctx, m, f, argc, argv, site);
|
|
265
|
+
}
|
|
266
|
+
|
|
214
267
|
/* String.fromCharCode(c): a unit up to 0xFF is a static one-unit string. */
|
|
215
268
|
static inline JSValue js_call_from_char_code(JSContext *ctx, JSValue f, JSValue this_val, JSValue c, const char *site) {
|
|
216
269
|
if (JS_LIKELY(js_is_function(f) && js_as_function(f)->entry == js_core_string_from_char_code && js_is_int32(c)
|
|
@@ -28,6 +28,9 @@ JS_WARN_UNUSED JSValue js_throw_reference_error(JSContext *ctx, const char *fmt,
|
|
|
28
28
|
JS_WARN_UNUSED JSValue js_throw_syntax_error(JSContext *ctx, const char *fmt, ...);
|
|
29
29
|
JS_WARN_UNUSED JSValue js_throw_uri_error(JSContext *ctx, const char *fmt, ...);
|
|
30
30
|
JS_WARN_UNUSED JSValue js_throw_stack_overflow(JSContext *ctx); /* RangeError: Maximum call stack size exceeded */
|
|
31
|
+
/* ERR_INVALID_ARG_TYPE: `The "dest" argument must be <expected>. Received …` (Node's wording
|
|
32
|
+
* of the value). Generated code calls it: a "use static" function's checked entry. */
|
|
33
|
+
JS_WARN_UNUSED JSValue js_throw_invalid_arg_type(JSContext *ctx, const char *name, const char *expected, JSValue value);
|
|
31
34
|
|
|
32
35
|
/* ---- Catching ---------------------------------------------------------- */
|
|
33
36
|
|
|
@@ -79,7 +79,7 @@ typedef struct JSFunction { /* kind = FUNCTION */
|
|
|
79
79
|
JSContext *realm; /* the realm the function was created in */
|
|
80
80
|
uint32_t flags;
|
|
81
81
|
uint32_t pad_;
|
|
82
|
-
struct JSShape *instance_shape; /*
|
|
82
|
+
struct JSShape *instance_shape; /* the root shape of the objects `new` makes with it as new.target: inline slots for all their constructors add */
|
|
83
83
|
} JSFunction;
|
|
84
84
|
|
|
85
85
|
extern const JSClass js_class_function;
|
|
@@ -296,10 +296,10 @@ JS_WARN_UNUSED JSValue js_own_keys(JSContext *ctx, JSObject *o);
|
|
|
296
296
|
#define JS_IC_EMPTY 0
|
|
297
297
|
#define JS_IC_OWN 1 /* get: an own data property; set: an own writable data property */
|
|
298
298
|
#define JS_IC_PROTO 2 /* get: a data property of `holder`, `depth` prototypes up */
|
|
299
|
-
#define JS_IC_ADD 3 /* set: adding the key takes the receiver from `shape` to `next` */
|
|
299
|
+
#define JS_IC_ADD 3 /* set: adding the key takes the receiver from `shape` to `next`; `depth` prototypes up to null, or to one whose writable data property it shadows */
|
|
300
300
|
#define JS_IC_GETTER 4 /* get: an accessor of `holder` (NULL: the receiver), `depth` prototypes up */
|
|
301
301
|
#define JS_IC_ABSENT 5 /* get: no such property on the receiver or its `depth` prototypes (the last's proto is null) */
|
|
302
|
-
#define JS_IC_MAX_DEPTH
|
|
302
|
+
#define JS_IC_MAX_DEPTH 8 /* prototypes a cache can check: Node's sockets are 6 deep (Socket, Duplex, Readable, Stream, EventEmitter, Object) */
|
|
303
303
|
|
|
304
304
|
/* One per property access site with a constant named key; a site is either a get or a
|
|
305
305
|
* set. Monomorphic: a miss refills it. Never caches dictionary shapes (their layout
|
|
@@ -314,7 +314,7 @@ typedef struct JSPropCache {
|
|
|
314
314
|
JSShape *fast; /* OWN, inline slot: the receiver shape; else NULL */
|
|
315
315
|
uint16_t fast_off; /* with `fast`: the slot's byte offset in the object */
|
|
316
316
|
uint8_t kind; /* JS_IC_* */
|
|
317
|
-
uint8_t depth; /* PROTO: hops to the holder; ADD: prototypes
|
|
317
|
+
uint8_t depth; /* PROTO: hops to the holder; ADD: prototypes checked (JS_IC_ADD) */
|
|
318
318
|
uint32_t slot; /* OWN, PROTO: the slot */
|
|
319
319
|
JSShape *shape; /* receiver shape; NULL = empty */
|
|
320
320
|
JSObject *holder; /* PROTO */
|
|
@@ -326,6 +326,8 @@ typedef struct JSPropCache {
|
|
|
326
326
|
/* A module's caches, once at startup (the GC resets them weakly). */
|
|
327
327
|
void js_register_prop_caches(JSRuntime *rt, JSPropCache *ics, size_t n);
|
|
328
328
|
|
|
329
|
+
/* The slow paths of a named access: `ic` is the site's cache, or NULL for a computed key's access
|
|
330
|
+
* (the runtime's shared caches only). */
|
|
329
331
|
JS_WARN_UNUSED JSValue js_get_prop_slow(JSContext *ctx, JSValue obj, JSValue key, JSPropCache *ic);
|
|
330
332
|
JS_WARN_UNUSED int js_set_prop_slow(JSContext *ctx, JSValue obj, JSValue key, JSValue v, bool strict, JSPropCache *ic);
|
|
331
333
|
|
|
@@ -373,6 +375,22 @@ static inline int js_set_prop(JSContext *ctx, JSValue obj, JSValue key, JSValue
|
|
|
373
375
|
return js_set_prop_slow(ctx, obj, key, v, strict, ic);
|
|
374
376
|
}
|
|
375
377
|
|
|
378
|
+
/* obj[key] and obj[key] = v at a site whose key is most likely always the same symbol or string
|
|
379
|
+
* (the optimizer's guess: a module binding stored once, as Node's kState). `site` holds the key
|
|
380
|
+
* the site's cache is for (EMPTY: none yet): while the key is that one, the access is a named
|
|
381
|
+
* access's, inline cache and all. Another key the slow path makes the site's, or, when it isn't
|
|
382
|
+
* a property key already (an index, a string not interned), accesses generically. */
|
|
383
|
+
JS_WARN_UNUSED JSValue js_get_keyed_slow(JSContext *ctx, JSValue obj, JSValue key, JSValue *site, JSPropCache *ic);
|
|
384
|
+
JS_WARN_UNUSED int js_set_keyed_slow(JSContext *ctx, JSValue obj, JSValue key, JSValue v, bool strict, JSValue *site, JSPropCache *ic);
|
|
385
|
+
static inline JSValue js_get_keyed(JSContext *ctx, JSValue obj, JSValue key, JSValue *site, JSPropCache *ic) {
|
|
386
|
+
if (JS_LIKELY(js_same_bits(key, *site))) return js_get_prop(ctx, obj, key, ic);
|
|
387
|
+
return js_get_keyed_slow(ctx, obj, key, site, ic);
|
|
388
|
+
}
|
|
389
|
+
static inline int js_set_keyed(JSContext *ctx, JSValue obj, JSValue key, JSValue v, bool strict, JSValue *site, JSPropCache *ic) {
|
|
390
|
+
if (JS_LIKELY(js_same_bits(key, *site))) return js_set_prop(ctx, obj, key, v, strict, ic);
|
|
391
|
+
return js_set_keyed_slow(ctx, obj, key, v, strict, site, ic);
|
|
392
|
+
}
|
|
393
|
+
|
|
376
394
|
/* An object's new shape. When the object is a prototype, caches that checked the chain it's on
|
|
377
395
|
* may be stale: the runtime's proto_epoch moves on. */
|
|
378
396
|
static JS_FAST_PATH void js_object_set_shape(JSContext *ctx, JSObject *o, JSShape *s) {
|
package/runtime/include/js_ops.h
CHANGED
|
@@ -46,6 +46,33 @@ JS_WARN_UNUSED JSValue js_ge(JSContext *ctx, JSValue a, JSValue b);
|
|
|
46
46
|
/* ===, !==: never throw (strings compare by content). */
|
|
47
47
|
bool js_strict_equals(JSContext *ctx, JSValue a, JSValue b);
|
|
48
48
|
static inline JSValue js_strict_eq(JSContext *ctx, JSValue a, JSValue b) { return js_bool(js_strict_equals(ctx, a, b)); }
|
|
49
|
+
/* A Number as a typed file's ("use static") int, an int64_t: truncated toward zero and wrapped
|
|
50
|
+
* modulo 2^64, as ToInt32 is for 32 bits; NaN and the infinities 0 (a plain C conversion of those
|
|
51
|
+
* would be undefined behaviour). */
|
|
52
|
+
static inline int64_t js_f64_to_i64(double d) {
|
|
53
|
+
if (d >= -9223372036854775808.0 && d < 9223372036854775808.0) return (int64_t)d;
|
|
54
|
+
if (d != d || d == INFINITY || d == -INFINITY) return 0;
|
|
55
|
+
double m = fmod(trunc(d), 18446744073709551616.0); /* exact: d is an integer this large */
|
|
56
|
+
if (m < 0) m += 18446744073709551616.0;
|
|
57
|
+
return (int64_t)(uint64_t)m;
|
|
58
|
+
}
|
|
59
|
+
/* A Number value as an int; anything else (typed code's caller passed it unchecked) 0. */
|
|
60
|
+
static inline int64_t js_value_to_i64(JSValue v) {
|
|
61
|
+
if (js_is_int32(v)) return js_as_int32(v);
|
|
62
|
+
return js_is_number(v) ? js_f64_to_i64(js_as_number(v)) : 0;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/* v === d, d a number literal (not NaN): v is a Number of that value (0 and -0 alike). */
|
|
66
|
+
static inline bool js_strict_eq_num(JSValue v, double d) {
|
|
67
|
+
if (js_is_int32(v)) return (double)js_as_int32(v) == d;
|
|
68
|
+
return js_is_number(v) && js_as_number(v) == d;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/* a === s, s a string literal: an atom, so a string with its units is s itself unless it isn't
|
|
72
|
+
* interned */
|
|
73
|
+
static inline bool js_strict_eq_atom(JSContext *ctx, JSValue a, JSValue s) {
|
|
74
|
+
return js_same_bits(a, s) || (js_is_string(a) && !js_is_atom(a) && js_string_equals(ctx, a, s));
|
|
75
|
+
}
|
|
49
76
|
static inline JSValue js_strict_ne(JSContext *ctx, JSValue a, JSValue b) { return js_bool(!js_strict_equals(ctx, a, b)); }
|
|
50
77
|
|
|
51
78
|
/* A switch whose cases are all string literals: the index of the first case `v` is === to, or
|
package/runtime/include/js_web.h
CHANGED
|
@@ -13,9 +13,6 @@
|
|
|
13
13
|
JS_WARN_UNUSED JSValue js_throw_node_error(JSContext *ctx, JSErrorKind kind, const char *code, const char *fmt, ...);
|
|
14
14
|
/* ERR_INVALID_THIS: `Value of "this" must be of type TextDecoder`. */
|
|
15
15
|
JS_WARN_UNUSED JSValue js_throw_invalid_this(JSContext *ctx, const char *type);
|
|
16
|
-
/* ERR_INVALID_ARG_TYPE: `The "dest" argument must be <expected>. Received …` (Node's wording
|
|
17
|
-
* of the value). */
|
|
18
|
-
JS_WARN_UNUSED JSValue js_throw_invalid_arg_type(JSContext *ctx, const char *name, const char *expected, JSValue value);
|
|
19
16
|
/* Node's "Received …" tail for a value: " Received type number (1)", " Received an instance of
|
|
20
17
|
* Array", " Received undefined". */
|
|
21
18
|
void js_node_received(JSContext *ctx, JSValue v, char *buf, size_t cap);
|
|
@@ -57,6 +54,9 @@ bool js_is_abort_signal(JSValue v);
|
|
|
57
54
|
void js_abort_signal_abort(JSContext *ctx, JSValue signal, JSValue reason);
|
|
58
55
|
/* A new signal that aborts when `source` does (AbortSignal.any([source])). */
|
|
59
56
|
JS_WARN_UNUSED JSValue js_abort_signal_follow(JSContext *ctx, JSValue source);
|
|
57
|
+
/* node:events' primitive: an EventTarget's listeners for a type, in the order added (getEventListeners,
|
|
58
|
+
* listenerCount); undefined for a value that isn't an EventTarget. */
|
|
59
|
+
JSValue js_event_target_listeners(JSContext *ctx, JSValue callee, JSValue this_val, uint32_t argc, const JSValue *argv, JSValue new_target);
|
|
60
60
|
|
|
61
61
|
/* headers.c (the headers component): Headers for Request and Response. */
|
|
62
62
|
typedef enum JSHeadersGuard { JS_HEADERS_NONE, JS_HEADERS_IMMUTABLE } JSHeadersGuard;
|
|
@@ -142,6 +142,11 @@ typedef struct JSShapeRegistry {
|
|
|
142
142
|
size_t root_cap, root_count;
|
|
143
143
|
struct JSShape **parents; /* shapes with transitions */
|
|
144
144
|
size_t parent_count, parent_cap;
|
|
145
|
+
/* Since the last collection: the root shapes and transitions' children made (the only shapes
|
|
146
|
+
* a young collection can find dead), and the shapes given transitions, parents[old_parents..]. */
|
|
147
|
+
struct JSShape **young;
|
|
148
|
+
size_t young_count, young_cap;
|
|
149
|
+
size_t old_parents;
|
|
145
150
|
} JSShapeRegistry;
|
|
146
151
|
|
|
147
152
|
/* Registered inline-cache arrays: reset weakly after marking. */
|
|
@@ -201,6 +206,9 @@ struct JSRuntime {
|
|
|
201
206
|
int (*on_handled_late)(JSContext *ctx, JSValue promise);
|
|
202
207
|
uint8_t failure; /* the exit status after an uncaught error: 1, or on_uncaught's */
|
|
203
208
|
struct JSWeakList { struct JSObject **items; size_t count, cap; } weak; /* WeakMap/Set/Ref, registries */
|
|
209
|
+
/* WeakMap and WeakSet entries set since the last collection, by key (EMPTY: every entry of the
|
|
210
|
+
* table): the only ones a young collection looks at (weak.c). */
|
|
211
|
+
struct JSWeakWrites { struct JSWeakWrite { struct JSObject *table; JSValue key; } *items; size_t count, cap; } weak_writes;
|
|
204
212
|
struct JSKeptAlive { JSValue *items; size_t count, cap; } kept_alive; /* [[KeptAlive]]: roots until ClearKeptObjects */
|
|
205
213
|
const struct JSComponent *const *components; /* installed in every realm */
|
|
206
214
|
size_t ncomponents;
|
|
@@ -218,6 +226,12 @@ struct JSRuntime {
|
|
|
218
226
|
const JSBigIntOps *bigint; /* set by the bigint component */
|
|
219
227
|
/* A typed array's length now: -1 when out of bounds (detached), -2 for anything else. */
|
|
220
228
|
double (*typed_array_length)(JSValue v);
|
|
229
|
+
/* Set by typed arrays: obj[i] and obj[i] = d (a Number) of a typed array, inline, for an index
|
|
230
|
+
* in bounds and an element that is a Number (not a BigInt): true when done; false for
|
|
231
|
+
* anything else, which takes the generic path. */
|
|
232
|
+
bool (*typed_array_get)(JSValue v, int32_t i, JSValue *out);
|
|
233
|
+
bool (*typed_array_set)(JSValue v, int32_t i, double d);
|
|
234
|
+
const struct JSClass *typed_array_class; /* set by typed arrays: js_buffer.h's inline element access tests it */
|
|
221
235
|
/* Set by typed arrays: a typed array's or DataView's bytes (its current window), true; false
|
|
222
236
|
* for anything else (an ArrayBuffer too: process.stdout.write takes views only). */
|
|
223
237
|
bool (*view_bytes)(JSValue v, uint8_t **data, size_t *length);
|
|
@@ -232,6 +246,7 @@ struct JSContext {
|
|
|
232
246
|
uint8_t barrier_threshold; /* js_store's slow path runs for an owner whose GC byte is this or more */
|
|
233
247
|
JSValue intrinsics[JS_INTRINSIC_COUNT];
|
|
234
248
|
JSContext *next_realm;
|
|
249
|
+
struct JSShape *array_shape; /* the root shape of its arrays with Array.prototype (js_new_array), made at the first */
|
|
235
250
|
};
|
|
236
251
|
|
|
237
252
|
JSRuntime *js_runtime_new(void);
|
package/runtime/src/atoms.c
CHANGED
|
@@ -63,6 +63,24 @@ static void insert(JSAtomTable *t, JSString *s, uint32_t hash) {
|
|
|
63
63
|
t->count++;
|
|
64
64
|
}
|
|
65
65
|
|
|
66
|
+
/* Removes `s`, which is in the table. Linear probing: later entries of its probe run move back
|
|
67
|
+
* into the hole, so that every lookup still finds them. */
|
|
68
|
+
static void remove_atom(JSAtomTable *t, const JSString *s) {
|
|
69
|
+
size_t mask = t->cap - 1, i = s->hash & mask;
|
|
70
|
+
while (t->entries[i].str != s) i = (i + 1) & mask;
|
|
71
|
+
t->entries[i].str = NULL;
|
|
72
|
+
t->count--;
|
|
73
|
+
for (size_t j = (i + 1) & mask; t->entries[j].str; j = (j + 1) & mask) {
|
|
74
|
+
size_t home = t->entries[j].hash & mask;
|
|
75
|
+
bool stays = i <= j ? (i < home && home <= j) : (i < home || home <= j);
|
|
76
|
+
if (!stays) {
|
|
77
|
+
t->entries[i] = t->entries[j]; /* barrier: none, the table is malloc'd and weak */
|
|
78
|
+
t->entries[j].str = NULL;
|
|
79
|
+
i = j;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
66
84
|
/* ---- Lifecycle --------------------------------------------------------- */
|
|
67
85
|
|
|
68
86
|
void js_register_static_atoms(JSRuntime *rt, const JSString *const *cells, size_t n) {
|
|
@@ -96,10 +114,18 @@ void js_atoms_init(JSRuntime *rt) {
|
|
|
96
114
|
|
|
97
115
|
static bool survives(const JSString *s) { return js_gc_is_marked(&s->cell); }
|
|
98
116
|
|
|
99
|
-
/* Drops unmarked atoms
|
|
100
|
-
*
|
|
101
|
-
|
|
117
|
+
/* Drops unmarked atoms. A young collection checks only the atoms added since the last one, and
|
|
118
|
+
* removes the dead ones in place. A full collection checks every atom, and resizes for the
|
|
119
|
+
* survivors: load <= 1/2, shrinking after mass death (dynamic keys), never below 512 entries. */
|
|
120
|
+
void js_atoms_sweep(JSRuntime *rt, bool full) {
|
|
102
121
|
JSAtomTable *t = &rt->atoms;
|
|
122
|
+
if (!full) {
|
|
123
|
+
for (size_t i = 0; i < t->young_count; i++)
|
|
124
|
+
if (!survives(t->young[i])) remove_atom(t, t->young[i]);
|
|
125
|
+
t->young_count = 0;
|
|
126
|
+
return;
|
|
127
|
+
}
|
|
128
|
+
t->young_count = 0;
|
|
103
129
|
size_t live = 0;
|
|
104
130
|
for (size_t i = 0; i < t->cap; i++) live += t->entries[i].str && survives(t->entries[i].str);
|
|
105
131
|
size_t cap = 512;
|
|
@@ -110,6 +136,8 @@ void js_atoms_sweep(JSRuntime *rt) {
|
|
|
110
136
|
void js_atoms_free(JSRuntime *rt) {
|
|
111
137
|
free(rt->atoms.entries);
|
|
112
138
|
rt->atoms.entries = NULL;
|
|
139
|
+
free(rt->atoms.young);
|
|
140
|
+
rt->atoms.young = NULL;
|
|
113
141
|
}
|
|
114
142
|
|
|
115
143
|
size_t js_atom_count(const JSRuntime *rt) {
|
|
@@ -137,6 +165,12 @@ JSValue js_atomize(JSContext *ctx, JSValue s) {
|
|
|
137
165
|
flat->cell.flags |= JS_STR_ATOM; /* immutable content: becoming the atom changes nothing else */
|
|
138
166
|
flat->hash = hash;
|
|
139
167
|
insert(t, flat, hash);
|
|
168
|
+
if (t->young_count == t->young_cap) {
|
|
169
|
+
t->young_cap = t->young_cap ? t->young_cap * 2 : 256;
|
|
170
|
+
t->young = realloc(t->young, t->young_cap * sizeof *t->young);
|
|
171
|
+
if (!t->young) js_fatal("out of memory growing the atom table");
|
|
172
|
+
}
|
|
173
|
+
t->young[t->young_count++] = flat;
|
|
140
174
|
return js_cell(&flat->cell);
|
|
141
175
|
}
|
|
142
176
|
|
package/runtime/src/builtins.c
CHANGED
|
@@ -793,7 +793,7 @@ CORE(object_proto_lookup_setter) {
|
|
|
793
793
|
|
|
794
794
|
/* ---- Function.prototype (§20.2.3) --------------------------------------------------------------- */
|
|
795
795
|
|
|
796
|
-
|
|
796
|
+
JSValue js_function_proto_call(JSContext *ctx, JSValue callee, JSValue this_val, uint32_t argc, const JSValue *argv, JSValue new_target) {
|
|
797
797
|
UNUSED_ARGS;
|
|
798
798
|
if (!js_is_callable(this_val)) return js_throw_type_error(ctx, "Function.prototype.call called on a non-function");
|
|
799
799
|
return js_call(ctx, this_val, ARG(0), argc > 1 ? argc - 1 : 0, argv + (argc > 1 ? 1 : argc));
|
|
@@ -822,10 +822,23 @@ static JSValueArray *list_from_array_like(JSContext *ctx, JSValue v) {
|
|
|
822
822
|
|
|
823
823
|
JSValueArray *js_list_from_array_like(JSContext *ctx, JSValue v) { return list_from_array_like(ctx, v); }
|
|
824
824
|
|
|
825
|
-
|
|
825
|
+
JSValue js_function_proto_apply(JSContext *ctx, JSValue callee, JSValue this_val, uint32_t argc, const JSValue *argv, JSValue new_target) {
|
|
826
826
|
UNUSED_ARGS;
|
|
827
827
|
if (!js_is_callable(this_val)) return js_throw_type_error(ctx, "Function.prototype.apply was called on a non-function");
|
|
828
828
|
if (js_is_nullish(ARG(1))) return js_call(ctx, this_val, ARG(0), 0, NULL);
|
|
829
|
+
/* a short dense array without holes (emit's arguments): its elements are its own data
|
|
830
|
+
* properties, so reading them can't run code. Copied, as the callee may change the array. */
|
|
831
|
+
if (js_is_array(ARG(1))) {
|
|
832
|
+
JSObject *a = js_as_object(ARG(1));
|
|
833
|
+
JSElements *e = a->elements;
|
|
834
|
+
uint32_t n = js_array_length(a);
|
|
835
|
+
JSValue args[8];
|
|
836
|
+
if (e->kind == JS_ELEMENTS_DENSE && e->length == n && n <= 8) {
|
|
837
|
+
uint32_t i = 0;
|
|
838
|
+
while (i < n && !js_is_empty(e->data[i])) args[i] = e->data[i], i++;
|
|
839
|
+
if (i == n) return js_call(ctx, this_val, ARG(0), n, args);
|
|
840
|
+
}
|
|
841
|
+
}
|
|
829
842
|
JSValueArray *list = list_from_array_like(ctx, ARG(1));
|
|
830
843
|
if (!list) return JS_EXCEPTION;
|
|
831
844
|
JSValue r = js_call(ctx, this_val, ARG(0), list->cell.aux, list->values);
|
|
@@ -959,8 +972,8 @@ void js_builtins_realm_init(JSContext *ctx, JSObject *global) {
|
|
|
959
972
|
|
|
960
973
|
JSValue object = define_constructor(ctx, global, "Object", object_ctor, 1, in[JS_INTRINSIC_OBJECT_PROTOTYPE]);
|
|
961
974
|
static const JSBuiltin function_proto[] = {
|
|
962
|
-
JS_BUILTIN_FN("call",
|
|
963
|
-
JS_BUILTIN_FN("apply",
|
|
975
|
+
JS_BUILTIN_FN("call", js_function_proto_call, 1),
|
|
976
|
+
JS_BUILTIN_FN("apply", js_function_proto_apply, 2),
|
|
964
977
|
JS_BUILTIN_FN("bind", function_proto_bind, 1),
|
|
965
978
|
JS_BUILTIN_FN("toString", function_proto_to_string, 0),
|
|
966
979
|
};
|
package/runtime/src/events.c
CHANGED
|
@@ -337,6 +337,25 @@ BUILTIN(remove_on_abort) {
|
|
|
337
337
|
return JS_UNDEFINED;
|
|
338
338
|
}
|
|
339
339
|
|
|
340
|
+
JSValue js_event_target_listeners(JSContext *ctx, JSValue callee, JSValue this_val, uint32_t argc, const JSValue *argv, JSValue new_target) {
|
|
341
|
+
(void)callee, (void)this_val, (void)new_target;
|
|
342
|
+
EventTargetObject *t = argc > 0 ? as_target(argv[0]) : NULL;
|
|
343
|
+
if (!t) return JS_UNDEFINED;
|
|
344
|
+
JSValue type = js_to_string(ctx, argc > 1 ? argv[1] : JS_UNDEFINED);
|
|
345
|
+
if (js_is_exception(type)) return type;
|
|
346
|
+
/* the callbacks first, then the array: its one allocation can't move what the loop reads */
|
|
347
|
+
JSValue *callbacks = malloc((t->n ? t->n : 1) * sizeof *callbacks);
|
|
348
|
+
if (!callbacks) js_fatal("out of memory in events");
|
|
349
|
+
uint32_t n = 0;
|
|
350
|
+
for (uint32_t i = 0; i < t->n; i++) {
|
|
351
|
+
ListenerObject *l = listener_at(t, i);
|
|
352
|
+
if (js_string_equals(ctx, l->type, type)) callbacks[n++] = l->callback;
|
|
353
|
+
}
|
|
354
|
+
JSValue list = js_new_array_from(ctx, callbacks, n);
|
|
355
|
+
free(callbacks);
|
|
356
|
+
return list;
|
|
357
|
+
}
|
|
358
|
+
|
|
340
359
|
/* addEventListener(type, listener, options | capture) */
|
|
341
360
|
BUILTIN(target_add_event_listener) {
|
|
342
361
|
UNUSED_ARGS;
|