How to turn the V8 compiled javascript into an EXE?

前端 未结 4 1299
面向向阳花
面向向阳花 2021-01-11 15:05

I know that google\'s v8 compiles javascript into native machine (binary if I understand correctly) code.
Is there a way to take the output and turn it into a exe?

相关标签:
4条回答
  • 2021-01-11 15:36

    Javascript cannot be compiled just once. The language has eval which is pretty widely used. (for JSON for instance) You need to carry around the JIT, and the whole runtime.

    JIT here is only an optimization, not the way to get rid of the compiler/interpreter.

    0 讨论(0)
  • 2021-01-11 15:37

    Node.js embeds V8. This might be a good example to learn from.

    0 讨论(0)
  • 2021-01-11 15:46

    I don't think you can directly turn a piece of JavaScript into an executable using V8, but you can probably make an application that bundles the V8 engine with the JavaScript and runs it as a stand-alone.

    You can find all information about V8 on its project page.

    Also note that JavaScript can't be completely compiled as it's a dynamic language. With V8, it's JIT-compiled (like .NET, for example.) It's still possible to turn it into a stand-alone executable though (like .NET, for example.)

    If you want to develop stand-alone applications that make use of HTML for rendering, you could have a look at Adobe Air as well.

    0 讨论(0)
  • 2021-01-11 15:49

    There have been a few tries at making js into native code, it's not something that can be used in production by any means, more of an academic interest. The Rhino interpreter for java has an option to make js into (java) bytecode so one approach is to convert to bytecode and then from bytecode to native with GCJ. There is some discussion about Rhino and GCJ but I don't know if anyone ever tried exactly that. https://groups.google.com/forum/#!msg/netscape.public.mozilla.jseng/c3tqyLZ19fw/8V4HeuMtIXUJ

    Another approach is using Python, specifically Py-Py which itself is written in a non-standard subset of Python called rPython. rPython is not meant for human consumption but it has the benefit of being something which can be compiled to native. One interesting (albeit wacky) experiment was to compile Javascript to Python and then in some cases that Python happens to be valid as rPython and can be compiled down to native with the rPython compiler.

    http://mozakai.blogspot.com/2010/07/experiments-with-static-javascript-as.html

    If a .exe file is really important, I would bundle V8 with your app since even if you can compile js to native, you still need a full interpreter if you use any eval() or similar. It would not be hard to write a tool for bundling everything into an .exe file as long as your users don't mind either an 8MB exe or 8MB V8.dll file.

    As a last thought, Big G has started allowing "native" apps based on chrome (google: "chrome packaged apps"). They have low level system access and can use the WebKit renderer allowing you to create your GUI in CSS and HTML and they have their own windows and icons so it is not obvious that they are running inside of chrome. This is probably still premature but it's something to keep an eye on in the desktop applications field.

    0 讨论(0)
提交回复
热议问题