From 7928f78a70be3ea04aa6652ce1b5284b2bc3342f Mon Sep 17 00:00:00 2001 From: Matheus Marchini Date: Mon, 25 Feb 2019 16:00:56 -0300 Subject: [PATCH] src: add rudimentary Promise support This patch allows llnode to list Promise objects with findjsobjects, findjsinstances and findrefs. We can investigate more fancy Promise features in the future (such as listing handlers, Promise status, etc.). For Node.js v10.x, since we don't have the JS_PROMISE type as postmortem metadata, we assume JS_PROMISE is the next type after JS_MESSAGE_OBJECT_TYPE. This is a safe assumption for Node.js v10.x, v12.x has the JS_PROMISE type, and v8.x is not supported anymore. ```console $ git log v10.0.0..v10.17.0 -L :InstanceType:deps/v8/src/objects.h | grep -C2 "JS_PROMISE" JS_MAP_VALUE_ITERATOR_TYPE, JS_MESSAGE_OBJECT_TYPE, JS_PROMISE_TYPE, JS_REGEXP_TYPE, JS_REGEXP_STRING_ITERATOR_TYPE, -- JS_MAP_VALUE_ITERATOR_TYPE, JS_MESSAGE_OBJECT_TYPE, JS_PROMISE_TYPE, JS_REGEXP_TYPE, + JS_REGEXP_STRING_ITERATOR_TYPE, ``` --- src/llv8-constants.cc | 7 +++++++ src/llv8-constants.h | 1 + src/llv8-inl.h | 1 + test/fixtures/inspect-scenario.js | 2 ++ test/plugin/inspect-test.js | 4 ++++ 5 files changed, 15 insertions(+) diff --git a/src/llv8-constants.cc b/src/llv8-constants.cc index 2f1f4684..3b752a05 100644 --- a/src/llv8-constants.cc +++ b/src/llv8-constants.cc @@ -518,6 +518,13 @@ void Types::Load() { kLastContextType = LoadConstant("LastContextType"); kJSErrorType = LoadConstant("type_JSError__JS_ERROR_TYPE"); + kJSPromiseType = LoadConstant("type_JSPromise__JS_PROMISE_TYPE"); + if (kJSPromiseType == -1) { + // NOTE(mmarchini): On Node.js v10.x, JS_PROMISE always comes after + // JS_MESSAGE_OBJECT_TYPE in the InstanceType enum. + kJSPromiseType = + LoadConstant("type_JSMessageObject__JS_MESSAGE_OBJECT_TYPE") + 1; + } kHeapNumberType = LoadConstant("type_HeapNumber__HEAP_NUMBER_TYPE"); kMapType = LoadConstant("type_Map__MAP_TYPE"); kGlobalObjectType = diff --git a/src/llv8-constants.h b/src/llv8-constants.h index cf297419..4bd6cf89 100644 --- a/src/llv8-constants.h +++ b/src/llv8-constants.h @@ -507,6 +507,7 @@ class Types : public Module { int64_t kLastContextType; int64_t kJSErrorType; + int64_t kJSPromiseType; int64_t kHeapNumberType; int64_t kMapType; int64_t kGlobalObjectType; diff --git a/src/llv8-inl.h b/src/llv8-inl.h index beda40cb..83b5e82e 100644 --- a/src/llv8-inl.h +++ b/src/llv8-inl.h @@ -314,6 +314,7 @@ inline bool JSObject::IsObjectType(LLV8* v8, int64_t type) { return type == v8->types()->kJSObjectType || type == v8->types()->kJSAPIObjectType || type == v8->types()->kJSErrorType || + type == v8->types()->kJSPromiseType || type == v8->types()->kJSSpecialAPIObjectType; } diff --git a/test/fixtures/inspect-scenario.js b/test/fixtures/inspect-scenario.js index 1864a8b7..db08ee39 100644 --- a/test/fixtures/inspect-scenario.js +++ b/test/fixtures/inspect-scenario.js @@ -67,6 +67,8 @@ function closure() { c.hashmap['stringifiedError'] = new Error('test'); c.hashmap['stringifiedErrorStack'] = c.hashmap['stringifiedError'].stack; + c.hashmap['promise'] = new Promise(() => {}); + c.hashmap[0] = null; c.hashmap[4] = undefined; c.hashmap[23] = /regexp/; diff --git a/test/plugin/inspect-test.js b/test/plugin/inspect-test.js index 92fe0c62..3e55b040 100644 --- a/test/plugin/inspect-test.js +++ b/test/plugin/inspect-test.js @@ -187,6 +187,10 @@ const hashMapTests = { }); } }, + 'promise': { + re: /.promise=(0x[0-9a-f]+):/, + desc: '.promise Promise property' + }, // .array=0x000003df9cbe7919:, 'array': { re: /.array=(0x[0-9a-f]+):/,