Skip to content

Commit

Permalink
Use DefineOwnProperty instead of ForceSet
Browse files Browse the repository at this point in the history
  • Loading branch information
kkoopa committed Nov 10, 2017
1 parent 02c9fb6 commit 95cbb97
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 10 deletions.
3 changes: 2 additions & 1 deletion README.md
Expand Up @@ -174,7 +174,8 @@ The `Nan::MaybeLocal` and `Nan::Maybe` types are monads that encapsulate `v8::Lo
- <a href="doc/maybe_types.md#api_nan_new_instance"><b><code>Nan::NewInstance()</code></b></a>
- <a href="doc/maybe_types.md#api_nan_get_function"><b><code>Nan::GetFunction()</code></b></a>
- <a href="doc/maybe_types.md#api_nan_set"><b><code>Nan::Set()</code></b></a>
- <a href="doc/maybe_types.md#api_nan_force_set"><b><code>Nan::ForceSet()</code></b></a>
- <a href="doc/maybe_types.md#api_nan_define_own_property"><b><code>Nan::DefineOwnProperty()</code></b></a>
- <a href="doc/maybe_types.md#api_nan_force_set"><del><b><code>Nan::ForceSet()</code></b></del></a>
- <a href="doc/maybe_types.md#api_nan_get"><b><code>Nan::Get()</code></b></a>
- <a href="doc/maybe_types.md#api_nan_get_property_attribute"><b><code>Nan::GetPropertyAttributes()</code></b></a>
- <a href="doc/maybe_types.md#api_nan_has"><b><code>Nan::Has()</code></b></a>
Expand Down
32 changes: 25 additions & 7 deletions doc/maybe_types.md
Expand Up @@ -15,7 +15,8 @@ The `Nan::MaybeLocal` and `Nan::Maybe` types are monads that encapsulate `v8::Lo
- <a href="#api_nan_new_instance"><b><code>Nan::NewInstance()</code></b></a>
- <a href="#api_nan_get_function"><b><code>Nan::GetFunction()</code></b></a>
- <a href="#api_nan_set"><b><code>Nan::Set()</code></b></a>
- <a href="#api_nan_force_set"><b><code>Nan::ForceSet()</code></b></a>
- <a href="#api_nan_define_own_property"><b><code>Nan::DefineOwnProperty()</code></b></a>
- <a href="#api_nan_force_set"><del><b><code>Nan::ForceSet()</code></b></del></a>
- <a href="#api_nan_get"><b><code>Nan::Get()</code></b></a>
- <a href="#api_nan_get_property_attribute"><b><code>Nan::GetPropertyAttributes()</code></b></a>
- <a href="#api_nan_has"><b><code>Nan::Has()</code></b></a>
Expand Down Expand Up @@ -209,18 +210,35 @@ Nan::Maybe<bool> Nan::Set(v8::Local<v8::Object> obj,
```


<a name="api_nan_define_own_property"></a>
### Nan::DefineOwnProperty()

A helper method for calling [`v8::Object#DefineOwnProperty()`](https://v8docs.nodesource.com/node-4.8/db/d85/classv8_1_1_object.html#a6f76b2ed605cb8f9185b92de0033a820) in a way compatible across supported versions of V8.

Signature:

```c++
Nan::Maybe<bool> Nan::DefineOwnProperty(v8::Local<v8::Object> obj,
v8::Local<v8::String> key,
v8::Local<v8::Value> value,
v8::PropertyAttribute attribs = v8::None);
```
<a name="api_nan_force_set"></a>
### Nan::ForceSet()
### <del>Nan::ForceSet()</del>
Deprecated, use <a href="#api_nan_define_own_property"><code>Nan::DefineOwnProperty()</code></a>.
A helper method for calling [`v8::Object#ForceSet()`](https://v8docs.nodesource.com/io.js-3.3/db/d85/classv8_1_1_object.html#a796b7b682896fb64bf1872747734e836) in a way compatible across supported versions of V8.
<del>A helper method for calling [`v8::Object#ForceSet()`](https://v8docs.nodesource.com/io.js-3.3/db/d85/classv8_1_1_object.html#a796b7b682896fb64bf1872747734e836) in a way compatible across supported versions of V8.</del>
Signature:
```c++
Nan::Maybe<bool> Nan::ForceSet(v8::Local<v8::Object> obj,
v8::Local<v8::Value> key,
v8::Local<v8::Value> value,
v8::PropertyAttribute attribs = v8::None);
NAN_DEPRECATED Nan::Maybe<bool> Nan::ForceSet(v8::Local<v8::Object> obj,
v8::Local<v8::Value> key,
v8::Local<v8::Value> value,
v8::PropertyAttribute attribs = v8::None);
```


Expand Down
2 changes: 2 additions & 0 deletions nan.h
Expand Up @@ -34,6 +34,8 @@
#define NODE_5_0_MODULE_VERSION 47
#define NODE_6_0_MODULE_VERSION 48
#define NODE_7_0_MODULE_VERSION 51
#define NODE_8_0_MODULE_VERSION 57
#define NODE_9_0_MODULE_VERSION 59

#ifdef _MSC_VER
# define NAN_HAS_CPLUSPLUS_11 (_MSC_VER >= 1800)
Expand Down
20 changes: 19 additions & 1 deletion nan_maybe_43_inl.h
Expand Up @@ -102,14 +102,32 @@ inline Maybe<bool> Set(
return obj->Set(isolate->GetCurrentContext(), index, value);
}

inline Maybe<bool> ForceSet(
inline Maybe<bool> DefineOwnProperty(
v8::Local<v8::Object> obj
, v8::Local<v8::String> key
, v8::Local<v8::Value> value
, v8::PropertyAttribute attribs = v8::None) {
v8::Isolate *isolate = v8::Isolate::GetCurrent();
v8::HandleScope scope(isolate);
return obj->DefineOwnProperty(isolate->GetCurrentContext(), key, value,
attribs);
}

NAN_DEPRECATED inline Maybe<bool> ForceSet(
v8::Local<v8::Object> obj
, v8::Local<v8::Value> key
, v8::Local<v8::Value> value
, v8::PropertyAttribute attribs = v8::None) {
v8::Isolate *isolate = v8::Isolate::GetCurrent();
v8::HandleScope scope(isolate);
#if NODE_MODULE_VERSION >= NODE_9_0_MODULE_VERSION
return key->IsName()
? obj->DefineOwnProperty(isolate->GetCurrentContext(),
key.As<v8::Name>(), value, attribs)
: Nothing<bool>();
#else
return obj->ForceSet(isolate->GetCurrentContext(), key, value, attribs);
#endif
}

inline MaybeLocal<v8::Value> Get(
Expand Down
15 changes: 14 additions & 1 deletion nan_maybe_pre_43_inl.h
Expand Up @@ -148,7 +148,20 @@ inline Maybe<bool> Set(
return Just<bool>(obj->Set(index, value));
}

inline Maybe<bool> ForceSet(
inline Maybe<bool> DefineOwnProperty(
v8::Handle<v8::Object> obj
, v8::Handle<v8::String> key
, v8::Handle<v8::Value> value
, v8::PropertyAttribute attribs = v8::None) {
v8::PropertyAttribute current = obj->GetPropertyAttributes(key);
return !(current & v8::DontDelete) || // configurable OR
!(current & v8::ReadOnly) && // writable AND
!((attribs ^ current) & ~v8::ReadOnly) // same excluding RO
? Just<bool>(obj->ForceSet(key, value, attribs))
: Nothing<bool>();
}

NAN_DEPRECATED inline Maybe<bool> ForceSet(
v8::Handle<v8::Object> obj
, v8::Handle<v8::Value> key
, v8::Handle<v8::Value> value
Expand Down

0 comments on commit 95cbb97

Please sign in to comment.