| 1 | /* |
| 2 | * Copyright (c) 2015 Canon Inc. |
| 3 | * Copyright (c) 2015 Igalia. |
| 4 | * Copyright (c) 2016 Apple Inc. All rights reserved. |
| 5 | * |
| 6 | * Redistribution and use in source and binary forms, with or without |
| 7 | * modification, are permitted provided that the following conditions |
| 8 | * are met: |
| 9 | * 1. Redistributions of source code must retain the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer. |
| 11 | * 2. Redistributions in binary form must reproduce the above copyright |
| 12 | * notice, this list of conditions and the following disclaimer in the |
| 13 | * documentation and/or other materials provided with the distribution. |
| 14 | * |
| 15 | * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' |
| 16 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
| 17 | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 18 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS |
| 19 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 20 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 21 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 22 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 23 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 24 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
| 25 | * THE POSSIBILITY OF SUCH DAMAGE. |
| 26 | * |
| 27 | */ |
| 28 | |
| 29 | // DO NOT EDIT THIS FILE. It is automatically generated from JavaScript files for |
| 30 | // builtins by the script: Source/JavaScriptCore/Scripts/generate-js-builtins.py |
| 31 | |
| 32 | #include "config.h" |
| 33 | #include "ReadableStreamBuiltins.h" |
| 34 | |
| 35 | #if ENABLE(STREAMS_API) |
| 36 | |
| 37 | #include "WebCoreJSClientData.h" |
| 38 | #include <JavaScriptCore/HeapInlines.h> |
| 39 | #include <JavaScriptCore/IdentifierInlines.h> |
| 40 | #include <JavaScriptCore/Intrinsic.h> |
| 41 | #include <JavaScriptCore/JSCJSValueInlines.h> |
| 42 | #include <JavaScriptCore/JSCellInlines.h> |
| 43 | #include <JavaScriptCore/StructureInlines.h> |
| 44 | #include <JavaScriptCore/VM.h> |
| 45 | |
| 46 | namespace WebCore { |
| 47 | |
| 48 | const JSC::ConstructAbility s_readableStreamInitializeReadableStreamCodeConstructAbility = JSC::ConstructAbility::CannotConstruct; |
| 49 | const int s_readableStreamInitializeReadableStreamCodeLength = 1915; |
| 50 | static const JSC::Intrinsic s_readableStreamInitializeReadableStreamCodeIntrinsic = JSC::NoIntrinsic; |
| 51 | const char* const s_readableStreamInitializeReadableStreamCode = |
| 52 | "(function (underlyingSource, strategy)\n" \ |
| 53 | "{\n" \ |
| 54 | " \"use strict\";\n" \ |
| 55 | "\n" \ |
| 56 | " if (underlyingSource === @undefined)\n" \ |
| 57 | " underlyingSource = { };\n" \ |
| 58 | " if (strategy === @undefined)\n" \ |
| 59 | " strategy = { };\n" \ |
| 60 | "\n" \ |
| 61 | " if (!@isObject(underlyingSource))\n" \ |
| 62 | " @throwTypeError(\"ReadableStream constructor takes an object as first argument\");\n" \ |
| 63 | "\n" \ |
| 64 | " if (strategy !== @undefined && !@isObject(strategy))\n" \ |
| 65 | " @throwTypeError(\"ReadableStream constructor takes an object as second argument, if any\");\n" \ |
| 66 | "\n" \ |
| 67 | " @putByIdDirectPrivate(this, \"state\", @streamReadable);\n" \ |
| 68 | " @putByIdDirectPrivate(this, \"reader\", @undefined);\n" \ |
| 69 | " @putByIdDirectPrivate(this, \"storedError\", @undefined);\n" \ |
| 70 | " @putByIdDirectPrivate(this, \"disturbed\", false);\n" \ |
| 71 | " //\n" \ |
| 72 | " @putByIdDirectPrivate(this, \"readableStreamController\", null);\n" \ |
| 73 | "\n" \ |
| 74 | " const type = underlyingSource.type;\n" \ |
| 75 | " const typeString = @toString(type);\n" \ |
| 76 | "\n" \ |
| 77 | " if (typeString === \"bytes\") {\n" \ |
| 78 | " if (!@readableByteStreamAPIEnabled())\n" \ |
| 79 | " @throwTypeError(\"ReadableByteStreamController is not implemented\");\n" \ |
| 80 | "\n" \ |
| 81 | " if (strategy.highWaterMark === @undefined)\n" \ |
| 82 | " strategy.highWaterMark = 0;\n" \ |
| 83 | " if (strategy.size !== @undefined)\n" \ |
| 84 | " @throwRangeError(\"Strategy for a ReadableByteStreamController cannot have a size\");\n" \ |
| 85 | "\n" \ |
| 86 | " let readableByteStreamControllerConstructor = @ReadableByteStreamController;\n" \ |
| 87 | " @putByIdDirectPrivate(this, \"readableStreamController\", new @ReadableByteStreamController(this, underlyingSource, strategy.highWaterMark, @isReadableStream));\n" \ |
| 88 | " } else if (type === @undefined) {\n" \ |
| 89 | " if (strategy.highWaterMark === @undefined)\n" \ |
| 90 | " strategy.highWaterMark = 1;\n" \ |
| 91 | " @putByIdDirectPrivate(this, \"readableStreamController\", new @ReadableStreamDefaultController(this, underlyingSource, strategy.size, strategy.highWaterMark, @isReadableStream));\n" \ |
| 92 | " } else\n" \ |
| 93 | " @throwRangeError(\"Invalid type for underlying source\");\n" \ |
| 94 | "\n" \ |
| 95 | " return this;\n" \ |
| 96 | "})\n" \ |
| 97 | ; |
| 98 | |
| 99 | const JSC::ConstructAbility s_readableStreamCancelCodeConstructAbility = JSC::ConstructAbility::CannotConstruct; |
| 100 | const int s_readableStreamCancelCodeLength = 324; |
| 101 | static const JSC::Intrinsic s_readableStreamCancelCodeIntrinsic = JSC::NoIntrinsic; |
| 102 | const char* const s_readableStreamCancelCode = |
| 103 | "(function (reason)\n" \ |
| 104 | "{\n" \ |
| 105 | " \"use strict\";\n" \ |
| 106 | "\n" \ |
| 107 | " if (!@isReadableStream(this))\n" \ |
| 108 | " return @Promise.@reject(@makeThisTypeError(\"ReadableStream\", \"cancel\"));\n" \ |
| 109 | "\n" \ |
| 110 | " if (@isReadableStreamLocked(this))\n" \ |
| 111 | " return @Promise.@reject(@makeTypeError(\"ReadableStream is locked\"));\n" \ |
| 112 | "\n" \ |
| 113 | " return @readableStreamCancel(this, reason);\n" \ |
| 114 | "})\n" \ |
| 115 | ; |
| 116 | |
| 117 | const JSC::ConstructAbility s_readableStreamGetReaderCodeConstructAbility = JSC::ConstructAbility::CannotConstruct; |
| 118 | const int s_readableStreamGetReaderCodeLength = 437; |
| 119 | static const JSC::Intrinsic s_readableStreamGetReaderCodeIntrinsic = JSC::NoIntrinsic; |
| 120 | const char* const s_readableStreamGetReaderCode = |
| 121 | "(function (options)\n" \ |
| 122 | "{\n" \ |
| 123 | " \"use strict\";\n" \ |
| 124 | "\n" \ |
| 125 | " if (!@isReadableStream(this))\n" \ |
| 126 | " throw @makeThisTypeError(\"ReadableStream\", \"getReader\");\n" \ |
| 127 | "\n" \ |
| 128 | " if (options === @undefined)\n" \ |
| 129 | " options = { };\n" \ |
| 130 | "\n" \ |
| 131 | " if (options.mode === @undefined)\n" \ |
| 132 | " return new @ReadableStreamDefaultReader(this);\n" \ |
| 133 | "\n" \ |
| 134 | " //\n" \ |
| 135 | " if (options.mode == 'byob')\n" \ |
| 136 | " return new @ReadableStreamBYOBReader(this);\n" \ |
| 137 | "\n" \ |
| 138 | " @throwRangeError(\"Invalid mode is specified\");\n" \ |
| 139 | "})\n" \ |
| 140 | ; |
| 141 | |
| 142 | const JSC::ConstructAbility s_readableStreamPipeThroughCodeConstructAbility = JSC::ConstructAbility::CannotConstruct; |
| 143 | const int s_readableStreamPipeThroughCodeLength = 299; |
| 144 | static const JSC::Intrinsic s_readableStreamPipeThroughCodeIntrinsic = JSC::NoIntrinsic; |
| 145 | const char* const s_readableStreamPipeThroughCode = |
| 146 | "(function (streams, options)\n" \ |
| 147 | "{\n" \ |
| 148 | " \"use strict\";\n" \ |
| 149 | "\n" \ |
| 150 | " const writable = streams.writable;\n" \ |
| 151 | " const readable = streams.readable;\n" \ |
| 152 | " const promise = this.pipeTo(writable, options);\n" \ |
| 153 | " if (@isPromise(promise))\n" \ |
| 154 | " @putByIdDirectPrivate(promise, \"promiseIsHandled\", true);\n" \ |
| 155 | " return readable;\n" \ |
| 156 | "})\n" \ |
| 157 | ; |
| 158 | |
| 159 | const JSC::ConstructAbility s_readableStreamPipeToCodeConstructAbility = JSC::ConstructAbility::CannotConstruct; |
| 160 | const int s_readableStreamPipeToCodeLength = 2645; |
| 161 | static const JSC::Intrinsic s_readableStreamPipeToCodeIntrinsic = JSC::NoIntrinsic; |
| 162 | const char* const s_readableStreamPipeToCode = |
| 163 | "(function (destination)\n" \ |
| 164 | "{\n" \ |
| 165 | " \"use strict\";\n" \ |
| 166 | "\n" \ |
| 167 | " //\n" \ |
| 168 | " //\n" \ |
| 169 | " const options = arguments[1];\n" \ |
| 170 | "\n" \ |
| 171 | " //\n" \ |
| 172 | " //\n" \ |
| 173 | " //\n" \ |
| 174 | "\n" \ |
| 175 | " const preventClose = @isObject(options) && !!options.preventClose;\n" \ |
| 176 | " const preventAbort = @isObject(options) && !!options.preventAbort;\n" \ |
| 177 | " const preventCancel = @isObject(options) && !!options.preventCancel;\n" \ |
| 178 | "\n" \ |
| 179 | " const source = this;\n" \ |
| 180 | "\n" \ |
| 181 | " let reader;\n" \ |
| 182 | " let lastRead;\n" \ |
| 183 | " let lastWrite;\n" \ |
| 184 | " let closedPurposefully = false;\n" \ |
| 185 | " let promiseCapability;\n" \ |
| 186 | "\n" \ |
| 187 | " function doPipe() {\n" \ |
| 188 | " lastRead = reader.read();\n" \ |
| 189 | " @Promise.prototype.@then.@call(@Promise.all([lastRead, destination.ready]), function([{ value, done }]) {\n" \ |
| 190 | " if (done)\n" \ |
| 191 | " closeDestination();\n" \ |
| 192 | " else if (destination.state === \"writable\") {\n" \ |
| 193 | " lastWrite = destination.write(value);\n" \ |
| 194 | " doPipe();\n" \ |
| 195 | " }\n" \ |
| 196 | " }, function(e) {\n" \ |
| 197 | " throw e;\n" \ |
| 198 | " });\n" \ |
| 199 | " }\n" \ |
| 200 | "\n" \ |
| 201 | " function cancelSource(reason) {\n" \ |
| 202 | " if (!preventCancel) {\n" \ |
| 203 | " reader.cancel(reason);\n" \ |
| 204 | " reader.releaseLock();\n" \ |
| 205 | " promiseCapability.@reject.@call(@undefined, reason);\n" \ |
| 206 | " } else {\n" \ |
| 207 | " @Promise.prototype.@then.@call(lastRead, function() {\n" \ |
| 208 | " reader.releaseLock();\n" \ |
| 209 | " promiseCapability.@reject.@call(@undefined, reason);\n" \ |
| 210 | " });\n" \ |
| 211 | " }\n" \ |
| 212 | " }\n" \ |
| 213 | "\n" \ |
| 214 | " function closeDestination() {\n" \ |
| 215 | " reader.releaseLock();\n" \ |
| 216 | "\n" \ |
| 217 | " const destinationState = destination.state;\n" \ |
| 218 | " if (!preventClose && (destinationState === \"waiting\" || destinationState === \"writable\")) {\n" \ |
| 219 | " closedPurposefully = true;\n" \ |
| 220 | " @Promise.prototype.@then.@call(destination.close(), promiseCapability.@resolve, promiseCapability.@reject);\n" \ |
| 221 | " } else if (lastWrite !== @undefined)\n" \ |
| 222 | " @Promise.prototype.@then.@call(lastWrite, promiseCapability.@resolve, promiseCapability.@reject);\n" \ |
| 223 | " else\n" \ |
| 224 | " promiseCapability.@resolve.@call();\n" \ |
| 225 | "\n" \ |
| 226 | " }\n" \ |
| 227 | "\n" \ |
| 228 | " function abortDestination(reason) {\n" \ |
| 229 | " reader.releaseLock();\n" \ |
| 230 | "\n" \ |
| 231 | " if (!preventAbort)\n" \ |
| 232 | " destination.abort(reason);\n" \ |
| 233 | " promiseCapability.@reject.@call(@undefined, reason);\n" \ |
| 234 | " }\n" \ |
| 235 | "\n" \ |
| 236 | " promiseCapability = @newPromiseCapability(@Promise);\n" \ |
| 237 | "\n" \ |
| 238 | " reader = source.getReader();\n" \ |
| 239 | "\n" \ |
| 240 | " @Promise.prototype.@then.@call(reader.closed, @undefined, abortDestination);\n" \ |
| 241 | " @Promise.prototype.@then.@call(destination.closed,\n" \ |
| 242 | " function() {\n" \ |
| 243 | " if (!closedPurposefully)\n" \ |
| 244 | " cancelSource(@makeTypeError('destination is closing or closed and cannot be piped to anymore'));\n" \ |
| 245 | " },\n" \ |
| 246 | " cancelSource\n" \ |
| 247 | " );\n" \ |
| 248 | "\n" \ |
| 249 | " doPipe();\n" \ |
| 250 | "\n" \ |
| 251 | " return promiseCapability.@promise;\n" \ |
| 252 | "})\n" \ |
| 253 | ; |
| 254 | |
| 255 | const JSC::ConstructAbility s_readableStreamTeeCodeConstructAbility = JSC::ConstructAbility::CannotConstruct; |
| 256 | const int s_readableStreamTeeCodeLength = 175; |
| 257 | static const JSC::Intrinsic s_readableStreamTeeCodeIntrinsic = JSC::NoIntrinsic; |
| 258 | const char* const s_readableStreamTeeCode = |
| 259 | "(function ()\n" \ |
| 260 | "{\n" \ |
| 261 | " \"use strict\";\n" \ |
| 262 | "\n" \ |
| 263 | " if (!@isReadableStream(this))\n" \ |
| 264 | " throw @makeThisTypeError(\"ReadableStream\", \"tee\");\n" \ |
| 265 | "\n" \ |
| 266 | " return @readableStreamTee(this, false);\n" \ |
| 267 | "})\n" \ |
| 268 | ; |
| 269 | |
| 270 | const JSC::ConstructAbility s_readableStreamLockedCodeConstructAbility = JSC::ConstructAbility::CannotConstruct; |
| 271 | const int s_readableStreamLockedCodeLength = 178; |
| 272 | static const JSC::Intrinsic s_readableStreamLockedCodeIntrinsic = JSC::NoIntrinsic; |
| 273 | const char* const s_readableStreamLockedCode = |
| 274 | "(function ()\n" \ |
| 275 | "{\n" \ |
| 276 | " \"use strict\";\n" \ |
| 277 | "\n" \ |
| 278 | " if (!@isReadableStream(this))\n" \ |
| 279 | " throw @makeGetterTypeError(\"ReadableStream\", \"locked\");\n" \ |
| 280 | "\n" \ |
| 281 | " return @isReadableStreamLocked(this);\n" \ |
| 282 | "})\n" \ |
| 283 | ; |
| 284 | |
| 285 | |
| 286 | #define DEFINE_BUILTIN_GENERATOR(codeName, functionName, overriddenName, argumentCount) \ |
| 287 | JSC::FunctionExecutable* codeName##Generator(JSC::VM& vm) \ |
| 288 | {\ |
| 289 | JSVMClientData* clientData = static_cast<JSVMClientData*>(vm.clientData); \ |
| 290 | return clientData->builtinFunctions().readableStreamBuiltins().codeName##Executable()->link(vm, nullptr, clientData->builtinFunctions().readableStreamBuiltins().codeName##Source(), WTF::nullopt, s_##codeName##Intrinsic); \ |
| 291 | } |
| 292 | WEBCORE_FOREACH_READABLESTREAM_BUILTIN_CODE(DEFINE_BUILTIN_GENERATOR) |
| 293 | #undef DEFINE_BUILTIN_GENERATOR |
| 294 | |
| 295 | |
| 296 | } // namespace WebCore |
| 297 | |
| 298 | #endif // ENABLE(STREAMS_API) |
| 299 | |