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 appdeclare 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:
intisint64_t,i8..u64are the C integers of that size,f64isdouble,f32isfloat,boolisbool. Astringargument is a NUL-terminatedconst char *valid only during the call; astringresult is copied from the returnedconst char *(NULLis""). 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. --linktakes C / Objective-C / C++ sources (compiled with the program and cached by content,-fobjc-arcfor.m/.mm), objects, archives and linker flags. It is repeatable.- On macOS the program already runs on the main thread, so AppKit works from
mainwith 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_externand a parser case fordeclare function;declarefor 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 ...inmain.rs/build.rs; sources are compiled separately, so a.mmfile is not forced through-std=gnu11.tests/run/native_extern.{tov,c,stdout};tests/run.rslinks<name>.cnext to a case if it exists.cargo test --test run --test uipasses (42 programs, 18 cases).
Open questions
- Is
declare functionthe right surface? Anexternkeyword, or a"c"import specifier, would also work, butdeclarekeeps files valid TypeScript. - Callbacks (C calling back into Tov): the main missing piece for #24. A
tov_fnclosure has an environment, so a C function pointer needs a trampoline or auserdataconvention. I left this out on purpose. - 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.
- Should
--linkalso be settable frompackage.jsonsotov devandtov runpick it up without flags? Todaytov runneeds it before the program name. - Windows is not supported yet, so no
.dllor 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
0No activity yet.