1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim:set ts=2 sw=2 sts=2 et cindent: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "mozilla/dom/ByteLengthQueuingStrategy.h"
8 #include "mozilla/dom/FunctionBinding.h"
9 #include "mozilla/dom/QueuingStrategyBinding.h"
11 #include "nsISupports.h"
13 #include "js/TypeDecls.h"
14 #include "js/PropertyAndElement.h"
16 namespace mozilla::dom
{
18 NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE_INHERITED(ByteLengthQueuingStrategy
,
20 NS_IMPL_ADDREF_INHERITED(ByteLengthQueuingStrategy
, BaseQueuingStrategy
)
21 NS_IMPL_RELEASE_INHERITED(ByteLengthQueuingStrategy
, BaseQueuingStrategy
)
23 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(ByteLengthQueuingStrategy
)
24 NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
25 NS_INTERFACE_MAP_END_INHERITING(BaseQueuingStrategy
)
28 already_AddRefed
<ByteLengthQueuingStrategy
>
29 ByteLengthQueuingStrategy::Constructor(const GlobalObject
& aGlobal
,
30 const QueuingStrategyInit
& aInit
) {
31 RefPtr
<ByteLengthQueuingStrategy
> strategy
= new ByteLengthQueuingStrategy(
32 aGlobal
.GetAsSupports(), aInit
.mHighWaterMark
);
33 return strategy
.forget();
36 JSObject
* ByteLengthQueuingStrategy::WrapObject(
37 JSContext
* aCx
, JS::Handle
<JSObject
*> aGivenProto
) {
38 return ByteLengthQueuingStrategy_Binding::Wrap(aCx
, this, aGivenProto
);
41 static bool ByteLengthQueuingStrategySize(JSContext
* cx
, unsigned argc
,
43 // https://streams.spec.whatwg.org/#blqs-internal-slots
44 JS::CallArgs args
= CallArgsFromVp(argc
, vp
);
46 // Step 1: Return ? GetV(chunk, "byteLength").
47 JS::Rooted
<JSObject
*> chunkObj(cx
, JS::ToObject(cx
, args
.get(0)));
52 return JS_GetProperty(cx
, chunkObj
, "byteLength", args
.rval());
55 // https://streams.spec.whatwg.org/#blqs-size
56 already_AddRefed
<Function
> ByteLengthQueuingStrategy::GetSize(
58 // Step 1. Return this's relevant global object's ByteLength queuing strategy
60 if (RefPtr
<Function
> fun
=
61 mGlobal
->GetByteLengthQueuingStrategySizeFunction()) {
65 // Note: Instead of eagerly allocating a size function for every global object
66 // we do it lazily once in this getter.
67 // After this point the steps refer to:
68 // https://streams.spec.whatwg.org/#byte-length-queuing-strategy-size-function
71 if (!jsapi
.Init(mGlobal
)) {
72 aRv
.ThrowUnknownError("Internal error");
75 JSContext
* cx
= jsapi
.cx();
77 // Step 1. Let steps be the following steps, given chunk
78 // Note: See ByteLengthQueuingStrategySize instead.
80 // Step 2. Let F be !CreateBuiltinFunction(steps, 1, "size", « »,
81 // globalObject’s relevant Realm).
82 JS::Rooted
<JSFunction
*> sizeFunction(
83 cx
, JS_NewFunction(cx
, ByteLengthQueuingStrategySize
, 1, 0, "size"));
85 aRv
.StealExceptionFromJSContext(cx
);
89 // Step 3. Set globalObject’s byte length queuing strategy size function to
90 // a Function that represents a reference to F,
91 // with callback context equal to globalObject’s relevant settings object.
92 JS::Rooted
<JSObject
*> funObj(cx
, JS_GetFunctionObject(sizeFunction
));
93 JS::Rooted
<JSObject
*> global(cx
, mGlobal
->GetGlobalJSObject());
94 RefPtr
<Function
> function
= new Function(cx
, funObj
, global
, mGlobal
);
95 mGlobal
->SetByteLengthQueuingStrategySizeFunction(function
);
97 return function
.forget();
100 } // namespace mozilla::dom