diff options
Diffstat (limited to 'src/javascript/jsc/bindings/helpers.h')
-rw-r--r-- | src/javascript/jsc/bindings/helpers.h | 197 |
1 files changed, 82 insertions, 115 deletions
diff --git a/src/javascript/jsc/bindings/helpers.h b/src/javascript/jsc/bindings/helpers.h index 591f9e4e4..a24ecefcb 100644 --- a/src/javascript/jsc/bindings/helpers.h +++ b/src/javascript/jsc/bindings/helpers.h @@ -1,182 +1,149 @@ #include "headers.h" #include "root.h" -#include <JavaScriptCore/JSCInlines.h> -#include <JavaScriptCore/VM.h> +#include <JavaScriptCore/Error.h> +#include <JavaScriptCore/Exception.h> #include <JavaScriptCore/Identifier.h> -#include <JavaScriptCore/JSValue.h> +#include <JavaScriptCore/JSCInlines.h> #include <JavaScriptCore/JSString.h> +#include <JavaScriptCore/JSValue.h> #include <JavaScriptCore/ThrowScope.h> -#include <JavaScriptCore/Error.h> -#include <JavaScriptCore/Exception.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)); - }; - +#include <JavaScriptCore/VM.h> - Wrap(CppType _cpp){ - auto buffer = alignedBuffer(); - cpp = new (buffer) CppType(_cpp); - }; +template <class CppType, typename ZigType> class Wrap { + public: + Wrap(){}; + Wrap(ZigType zig) { + result = zig; + cpp = static_cast<CppType *>(static_cast<void *>(&zig)); + }; - ~Wrap(){}; + Wrap(ZigType *zig) { cpp = static_cast<CppType *>(static_cast<void *>(&zig)); }; - unsigned char* alignedBuffer() { - return result.bytes + alignof(CppType) - reinterpret_cast<intptr_t>(result.bytes) % alignof(CppType); - } + Wrap(CppType _cpp) { + auto buffer = alignedBuffer(); + cpp = new (buffer) CppType(_cpp); + }; - ZigType result; - CppType* cpp; + ~Wrap(){}; - static ZigType wrap(CppType obj) { - return *static_cast<ZigType*>(static_cast<void*>(&obj)); - } + unsigned char *alignedBuffer() { + return result.bytes + alignof(CppType) - + reinterpret_cast<intptr_t>(result.bytes) % alignof(CppType); + } - static CppType unwrap(ZigType obj) { - return *static_cast<CppType*>(static_cast<void*>(&obj)); - } + ZigType result; + CppType *cpp; - static CppType* unwrap(ZigType* obj) { - return static_cast<CppType*>(static_cast<void*>(obj)); - } + 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 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)); +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); +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])); - } +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); + 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(); } -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); + return WTF::String(WTF::StringImpl::createWithoutCopying(str.ptr, str.len)); } -static const WTF::String toString(ZigString str) { - if (str.len == 0 || str.ptr == nullptr) { - return WTF::String(); - } +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)); + 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 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 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() }; + 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::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::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(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::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 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, ZigString msg, JSC::JSGlobalObject* global) { - auto str = toJSString(msg, global); - scope.throwException(global, JSC::Exception::create(global->vm(), JSC::JSValue(str))); +static void throwException(JSC::ThrowScope &scope, ZigString msg, JSC::JSGlobalObject *global) { + auto str = toJSString(msg, global); + scope.throwException(global, JSC::Exception::create(global->vm(), JSC::JSValue(str))); } +static ZigString toZigString(JSC::JSValue val, JSC::JSGlobalObject *global) { + auto scope = DECLARE_THROW_SCOPE(global->vm()); + WTF::String str = val.toWTFString(global); - -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; - } - + if (scope.exception()) { + scope.clearException(); scope.release(); + return ZigStringEmpty; + } + scope.release(); - return toZigString(str); + return toZigString(str); } - -}
\ No newline at end of file +static ZigException ZigExceptionNone = ZigException{ + 0, 0, ZigStringEmpty, ZigStringEmpty, ZigStringEmpty, -1, -1, ZigStringEmpty, nullptr}; +} // namespace Zig |