#58

Calling C from Tov: declare function and --link (a first step for #24)

Openimplicit-invocationopened 1mo ago

This is a small, concrete first step toward item 1 of #24 (native FFI). It covers calling C from Tov with plain values. It does not cover callbacks or ownership of Tov objects; those are the open questions below.

What it looks like

declare function fox_present(rgba: u8[], w: int, h: int): void
declare function fox_pump(): int
declare function fox_name(): string

function main() {
  while (fox_pump() === 1) fox_present(frame(), 540, 960)
}
tov build --link shell.m --link "-framework Cocoa" app.tov -o app
  • declare function name(params): T; has no body and calls the C function of that name. It is TypeScript syntax, so a Tov file that uses it is still valid TypeScript for tooling.
  • Types: int is int64_t, i8..u64 are the C integers of that size, f64 is double, f32 is float, bool is bool. A string argument is a NUL-terminated const char * valid only during the call; a string result is copied from the returned const char * (NULL is ""). An array of numbers is passed as a pointer to its elements, with the length as another argument.
  • Anything else, and generics, async, throws, inout, optional and rest parameters, is a new error, T0860.
  • --link takes C / Objective-C / C++ sources (compiled with the program and cached by content, -fobjc-arc for .m/.mm), objects, archives and linker flags. It is repeatable.
  • On macOS the program already runs on the main thread, so AppKit works from main with a polling loop (fox_pump() above).

Why

The desktop port described in #24 creates windows through a helper process and JSON pipes. With this change a Tov program can call the window system and a GPU API directly. As a test I built a macOS app that is a single Tov process: its main loop calls a small AppKit shell and a small Metal renderer written in C / Objective-C. The same game scene renders in about 1.8 ms per frame at 1080x1920 (4x MSAA, shadow map), against 71 ms for the same scene rasterized on the CPU. A call into C costs about 0.8 ns (a Tov-to-Tov call is about 0.45 ns), and an array is passed by pointer without a copy.

Implementation

The branch has one commit, about 200 changed lines plus tests and a spec section (7e. Calling C): https://github.com/implicit-invocation/Tov/compare/main...extern-functions

  • FnDecl.is_extern and a parser case for declare function; declare for anything else stays X0021.
  • The checker validates the signature (T0860) and skips the body.
  • Codegen emits a C prototype and a wrapper with the normal instance signature that converts the arguments and the result, so calls to it are ordinary Tov calls.
  • tov build --link ... in main.rs / build.rs; sources are compiled separately, so a .mm file is not forced through -std=gnu11.
  • tests/run/native_extern.{tov,c,stdout}; tests/run.rs links <name>.c next to a case if it exists. cargo test --test run --test ui passes (42 programs, 18 cases).

Open questions

  1. Is declare function the right surface? An extern keyword, or a "c" import specifier, would also work, but declare keeps files valid TypeScript.
  2. Callbacks (C calling back into Tov): the main missing piece for #24. A tov_fn closure has an environment, so a C function pointer needs a trampoline or a userdata convention. I left this out on purpose.
  3. Ownership: today strings and arrays are borrowed for the call only. Returning buffers, keeping them, or passing records and class instances needs an ownership model.
  4. Should --link also be settable from package.json so tov dev and tov run pick it up without flags? Today tov run needs it before the program name.
  5. Windows is not supported yet, so no .dll or MSVC handling here.

If this direction is welcome I will open it as a pull request, adjusted to your answers to the questions above.

Activity

0

No activity yet.