1. Home
  2. Docs
  3. Contribute
  4. How NectarJS works?

How NectarJS works?

NetcarJS is an EcmaScript/JavaScript native compiler. It takes JS code en compile into a binary file for a specific targeted platform.

The native compilation steps

These are the compilation steps:

  • 1. NectarJS checks the JS syntax with JSLint
  • 2. NectarJS checks for modules to include
  • 2. NectarJS converts JS to C++
  • 3. NectarJS adds standard module for selected compilation environment
  • 4. NectarJS uses a C++ compiler (G++, Clang++, …) to compile the code into a native binary

How NectarJS works under the hood?

NectarJS has a central c++ file, njs.h that describes the behavior of a variable in NectarJS.

For example: this is a piece of valid C++ code with njs.h included:

var a = 0;
a = "Hello world";

A var in the NectarJS C++ environment is described like this:

  • – A type, with an enum list
  • – A pointer, that point to the location of the value of the variable
  • – Differents constructors, for each var types
  • – Different copy constructors, for each var types
  • – A generic destructor, that, depending of the context, delete the value and free the ptr

The value of the variable is stored in a register. You can find more information about this register in the memory management part.

When a var is created, a pointer is generated and a value is stored at the location of the pointer in the register. Like a JS object depending of the nature of the var this ptr will be used as reference (an object for example), or a copy of the var with a new ptr will be use as a copy (string for example).

When a new value is assigned to an existing var, the pointer is reused to store the new value.

NectarJS already supports these value types:

  • – undefined
  • – number
  • – bool
  • – object
  • – function
  • – string
  • – NaN
  • – infinity

Was this article helpful to you? Yes No

How can we help?