Skip to content

Commit

Permalink
List, refactor: switch to ...args syntax.
Browse files Browse the repository at this point in the history
  • Loading branch information
xicilion committed Aug 3, 2017
1 parent 9148441 commit 8751900
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 40 deletions.
4 changes: 2 additions & 2 deletions fibjs/include/DBResult.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,13 @@ class DBResult : public DBResult_base {
virtual result_t get_length(int32_t& retVal);
virtual result_t resize(int32_t sz);
virtual result_t push(Variant v, int32_t& retVal);
virtual result_t push(const v8::FunctionCallbackInfo<v8::Value>& args, int32_t& retVal);
virtual result_t push(v8::Local<v8::Array> els, int32_t& retVal);
virtual result_t pushArray(v8::Local<v8::Array> data);
virtual result_t indexOf(Variant searchElement, int32_t fromIndex, int32_t& retVal);
virtual result_t lastIndexOf(Variant searchElement, int32_t fromIndex, int32_t& retVal);
virtual result_t pop(Variant& retVal);
virtual result_t slice(int32_t start, int32_t end, obj_ptr<List_base>& retVal);
virtual result_t concat(const v8::FunctionCallbackInfo<v8::Value>& args, obj_ptr<List_base>& retVal);
virtual result_t concat(v8::Local<v8::Array> lists, obj_ptr<List_base>& retVal);
virtual result_t every(v8::Local<v8::Function> func, v8::Local<v8::Value> thisArg, bool& retVal);
virtual result_t some(v8::Local<v8::Function> func, v8::Local<v8::Value> thisArg, bool& retVal);
virtual result_t filter(v8::Local<v8::Function> func, v8::Local<v8::Value> thisArg, obj_ptr<List_base>& retVal);
Expand Down
9 changes: 7 additions & 2 deletions fibjs/include/List.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ class List : public List_base {
virtual result_t get_length(int32_t& retVal);
virtual result_t resize(int32_t sz);
virtual result_t push(Variant v, int32_t& retVal);
virtual result_t push(const v8::FunctionCallbackInfo<v8::Value>& args, int32_t& retVal);
virtual result_t push(v8::Local<v8::Array> els, int32_t& retVal);
virtual result_t pushArray(v8::Local<v8::Array> data);
virtual result_t indexOf(Variant searchElement, int32_t fromIndex, int32_t& retVal);
virtual result_t lastIndexOf(Variant searchElement, int32_t fromIndex, int32_t& retVal);
virtual result_t pop(Variant& retVal);
virtual result_t slice(int32_t start, int32_t end, obj_ptr<List_base>& retVal);
virtual result_t concat(const v8::FunctionCallbackInfo<v8::Value>& args, obj_ptr<List_base>& retVal);
virtual result_t concat(v8::Local<v8::Array> lists, obj_ptr<List_base>& retVal);
virtual result_t every(v8::Local<v8::Function> func, v8::Local<v8::Value> thisArg, bool& retVal);
virtual result_t some(v8::Local<v8::Function> func, v8::Local<v8::Value> thisArg, bool& retVal);
virtual result_t filter(v8::Local<v8::Function> func, v8::Local<v8::Value> thisArg, obj_ptr<List_base>& retVal);
Expand Down Expand Up @@ -69,6 +69,11 @@ class List : public List_base {
push(newVal, r);
}

void append(v8::Local<v8::Value> newVal)
{
append(Variant(newVal));
}

private:
v8::Local<v8::Value> _call(v8::Local<v8::Function> func,
v8::Local<v8::Value> thisArg, int32_t i);
Expand Down
12 changes: 8 additions & 4 deletions fibjs/include/ifs/List.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ class List_base : public object_base {
virtual result_t get_length(int32_t& retVal) = 0;
virtual result_t resize(int32_t sz) = 0;
virtual result_t push(Variant v, int32_t& retVal) = 0;
virtual result_t push(const v8::FunctionCallbackInfo<v8::Value>& args, int32_t& retVal) = 0;
virtual result_t push(v8::Local<v8::Array> els, int32_t& retVal) = 0;
virtual result_t indexOf(Variant searchElement, int32_t fromIndex, int32_t& retVal) = 0;
virtual result_t lastIndexOf(Variant searchElement, int32_t fromIndex, int32_t& retVal) = 0;
virtual result_t pushArray(v8::Local<v8::Array> data) = 0;
virtual result_t pop(Variant& retVal) = 0;
virtual result_t slice(int32_t start, int32_t end, obj_ptr<List_base>& retVal) = 0;
virtual result_t concat(const v8::FunctionCallbackInfo<v8::Value>& args, obj_ptr<List_base>& retVal) = 0;
virtual result_t concat(v8::Local<v8::Array> lists, obj_ptr<List_base>& retVal) = 0;
virtual result_t every(v8::Local<v8::Function> func, v8::Local<v8::Value> thisArg, bool& retVal) = 0;
virtual result_t some(v8::Local<v8::Function> func, v8::Local<v8::Value> thisArg, bool& retVal) = 0;
virtual result_t filter(v8::Local<v8::Function> func, v8::Local<v8::Value> thisArg, obj_ptr<List_base>& retVal) = 0;
Expand Down Expand Up @@ -218,7 +218,9 @@ inline void List_base::s_push(const v8::FunctionCallbackInfo<v8::Value>& args)

METHOD_OVER(-1, 0);

hr = pInst->push(args, vr);
ARG_LIST(0);

hr = pInst->push(v0, vr);

METHOD_RETURN();
}
Expand Down Expand Up @@ -311,7 +313,9 @@ inline void List_base::s_concat(const v8::FunctionCallbackInfo<v8::Value>& args)

METHOD_OVER(-1, 0);

hr = pInst->concat(args, vr);
ARG_LIST(0);

hr = pInst->concat(v0, vr);

METHOD_RETURN();
}
Expand Down
45 changes: 22 additions & 23 deletions fibjs/src/collection/List.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,16 +138,16 @@ result_t List::lastIndexOf(Variant searchElement, int32_t fromIndex, int32_t& re
return 0;
}

result_t List::push(const v8::FunctionCallbackInfo<v8::Value>& args, int32_t& retVal)
result_t List::push(v8::Local<v8::Array> els, int32_t& retVal)
{
if (m_freeze)
return CHECK_ERROR(CALL_E_INVALID_CALL);

int32_t len = args.Length();
int32_t i, r;
int32_t len = els->Length();
int32_t i;

for (i = 0; i < len; i++)
push(args[i], r);
append(els->Get(i));

retVal = (int32_t)m_array.size();
return 0;
Expand All @@ -159,10 +159,10 @@ result_t List::pushArray(v8::Local<v8::Array> data)
return CHECK_ERROR(CALL_E_INVALID_CALL);

int32_t len = data->Length();
int32_t i, r;
int32_t i;

for (i = 0; i < len; i++)
push(data->Get(i), r);
append(data->Get(i));
return 0;
}

Expand All @@ -180,38 +180,37 @@ result_t List::pop(Variant& retVal)
return 0;
}

result_t List::slice(int32_t start, int32_t end,
obj_ptr<List_base>& retVal)
result_t List::slice(int32_t start, int32_t end, obj_ptr<List_base>& retVal)
{
int32_t r;
if (end < 0)
end = (int32_t)m_array.size();

retVal = new List();
obj_ptr<List> l = new List();
while (start < end)
retVal->push(m_array[start++], r);
l->append(m_array[start++]);

retVal = l;

return 0;
}

result_t List::concat(const v8::FunctionCallbackInfo<v8::Value>& args,
obj_ptr<List_base>& retVal)
result_t List::concat(v8::Local<v8::Array> lists, obj_ptr<List_base>& retVal)
{
if (m_freeze)
return CHECK_ERROR(CALL_E_INVALID_CALL);

obj_ptr<List> a;
int32_t i, len, r;
int32_t i, len;

a = new List();

len = (int32_t)m_array.size();
for (i = 0; i < len; i++)
a->push(m_array[i], r);
a->append(m_array[i]);

len = args.Length();
len = lists->Length();
for (i = 0; i < len; i++) {
v8::Local<v8::Value> v = args[i];
v8::Local<v8::Value> v = lists->Get(i);
obj_ptr<List_base> a1 = List_base::getInstance(v);

if (a1) {
Expand All @@ -222,15 +221,15 @@ result_t List::concat(const v8::FunctionCallbackInfo<v8::Value>& args,
Variant v1;

a1->_indexed_getter(i1, v1);
a->push(v1, r);
a->append(v1);
}
} else if (v->IsArray()) {
int32_t i1, len1;
v8::Local<v8::Array> arr = v8::Local<v8::Array>::Cast(v);

len1 = arr->Length();
for (i1 = 0; i1 < len1; i1++)
a->push(arr->Get(i1), r);
a->append(arr->Get(i1));
} else
return CHECK_ERROR(CALL_E_INVALIDARG);
}
Expand Down Expand Up @@ -296,7 +295,7 @@ result_t List::filter(v8::Local<v8::Function> func,
v8::Local<v8::Value> thisArg, obj_ptr<List_base>& retVal)
{
obj_ptr<List> a;
int32_t i, len, tmp;
int32_t i, len;

a = new List();

Expand All @@ -308,7 +307,7 @@ result_t List::filter(v8::Local<v8::Function> func,
return CALL_E_JAVASCRIPT;

if (r->BooleanValue())
a->push(m_array[i], tmp);
a->append(m_array[i]);
}

retVal = a;
Expand Down Expand Up @@ -336,7 +335,7 @@ result_t List::map(v8::Local<v8::Function> func,
v8::Local<v8::Value> thisArg, obj_ptr<List_base>& retVal)
{
obj_ptr<List> a;
int32_t i, len, tmp;
int32_t i, len;

a = new List();

Expand All @@ -347,7 +346,7 @@ result_t List::map(v8::Local<v8::Function> func,
if (r.IsEmpty())
return CALL_E_JAVASCRIPT;

a->push(r, tmp);
a->append(r);
}

retVal = a;
Expand Down
8 changes: 4 additions & 4 deletions fibjs/src/db/sql/DBResult.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,12 @@ result_t DBResult::push(Variant v, int32_t& retVal)
return m_array->push(v, retVal);
}

result_t DBResult::push(const v8::FunctionCallbackInfo<v8::Value>& args, int32_t& retVal)
result_t DBResult::push(v8::Local<v8::Array> els, int32_t& retVal)
{
if (!m_size)
return CHECK_ERROR(CALL_E_INVALID_CALL);

return m_array->push(args, retVal);
return m_array->push(els, retVal);
}

result_t DBResult::pushArray(v8::Local<v8::Array> data)
Expand All @@ -104,12 +104,12 @@ result_t DBResult::slice(int32_t start, int32_t end, obj_ptr<List_base>& retVal)
return m_array->slice(start, end, retVal);
}

result_t DBResult::concat(const v8::FunctionCallbackInfo<v8::Value>& args, obj_ptr<List_base>& retVal)
result_t DBResult::concat(v8::Local<v8::Array> lists, obj_ptr<List_base>& retVal)
{
if (!m_size)
return CHECK_ERROR(CALL_E_INVALID_CALL);

return m_array->concat(args, retVal);
return m_array->concat(lists, retVal);
}

result_t DBResult::every(v8::Local<v8::Function> func,
Expand Down
10 changes: 5 additions & 5 deletions idl/zh-cn/List.idl
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,11 @@ interface List : object
*/
Integer lastIndexOf(Variant searchElement, Integer fromIndex = 0);

/*! @brief 在数组结尾添加一个元素
@param ... 指定添加的多个元素
/*! @brief 在数组结尾添加多个元素
@param els 指定添加的多个元素
@return 添加元素之后数组的长度
*/
Integer push(...);
Integer push(...els);

/*! @brief 在数组结尾添加一组元素
@param data 添加的一组元素
Expand All @@ -84,10 +84,10 @@ interface List : object
List slice(Integer start = 0, Integer end = -1);

/*! @brief 将当前数组与给定的一个或者多个数组合并
@param ... 给定的一个或多个数组
@param lists 给定的一个或多个数组
@return 返回合并的数组
*/
List concat(...);
List concat(...lists);

/*! @brief 检测全部数据是否通过测试函数
@param func 测试函数
Expand Down

0 comments on commit 8751900

Please sign in to comment.