Skip to content

Commit

Permalink
src: added Freeze and Seal method to Object class.
Browse files Browse the repository at this point in the history
PR-URL: #955
Reviewed-By: Gabriel Schulhof <[email protected]>
Reviewed-By: Michael Dawson <[email protected]>
  • Loading branch information
NickNaso authored and mhdawson committed Mar 31, 2021
1 parent bc5147c commit 77350ee
Show file tree
Hide file tree
Showing 8 changed files with 113 additions and 0 deletions.
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 @@ -1379,6 +1379,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 @@ -776,6 +776,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 @@ -67,6 +67,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 @@ -141,6 +144,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 @@ -39,6 +39,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 Node-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';
}
}

0 comments on commit 77350ee

Please sign in to comment.