Skip to content

Commit

Permalink
[Maybe types] Add MakeMaybe(...)
Browse files Browse the repository at this point in the history
Wraps Locals in MaybeLocals. When called with a MaybeLocal it is the identity function and returns its argument. This is useful in writing generic template code on top of NAN.
  • Loading branch information
agnat authored and kkoopa committed Oct 7, 2015
1 parent 66ac6e6 commit 48d7b53
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ The `Nan::MaybeLocal` and `Nan::Maybe` types are monads that encapsulate `v8::Lo
- <a href="doc/maybe_types.md#api_nan_get_start_column"><b><code>Nan::GetStartColumn()</code></b></a>
- <a href="doc/maybe_types.md#api_nan_get_end_column"><b><code>Nan::GetEndColumn()</code></b></a>
- <a href="doc/maybe_types.md#api_nan_clone_element_at"><b><code>Nan::CloneElementAt()</code></b></a>
- <a href="doc/maybe_types.md#api_nan_make_maybe"><b><code>Nan::MakeMaybe()</code></b></a>

### Script

Expand Down
20 changes: 20 additions & 0 deletions doc/maybe_types.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ The `Nan::MaybeLocal` and `Nan::Maybe` types are monads that encapsulate `v8::Lo
- <a href="#api_nan_get_start_column"><b><code>Nan::GetStartColumn()</code></b></a>
- <a href="#api_nan_get_end_column"><b><code>Nan::GetEndColumn()</code></b></a>
- <a href="#api_nan_clone_element_at"><b><code>Nan::CloneElementAt()</code></b></a>
- <a href="#api_nan_make_maybe"><b><code>Nan::MakeMaybe()</code></b></a>

<a name="api_nan_maybe_local"></a>
### Nan::MaybeLocal
Expand Down Expand Up @@ -478,3 +479,22 @@ Signature:
```c++
Nan::MaybeLocal<v8::Object> Nan::CloneElementAt(v8::Local<v8::Array> array, uint32_t index);
```
<a name="api_nan_make_maybe"></a>
### Nan::MakeMaybe()
Wraps a `v8::Local<>` in a `Nan::MaybeLocal<>`. When called with a `Nan::MaybeLocal<>` it just returns its argument. This is useful in generic template code that builds on NAN.
Synopsis:
```c++
MaybeLocal<v8::Number> someNumber = MakeMaybe(New<v8::Number>(3.141592654));
MaybeLocal<v8::String> someString = MakeMaybe(New<v8::String>("probably"));
```

Signature:

```c++
template <typename T, template <typename> class MaybeMaybe>
Nan::MaybeLocal<T> Nan::MakeMaybe(MaybeMaybe<T> v);
```
26 changes: 26 additions & 0 deletions nan.h
Original file line number Diff line number Diff line change
Expand Up @@ -2186,6 +2186,32 @@ struct Tap {

#undef TYPE_CHECK

//=== Generic Maybefication ===================================================

namespace imp {

template <typename T> struct Maybefier;

template <typename T> struct Maybefier<v8::Local<T> > {
static MaybeLocal<T> convert(v8::Local<T> v) {
return MaybeLocal<T>(v);
}
};

template <typename T> struct Maybefier<MaybeLocal<T> > {
static MaybeLocal<T> convert(MaybeLocal<T> v) {
return v;
}
};

} // end of namespace imp

template <typename T, template <typename> class MaybeMaybe>
MaybeLocal<T>
MakeMaybe(MaybeMaybe<T> v) {
return imp::Maybefier<MaybeMaybe<T> >::convert(v);
}

} // end of namespace Nan

#endif // NAN_H_
9 changes: 9 additions & 0 deletions test/cpp/nannew.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,13 @@ NAN_METHOD(newExternal) {
info.GetReturnValue().Set(New<External>(&ttt));
}

NAN_METHOD(invokeMakeMaybe) {
Nan::MaybeLocal<v8::Number> number = MakeMaybe(New<v8::Number>(3.141592654));
Nan::MaybeLocal<v8::String> string = MakeMaybe(New<v8::String>("probably"));
(void)string;
info.GetReturnValue().Set(number.ToLocalChecked());
}

NAN_MODULE_INIT(Init) {
NAN_EXPORT(target, testArray);
NAN_EXPORT(target, testBoolean);
Expand Down Expand Up @@ -478,6 +485,8 @@ NAN_MODULE_INIT(Init) {
NAN_EXPORT(target, newStringFromStdString);

NAN_EXPORT(target, newExternal);

NAN_EXPORT(target, invokeMakeMaybe);
}

} // end of anonymous namespace
Expand Down
5 changes: 5 additions & 0 deletions test/js/nannew-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,8 @@ test('strings', function (t) {
t.end();
});

test('test MakeMaybe(...)', function (t) {
t.plan(1);
t.ok(bindings.invokeMakeMaybe() - Math.PI < 10e-8);
t.end();
});

0 comments on commit 48d7b53

Please sign in to comment.