aboutsummaryrefslogtreecommitdiff
path: root/src/javascript/jsc/bindings/helpers.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/javascript/jsc/bindings/helpers.h')
-rw-r--r--src/javascript/jsc/bindings/helpers.h77
1 files changed, 68 insertions, 9 deletions
diff --git a/src/javascript/jsc/bindings/helpers.h b/src/javascript/jsc/bindings/helpers.h
index 73582d1dc..1c1237969 100644
--- a/src/javascript/jsc/bindings/helpers.h
+++ b/src/javascript/jsc/bindings/helpers.h
@@ -1,3 +1,5 @@
+#pragma once
+
#include "headers.h"
#include "root.h"
@@ -10,8 +12,6 @@
#include <JavaScriptCore/ThrowScope.h>
#include <JavaScriptCore/VM.h>
-#pragma once
-
template <class CppType, typename ZigType> class Wrap {
public:
Wrap(){};
@@ -72,46 +72,90 @@ static const JSC::Identifier toIdentifier(ZigString str, JSC::JSGlobalObject *gl
return JSC::Identifier::fromString(global->vm(), str.ptr, str.len);
}
+static bool isTaggedUTF16Ptr(const unsigned char *ptr) {
+ return (reinterpret_cast<uintptr_t>(ptr) & (static_cast<uint64_t>(1) << 63)) != 0;
+}
+
+static bool isTaggedExternalPtr(const unsigned char *ptr) {
+ return (reinterpret_cast<uintptr_t>(ptr) & (static_cast<uint64_t>(1) << 62)) != 0;
+}
+
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));
+ return !isTaggedUTF16Ptr(str.ptr)
+ ? WTF::String(WTF::StringImpl::createWithoutCopying(str.ptr, str.len))
+ : WTF::String(WTF::StringImpl::createWithoutCopying(
+ reinterpret_cast<const UChar *>(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));
+ return !isTaggedUTF16Ptr(str.ptr) ? WTF::String(WTF::StringImpl::create(str.ptr, str.len))
+ : WTF::String(WTF::StringImpl::create(
+ reinterpret_cast<const UChar *>(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));
+ return !isTaggedUTF16Ptr(str.ptr) ? WTF::String(WTF::StringImpl::create(str.ptr, str.len))
+ : WTF::String(WTF::StringImpl::create(
+ reinterpret_cast<const UChar *>(str.ptr), str.len));
}
static const JSC::JSString *toJSString(ZigString str, JSC::JSGlobalObject *global) {
return JSC::jsOwnedString(global->vm(), toString(str));
}
+static const JSC::JSValue toJSStringValue(ZigString str, JSC::JSGlobalObject *global) {
+ return JSC::JSValue(toJSString(str, global));
+}
+
+static const JSC::JSString *toJSStringGC(ZigString str, JSC::JSGlobalObject *global) {
+ return JSC::jsString(global->vm(), toStringCopy(str));
+}
+
+static const JSC::JSValue toJSStringValueGC(ZigString str, JSC::JSGlobalObject *global) {
+ return JSC::JSValue(toJSString(str, global));
+}
+
static const ZigString ZigStringEmpty = ZigString{nullptr, 0};
static const unsigned char __dot_char = '.';
static const ZigString ZigStringCwd = ZigString{&__dot_char, 1};
+static const unsigned char *taggedUTF16Ptr(const UChar *ptr) {
+ return reinterpret_cast<const unsigned char *>(reinterpret_cast<uintptr_t>(ptr) |
+ (static_cast<uint64_t>(1) << 63));
+}
+
static ZigString toZigString(WTF::String str) {
- return str.isEmpty() ? ZigStringEmpty : ZigString{str.characters8(), str.length()};
+ return str.isEmpty()
+ ? ZigStringEmpty
+ : ZigString{str.is8Bit() ? str.characters8() : taggedUTF16Ptr(str.characters16()),
+ str.length()};
}
static ZigString toZigString(WTF::String *str) {
- return str->isEmpty() ? ZigStringEmpty : ZigString{str->characters8(), str->length()};
+ return str->isEmpty()
+ ? ZigStringEmpty
+ : ZigString{str->is8Bit() ? str->characters8() : taggedUTF16Ptr(str->characters16()),
+ str->length()};
}
static ZigString toZigString(WTF::StringImpl &str) {
- return str.isEmpty() ? ZigStringEmpty : ZigString{str.characters8(), str.length()};
+ return str.isEmpty()
+ ? ZigStringEmpty
+ : ZigString{str.is8Bit() ? str.characters8() : taggedUTF16Ptr(str.characters16()),
+ str.length()};
}
static ZigString toZigString(WTF::StringView &str) {
- return str.isEmpty() ? ZigStringEmpty : ZigString{str.characters8(), str.length()};
+ return str.isEmpty()
+ ? ZigStringEmpty
+ : ZigString{str.is8Bit() ? str.characters8() : taggedUTF16Ptr(str.characters16()),
+ str.length()};
}
static ZigString toZigString(JSC::JSString &str, JSC::JSGlobalObject *global) {
@@ -152,4 +196,19 @@ static ZigString toZigString(JSC::JSValue val, JSC::JSGlobalObject *global) {
return toZigString(str);
}
+static JSC::JSValue getErrorInstance(const ZigString *str, JSC__JSGlobalObject *globalObject) {
+ JSC::VM &vm = globalObject->vm();
+
+ auto scope = DECLARE_THROW_SCOPE(vm);
+ JSC::JSValue message = Zig::toJSString(*str, globalObject);
+ JSC::JSValue options = JSC::jsUndefined();
+ JSC::Structure *errorStructure = globalObject->errorStructure();
+ JSC::JSObject *result =
+ JSC::ErrorInstance::create(globalObject, errorStructure, message, options);
+ RETURN_IF_EXCEPTION(scope, JSC::JSValue());
+ scope.release();
+
+ return JSC::JSValue(result);
+}
+
}; // namespace Zig