NativeScript: Enabling beta android v8 features

nv8One of the interesting things about the v8 engine is there is a lot of development going on behind the scenes that you can actually enable right now.  This is supported in NodeJS, Chrome, and yes even NativeScript.

Currently v8 for NativeScript 1.7 Android is running on v4.7.80 --   So because this is fairly old; a lot of the ES6 code has not been exposed to the engine.   As NativeScript adopts the newer v8 engines, most of the ES6 functionality should start becoming available by default.    However if you are inpatient, you can enable this in your own NativeScript application.  Please note; some of the functionality might work perfectly; but some of it might be very buggy; that is why it is behind feature flags.

To enable functionality; you need to open the "package.json" folder in your "app" folder; it typically will look like this:

{
  "name": "tns-template-hello-world",
  "main": "app.js",
  "version": "1.7.0",
  "author": {
    "name": "Telerik",
    "email": "support@telerik.com"
  },
  "description": "Nativescript hello-world project template",
  "license": "Apache-2.0",
  "keywords": [
    "telerik",
    "mobile",
    "nativescript",
    "{N}",
    "tns",
    "appbuilder",
    "template"
  ],
  "repository": {
    "type": "git",
    "url": "git://github.com/NativeScript/template-hello-world.git"
  },
  "bugs": {
    "url": "https://github.com/NativeScript/template-hello-world/issues"
  },
  "homepage": "https://github.com/NativeScript/template-hello-world",
  "android": {
    "v8Flags": "--expose_gc"
  },
  "readme": "ERROR: No README data found!",
  "_id": "tns-template-hello-world@1.7.0",
  "_from": "tns-template-hello-world@1.7.0"
}

Yes, this is the STOCK 1.7.0 hello world template definition.     Do you see about 5 lines up the package.json from the bottom?

"android": {
  "v8Flags": "--expose_gc"
}

Well, that is where you would add your flags; !!!WARNING!!! DO NOT REMOVE the --expose_gc if you remove it, your app will crash when NS attempts to manually do garbage collection.  So, trust me when I say, do not remove it!

So some of the flags you can add and use are:

  • --use_strict = Will force everything to be in strict mode
  • --harmony_proxies = Enable ES6 Proxy support
  • --harmony_collections = Enable ES6 Maps/Sets
  • --harmony_typeof = Enable ES6 Typeof
  • --harmony_scoping = Enable ES6 Scoping rules
  • --harmony_generators = Enabled ES6 Generators
  • --harmony_itteration = Enable ES6 for-of
  • --harmony_numeric_literals = Enables the new ES6 number constants
  • --harmony_strings = Enable new ES6 String features
  • --harmony_arrays = Enable new ES6 Array features
  • --harmony_maths = Enable new ES6 Math features

These are all Harmony / ES6 related; some of the ES6 code might already be exposed; some of the ES6 code doesn't work real well and might be incomplete.  Please note that some of these ES6 features may already be fully live in the engine.   As NS gets closer to 5.1 of the v8 the more feature will be completed and exposed by default without having to use these flags.   There are MANY other flags you can use to tune the v8 engine and disable functionality; however if you don't know how what they do and the ramifications of using them, I would not set them in any customer facing application.

So if you wanted to enable the new Array & new Typeof abilities your android section would be

"android": {
  "v8Flags": "--expose_gc --harmony_array --harmony_typeof"
}

One other word of caution; these are ONLY for Android; this does not effect the iOS or Windows runtimes.  So if you enable a feature in Android; that same code may or may not run on iOS or Windows.

Leave a Reply

Your email address will not be published. Required fields are marked *