I've seen similar questions (or so it may seem), such as
How do I compile C++ to JavaScript in a browser?
Executing Javascript Code From Objective C
Library to write javascript code
What language is JavaScript written in?
Is there any way to compile code ( C/ C++ ) with in browser?
Is there a way to use C++ in JavaScript?
Possible to write a Compiler with Javascript?
[most closely related] Is there such a thing as a javascript compiler?
How can I write a JavaScript compiler for Arduino or similar microcontroller?
but some of these talk about the opposite.
Basically what I mean is to take C++ code, convert it to assembly (NTO WebAssembly) [or just skip this step like Microsoft does], and generate machine code, then put the bytes of that machine code in an executable of some kind (unix executable or windows one), just like actual compilers do.
I realize that this means there will be differences for difference processors and architectures, since it will be writing the machine code directly, but JavaScript has the ability to write pure bytes and applications using mimeType application/octet-stream
and download it to the computer, like
var a =document.createElement("a");
a.href = URL.createObjectURL(
new Blob([
(bytes => {
var headers = "some header bytes";
for(var i = headers.length; i < bytes.length; i++) {
bytes[i] = Math.floor(
Math.random() * 256 //represents machine code
//and other file headers etc
)
}
headers.split("").forEach((h, i) => {
bytes[i] = h.charCodeAt(h)
})
return bytes
})(new Uint8Array(500))
], {
type: "application/octet-stream" //not really
//necessary for HTML but for executables it might be
})
)
a.download = "BoruchHashem.html"//or.exe, but this is just testing
a.click()
[like in https://stackoverflow.com/questions/23451726/saving-binary-data-as-file-using-javascript-from-a-browser ]
If one simply knows the archetecture of the processor they are trying to compile for, [and the necessary op-codes][1], as well as the file format specification for the particular type of executable they are trying to run ([such as exe for windows][2]) they should be able to analyze the C++ code using an [AST][3] written in JavaScript (https://stackoverflow.com/questions/13171616/c-parser-in-javascript), then generate the necessary op-codes and machine language bytes and put that in the necessary section of the .exe file, put all of the bytes into a blob and download it to the user (obviously there are security risks if its a public website, anyone can insert malware into it etc. but that's another issue)
The question is is there any documentation on this, or is there any ready-made JavaScript compilers for C++ that are readily available? Was not able to find anything on github with google searches [1]: http://ref.x86asm.net/coder32.html [2]: https://blog.kowalczyk.info/articles/pefileformat.html [3]: https://github.com/ajaxorg/treehugger