#TypeError: undefined is not an object when using FFI

1 messages · Page 1 of 1 (latest)

lone mango
#
theo ❯❯ bun src/example.ts
56 |       break;
57 |     }
58 |   }
59 |   return wrap.native = functionToCall, wrap.ptr = functionToCall.ptr, wrap;
60 |
61 | }, dlopen = function(path, options) {
                                           ^
TypeError: undefined is not an object
      at bun:ffi:61:40
      at /home/theo/dev/gurafikku/src/index.ts:5:26
import { dlopen, FFIType, ptr, suffix } from "bun:ffi";

const path = `libdawn.${suffix}`;

const { symbols: dawn } = dlopen(path, {
  wgpuCreateInstance: {
    args: [FFIType.pointer],
    returns: FFIType.pointer,
  },
  wgpuInstanceRelease: {
    args: [FFIType.pointer],
    returns: FFIType.void,
  },
});

The libdawn.so is in zig-out/lib and i tried using LD_LIBRARY_PATH=$PWD/zig-out/lib to point to it but that didnt help 🤔

lone mango
#

welp

vale notch
#

could you give build command for libdawn?

vale notch
#

figured it out

#

looks like you are trying to import static library

#

we should emit error instead of returning nothing

lone mango
#

It's a bit more than just building libdawn, I had to use the zig package manager to link it to a empty zig lib...
zig build -Dcpu=baseline -Doptimize=ReleaseFast is what i ran

const std = @import("std");
const mach_gpu_dawn = @import("mach_gpu_dawn");

pub fn build(b: *std.Build) !void {
    const target = b.standardTargetOptions(.{});
    const optimize = b.standardOptimizeOption(.{});

    const lib = b.addSharedLibrary(.{
        .name = "gurafikku",
        .root_source_file = .{ .path = "src/main.zig" },
        .target = target,
        .optimize = optimize,
    });
    try mach_gpu_dawn.link(b, lib, .{
        .install_libs = true,
        .from_source = true,
        .shared_libs = true,
    });
    b.installArtifact(lib);

    const main_tests = b.addTest(.{
        .root_source_file = .{ .path = "src/main.zig" },
        .target = target,
        .optimize = optimize,
    });

    const run_main_tests = b.addRunArtifact(main_tests);

    const test_step = b.step("test", "Run library tests");
    test_step.dependOn(&run_main_tests.step);
}
#

but no im using a .so

lone mango
#
heo@gigatop ~/d/gurafikku (main)> file zig-out/lib/libdawn.so
zig-out/lib/libdawn.so: ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically linked, with debug_info, not stripped

🤔

vale notch
#

that still returns undefined?

vale notch
#

did some more testing and looks like there might be bigger issue at hand

lone mango
#

oh 👀

earnest crater
mellow sky
#

I can +1 this doesn't work for me (MacOS) for zig, rust, or c, accross dylib or so files

vale notch
#

@strange vigil this does get called :)

strange vigil
#

oh weird

#

where/how?

vale notch
#

dlopen()

mellow sky
#

@vale notch any ideas on how i could do a workaround? Building a web framework and want routing to be in a faster lang like zig

vale notch
#

either i or jarred will fix it

#

until then you can use napi

#

@maiden plinth remind 3h this

maiden plinthBOT
#

Alright @vale notch, in 3 hours: this

maiden plinthBOT
#

@vale notch, <t:1694672686:R>: this

mellow sky
#

thanks man, I appreciate it

vale notch
#

@lone mango @mellow sky so the issue is that you are looking for library in global instead of relative scope

#

dlopen('file.so') and dlopen('./file.so')

#

so either use absolute paths

#

or dont forget ./

#

the only bug here is that bun isn't throwing the error

vale notch
#

whole file is full of same bug lol

lone mango
#

oh lol

vale notch
#

two files now

#

truly a bun moment

lone mango
#

Hmm. Is it relative to $PWD (./) or the ts file thats in ./src

vale notch
#

likely where you are running binary from

lone mango
#
const path = `../zig-out/lib/libdawn.${suffix}`;

const path = `./zig-out/lib/libdawn.${suffix}`;

both dont seem to work thonk

vale notch
#

full path?

lone mango
#
/home/theo/dev/gurafikku/packages/gurafikku/zig-out/lib/libdawn.so
56 |       break;
57 |     }
58 |   }
59 |   return wrap.native = functionToCall, wrap.ptr = functionToCall.ptr, wrap;
60 | 
61 | }, dlopen = function(path, options) {
                                           ^
TypeError: undefined is not an object
#

/home/theo/dev/gurafikku/packages/gurafikku/zig-out/lib/libdawn.so: ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically linked, with debug_info, not stripped

vale notch
#

error

#

sadly i am on arm linux so wont be able to give you my executable to see what is happening

lone mango
#

hm ok

vale notch
#

it keeps going 🙂

#

even outside ffi

lone mango
#

oof

vale notch
#

not gonna fix it outside ffi for now

#

clearly a bigger problem

vale notch
#

ok now i am lost

#

it only affects one single function

#

okay i think i figured it out

#

even more deep

#

yeah

#

@strange vigil errors thrown inside bun/js/*.ts are silenced and execution resumes

#

but only from native function calls

#
import { CString } from 'bun:ffi';

const err = new CString({});
console.log('still alive', err);```
#

add BunCString to exports and it will behave correctly in user code

#
import { BunCString } from 'bun:ffi';

const err = new BunCString({}); // throws here
console.log('still alive', err);```
strange vigil
#

oh no

#

i wonder if we are using

#

a catch scope

#

when initializing them

lone mango
#

😭

Now I'm getting a better error message but it still doesnt work with an absolute path

#
/home/theo/dev/gurafikku/packages/gurafikku/zig-out/lib/libdawn.so
ERR_DLOPEN_FAILED: Failed to open library. This is usually caused by a missing library or an invalid library path.
 syscall: "dlopen"
lone mango
#

Rebuilt with zig build -Dcpu=baseline -Dtarget=x86_64-linux-gnu, same issue