Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WebKit upgrade #12246

Merged
merged 54 commits into from
Jul 2, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
54 commits
Select commit Hold shift + click to select a range
3c9719c
Replace ZigString.toValue, ZigString.toValueAuto, ZigString.toValueGC…
Jarred-Sumner Jun 29, 2024
880b394
Update bindings.zig
Jarred-Sumner Jun 29, 2024
6b012ab
Update bindings.zig
Jarred-Sumner Jun 29, 2024
a1d49f9
WIP
Jarred-Sumner Jun 29, 2024
30288e3
so many changes
Jarred-Sumner Jun 29, 2024
7d2ab05
Almost odne
Jarred-Sumner Jun 29, 2024
dd53d29
alright it ocmpiled
Jarred-Sumner Jun 29, 2024
9f6d785
Fixup
Jarred-Sumner Jun 29, 2024
9ee7b3b
Bump
Jarred-Sumner Jun 29, 2024
44f8330
wip
Jarred-Sumner Jun 29, 2024
44ae3bb
Merge branch 'main' into jarred/zig-upgrade
Jarred-Sumner Jun 29, 2024
8b45d0d
Apply formatting changes
Jarred-Sumner Jun 29, 2024
43f416b
Update CMakeLists.txt
Jarred-Sumner Jun 29, 2024
352a15f
Bump
Jarred-Sumner Jun 30, 2024
cd71b5b
Apply formatting changes
Jarred-Sumner Jun 30, 2024
84d2380
Redo Asymmetric matchers
Jarred-Sumner Jun 30, 2024
4fdf03e
Add `bun run zig-check-windows` to check if windows passes sema
Jarred-Sumner Jun 30, 2024
01e423f
Mark more JSC.conv
Jarred-Sumner Jun 30, 2024
d744ce1
wrap everything
Jarred-Sumner Jun 30, 2024
368ccde
more
Jarred-Sumner Jun 30, 2024
074c84a
more conv
Jarred-Sumner Jun 30, 2024
72c933c
Update BunObject+exports.h
Jarred-Sumner Jun 30, 2024
6f0d912
Update base.zig
Jarred-Sumner Jun 30, 2024
d5419f9
move some files
Jarred-Sumner Jun 30, 2024
fa1c315
wip
Jarred-Sumner Jun 30, 2024
6461c4b
wip
Jarred-Sumner Jun 30, 2024
ab6601e
Move path into its own file
Jarred-Sumner Jun 30, 2024
697e9b7
It successfully compiled on Windows!
Jarred-Sumner Jun 30, 2024
e877d2d
wip
Jarred-Sumner Jul 1, 2024
53e3ce0
hm
Jarred-Sumner Jul 1, 2024
bed7243
wip
Jarred-Sumner Jul 1, 2024
1897e0f
Cmake
Jarred-Sumner Jul 1, 2024
0c7bfd4
Remove flag
Jarred-Sumner Jul 1, 2024
08c4b4e
cmake
Jarred-Sumner Jul 1, 2024
bf5872d
Some fixes
Jarred-Sumner Jul 1, 2024
59724ef
Update
Jarred-Sumner Jul 1, 2024
e003a90
Apply formatting changes
Jarred-Sumner Jul 1, 2024
fb9f7a5
Deflake fetch-gzip test on Windows
Jarred-Sumner Jul 1, 2024
5ddd623
Fix various things
Jarred-Sumner Jul 1, 2024
47e40f3
bump
Jarred-Sumner Jul 1, 2024
7aa72a7
Apply formatting changes
Jarred-Sumner Jul 1, 2024
05e3b9a
Remove the callconvs in most cases
Jarred-Sumner Jul 1, 2024
bfde2af
lots
Jarred-Sumner Jul 1, 2024
ed382e5
many
Jarred-Sumner Jul 1, 2024
3576965
Fix more tests
Jarred-Sumner Jul 1, 2024
e6cd552
increase timeout
Jarred-Sumner Jul 1, 2024
05e1f77
Get FFI to work again
Jarred-Sumner Jul 1, 2024
42f1fc3
Prettier logs
Jarred-Sumner Jul 2, 2024
89747d4
Fix rare race condition when starting HTTP thread
Jarred-Sumner Jul 2, 2024
3f8597b
Fix rare race condition when calling `Bun.file().{text,arrayBuffer,*}()`
Jarred-Sumner Jul 2, 2024
78bbf5b
Attempt to workaround https://github.com/ziglang/zig/issues/20472
Jarred-Sumner Jul 2, 2024
71ec236
Remove unnecessary callconv usages
Jarred-Sumner Jul 2, 2024
ec426f7
fixups
Jarred-Sumner Jul 2, 2024
3172062
1 more
Jarred-Sumner Jul 2, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Get FFI to work again
  • Loading branch information
Jarred-Sumner committed Jul 1, 2024
commit 05e1f777fa47e6a66bf816b92cf656efe9239d71
34 changes: 29 additions & 5 deletions src/bun.js/bindings/JSFFIFunction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,23 +115,26 @@ extern "C" void Bun__untrackFFIFunction(Zig::GlobalObject* globalObject, JSC::En
}
extern "C" JSC::EncodedJSValue Bun__CreateFFIFunctionValue(Zig::GlobalObject* globalObject, const ZigString* symbolName, unsigned argCount, Zig::FFIFunction functionPointer, bool strong, bool addPtrField)
{
auto* function = Bun__CreateFFIFunction(globalObject, symbolName, argCount, functionPointer, strong);
if (addPtrField) {
auto* function = Zig::JSFFIFunction::createForFFI(globalObject->vm(), globalObject, argCount, symbolName != nullptr ? Zig::toStringCopy(*symbolName) : String(), reinterpret_cast<Bun::CFFIFunction>(functionPointer));
auto& vm = globalObject->vm();
// We should only expose the "ptr" field when it's a JSCallback for bun:ffi.
// Not for internal usages of this function type.
// We should also consider a separate JSFunction type for our usage to not have this branch in the first place...
function->putDirect(vm, JSC::Identifier::fromString(vm, String(MAKE_STATIC_STRING_IMPL("ptr"))), JSC::jsNumber(bitwise_cast<double>(functionPointer)), JSC::PropertyAttribute::ReadOnly | 0);

return JSC::JSValue::encode(function);
}
return JSC::JSValue::encode(function);

return Bun__CreateFFIFunctionWithDataValue(globalObject, symbolName, argCount, functionPointer, strong, nullptr);
}

namespace Zig {
using namespace JSC;

const ClassInfo JSFFIFunction::s_info = { "Function"_s, &Base::s_info, nullptr, nullptr, CREATE_METHOD_TABLE(JSFFIFunction) };

JSFFIFunction::JSFFIFunction(VM& vm, NativeExecutable* executable, JSGlobalObject* globalObject, Structure* structure, FFIFunction&& function)
JSFFIFunction::JSFFIFunction(VM& vm, NativeExecutable* executable, JSGlobalObject* globalObject, Structure* structure, CFFIFunction&& function)
: Base(vm, executable, globalObject, structure)
, m_function(WTFMove(function))
{
Expand All @@ -157,11 +160,32 @@ void JSFFIFunction::finishCreation(VM& vm, NativeExecutable* executable, unsigne

JSFFIFunction* JSFFIFunction::create(VM& vm, Zig::GlobalObject* globalObject, unsigned length, const String& name, FFIFunction FFIFunction, Intrinsic intrinsic, NativeFunction nativeConstructor)
{

NativeExecutable* executable = vm.getHostFunction(FFIFunction, ImplementationVisibility::Public, intrinsic, FFIFunction, nullptr, name);
Structure* structure = globalObject->FFIFunctionStructure();
JSFFIFunction* function = new (NotNull, allocateCell<JSFFIFunction>(vm)) JSFFIFunction(vm, executable, globalObject, structure, reinterpret_cast<CFFIFunction>(WTFMove(FFIFunction)));
function->finishCreation(vm, executable, length, name);
return function;
}

#if OS(WINDOWS)

JSC_DEFINE_HOST_FUNCTION(JSFFIFunction::trampoline, (JSC::JSGlobalObject * globalObject, JSC::CallFrame* callFrame))
{
const auto* function = jsCast<JSFFIFunction*>(callFrame->jsCallee());
return function->function()(globalObject, callFrame);
}

#endif

JSFFIFunction* JSFFIFunction::createForFFI(VM& vm, Zig::GlobalObject* globalObject, unsigned length, const String& name, CFFIFunction FFIFunction)
{
#if OS(WINDOWS)
NativeExecutable* executable = vm.getHostFunction(trampoline, ImplementationVisibility::Public, NoIntrinsic, trampoline, nullptr, name);
#else
NativeExecutable* executable = vm.getHostFunction(FFIFunction, ImplementationVisibility::Public, NoIntrinsic, FFIFunction, nullptr, name);
#endif
Structure* structure = globalObject->FFIFunctionStructure();
JSFFIFunction* function = new (NotNull, allocateCell<JSFFIFunction>(vm)) JSFFIFunction(vm, executable, globalObject, structure, WTFMove(FFIFunction));
JSFFIFunction* function = new (NotNull, allocateCell<JSFFIFunction>(vm)) JSFFIFunction(vm, executable, globalObject, structure, reinterpret_cast<CFFIFunction>(WTFMove(FFIFunction)));
function->finishCreation(vm, executable, length, name);
return function;
}
Expand Down
13 changes: 10 additions & 3 deletions src/bun.js/bindings/JSFFIFunction.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,23 +70,30 @@ class JSFFIFunction final : public JSC::JSFunction {
DECLARE_EXPORT_INFO;

JS_EXPORT_PRIVATE static JSFFIFunction* create(VM&, Zig::GlobalObject*, unsigned length, const String& name, FFIFunction, Intrinsic = NoIntrinsic, NativeFunction nativeConstructor = callHostFunctionAsConstructor);
JS_EXPORT_PRIVATE static JSFFIFunction* createForFFI(VM&, Zig::GlobalObject*, unsigned length, const String& name, CFFIFunction);

static Structure* createStructure(VM& vm, JSGlobalObject* globalObject, JSValue prototype)
{
ASSERT(globalObject);
return Structure::create(vm, globalObject, prototype, TypeInfo(JSFunctionType, StructureFlags), info());
}

const FFIFunction function() { return m_function; }
const CFFIFunction function() const { return m_function; }

#if OS(WINDOWS)

static JSC::EncodedJSValue JSC_HOST_CALL_ATTRIBUTES trampoline(JSGlobalObject* globalObject, CallFrame* callFrame);

#endif

void* dataPtr;

private:
JSFFIFunction(VM&, NativeExecutable*, JSGlobalObject*, Structure*, FFIFunction&&);
JSFFIFunction(VM&, NativeExecutable*, JSGlobalObject*, Structure*, CFFIFunction&&);
void finishCreation(VM&, NativeExecutable*, unsigned length, const String& name);
DECLARE_VISIT_CHILDREN;

FFIFunction m_function;
CFFIFunction m_function;
};

} // namespace JSC
Expand Down
152 changes: 80 additions & 72 deletions test/js/bun/ffi/ffi-test.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,60 +3,67 @@
#include <stdio.h>
#include <stdlib.h>

bool returns_true();
bool returns_false();
char returns_42_char();
float returns_42_float();
double returns_42_double();
uint8_t returns_42_uint8_t();
int8_t returns_neg_42_int8_t();
uint16_t returns_42_uint16_t();
uint32_t returns_42_uint32_t();
uint64_t returns_42_uint64_t();
int16_t returns_neg_42_int16_t();
int32_t returns_neg_42_int32_t();
int64_t returns_neg_42_int64_t();
#ifdef _WIN32
#define FFI_EXPORT __declspec(dllexport)
#else
#define FFI_EXPORT __attribute__((visibility("default")))
#endif

bool cb_identity_true(bool (*cb)());
bool cb_identity_false(bool (*cb)());
char cb_identity_42_char(char (*cb)());
float cb_identity_42_float(float (*cb)());
double cb_identity_42_double(double (*cb)());
uint8_t cb_identity_42_uint8_t(uint8_t (*cb)());
int8_t cb_identity_neg_42_int8_t(int8_t (*cb)());
uint16_t cb_identity_42_uint16_t(uint16_t (*cb)());
uint32_t cb_identity_42_uint32_t(uint32_t (*cb)());
uint64_t cb_identity_42_uint64_t(uint64_t (*cb)());
int16_t cb_identity_neg_42_int16_t(int16_t (*cb)());
int32_t cb_identity_neg_42_int32_t(int32_t (*cb)());
int64_t cb_identity_neg_42_int64_t(int64_t (*cb)());
FFI_EXPORT bool returns_true();
FFI_EXPORT bool returns_false();
FFI_EXPORT char returns_42_char();
FFI_EXPORT float returns_42_float();
FFI_EXPORT double returns_42_double();
FFI_EXPORT uint8_t returns_42_uint8_t();
FFI_EXPORT int8_t returns_neg_42_int8_t();
FFI_EXPORT uint16_t returns_42_uint16_t();
FFI_EXPORT uint32_t returns_42_uint32_t();
FFI_EXPORT uint64_t returns_42_uint64_t();
FFI_EXPORT int16_t returns_neg_42_int16_t();
FFI_EXPORT int32_t returns_neg_42_int32_t();
FFI_EXPORT int64_t returns_neg_42_int64_t();

bool identity_bool_true();
bool identity_bool_false();
char identity_char(char a);
float identity_float(float a);
bool identity_bool(bool ident);
double identity_double(double a);
int8_t identity_int8_t(int8_t a);
int16_t identity_int16_t(int16_t a);
int32_t identity_int32_t(int32_t a);
int64_t identity_int64_t(int64_t a);
uint8_t identity_uint8_t(uint8_t a);
uint16_t identity_uint16_t(uint16_t a);
uint32_t identity_uint32_t(uint32_t a);
uint64_t identity_uint64_t(uint64_t a);
FFI_EXPORT bool cb_identity_true(bool (*cb)());
FFI_EXPORT bool cb_identity_false(bool (*cb)());
FFI_EXPORT char cb_identity_42_char(char (*cb)());
FFI_EXPORT float cb_identity_42_float(float (*cb)());
FFI_EXPORT double cb_identity_42_double(double (*cb)());
FFI_EXPORT uint8_t cb_identity_42_uint8_t(uint8_t (*cb)());
FFI_EXPORT int8_t cb_identity_neg_42_int8_t(int8_t (*cb)());
FFI_EXPORT uint16_t cb_identity_42_uint16_t(uint16_t (*cb)());
FFI_EXPORT uint32_t cb_identity_42_uint32_t(uint32_t (*cb)());
FFI_EXPORT uint64_t cb_identity_42_uint64_t(uint64_t (*cb)());
FFI_EXPORT int16_t cb_identity_neg_42_int16_t(int16_t (*cb)());
FFI_EXPORT int32_t cb_identity_neg_42_int32_t(int32_t (*cb)());
FFI_EXPORT int64_t cb_identity_neg_42_int64_t(int64_t (*cb)());

char add_char(char a, char b);
float add_float(float a, float b);
double add_double(double a, double b);
int8_t add_int8_t(int8_t a, int8_t b);
int16_t add_int16_t(int16_t a, int16_t b);
int32_t add_int32_t(int32_t a, int32_t b);
int64_t add_int64_t(int64_t a, int64_t b);
uint8_t add_uint8_t(uint8_t a, uint8_t b);
uint16_t add_uint16_t(uint16_t a, uint16_t b);
uint32_t add_uint32_t(uint32_t a, uint32_t b);
uint64_t add_uint64_t(uint64_t a, uint64_t b);
FFI_EXPORT bool identity_bool_true();
FFI_EXPORT bool identity_bool_false();
FFI_EXPORT char identity_char(char a);
FFI_EXPORT float identity_float(float a);
FFI_EXPORT bool identity_bool(bool ident);
FFI_EXPORT double identity_double(double a);
FFI_EXPORT int8_t identity_int8_t(int8_t a);
FFI_EXPORT int16_t identity_int16_t(int16_t a);
FFI_EXPORT int32_t identity_int32_t(int32_t a);
FFI_EXPORT int64_t identity_int64_t(int64_t a);
FFI_EXPORT uint8_t identity_uint8_t(uint8_t a);
FFI_EXPORT uint16_t identity_uint16_t(uint16_t a);
FFI_EXPORT uint32_t identity_uint32_t(uint32_t a);
FFI_EXPORT uint64_t identity_uint64_t(uint64_t a);
FFI_EXPORT void *identity_ptr(void *ident);

FFI_EXPORT char add_char(char a, char b);
FFI_EXPORT float add_float(float a, float b);
FFI_EXPORT double add_double(double a, double b);
FFI_EXPORT int8_t add_int8_t(int8_t a, int8_t b);
FFI_EXPORT int16_t add_int16_t(int16_t a, int16_t b);
FFI_EXPORT int32_t add_int32_t(int32_t a, int32_t b);
FFI_EXPORT int64_t add_int64_t(int64_t a, int64_t b);
FFI_EXPORT uint8_t add_uint8_t(uint8_t a, uint8_t b);
FFI_EXPORT uint16_t add_uint16_t(uint16_t a, uint16_t b);
FFI_EXPORT uint32_t add_uint32_t(uint32_t a, uint32_t b);
FFI_EXPORT uint64_t add_uint64_t(uint64_t a, uint64_t b);

bool returns_false() { return false; }
bool returns_true() { return true; }
Expand Down Expand Up @@ -98,7 +105,8 @@ uint16_t add_uint16_t(uint16_t a, uint16_t b) { return a + b; }
uint32_t add_uint32_t(uint32_t a, uint32_t b) { return a + b; }
uint64_t add_uint64_t(uint64_t a, uint64_t b) { return a + b; }

void *ptr_should_point_to_42_as_int32_t();
FFI_EXPORT void *ptr_should_point_to_42_as_int32_t();

void *ptr_should_point_to_42_as_int32_t() {
int32_t *ptr = malloc(sizeof(int32_t));
*ptr = 42;
Expand All @@ -107,37 +115,37 @@ void *ptr_should_point_to_42_as_int32_t() {

static uint8_t buffer_with_deallocator[128];
static int deallocatorCalled;
void deallocator(void *ptr, void *userData) { deallocatorCalled++; }
void *getDeallocatorCallback() {
FFI_EXPORT void deallocator(void *ptr, void *userData) { deallocatorCalled++; }
FFI_EXPORT void *getDeallocatorCallback() {
deallocatorCalled = 0;
return &deallocator;
}
void *getDeallocatorBuffer() {
FFI_EXPORT void *getDeallocatorBuffer() {
deallocatorCalled = 0;
return &buffer_with_deallocator;
}
int getDeallocatorCalledCount() { return deallocatorCalled; }
FFI_EXPORT int getDeallocatorCalledCount() { return deallocatorCalled; }

bool is_null(int32_t *ptr) { return ptr == NULL; }
bool does_pointer_equal_42_as_int32_t(int32_t *ptr);
FFI_EXPORT bool is_null(int32_t *ptr) { return ptr == NULL; }
FFI_EXPORT bool does_pointer_equal_42_as_int32_t(int32_t *ptr);
bool does_pointer_equal_42_as_int32_t(int32_t *ptr) { return *ptr == 42; }

void *return_a_function_ptr_to_function_that_returns_true();
FFI_EXPORT void *return_a_function_ptr_to_function_that_returns_true();
void *return_a_function_ptr_to_function_that_returns_true() {
return (void *)&returns_true;
}

bool cb_identity_true(bool (*cb)()) { return cb(); }
FFI_EXPORT bool cb_identity_true(bool (*cb)()) { return cb(); }

bool cb_identity_false(bool (*cb)()) { return cb(); }
char cb_identity_42_char(char (*cb)()) { return cb(); }
float cb_identity_42_float(float (*cb)()) { return cb(); }
double cb_identity_42_double(double (*cb)()) { return cb(); }
uint8_t cb_identity_42_uint8_t(uint8_t (*cb)()) { return cb(); }
int8_t cb_identity_neg_42_int8_t(int8_t (*cb)()) { return cb(); }
uint16_t cb_identity_42_uint16_t(uint16_t (*cb)()) { return cb(); }
uint32_t cb_identity_42_uint32_t(uint32_t (*cb)()) { return cb(); }
uint64_t cb_identity_42_uint64_t(uint64_t (*cb)()) { return cb(); }
int16_t cb_identity_neg_42_int16_t(int16_t (*cb)()) { return cb(); }
int32_t cb_identity_neg_42_int32_t(int32_t (*cb)()) { return cb(); }
int64_t cb_identity_neg_42_int64_t(int64_t (*cb)()) { return cb(); }
FFI_EXPORT bool cb_identity_false(bool (*cb)()) { return cb(); }
FFI_EXPORT char cb_identity_42_char(char (*cb)()) { return cb(); }
FFI_EXPORT float cb_identity_42_float(float (*cb)()) { return cb(); }
FFI_EXPORT double cb_identity_42_double(double (*cb)()) { return cb(); }
FFI_EXPORT uint8_t cb_identity_42_uint8_t(uint8_t (*cb)()) { return cb(); }
FFI_EXPORT int8_t cb_identity_neg_42_int8_t(int8_t (*cb)()) { return cb(); }
FFI_EXPORT uint16_t cb_identity_42_uint16_t(uint16_t (*cb)()) { return cb(); }
FFI_EXPORT uint32_t cb_identity_42_uint32_t(uint32_t (*cb)()) { return cb(); }
FFI_EXPORT uint64_t cb_identity_42_uint64_t(uint64_t (*cb)()) { return cb(); }
FFI_EXPORT int16_t cb_identity_neg_42_int16_t(int16_t (*cb)()) { return cb(); }
FFI_EXPORT int32_t cb_identity_neg_42_int32_t(int32_t (*cb)()) { return cb(); }
FFI_EXPORT int64_t cb_identity_neg_42_int64_t(int64_t (*cb)()) { return cb(); }
Loading