Skip to content

Commit 467dd51

Browse files
Jake ChampionJakeChampion
authored andcommitted
refactor native-stream-sink to extend from BuiltinNoConstructor class
1 parent 135b5f9 commit 467dd51

File tree

4 files changed

+103
-72
lines changed

4 files changed

+103
-72
lines changed

c-dependencies/js-compute-runtime/builtins/native-stream-sink.cpp

Lines changed: 40 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -12,68 +12,57 @@
1212

1313
// A JS class to use as the underlying sink for native writable streams, used
1414
// for TransformStream.
15-
namespace NativeStreamSink {
16-
namespace Slots {
17-
enum {
18-
Owner, // TransformStream.
19-
Controller, // The WritableStreamDefaultController.
20-
InternalWriter, // Only used to lock the stream if it's consumed internally.
21-
StartPromise, // Used as the return value of `start`, can be undefined.
22-
// Needed to properly implement TransformStream.
23-
WriteAlgorithm,
24-
AbortAlgorithm,
25-
CloseAlgorithm,
26-
// AbortAlgorithm, TODO: implement
27-
Count
28-
};
29-
};
30-
31-
JSObject *owner(JSObject *self) { return &JS::GetReservedSlot(self, Slots::Owner).toObject(); }
32-
33-
JS::Value startPromise(JSObject *self) { return JS::GetReservedSlot(self, Slots::StartPromise); }
34-
35-
WriteAlgorithm *writeAlgorithm(JSObject *self) {
36-
return (WriteAlgorithm *)JS::GetReservedSlot(self, Slots::WriteAlgorithm).toPrivate();
15+
namespace builtins {
16+
17+
JSObject *NativeStreamSink::owner(JSObject *self) {
18+
return &JS::GetReservedSlot(self, Slots::Owner).toObject();
19+
}
20+
21+
JS::Value NativeStreamSink::startPromise(JSObject *self) {
22+
return JS::GetReservedSlot(self, Slots::StartPromise);
23+
}
24+
25+
NativeStreamSink::WriteAlgorithmImplementation *NativeStreamSink::writeAlgorithm(JSObject *self) {
26+
return (WriteAlgorithmImplementation *)JS::GetReservedSlot(self, Slots::WriteAlgorithm)
27+
.toPrivate();
3728
}
3829

39-
AbortAlgorithm *abortAlgorithm(JSObject *self) {
40-
return (AbortAlgorithm *)JS::GetReservedSlot(self, Slots::AbortAlgorithm).toPrivate();
30+
NativeStreamSink::AbortAlgorithmImplementation *NativeStreamSink::abortAlgorithm(JSObject *self) {
31+
return (AbortAlgorithmImplementation *)JS::GetReservedSlot(self, Slots::AbortAlgorithm)
32+
.toPrivate();
4133
}
4234

43-
CloseAlgorithm *closeAlgorithm(JSObject *self) {
44-
return (CloseAlgorithm *)JS::GetReservedSlot(self, Slots::CloseAlgorithm).toPrivate();
35+
NativeStreamSink::CloseAlgorithmImplementation *NativeStreamSink::closeAlgorithm(JSObject *self) {
36+
return (CloseAlgorithmImplementation *)JS::GetReservedSlot(self, Slots::CloseAlgorithm)
37+
.toPrivate();
4538
}
4639

47-
JSObject *controller(JSObject *self) {
40+
JSObject *NativeStreamSink::controller(JSObject *self) {
4841
return &JS::GetReservedSlot(self, Slots::Controller).toObject();
4942
}
5043

5144
/**
5245
* Returns the underlying sink for the given controller iff it's an object,
5346
* nullptr otherwise.
5447
*/
55-
static JSObject *get_controller_sink(JSContext *cx, JS::HandleObject controller) {
48+
JSObject *NativeStreamSink::get_controller_sink(JSContext *cx, JS::HandleObject controller) {
5649
JS::RootedValue sink(cx, JS::WritableStreamControllerGetUnderlyingSink(cx, controller));
5750
return sink.isObject() ? &sink.toObject() : nullptr;
5851
}
5952

60-
JSObject *get_stream_sink(JSContext *cx, JS::HandleObject stream) {
53+
JSObject *NativeStreamSink::get_stream_sink(JSContext *cx, JS::HandleObject stream) {
6154
JS::RootedObject controller(cx, JS::WritableStreamGetController(cx, stream));
6255
return get_controller_sink(cx, controller);
6356
}
6457

65-
bool stream_has_native_sink(JSContext *cx, JS::HandleObject stream) {
58+
bool NativeStreamSink::stream_has_native_sink(JSContext *cx, JS::HandleObject stream) {
6659
MOZ_RELEASE_ASSERT(JS::IsWritableStream(stream));
6760

6861
JSObject *sink = get_stream_sink(cx, stream);
6962
return is_instance(sink);
7063
}
7164

72-
const unsigned ctor_length = 0;
73-
74-
bool check_receiver(JSContext *cx, JS::HandleValue receiver, const char *method_name);
75-
76-
bool start(JSContext *cx, unsigned argc, JS::Value *vp) {
65+
bool NativeStreamSink::start(JSContext *cx, unsigned argc, JS::Value *vp) {
7766
METHOD_HEADER(1)
7867

7968
MOZ_ASSERT(args[0].isObject());
@@ -93,45 +82,46 @@ bool start(JSContext *cx, unsigned argc, JS::Value *vp) {
9382
return true;
9483
}
9584

96-
bool write(JSContext *cx, unsigned argc, JS::Value *vp) {
85+
bool NativeStreamSink::write(JSContext *cx, unsigned argc, JS::Value *vp) {
9786
METHOD_HEADER(1)
9887

9988
JS::RootedObject owner(cx, NativeStreamSink::owner(self));
10089
JS::HandleValue chunk(args[0]);
10190

102-
WriteAlgorithm *write = writeAlgorithm(self);
91+
WriteAlgorithmImplementation *write = writeAlgorithm(self);
10392
return write(cx, args, self, owner, chunk);
10493
}
10594

106-
bool abort(JSContext *cx, unsigned argc, JS::Value *vp) {
95+
bool NativeStreamSink::abort(JSContext *cx, unsigned argc, JS::Value *vp) {
10796
METHOD_HEADER(1)
10897

10998
JS::RootedObject owner(cx, NativeStreamSink::owner(self));
11099
JS::HandleValue reason(args[0]);
111100

112-
AbortAlgorithm *abort = abortAlgorithm(self);
101+
AbortAlgorithmImplementation *abort = abortAlgorithm(self);
113102
return abort(cx, args, self, owner, reason);
114103
}
115104

116-
bool close(JSContext *cx, unsigned argc, JS::Value *vp) {
105+
bool NativeStreamSink::close(JSContext *cx, unsigned argc, JS::Value *vp) {
117106
METHOD_HEADER(0)
118107

119108
JS::RootedObject owner(cx, NativeStreamSink::owner(self));
120109

121-
CloseAlgorithm *close = closeAlgorithm(self);
110+
CloseAlgorithmImplementation *close = closeAlgorithm(self);
122111
return close(cx, args, self, owner);
123112
}
124113

125-
const JSFunctionSpec methods[] = {JS_FN("start", start, 1, 0), JS_FN("write", write, 2, 0),
126-
JS_FN("abort", abort, 2, 0), JS_FN("close", close, 1, 0),
127-
JS_FS_END};
128-
129-
const JSPropertySpec properties[] = {JS_PS_END};
114+
const JSFunctionSpec NativeStreamSink::methods[] = {
115+
JS_FN("start", start, 1, 0), JS_FN("write", write, 2, 0), JS_FN("abort", abort, 2, 0),
116+
JS_FN("close", close, 1, 0), JS_FS_END};
130117

131-
CLASS_BOILERPLATE_NO_CTOR(NativeStreamSink)
118+
const JSPropertySpec NativeStreamSink::properties[] = {JS_PS_END};
132119

133-
JSObject *create(JSContext *cx, JS::HandleObject owner, JS::HandleValue startPromise,
134-
WriteAlgorithm *write, CloseAlgorithm *close, AbortAlgorithm *abort) {
120+
JSObject *NativeStreamSink::create(JSContext *cx, JS::HandleObject owner,
121+
JS::HandleValue startPromise,
122+
WriteAlgorithmImplementation *write,
123+
CloseAlgorithmImplementation *close,
124+
AbortAlgorithmImplementation *abort) {
135125
JS::RootedObject sink(cx, JS_NewObjectWithGivenProto(cx, &class_, proto_obj));
136126
if (!sink)
137127
return nullptr;
@@ -143,4 +133,4 @@ JSObject *create(JSContext *cx, JS::HandleObject owner, JS::HandleValue startPro
143133
JS::SetReservedSlot(sink, Slots::CloseAlgorithm, JS::PrivateValue((void *)close));
144134
return sink;
145135
}
146-
} // namespace NativeStreamSink
136+
} // namespace builtins
Lines changed: 57 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,61 @@
11
#ifndef JS_COMPUTE_RUNTIME_NATIVE_STREAM_SINK_H
22
#define JS_COMPUTE_RUNTIME_NATIVE_STREAM_SINK_H
3+
#include "builtin.h"
34

4-
namespace NativeStreamSink {
5-
typedef bool WriteAlgorithm(JSContext *cx, JS::CallArgs args, JS::HandleObject stream,
6-
JS::HandleObject owner, JS::HandleValue chunk);
7-
typedef bool AbortAlgorithm(JSContext *cx, JS::CallArgs args, JS::HandleObject stream,
8-
JS::HandleObject owner, JS::HandleValue reason);
9-
typedef bool CloseAlgorithm(JSContext *cx, JS::CallArgs args, JS::HandleObject stream,
10-
JS::HandleObject owner);
11-
bool is_instance(JSObject *obj);
12-
// Register the class.
13-
bool init_class(JSContext *cx, JS::HandleObject global);
14-
JSObject *create(JSContext *cx, JS::HandleObject owner, JS::HandleValue startPromise,
15-
WriteAlgorithm *write, CloseAlgorithm *close, AbortAlgorithm *abort);
16-
JSObject *get_stream_sink(JSContext *cx, JS::HandleObject stream);
17-
JSObject *owner(JSObject *self);
18-
19-
} // namespace NativeStreamSink
5+
namespace builtins {
6+
7+
class NativeStreamSink : public BuiltinNoConstructor<NativeStreamSink> {
8+
private:
9+
public:
10+
static constexpr const char *class_name = "NativeStreamSink";
11+
12+
enum Slots {
13+
Owner, // TransformStream.
14+
Controller, // The WritableStreamDefaultController.
15+
InternalWriter, // Only used to lock the stream if it's consumed internally.
16+
StartPromise, // Used as the return value of `start`, can be undefined.
17+
// Needed to properly implement TransformStream.
18+
WriteAlgorithm,
19+
AbortAlgorithm,
20+
CloseAlgorithm,
21+
// AbortAlgorithm, TODO: implement
22+
Count
23+
};
24+
25+
typedef bool WriteAlgorithmImplementation(JSContext *cx, JS::CallArgs args,
26+
JS::HandleObject stream, JS::HandleObject owner,
27+
JS::HandleValue chunk);
28+
typedef bool AbortAlgorithmImplementation(JSContext *cx, JS::CallArgs args,
29+
JS::HandleObject stream, JS::HandleObject owner,
30+
JS::HandleValue reason);
31+
typedef bool CloseAlgorithmImplementation(JSContext *cx, JS::CallArgs args,
32+
JS::HandleObject stream, JS::HandleObject owner);
33+
34+
static const JSFunctionSpec methods[];
35+
static const JSPropertySpec properties[];
36+
37+
static JSObject *owner(JSObject *self);
38+
static JS::Value startPromise(JSObject *self);
39+
static WriteAlgorithmImplementation *writeAlgorithm(JSObject *self);
40+
static AbortAlgorithmImplementation *abortAlgorithm(JSObject *self);
41+
static CloseAlgorithmImplementation *closeAlgorithm(JSObject *self);
42+
static JSObject *controller(JSObject *self);
43+
44+
/**
45+
* Returns the underlying sink for the given controller iff it's an object,
46+
* nullptr otherwise.
47+
*/
48+
static JSObject *get_controller_sink(JSContext *cx, JS::HandleObject controller);
49+
static JSObject *get_stream_sink(JSContext *cx, JS::HandleObject stream);
50+
static bool stream_has_native_sink(JSContext *cx, JS::HandleObject stream);
51+
static bool start(JSContext *cx, unsigned argc, JS::Value *vp);
52+
static bool write(JSContext *cx, unsigned argc, JS::Value *vp);
53+
static bool abort(JSContext *cx, unsigned argc, JS::Value *vp);
54+
static bool close(JSContext *cx, unsigned argc, JS::Value *vp);
55+
static JSObject *create(JSContext *cx, JS::HandleObject owner, JS::HandleValue startPromise,
56+
WriteAlgorithmImplementation *write, CloseAlgorithmImplementation *close,
57+
AbortAlgorithmImplementation *abort);
58+
};
59+
60+
} // namespace builtins
2061
#endif

c-dependencies/js-compute-runtime/builtins/transform-stream.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -321,11 +321,11 @@ JSObject *writable(JSObject *self) {
321321
}
322322

323323
bool is_ts_writable(JSContext *cx, JS::HandleObject writable) {
324-
JSObject *sink = NativeStreamSink::get_stream_sink(cx, writable);
325-
if (!sink || !NativeStreamSink::is_instance(sink)) {
324+
JSObject *sink = builtins::NativeStreamSink::get_stream_sink(cx, writable);
325+
if (!sink || !builtins::NativeStreamSink::is_instance(sink)) {
326326
return false;
327327
}
328-
JSObject *stream_owner = NativeStreamSink::owner(sink);
328+
JSObject *stream_owner = builtins::NativeStreamSink::owner(sink);
329329
return stream_owner ? is_instance(stream_owner) : false;
330330
}
331331

@@ -794,8 +794,8 @@ bool Initialize(JSContext *cx, JS::HandleObject stream, JS::HandleObject startPr
794794
// writableSizeAlgorithm).
795795
JS::RootedValue startPromiseVal(cx, JS::ObjectValue(*startPromise));
796796
JS::RootedObject sink(
797-
cx, NativeStreamSink::create(cx, stream, startPromiseVal, DefaultSinkWriteAlgorithm,
798-
DefaultSinkCloseAlgorithm, DefaultSinkAbortAlgorithm));
797+
cx, builtins::NativeStreamSink::create(cx, stream, startPromiseVal, DefaultSinkWriteAlgorithm,
798+
DefaultSinkCloseAlgorithm, DefaultSinkAbortAlgorithm));
799799
if (!sink)
800800
return false;
801801

c-dependencies/js-compute-runtime/js-compute-builtins.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4894,7 +4894,7 @@ bool define_fastly_sys(JSContext *cx, HandleObject global) {
48944894

48954895
if (!builtins::NativeStreamSource::init_class(cx, global))
48964896
return false;
4897-
if (!NativeStreamSink::init_class(cx, global))
4897+
if (!builtins::NativeStreamSink::init_class(cx, global))
48984898
return false;
48994899
if (!TransformStreamDefaultController::init_class(cx, global))
49004900
return false;

0 commit comments

Comments
 (0)