# First native bridge

This example calls a small C library through a `.vxc` boundary, then exercises
success and two failure paths. It has two Vex files, a C source file and a
manifest. It uses `cc` and an object file built for the same target as the Vex
program. The example makes allocation ownership and cleanup visible at the
foreign boundary.

Create an empty directory and put all four files in it. This teaching fixture
uses global unsynchronized counters: call it only from one thread, with no
concurrent bridge operations. The `unsafe` boundary reflects that requirement.
The numeric `-1` and `-2` values are fixture statuses, not universal C errors.

## C implementation

The handle is either null or an allocated, initialized byte. `bridge_read`
requires a live, non-null handle returned by `bridge_open`; reading after
`bridge_close`, passing another pointer, or closing twice violates its contract.

```c [bridge.c]
#include <stdint.h>
#include <stdlib.h>
static int32_t opened = 0;
static int32_t closed = 0;
void bridge_reset(void) { opened = 0; closed = 0; }
void *bridge_open(int32_t fail) {
    if (fail) return NULL;
    uint8_t *p = malloc(1);
    if (!p) return NULL;
    *p = 42;
    opened++;
    return p;
}
int32_t bridge_read(const void *p) { return *(const uint8_t *)p; }
void bridge_close(void *p) { if (p) { closed++; free(p); } }
int32_t bridge_opened(void) { return opened; }
int32_t bridge_closed(void) { return closed; }
```

## Native feature manifest

`all` selects the local object in this teaching fixture. Build the object for
the actual target; the manifest does not make one object portable.

```json [vex.json]
{"name":"ffi-demo","version":"0.1.0","main":"main.vx","native":{"features":{"bridge":{"all":{"path":"bridge.o","type":"static"}}}}}
```

## Vex boundary

The boundary owns the handle during `runCase`: null exits before any read or
close; after a successful open, each branch calls `bridge_close` exactly once.
No raw handle escapes to the caller. The counter functions are unsafe because
the C fixture uses unsynchronized global state.

```vex [native.vxc]
extern "NATIVE" from "bridge" {
    fn bridge_reset();
    fn bridge_open(fail: i32): Ptr<Opaque!>;
    fn bridge_read(handle: Ptr<Opaque>): i32;
    fn bridge_close(handle: Ptr<Opaque!>);
    fn bridge_opened(): i32;
    fn bridge_closed(): i32;
}
export unsafe fn runCase(failOpen: bool, failAfterOpen: bool): i32 {
    unsafe { bridge_reset(); }
    let handle = unsafe { bridge_open(if failOpen { 1 } else { 0 }) };
    if handle == Ptr.nullMut<Opaque>() { return -1; }
    if failAfterOpen {
        unsafe { bridge_close(handle); }
        return -2;
    }
    let value = unsafe { bridge_read(handle as Ptr<Opaque>) };
    unsafe { bridge_close(handle); }
    return value;
}
export unsafe fn opened(): i32 { return unsafe { bridge_opened() }; }
export unsafe fn closed(): i32 { return unsafe { bridge_closed() }; }
```

## Vex caller

The caller obeys the single-threaded fixture contract. Each group of three
lines reports the case result, successful opens, and closes.

```vex [main.vx]
import { runCase, opened, closed } from "./native.vxc";
fn main(): i32 {
    $println(unsafe { runCase(false, false) }); $println(unsafe { opened() }); $println(unsafe { closed() });
    $println(unsafe { runCase(false, true) });  $println(unsafe { opened() }); $println(unsafe { closed() });
    $println(unsafe { runCase(true, false) });  $println(unsafe { opened() }); $println(unsafe { closed() });
    return 0;
}
```

## Build and run

From the directory containing these files, using a C compiler for the same
host target as Vex:

```bash
cc -std=c11 -c bridge.c -o bridge.o
vex lint main.vx
vex run main.vx
```

Expected stdout:

```text
42
1
1
-2
1
1
-1
0
0
```

The successful read and the simulated error after opening each have one open
and one close. The simulated open failure has neither. These counters cover
only this fixture and these paths; they are not a proof of behavior under
allocation failure, asynchronous cancellation, concurrent calls, or a different
foreign library. Production wrappers must define those contracts explicitly.

See [Native FFI pipeline](../references/vex-pm-native-ffi.md) for artifact selection
and [FFI](ffi.md) for provider and pointer rules.
