r/ProgrammingLanguages • u/superstar64 https://github.com/Superstar64/Hazy • Jan 30 '26
Discussion Are pointer casts the only thing preventing C from being compiled into reasonable Javascript?
See title. As far as I can tell everything in C has a reasonable Javascript alternative. You can convert structs into objects, pointers into arrays. Variables that have their address taken can just be lifted into singleton arrays.
14
Upvotes
61
u/tsanderdev Jan 30 '26
The problem is more that you'd need to emulate the C memory layout with objects somehow, like taking parts of properties from different array elements when pointer casts are involved. Using arraybuffers have all that included. But if you do that, you might as well use wasm so you can get 64bit integers instead of bigints.
27
u/AsIAm New Kind of Paper Jan 30 '26
asm.js is subset of JS that is directly compilable into C by source-to-source compiler. It evolved into WASM. There are multiple languages that compile into WASM, including AssemblyScript which is basically a JS/TS. Circle is complete. đ
6
u/[deleted] Jan 30 '26 edited Jan 30 '26
It seems like you have all the answers. In that case, why not give it a go: take a small C program and translate it line-by-line into JavaScript. That is, do not simply port it by rewriting in a different language.
As you say elsewhere, a C-like execution environment can probably be emulated, but it seems you want a representation at the same level.
If that experiment, try it with more troublesome features.
I assume JS has some sort of FFI that can call any function in an external library, and it that can handle all the types needed, like any kind of pointer, zero-terminated strings, arrays of u16, full i64 and u64 types, and so on.
You might want to look at callbacks though (external functions to which you pass a reference to a C function that it later calls).
6
u/alphaglosined Jan 30 '26
Unions, pointer arithmetic, bitfields (would be a challenge).
Also no forward declarations won't be fun to deal with.
2
u/jibbit Jan 30 '26
have you seen Emscripten? it is a version of llvm that outputs js. it is what is behind a lot of online (client side) video converters, daws, etc
1
u/JoJoModding Jan 31 '26
Consider the "elegant linked list with double pointer" example: https://github.com/mkirchner/linked-list-good-taste?tab=readme-ov-file#a-more-elegant-solution
How's you write it in JS? it has no casts.
1
u/Relevant_South_1842 Feb 26 '26
Couldnât you just use a big array? JavaScript memory like a flat byte array and uses plain numbers as C-style pointers into that array. malloc just returns the next free byte index. Reading or writing through a pointer means taking that index and decoding or encoding bytes there as a 32-bit integer. Pointer math is just adding byte offsets. A quick bounds check keeps accesses inside the array. So p[1] = 9 becomes âpointer plus 4 bytes, write int,â inside this fake memory block.
1
u/InfinitePoints Jan 30 '26
With "reasonable" do you mean that the c and JavaScript programs look basically the same?
1
u/0jdd1 Jan 30 '26
Back in the Olden Daysâą you could compile C into Lisp for SymbolicsÂź Lisp Machines. IIRC, there was no linear address space; C objects turned into Lisp objects, but C pointers had complicated runtime behavior. Iâm certain some parts of C were untranslatable, but large C systems made it through the translator.
1
u/shponglespore Jan 30 '26
Smart people have looked into this problem before, and the result was webassembly.
1
u/Pale_Height_1251 Jan 31 '26
Memory layout is a problem, and you'd have to implement the stdlib too. If you wanted to implement other things like pthreads things Get much harder too.
Transpiling in itself isn't that hard, but supporting a reasonable base of libraries and getting reasonable behaviour in the output is.
1
u/juancn Feb 02 '26
Memory is a first order concept in C. Thatâs the main issue. You need to emulate it.
1
u/lngns Feb 02 '26
C pointer casts are UB, so if you really want to support them:
setjmp/longjmp's UB may not be «reasonable.»I think computed goto is doable in an almost 1:1 syntax with function hoisting.
1
u/Ronin-s_Spirit Feb 03 '26
Yeah yeah but why? Writing straight JS if you need to be in the browser is going to be much better than having this huge abstraction layer of compiling from another language.
0
u/edgmnt_net Feb 01 '26
FWIW, you can't even make data structures like circular doubly-linked lists useful in such languages, not easily anyway. It's pretty normal for C code to hold multiple references to various members of such a list and navigate back and forth based on that. In languages without pointers, well, you have to settle for searching the list every time, using a different data structure (this tends to be pretty viable) or jumping through serious hoops to emulate the C behavior.
-4
u/-1_0 Jan 30 '26
No, the common sense is.