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

src: added Freeze and Seal method to Object class. #955

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
24 changes: 24 additions & 0 deletions doc/object.md
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,30 @@ void Napi::Object::DefineProperties (____ properties)
Defines properties on the object.
### Freeze()
```cpp
void Napi::Object::Freeze()
```

The `Napi::Object::Freeze()` method freezes an object. A frozen object can no
longer changed. Freezing an object prevents new properties from being added to
it, existing properties from being removed, prevents changing the
enumerability, configurability, or writability of existing properties and
prevents the valuee of existing properties from being changed. In addition,
freezing an object also prevents its prototype from being changed.

### Seal()

```cpp
void Napi::Object::Seal()
```

The `Napi::Object::Seal()` method seals an object, preventing new properties
from being added to it and marking all existing properties as non-configurable.
Values of present properties can still be changed as long as thery are
writable.

### operator\[\]()

```cpp
Expand Down
12 changes: 12 additions & 0 deletions napi-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -1378,6 +1378,18 @@ inline void Object::AddFinalizer(Finalizer finalizeCallback,
}
}

#if NAPI_VERSION >= 8
inline void Object::Freeze() {
napi_status status = napi_object_freeze(_env, _value);
NAPI_THROW_IF_FAILED_VOID(_env, status);
}

inline void Object::Seal() {
napi_status status = napi_object_seal(_env, _value);
NAPI_THROW_IF_FAILED_VOID(_env, status);
}
#endif // NAPI_VERSION >= 8

////////////////////////////////////////////////////////////////////////////////
// External class
////////////////////////////////////////////////////////////////////////////////
Expand Down
4 changes: 4 additions & 0 deletions napi.h
Original file line number Diff line number Diff line change
Expand Up @@ -752,6 +752,10 @@ namespace Napi {
inline void AddFinalizer(Finalizer finalizeCallback,
T* data,
Hint* finalizeHint);
#if NAPI_VERSION >= 8
void Freeze();
void Seal();
#endif // NAPI_VERSION >= 8
};

template <typename T>
Expand Down
6 changes: 6 additions & 0 deletions test/binding.cc
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ Object InitObjectReference(Env env);
Object InitReference(Env env);
Object InitVersionManagement(Env env);
Object InitThunkingManual(Env env);
#if (NAPI_VERSION > 7)
Object InitObjectFreezeSeal(Env env);
#endif

Object Init(Env env, Object exports) {
#if (NAPI_VERSION > 5)
Expand Down Expand Up @@ -139,6 +142,9 @@ Object Init(Env env, Object exports) {
exports.Set("reference", InitReference(env));
exports.Set("version_management", InitVersionManagement(env));
exports.Set("thunking_manual", InitThunkingManual(env));
#if (NAPI_VERSION > 7)
exports.Set("object_freeze_seal", InitObjectFreezeSeal(env));
#endif
return exports;
}

Expand Down
1 change: 1 addition & 0 deletions test/binding.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
'object/has_own_property.cc',
'object/has_property.cc',
'object/object.cc',
'object/object_freeze_seal.cc',
'object/set_property.cc',
'object/subscript_operator.cc',
'promise.cc',
Expand Down
4 changes: 4 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,10 @@ if (majorNodeVersion < 12) {
testModules.splice(testModules.indexOf('objectwrap_worker_thread'), 1);
}

if (napiVersion < 8) {
testModules.splice(testModules.indexOf('object/object_freeze_seal'), 1);
}

(async function() {
console.log(`Testing with N-API Version '${napiVersion}'.`);

Expand Down
24 changes: 24 additions & 0 deletions test/object/object_freeze_seal.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include "napi.h"

#if (NAPI_VERSION > 7)

using namespace Napi;

void Freeze(const CallbackInfo& info) {
Object obj = info[0].As<Object>();
obj.Freeze();
}

void Seal(const CallbackInfo& info) {
Object obj = info[0].As<Object>();
obj.Seal();
}

Object InitObjectFreezeSeal(Env env) {
Object exports = Object::New(env);
exports["freeze"] = Function::New(env, Freeze);
exports["seal"] = Function::New(env, Seal);
return exports;
}

#endif
38 changes: 38 additions & 0 deletions test/object/object_freeze_seal.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
'use strict';
const buildType = process.config.target_defaults.default_configuration;
const assert = require('assert');

test(require(`../build/${buildType}/binding.node`));
test(require(`../build/${buildType}/binding_noexcept.node`));

function test(binding) {
{
const obj = { x: 'a', y: 'b', z: 'c' };
binding.object_freeze_seal.freeze(obj);
assert.strictEqual(Object.isFrozen(obj), true);
assert.throws(() => {
obj.x = 10;
}, /Cannot assign to read only property 'x' of object '#<Object>/);
assert.throws(() => {
obj.w = 15;
}, /Cannot add property w, object is not extensible/);
assert.throws(() => {
delete obj.x;
}, /Cannot delete property 'x' of #<Object>/);
}

{
const obj = { x: 'a', y: 'b', z: 'c' };
binding.object_freeze_seal.seal(obj);
assert.strictEqual(Object.isSealed(obj), true);
assert.throws(() => {
obj.w = 'd';
}, /Cannot add property w, object is not extensible/);
assert.throws(() => {
delete obj.x;
}, /Cannot delete property 'x' of #<Object>/);
// Sealed objects allow updating existing properties,
// so this should not throw.
obj.x = 'd';
}
}