Skip to content

Commit 61c6a8f

Browse files
Jake ChampionJakeChampion
authored andcommitted
Allow TransformStream to be used as a base class to extend from within application javascript
1 parent 2b724b7 commit 61c6a8f

File tree

1 file changed

+112
-98
lines changed

1 file changed

+112
-98
lines changed

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

Lines changed: 112 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -2599,100 +2599,6 @@ bool ExtractStrategy(JSContext *cx, HandleValue strategy, double default_hwm, do
25992599
* https://streams.spec.whatwg.org/#ts-class
26002600
*/
26012601
namespace TransformStream {
2602-
JSObject *create(JSContext *cx, double writableHighWaterMark,
2603-
JS::HandleFunction writableSizeAlgorithm, double readableHighWaterMark,
2604-
JS::HandleFunction readableSizeAlgorithm, HandleValue transformer,
2605-
HandleObject startFunction, HandleObject transformFunction,
2606-
HandleObject flushFunction);
2607-
2608-
/**
2609-
* https://streams.spec.whatwg.org/#ts-constructor
2610-
*/
2611-
bool constructor(JSContext *cx, unsigned argc, Value *vp) {
2612-
CTOR_HEADER("TransformStream", 0);
2613-
2614-
RootedObject startFunction(cx);
2615-
RootedObject transformFunction(cx);
2616-
RootedObject flushFunction(cx);
2617-
2618-
// 1. If transformer is missing, set it to null.
2619-
RootedValue transformer(cx, args.get(0));
2620-
if (transformer.isUndefined()) {
2621-
transformer.setNull();
2622-
}
2623-
2624-
if (transformer.isObject()) {
2625-
RootedObject transformerDict(cx, &transformer.toObject());
2626-
2627-
// 2. Let transformerDict be transformer, [converted to an IDL value] of
2628-
// type `
2629-
// [Transformer]`.
2630-
// Note: we do the extraction of dict entries manually, because no WebIDL
2631-
// codegen.
2632-
if (!ExtractFunction(cx, transformerDict, "start", &startFunction)) {
2633-
return false;
2634-
}
2635-
2636-
if (!ExtractFunction(cx, transformerDict, "transform", &transformFunction)) {
2637-
return false;
2638-
}
2639-
2640-
if (!ExtractFunction(cx, transformerDict, "flush", &flushFunction)) {
2641-
return false;
2642-
}
2643-
2644-
// 3. If transformerDict["readableType"] [exists], throw a `[RangeError]`
2645-
// exception.
2646-
bool found;
2647-
if (!JS_HasProperty(cx, transformerDict, "readableType", &found)) {
2648-
return false;
2649-
}
2650-
if (found) {
2651-
JS_ReportErrorLatin1(cx, "transformer.readableType is reserved for future use");
2652-
return false;
2653-
}
2654-
2655-
// 4. If transformerDict["writableType"] [exists], throw a `[RangeError]`
2656-
// exception.
2657-
if (!JS_HasProperty(cx, transformerDict, "writableType", &found)) {
2658-
return false;
2659-
}
2660-
if (found) {
2661-
JS_ReportErrorLatin1(cx, "transformer.writableType is reserved for future use");
2662-
return false;
2663-
}
2664-
}
2665-
2666-
// 5. Let readableHighWaterMark be ? [ExtractHighWaterMark](readableStrategy,
2667-
// 0).
2668-
// 6. Let readableSizeAlgorithm be !
2669-
// [ExtractSizeAlgorithm](readableStrategy).
2670-
double readableHighWaterMark;
2671-
JS::RootedFunction readableSizeAlgorithm(cx);
2672-
if (!ExtractStrategy(cx, args.get(2), 0, &readableHighWaterMark, &readableSizeAlgorithm)) {
2673-
return false;
2674-
}
2675-
2676-
// 7. Let writableHighWaterMark be ? [ExtractHighWaterMark](writableStrategy,
2677-
// 1).
2678-
// 8. Let writableSizeAlgorithm be !
2679-
// [ExtractSizeAlgorithm](writableStrategy).
2680-
double writableHighWaterMark;
2681-
JS::RootedFunction writableSizeAlgorithm(cx);
2682-
if (!ExtractStrategy(cx, args.get(1), 1, &writableHighWaterMark, &writableSizeAlgorithm)) {
2683-
return false;
2684-
}
2685-
2686-
// Steps 9-13.
2687-
RootedObject self(cx, create(cx, writableHighWaterMark, writableSizeAlgorithm,
2688-
readableHighWaterMark, readableSizeAlgorithm, transformer,
2689-
startFunction, transformFunction, flushFunction));
2690-
if (!self)
2691-
return false;
2692-
2693-
args.rval().setObject(*self);
2694-
return true;
2695-
}
26962602

26972603
const unsigned ctor_length = 0;
26982604

@@ -2814,8 +2720,105 @@ const JSFunctionSpec methods[] = {JS_FS_END};
28142720
const JSPropertySpec properties[] = {JS_PSG("readable", readable_get, JSPROP_ENUMERATE),
28152721
JS_PSG("writable", writable_get, JSPROP_ENUMERATE), JS_PS_END};
28162722

2723+
bool constructor(JSContext *cx, unsigned argc, Value *vp);
2724+
28172725
CLASS_BOILERPLATE_CUSTOM_INIT(TransformStream)
28182726

2727+
JSObject *create(JSContext *cx, HandleObject self, double writableHighWaterMark,
2728+
JS::HandleFunction writableSizeAlgorithm, double readableHighWaterMark,
2729+
JS::HandleFunction readableSizeAlgorithm, HandleValue transformer,
2730+
HandleObject startFunction, HandleObject transformFunction,
2731+
HandleObject flushFunction);
2732+
/**
2733+
* https://streams.spec.whatwg.org/#ts-constructor
2734+
*/
2735+
bool constructor(JSContext *cx, unsigned argc, Value *vp) {
2736+
CTOR_HEADER("TransformStream", 0);
2737+
2738+
RootedObject startFunction(cx);
2739+
RootedObject transformFunction(cx);
2740+
RootedObject flushFunction(cx);
2741+
2742+
// 1. If transformer is missing, set it to null.
2743+
RootedValue transformer(cx, args.get(0));
2744+
if (transformer.isUndefined()) {
2745+
transformer.setNull();
2746+
}
2747+
2748+
if (transformer.isObject()) {
2749+
RootedObject transformerDict(cx, &transformer.toObject());
2750+
2751+
// 2. Let transformerDict be transformer, [converted to an IDL value] of
2752+
// type `
2753+
// [Transformer]`.
2754+
// Note: we do the extraction of dict entries manually, because no WebIDL
2755+
// codegen.
2756+
if (!ExtractFunction(cx, transformerDict, "start", &startFunction)) {
2757+
return false;
2758+
}
2759+
2760+
if (!ExtractFunction(cx, transformerDict, "transform", &transformFunction)) {
2761+
return false;
2762+
}
2763+
2764+
if (!ExtractFunction(cx, transformerDict, "flush", &flushFunction)) {
2765+
return false;
2766+
}
2767+
2768+
// 3. If transformerDict["readableType"] [exists], throw a `[RangeError]`
2769+
// exception.
2770+
bool found;
2771+
if (!JS_HasProperty(cx, transformerDict, "readableType", &found)) {
2772+
return false;
2773+
}
2774+
if (found) {
2775+
JS_ReportErrorLatin1(cx, "transformer.readableType is reserved for future use");
2776+
return false;
2777+
}
2778+
2779+
// 4. If transformerDict["writableType"] [exists], throw a `[RangeError]`
2780+
// exception.
2781+
if (!JS_HasProperty(cx, transformerDict, "writableType", &found)) {
2782+
return false;
2783+
}
2784+
if (found) {
2785+
JS_ReportErrorLatin1(cx, "transformer.writableType is reserved for future use");
2786+
return false;
2787+
}
2788+
}
2789+
2790+
// 5. Let readableHighWaterMark be ? [ExtractHighWaterMark](readableStrategy,
2791+
// 0).
2792+
// 6. Let readableSizeAlgorithm be !
2793+
// [ExtractSizeAlgorithm](readableStrategy).
2794+
double readableHighWaterMark;
2795+
JS::RootedFunction readableSizeAlgorithm(cx);
2796+
if (!ExtractStrategy(cx, args.get(2), 0, &readableHighWaterMark, &readableSizeAlgorithm)) {
2797+
return false;
2798+
}
2799+
2800+
// 7. Let writableHighWaterMark be ? [ExtractHighWaterMark](writableStrategy,
2801+
// 1).
2802+
// 8. Let writableSizeAlgorithm be !
2803+
// [ExtractSizeAlgorithm](writableStrategy).
2804+
double writableHighWaterMark;
2805+
JS::RootedFunction writableSizeAlgorithm(cx);
2806+
if (!ExtractStrategy(cx, args.get(1), 1, &writableHighWaterMark, &writableSizeAlgorithm)) {
2807+
return false;
2808+
}
2809+
2810+
// Steps 9-13.
2811+
RootedObject transformStreamInstance(cx, JS_NewObjectForConstructor(cx, &class_, args));
2812+
RootedObject self(cx, create(cx, transformStreamInstance, writableHighWaterMark,
2813+
writableSizeAlgorithm, readableHighWaterMark, readableSizeAlgorithm,
2814+
transformer, startFunction, transformFunction, flushFunction));
2815+
if (!self)
2816+
return false;
2817+
2818+
args.rval().setObject(*self);
2819+
return true;
2820+
}
2821+
28192822
bool init_class(JSContext *cx, HandleObject global) {
28202823
bool ok = init_class_impl(cx, global);
28212824
if (!ok)
@@ -3227,14 +3230,11 @@ bool ErrorWritableAndUnblockWrite(JSContext *cx, HandleObject stream, HandleValu
32273230
* https://streams.spec.whatwg.org/#ts-constructor
32283231
* Steps 9-13.
32293232
*/
3230-
JSObject *create(JSContext *cx, double writableHighWaterMark,
3233+
JSObject *create(JSContext *cx, HandleObject self, double writableHighWaterMark,
32313234
JS::HandleFunction writableSizeAlgorithm, double readableHighWaterMark,
32323235
JS::HandleFunction readableSizeAlgorithm, HandleValue transformer,
32333236
HandleObject startFunction, HandleObject transformFunction,
32343237
HandleObject flushFunction) {
3235-
RootedObject self(cx, JS_NewObjectWithGivenProto(cx, &class_, proto_obj));
3236-
if (!self)
3237-
return nullptr;
32383238

32393239
// Step 9.
32403240
RootedObject startPromise(cx, JS::NewPromiseObject(cx, nullptr));
@@ -3278,6 +3278,20 @@ JSObject *create(JSContext *cx, double writableHighWaterMark,
32783278
return self;
32793279
}
32803280

3281+
JSObject *create(JSContext *cx, double writableHighWaterMark,
3282+
JS::HandleFunction writableSizeAlgorithm, double readableHighWaterMark,
3283+
JS::HandleFunction readableSizeAlgorithm, HandleValue transformer,
3284+
HandleObject startFunction, HandleObject transformFunction,
3285+
HandleObject flushFunction) {
3286+
RootedObject self(cx, JS_NewObjectWithGivenProto(cx, &class_, proto_obj));
3287+
if (!self)
3288+
return nullptr;
3289+
3290+
return TransformStream::create(cx, self, writableHighWaterMark, writableSizeAlgorithm,
3291+
readableHighWaterMark, readableSizeAlgorithm, transformer,
3292+
startFunction, transformFunction, flushFunction);
3293+
}
3294+
32813295
/**
32823296
* Implementation of
32833297
* https://streams.spec.whatwg.org/#readablestream-create-a-proxy

0 commit comments

Comments
 (0)