Nodejs is not a language, keep in mind of that. Nodejs is engine like V8 but the difference is Nodejs make Javascript Language run standalone far away browser!
Nodejs is uses V8 engine program which Google created as a part of whole Nodejs's Engine
in first line of code Nodejs program you will see this line
#include<include/v8.h>
and this pre-processor directive that grap V8 code as library into this C++ program. so that you can call some function/classes that exist inside this library
v8::Local<v8::Value> GetFoo(v8::Local<v8::Context> context,
v8::Local<v8::Object> 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<v8::String> foo_string =
v8::String::NewFromUtf8(isolate, "foo").ToLocalChecked();
v8::Local<v8::Value> 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<v8::Value>();
}
}
where v8
is namespace c++ helps you access classes and functions that exist inside V8 engine
like exactly
#include<cmath>
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 <cmath> library
For adding features in Javascript you have to make V8 can compile this new feature.
I will add simple function which doesn't exist normally in javascript, and i will call this function with name GetFoo
and this function return what is pass it e.g "Hello World From C++"
v8::Local<v8::Value> GetFoo(v8::Local<v8::Context> context,
v8::Local<v8::Object> 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<v8::String> foo_string =
v8::String::NewFromUtf8(isolate, "foo").ToLocalChecked();
v8::Local<v8::Value> 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<v8::Value>();
}
}
Now we created function called GetFoo
when will execute? when V8 see this code in javascript karim_get_foo()
.
v8::Local<v8::Context> CreateShellContext(v8::Isolate* isolate) {
// Create a template for the global object.
v8::Local<v8::ObjectTemplate> 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 in console
Hello World From C++
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 <FixedArray
and this machine code depend on your Machine Processor and Operating System.
#nodejs #c++ #part3.1