94intmain(int argc, char* argv[]){ 95#if defined(__POSIX__) && defined(NODE_SHARED_MODE) 96// In node::PlatformInit(), we squash all signal handlers for non-shared lib 97// build. In order to run test cases against shared lib build, we also need 98// to do the same thing for shared lib build here, but only for SIGPIPE for 99// now. If node::PlatformInit() is moved to here, then this section could be 100// removed. 101 { 102struct sigaction act; 103memset(&act, 0, sizeof(act)); 104 act.sa_handler = SIG_IGN; 105 sigaction(SIGPIPE, &act, nullptr); 106 } 107#endif 108 109#if defined(__linux__) 110char** envp = environ; 111while (*envp++ != nullptr) {} 112 Elf_auxv_t* auxv = reinterpret_cast<Elf_auxv_t*>(envp); 113for (; auxv->a_type != AT_NULL; auxv++) { 114if (auxv->a_type == AT_SECURE) { 115 node::linux_at_secure = auxv->a_un.a_val; 116break; 117 } 118 } 119#endif 120// Disable stdio buffering, it interacts poorly with printf() 121// calls elsewhere in the program (e.g., any logging from V8.) 122 setvbuf(stdout, nullptr, _IONBF, 0); 123 setvbuf(stderr, nullptr, _IONBF, 0); 124return node::Start(argc, argv); 125 }
4180intStart(int argc, char** argv){ 4181 atexit([] () { uv_tty_reset_mode(); }); 4182 PlatformInit(); 4183 performance::performance_node_start = PERFORMANCE_NOW(); 4184 4185 CHECK_GT(argc, 0); 4186 4187// Hack around with the argv pointer. Used for process.title = "blah". 4188 argv = uv_setup_args(argc, argv); 4189 4190// This needs to run *before* V8::Initialize(). The const_cast is not 4191// optional, in case you're wondering. 4192int exec_argc; 4193constchar** exec_argv; 4194 Init(&argc, const_cast<constchar**>(argv), &exec_argc, &exec_argv); 4195 4196#if HAVE_OPENSSL 4197 { 4198std::string extra_ca_certs; 4199if (SafeGetenv("NODE_EXTRA_CA_CERTS", &extra_ca_certs)) 4200 crypto::UseExtraCaCerts(extra_ca_certs); 4201 } 4202#ifdef NODE_FIPS_MODE 4203// In the case of FIPS builds we should make sure 4204// the random source is properly initialized first. 4205 OPENSSL_init(); 4206#endif// NODE_FIPS_MODE 4207// V8 on Windows doesn't have a good source of entropy. Seed it from 4208// OpenSSL's pool. 4209 V8::SetEntropySource(crypto::EntropySource); 4210#endif// HAVE_OPENSSL 4211 4212 v8_platform.Initialize(v8_thread_pool_size); 4213 V8::Initialize(); 4214 performance::performance_v8_start = PERFORMANCE_NOW(); 4215 v8_initialized = true; 4216constint exit_code = 4217 Start(uv_default_loop(), argc, argv, exec_argc, exec_argv); 4218 v8_platform.StopTracingAgent(); 4219 v8_initialized = false; 4220 V8::Dispose(); 4221 4222// uv_run cannot be called from the time before the beforeExit callback 4223// runs until the program exits unless the event loop has any referenced 4224// handles after beforeExit terminates. This prevents unrefed timers 4225// that happen to terminate during shutdown from being run unsafely. 4226// Since uv_run cannot be called, uv_async handles held by the platform 4227// will never be fully cleaned up. 4228 v8_platform.Dispose(); 4229 4230delete[] exec_argv; 4231 exec_argv = nullptr; 4232 4233return exit_code; 4234 }
2849staticboolExecuteBootstrapper(Environment* env, Local<Function> bootstrapper, 2850int argc, Local<Value> argv[], 2851 Local<Value>* out){ 2852bool ret = bootstrapper->Call( 2853 env->context(), Null(env->isolate()), argc, argv).ToLocal(out); 2854 2855// If there was an error during bootstrap then it was either handled by the 2856// FatalException handler or it's unrecoverable (e.g. max call stack 2857// exceeded). Either way, clear the stack so that the AsyncCallbackScope 2858// destructor doesn't fail on the id check. 2859// There are only two ways to have a stack size > 1: 1) the user manually 2860// called MakeCallback or 2) user awaited during bootstrap, which triggered 2861// _tickCallback(). 2862if (!ret) { 2863 env->async_hooks()->clear_async_id_stack(); 2864 } 2865 2866return ret; 2867 }
// This file creates the internal module & binding loaders used by built-in // modules. In contrast, user land modules are loaded using // lib/internal/modules/cjs/loader.js (CommonJS Modules) or // lib/internal/modules/esm/* (ES Modules). // // This file is compiled and run by node.cc before bootstrap/node.js // was called, therefore the loaders are bootstraped before we start to // actually bootstrap Node.js. It creates the following objects: // // C++ binding loaders: // - process.binding(): the legacy C++ binding loader, accessible from user land // because it is an object attached to the global process object. // These C++ bindings are created using NODE_BUILTIN_MODULE_CONTEXT_AWARE() // and have their nm_flags set to NM_F_BUILTIN. We do not make any guarantees // about the stability of these bindings, but still have to take care of // compatibility issues caused by them from time to time. // - process._linkedBinding(): intended to be used by embedders to add // additional C++ bindings in their applications. These C++ bindings // can be created using NODE_MODULE_CONTEXT_AWARE_CPP() with the flag // NM_F_LINKED. // - internalBinding(): the private internal C++ binding loader, inaccessible // from user land because they are only available from NativeModule.require() // These C++ bindings are created using NODE_MODULE_CONTEXT_AWARE_INTERNAL() // and have their nm_flags set to NM_F_INTERNAL. // // Internal JavaScript module loader: // - NativeModule: a minimal module system used to load the JavaScript core // modules found in lib/**/*.js and deps/**/*.js. All core modules are // compiled into the node binary via node_javascript.cc generated by js2c.py, // so they can be loaded faster without the cost of I/O. This class makes the // lib/internal/*, deps/internal/* modules and internalBinding() available by // default to core modules, and lets the core modules require itself via // require('internal/bootstrap/loaders') even when this file is not written in // CommonJS style. // // Other objects: // - process.moduleLoadList: an array recording the bindings and the modules // loaded in the process and the order in which they are loaded.
741// bootstrap main module. 742 Module.runMain = function() { 743// Load the main module--the command line argument. 744 Module._load(process.argv[1], null, true); 745// Handle any nextTicks added in the first tick of the program 746 process._tickCallback(); 747 };
JavaScript is a class-free, object-oriented language, and as such, it uses prototypal inheritance instead of classical inheritance. This can be puzzling to programmers trained in conventional object-oriented languages like C++ and Java.
Class-based object-oriented languages, such as Java and C++, are founded on the concept of two distinct entities: classes and instances. JavaScript is a prototype-based language and so does not make this distinction: it simply has objects. JavaScript does not natively support the declaration of class hierarchies; however, JavaScript's prototype mechanism simplifies the process of adding custom properties and methods to all instances of an object. In JavaScript, you can add custom properties to objects. For example:
// Create an object "bicycle" function bicycle(){ } // Create an instance of bicycle called roadbike var roadbike = new bicycle() // Define a custom property, wheels, on roadbike roadbike.wheels = 2 A custom property added this way only exists for that instance of the object. If we create another instance of bicycle(), called mountainbike for example, mountainbike.wheels would return undefined unless the wheels property is explicitly added.
Sometimes this is exactly what is required, at other times it would be helpful to add the custom property to all instances of an object - all bicycles have wheels after all. This is where the prototype object of JavaScript is very useful. To use the prototype object, reference the keyword prototype on the object before adding the custom property to it as follows:
// First, create the "bicycle" object function bicycle(){ } // Assign the wheels property to the object's prototype bicycle.prototype.wheels = 2 All instances of bicycle() will now have the wheels property prebuilt into them.