aboutsummaryrefslogtreecommitdiff
path: root/src/javascript/jsc/bindings/helpers.h
blob: c21f3fb3ca0d9618b220e2bda75e9c5ad0c558b8 (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

#include "headers.h"
#include "root.h"

#include <JavaScriptCore/JSCInlines.h>
#include <JavaScriptCore/VM.h>


template<class CppType, typename ZigType>
class Wrap  {
public:
    Wrap(){
    };

    Wrap(ZigType zig){
        result = zig;
        cpp = static_cast<CppType*>(static_cast<void*>(&zig));
    };

    Wrap(CppType _cpp){
        char* buffer = alignedBuffer();
        memcpy(buffer, std::move(reinterpret_cast<char*>(reinterpret_cast<void*>(&_cpp))), sizeof(CppType));
        cpp = reinterpret_cast<CppType*>(buffer);
    };


    ~Wrap(){};

    char* alignedBuffer() {
        return result.bytes + alignof(CppType) - reinterpret_cast<intptr_t>(result.bytes) % alignof(CppType);
    }

    ZigType result;
    CppType* cpp;

    static ZigType wrap(CppType obj) {
        return *static_cast<ZigType*>(static_cast<void*>(&obj));
    }

    static ZigType wrap(CppType* obj) {
        return *static_cast<ZigType*>(static_cast<void*>(obj));
    }
};





template<class To, class From>
To cast(From v)
{
    return *static_cast<To*>(static_cast<void*>(v));
}

template<class To, class From>
To ccast(From v)
{
    return *static_cast<const To*>(static_cast<const void*>(v));
}

typedef JSC__JSValue (* NativeCallbackFunction)(void* arg0, JSC__JSGlobalObject* arg1, JSC__CallFrame* arg2);

static const JSC::ArgList makeArgs(JSC__JSValue* v, size_t count) {
    JSC::MarkedArgumentBuffer args = JSC::MarkedArgumentBuffer();
    args.ensureCapacity(count);
    for (size_t i = 0; i < count; ++i) {
        args.append(JSC::JSValue::decode(v[i]));
    }

    return JSC::ArgList(args);
}