summaryrefslogtreecommitdiff
path: root/spec/bundler/realworld/ffi_spec.rb
blob: 083ea38901d63f22af3488d9fdad2518d5c3a2f6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# frozen_string_literal: true

RSpec.describe "loading dinamically linked library on a bundle exec context", :realworld => true do
  it "passes ENV right after argv in memory" do
    create_file "foo.rb", <<~RUBY
      require 'ffi'

      module FOO
        extend FFI::Library
        ffi_lib './libfoo.so'

        attach_function :Hello, [], :void
      end

      FOO.Hello()
    RUBY

    create_file "libfoo.c", <<~'C'
      #include <stdio.h>

      static int foo_init(int argc, char** argv, char** envp) {
        if (argv[argc+1] == NULL) {
          printf("FAIL\n");
        } else {
          printf("OK\n");
        }

        return 0;
      }

      #if defined(__APPLE__) && defined(__MACH__)
      __attribute__((section("__DATA,__mod_init_func"), used, aligned(sizeof(void*))))
      #else
      __attribute__((section(".init_array")))
      #endif
      static void *ctr = &foo_init;

      extern char** environ;

      void Hello() {
        return;
      }
    C

    sys_exec "gcc -g -o libfoo.so -shared -fpic libfoo.c"

    install_gemfile <<-G
      source "https://rubygems.org"

      gem 'ffi'
    G

    bundle "exec ruby foo.rb"

    expect(out).to eq("OK")
  end
end