-
Notifications
You must be signed in to change notification settings - Fork 461
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
src: added Freeze and Seal method to Object class.
PR-URL: #955 Reviewed-By: Gabriel Schulhof <gabrielschulhof@gmail.com> Reviewed-By: Michael Dawson <midawson@redhat.com>
- Loading branch information
Showing
8 changed files
with
113 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
} | ||
} |