Permalink
Browse files

util, feat: support deepFreeze.

  • Loading branch information...
xicilion committed Aug 15, 2018
1 parent 62ae2a1 commit a1ec0af8164ca0fbba85490fc26aca9a3fa7fbf3
Showing with 72 additions and 3 deletions.
  1. +17 −0 fibjs/include/ifs/util.h
  2. +21 −0 fibjs/src/util/util.cpp
  3. +6 −0 idl/zh-cn/util.idl
  4. +28 −3 test/util_test.js
View
@@ -59,6 +59,7 @@ class util_base : public object_base {
static result_t keys(v8::Local<v8::Value> v, v8::Local<v8::Array>& retVal);
static result_t values(v8::Local<v8::Value> v, v8::Local<v8::Array>& retVal);
static result_t clone(v8::Local<v8::Value> v, v8::Local<v8::Value>& retVal);
static result_t deepFreeze(v8::Local<v8::Value> v);
static result_t extend(v8::Local<v8::Value> v, OptArgs objs, v8::Local<v8::Value>& retVal);
static result_t _extend(v8::Local<v8::Value> v, OptArgs objs, v8::Local<v8::Value>& retVal);
static result_t pick(v8::Local<v8::Value> v, OptArgs objs, v8::Local<v8::Object>& retVal);
@@ -125,6 +126,7 @@ class util_base : public object_base {
static void s_keys(const v8::FunctionCallbackInfo<v8::Value>& args);
static void s_values(const v8::FunctionCallbackInfo<v8::Value>& args);
static void s_clone(const v8::FunctionCallbackInfo<v8::Value>& args);
static void s_deepFreeze(const v8::FunctionCallbackInfo<v8::Value>& args);
static void s_extend(const v8::FunctionCallbackInfo<v8::Value>& args);
static void s__extend(const v8::FunctionCallbackInfo<v8::Value>& args);
static void s_pick(const v8::FunctionCallbackInfo<v8::Value>& args);
@@ -187,6 +189,7 @@ inline ClassInfo& util_base::class_info()
{ "keys", s_keys, true },
{ "values", s_values, true },
{ "clone", s_clone, true },
{ "deepFreeze", s_deepFreeze, true },
{ "extend", s_extend, true },
{ "_extend", s__extend, true },
{ "pick", s_pick, true },
@@ -758,6 +761,20 @@ inline void util_base::s_clone(const v8::FunctionCallbackInfo<v8::Value>& args)
METHOD_RETURN();
}
inline void util_base::s_deepFreeze(const v8::FunctionCallbackInfo<v8::Value>& args)
{
METHOD_NAME("util.deepFreeze");
METHOD_ENTER();
METHOD_OVER(1, 1);
ARG(v8::Local<v8::Value>, 0);
hr = deepFreeze(v0);
METHOD_VOID();
}
inline void util_base::s_extend(const v8::FunctionCallbackInfo<v8::Value>& args)
{
v8::Local<v8::Value> vr;
View
@@ -10,6 +10,7 @@
#include "ifs/encoding.h"
#include "QuickArray.h"
#include <map>
#include "v8_api.h"
namespace fibjs {
@@ -124,6 +125,26 @@ result_t util_base::clone(v8::Local<v8::Value> v, v8::Local<v8::Value>& retVal)
return 0;
}
result_t util_base::deepFreeze(v8::Local<v8::Value> v)
{
if (v.IsEmpty() || !v->IsObject())
return 0;
v8::Local<v8::Object> obj = v8::Local<v8::Object>::Cast(v);
if (!isFrozen(obj)) {
obj->SetIntegrityLevel(obj->CreationContext(), v8::IntegrityLevel::kFrozen).ToChecked();
v8::Local<v8::Array> names = obj->GetPropertyNames(obj->CreationContext())
.ToLocalChecked();
TryCatch try_catch;
for (int32_t i = 0; i < names->Length(); i++)
deepFreeze(obj->Get(names->Get(i)));
}
return 0;
}
result_t util_base::extend(v8::Local<v8::Value> v, OptArgs objs,
v8::Local<v8::Value>& retVal)
{
View
@@ -243,6 +243,12 @@ module util
*/
static Value clone(Value v);
/*! @brief 深度冻结一个对象,被冻结后的对象及其包含的对象都将不允许修改
@param v 指定要冻结的对象
*/
static deepFreeze(Value v);
/*! @brief 将一个或者多个对象的键值扩展到指定对象
@param v 指定要扩展的对象
View
@@ -486,6 +486,30 @@ describe('util', () => {
util.clone(new Map());
});
it('deepFreeze', () => {
var o = {
a: {
b: 100
},
b: [200]
};
util.deepFreeze(o);
o.c = 200;
o.a.c = 300;
assert.throws(() => {
o.b.push(400);
})
assert.deepEqual(o, {
a: {
b: 100
},
b: [200]
});
});
it('extend', () => {
var result;
assert.equal(util.extend({}, {
@@ -1254,9 +1278,10 @@ describe('util', () => {
it('modules', () => {
const modules = util.buildInfo().modules;
// built-in modules
;[
;
[
"zmq",
"zlib",
"zip",
@@ -1562,4 +1587,4 @@ describe('util', () => {
});
});
require.main === module && test.run(console.DEBUG);
require.main === module && test.run(console.DEBUG);

0 comments on commit a1ec0af

Please sign in to comment.