Enable Harmony ES6 features in V8 at runtime in a module?

Node v0.10.20 provides many options related to harmony,

--harmony_typeof (enable harmony semantics for typeof) --harmony_scoping (enable harmony block scoping) --harmony_modules (enable harmony modules (implies block scoping) --harmony_proxies (enable harmony proxies) --harmony_collections (enable harmony collections (sets, maps, and weak maps)) --harmony (enable all harmony features (except typeof)) 

I understand that they are not production-ready functions and that they are under development, but many of them are good enough.

Is there a way to enable them at runtime?

 "use strict"; "use harmony collections"; 

Something like the above. Even if it were not just the inclusion of these functions at the module level, it would be nice to ensure that they are included inside the module, and not assume that they were included.

+6
source share
3 answers

No, you can’t. In fact, some things could potentially go horribly wrong inside V8's internal elements if you tried to break into several different parameters of these flags in the same V8 instance (disclosure: I implemented most of these flags).

+10
source

There is no way to do this, the interpreter reads the contents of the modules, then checks them and then evaluates. If you use some ES6 syntax, then the check will not complete and the code will not be evaluated.

You can isolate ES6 syntax files and run them as child processes (with the necessary parameters), but I think this is not the way you want to do it.

+1
source

The previous answer is not such a bad idea for a module (allocating ES6 files in the exec / child process), if you can deal with the idea of ​​running it in a child process.

The best apparent answer is if you are a module, a document for which these functions are required, and check them at runtime and pledge with a useful error. I myself didn’t understand exactly how good it is to check (give me a break, I used node for 3 days)

If you are writing an application, the answer is slightly different. In my case, the application I'm writing will most likely use these functions - and due to the limited ability to use only one parameter in the shebang line, not being able to change the JS version at runtime (which of course, as explained above, makes sense ) and does not want to execute the child process (my server is already multithreaded) - I was forced to write a script to start my node server so that my users would not write, t need to find out the correct node communication line to start my application (ugly), and if I I will ever want to use more than --harmony and "use strict"; I can do this with a script, since it is just a shell script that calls node and my application.

It is recommended that you use #!/usr/bin/env node as a shebang (which will find the node for you wherever it is installed), however you can only use one option in shebang, so this will not work with --harmony (or any another parameter)

Of course, you can always run node --harmony --use_strict --blah_blah yourScript.js , but if you need certain parameters, you will have to type them every time, so it is recommended to use a shell script (from me!). I suppose you could include this (or such) script in your module and recommend using it when running an application using your module.

This is a similar implementation for a shell script that I use for my server, it will find node and run your script with any parameters you need:

 #!/bin/bash if [ "$myScript" == "" ]; then myScript="./src/myNodeServer.js" fi if [ "$myNodeParameters" == "" ]; then myNodeParameters="--harmony --use_strict" fi if [ "$myNode" = "" ]; then myNode=`which node` fi if [ "$myNode" = "" ]; then echo node was not found! this app requires nodeJS to be installed in order to run. echo if you have nodeJS installed but is not found, please make sure the 'which' echo command is available. alternatively, you can forcibly specify the location of echo node with the $myNode environment variable, or editing this file. else echo Yay! node binary was found at $myNode fi if [ "$1" = "start" ]; then echo you asked to start.. echo calling $myNode $myParameters $myScript $2 $myNode $myParameters $myScript $2 exit elif [ "$1" = "-h" ] || [ "$1" = "--help" ]; then echo you asked for help.. echo usage: echo $0 start [script.js] [parameters for script] echo parameters for node and node location can be echo set with the \$myParameters and \$myNode env echo variables (or edit the top of this file). exit else echo no valid command specified - use $0 --help to see help. fi 

It is worth noting that if you ONLY want to use harmony and strict, while you cannot specify both in shebang, you can rigidly specify the location of the node and use "use strict"; nickname:

 #!/usr/bin/node --harmony "use strict"; 
+1
source

Source: https://habr.com/ru/post/956202/


All Articles