logoalt Hacker News

flohofwoeyesterday at 8:19 PM3 repliesview on HN

Think of it as traditional FFI (foreign function interface) situation.

Many important libraries have been written in C and only come with a C API. To use those libraries in non-C languages (such as Java) you need a mechanism to call from Java into C APIs, and most non-C language have that feature (e.g. for Java this was called JNI but has now been replaced by this: https://docs.oracle.com/en/java/javase/21/core/foreign-funct...), e.g. C APIs are a sort of lingua franca of the computing world.

The DOM is the same thing as those C libraries, an important library that's only available with an API for a single language, but this language is Javascript instead of C.

To use such a JS library API from a non-JS language you need an FFI mechanism quite similar to the C FFI that's been implemented in most native programming languages. Being able to call efficiently back and forth between WASM and JS is this FFI feature, but you need some minimal JS glue code for marshalling complex arguments between the WASM and JS side (but you also need to do that in native scenarios, for instance you can't directly pass a Java string into a C API).


Replies

dontlaughtoday at 8:23 AM

DOM API interactions with JS are already described with a DDL, they're not exactly native JS APIs.

jculyesterday at 9:44 PM

Wow, I've used JNI many times, but many years ago. It is a bit painful. Cool to see it's been replaced by FFM, didn't know that existed.

gspryesterday at 8:35 PM

Sure, but if someone came at the C-centric ecosystem today and said "let's do the work to make it so that any language can play in this world", then surely "just FFI through C" would be considered rather underwhelming?

show 2 replies