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

  • 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. – FlatAssembler Aug 13 '20 at 04:26
  • @FlatAssembler what specifically about it isn't good for writing a compiler – B''H Bi'ezras -- Boruch Hashem Aug 13 '20 at 04:46
  • You'd be better off running the compiler for each platform on your server – Alan Birtles Aug 13 '20 at 05: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 – B''H Bi'ezras -- Boruch Hashem Aug 13 '20 at 05:27
  • Your other option then is to try and write a compiler yourself in JavaScript or compile one using web assembly, neither seems like a fun task – Alan Birtles Aug 13 '20 at 05:31
  • If you want to write a compiler, please, for your own sake, *do not start with C++*. Start with something really easy to parse like C. The C standard is understandable by humans. Implementing a C++ compiler will take a team of experts many months to even begin. First step: Write a parser for a contrived language. Build up from there. – tadman Aug 13 '20 at 05:32
  • @AlanBirtles are there any other examples of this that have ever been done? been trying to write basic "compiler" from assembly code in the meantime (then work from C++ to assembly later) – B''H Bi'ezras -- Boruch Hashem Aug 13 '20 at 05:32
  • @tadman OK fine I can work with a basic fasm-level javascript compiler, just taking the assembly code and converting to op codes and putting that in the exe file specification.. do you know if anything has been done like that b4? – B''H Bi'ezras -- Boruch Hashem Aug 13 '20 at 05:33
  • Sure, JavaScript does that in its [JIT](https://hacks.mozilla.org/2017/02/a-crash-course-in-just-in-time-jit-compilers/). If you want to write a compiler from scratch that's great, but start small, stupidly small, because these things have a way of being vastly more complicated than you've ever fathomed. Writing an assembler in JavaScript is challenging. Writing a C compiler in JavaScript is going to be very hard. Writing a C++ compiler in JavaScript is going to be a *multi-year commitment*. – tadman Aug 13 '20 at 05:34
  • 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](http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf) is half the size by comparison. – tadman Aug 13 '20 at 05:36
  • @tadman OK I agree, I'd just like to do an assembler than just like fasm but with JavaScript. Any idea where to start? – B''H Bi'ezras -- Boruch Hashem Aug 13 '20 at 05:37
  • I'd look at examples first, and read a lot of resources on compiler design, of which there are many, many books. Here's a [video series on writing a compiler from scratch](https://www.destroyallsoftware.com/screencasts/catalog/a-compiler-from-scratch). – tadman Aug 13 '20 at 05:40
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/219718/discussion-between-bluejayke-and-tadman). – B''H Bi'ezras -- Boruch Hashem Aug 13 '20 at 05:40

0 Answers0