aboutsummaryrefslogtreecommitdiff
path: root/src/bun.js/bindings/JSBufferList.h
blob: 67d8d21f3f7aea5bf631df03d6ec7b281eb11495 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#pragma once

#include "root.h"
#include "wtf/Deque.h"

namespace WebCore {
using namespace JSC;

class JSBufferList : public JSC::JSNonFinalObject {
    using Base = JSC::JSNonFinalObject;

public:
    JSBufferList(JSC::VM& vm, JSC::Structure* structure)
        : Base(vm, structure)
    {
    }

    DECLARE_INFO;

    static constexpr unsigned StructureFlags = Base::StructureFlags;

    template<typename, JSC::SubspaceAccess mode> static JSC::GCClient::IsoSubspace* subspaceFor(JSC::VM& vm)
    {
        if constexpr (mode == JSC::SubspaceAccess::Concurrently)
            return nullptr;
        return subspaceForImpl(vm);
    }

    static JSC::GCClient::IsoSubspace* subspaceForImpl(JSC::VM& vm);

    static JSC::Structure* createStructure(JSC::VM& vm, JSC::JSGlobalObject* globalObject,
        JSC::JSValue prototype)
    {
        return JSC::Structure::create(vm, globalObject, prototype,
            JSC::TypeInfo(JSC::ObjectType, StructureFlags), info());
    }

    static JSBufferList* create(JSC::VM& vm, JSC::JSGlobalObject* globalObject, JSC::Structure* structure)
    {
        JSBufferList* accessor = new (NotNull, JSC::allocateCell<JSBufferList>(vm)) JSBufferList(vm, structure);
        accessor->finishCreation(vm, globalObject);
        return accessor;
    }

    void finishCreation(JSC::VM& vm, JSC::JSGlobalObject* globalObject);
    static void destroy(JSCell*) {}

    size_t length() { return m_deque.size(); }
    void push(JSC::VM& vm, JSC::JSValue v)
    {
        m_deque.append(WriteBarrier<Unknown>());
        m_deque.last().set(vm, this, v);
    }
    void unshift(JSC::VM& vm, JSC::JSValue v)
    {
        m_deque.prepend(WriteBarrier<Unknown>());
        m_deque.first().set(vm, this, v);
    }
    JSC::JSValue shift()
    {
        if (UNLIKELY(length() == 0))
            return JSC::jsUndefined();
        auto v = m_deque.first().get();
        m_deque.removeFirst();
        return v;
    }
    void clear()
    {
        m_deque.clear();
    }
    JSC::JSValue first()
    {
        if (UNLIKELY(length() == 0))
            return JSC::jsUndefined();
        return JSC::JSValue(m_deque.first().get());
    }

    JSC::JSValue concat(JSC::VM&, JSC::JSGlobalObject*, int32_t);
    JSC::JSValue join(JSC::VM&, JSC::JSGlobalObject*, JSString*);
    JSC::JSValue consume(JSC::VM&, JSC::JSGlobalObject*, int32_t, bool);
    JSC::JSValue _getBuffer(JSC::VM&, JSC::JSGlobalObject*, int32_t);
    JSC::JSValue _getString(JSC::VM&, JSC::JSGlobalObject*, int32_t);

private:
    Deque<WriteBarrier<Unknown>> m_deque;
};

EncodedJSValue constructJSBufferList(JSGlobalObject* lexicalGlobalObject, CallFrame* callFrame);

}