diff options
author | Ben Noordhuis <info@bnoordhuis.nl> | 2013-07-03 06:23:44 +0400 |
---|---|---|
committer | Ben Noordhuis <info@bnoordhuis.nl> | 2013-07-06 19:44:44 +0400 |
commit | 110a9cd8db515c4d1a9ac5cd8837291da7c6c5ea (patch) | |
tree | 71e5a14a98131d89d670f842eb36bfcccab00b7b /src/node.h | |
parent | 9b3de60d3537df657e75887436a5b1df5ed80c2d (diff) |
lib, src: upgrade after v8 api change
This is a big commit that touches just about every file in the src/
directory. The V8 API has changed in significant ways. The most
important changes are:
* Binding functions take a const v8::FunctionCallbackInfo<T>& argument
rather than a const v8::Arguments& argument.
* Binding functions return void rather than v8::Handle<v8::Value>. The
return value is returned with the args.GetReturnValue().Set() family
of functions.
* v8::Persistent<T> no longer derives from v8::Handle<T> and no longer
allows you to directly dereference the object that the persistent
handle points to. This means that the common pattern of caching
oft-used JS values in a persistent handle no longer quite works,
you first need to reconstruct a v8::Local<T> from the persistent
handle with the Local<T>::New(isolate, persistent) factory method.
A handful of (internal) convenience classes and functions have been
added to make dealing with the new API a little easier.
The most visible one is node::Cached<T>, which wraps a v8::Persistent<T>
with some template sugar. It can hold arbitrary types but so far it's
exclusively used for v8::Strings (which was by far the most commonly
cached handle type.)
Diffstat (limited to 'src/node.h')
-rw-r--r-- | src/node.h | 57 |
1 files changed, 17 insertions, 40 deletions
diff --git a/src/node.h b/src/node.h index 59d03a355ae..16bfec56d82 100644 --- a/src/node.h +++ b/src/node.h @@ -95,10 +95,6 @@ v8::Handle<v8::Object> SetupProcessObject(int argc, char *argv[]); void Load(v8::Handle<v8::Object> process); void EmitExit(v8::Handle<v8::Object> process); -#define NODE_PSYMBOL(s) \ - v8::Persistent<v8::String>::New(v8::Isolate::GetCurrent(), \ - v8::String::NewSymbol(s)) - /* Converts a unixtime to V8 Date */ #define NODE_UNIXTIME_V8(t) v8::Date::New(1000*static_cast<double>(t)) #define NODE_V8_UNIXTIME(v) (static_cast<double>((v)->NumberValue())/1000.0); @@ -109,25 +105,25 @@ void EmitExit(v8::Handle<v8::Object> process); static_cast<v8::PropertyAttribute>( \ v8::ReadOnly|v8::DontDelete)) -template <typename target_t> -void SetMethod(target_t obj, const char* name, - v8::InvocationCallback callback) -{ - obj->Set(v8::String::NewSymbol(name), - v8::FunctionTemplate::New(callback)->GetFunction()); +// Used to be a macro, hence the uppercase name. +template <typename TypeName> +inline void NODE_SET_METHOD(TypeName& recv, + const char* name, + v8::FunctionCallback callback) { + v8::Local<v8::FunctionTemplate> t = v8::FunctionTemplate::New(callback); + recv->Set(v8::String::New(name), t->GetFunction()); } - -template <typename target_t> -void SetPrototypeMethod(target_t target, - const char* name, v8::InvocationCallback callback) -{ - v8::Local<v8::FunctionTemplate> templ = v8::FunctionTemplate::New(callback); - target->PrototypeTemplate()->Set(v8::String::NewSymbol(name), templ); +#define NODE_SET_METHOD node::NODE_SET_METHOD + +// Used to be a macro, hence the uppercase name. +// Not a template because it only makes sense for FunctionTemplates. +inline void NODE_SET_PROTOTYPE_METHOD(v8::Handle<v8::FunctionTemplate> recv, + const char* name, + v8::FunctionCallback callback) { + v8::Local<v8::FunctionTemplate> t = v8::FunctionTemplate::New(callback); + recv->PrototypeTemplate()->Set(v8::String::New(name), t->GetFunction()); } - -// for backwards compatibility -#define NODE_SET_METHOD node::SetMethod -#define NODE_SET_PROTOTYPE_METHOD node::SetPrototypeMethod +#define NODE_SET_PROTOTYPE_METHOD node::NODE_SET_PROTOTYPE_METHOD enum encoding {ASCII, UTF8, BASE64, UCS2, BINARY, HEX, BUFFER}; enum encoding ParseEncoding(v8::Handle<v8::Value> encoding_v, @@ -151,25 +147,6 @@ NODE_EXTERN ssize_t DecodeWrite(char *buf, v8::Local<v8::Object> BuildStatsObject(const uv_stat_t* s); -static inline v8::Persistent<v8::Function>* cb_persist(v8::Local<v8::Value> v) { - v8::Persistent<v8::Function>* fn = new v8::Persistent<v8::Function>(); - *fn = v8::Persistent<v8::Function>::New(v8::Isolate::GetCurrent(), - v.As<v8::Function>()); - return fn; -} - -static inline v8::Persistent<v8::Function>* cb_unwrap(void *data) { - v8::Persistent<v8::Function> *cb = - reinterpret_cast<v8::Persistent<v8::Function>*>(data); - assert((*cb)->IsFunction()); - return cb; -} - -static inline void cb_destroy(v8::Persistent<v8::Function> * cb) { - cb->Dispose(v8::Isolate::GetCurrent()); - delete cb; -} - NODE_EXTERN v8::Local<v8::Value> ErrnoException(int errorno, const char *syscall = NULL, const char *msg = "", |