Skip to content

Commit 135b5f9

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

File tree

4 files changed

+107
-94
lines changed

4 files changed

+107
-94
lines changed

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

Lines changed: 35 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -13,31 +13,35 @@
1313

1414
// A JS class to use as the underlying source for native readable streams, used
1515
// for Request/Response bodies and TransformStream.
16-
namespace NativeStreamSource {
16+
namespace builtins {
1717

18-
JSObject *owner(JSObject *self) {
18+
JSObject *NativeStreamSource::owner(JSObject *self) {
1919
MOZ_ASSERT(is_instance(self));
2020
return &JS::GetReservedSlot(self, Slots::Owner).toObject();
2121
}
2222

23-
JSObject *stream(JSObject *self) { return RequestOrResponse::body_stream(owner(self)); }
23+
JSObject *NativeStreamSource::stream(JSObject *self) {
24+
return RequestOrResponse::body_stream(owner(self));
25+
}
2426

25-
JS::Value startPromise(JSObject *self) {
27+
JS::Value NativeStreamSource::startPromise(JSObject *self) {
2628
MOZ_ASSERT(is_instance(self));
2729
return JS::GetReservedSlot(self, Slots::StartPromise);
2830
}
2931

30-
PullAlgorithm *pullAlgorithm(JSObject *self) {
32+
NativeStreamSource::PullAlgorithmImplementation *NativeStreamSource::pullAlgorithm(JSObject *self) {
3133
MOZ_ASSERT(is_instance(self));
32-
return (PullAlgorithm *)JS::GetReservedSlot(self, Slots::PullAlgorithm).toPrivate();
34+
return (PullAlgorithmImplementation *)JS::GetReservedSlot(self, Slots::PullAlgorithm).toPrivate();
3335
}
3436

35-
CancelAlgorithm *cancelAlgorithm(JSObject *self) {
37+
NativeStreamSource::CancelAlgorithmImplementation *
38+
NativeStreamSource::cancelAlgorithm(JSObject *self) {
3639
MOZ_ASSERT(is_instance(self));
37-
return (CancelAlgorithm *)JS::GetReservedSlot(self, Slots::CancelAlgorithm).toPrivate();
40+
return (CancelAlgorithmImplementation *)JS::GetReservedSlot(self, Slots::CancelAlgorithm)
41+
.toPrivate();
3842
}
3943

40-
JSObject *controller(JSObject *self) {
44+
JSObject *NativeStreamSource::controller(JSObject *self) {
4145
MOZ_ASSERT(is_instance(self));
4246
return &JS::GetReservedSlot(self, Slots::Controller).toObject();
4347
}
@@ -46,33 +50,33 @@ JSObject *controller(JSObject *self) {
4650
* Returns the underlying source for the given controller iff it's an object,
4751
* nullptr otherwise.
4852
*/
49-
static JSObject *get_controller_source(JSContext *cx, JS::HandleObject controller) {
53+
JSObject *NativeStreamSource::get_controller_source(JSContext *cx, JS::HandleObject controller) {
5054
JS::RootedValue source(cx);
5155
bool success __attribute__((unused));
5256
success = JS::ReadableStreamControllerGetUnderlyingSource(cx, controller, &source);
5357
MOZ_ASSERT(success);
5458
return source.isObject() ? &source.toObject() : nullptr;
5559
}
5660

57-
JSObject *get_stream_source(JSContext *cx, JS::HandleObject stream) {
61+
JSObject *NativeStreamSource::get_stream_source(JSContext *cx, JS::HandleObject stream) {
5862
MOZ_ASSERT(JS::IsReadableStream(stream));
5963
JS::RootedObject controller(cx, JS::ReadableStreamGetController(cx, stream));
6064
return get_controller_source(cx, controller);
6165
}
6266

63-
bool stream_has_native_source(JSContext *cx, JS::HandleObject stream) {
67+
bool NativeStreamSource::stream_has_native_source(JSContext *cx, JS::HandleObject stream) {
6468
JSObject *source = get_stream_source(cx, stream);
6569
return is_instance(source);
6670
}
6771

68-
bool stream_is_body(JSContext *cx, JS::HandleObject stream) {
72+
bool NativeStreamSource::stream_is_body(JSContext *cx, JS::HandleObject stream) {
6973
JSObject *stream_source = get_stream_source(cx, stream);
7074
return NativeStreamSource::is_instance(stream_source) &&
7175
RequestOrResponse::is_instance(owner(stream_source));
7276
}
7377

74-
void set_stream_piped_to_ts_writable(JSContext *cx, JS::HandleObject stream,
75-
JS::HandleObject writable) {
78+
void NativeStreamSource::set_stream_piped_to_ts_writable(JSContext *cx, JS::HandleObject stream,
79+
JS::HandleObject writable) {
7680
JS::RootedObject source(cx, NativeStreamSource::get_stream_source(cx, stream));
7781
MOZ_ASSERT(is_instance(source));
7882
JS::RootedObject sink(cx, NativeStreamSink::get_stream_sink(cx, writable));
@@ -81,12 +85,12 @@ void set_stream_piped_to_ts_writable(JSContext *cx, JS::HandleObject stream,
8185
JS::SetReservedSlot(source, Slots::PipedToTransformStream, JS::ObjectValue(*transform_stream));
8286
}
8387

84-
JSObject *piped_to_transform_stream(JSObject *self) {
88+
JSObject *NativeStreamSource::piped_to_transform_stream(JSObject *self) {
8589
MOZ_ASSERT(is_instance(self));
8690
return JS::GetReservedSlot(self, Slots::PipedToTransformStream).toObjectOrNull();
8791
}
8892

89-
bool lock_stream(JSContext *cx, JS::HandleObject stream) {
93+
bool NativeStreamSource::lock_stream(JSContext *cx, JS::HandleObject stream) {
9094
MOZ_ASSERT(JS::IsReadableStream(stream));
9195

9296
bool locked;
@@ -108,11 +112,7 @@ bool lock_stream(JSContext *cx, JS::HandleObject stream) {
108112
return true;
109113
}
110114

111-
const unsigned ctor_length = 0;
112-
113-
bool check_receiver(JSContext *cx, JS::HandleValue receiver, const char *method_name);
114-
115-
bool start(JSContext *cx, unsigned argc, JS::Value *vp) {
115+
bool NativeStreamSource::start(JSContext *cx, unsigned argc, JS::Value *vp) {
116116
METHOD_HEADER(1)
117117

118118
MOZ_ASSERT(args[0].isObject());
@@ -130,37 +130,38 @@ bool start(JSContext *cx, unsigned argc, JS::Value *vp) {
130130
return true;
131131
}
132132

133-
bool pull(JSContext *cx, unsigned argc, JS::Value *vp) {
133+
bool NativeStreamSource::pull(JSContext *cx, unsigned argc, JS::Value *vp) {
134134
METHOD_HEADER(1)
135135

136136
JS::RootedObject owner(cx, NativeStreamSource::owner(self));
137137
JS::RootedObject controller(cx, &args[0].toObject());
138138
MOZ_ASSERT(controller == NativeStreamSource::controller(self));
139139
MOZ_ASSERT(get_controller_source(cx, controller) == self.get());
140140

141-
PullAlgorithm *pull = pullAlgorithm(self);
141+
PullAlgorithmImplementation *pull = pullAlgorithm(self);
142142
return pull(cx, args, self, owner, controller);
143143
}
144144

145-
bool cancel(JSContext *cx, unsigned argc, JS::Value *vp) {
145+
bool NativeStreamSource::cancel(JSContext *cx, unsigned argc, JS::Value *vp) {
146146
METHOD_HEADER(0)
147147

148148
JS::RootedObject owner(cx, NativeStreamSource::owner(self));
149149
JS::HandleValue reason(args.get(0));
150150

151-
CancelAlgorithm *cancel = cancelAlgorithm(self);
151+
CancelAlgorithmImplementation *cancel = cancelAlgorithm(self);
152152
return cancel(cx, args, self, owner, reason);
153153
}
154154

155-
const JSFunctionSpec methods[] = {JS_FN("start", start, 1, 0), JS_FN("pull", pull, 1, 0),
156-
JS_FN("cancel", cancel, 1, 0), JS_FS_END};
157-
158-
const JSPropertySpec properties[] = {JS_PS_END};
155+
const JSFunctionSpec NativeStreamSource::methods[] = {JS_FN("start", start, 1, 0),
156+
JS_FN("pull", pull, 1, 0),
157+
JS_FN("cancel", cancel, 1, 0), JS_FS_END};
159158

160-
CLASS_BOILERPLATE_NO_CTOR(NativeStreamSource)
159+
const JSPropertySpec NativeStreamSource::properties[] = {JS_PS_END};
161160

162-
JSObject *create(JSContext *cx, JS::HandleObject owner, JS::HandleValue startPromise,
163-
PullAlgorithm *pull, CancelAlgorithm *cancel) {
161+
JSObject *NativeStreamSource::create(JSContext *cx, JS::HandleObject owner,
162+
JS::HandleValue startPromise,
163+
PullAlgorithmImplementation *pull,
164+
CancelAlgorithmImplementation *cancel) {
164165
JS::RootedObject source(cx, JS_NewObjectWithGivenProto(cx, &class_, proto_obj));
165166
if (!source)
166167
return nullptr;
@@ -172,4 +173,4 @@ JSObject *create(JSContext *cx, JS::HandleObject owner, JS::HandleValue startPro
172173
JS::SetReservedSlot(source, Slots::PipedToTransformStream, JS::NullValue());
173174
return source;
174175
}
175-
} // namespace NativeStreamSource
176+
} // namespace builtins
Lines changed: 48 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,53 @@
11
#ifndef JS_COMPUTE_RUNTIME_NATIVE_STREAM_SOURCE_H
22
#define JS_COMPUTE_RUNTIME_NATIVE_STREAM_SOURCE_H
33

4-
namespace NativeStreamSource {
5-
namespace Slots {
6-
enum {
7-
Owner, // Request or Response object, or TransformStream.
8-
Controller, // The ReadableStreamDefaultController.
9-
InternalReader, // Only used to lock the stream if it's consumed internally.
10-
StartPromise, // Used as the return value of `start`, can be undefined.
11-
// Needed to properly implement TransformStream.
12-
PullAlgorithm,
13-
CancelAlgorithm,
14-
PipedToTransformStream, // The TransformStream this source's stream is piped
15-
// to, if any. Only applies if the source backs a
16-
// RequestOrResponse's body.
17-
Count
18-
};
19-
};
20-
bool is_instance(JSObject *obj);
21-
// Register the class.
22-
bool init_class(JSContext *cx, JS::HandleObject global);
23-
typedef bool PullAlgorithm(JSContext *cx, JS::CallArgs args, JS::HandleObject stream,
24-
JS::HandleObject owner, JS::HandleObject controller);
25-
typedef bool CancelAlgorithm(JSContext *cx, JS::CallArgs args, JS::HandleObject stream,
26-
JS::HandleObject owner, JS::HandleValue reason);
4+
#include "builtin.h"
275

28-
JSObject *create(JSContext *cx, JS::HandleObject owner, JS::HandleValue startPromise,
29-
PullAlgorithm *pull, CancelAlgorithm *cancel);
30-
JSObject *get_stream_source(JSContext *cx, JS::HandleObject stream);
31-
JSObject *owner(JSObject *self);
32-
JSObject *stream(JSObject *self);
33-
bool stream_has_native_source(JSContext *cx, JS::HandleObject stream);
34-
bool stream_is_body(JSContext *cx, JS::HandleObject stream);
35-
bool lock_stream(JSContext *cx, JS::HandleObject stream);
36-
JSObject *piped_to_transform_stream(JSObject *source);
37-
JSObject *controller(JSObject *self);
38-
void set_stream_piped_to_ts_writable(JSContext *cx, JS::HandleObject stream,
39-
JS::HandleObject writable);
40-
} // namespace NativeStreamSource
6+
namespace builtins {
7+
class NativeStreamSource : public BuiltinNoConstructor<NativeStreamSource> {
8+
private:
9+
public:
10+
static constexpr const char *class_name = "NativeStreamSource";
11+
enum Slots {
12+
Owner, // Request or Response object, or TransformStream.
13+
Controller, // The ReadableStreamDefaultController.
14+
InternalReader, // Only used to lock the stream if it's consumed internally.
15+
StartPromise, // Used as the return value of `start`, can be undefined.
16+
// Needed to properly implement TransformStream.
17+
PullAlgorithm,
18+
CancelAlgorithm,
19+
PipedToTransformStream, // The TransformStream this source's stream is piped
20+
// to, if any. Only applies if the source backs a
21+
// RequestOrResponse's body.
22+
Count
23+
};
24+
static const JSFunctionSpec methods[];
25+
static const JSPropertySpec properties[];
26+
typedef bool PullAlgorithmImplementation(JSContext *cx, JS::CallArgs args,
27+
JS::HandleObject stream, JS::HandleObject owner,
28+
JS::HandleObject controller);
29+
typedef bool CancelAlgorithmImplementation(JSContext *cx, JS::CallArgs args,
30+
JS::HandleObject stream, JS::HandleObject owner,
31+
JS::HandleValue reason);
32+
static JSObject *owner(JSObject *self);
33+
static JSObject *stream(JSObject *self);
34+
static JS::Value startPromise(JSObject *self);
35+
static PullAlgorithmImplementation *pullAlgorithm(JSObject *self);
36+
static CancelAlgorithmImplementation *cancelAlgorithm(JSObject *self);
37+
static JSObject *controller(JSObject *self);
38+
static JSObject *get_controller_source(JSContext *cx, JS::HandleObject controller);
39+
static JSObject *get_stream_source(JSContext *cx, JS::HandleObject stream);
40+
static bool stream_has_native_source(JSContext *cx, JS::HandleObject stream);
41+
static bool stream_is_body(JSContext *cx, JS::HandleObject stream);
42+
static void set_stream_piped_to_ts_writable(JSContext *cx, JS::HandleObject stream,
43+
JS::HandleObject writable);
44+
static JSObject *piped_to_transform_stream(JSObject *self);
45+
static bool lock_stream(JSContext *cx, JS::HandleObject stream);
46+
static bool start(JSContext *cx, unsigned argc, JS::Value *vp);
47+
static bool pull(JSContext *cx, unsigned argc, JS::Value *vp);
48+
static bool cancel(JSContext *cx, unsigned argc, JS::Value *vp);
49+
static JSObject *create(JSContext *cx, JS::HandleObject owner, JS::HandleValue startPromise,
50+
PullAlgorithmImplementation *pull, CancelAlgorithmImplementation *cancel);
51+
};
52+
} // namespace builtins
4153
#endif

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@ bool pipeTo(JSContext *cx, unsigned argc, JS::Value *vp) {
3838
// writable end of a TransformStream, set the TransformStream as the owner of
3939
// the receiver's source. This enables us to shortcut operations later on.
4040
JS::RootedObject target(cx, args[0].isObject() ? &args[0].toObject() : nullptr);
41-
if (target && NativeStreamSource::stream_has_native_source(cx, self) &&
41+
if (target && builtins::NativeStreamSource::stream_has_native_source(cx, self) &&
4242
JS::IsWritableStream(target) && TransformStream::is_ts_writable(cx, target)) {
43-
NativeStreamSource::set_stream_piped_to_ts_writable(cx, self, target);
43+
builtins::NativeStreamSource::set_stream_piped_to_ts_writable(cx, self, target);
4444
}
4545

4646
return JS::Call(cx, args.thisv(), original_pipeTo, JS::HandleValueArray(args), args.rval());
@@ -279,18 +279,18 @@ JSObject *readable(JSObject *self) {
279279
}
280280

281281
bool is_ts_readable(JSContext *cx, JS::HandleObject readable) {
282-
JSObject *source = NativeStreamSource::get_stream_source(cx, readable);
283-
if (!source || !NativeStreamSource::is_instance(source)) {
282+
JSObject *source = builtins::NativeStreamSource::get_stream_source(cx, readable);
283+
if (!source || !builtins::NativeStreamSource::is_instance(source)) {
284284
return false;
285285
}
286-
JSObject *stream_owner = NativeStreamSource::owner(source);
286+
JSObject *stream_owner = builtins::NativeStreamSource::owner(source);
287287
return stream_owner ? TransformStream::is_instance(stream_owner) : false;
288288
}
289289

290290
JSObject *ts_from_readable(JSContext *cx, JS::HandleObject readable) {
291291
MOZ_ASSERT(is_ts_readable(cx, readable));
292-
JSObject *source = NativeStreamSource::get_stream_source(cx, readable);
293-
return NativeStreamSource::owner(source);
292+
JSObject *source = builtins::NativeStreamSource::get_stream_source(cx, readable);
293+
return builtins::NativeStreamSource::owner(source);
294294
}
295295

296296
bool readable_used_as_body(JSObject *self) {
@@ -817,8 +817,8 @@ bool Initialize(JSContext *cx, JS::HandleObject stream, JS::HandleObject startPr
817817
// Step 8. Set stream.[readable] to ! [CreateReadableStream](startAlgorithm,
818818
// pullAlgorithm, cancelAlgorithm, readableHighWaterMark,
819819
// readableSizeAlgorithm).
820-
JS::RootedObject source(
821-
cx, NativeStreamSource::create(cx, stream, startPromiseVal, pullAlgorithm, cancelAlgorithm));
820+
JS::RootedObject source(cx, builtins::NativeStreamSource::create(cx, stream, startPromiseVal,
821+
pullAlgorithm, cancelAlgorithm));
822822
if (!source)
823823
return false;
824824

0 commit comments

Comments
 (0)