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<include/v8.h>
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<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>();
}
}
wherev8
is namespace c++ helps you access classes and functions that exist insideV8 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 makeV8can compile this new feature.
I will add simple function which doesn't exist normally in javascript, and i will call this function with nameGetFoo
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 calledGetFoo
when will execute? whenV8see this code in javascriptkarim_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 inconsole
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 yourMachine ProcessorandOperating System.
#nodejs #c++ #part3.1