|
| 1 | +// TODO: remove these once the warnings are fixed |
| 2 | +#pragma clang diagnostic push |
| 3 | +#pragma clang diagnostic ignored "-Winvalid-offsetof" |
| 4 | +#include "js/experimental/TypedData.h" // used within "js/Stream.h" |
| 5 | +#pragma clang diagnostic pop |
| 6 | + |
| 7 | +#include "js/Stream.h" |
| 8 | + |
| 9 | +#include "builtin.h" |
| 10 | +#include "builtins/native-stream-sink.h" |
| 11 | +#include "builtins/native-stream-source.h" |
| 12 | +#include "js-compute-builtins.h" |
| 13 | + |
| 14 | +// A JS class to use as the underlying source for native readable streams, used |
| 15 | +// for Request/Response bodies and TransformStream. |
| 16 | +namespace NativeStreamSource { |
| 17 | + |
| 18 | +JSObject *owner(JSObject *self) { |
| 19 | + MOZ_ASSERT(is_instance(self)); |
| 20 | + return &JS::GetReservedSlot(self, Slots::Owner).toObject(); |
| 21 | +} |
| 22 | + |
| 23 | +JSObject *stream(JSObject *self) { return RequestOrResponse::body_stream(owner(self)); } |
| 24 | + |
| 25 | +JS::Value startPromise(JSObject *self) { |
| 26 | + MOZ_ASSERT(is_instance(self)); |
| 27 | + return JS::GetReservedSlot(self, Slots::StartPromise); |
| 28 | +} |
| 29 | + |
| 30 | +PullAlgorithm *pullAlgorithm(JSObject *self) { |
| 31 | + MOZ_ASSERT(is_instance(self)); |
| 32 | + return (PullAlgorithm *)JS::GetReservedSlot(self, Slots::PullAlgorithm).toPrivate(); |
| 33 | +} |
| 34 | + |
| 35 | +CancelAlgorithm *cancelAlgorithm(JSObject *self) { |
| 36 | + MOZ_ASSERT(is_instance(self)); |
| 37 | + return (CancelAlgorithm *)JS::GetReservedSlot(self, Slots::CancelAlgorithm).toPrivate(); |
| 38 | +} |
| 39 | + |
| 40 | +JSObject *controller(JSObject *self) { |
| 41 | + MOZ_ASSERT(is_instance(self)); |
| 42 | + return &JS::GetReservedSlot(self, Slots::Controller).toObject(); |
| 43 | +} |
| 44 | + |
| 45 | +/** |
| 46 | + * Returns the underlying source for the given controller iff it's an object, |
| 47 | + * nullptr otherwise. |
| 48 | + */ |
| 49 | +static JSObject *get_controller_source(JSContext *cx, JS::HandleObject controller) { |
| 50 | + JS::RootedValue source(cx); |
| 51 | + bool success __attribute__((unused)); |
| 52 | + success = JS::ReadableStreamControllerGetUnderlyingSource(cx, controller, &source); |
| 53 | + MOZ_ASSERT(success); |
| 54 | + return source.isObject() ? &source.toObject() : nullptr; |
| 55 | +} |
| 56 | + |
| 57 | +JSObject *get_stream_source(JSContext *cx, JS::HandleObject stream) { |
| 58 | + MOZ_ASSERT(JS::IsReadableStream(stream)); |
| 59 | + JS::RootedObject controller(cx, JS::ReadableStreamGetController(cx, stream)); |
| 60 | + return get_controller_source(cx, controller); |
| 61 | +} |
| 62 | + |
| 63 | +bool stream_has_native_source(JSContext *cx, JS::HandleObject stream) { |
| 64 | + JSObject *source = get_stream_source(cx, stream); |
| 65 | + return is_instance(source); |
| 66 | +} |
| 67 | + |
| 68 | +bool stream_is_body(JSContext *cx, JS::HandleObject stream) { |
| 69 | + JSObject *stream_source = get_stream_source(cx, stream); |
| 70 | + return NativeStreamSource::is_instance(stream_source) && |
| 71 | + RequestOrResponse::is_instance(owner(stream_source)); |
| 72 | +} |
| 73 | + |
| 74 | +void set_stream_piped_to_ts_writable(JSContext *cx, JS::HandleObject stream, |
| 75 | + JS::HandleObject writable) { |
| 76 | + JS::RootedObject source(cx, NativeStreamSource::get_stream_source(cx, stream)); |
| 77 | + MOZ_ASSERT(is_instance(source)); |
| 78 | + JS::RootedObject sink(cx, NativeStreamSink::get_stream_sink(cx, writable)); |
| 79 | + JS::RootedObject transform_stream(cx, NativeStreamSink::owner(sink)); |
| 80 | + MOZ_ASSERT(transform_stream); |
| 81 | + JS::SetReservedSlot(source, Slots::PipedToTransformStream, JS::ObjectValue(*transform_stream)); |
| 82 | +} |
| 83 | + |
| 84 | +JSObject *piped_to_transform_stream(JSObject *self) { |
| 85 | + MOZ_ASSERT(is_instance(self)); |
| 86 | + return JS::GetReservedSlot(self, Slots::PipedToTransformStream).toObjectOrNull(); |
| 87 | +} |
| 88 | + |
| 89 | +bool lock_stream(JSContext *cx, JS::HandleObject stream) { |
| 90 | + MOZ_ASSERT(JS::IsReadableStream(stream)); |
| 91 | + |
| 92 | + bool locked; |
| 93 | + JS::ReadableStreamIsLocked(cx, stream, &locked); |
| 94 | + if (locked) { |
| 95 | + JS_ReportErrorLatin1(cx, "Can't lock an already locked ReadableStream"); |
| 96 | + return false; |
| 97 | + } |
| 98 | + |
| 99 | + JS::RootedObject self(cx, get_stream_source(cx, stream)); |
| 100 | + MOZ_ASSERT(is_instance(self)); |
| 101 | + |
| 102 | + auto mode = JS::ReadableStreamReaderMode::Default; |
| 103 | + JS::RootedObject reader(cx, JS::ReadableStreamGetReader(cx, stream, mode)); |
| 104 | + if (!reader) |
| 105 | + return false; |
| 106 | + |
| 107 | + JS::SetReservedSlot(self, Slots::InternalReader, JS::ObjectValue(*reader)); |
| 108 | + return true; |
| 109 | +} |
| 110 | + |
| 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) { |
| 116 | + METHOD_HEADER(1) |
| 117 | + |
| 118 | + MOZ_ASSERT(args[0].isObject()); |
| 119 | + JS::RootedObject controller(cx, &args[0].toObject()); |
| 120 | + MOZ_ASSERT(get_controller_source(cx, controller) == self); |
| 121 | + |
| 122 | + JS::SetReservedSlot(self, NativeStreamSource::Slots::Controller, args[0]); |
| 123 | + |
| 124 | + // For TransformStream, StartAlgorithm returns the same Promise for both the |
| 125 | + // readable and writable stream. All other native initializations of |
| 126 | + // ReadableStream have StartAlgorithm return undefined. Instead of introducing |
| 127 | + // both the StartAlgorithm as a pointer and startPromise as a value, we just |
| 128 | + // store the latter or undefined, and always return it. |
| 129 | + args.rval().set(startPromise(self)); |
| 130 | + return true; |
| 131 | +} |
| 132 | + |
| 133 | +bool pull(JSContext *cx, unsigned argc, JS::Value *vp) { |
| 134 | + METHOD_HEADER(1) |
| 135 | + |
| 136 | + JS::RootedObject owner(cx, NativeStreamSource::owner(self)); |
| 137 | + JS::RootedObject controller(cx, &args[0].toObject()); |
| 138 | + MOZ_ASSERT(controller == NativeStreamSource::controller(self)); |
| 139 | + MOZ_ASSERT(get_controller_source(cx, controller) == self.get()); |
| 140 | + |
| 141 | + PullAlgorithm *pull = pullAlgorithm(self); |
| 142 | + return pull(cx, args, self, owner, controller); |
| 143 | +} |
| 144 | + |
| 145 | +bool cancel(JSContext *cx, unsigned argc, JS::Value *vp) { |
| 146 | + METHOD_HEADER(0) |
| 147 | + |
| 148 | + JS::RootedObject owner(cx, NativeStreamSource::owner(self)); |
| 149 | + JS::HandleValue reason(args.get(0)); |
| 150 | + |
| 151 | + CancelAlgorithm *cancel = cancelAlgorithm(self); |
| 152 | + return cancel(cx, args, self, owner, reason); |
| 153 | +} |
| 154 | + |
| 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}; |
| 159 | + |
| 160 | +CLASS_BOILERPLATE_NO_CTOR(NativeStreamSource) |
| 161 | + |
| 162 | +JSObject *create(JSContext *cx, JS::HandleObject owner, JS::HandleValue startPromise, |
| 163 | + PullAlgorithm *pull, CancelAlgorithm *cancel) { |
| 164 | + JS::RootedObject source(cx, JS_NewObjectWithGivenProto(cx, &class_, proto_obj)); |
| 165 | + if (!source) |
| 166 | + return nullptr; |
| 167 | + |
| 168 | + JS::SetReservedSlot(source, Slots::Owner, JS::ObjectValue(*owner)); |
| 169 | + JS::SetReservedSlot(source, Slots::StartPromise, startPromise); |
| 170 | + JS::SetReservedSlot(source, Slots::PullAlgorithm, JS::PrivateValue((void *)pull)); |
| 171 | + JS::SetReservedSlot(source, Slots::CancelAlgorithm, JS::PrivateValue((void *)cancel)); |
| 172 | + JS::SetReservedSlot(source, Slots::PipedToTransformStream, JS::NullValue()); |
| 173 | + return source; |
| 174 | +} |
| 175 | +} // namespace NativeStreamSource |
0 commit comments