Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Discuss a possible redesign of NanNew(...) #210

Merged
merged 24 commits into from
Jan 14, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
4600596
Added a sketch to discuss a redesign of NanNew(...)
agnat Oct 30, 2014
9e883ea
Cosmetics.
agnat Oct 30, 2014
260753e
Added v8::Date construction.
agnat Oct 30, 2014
8e5d245
Added integer type conversion.
agnat Oct 30, 2014
298d4c3
Fixed indentation.
agnat Oct 30, 2014
40dc82a
Added v0.10 support and moved things into implementation files
agnat Oct 31, 2014
a263261
Tell windows about π.
agnat Oct 31, 2014
b18b364
Fixed a typo.
agnat Oct 31, 2014
5bfe710
Moved API to header, added tap test helper and script support [WIP]
agnat Oct 31, 2014
220380d
Added some more C++ tests.
agnat Oct 31, 2014
830d360
Another attempt to fix the windows build,
agnat Oct 31, 2014
880ea4b
Wrote some more tests.
agnat Oct 31, 2014
1d7bf7c
Moved NanTap testing adapter
agnat Nov 1, 2014
4f22214
Added tests for arrays, dates and (unbound) scripts
agnat Nov 1, 2014
2e883bf
Added NanRunScript() test
agnat Nov 1, 2014
48c13d1
Added Signature and FunctionTemplate
agnat Nov 1, 2014
06e09b2
Added Boolean-, Number- and StringObject
agnat Nov 1, 2014
a9ae823
Added regression test for #212.
agnat Nov 2, 2014
2af0e94
Added Persistent and Handle to Local conversion.
agnat Nov 2, 2014
0ad686f
Added nasty string constructors for bug compatibility.
agnat Nov 3, 2014
1ae238c
NanNew redesign: Moving in.
agnat Nov 3, 2014
22cfc46
NanNew redesign: Finalized
agnat Nov 3, 2014
a3d40e6
NanNew redesign: Fixed build using the old implementation.
agnat Nov 3, 2014
090a6e6
NanNew redesign: Fix - Use Script::New instead of Script::Compile
agnat Nov 6, 2014
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 67 additions & 10 deletions nan.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,23 @@

#undef notset


#if (NODE_MODULE_VERSION < 12)
typedef v8::InvocationCallback NanFunctionCallback;
typedef v8::Script NanUnboundScript;
typedef v8::Script NanBoundScript;
#else
typedef v8::FunctionCallback NanFunctionCallback;
typedef v8::UnboundScript NanUnboundScript;
typedef v8::Script NanBoundScript;
#endif

#define NAN_NEW_NAN_NEW

#ifdef NAN_NEW_NAN_NEW
# include <nan_new.h>
#endif

// some generic helpers

template<typename T> NAN_INLINE bool NanSetPointerSafe(
Expand Down Expand Up @@ -176,8 +193,7 @@ NAN_INLINE uint32_t NanUInt32OptionValue(
# define _NAN_INDEX_QUERY_ARGS _NAN_INDEX_QUERY_ARGS_TYPE args
# define _NAN_INDEX_QUERY_RETURN_TYPE void

typedef v8::FunctionCallback NanFunctionCallback;

#ifndef NAN_NEW_NAN_NEW
template<typename T>
NAN_INLINE v8::Local<T> NanNew() {
return T::New(v8::Isolate::GetCurrent());
Expand Down Expand Up @@ -239,9 +255,6 @@ NAN_INLINE uint32_t NanUInt32OptionValue(
return v8::Date::New(v8::Isolate::GetCurrent(), time).As<v8::Date>();
}

typedef v8::UnboundScript NanUnboundScript;
typedef v8::Script NanBoundScript;

template<typename T, typename P>
NAN_INLINE v8::Local<T> NanNew(
P s
Expand Down Expand Up @@ -493,6 +506,7 @@ NAN_INLINE uint32_t NanUInt32OptionValue(
v8::String::ExternalAsciiStringResource *resource) {
return v8::String::NewExternal(v8::Isolate::GetCurrent(), resource);
}
#endif

# define NanScope() v8::HandleScope scope(v8::Isolate::GetCurrent())
# define NanEscapableScope() \
Expand Down Expand Up @@ -1037,13 +1051,12 @@ NAN_INLINE _NanWeakCallbackInfo<T, P>* NanMakeWeakPersistent(
# define _NAN_INDEX_QUERY_ARGS _NAN_INDEX_QUERY_ARGS_TYPE args
# define _NAN_INDEX_QUERY_RETURN_TYPE v8::Handle<v8::Integer>

typedef v8::InvocationCallback NanFunctionCallback;

NAN_DEPRECATED NAN_INLINE v8::Local<v8::String> NanSymbol(
const char* data, int length = -1) {
return v8::String::NewSymbol(data, length);
}

#ifndef NAN_NEW_NAN_NEW
template<typename T>
NAN_INLINE v8::Local<T> NanNew() {
return T::New();
Expand Down Expand Up @@ -1134,9 +1147,6 @@ NAN_INLINE _NanWeakCallbackInfo<T, P>* NanMakeWeakPersistent(
return v8::Local<v8::Boolean>::New(v8::Boolean::New(value));
}

typedef v8::Script NanUnboundScript;
typedef v8::Script NanBoundScript;

template<typename T, typename P>
NAN_INLINE v8::Local<T> NanNew(
P s
Expand Down Expand Up @@ -1327,6 +1337,8 @@ NAN_INLINE _NanWeakCallbackInfo<T, P>* NanMakeWeakPersistent(
return v8::String::NewExternal(resource);
}

#endif

# define NanScope() v8::HandleScope scope
# define NanEscapableScope() v8::HandleScope scope
# define NanEscapeScope(val) scope.Close(val)
Expand Down Expand Up @@ -2413,4 +2425,49 @@ NAN_INLINE void NanSetInstanceTemplate(
NanSetTemplate(templ->InstanceTemplate(), name, value, attributes);
}

//=== Export ==================================================================

inline
void
NanExport(v8::Handle<v8::Object> target, const char * name,
NanFunctionCallback f)
{
target->Set(NanNew<v8::String>(name),
NanNew<v8::FunctionTemplate>(f)->GetFunction());
}

//=== Tap Reverse Binding =====================================================

struct NanTap {
NanTap(v8::Handle<v8::Value> t) : t_() {
NanAssignPersistent(t_, t->ToObject());
};
~NanTap() { NanDisposePersistent(t_); } // not sure if neccessary

inline void plan(int i) {
v8::Handle<v8::Value> arg = NanNew(i);
NanMakeCallback(NanNew(t_), "plan", 1, &arg);
}

inline void ok(bool isOk, const char * msg = NULL) {
v8::Handle<v8::Value> args[2];
args[0] = NanNew(isOk);
if (msg) args[1] = NanNew(msg);
NanMakeCallback(NanNew(t_), "ok", msg ? 2 : 1, args);
}

private:

v8::Persistent<v8::Object> t_;
};

#define NAN_STRINGIZE2(x) #x
#define NAN_STRINGIZE(x) NAN_STRINGIZE2(x)
#define NAN_TEST_EXPRESSION(expression) \
( expression ), __FILE__ ":" NAN_STRINGIZE(__LINE__) ": " #expression

#define return_NanValue(v) NanReturnValue(v)
#define return_NanUndefined() NanReturnUndefined()
#define NAN_EXPORT(target, function) NanExport(target, #function, function)

#endif // NAN_H_
213 changes: 213 additions & 0 deletions nan_implementation_12.inl
Original file line number Diff line number Diff line change
@@ -0,0 +1,213 @@

//==============================================================================
// node v0.11 implementation
//==============================================================================


namespace NanIntern {

//=== Array ====================================================================

Factory<v8::Array>::return_t
Factory<v8::Array>::New() {
return v8::Array::New(v8::Isolate::GetCurrent());
}

Factory<v8::Array>::return_t
Factory<v8::Array>::New(int length) {
return v8::Array::New(v8::Isolate::GetCurrent(), length);
}

//=== Boolean ==================================================================

Factory<v8::Boolean>::return_t
Factory<v8::Boolean>::New(bool value) {
return v8::Boolean::New(v8::Isolate::GetCurrent(), value);
}

//=== Boolean Object ===========================================================

Factory<v8::BooleanObject>::return_t
Factory<v8::BooleanObject>::New(bool value) {
return v8::BooleanObject::New(value).As<v8::BooleanObject>();
}

//=== Date =====================================================================

Factory<v8::Date>::return_t
Factory<v8::Date>::New(double value) {
return v8::Date::New(v8::Isolate::GetCurrent(), value).As<v8::Date>();
}

//=== External =================================================================

Factory<v8::External>::return_t
Factory<v8::External>::New(void * value) {
return v8::External::New(v8::Isolate::GetCurrent(), value);
}

//=== Function Template ========================================================

Factory<v8::FunctionTemplate>::return_t
Factory<v8::FunctionTemplate>::New( NanFunctionCallback callback
, v8::Handle<v8::Value> data
, v8::Handle<v8::Signature> signature)
{
return v8::FunctionTemplate::New(v8::Isolate::GetCurrent(), callback, data,
signature);
}

//=== Number ===================================================================

Factory<v8::Number>::return_t
Factory<v8::Number>::New(double value) {
return v8::Number::New(v8::Isolate::GetCurrent(), value);
}

//=== Number Object ============================================================

Factory<v8::NumberObject>::return_t
Factory<v8::NumberObject>::New(double value) {
return v8::NumberObject::New( v8::Isolate::GetCurrent()
, value).As<v8::NumberObject>();
}

//=== Integer, Int32 and Uint32 ================================================

template <typename T>
typename IntegerFactory<T>::return_t
IntegerFactory<T>::New(int32_t value) {
return To<T>(T::New(v8::Isolate::GetCurrent(), value));
}

template <typename T>
typename IntegerFactory<T>::return_t
IntegerFactory<T>::New(uint32_t value) {
return To<T>(T::NewFromUnsigned(v8::Isolate::GetCurrent(), value));
}

Factory<v8::Uint32>::return_t
Factory<v8::Uint32>::New(int32_t value) {
return To<v8::Uint32>(v8::Uint32::NewFromUnsigned(v8::Isolate::GetCurrent(), value));
}

Factory<v8::Uint32>::return_t
Factory<v8::Uint32>::New(uint32_t value) {
return To<v8::Uint32>(v8::Uint32::NewFromUnsigned(v8::Isolate::GetCurrent(), value));
}

//=== Object ===================================================================

Factory<v8::Object>::return_t
Factory<v8::Object>::New() {
return v8::Object::New(v8::Isolate::GetCurrent());
}

//=== RegExp ===================================================================

Factory<v8::RegExp>::return_t
Factory<v8::RegExp>::New(v8::Handle<v8::String> pattern, v8::RegExp::Flags flags) {
return v8::RegExp::New(pattern, flags);
}

//=== Script ===================================================================

Factory<v8::Script>::return_t
Factory<v8::Script>::New( v8::Local<v8::String> source) {
v8::ScriptCompiler::Source src(source);
return v8::ScriptCompiler::Compile(v8::Isolate::GetCurrent(), &src);
}

Factory<v8::Script>::return_t
Factory<v8::Script>::New( v8::Local<v8::String> source
, v8::ScriptOrigin const& origin)
{
v8::ScriptCompiler::Source src(source, origin);
return v8::ScriptCompiler::Compile(v8::Isolate::GetCurrent(), &src);
}

//=== Signature ================================================================

Factory<v8::Signature>::return_t
Factory<v8::Signature>::New( Factory<v8::Signature>::FTH receiver
, int argc
, Factory<v8::Signature>::FTH argv[])
{
return v8::Signature::New(v8::Isolate::GetCurrent(), receiver, argc, argv);
}

//=== String ===================================================================

Factory<v8::String>::return_t
Factory<v8::String>::New(const char * value, int length) {
return v8::String::NewFromUtf8(v8::Isolate::GetCurrent(), value,
v8::String::kNormalString, length);
}

Factory<v8::String>::return_t
Factory<v8::String>::New(std::string const& value) {
return v8::String::NewFromUtf8(v8::Isolate::GetCurrent(),
&*value.begin(), v8::String::kNormalString, value.size());
}

Factory<v8::String>::return_t
Factory<v8::String>::New(const uint8_t * value, int length) {
return v8::String::NewFromOneByte(v8::Isolate::GetCurrent(), value,
v8::String::kNormalString, length);
}

Factory<v8::String>::return_t
Factory<v8::String>::New(const uint16_t * value, int length) {
return v8::String::NewFromTwoByte(v8::Isolate::GetCurrent(), value,
v8::String::kNormalString, length);
}

Factory<v8::String>::return_t
Factory<v8::String>::New(v8::String::ExternalStringResource * value) {
return v8::String::NewExternal(v8::Isolate::GetCurrent(), value);
}

Factory<v8::String>::return_t
Factory<v8::String>::New(v8::String::ExternalAsciiStringResource * value) {
return v8::String::NewExternal(v8::Isolate::GetCurrent(), value);
}

//=== String Object ============================================================

Factory<v8::StringObject>::return_t
Factory<v8::StringObject>::New(v8::Handle<v8::String> value) {
return v8::StringObject::New(value).As<v8::StringObject>();
}

//=== Unbound Script ===========================================================

Factory<v8::UnboundScript>::return_t
Factory<v8::UnboundScript>::New(v8::Local<v8::String> source) {
v8::ScriptCompiler::Source src(source);
return v8::ScriptCompiler::CompileUnbound(v8::Isolate::GetCurrent(), &src);
}

Factory<v8::UnboundScript>::return_t
Factory<v8::UnboundScript>::New( v8::Local<v8::String> source
, v8::ScriptOrigin const& origin) {
v8::ScriptCompiler::Source src(source, origin);
return v8::ScriptCompiler::CompileUnbound(v8::Isolate::GetCurrent(), &src);
}

} // end of namespace NanIntern

//=== Presistents and Handles ==================================================

template <typename T>
v8::Local<T>
NanNew(v8::Handle<T> h) {
return v8::Local<T>::New(v8::Isolate::GetCurrent(), h);
}

template <typename T>
v8::Local<T>
NanNew(v8::Persistent<T> const& p) {
return v8::Local<T>::New(v8::Isolate::GetCurrent(), p);
}


Loading