Files
bun.sh/src/bun.js/bindings/helpers.h
Jarred Sumner 7485c7c7cb feat: Windows + CMake Build System (#4410)
* Prepare for windows event loop

* More progress

* Update libuv.zig

* wip

* Make compiling each dependency a shell script

* Bump mimalloc

* Add the build scripts

* Update settings.json

* Fix a bunch of compiler warnings

* Remove more warnings

* more warnings

* cmake works

* Update JSSQLStatement.h

* put it in the zig file

* Fix usockets warnings

* Fixup

* Fix one of the compiler errors

* chunk

* draw the rest of the owl

* theres more

* Rename Process -> BunProcess

Works around a Windows issue

* Add musl polyfill for memmem on Windows

* More

* 12 mb

* Fix getenvZ

* fix variosu issues

* Add fast-ish path for bun install on Windows

* Update windows.zig

* Update windows.zig

* Fix build issue

* it works

* hmmm

* Rename file

* Fixups

* Update wtf-bindings.cpp

* Update src/bun.js/bindings/headers-handwritten.h

Co-authored-by: Dylan Conway <35280289+dylan-conway@users.noreply.github.com>

* further!

* more

* Update .gitignore

* hm

* quite a lot of fixes

* Update CMakeLists.txt

* zig fmt

* Many more things are starting to work.

* reb

* regenaret

* Update JSSink.h

* fixup

* fetch works

* Bun.serve() and much of the event loop works now

* Make require() work

* bun install progress

* more things work

* use less std.os

* Fixes

* small fixes

* Bump

* Bummp

* Undo that change

* We have to bump the version of Debian because libarchive has a higher minimum requirement

* ok

* some clenaup

* windows

* Update bun.zig

* fixup

* avoid duplicate symbols

* avoid undefined symbols

* bump

* Remove issue template for install

It's not used, and use the bug issue instead.

* Add types for cp and cpSync

* Add types for watchFile and unwatchFile

* Add bun-types to 'bun fmt' script

* Update nodejs compat docs cp/cpSync/watchFile/unwatchFile (#4525)

* feat(fetch) rejectUnauthorized and checkServerIdentity (#4514)

* enable root certs on fetch

* rebase

* fix lookup

* some fixes and improvements

* fmt

* more fixes

* more fixes

* check detached onHandshake

* fix promise case

* fix cert non-Native

* add fetch tls tests

* more one test

* churn

* Update feature_flags.zig

* Update response.zig

* Revert "avoid undefined symbols"

This reverts commit ca835b726f.

* Revert "avoid duplicate symbols"

This reverts commit 4ac6ca8700.

* Update feature_flags.zig

* Set permissions

* more

* Update mimalloc

* Fix sqlite test failures

* Fix some test failures

* Make sure we remove libusockets is removed

* hm

* [dave]: fix webcrypto crash

* bump

* Update index.ts

* windows zig compiles

* cmake on mac works

* progress

* yay

* bun run build

* fix

* ok

* oops

* asdfasfdafdsafda

* fghjkl

* git ignore

* wow

* Process -> BunProcess

* hmm

* blah

* finalize merge

* now it only has linker errors on mac

* sdfadsf

* g

* getting farther

* sxdcvbnmk,

* adfhjskfjdhkas

* a

* fgh

* update build dot zig

* asdfg

* theoretical -DCANARY flag we can use

* asdf

* cool

* okay

* colorterm

* New build workflow

* Fix script

* Use sudo

* More sudo

* Tweak dependencies

* Another sudo attempt

* Tweak script

* 16.0 -> 16

* Tweak script

* Tweak script

* Tweak script

* Tweak script

* Tweak script

* bun install

* ssh into github actions

* add more to ssh

* Fix postinstal

* Skip llvm

* New dockerfile

* Build

* More changes to Dockerfile

* chaos chaos chaos

* okay

* a

* more cmake nonsense

* add unified sources code (does not work)

* stuff

* prepare for CI builds

* ok

* yay

* yeah

* make this more stable simply by trying again if it fails, 5 times, then lose. it fixes the stability issue i was running into L O L

* messing with ci

* x

* a

* clean dependencies before build

* oops

* this is not going to work but its closer

* not gonna work either

* a

* a

* did i do it

* a

* a

* work around weird fs+Bun.build issues

* properly pass debug flag correctly

* idk im sorry

* lose

* maybe

* run the tests please

* a

* fix zlib script

* a

* hi

* prevent stupid ci issue

* i totally didnt leave in a syntax error on cmakelists

* a

* lol

* relax

* 😭

* a

* SO SILLY

* 😡 one line mistake

* one character diff

* fix linking symbols missing

* work on dependency scripts

* does this work now?

* fix mac build

* a

* bump!

* woops

* add macos baseline build

* .

* fix sqlite and also enable $assert/$debug support in builtin functions

* okay

* oops

* zig upgrade lol

* Merge

* Fix spawn test issue

* Set a timeout

* yeah

* etc

* mi

---------

Co-authored-by: Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com>
Co-authored-by: Dylan Conway <dylan.conway567@gmail.com>
Co-authored-by: Dylan Conway <35280289+dylan-conway@users.noreply.github.com>
Co-authored-by: Ashcon Partovi <ashcon@partovi.net>
Co-authored-by: Birk Skyum <74932975+birkskyum@users.noreply.github.com>
Co-authored-by: dave caruso <me@paperdave.net>
2023-10-27 01:51:56 -07:00

417 lines
13 KiB
C++

#pragma once
#include "root.h"
#include <JavaScriptCore/Error.h>
#include <JavaScriptCore/Exception.h>
#include <JavaScriptCore/Identifier.h>
#include <JavaScriptCore/JSGlobalObject.h>
#include <JavaScriptCore/JSString.h>
#include <JavaScriptCore/ThrowScope.h>
#include <JavaScriptCore/VM.h>
using JSC__JSGlobalObject = JSC::JSGlobalObject;
using JSC__JSValue = JSC::EncodedJSValue;
using JSC__CallFrame = JSC::CallFrame;
namespace Zig {
}
#include "headers-handwritten.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));
}
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 {
// 8 bit byte
// we tag the final two bits
// so 56 bits are copied over
// rest we zero out for consistentcy
static const unsigned char* untag(const unsigned char* ptr)
{
return reinterpret_cast<const unsigned char*>(
(((reinterpret_cast<uintptr_t>(ptr) & ~(static_cast<uint64_t>(1) << 63) & ~(static_cast<uint64_t>(1) << 62)) & ~(static_cast<uint64_t>(1) << 61)) & ~(static_cast<uint64_t>(1) << 60)));
}
static void* untagVoid(const unsigned char* ptr)
{
return const_cast<void*>(reinterpret_cast<const void*>(untag(ptr)));
}
static void* untagVoid(const char16_t* ptr)
{
return untagVoid(reinterpret_cast<const unsigned char*>(ptr));
}
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(), untag(str.ptr), str.len);
}
static bool isTaggedUTF16Ptr(const unsigned char* ptr)
{
return (reinterpret_cast<uintptr_t>(ptr) & (static_cast<uint64_t>(1) << 63)) != 0;
}
// Do we need to convert the string from UTF-8 to UTF-16?
static bool isTaggedUTF8Ptr(const unsigned char* ptr)
{
return (reinterpret_cast<uintptr_t>(ptr) & (static_cast<uint64_t>(1) << 61)) != 0;
}
static bool isTaggedExternalPtr(const unsigned char* ptr)
{
return (reinterpret_cast<uintptr_t>(ptr) & (static_cast<uint64_t>(1) << 62)) != 0;
}
static void free_global_string(void* str, void* ptr, unsigned len)
{
// i don't understand why this happens
if (ptr == nullptr)
return;
ZigString__free_global(reinterpret_cast<const unsigned char*>(ptr), len);
}
// Switching to AtomString doesn't yield a perf benefit because we're recreating it each time.
static const WTF::String toString(ZigString str)
{
if (str.len == 0 || str.ptr == nullptr) {
return WTF::String();
}
if (UNLIKELY(isTaggedUTF8Ptr(str.ptr))) {
return WTF::String::fromUTF8(untag(str.ptr), str.len);
}
if (UNLIKELY(isTaggedExternalPtr(str.ptr))) {
return !isTaggedUTF16Ptr(str.ptr)
? WTF::String(WTF::ExternalStringImpl::create(untag(str.ptr), str.len, untagVoid(str.ptr), free_global_string))
: WTF::String(WTF::ExternalStringImpl::create(
reinterpret_cast<const UChar*>(untag(str.ptr)), str.len, untagVoid(str.ptr), free_global_string));
}
return !isTaggedUTF16Ptr(str.ptr)
? WTF::String(WTF::StringImpl::createWithoutCopying(untag(str.ptr), str.len))
: WTF::String(WTF::StringImpl::createWithoutCopying(
reinterpret_cast<const UChar*>(untag(str.ptr)), str.len));
}
static WTF::AtomString toAtomString(ZigString str)
{
if (!isTaggedUTF16Ptr(str.ptr)) {
return makeAtomString(untag(str.ptr), str.len);
} else {
return makeAtomString(reinterpret_cast<const UChar*>(untag(str.ptr)), str.len);
}
}
static const WTF::String toString(ZigString str, StringPointer ptr)
{
if (str.len == 0 || str.ptr == nullptr || ptr.len == 0) {
return WTF::String();
}
if (UNLIKELY(isTaggedUTF8Ptr(str.ptr))) {
return WTF::String::fromUTF8(&untag(str.ptr)[ptr.off], ptr.len);
}
return !isTaggedUTF16Ptr(str.ptr)
? WTF::String(WTF::StringImpl::createWithoutCopying(&untag(str.ptr)[ptr.off], ptr.len))
: WTF::String(WTF::StringImpl::createWithoutCopying(
&reinterpret_cast<const UChar*>(untag(str.ptr))[ptr.off], ptr.len));
}
static const WTF::String toStringCopy(ZigString str, StringPointer ptr)
{
if (str.len == 0 || str.ptr == nullptr || ptr.len == 0) {
return WTF::String();
}
if (UNLIKELY(isTaggedUTF8Ptr(str.ptr))) {
return WTF::String::fromUTF8(&untag(str.ptr)[ptr.off], ptr.len);
}
return !isTaggedUTF16Ptr(str.ptr)
? WTF::String(WTF::StringImpl::create(&untag(str.ptr)[ptr.off], ptr.len))
: WTF::String(WTF::StringImpl::create(
&reinterpret_cast<const UChar*>(untag(str.ptr))[ptr.off], ptr.len));
}
static const WTF::String toStringCopy(ZigString str)
{
if (str.len == 0 || str.ptr == nullptr) {
return WTF::String();
}
if (UNLIKELY(isTaggedUTF8Ptr(str.ptr))) {
return WTF::String::fromUTF8(untag(str.ptr), str.len);
}
if (isTaggedUTF16Ptr(str.ptr)) {
UChar* out = nullptr;
auto impl = WTF::StringImpl::tryCreateUninitialized(str.len, out);
if (UNLIKELY(!impl))
return WTF::String();
memcpy(out, untag(str.ptr), str.len * sizeof(UChar));
return WTF::String(WTFMove(impl));
} else {
LChar* out = nullptr;
auto impl = WTF::StringImpl::tryCreateUninitialized(str.len, out);
if (UNLIKELY(!impl))
return WTF::String();
memcpy(out, untag(str.ptr), str.len * sizeof(LChar));
return WTF::String(WTFMove(impl));
}
}
static WTF::String toStringNotConst(ZigString str) { return toString(str); }
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(toJSStringGC(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 BunString BunStringCwd = BunString { BunStringTag::StaticZigString, ZigStringCwd };
static const BunString BunStringEmpty = BunString { BunStringTag::Empty, nullptr };
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->is8Bit() ? str->characters8() : taggedUTF16Ptr(str->characters16()),
str->length() };
}
static ZigString toZigString(WTF::StringImpl& str)
{
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.is8Bit() ? str.characters8() : taggedUTF16Ptr(str.characters16()),
str.length() };
}
static ZigString toZigString(const WTF::StringView& str)
{
return str.isEmpty()
? ZigStringEmpty
: ZigString { str.is8Bit() ? str.characters8() : taggedUTF16Ptr(str.characters16()),
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(untag(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);
}
static const WTF::String toStringStatic(ZigString str)
{
if (str.len == 0 || str.ptr == nullptr) {
return WTF::String();
}
if (UNLIKELY(isTaggedUTF8Ptr(str.ptr))) {
abort();
}
if (isTaggedUTF16Ptr(str.ptr)) {
return WTF::String(AtomStringImpl::add(reinterpret_cast<const UChar*>(untag(str.ptr)), str.len));
}
return WTF::String(AtomStringImpl::add(
reinterpret_cast<const LChar*>(untag(str.ptr)), str.len));
}
static JSC::JSValue getErrorInstance(const ZigString* str, JSC__JSGlobalObject* globalObject)
{
JSC::VM& vm = globalObject->vm();
JSC::JSObject* result = JSC::createError(globalObject, toStringCopy(*str));
JSC::EnsureStillAliveScope ensureAlive(result);
return JSC::JSValue(result);
}
static JSC::JSValue getTypeErrorInstance(const ZigString* str, JSC__JSGlobalObject* globalObject)
{
JSC::VM& vm = globalObject->vm();
JSC::JSObject* result = JSC::createTypeError(globalObject, toStringCopy(*str));
JSC::EnsureStillAliveScope ensureAlive(result);
return JSC::JSValue(result);
}
static JSC::JSValue getSyntaxErrorInstance(const ZigString* str, JSC__JSGlobalObject* globalObject)
{
JSC::VM& vm = globalObject->vm();
JSC::JSObject* result = JSC::createSyntaxError(globalObject, toStringCopy(*str));
JSC::EnsureStillAliveScope ensureAlive(result);
return JSC::JSValue(result);
}
static JSC::JSValue getRangeErrorInstance(const ZigString* str, JSC__JSGlobalObject* globalObject)
{
JSC::VM& vm = globalObject->vm();
JSC::JSObject* result = JSC::createRangeError(globalObject, toStringCopy(*str));
JSC::EnsureStillAliveScope ensureAlive(result);
return JSC::JSValue(result);
}
}; // namespace Zig
JSC::JSValue createSystemError(JSC::JSGlobalObject* global, ASCIILiteral message, ASCIILiteral syscall, int err);
JSC::JSValue createSystemError(JSC::JSGlobalObject* global, ASCIILiteral syscall, int err);
static void throwSystemError(JSC::ThrowScope& scope, JSC::JSGlobalObject* globalObject, ASCIILiteral syscall, int err)
{
scope.throwException(globalObject, createSystemError(globalObject, syscall, err));
}
static void throwSystemError(JSC::ThrowScope& scope, JSC::JSGlobalObject* globalObject, ASCIILiteral message, ASCIILiteral syscall, int err)
{
scope.throwException(globalObject, createSystemError(globalObject, message, syscall, err));
}
template<typename WebCoreType, typename OutType>
OutType* WebCoreCast(JSC__JSValue JSValue0)
{
// we must use jsDynamicCast here so that we check that the type is correct
WebCoreType* jsdomURL = JSC::jsDynamicCast<WebCoreType*>(JSC::JSValue::decode(JSValue0));
if (jsdomURL == nullptr) {
return nullptr;
}
return reinterpret_cast<OutType*>(&jsdomURL->wrapped());
}