aboutsummaryrefslogtreecommitdiff
path: root/src/javascript/jsc/bindings/helpers.h
blob: 5785642dc5c13fed55952c4672ad7fb455348345 (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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#include "headers.h"
#include "root.h"

#include <JavaScriptCore/Error.h>
#include <JavaScriptCore/Exception.h>
#include <JavaScriptCore/Identifier.h>
#include <JavaScriptCore/JSCInlines.h>
#include <JavaScriptCore/JSString.h>
#include <JavaScriptCore/JSValue.h>
#include <JavaScriptCore/ThrowScope.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(ZigType *zig) { cpp = static_cast<CppType *>(static_cast<void *>(&zig)); };

  Wrap(CppType _cpp) {
    auto buffer = alignedBuffer();
    cpp = new (buffer) CppType(_cpp);
  };

  ~Wrap(){};

  unsigned 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 CppType unwrap(ZigType obj) { return *static_cast<CppType *>(static_cast<void *>(&obj)); }

  static CppType *unwrap(ZigType *obj) { return static_cast<CppType *>(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);
}

namespace Zig {

static const JSC::Identifier toIdentifier(ZigString str, JSC::JSGlobalObject *global) {
  if (str.len == 0 || str.ptr == nullptr) { return JSC::Identifier::EmptyIdentifier; }

  return JSC::Identifier::fromString(global->vm(), str.ptr, str.len);
}

static const WTF::String toString(ZigString str) {
  if (str.len == 0 || str.ptr == nullptr) { return WTF::String(); }

  return WTF::String(WTF::StringImpl::createWithoutCopying(str.ptr, str.len));
}

static const WTF::String toStringCopy(ZigString str) {
  if (str.len == 0 || str.ptr == nullptr) { return WTF::String(); }

  return WTF::String(WTF::StringImpl::create(str.ptr, str.len));
}

static WTF::String toStringNotConst(ZigString str) {
  if (str.len == 0 || str.ptr == nullptr) { return WTF::String(); }

  return WTF::String(WTF::StringImpl::createWithoutCopying(str.ptr, str.len));
}

static const JSC::JSString *toJSString(ZigString str, JSC::JSGlobalObject *global) {
  return JSC::jsOwnedString(global->vm(), toString(str));
}

static const ZigString ZigStringEmpty = ZigString{nullptr, 0};
static const unsigned char __dot_char = '.';
static const ZigString ZigStringCwd = ZigString{&__dot_char, 1};

static ZigString toZigString(WTF::String str) {
  return str.isEmpty() ? ZigStringEmpty : ZigString{str.characters8(), str.length()};
}

static ZigString toZigString(WTF::String *str) {
  return str->isEmpty() ? ZigStringEmpty : ZigString{str->characters8(), str->length()};
}

static ZigString toZigString(WTF::StringImpl &str) {
  return str.isEmpty() ? ZigStringEmpty : ZigString{str.characters8(), str.length()};
}

static ZigString toZigString(WTF::StringView &str) {
  return str.isEmpty() ? ZigStringEmpty : ZigString{str.characters8(), str.length()};
}

static ZigString toZigString(JSC::JSString &str, JSC::JSGlobalObject *global) {
  return toZigString(str.value(global));
}

static ZigString toZigString(JSC::JSString *str, JSC::JSGlobalObject *global) {
  return toZigString(str->value(global));
}

static ZigString toZigString(JSC::Identifier &str, JSC::JSGlobalObject *global) {
  return toZigString(str.string());
}

static ZigString toZigString(JSC::Identifier *str, JSC::JSGlobalObject *global) {
  return toZigString(str->string());
}

static WTF::StringView toStringView(ZigString str) { return WTF::StringView(str.ptr, str.len); }

static void throwException(JSC::ThrowScope &scope, ZigErrorType err, JSC::JSGlobalObject *global) {
  scope.throwException(global,
                       JSC::Exception::create(global->vm(), JSC::JSValue((JSC::JSCell *)err.ptr)));
}

static ZigString toZigString(JSC::JSValue val, JSC::JSGlobalObject *global) {
  auto scope = DECLARE_THROW_SCOPE(global->vm());
  WTF::String str = val.toWTFString(global);

  if (scope.exception()) {
    scope.clearException();
    scope.release();
    return ZigStringEmpty;
  }

  scope.release();

  return toZigString(str);
}

}; // namespace Zig