|
| 1 | +#include "node.h" |
| 2 | +#include "env-inl.h" |
| 3 | +#include "node_internals.h" |
| 4 | +#include "v8.h" |
| 5 | + |
| 6 | +namespace node { |
| 7 | + |
| 8 | +using v8::Array; |
| 9 | +using v8::ArrayBuffer; |
| 10 | +using v8::Function; |
| 11 | +using v8::FunctionCallbackInfo; |
| 12 | +using v8::Integer; |
| 13 | +using v8::Isolate; |
| 14 | +using v8::Local; |
| 15 | +using v8::Object; |
| 16 | +using v8::Promise; |
| 17 | +using v8::PromiseRejectMessage; |
| 18 | +using v8::Uint32Array; |
| 19 | +using v8::Value; |
| 20 | + |
| 21 | +void SetupProcessObject(const FunctionCallbackInfo<Value>& args) { |
| 22 | + Environment* env = Environment::GetCurrent(args); |
| 23 | + CHECK(args[0]->IsFunction()); |
| 24 | + env->set_push_values_to_array_function(args[0].As<Function>()); |
| 25 | +} |
| 26 | + |
| 27 | +void RunMicrotasks(const FunctionCallbackInfo<Value>& args) { |
| 28 | + args.GetIsolate()->RunMicrotasks(); |
| 29 | +} |
| 30 | + |
| 31 | +void SetupNextTick(const FunctionCallbackInfo<Value>& args) { |
| 32 | + Environment* env = Environment::GetCurrent(args); |
| 33 | + |
| 34 | + CHECK(args[0]->IsFunction()); |
| 35 | + CHECK(args[1]->IsObject()); |
| 36 | + |
| 37 | + env->set_tick_callback_function(args[0].As<Function>()); |
| 38 | + |
| 39 | + env->SetMethod(args[1].As<Object>(), "runMicrotasks", RunMicrotasks); |
| 40 | + |
| 41 | + // Values use to cross communicate with processNextTick. |
| 42 | + uint32_t* const fields = env->tick_info()->fields(); |
| 43 | + uint32_t const fields_count = env->tick_info()->fields_count(); |
| 44 | + |
| 45 | + Local<ArrayBuffer> array_buffer = |
| 46 | + ArrayBuffer::New(env->isolate(), fields, sizeof(*fields) * fields_count); |
| 47 | + |
| 48 | + args.GetReturnValue().Set(Uint32Array::New(array_buffer, 0, fields_count)); |
| 49 | +} |
| 50 | + |
| 51 | +void PromiseRejectCallback(PromiseRejectMessage message) { |
| 52 | + Local<Promise> promise = message.GetPromise(); |
| 53 | + Isolate* isolate = promise->GetIsolate(); |
| 54 | + Local<Value> value = message.GetValue(); |
| 55 | + Local<Integer> event = Integer::New(isolate, message.GetEvent()); |
| 56 | + |
| 57 | + Environment* env = Environment::GetCurrent(isolate); |
| 58 | + Local<Function> callback = env->promise_reject_function(); |
| 59 | + |
| 60 | + if (value.IsEmpty()) |
| 61 | + value = Undefined(isolate); |
| 62 | + |
| 63 | + Local<Value> args[] = { event, promise, value }; |
| 64 | + Local<Object> process = env->process_object(); |
| 65 | + |
| 66 | + callback->Call(process, arraysize(args), args); |
| 67 | +} |
| 68 | + |
| 69 | +void SetupPromises(const FunctionCallbackInfo<Value>& args) { |
| 70 | + Environment* env = Environment::GetCurrent(args); |
| 71 | + Isolate* isolate = env->isolate(); |
| 72 | + |
| 73 | + CHECK(args[0]->IsFunction()); |
| 74 | + |
| 75 | + isolate->SetPromiseRejectCallback(PromiseRejectCallback); |
| 76 | + env->set_promise_reject_function(args[0].As<Function>()); |
| 77 | +} |
| 78 | + |
| 79 | +#define BOOTSTRAP_METHOD(name, fn) env->SetMethod(bootstrapper, #name, fn) |
| 80 | + |
| 81 | +// The Bootstrapper object is an ephemeral object that is used only during |
| 82 | +// the bootstrap process of the Node.js environment. A reference to the |
| 83 | +// bootstrap object must not be kept around after the bootstrap process |
| 84 | +// completes so that it can be gc'd as soon as possible. |
| 85 | +void SetupBootstrapObject(Environment* env, |
| 86 | + Local<Object> bootstrapper) { |
| 87 | + BOOTSTRAP_METHOD(_setupProcessObject, SetupProcessObject); |
| 88 | + BOOTSTRAP_METHOD(_setupNextTick, SetupNextTick); |
| 89 | + BOOTSTRAP_METHOD(_setupPromises, SetupPromises); |
| 90 | + BOOTSTRAP_METHOD(_cpuUsage, CPUUsage); |
| 91 | + BOOTSTRAP_METHOD(_hrtime, Hrtime); |
| 92 | + BOOTSTRAP_METHOD(_memoryUsage, MemoryUsage); |
| 93 | + BOOTSTRAP_METHOD(_rawDebug, RawDebug); |
| 94 | +} |
| 95 | +#undef BOOTSTRAP_METHOD |
| 96 | + |
| 97 | +} // namespace node |
0 commit comments