0

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

13
  • JavaScript is not a particularly good language for making complex compilers, you know. C++ is a very complex language, and nobody would, as a proof of a concept, write a compiler for it in JavaScript. Commented Aug 13, 2020 at 4:26
  • @FlatAssembler what specifically about it isn't good for writing a compiler Commented Aug 13, 2020 at 4:46
  • You'd be better off running the compiler for each platform on your server Commented Aug 13, 2020 at 5:26
  • @AlanBirtles what server? I can't afford server hosting, I Want to make a basic client-side compiler, plus think of all the security I'd have to go through when running compilers for different programs on the server Commented Aug 13, 2020 at 5:27
  • 1
    The C++ standard is 1151 pages of extremely technical specifications. It is not something you can quickly understand and build a compiler for in a weekend. The C specification is half the size by comparison. Commented Aug 13, 2020 at 5:36

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.