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

#include

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

v8::Local GetFoo(v8::Local context, v8::Local obj) { v8::Isolate* isolate = context->GetIsolate(); v8::EscapableHandleScope handle_scope(isolate); // The 'foo_string' handle cannot be returned from this function because // it is not “escaped” with `.Escape()`. v8::Local foo_string = v8::String::NewFromUtf8(isolate, "foo").ToLocalChecked(); v8::Local return_value; if (obj->Get(context, foo_string).ToLocal(&return_value)) { return handle_scope.Escape(return_value); } else { // There was a JS exception! Handle it somehow. return v8::Local(); } }

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

like exactly

#include

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

pow(5, 2) // 5 * 2 = 10 // this function exist inside library

#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++"

v8::Local GetFoo(v8::Local context, v8::Local obj) { v8::Isolate* isolate = context->GetIsolate(); v8::EscapableHandleScope handle_scope(isolate); // The 'foo_string' handle cannot be returned from this function because // it is not “escaped” with `.Escape()`. v8::Local foo_string = v8::String::NewFromUtf8(isolate, "foo").ToLocalChecked(); v8::Local return_value; if (obj->Get(context, foo_string).ToLocal(&return_value)) { return handle_scope.Escape(return_value); } else { // There was a JS exception! Handle it somehow. return v8::Local(); } }

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

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

Now, when we write this javascript code

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

will print inconsole

Hello World From C++

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

var x = "x variable";

what is generated machine code for this line of code?

Code source_position kind = FUNCTION compiler full-codegen Instructions (size 164) = 0 000001854AA38100 ;; debug: position 0 000001854AA38101 000001854AA38104 000001854AA38105 000001854AA38106 000001854AA3810A 000001854AA3810B 000001A9E1DC5819 0 55 4889e5 push rbp REX.W movq rbp,rsp push rsi push rdi push [r13-0x58] push rsi 1 4 56 5 57 6 41ff75a8 10 56 11 49ba1958dce1a9010000 REX.W movq r10 ;; object: 000001A9E1DC5819

machine_code_for_javascript

and this machine code depend on yourMachine ProcessorandOperating System.

#nodejs #c++ #part3.1

You are not logged in.