eina-js: Fixed some tests for new compatibility

This commit is contained in:
Felipe Magno de Almeida 2015-01-13 18:49:57 -02:00
parent 5ce444d4ce
commit d59ec44336
8 changed files with 130 additions and 105 deletions

View File

@ -135,7 +135,10 @@ struct accessor
* @warning It is important to take care when using it, since the
* handle will be automatically release upon object destruction.
*/
Eina_Accessor* native_handle() const;
Eina_Accessor* native_handle() const
{
return _impl;
}
/**
* @brief Swap content between both objects.

View File

@ -8,12 +8,15 @@
#include <eina_js_accessor.hh>
#include <eina_js_compatibility.hh>
#include <iostream>
namespace efl { namespace eina { namespace js {
EAPI void register_destroy_accessor(v8::Isolate *isolate,
v8::Handle<v8::Object> global,
v8::Handle<v8::String> name)
{
std::cerr << "register_destroy_accessor" << std::endl;
typedef void (*deleter_t)(void*);
auto f = [](compatibility_callback_info_type info) -> compatibility_return_type

View File

@ -7,84 +7,65 @@
#include <eina_js_value.hh>
#include <eina_js_compatibility.hh>
#include <eina_js_get_value.hh>
#include <eina_js_get_value_from_c.hh>
#include <iostream>
namespace efl { namespace eina { namespace js {
template <typename T>
js::compatibility_return_type accessor_get(js::compatibility_callback_info_type info)
{
std::cout << __func__ << ":" << __LINE__ << std::endl;
v8::Isolate* isolate = info.GetIsolate();
if (info.Length() != 1 || !info[0]->IsNumber())
return js::compatibility_return();
auto idx = js::get_value_from_javascript
(info[0], isolate, js::value_tag<std::size_t>());
std::cout << __func__ << ":" << __LINE__ << std::endl;
void *ptr = compatibility_get_pointer_internal_field(info.Holder(), 0);
std::cout << __func__ << ":" << __LINE__ << std::endl;
return compatibility_return
(js::get_value_from_c((*static_cast<T*>(ptr))[idx], isolate), info);
};
/* Creates a copy from \p a accessor and exports it to be manipulated by the JS
code */
template<class T>
v8::Local<v8::Object> export_accessor(::efl::eina::accessor<T> &a,
v8::Isolate *isolate)
;
// {
// using v8::Local;
// using v8::Value;
// using v8::String;
// using v8::FunctionCallbackInfo;
// using v8::FunctionTemplate;
// using v8::ObjectTemplate;
v8::Local<v8::Object> export_accessor( ::efl::eina::accessor<T> &a, v8::Isolate *isolate)
{
typedef ::efl::eina::accessor<T> accessor_type;
static auto obj_tpl = [&]() -> compatibility_persistent<v8::ObjectTemplate>
{
auto obj_tpl = compatibility_new<v8::ObjectTemplate>(isolate);
obj_tpl->SetInternalFieldCount(1);
// typedef ::efl::eina::accessor<T> value_type;
// typedef value_type *ptr_type;
// typedef void (*deleter_t)(void*);
obj_tpl->Set(js::compatibility_new<v8::String>(isolate, "get")
, js::compatibility_new<v8::FunctionTemplate>(isolate, &accessor_get<accessor_type>));
return {isolate, obj_tpl};
}();
// auto obj_tpl = compatibility_new<ObjectTemplate>(isolate);
// obj_tpl->SetInternalFieldCount(2);
// auto ret = obj_tpl->NewInstance();
// auto get = [](const FunctionCallbackInfo<Value> &info) {
// if (info.Length() != 1 || !info[0]->IsNumber())
// return;
// auto idx = [&info]() -> std::size_t {
// auto idx = info[0];
// if (idx->IsInt32())
// return idx->ToInt32()->Value();
// else if (idx->IsUint32())
// return idx->ToUint32()->Value();
// else
// return idx->ToNumber()->Value();
// }();
// void *ptr = info.Holder()->GetAlignedPointerFromInternalField(0);
// auto &value = *static_cast<ptr_type>(ptr);
// info.GetReturnValue().Set(value_cast<Local<Value>>(value[idx],
// info.GetIsolate()));
// };
// ret->Set(String::NewFromUtf8(isolate, "get"),
// FunctionTemplate::New(isolate, get)->GetFunction());
// {
// deleter_t deleter = [](void *a) {
// delete static_cast<ptr_type>(a);
// };
// std::unique_ptr<value_type> ptr(new value_type(a));
// ret->SetAlignedPointerInInternalField(0, ptr.get());
// ret->SetAlignedPointerInInternalField(1,
// reinterpret_cast<void*>(deleter));
// ptr.release();
// }
// return ret;
// }
auto instance = obj_tpl.handle()->NewInstance();
compatibility_set_pointer_internal_field(instance, 0, &a);
return instance;
}
/* Extracts and returns a copy from the internal accessor object from the JS
object */
template<class T>
::efl::eina::accessor<T> import_accessor(v8::Handle<v8::Object> o)
;
// {
// typedef ::efl::eina::accessor<T> value_type;
// typedef value_type *ptr_type;
{
typedef ::efl::eina::accessor<T> accessor_type;
// ptr_type acc = reinterpret_cast<ptr_type>
// (o->GetAlignedPointerFromInternalField(0));
// return value_type(*acc);
// }
void* ptr = compatibility_get_pointer_internal_field(o, 0);
return *static_cast<accessor_type*>(ptr);
}
/* Registers the function to destroy the accessor objects to the JS code */
void register_destroy_accessor(v8::Isolate *isolate,

View File

@ -8,7 +8,7 @@
namespace efl { namespace eina {namespace js {
EAPI
void convert_error_to_javascript_exception(v8::Isolate *isolate)
js::compatibility_return_type convert_error_to_javascript_exception(v8::Isolate *isolate)
{
using v8::Local;
using v8::Object;
@ -23,7 +23,7 @@ void convert_error_to_javascript_exception(v8::Isolate *isolate)
compatibility_new<v8::String>(isolate, "Eina_Error"));
je->Set(compatibility_new<v8::String>(isolate, "value"),
compatibility_new<v8::String>(isolate, eina_error_msg_get(err)));
compatibility_throw(isolate, je);
return compatibility_throw(isolate, je);
}
} } } // namespace efl { namespace js {

View File

@ -4,7 +4,7 @@
#include <Eina.hh>
#include EINA_STRINGIZE(V8_INCLUDE_HEADER)
namespace efl { namespace js {
namespace efl { namespace eina { namespace js {
/**
* Converts the error value set through Eina's error tool to a JavaScript
@ -21,6 +21,6 @@ namespace efl { namespace js {
*/
void convert_error_to_javascript_exception(v8::Isolate *isolate);
} } // namespace efl::js
} } } // namespace efl::eina::js
#endif /* EINA_JS_ERROR_HH */

View File

@ -25,19 +25,6 @@ efl::eina::js::compatibility_return_type print(efl::eina::js::compatibility_call
return efl::eina::js::compatibility_return();
}
efl::eina::js::compatibility_return_type clear_eina_error(efl::eina::js::compatibility_callback_info_type args)
{
eina_error_set(0);
efl::js::convert_error_to_javascript_exception(args.GetIsolate());
return efl::eina::js::compatibility_return();
}
efl::eina::js::compatibility_return_type set_eina_error(efl::eina::js::compatibility_callback_info_type args)
{
eina_error_set(eina_error_msg_static_register("foobar"));
efl::js::convert_error_to_javascript_exception(args.GetIsolate());
return efl::eina::js::compatibility_return();
}
static const char script[] =
"function assert(test, message) { if (test !== true) throw message; };"

View File

@ -13,7 +13,9 @@
#include <Eina.h>
#include <Eo.hh>
#include <eina_js_accessor.hh>
#include <eina_js_list.hh>
#include <eina_js_error.hh>
const char* ToCString(const v8::String::Utf8Value& value) {
return *value ? *value : "<string conversion failed>";
@ -85,14 +87,31 @@ efl::eina::js::compatibility_return_type Print(efl::eina::js::compatibility_call
return efl::eina::js::compatibility_return();
}
efl::eina::js::compatibility_return_type clear_eina_error(efl::eina::js::compatibility_callback_info_type args)
{
eina_error_set(0);
return efl::eina::js::convert_error_to_javascript_exception(args.GetIsolate());
}
efl::eina::js::compatibility_return_type set_eina_error(efl::eina::js::compatibility_callback_info_type args)
{
eina_error_set(eina_error_msg_static_register("foobar"));
return efl::eina::js::convert_error_to_javascript_exception(args.GetIsolate());
}
EAPI void eina_container_register(v8::Handle<v8::Object> global, v8::Isolate* isolate);
EAPI v8::Handle<v8::FunctionTemplate> get_list_instance_template();
efl::eina::ptr_list<int> list;
efl::eina::js::range_eina_list<int> raw_list;
efl::eina::array<int> array;
void test_setup(v8::Handle<v8::Object> exports)
{
v8::Isolate* isolate = v8::Isolate::GetCurrent();
// container globals
std::cerr << __LINE__ << std::endl;
list.push_back(new int(5));
list.push_back(new int(10));
@ -111,10 +130,35 @@ void test_setup(v8::Handle<v8::Object> exports)
, get_list_instance_template()->GetFunction()->NewInstance(1, a));
std::cerr << __LINE__ << std::endl;
// error globals
exports->Set(efl::eina::js::compatibility_new<v8::String>(isolate, "clear_eina_error"),
efl::eina::js::compatibility_new<v8::FunctionTemplate>(isolate, clear_eina_error)
->GetFunction());
exports->Set(efl::eina::js::compatibility_new<v8::String>(isolate, "set_eina_error"),
efl::eina::js::compatibility_new<v8::FunctionTemplate>(isolate, set_eina_error)
->GetFunction());
std::cerr << __LINE__ << std::endl;
// accessor
Eina_Array* array = eina_array_new(2);
eina_array_push(array, new int(42));
eina_array_push(array, new int(24));
static efl::eina::accessor<int> acc(eina_array_accessor_new(array));
std::cerr << __LINE__ << std::endl;
// efl::eina::js::register_destroy_accessor
// (isolate, exports
// , efl::eina::js::compatibility_new<v8::String>(isolate, "destroy_accessor"));
std::cerr << __LINE__ << std::endl;
v8::Local<v8::Object> wrapped_acc = efl::eina::js::export_accessor( acc, isolate);
exports->Set(efl::eina::js::compatibility_new<v8::String>(isolate, "acc"), wrapped_acc);
std::cerr << __LINE__ << std::endl;
}
#ifndef HAVE_NODEJS
int main(int, char*[])
{
efl::eina::eina_init eina_init;
@ -127,13 +171,10 @@ int main(int, char*[])
v8::Isolate::Scope isolate_scope(isolate);
efl::eina::js::compatibility_handle_scope handle_scope(isolate);
v8::Handle<v8::Context> context;
{
// Create a template for the global object.
v8::Handle<v8::ObjectTemplate> global = efl::eina::js::compatibility_new<v8::ObjectTemplate>(isolate);
context = efl::eina::js::compatibility_new<v8::Context>(isolate, nullptr, global);
}
v8::Handle<v8::Context> context
= efl::eina::js::compatibility_new<v8::Context>
(isolate, nullptr
, efl::eina::js::compatibility_new<v8::ObjectTemplate>(isolate));
if (context.IsEmpty()) {
fprintf(stderr, "Error creating context\n");
return 1;
@ -163,19 +204,20 @@ int main(int, char*[])
v8::Context::Scope context_scope(context);
v8::Local<v8::String> name(efl::eina::js::compatibility_new<v8::String>
(nullptr, "(shell)"));
v8::Local<v8::Object> global = context->Global();
v8::Handle<v8::Object> console = efl::eina::js::compatibility_new<v8::Object>(isolate);
global->Set(efl::eina::js::compatibility_new<v8::String>(isolate, "console"), console);
console->Set(efl::eina::js::compatibility_new<v8::String>(isolate, "log")
, efl::eina::js::compatibility_new<v8::FunctionTemplate>(isolate, & ::Print)
->GetFunction());
std::cerr << __LINE__ << std::endl;
v8::Handle<v8::Object> exports = efl::eina::js::compatibility_new<v8::Object>(isolate);
context->Global()->Set(efl::eina::js::compatibility_new<v8::String>(isolate, "suite"), exports);
global->Set(efl::eina::js::compatibility_new<v8::String>(isolate, "suite"), exports);
test_setup(exports);
std::cerr << __LINE__ << std::endl;
v8::Handle<v8::Object> console = efl::eina::js::compatibility_new<v8::Object>(isolate);
context->Global()->Set(efl::eina::js::compatibility_new<v8::String>(isolate, "console"), console);
console->Set(efl::eina::js::compatibility_new<v8::String>(isolate, "log")
, efl::eina::js::compatibility_new<v8::FunctionTemplate>(isolate, & ::Print)
->GetFunction());
efl::eina::js::compatibility_handle_scope handle_scope(v8::Isolate::GetCurrent());
std::cerr << __LINE__ << std::endl;

View File

@ -59,25 +59,34 @@ assert (s2.length == 2);
assert (s2[0] == l1[1]);
assert (s2[1] == l1[2]);
console.log ("Test execution with success");
// error tests
var captured = false;
try {
clear_eina_error();
suite.clear_eina_error();
} catch(e) {
console.log("Exception ", e.toString());
captured = true;
}
assert(captured === false, '#1');
assert(captured === false, 'error #1');
captured = false;
try {
set_eina_error();
suite.set_eina_error();
} catch(e) {
assert(e.code === 'Eina_Error', '#2');
assert(e.value === 'foobar', '#3');
console.log("Exception ", e.toString());
assert(e.code === 'Eina_Error', 'error #2');
assert(e.value === 'foobar', 'error #3');
captured = true;
}
assert(captured === true, '#4');
assert(captured === true, 'error #4');
// accessor tests
assert(suite.acc.get(0) === 42, 'accessor #1');
assert(suite.acc.get(1) === 24, 'accessor #2');
// finished tests
console.log ("Test execution with success");