Implemented slice function

This commit is contained in:
Felipe Magno de Almeida 2014-09-17 18:10:26 -03:00
parent 47599c43f3
commit d0f80c915a
3 changed files with 56 additions and 7 deletions

View File

@ -15,6 +15,7 @@ struct eina_list_base
virtual std::size_t size() const = 0;
virtual eina_list_base* concat(eina_list_base const& rhs) const = 0;
virtual eina_list_base* slice(std::int64_t i, std::int64_t j) const = 0;
virtual int index_of(v8::Isolate* isolate, v8::Local<v8::Value> v) const = 0;
virtual int last_index_of(v8::Isolate* isolate, v8::Local<v8::Value> v) const = 0;
virtual v8::Local<v8::Value> get(v8::Isolate*, std::size_t) const = 0;
@ -105,12 +106,21 @@ namespace detail {
template <typename T>
eina_list_base* concat(T const& self, eina_list_base const& other)
{
std::cout << __func__ << std::endl;
T const& rhs = static_cast<T const&>(other);
typedef typename T::container_type container_type;
container_type list(self._container.begin(), self._container.end());
list.insert(list.end(), rhs._container.begin(), rhs._container.end());
return new T(list.release_native_handle());
std::cout << __func__ << std::endl;
T const& rhs = static_cast<T const&>(other);
typedef typename T::container_type container_type;
container_type list(self._container.begin(), self._container.end());
list.insert(list.end(), rhs._container.begin(), rhs._container.end());
return new T(list.release_native_handle());
}
template <typename T>
eina_list_base* slice(T const& self, std::int64_t i, std::int64_t j)
{
std::cout << __func__ << std::endl;
typedef typename T::container_type container_type;
container_type list(std::next(self._container.begin(), i), std::next(self._container.begin(), j));
return new T(list.release_native_handle());
}
}
@ -137,6 +147,10 @@ struct eina_list : eina_list_common<efl::eina::list
{
return detail::concat(*this, other);
}
eina_list_base* slice(std::int64_t i, std::int64_t j) const
{
return detail::slice(*this, i, j);
}
};
template <typename T>
@ -160,6 +174,12 @@ struct range_eina_list : eina_list_common<efl::eina::range_list<T> >
list.insert(list.end(), rhs._container.begin(), rhs._container.end());
return new eina_list<T>(list.release_native_handle());
}
eina_list_base* slice(std::int64_t i, std::int64_t j) const
{
efl::eina::list<T, clone_allocator_type>
list(std::next(this->_container.begin(), i), std::next(this->_container.begin(), j));
return new eina_list<T>(list.release_native_handle());
}
};
} }

View File

@ -55,7 +55,23 @@ v8::Local<v8::Object> concat(eina_list_base& lhs, v8::Isolate* isolate, v8::Loca
std::cout << "Some test failed" << std::endl;
std::abort();
}
v8::Local<v8::Object> slice(eina_list_base& self, v8::Isolate* isolate, v8::Local<v8::Value> iv
, v8::Local<v8::Value> jv)
{
if((iv->IsUint32() || iv->IsInt32()) && (jv->IsUint32() || jv->IsInt32()))
{
std::int64_t i = iv->IntegerValue(), j = jv->IntegerValue();
v8::Handle<v8::Value> a[] = {v8::External::New(isolate, self.slice(i, j))};
v8::Local<v8::Object> result = instance_template->GetFunction()->NewInstance(1, a);
return result;
}
else
std::cout << "parameters are not integral" << std::endl;
std::cout << "Some test failed" << std::endl;
std::abort();
}
void length(v8::Local<v8::String>, v8::PropertyCallbackInfo<v8::Value> const& info)
{
eina_list_base* self = static_cast<eina_list_base*>(info.This()->GetAlignedPointerFromInternalField(0));
@ -158,6 +174,7 @@ R call_impl(v8::Isolate* isolate
}
} print_;
std::cout << "self " << self << " pointer " << (void*)f << std::endl;
assert(self != 0);
return (*f)(*self, isolate, js::get_element<N, Sig>(isolate, args)...);
}
@ -260,6 +277,8 @@ EAPI void eina_list_register(v8::Handle<v8::ObjectTemplate> global, v8::Isolate*
using namespace std::placeholders;
efl::js::register_<efl::js::eina_list_base>
(isolate, "concat", &efl::js::concat, prototype);
efl::js::register_<efl::js::eina_list_base>
(isolate, "slice", &efl::js::slice, prototype);
efl::js::register_<efl::js::eina_list_base>
(isolate, "toString", std::bind(&efl::js::eina_list_base::to_string, _1, _2), prototype);
efl::js::register_<efl::js::eina_list_base>

View File

@ -49,6 +49,16 @@ static const char script[] =
"assert (c.lastIndexOf(c[3]) == 3);\n"
"assert (c.lastIndexOf(c[4]) == 4);\n"
"assert (c.lastIndexOf(c[5]) == 5);\n"
"var s1 = l1.slice(1, 3);\n"
"print (\"s1 \", s1.toString());\n"
"assert (s1.length == 2);\n"
"assert (s1[0] == l1[1]);\n"
"assert (s1[1] == l1[2]);\n"
"var s2 = c.slice(1, 3);\n"
"print (\"s2 \", s2.toString());\n"
"assert (s2.length == 2);\n"
"assert (s2[0] == l1[1]);\n"
"assert (s2[1] == l1[2]);\n"
;
const char* ToCString(const v8::String::Utf8Value& value) {