Compile JavaScript to Native Code with V8 - javascript

Is it really possible, with Google's V8 Engine, to compile JavaScript into Native Code, save it as a binary file, and execute it whenever I want through my software envorinment, on any machine?

You can use the V8 snapshot functionality to precompile the code. This still means that you have to have a full version of V8 running to load the snapshot (i.e., you don't get stand-alone native code, it needs to run inside the V8 VM), so all you save is the compilation time.
Also, the quality of snapshot code isn't necessarily as good as JIT'ed code because JIT code can use, e.g., SSE2/SSE3 if it's available, which snapshots can't assume.

As far as I know, V8 is purely a just-in-time compiler, and does not have an ahead-of-time option.
As discussed at the articles I linked, JITs allow better, more flexible optimizations.

Instead, it might be possible to use a .NET JavaScript/JScript compiler to create a .NET exe, then convert the .NET exe to a native .exe using the Mono ahead-of-time compiler.

The closest you might get to acheiving your goal is to create a self-executing Javascript bytecode wrapper.
A project that does this is pkg
It somehow creates a self-contained binary executable from Javascript, including module dependencies and asset files and produces a self-contained executable.
Installation and use is easy:
$ npm install -g pkg
$ pkg index.js -o my-program
$ ./my-program
My understanding is that this binary contains nodejs bytecode. It also appears that you can cross-compile.
Note: I've tried ncc and nexe also, but I haven't found them to be as useful. ncc just creates a self-contained Javascript file and nexe encountered a Python error when I tried to use it.

Related

Compile Dlib for webassembly

I am trying to build dlib for webassembly using emscripten but I am not sure how to do so.
Currently, dlib generates executables and not bytecode which is needed for emscripten. Is there some way to get around this issue?
Currently this is what i am doing (from within the dlib-19.4 folder.
cd examples
mkdir build
cd build
cmake ..
emmake make
for the next step, I need to input a bytecode file however, dlib seems to just generate executables which the emcc command will not accept.
From what I can read from Dlib's documentation is that it has a lot of features that emscripten does not provide including threading and so on. So you will need to exclude functionality thats not supported.
Also referring to How to Compile section, it mentions how to use Dlib without Cmake.
It has a simple gcc example for SVM.
You should probably use this approach ie include the appropriate dlib headers in your project, write your functions and expose them for emscipten.
Then after building the js file you should be able to call the "exposed" functions from JS.

What javascript parser sould I use to re-implement jsCoverage in JavaScript for node?

I really like code-coverage reports for my code in node.js.
I've already created a node.js module that can inject instrumented code (and mock our require statements) called requiremock
I'm using that in my other module nodecoverage together with the binary version of jsCoverage (windows) to generate code coverage reports, injecting instrumented versions of code with requiremock.
The problems with using jsCoverage are
It needs compilation for the platform, because it's written in C(++), I would like to implement it in JavaScript so it can be used on any platform without compilation.
It writes the instrumented versions of code as files on disk. With requiremock I can generate the instrumented JavaScript files in memory and run those when the original file is required.
jsCoverage does not report code coverage correctly when using function hoisting, and I use that a lot in node.js
So my question is:
What JavaScript parser written in JavaScript should I choose to reimplement jsCoverage as a node.js module?
I have to be able to know the linenumber of the code in the original file, and also know what whitespacing was like.
Try esprima. It's awesome. Also node-cover potentially already does have what you need

Is there a node module that will compile a project to a client side file?

I'm new to node, but so many client-side open source projects use it that I am assuming there is a way to compile a node project to be compressed into one minified file that is ready for client-side usage. The open source project I'm building is using node for it's nice module and testing support, but all of the code is meant to be run on the client.
I've seen projects like this: https://github.com/mape/connect-assetmanager
But what I'm not sure of, is whether node-specific constructs like export and module are removed or supported, so that there aren't problems on the client side?
You should look at http://requirejs.org/
It replaces and extends require for both node (server-side) and browser side. It also supports merge and minification
Just came across this:
https://github.com/substack/node-browserify
Awesome project!!
You are incorrect. Node is server-side only. Perhaps you have a bit of a misunderstanding what Node is and isn't.
Nodejs is basically the Google Chrome V8 Javascript engine, packages as an executable that can run on a console. Javascript code written for node could run on the client though, but it's not as simple as you may think.
What functionality that you currently have on the server do you want to run on the client? Are you just looking for a minifier like yui compressor?

How to replace the rhino scripting jar used by Eclipse?

I want to use Eclipse Indigo to develop some JavaScript code. But the rhino jar used by Eclipse must be older than the most recent version and it makes problems. I want to replace it with the newest build.
Does anyone know how can I replace it?
To replace rhino used within your eclipse by JSDT, you would have to
turn your rhino.jar into an OSGi bundle, similar to the org.mozilla.javascript listed on http://download.eclipse.org/tools/orbit/downloads/drops/R20120119162704/
Find the feature that includes the javascript jar, maybe scanning the eclipse/features/*/feature.xml files
Create a feature patch for that feature including your new version of rhino, and export
Install it into you eclipse
But OSGi bundles can add valid bundle version ranges to their dependencies, so you wouldn't be able to tell if it could work without trying it, or finding all bundles that depend on rhino and checking their dependency.

How can I compile CoffeeScript from .NET?

I want to write an HttpHandler that compiles CoffeeScript code on-the-fly and sends the resulting JavaScript code. I have tried MS [JScript][1] and IronJS without success. I don't want to use [Rhino][2] because the Java dependency would make it too difficult to distribute.
How can CoffeeScript be compiled from .NET?
CoffeeScript-dotnet
Command line tool for compiling CoffeeScript. Includes a file system watcher to automatically recompile CoffeeScripts when they change. Roughly equivalent to the coffee-script node package for linux / mac.
CoffeeSharp
Includes a command line tool similar to CoffeeScript-dotnet as well as a http handler that compiles CoffeeScripts when requested from an asp.net site.
SassAndCoffeeScript
Library for Asp.net mvc that compiles sass and coffeescript files on request. Also supports minification and combination.
Manually Compile With IronJS
IronJS is a .NET javascript interpreter that can successfully load the CoffeeScript compiler and compile CoffeeScript.
Manually Compile With Node.js
Get the node binaries and add the bin directory to your path. Write a node.js script to load the CoffeeScript compiler and your CoffeeScript files and save the compiled javascript.
CoffeeScript is now fully supported by Chirpy:
http://chirpy.codeplex.com/
You specifically said that you wanted to write a runtime compiler, so this may not be exactly what you are looking for, but if the main point is to have a way to generate the javascript result, the Mindscape Web Workbench is interesting. It is a free extension for Visual Studio.NET 2010 and available in the Extension Manager. It gives Intellisense, syntax highlighting and compiles to JS as you write. I am just getting started using it but looks promising. Scott Hanselman talks about it here. It also supports LESS and Sass.
I've managed to compile CoffeeScript from .NET using IKVM, jcoffeescript and Rhino. It was straightforward, except that the JCoffeeScriptCompiler constructor overload without parameters didn't work. It ran OK with a java.util.Collections.EMPTY_LIST as parameter.
This is how I did it:
Download IKVM, jcoffeescript and Rhino.
Run ikvmc against js.jar, creating js.dll.
Run ikvmc against the jcoffeescript jar.
Add a reference to the jcoffeescript dll in Visual Studio. More references may be needed, but you will be warned about those.
Run new org.jcoffeescript.JCoffeeScriptCompiler(java.util.Collections.EMPTY_LIST).compile() in your code.
The next step would be to create a build task and/or an HTTP handler.
Check out the new coffeescript-dotnet project, which uses the Jurassic JavaScript implementation.
Since the CoffeeScript compiler now runs on Internet Explorer, after a couple of recent tweaks, it should be good to go within other MS-flavors of JavaScript as well. Try including extras/coffee-script.js from the latest version, and you should be good to go with CoffeeScript.compile(code).
I tried running the bundled extras/coffee-script.js through Windows Based Script Host (or just wscript) and it didn't report any issues. I then added this line:
WScript.Echo(CoffeeScript.compile('a: 1'));
at the end of the file and run it through wscript again and it printed the resulting JavaScript correctly.
Are you using COM objects? Can you share some more of the code responsible for initialising the MScript object reference?
CoffeeScript in Visual Studio 2010
It's Chirpy's fork (chirpy is a tool for mashing, minifing, and validating javascript, stylesheet, and dotless files)
"OK, I think I got it working on my fork, based mostly on other peoples' work. Check it out:
http://chirpy.codeplex.com/SourceControl/network/Forks/Domenic/CoffeeScriptFixes"
from http://chirpy.codeplex.com/workitem/48
I don't have a direct answer, (I hope you find one), but maybe take a look at the following to see how it might be done.
Jint - JavaScript interpreter for .NET
Using IKVM to compile Rhino would get rid of the Java runtime requirement.
jcoffeescript. I haven't looked at jcoffeescript, but I think it depends on JRuby and Rhino. You could possibly IKVM.NET this as well.
IronJS now supports CoffeeScript and is generally faster than the other .NET JS engines:
I have a blog post about wiring the two together here:
http://otac0n.com/blog/2011/06/29/CoffeeDemo-A-Simple-Demo-Of-IronJS-Using-CoffeeScript.aspx
My main editor is VS 2010 and I love the WorkBench extension. it's nice it auto compiles to js everytime you hit save on your .coffee file, also introduces you to SASS which I had read about but never got around.
They offer a pay version to that will autmaically shrink/minify your js and css files as well, since your.coffee and .scss are your source files anyway.
I'd encourage all VS users to go ahead and install this especially if you run VS 2010.
The only knock, and someone please correct me or enlighten me, is that with .coffee syntax it's not highlighted the way say html, js, c# code is. it might be because I am using a color scheme from http://studiostyl.es/ and for the record http://studiostyl.es/schemes/coffee- just shares the name coffee no special syntax highlight support for coffeescript that I am aware of. but no reason not to start using the workbench addin today!
Okay workbench website claims: syntax highlighting so again maybe it's the studiostyle.es i chose.
I know this is old but I came here to answer a very similar question: How do I get my CoffeeScript to compile using Visual Studio 2012 Express? Note that the free Express version does not allow any extensions so I could not continue to use the Mindscape Workbench extension that had served me well for quite some time.
It turns out to be very easy. Just use NuGet to install the Jurassic-Coffee package and off you go.
One advantage of using this package over mindscape workbench is that you can reference your coffee directly from the script tags in the html. It minifies and caches the compiled JS so you only do work if the requested coffee file has changed.
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="home.coffee"></script>
</head>
The mindscape workbench allows you to bundle together different coffescript files which is very handy for modularising your coffeescript. You can also do this using Jurassic Coffee by utilising the #= require statement to include other coffee module files, for example:
#= require Classes\GridWrapper.coffee
class UsersGrid
constructor:->
#grid = new GridWrapper()
I think having the #= require staement in the coffee file is actually cleaner and clearer than the mindscape workbench approach, which kind of hides all this behind their interface so you forget easily what dependencies you have.
Note
There is one potential gotcha. The Nuget installer will put in an httphandler entry into your web.config that may not be compatible with IIS Express integrated managed pipeline mode.
You might therefore see the following error:
An ASP.NET setting has been detected that does not apply in Integrated
managed pipeline mode.
To fix this just remove the handler shown below.
<system.web>
//other stuff
<httpHandlers>
<add type="JurassicCoffee.Web.JurassicCoffeeHttpHandler,JurassicCoffee" validate="false" path="*.coffee" verb="*" />
</httpHandlers>
</system.web>
You could simply write a port of it to C#. I have ported Jison to C# (which is the underlying project that makes CoffeeScript run). I would think it may be a bit different, but both Jison parsers work the same.
I have not pull requested it back yet to Jison's main architecture, but will be doing so soon.
https://github.com/robertleeplummerjr
Instead of shelling out to CScript you could shell out to Node.js (here are self-contained Windows binaries)
I've tried to compile the extras/coffee-script.js file, unmodified, to jsc, the JScript.NET compiler for .NET, and I got many errors. Here are the noteworthy ones:
'require' is a new reserved word and should not be used as an identifier
'ensure' is a new reserved word and should not be used as an identifier
Objects of type 'Global Object' do not have such a member
Other errors were caused by the above said errors.
You might also want to check out jurassic-coffee, it is also a coffee-script compiler running the original compiler in jurassic.
It features sprocket style "#= require file.coffee" or "#= require file.js" wich can be used to keep .coffee files modular and combined right before compilation as well as embedding .js files.
It sports a HttpHandler with file watchers for .js and .coffee files that keeps track of what .coffee files needs to be re-compiled and pass through to the compiled *.js files for the rest.
jurassic-coffee is also available as a Nuget package
https://github.com/creamdog/JurassicCoffee
I've done an HttpHandler that uses Windows Script Host behind the scenes: https://github.com/duncansmart/LessCoffee and works great (it also compiles *.less files).
It's on NuGet: http://nuget.org/List/Packages/LessCoffee
It's based on this simple wrapper: https://github.com/duncansmart/coffeescript-windows
I wrote an inteructive shell using v8.
https://github.com/mattn/coffee-script-v8
This work as single executable file. (Don't use external files)
It can't use require(). But enough to learn coffeescript.

Categories

Resources