Skip to content

Commit 3ddd2c5

Browse files
Jake ChampionJakeChampion
authored andcommitted
refactor TransformStreamDefaultController to extend from BuiltinNoConstructor class
1 parent 467dd51 commit 3ddd2c5

File tree

6 files changed

+134
-86
lines changed

6 files changed

+134
-86
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ bool deflate_chunk(JSContext *cx, JS::HandleObject self, JS::HandleValue chunk,
146146
}
147147

148148
JS::RootedValue out_chunk(cx, JS::ObjectValue(*out_obj));
149-
if (!TransformStreamDefaultController::Enqueue(cx, controller, out_chunk)) {
149+
if (!builtins::TransformStreamDefaultController::Enqueue(cx, controller, out_chunk)) {
150150
return false;
151151
}
152152
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ bool inflate_chunk(JSContext *cx, JS::HandleObject self, JS::HandleValue chunk,
149149
}
150150

151151
JS::RootedValue out_chunk(cx, JS::ObjectValue(*out_obj));
152-
if (!TransformStreamDefaultController::Enqueue(cx, controller, out_chunk)) {
152+
if (!builtins::TransformStreamDefaultController::Enqueue(cx, controller, out_chunk)) {
153153
return false;
154154
}
155155
}

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

Lines changed: 62 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -16,40 +16,38 @@
1616
* All algorithm names and steps refer to spec algorithms defined at
1717
* https://streams.spec.whatwg.org/#ts-default-controller-class
1818
*/
19-
namespace TransformStreamDefaultController {
20-
bool is_instance(JSObject *obj);
21-
22-
JSObject *stream(JSObject *controller) {
19+
// A JS class to use as the underlying sink for native writable streams, used
20+
// for TransformStream.
21+
namespace builtins {
22+
JSObject *TransformStreamDefaultController::stream(JSObject *controller) {
2323
MOZ_ASSERT(is_instance(controller));
2424
return &JS::GetReservedSlot(controller, Slots::Stream).toObject();
2525
}
2626

27-
TransformAlgorithm *transformAlgorithm(JSObject *controller) {
27+
TransformStreamDefaultController::TransformAlgorithmImplementation *
28+
TransformStreamDefaultController::transformAlgorithm(JSObject *controller) {
2829
MOZ_ASSERT(is_instance(controller));
29-
return (TransformAlgorithm *)JS::GetReservedSlot(controller, Slots::TransformAlgorithm)
30+
return (TransformAlgorithmImplementation *)JS::GetReservedSlot(controller,
31+
Slots::TransformAlgorithm)
3032
.toPrivate();
3133
}
3234

33-
FlushAlgorithm *flushAlgorithm(JSObject *controller) {
35+
TransformStreamDefaultController::FlushAlgorithmImplementation *
36+
TransformStreamDefaultController::flushAlgorithm(JSObject *controller) {
3437
MOZ_ASSERT(is_instance(controller));
35-
return (FlushAlgorithm *)JS::GetReservedSlot(controller, Slots::FlushAlgorithm).toPrivate();
38+
return (FlushAlgorithmImplementation *)JS::GetReservedSlot(controller, Slots::FlushAlgorithm)
39+
.toPrivate();
3640
}
3741

38-
const unsigned ctor_length = 0;
39-
40-
bool check_receiver(JSContext *cx, JS::HandleValue receiver, const char *method_name);
41-
42-
bool Enqueue(JSContext *cx, JS::HandleObject controller, JS::HandleValue chunk);
43-
bool Terminate(JSContext *cx, JS::HandleObject controller);
44-
4542
/**
4643
* https://streams.spec.whatwg.org/#ts-default-controller-desired-size
4744
*/
48-
bool desiredSize_get(JSContext *cx, unsigned argc, JS::Value *vp) {
45+
bool TransformStreamDefaultController::desiredSize_get(JSContext *cx, unsigned argc,
46+
JS::Value *vp) {
4947
METHOD_HEADER_WITH_NAME(0, "get desiredSize")
5048

5149
// 1. Let readableController be [this].[stream].[readable].[controller].
52-
JSObject *stream = ::TransformStreamDefaultController::stream(self);
50+
JSObject *stream = TransformStreamDefaultController::stream(self);
5351
JSObject *readable = TransformStream::readable(stream);
5452
double value;
5553
bool has_value;
@@ -66,13 +64,10 @@ bool desiredSize_get(JSContext *cx, unsigned argc, JS::Value *vp) {
6664
return true;
6765
}
6866

69-
const JSPropertySpec properties[] = {JS_PSG("desiredSize", desiredSize_get, JSPROP_ENUMERATE),
70-
JS_PS_END};
71-
7267
/**
7368
* https://streams.spec.whatwg.org/#ts-default-controller-enqueue
7469
*/
75-
bool enqueue_js(JSContext *cx, unsigned argc, JS::Value *vp) {
70+
bool TransformStreamDefaultController::enqueue_js(JSContext *cx, unsigned argc, JS::Value *vp) {
7671
METHOD_HEADER_WITH_NAME(0, "enqueue")
7772

7873
// 1. Perform TransformStreamDefaultControllerEnqueue([this], chunk).
@@ -87,12 +82,12 @@ bool enqueue_js(JSContext *cx, unsigned argc, JS::Value *vp) {
8782
/**
8883
* https://streams.spec.whatwg.org/#ts-default-controller-error
8984
*/
90-
bool error_js(JSContext *cx, unsigned argc, JS::Value *vp) {
85+
bool TransformStreamDefaultController::error_js(JSContext *cx, unsigned argc, JS::Value *vp) {
9186
METHOD_HEADER_WITH_NAME(0, "error")
9287

9388
// 1. Perform TransformStreamDefaultControllerError(this, e).
9489
// (inlined)
95-
JS::RootedObject stream(cx, ::TransformStreamDefaultController::stream(self));
90+
JS::RootedObject stream(cx, TransformStreamDefaultController::stream(self));
9691

9792
if (!TransformStream::Error(cx, stream, args.get(0))) {
9893
return false;
@@ -105,7 +100,7 @@ bool error_js(JSContext *cx, unsigned argc, JS::Value *vp) {
105100
/**
106101
* https://streams.spec.whatwg.org/#ts-default-controller-terminate
107102
*/
108-
bool terminate_js(JSContext *cx, unsigned argc, JS::Value *vp) {
103+
bool TransformStreamDefaultController::terminate_js(JSContext *cx, unsigned argc, JS::Value *vp) {
109104
METHOD_HEADER_WITH_NAME(0, "terminate")
110105

111106
// 1. Perform TransformStreamDefaultControllerTerminate(this).
@@ -117,14 +112,18 @@ bool terminate_js(JSContext *cx, unsigned argc, JS::Value *vp) {
117112
return true;
118113
}
119114

120-
const JSFunctionSpec methods[] = {JS_FN("enqueue", enqueue_js, 1, JSPROP_ENUMERATE),
121-
JS_FN("error", error_js, 1, JSPROP_ENUMERATE),
122-
JS_FN("terminate", terminate_js, 0, JSPROP_ENUMERATE), JS_FS_END};
115+
const JSFunctionSpec TransformStreamDefaultController::methods[] = {
116+
JS_FN("enqueue", enqueue_js, 1, JSPROP_ENUMERATE),
117+
JS_FN("error", error_js, 1, JSPROP_ENUMERATE),
118+
JS_FN("terminate", terminate_js, 0, JSPROP_ENUMERATE), JS_FS_END};
123119

124-
CLASS_BOILERPLATE_NO_CTOR(TransformStreamDefaultController)
120+
const JSPropertySpec TransformStreamDefaultController::properties[] = {
121+
JS_PSG("desiredSize", desiredSize_get, JSPROP_ENUMERATE), JS_PS_END};
125122

126-
JSObject *create(JSContext *cx, JS::HandleObject stream, TransformAlgorithm *transformAlgo,
127-
FlushAlgorithm *flushAlgo) {
123+
JSObject *TransformStreamDefaultController::create(
124+
JSContext *cx, JS::HandleObject stream,
125+
TransformStreamDefaultController::TransformAlgorithmImplementation *transformAlgo,
126+
TransformStreamDefaultController::FlushAlgorithmImplementation *flushAlgo) {
128127
JS::RootedObject controller(cx, JS_NewObjectWithGivenProto(cx, &class_, proto_obj));
129128
if (!controller)
130129
return nullptr;
@@ -151,8 +150,9 @@ JSObject *create(JSContext *cx, JS::HandleObject stream, TransformAlgorithm *tra
151150
return controller;
152151
}
153152

154-
void set_transformer(JSObject *controller, JS::Value transformer, JSObject *transformFunction,
155-
JSObject *flushFunction) {
153+
void TransformStreamDefaultController::set_transformer(JSObject *controller, JS::Value transformer,
154+
JSObject *transformFunction,
155+
JSObject *flushFunction) {
156156
JS::SetReservedSlot(controller, Slots::Transformer, transformer);
157157
JS::SetReservedSlot(controller, Slots::TransformInput, JS::ObjectOrNullValue(transformFunction));
158158
JS::SetReservedSlot(controller, Slots::FlushInput, JS::ObjectOrNullValue(flushFunction));
@@ -161,11 +161,12 @@ void set_transformer(JSObject *controller, JS::Value transformer, JSObject *tran
161161
/**
162162
* TransformStreamDefaultControllerEnqueue
163163
*/
164-
bool Enqueue(JSContext *cx, JS::HandleObject controller, JS::HandleValue chunk) {
164+
bool TransformStreamDefaultController::Enqueue(JSContext *cx, JS::HandleObject controller,
165+
JS::HandleValue chunk) {
165166
MOZ_ASSERT(is_instance(controller));
166167

167168
// 1. Let stream be controller.[stream].
168-
JS::RootedObject stream(cx, ::TransformStreamDefaultController::stream(controller));
169+
JS::RootedObject stream(cx, TransformStreamDefaultController::stream(controller));
169170

170171
// 2. Let readableController be stream.[readable].[controller].
171172
JS::RootedObject readable(cx, TransformStream::readable(stream));
@@ -224,11 +225,11 @@ bool Enqueue(JSContext *cx, JS::HandleObject controller, JS::HandleValue chunk)
224225
/**
225226
* TransformStreamDefaultControllerTerminate
226227
*/
227-
bool Terminate(JSContext *cx, JS::HandleObject controller) {
228+
bool TransformStreamDefaultController::Terminate(JSContext *cx, JS::HandleObject controller) {
228229
MOZ_ASSERT(is_instance(controller));
229230

230231
// 1. Let stream be controller.[stream].
231-
JS::RootedObject stream(cx, ::TransformStreamDefaultController::stream(controller));
232+
JS::RootedObject stream(cx, TransformStreamDefaultController::stream(controller));
232233

233234
// 2. Let readableController be stream.[readable].[controller].
234235
JS::RootedObject readable(cx, TransformStream::readable(stream));
@@ -275,8 +276,8 @@ bool Terminate(JSContext *cx, JS::HandleObject controller) {
275276
* of <invoke a callback function> and the conversion step from
276277
* https://webidl.spec.whatwg.org/#es-promise on the completion value.
277278
*/
278-
JSObject *InvokePromiseReturningCallback(JSContext *cx, JS::HandleValue receiver,
279-
JS::HandleValue callback, JS::HandleValueArray args) {
279+
JSObject *TransformStreamDefaultController::InvokePromiseReturningCallback(
280+
JSContext *cx, JS::HandleValue receiver, JS::HandleValue callback, JS::HandleValueArray args) {
280281
JS::RootedValue rval(cx);
281282
if (!JS::Call(cx, receiver, callback, args, &rval)) {
282283
return PromiseRejectedWithPendingError(cx);
@@ -291,8 +292,8 @@ JSObject *InvokePromiseReturningCallback(JSContext *cx, JS::HandleValue receiver
291292
*
292293
* Steps 2.* and 4 of SetUpTransformStreamDefaultControllerFromTransformer.
293294
*/
294-
JSObject *transform_algorithm_transformer(JSContext *cx, JS::HandleObject controller,
295-
JS::HandleValue chunk) {
295+
JSObject *TransformStreamDefaultController::transform_algorithm_transformer(
296+
JSContext *cx, JS::HandleObject controller, JS::HandleValue chunk) {
296297
MOZ_ASSERT(is_instance(controller));
297298

298299
// Step 2. Let transformAlgorithm be the following steps, taking a chunk
@@ -329,7 +330,9 @@ JSObject *transform_algorithm_transformer(JSContext *cx, JS::HandleObject contro
329330
*
330331
* Steps 3 and 5 of SetUpTransformStreamDefaultControllerFromTransformer.
331332
*/
332-
JSObject *flush_algorithm_transformer(JSContext *cx, JS::HandleObject controller) {
333+
JSObject *
334+
TransformStreamDefaultController::flush_algorithm_transformer(JSContext *cx,
335+
JS::HandleObject controller) {
333336
MOZ_ASSERT(is_instance(controller));
334337

335338
// Step 3. Let flushAlgorithm be an algorithm which returns a promise
@@ -352,8 +355,9 @@ JSObject *flush_algorithm_transformer(JSContext *cx, JS::HandleObject controller
352355
* SetUpTransformStreamDefaultController
353356
* https://streams.spec.whatwg.org/#set-up-transform-stream-default-controller
354357
*/
355-
JSObject *SetUp(JSContext *cx, JS::HandleObject stream, TransformAlgorithm *transformAlgo,
356-
FlushAlgorithm *flushAlgo) {
358+
JSObject *TransformStreamDefaultController::SetUp(JSContext *cx, JS::HandleObject stream,
359+
TransformAlgorithmImplementation *transformAlgo,
360+
FlushAlgorithmImplementation *flushAlgo) {
357361
MOZ_ASSERT(TransformStream::is_instance(stream));
358362

359363
// Step 1 of SetUpTransformStreamDefaultControllerFromTransformer and step 1-6
@@ -367,8 +371,11 @@ JSObject *SetUp(JSContext *cx, JS::HandleObject stream, TransformAlgorithm *tran
367371
* SetUpTransformStreamDefaultControllerFromTransformer
368372
* https://streams.spec.whatwg.org/#set-up-transform-stream-default-controller-from-transformer
369373
*/
370-
JSObject *SetUpFromTransformer(JSContext *cx, JS::HandleObject stream, JS::HandleValue transformer,
371-
JS::HandleObject transformFunction, JS::HandleObject flushFunction) {
374+
JSObject *TransformStreamDefaultController::SetUpFromTransformer(JSContext *cx,
375+
JS::HandleObject stream,
376+
JS::HandleValue transformer,
377+
JS::HandleObject transformFunction,
378+
JS::HandleObject flushFunction) {
372379
MOZ_ASSERT(TransformStream::is_instance(stream));
373380

374381
// Step 1, moved into SetUpTransformStreamDefaultController.
@@ -390,8 +397,10 @@ JSObject *SetUpFromTransformer(JSContext *cx, JS::HandleObject stream, JS::Handl
390397
/**
391398
* Steps 2.* of TransformStreamDefaultControllerPerformTransform.
392399
*/
393-
bool transformPromise_catch_handler(JSContext *cx, JS::HandleObject controller,
394-
JS::HandleValue extra, JS::CallArgs args) {
400+
bool TransformStreamDefaultController::transformPromise_catch_handler(JSContext *cx,
401+
JS::HandleObject controller,
402+
JS::HandleValue extra,
403+
JS::CallArgs args) {
395404
JS::RootedValue r(cx, args.get(0));
396405
// 1. Perform ! [TransformStreamError](controller.[stream], r).
397406
JS::RootedObject streamObj(cx, stream(controller));
@@ -407,12 +416,14 @@ bool transformPromise_catch_handler(JSContext *cx, JS::HandleObject controller,
407416
/**
408417
* TransformStreamDefaultControllerPerformTransform
409418
*/
410-
JSObject *PerformTransform(JSContext *cx, JS::HandleObject controller, JS::HandleValue chunk) {
419+
JSObject *TransformStreamDefaultController::PerformTransform(JSContext *cx,
420+
JS::HandleObject controller,
421+
JS::HandleValue chunk) {
411422
MOZ_ASSERT(is_instance(controller));
412423

413424
// 1. Let transformPromise be the result of performing
414425
// controller.[transformAlgorithm], passing chunk.
415-
TransformAlgorithm *transformAlgo = transformAlgorithm(controller);
426+
TransformAlgorithmImplementation *transformAlgo = transformAlgorithm(controller);
416427
JS::RootedObject transformPromise(cx, transformAlgo(cx, controller, chunk));
417428
if (!transformPromise) {
418429
return nullptr;
@@ -432,7 +443,7 @@ JSObject *PerformTransform(JSContext *cx, JS::HandleObject controller, JS::Handl
432443
/**
433444
* TransformStreamDefaultControllerClearAlgorithms
434445
*/
435-
void ClearAlgorithms(JSObject *controller) {
446+
void TransformStreamDefaultController::ClearAlgorithms(JSObject *controller) {
436447
MOZ_ASSERT(is_instance(controller));
437448

438449
// 1. Set controller.[transformAlgorithm] to undefined.
@@ -443,4 +454,4 @@ void ClearAlgorithms(JSObject *controller) {
443454
JS::SetReservedSlot(controller, Slots::FlushAlgorithm, JS::PrivateValue(nullptr));
444455
JS::SetReservedSlot(controller, Slots::FlushInput, JS::UndefinedValue());
445456
}
446-
} // namespace TransformStreamDefaultController
457+
} // namespace builtins

c-dependencies/js-compute-runtime/builtins/transform-stream-default-controller.h

Lines changed: 61 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,33 +3,68 @@
33

44
#include "builtin.h"
55

6-
namespace TransformStreamDefaultController {
7-
typedef JSObject *TransformAlgorithm(JSContext *cx, JS::HandleObject controller,
8-
JS::HandleValue chunk);
9-
typedef JSObject *FlushAlgorithm(JSContext *cx, JS::HandleObject controller);
10-
11-
namespace Slots {
12-
enum {
13-
Stream,
14-
Transformer,
15-
TransformAlgorithm,
16-
TransformInput, // JS::Value to be used by TransformAlgorithm, e.g. a
17-
// JSFunction to call.
18-
FlushAlgorithm,
19-
FlushInput, // JS::Value to be used by FlushAlgorithm, e.g. a JSFunction to
20-
// call.
21-
Count
22-
};
6+
namespace builtins {
7+
8+
class TransformStreamDefaultController
9+
: public BuiltinNoConstructor<TransformStreamDefaultController> {
10+
private:
11+
public:
12+
static constexpr const char *class_name = "TransformStreamDefaultController";
13+
14+
enum Slots {
15+
Stream,
16+
Transformer,
17+
TransformAlgorithm,
18+
TransformInput, // JS::Value to be used by TransformAlgorithm, e.g. a
19+
// JSFunction to call.
20+
FlushAlgorithm,
21+
FlushInput, // JS::Value to be used by FlushAlgorithm, e.g. a JSFunction to
22+
// call.
23+
Count
24+
};
25+
26+
typedef JSObject *TransformAlgorithmImplementation(JSContext *cx, JS::HandleObject controller,
27+
JS::HandleValue chunk);
28+
typedef JSObject *FlushAlgorithmImplementation(JSContext *cx, JS::HandleObject controller);
29+
30+
static const JSFunctionSpec methods[];
31+
static const JSPropertySpec properties[];
32+
33+
static bool Enqueue(JSContext *cx, JS::HandleObject controller, JS::HandleValue chunk);
34+
static bool Terminate(JSContext *cx, JS::HandleObject controller);
35+
36+
static JSObject *stream(JSObject *controller);
37+
static TransformAlgorithmImplementation *transformAlgorithm(JSObject *controller);
38+
static FlushAlgorithmImplementation *flushAlgorithm(JSObject *controller);
39+
static bool desiredSize_get(JSContext *cx, unsigned argc, JS::Value *vp);
40+
static bool enqueue_js(JSContext *cx, unsigned argc, JS::Value *vp);
41+
static bool error_js(JSContext *cx, unsigned argc, JS::Value *vp);
42+
static bool terminate_js(JSContext *cx, unsigned argc, JS::Value *vp);
43+
static JSObject *create(JSContext *cx, JS::HandleObject stream,
44+
TransformAlgorithmImplementation *transformAlgo,
45+
FlushAlgorithmImplementation *flushAlgo);
46+
static void set_transformer(JSObject *controller, JS::Value transformer,
47+
JSObject *transformFunction, JSObject *flushFunction);
48+
static JSObject *InvokePromiseReturningCallback(JSContext *cx, JS::HandleValue receiver,
49+
JS::HandleValue callback,
50+
JS::HandleValueArray args);
51+
static JSObject *transform_algorithm_transformer(JSContext *cx, JS::HandleObject controller,
52+
JS::HandleValue chunk);
53+
static JSObject *flush_algorithm_transformer(JSContext *cx, JS::HandleObject controller);
54+
static JSObject *SetUp(JSContext *cx, JS::HandleObject stream,
55+
TransformAlgorithmImplementation *transformAlgo,
56+
FlushAlgorithmImplementation *flushAlgo);
57+
static JSObject *SetUpFromTransformer(JSContext *cx, JS::HandleObject stream,
58+
JS::HandleValue transformer,
59+
JS::HandleObject transformFunction,
60+
JS::HandleObject flushFunction);
61+
static bool transformPromise_catch_handler(JSContext *cx, JS::HandleObject controller,
62+
JS::HandleValue extra, JS::CallArgs args);
63+
static JSObject *PerformTransform(JSContext *cx, JS::HandleObject controller,
64+
JS::HandleValue chunk);
65+
static void ClearAlgorithms(JSObject *controller);
2366
};
2467

25-
// Register the class.
26-
bool init_class(JSContext *cx, JS::HandleObject global);
27-
bool Enqueue(JSContext *cx, JS::HandleObject controller, JS::HandleValue chunk);
28-
JSObject *PerformTransform(JSContext *cx, JS::HandleObject controller, JS::HandleValue chunk);
29-
void ClearAlgorithms(JSObject *controller);
30-
JSObject *SetUpFromTransformer(JSContext *cx, JS::HandleObject stream, JS::HandleValue transformer,
31-
JS::HandleObject transformFunction, JS::HandleObject flushFunction);
32-
FlushAlgorithm *flushAlgorithm(JSObject *controller);
33-
} // namespace TransformStreamDefaultController
68+
} // namespace builtins
3469

3570
#endif

0 commit comments

Comments
 (0)