Skip to content

Commit 215b1d9

Browse files
authored
feat: implement AbortController and AbortSignal (#240)
* Implement AbortConrtoller and AbortSignal * Add Response aborter algorithm * Replace glitch.me with fastly.dev * Reject bodyAll promise on abort * Add more spec references to AbortSignal * Fix indentation * Check if request's abort signal is instance of AbortSignal * Spec subtleties * More spec subtleties * Fix AbortSignal protype chain * Missing spec ref
1 parent 059d09a commit 215b1d9

33 files changed

+1608
-226
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
#include "abort-controller.h"
2+
#include "abort-signal.h"
3+
4+
namespace builtins {
5+
namespace web {
6+
namespace abort {
7+
8+
const JSFunctionSpec AbortController::static_methods[] = {
9+
JS_FS_END,
10+
};
11+
12+
const JSPropertySpec AbortController::static_properties[] = {
13+
JS_PS_END,
14+
};
15+
16+
const JSFunctionSpec AbortController::methods[] = {
17+
JS_FN("abort", abort, 0, JSPROP_ENUMERATE),
18+
JS_FS_END,
19+
};
20+
21+
const JSPropertySpec AbortController::properties[] = {
22+
JS_PSG("signal", signal_get, JSPROP_ENUMERATE),
23+
JS_PS_END,
24+
};
25+
26+
bool AbortController::signal_get(JSContext *cx, unsigned argc, JS::Value *vp) {
27+
METHOD_HEADER(0);
28+
29+
args.rval().set(JS::GetReservedSlot(self, Slots::Signal));
30+
return true;
31+
}
32+
33+
bool AbortController::abort(JSContext *cx, unsigned argc, JS::Value *vp) {
34+
METHOD_HEADER(0);
35+
36+
RootedValue reason(cx, args.get(0));
37+
RootedObject signal(cx, JS::GetReservedSlot(self, Slots::Signal).toObjectOrNull());
38+
if (!signal) {
39+
return false;
40+
}
41+
42+
return AbortSignal::abort(cx, signal, reason);
43+
}
44+
45+
bool AbortController::constructor(JSContext *cx, unsigned argc, JS::Value *vp) {
46+
CTOR_HEADER("AbortController", 0);
47+
48+
RootedObject self(cx, JS_NewObjectForConstructor(cx, &class_, args));
49+
if (!self) {
50+
return false;
51+
}
52+
53+
RootedObject signal(cx, AbortSignal::create(cx));
54+
if (!signal) {
55+
return false;
56+
}
57+
58+
SetReservedSlot(self, Slots::Signal, JS::ObjectValue(*signal));
59+
60+
args.rval().setObject(*self);
61+
return true;
62+
}
63+
64+
bool AbortController::init_class(JSContext *cx, JS::HandleObject global) {
65+
return init_class_impl(cx, global);
66+
}
67+
68+
} // namespace abort
69+
} // namespace web
70+
} // namespace builtins
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#ifndef WEB_BUILTINS_ABORT_CONTROLLER_H
2+
#define WEB_BUILTINS_ABORT_CONTROLLER_H
3+
4+
#include "builtin.h"
5+
6+
namespace builtins {
7+
namespace web {
8+
namespace abort {
9+
10+
class AbortController : public BuiltinImpl<AbortController> {
11+
static bool signal_get(JSContext *cx, unsigned argc, JS::Value *vp);
12+
static bool abort(JSContext *cx, unsigned argc, JS::Value *vp);
13+
14+
static JSObject *create(JSContext *cx);
15+
16+
public:
17+
static constexpr const char *class_name = "AbortController";
18+
static constexpr unsigned ctor_length = 0;
19+
20+
enum Slots { Signal = 0, Count };
21+
22+
static const JSFunctionSpec static_methods[];
23+
static const JSPropertySpec static_properties[];
24+
static const JSFunctionSpec methods[];
25+
static const JSPropertySpec properties[];
26+
27+
static bool init_class(JSContext *cx, HandleObject global);
28+
static bool constructor(JSContext *cx, unsigned argc, Value *vp);
29+
};
30+
31+
} // namespace abort
32+
} // namespace web
33+
} // namespace builtins
34+
35+
36+
37+
#endif // WEB_BUILTINS_ABORT_CONTROLLER_H

0 commit comments

Comments
 (0)