@@ -47,17 +47,20 @@ using v8::FunctionCallbackInfo;
4747using v8::FunctionTemplate;
4848using v8::HandleScope;
4949using v8::IndexedPropertyHandlerConfiguration;
50+ using v8::Int32;
5051using v8::Integer;
5152using v8::Isolate;
5253using v8::Local;
5354using v8::Maybe;
5455using v8::MaybeLocal;
56+ using v8::MeasureMemoryMode;
5557using v8::Name;
5658using v8::NamedPropertyHandlerConfiguration;
5759using v8::Number;
5860using v8::Object;
5961using v8::ObjectTemplate;
6062using v8::PrimitiveArray;
63+ using v8::Promise;
6164using v8::PropertyAttribute;
6265using v8::PropertyCallbackInfo;
6366using v8::PropertyDescriptor;
@@ -1203,11 +1206,39 @@ static void WatchdogHasPendingSigint(const FunctionCallbackInfo<Value>& args) {
12031206 args.GetReturnValue ().Set (ret);
12041207}
12051208
1209+ static void MeasureMemory (const FunctionCallbackInfo<Value>& args) {
1210+ CHECK (args[0 ]->IsInt32 ());
1211+ int32_t mode = args[0 ].As <v8::Int32>()->Value ();
1212+ Isolate* isolate = args.GetIsolate ();
1213+ Environment* env = Environment::GetCurrent (args);
1214+ Local<Context> context;
1215+ if (args[1 ]->IsUndefined ()) {
1216+ context = isolate->GetCurrentContext ();
1217+ } else {
1218+ CHECK (args[1 ]->IsObject ());
1219+ ContextifyContext* sandbox =
1220+ ContextifyContext::ContextFromContextifiedSandbox (env,
1221+ args[1 ].As <Object>());
1222+ CHECK_NOT_NULL (sandbox);
1223+ context = sandbox->context ();
1224+ if (context.IsEmpty ()) { // Not yet fully initilaized
1225+ return ;
1226+ }
1227+ }
1228+ v8::Local<v8::Promise> promise;
1229+ if (!isolate->MeasureMemory (context, static_cast <v8::MeasureMemoryMode>(mode))
1230+ .ToLocal (&promise)) {
1231+ return ;
1232+ }
1233+ args.GetReturnValue ().Set (promise);
1234+ }
1235+
12061236void Initialize (Local<Object> target,
12071237 Local<Value> unused,
12081238 Local<Context> context,
12091239 void * priv) {
12101240 Environment* env = Environment::GetCurrent (context);
1241+ Isolate* isolate = env->isolate ();
12111242 ContextifyContext::Init (env, target);
12121243 ContextifyScript::Init (env, target);
12131244
@@ -1224,6 +1255,19 @@ void Initialize(Local<Object> target,
12241255
12251256 env->set_compiled_fn_entry_template (tpl->InstanceTemplate ());
12261257 }
1258+
1259+ Local<Object> constants = Object::New (env->isolate ());
1260+ Local<Object> measure_memory = Object::New (env->isolate ());
1261+ Local<Object> memory_mode = Object::New (env->isolate ());
1262+ MeasureMemoryMode SUMMARY = MeasureMemoryMode::kSummary ;
1263+ MeasureMemoryMode DETAILED = MeasureMemoryMode::kDetailed ;
1264+ NODE_DEFINE_CONSTANT (memory_mode, SUMMARY);
1265+ NODE_DEFINE_CONSTANT (memory_mode, DETAILED);
1266+ READONLY_PROPERTY (measure_memory, " mode" , memory_mode);
1267+ READONLY_PROPERTY (constants, " measureMemory" , measure_memory);
1268+ target->Set (context, env->constants_string (), constants).Check ();
1269+
1270+ env->SetMethod (target, " measureMemory" , MeasureMemory);
12271271}
12281272
12291273} // namespace contextify
0 commit comments