Dive deeper into NodeJS - Adding Features in JS (NodeJS)

Karim Muhammad
Nov 11, 2023
2 min read
post_comment0 Comments
post_like0 Likes

#NODEJS

Nodejs is not a language, keep in mind of that. Nodejs is engine like V8 but the difference isNodejsmake Javascript Language run standalone far away browser!

Nodejsis uses V8 engine program which Google created as a part of whole Nodejs's Engine

in first line of codeNodejsprogram you will see this line

1#include<include/v8.h>
2

and this pre-processor directive that grapV8 codeas library into thisC++ program. so that you can call some function/classes that exist inside this library

1v8::Local<v8::Value> GetFoo(v8::Local<v8::Context> context,
2                            v8::Local<v8::Object> obj) {
3  v8::Isolate* isolate = context->GetIsolate();
4  v8::EscapableHandleScope handle_scope(isolate);
5
6  // The 'foo_string' handle cannot be returned from this function because
7  // it is not “escaped” with `.Escape()`.
8  v8::Local<v8::String> foo_string =
9      v8::String::NewFromUtf8(isolate, "foo").ToLocalChecked();
10
11  v8::Local<v8::Value> return_value;
12  if (obj->Get(context, foo_string).ToLocal(&return_value)) {
13    return handle_scope.Escape(return_value);
14  } else {
15    // There was a JS exception! Handle it somehow.
16    return v8::Local<v8::Value>();
17  }
18}
19

wherev8is namespace c++ helps you access classes and functions that exist insideV8 engine

like exactly

1#include<cmath>
2

which grap maths functions into your code so that you can call this function for example

1pow(5, 2) // 5 * 2 = 10
2// this function exist inside <cmath> library
3

#How to add new features/function in Javascript?

For adding features in Javascript you have to makeV8can compile this new feature.

#What feature we will add?

I will add simple function which doesn't exist normally in javascript, and i will call this function with nameGetFooand this function return what is pass it e.g"Hello World From C++"

1v8::Local<v8::Value> GetFoo(v8::Local<v8::Context> context,
2                            v8::Local<v8::Object> obj) {
3  v8::Isolate* isolate = context->GetIsolate();
4  v8::EscapableHandleScope handle_scope(isolate);
5
6  // The 'foo_string' handle cannot be returned from this function because
7  // it is not “escaped” with `.Escape()`.
8  v8::Local<v8::String> foo_string =
9      v8::String::NewFromUtf8(isolate, "foo").ToLocalChecked();
10
11  v8::Local<v8::Value> return_value;
12  if (obj->Get(context, foo_string).ToLocal(&return_value)) {
13    return handle_scope.Escape(return_value);
14  } else {
15    // There was a JS exception! Handle it somehow.
16    return v8::Local<v8::Value>();
17  }
18}
19

Now we created function calledGetFoowhen will execute? whenV8see this code in javascriptkarim_get_foo().

1v8::Local<v8::Context> CreateShellContext(v8::Isolate* isolate) {
2  // Create a template for the global object.
3  v8::Local<v8::ObjectTemplate> global = v8::ObjectTemplate::New(isolate);
4  // Bind the global 'print' function to the C++ Print callback.
5  global->Set(isolate, "karim_get_foo", v8::FunctionTemplate::New(isolate, GetFoo));
6
7    // GetFoo -> Here we binded function which we created before.
8    // karim_get_foo -> new script we added in javascript, and it will trigger C++ function `GetFoo`
9}
10

Now, when we write this javascript code

1console.log( karim_get_foo("Hello World From C++") );
2

will print inconsole

Hello World From C++

#How V8 generate Machine code for Javascript code? let's see...

1var x = "x variable";
2

what is generated machine code for this line of code?

1Code
2source_position
3kind = FUNCTION
4compiler
5full-codegen
6Instructions (size 164)
7= 0
8000001854AA38100
9;; debug: position 0
10000001854AA38101
11000001854AA38104
12000001854AA38105
13000001854AA38106
14000001854AA3810A
15000001854AA3810B
16000001A9E1DC5819
170 55
184889e5
19push rbp
20REX.W movq rbp,rsp
21push rsi
22push rdi
23push [r13-0x58]
24push rsi
251
264 56
275 57
286 41ff75a8
2910 56
3011 49ba1958dce1a9010000 REX.W movq r10
31;; object: 000001A9E1DC5819 <FixedArray
32

machine_code_for_javascript

and this machine code depend on yourMachine ProcessorandOperating System.

#nodejs #c++ #part3.1

You are not logged in.