|
|
|
// Copyright Joyent, Inc. and other Node contributors.
|
|
|
|
//
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
// copy of this software and associated documentation files (the
|
|
|
|
// "Software"), to deal in the Software without restriction, including
|
|
|
|
// without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
// distribute, sublicense, and/or sell copies of the Software, and to permit
|
|
|
|
// persons to whom the Software is furnished to do so, subject to the
|
|
|
|
// following conditions:
|
|
|
|
//
|
|
|
|
// The above copyright notice and this permission notice shall be included
|
|
|
|
// in all copies or substantial portions of the Software.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
|
|
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
|
|
|
|
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
|
|
|
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
|
|
|
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
|
|
|
|
// USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
|
|
|
|
#ifndef SRC_ASYNC_WRAP_H_
|
|
|
|
#define SRC_ASYNC_WRAP_H_
|
|
|
|
|
|
|
|
#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
|
|
|
|
|
|
|
|
#include "base-object.h"
|
|
|
|
#include "v8.h"
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
|
|
|
|
namespace node {
|
|
|
|
|
|
|
|
#define NODE_ASYNC_ID_OFFSET 0xA1C
|
|
|
|
|
|
|
|
#define NODE_ASYNC_NON_CRYPTO_PROVIDER_TYPES(V) \
|
|
|
|
V(NONE) \
|
|
|
|
V(FSEVENTWRAP) \
|
|
|
|
V(FSREQWRAP) \
|
|
|
|
V(GETADDRINFOREQWRAP) \
|
|
|
|
V(GETNAMEINFOREQWRAP) \
|
|
|
|
V(HTTPPARSER) \
|
|
|
|
V(JSSTREAM) \
|
|
|
|
V(PIPECONNECTWRAP) \
|
|
|
|
V(PIPEWRAP) \
|
|
|
|
V(PROCESSWRAP) \
|
|
|
|
V(PROMISE) \
|
|
|
|
V(QUERYWRAP) \
|
|
|
|
V(SHUTDOWNWRAP) \
|
|
|
|
V(SIGNALWRAP) \
|
|
|
|
V(STATWATCHER) \
|
|
|
|
V(TCPCONNECTWRAP) \
|
|
|
|
V(TCPWRAP) \
|
|
|
|
V(TIMERWRAP) \
|
|
|
|
V(TTYWRAP) \
|
|
|
|
V(UDPSENDWRAP) \
|
|
|
|
V(UDPWRAP) \
|
|
|
|
V(WRITEWRAP) \
|
|
|
|
V(ZLIB)
|
|
|
|
|
|
|
|
#if HAVE_OPENSSL
|
|
|
|
#define NODE_ASYNC_CRYPTO_PROVIDER_TYPES(V) \
|
|
|
|
V(SSLCONNECTION) \
|
|
|
|
V(PBKDF2REQUEST) \
|
|
|
|
V(RANDOMBYTESREQUEST) \
|
|
|
|
V(TLSWRAP)
|
|
|
|
#else
|
|
|
|
#define NODE_ASYNC_CRYPTO_PROVIDER_TYPES(V)
|
|
|
|
#endif // HAVE_OPENSSL
|
|
|
|
|
|
|
|
#define NODE_ASYNC_PROVIDER_TYPES(V) \
|
|
|
|
NODE_ASYNC_NON_CRYPTO_PROVIDER_TYPES(V) \
|
|
|
|
NODE_ASYNC_CRYPTO_PROVIDER_TYPES(V)
|
|
|
|
|
|
|
|
class Environment;
|
|
|
|
|
|
|
|
class AsyncWrap : public BaseObject {
|
|
|
|
public:
|
|
|
|
enum ProviderType {
|
|
|
|
#define V(PROVIDER) \
|
|
|
|
PROVIDER_ ## PROVIDER,
|
|
|
|
NODE_ASYNC_PROVIDER_TYPES(V)
|
|
|
|
#undef V
|
|
|
|
PROVIDERS_LENGTH,
|
|
|
|
};
|
|
|
|
|
|
|
|
AsyncWrap(Environment* env,
|
|
|
|
v8::Local<v8::Object> object,
|
|
|
|
ProviderType provider,
|
|
|
|
bool silent = false);
|
|
|
|
|
|
|
|
virtual ~AsyncWrap();
|
|
|
|
|
|
|
|
static void Initialize(v8::Local<v8::Object> target,
|
|
|
|
v8::Local<v8::Value> unused,
|
|
|
|
v8::Local<v8::Context> context);
|
|
|
|
|
async_wrap,src: add GetAsyncId() method
Allow handles to retrieve their own uid's by adding a new method on the
FunctionTemplates. Implementation of these into all other classes will
come in a future commit.
Add the method AsyncWrap::GetAsyncId() to all inheriting class objects
so the uid of the handle can be retrieved from JS.
In all applicable locations, run ClearWrap() on the object holding the
pointer so that it never points to invalid memory and make sure Wrap()
is always run so the class pointer is correctly attached to the object
and can be retrieved so GetAsyncId() can be run.
In many places a class instance was not removing its own pointer from
object() in the destructor. This left an invalid pointer in the JS
object that could cause the application to segfault under certain
conditions.
Remove ClearWrap() from ReqWrap for continuity. The ReqWrap constructor
was not the one to call Wrap(), so it shouldn't be the one to call
ClearWrap().
Wrap() has been added to all constructors that inherit from AsyncWrap.
Normally it's the child most class. Except in the case of HandleWrap.
Which must be the constructor that runs Wrap() because the class pointer
is retrieved for certain calls and because other child classes have
multiple inheritance to pointer to the HandleWrap needs to be stored.
ClearWrap() has been placed in all FunctionTemplate constructors so that
no random values are returned when running getAsyncId(). ClearWrap() has
also been placed in all class destructors, except in those that use
MakeWeak() because the destructor will run during GC. Making the
object() inaccessible.
It could be simplified to where AsyncWrap sets the internal pointer,
then if an inheriting class needs one of it's own it could set it again.
But the inverse would need to be true also, where AsyncWrap then also
runs ClearWeak. Unforunately because some of the handles are cleaned up
during GC that's impossible. Also in the case of ReqWrap it runs Reset()
in the destructor, making the object() inaccessible. Meaning,
ClearWrap() must be run by the class that runs Wrap(). There's currently
no generalized way of taking care of this across all instances of
AsyncWrap.
I'd prefer that there be checks in there for these things, but haven't
found a way to place them that wouldn't be just as unreliable.
Add test that checks all resources that can run getAsyncId(). Would like
a way to enforce that any new classes that can also run getAsyncId() are
tested, but don't have one.
PR-URL: https://github.com/nodejs/node/pull/12892
Ref: https://github.com/nodejs/node/pull/11883
Ref: https://github.com/nodejs/node/pull/8531
Reviewed-By: Andreas Madsen <amwebdk@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Sam Roberts <vieuxtech@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Refael Ackermann <refack@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com>
8 years ago
|
|
|
static void GetAsyncId(const v8::FunctionCallbackInfo<v8::Value>& args);
|
|
|
|
static void PushAsyncIds(const v8::FunctionCallbackInfo<v8::Value>& args);
|
|
|
|
static void PopAsyncIds(const v8::FunctionCallbackInfo<v8::Value>& args);
|
|
|
|
static void ClearIdStack(const v8::FunctionCallbackInfo<v8::Value>& args);
|
|
|
|
static void AsyncReset(const v8::FunctionCallbackInfo<v8::Value>& args);
|
|
|
|
static void QueueDestroyId(const v8::FunctionCallbackInfo<v8::Value>& args);
|
|
|
|
|
|
|
|
static void EmitAsyncInit(Environment* env,
|
|
|
|
v8::Local<v8::Object> object,
|
|
|
|
v8::Local<v8::String> type,
|
|
|
|
double id,
|
|
|
|
double trigger_id);
|
|
|
|
|
|
|
|
static bool EmitBefore(Environment* env, double id);
|
|
|
|
static bool EmitAfter(Environment* env, double id);
|
|
|
|
|
|
|
|
inline ProviderType provider_type() const;
|
|
|
|
|
|
|
|
inline double get_id() const;
|
|
|
|
|
|
|
|
inline double get_trigger_id() const;
|
|
|
|
|
|
|
|
void AsyncReset(bool silent = false);
|
|
|
|
|
|
|
|
// Only call these within a valid HandleScope.
|
|
|
|
// TODO(trevnorris): These should return a MaybeLocal.
|
|
|
|
v8::Local<v8::Value> MakeCallback(const v8::Local<v8::Function> cb,
|
|
|
|
int argc,
|
|
|
|
v8::Local<v8::Value>* argv);
|
|
|
|
inline v8::Local<v8::Value> MakeCallback(const v8::Local<v8::String> symbol,
|
|
|
|
int argc,
|
|
|
|
v8::Local<v8::Value>* argv);
|
|
|
|
inline v8::Local<v8::Value> MakeCallback(uint32_t index,
|
|
|
|
int argc,
|
|
|
|
v8::Local<v8::Value>* argv);
|
|
|
|
|
|
|
|
virtual size_t self_size() const = 0;
|
|
|
|
|
|
|
|
private:
|
|
|
|
inline AsyncWrap();
|
|
|
|
const ProviderType provider_type_;
|
|
|
|
// Because the values may be Reset(), cannot be made const.
|
|
|
|
double async_id_;
|
|
|
|
double trigger_id_;
|
|
|
|
};
|
|
|
|
|
|
|
|
void LoadAsyncWrapperInfo(Environment* env);
|
|
|
|
|
|
|
|
bool DomainEnter(Environment* env, v8::Local<v8::Object> object);
|
|
|
|
bool DomainExit(Environment* env, v8::Local<v8::Object> object);
|
|
|
|
|
|
|
|
} // namespace node
|
|
|
|
|
|
|
|
#endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
|
|
|
|
|
|
|
|
#endif // SRC_ASYNC_WRAP_H_
|