eina-js: Fixed tests

This commit is contained in:
Felipe Magno de Almeida 2015-01-16 23:03:16 -02:00
parent 5f77d8639a
commit 70a523e297
14 changed files with 117 additions and 1062 deletions

View File

@ -60,12 +60,7 @@ AM_TESTS_ENVIRONMENT = EINA_SUITE_PATH='$(abs_builddir)/tests/eina_js/eina_js_su
TESTS += tests/eina_js/eina_js_suite.js
lib_LTLIBRARIES += tests/eina_js/libeina_js_suite.la \
tests/eina_js/libeina_js_value.la \
tests/eina_js/libeina_js_error.la \
tests/eina_js/libeina_js_accessor.la \
tests/eina_js/libeina_js_log.la \
tests/eina_js/libeina_js_iterator.la
lib_LTLIBRARIES += tests/eina_js/libeina_js_suite.la
tests/eina_js/eina_js_suite.js: $(top_builddir)/src/tests/eina_js/eina_js_suite.node
@ -81,46 +76,11 @@ $(top_builddir)/src/tests/eina_js/eina_js_suite.node: $(top_builddir)/src/tests/
tests_eina_js_libeina_js_suite_la_SOURCES = \
tests/eina_js/eina_js_suite.cc
tests_eina_js_libeina_js_value_la_SOURCES = \
tests/eina_js/eina_js_value.cc
tests_eina_js_libeina_js_error_la_SOURCES = \
tests/eina_js/eina_js_error.cc
tests_eina_js_libeina_js_accessor_la_SOURCES = \
tests/eina_js/eina_js_accessor.cc
tests_eina_js_libeina_js_log_la_SOURCES = \
tests/eina_js/eina_js_log.cc
tests_eina_js_libeina_js_iterator_la_SOURCES = \
tests/eina_js/eina_js_iterator.cc
tests_eina_js_libeina_js_suite_la_CXXFLAGS = $(EINA_JS_TEST_CXXFLAGS)
tests_eina_js_libeina_js_suite_la_LIBADD = \
@CHECK_LIBS@ @USE_EO_LIBS@ @USE_EINA_LIBS@ @USE_EOLIAN_LIBS@ @USE_EINA_JS_LIBS@
tests_eina_js_libeina_js_suite_la_DEPENDENCIES = @USE_EINA_INTERNAL_LIBS@ @USE_EINA_JS_INTERNAL_LIBS@ @USE_EO_INTERNAL_LIBS@
tests_eina_js_libeina_js_value_la_CXXFLAGS = $(EINA_JS_TEST_CXXFLAGS)
tests_eina_js_libeina_js_value_la_LIBADD = @CHECK_LIBS@ @USE_EINA_JS_LIBS@ @USE_EINA_LIBS@ @USE_EO_LIBS@
tests_eina_js_libeina_js_value_la_DEPENDENCIES = @USE_EINA_INTERNAL_LIBS@ @USE_EINA_JS_INTERNAL_LIBS@ @USE_EO_INTERNAL_LIBS@
tests_eina_js_libeina_js_error_la_CXXFLAGS = $(EINA_JS_TEST_CXXFLAGS)
tests_eina_js_libeina_js_error_la_LIBADD = @CHECK_LIBS@ @USE_EINA_JS_LIBS@ @USE_EINA_LIBS@ @USE_EO_LIBS@
tests_eina_js_libeina_js_error_la_DEPENDENCIES = @USE_EINA_INTERNAL_LIBS@ @USE_EINA_JS_INTERNAL_LIBS@ @USE_EO_INTERNAL_LIBS@
tests_eina_js_libeina_js_accessor_la_CXXFLAGS = $(EINA_JS_TEST_CXXFLAGS)
tests_eina_js_libeina_js_accessor_la_LIBADD = @CHECK_LIBS@ @USE_EINA_JS_LIBS@ @USE_EINA_LIBS@ @USE_EO_LIBS@
tests_eina_js_libeina_js_accessor_la_DEPENDENCIES = @USE_EINA_INTERNAL_LIBS@ @USE_EINA_JS_INTERNAL_LIBS@ @USE_EO_INTERNAL_LIBS@
tests_eina_js_libeina_js_log_la_CXXFLAGS = $(EINA_JS_TEST_CXXFLAGS)
tests_eina_js_libeina_js_log_la_LIBADD = @CHECK_LIBS@ @USE_EINA_JS_LIBS@ @USE_EINA_LIBS@ @USE_EO_LIBS@
tests_eina_js_libeina_js_log_la_DEPENDENCIES = @USE_EINA_INTERNAL_LIBS@ @USE_EINA_JS_INTERNAL_LIBS@ @USE_EO_INTERNAL_LIBS@
tests_eina_js_libeina_js_iterator_la_CXXFLAGS = $(EINA_JS_TEST_CXXFLAGS)
tests_eina_js_libeina_js_iterator_la_LIBADD = @CHECK_LIBS@ @USE_EINA_JS_LIBS@ @USE_EINA_LIBS@ @USE_EO_LIBS@
tests_eina_js_libeina_js_iterator_la_DEPENDENCIES = @USE_EINA_INTERNAL_LIBS@ @USE_EINA_JS_INTERNAL_LIBS@ @USE_EO_INTERNAL_LIBS@
else
check_PROGRAMS += tests/eina_js/eina_js_suite
TESTS += tests/eina_js/eina_js_suite
@ -128,51 +88,13 @@ TESTS += tests/eina_js/eina_js_suite
tests_eina_js_eina_js_suite_SOURCES = \
tests/eina_js/eina_js_suite.cc
# tests_eina_js_eina_js_value_SOURCES = \
# tests/eina_js/eina_js_value.cc
# tests_eina_js_eina_js_error_SOURCES = \
# tests/eina_js/eina_js_error.cc
# tests_eina_js_eina_js_accessor_SOURCES = \
# tests/eina_js/eina_js_accessor.cc
# tests_eina_js_eina_js_log_SOURCES = \
# tests/eina_js/eina_js_log.cc
# tests_eina_js_eina_js_iterator_SOURCES = \
# tests/eina_js/eina_js_iterator.cc
tests_eina_js_eina_js_suite_CXXFLAGS = $(EINA_JS_TEST_CXXFLAGS)
tests_eina_js_eina_js_suite_LDADD = \
@CHECK_LIBS@ @USE_EO_LIBS@ @USE_EINA_LIBS@ @USE_EOLIAN_LIBS@ @USE_EINA_JS_LIBS@ @EINA_JS_LIBS@
tests_eina_js_eina_js_suite_DEPENDENCIES = @USE_EINA_INTERNAL_LIBS@ @USE_EINA_JS_INTERNAL_LIBS@ @USE_EO_INTERNAL_LIBS@ tests/eina_js/eina_js_suite.js
# tests_eina_js_eina_js_value_CXXFLAGS = $(EINA_JS_TEST_CXXFLAGS)
# tests_eina_js_eina_js_value_LDADD = @CHECK_LIBS@ @USE_EINA_JS_LIBS@ @USE_EINA_LIBS@ @USE_EO_LIBS@
# tests_eina_js_eina_js_value_DEPENDENCIES = @USE_EINA_INTERNAL_LIBS@ @USE_EINA_JS_INTERNAL_LIBS@ @USE_EO_INTERNAL_LIBS@
# tests_eina_js_eina_js_error_CXXFLAGS = $(EINA_JS_TEST_CXXFLAGS)
# tests_eina_js_eina_js_error_LDADD = @CHECK_LIBS@ @USE_EINA_JS_LIBS@ @USE_EINA_LIBS@ @USE_EO_LIBS@
# tests_eina_js_eina_js_error_DEPENDENCIES = @USE_EINA_INTERNAL_LIBS@ @USE_EINA_JS_INTERNAL_LIBS@ @USE_EO_INTERNAL_LIBS@
# tests_eina_js_eina_js_accessor_CXXFLAGS = $(EINA_JS_TEST_CXXFLAGS)
# tests_eina_js_eina_js_accessor_LDADD = @CHECK_LIBS@ @USE_EINA_JS_LIBS@ @USE_EINA_LIBS@ @USE_EO_LIBS@
# tests_eina_js_eina_js_accessor_DEPENDENCIES = @USE_EINA_INTERNAL_LIBS@ @USE_EINA_JS_INTERNAL_LIBS@ @USE_EO_INTERNAL_LIBS@
# tests_eina_js_eina_js_log_CXXFLAGS = $(EINA_JS_TEST_CXXFLAGS)
# tests_eina_js_eina_js_log_LDADD = @CHECK_LIBS@ @USE_EINA_JS_LIBS@ @USE_EINA_LIBS@ @USE_EO_LIBS@
# tests_eina_js_eina_js_log_DEPENDENCIES = @USE_EINA_INTERNAL_LIBS@ @USE_EINA_JS_INTERNAL_LIBS@ @USE_EO_INTERNAL_LIBS@
# tests_eina_js_eina_js_iterator_CXXFLAGS = $(EINA_JS_TEST_CXXFLAGS)
# tests_eina_js_eina_js_iterator_LDADD = @CHECK_LIBS@ @USE_EINA_JS_LIBS@ @USE_EINA_LIBS@ @USE_EO_LIBS@
# tests_eina_js_eina_js_iterator_DEPENDENCIES = @USE_EINA_INTERNAL_LIBS@ @USE_EINA_JS_INTERNAL_LIBS@ @USE_EO_INTERNAL_LIBS@
endif
#EXTRA_DIST += tests/eina_js/eina_js_suite.node
endif
endif

View File

@ -271,20 +271,11 @@ compatibility_return()
return compatibility_return_nil_impl(std::integral_constant<bool, v8_uses_callback_info>());
}
template <typename T = std::integral_constant<bool, v8_uses_isolate> >
struct _v8_isolate_throw_exception;
template <>
struct _v8_isolate_throw_exception<std::true_type> : v8::Isolate
struct _v8_isolate_throw_exception : v8::Isolate
{
};
template <>
struct _v8_isolate_throw_exception<std::false_type> : v8::Isolate
{
template <typename...Args>
static v8::Handle<v8::Value> ThrowException(v8::Handle<v8::Value> v, Args...args)
v8::Handle<v8::Value> ThrowException_impl(v8::Handle<v8::Value> v)
{
using namespace v8;
return ThrowException(v);
}
};
@ -292,13 +283,13 @@ struct _v8_isolate_throw_exception<std::false_type> : v8::Isolate
inline void
compatibility_throw_impl(v8::Isolate* isolate, v8::Local<v8::Value> exception, std::true_type)
{
static_cast<_v8_isolate_throw_exception<>*>(isolate)->ThrowException(exception);
static_cast<_v8_isolate_throw_exception*>(isolate)->ThrowException_impl(exception);
}
inline v8::Handle<v8::Value>
compatibility_throw_impl(v8::Isolate* isolate, v8::Local<v8::Value> exception, std::false_type)
{
return static_cast<_v8_isolate_throw_exception<>*>(isolate)->ThrowException(exception);
return static_cast<_v8_isolate_throw_exception*>(isolate)->ThrowException_impl(exception);
}
inline std::conditional<v8_uses_isolate, void, v8::Handle<v8::Value> >::type
@ -310,13 +301,13 @@ compatibility_throw(v8::Isolate* isolate, v8::Local<v8::Value> exception)
inline void
compatibility_throw_impl(v8::Local<v8::Value> exception, std::true_type)
{
static_cast<_v8_isolate_throw_exception<>*>(v8::Isolate::GetCurrent())->ThrowException(exception);
static_cast<_v8_isolate_throw_exception*>(v8::Isolate::GetCurrent())->ThrowException_impl(exception);
}
inline v8::Handle<v8::Value>
compatibility_throw_impl(v8::Local<v8::Value> exception, std::false_type)
{
return static_cast<_v8_isolate_throw_exception<>*>(v8::Isolate::GetCurrent())->ThrowException(exception);
return static_cast<_v8_isolate_throw_exception*>(v8::Isolate::GetCurrent())->ThrowException_impl(exception);
}
inline std::conditional<v8_uses_isolate, void, v8::Handle<v8::Value> >::type

View File

@ -10,19 +10,20 @@ namespace efl { namespace eina {namespace js {
EAPI
js::compatibility_return_type convert_error_to_javascript_exception(v8::Isolate *isolate)
{
using v8::Local;
using v8::Object;
using v8::String;
std::cerr << __func__ << ':' << __LINE__ << std::endl;
Eina_Error err = eina_error_get();
if (!err)
return;
return compatibility_return();
std::cerr << __func__ << ':' << __LINE__ << std::endl;
Local<Object> je = eina::js::compatibility_new<v8::Object>(isolate);
v8::Local<v8::Object> je = eina::js::compatibility_new<v8::Object>(isolate);
std::cerr << __func__ << ':' << __LINE__ << std::endl;
je->Set(compatibility_new<v8::String>(isolate, "code"),
compatibility_new<v8::String>(isolate, "Eina_Error"));
std::cerr << __func__ << ':' << __LINE__ << std::endl;
je->Set(compatibility_new<v8::String>(isolate, "value"),
compatibility_new<v8::String>(isolate, eina_error_msg_get(err)));
std::cerr << __func__ << ':' << __LINE__ << std::endl;
return compatibility_throw(isolate, je);
}

View File

@ -4,6 +4,8 @@
#include <Eina.hh>
#include EINA_STRINGIZE(V8_INCLUDE_HEADER)
#include <eina_js_compatibility.hh>
namespace efl { namespace eina { namespace js {
/**
@ -19,7 +21,7 @@ namespace efl { namespace eina { namespace js {
* exception will be generated for each call after some error is reached. We,
* therefore, suggest you to call `eina_error_set(0)` afterwards.
*/
void convert_error_to_javascript_exception(v8::Isolate *isolate);
js::compatibility_return_type convert_error_to_javascript_exception(v8::Isolate *isolate);
} } } // namespace efl::eina::js

View File

@ -24,46 +24,39 @@ template<class T>
v8::Local<v8::Object> export_iterator(::efl::eina::iterator<T> *i,
v8::Isolate *isolate)
{
using v8::Local;
using v8::Value;
using v8::Object;
using v8::String;
using v8::FunctionCallbackInfo;
using v8::FunctionTemplate;
using v8::ObjectTemplate;
typedef ::efl::eina::iterator<T> value_type;
typedef value_type *ptr_type;
typedef void (*deleter_t)(void*);
auto obj_tpl = ObjectTemplate::New(isolate);
auto obj_tpl = compatibility_new<v8::ObjectTemplate>(isolate);
obj_tpl->SetInternalFieldCount(2);
auto ret = obj_tpl->NewInstance();
auto next = [](const FunctionCallbackInfo<Value> &info) {
auto next = [](js::compatibility_callback_info_type info) -> compatibility_return_type
{
if (info.Length() != 0)
return;
return compatibility_return();
void *ptr = info.This()->GetAlignedPointerFromInternalField(0);
void *ptr = compatibility_get_pointer_internal_field(info.This(), 0);
auto &value = *static_cast<ptr_type>(ptr);
Local<Object> o = v8::Object::New(info.GetIsolate());
o->Set(String::NewFromUtf8(info.GetIsolate(), "value"),
value_cast<Local<Value>>(*value, info.GetIsolate()));
info.GetReturnValue().Set(o);
v8::Local<v8::Object> o = compatibility_new<v8::Object>(info.GetIsolate());
o->Set(compatibility_new<v8::String>(info.GetIsolate(), "value"),
value_cast<v8::Local<v8::Value>>(*value, info.GetIsolate()));
++value;
};
return compatibility_return(o, info);
};
ret->Set(String::NewFromUtf8(isolate, "next"),
FunctionTemplate::New(isolate, next)->GetFunction());
ret->Set(compatibility_new<v8::String>(isolate, "next"),
compatibility_new<v8::FunctionTemplate>(isolate, next)->GetFunction());
{
deleter_t deleter = [](void *i) {
delete static_cast<ptr_type>(i);
};
ret->SetAlignedPointerInInternalField(0, i);
ret->SetAlignedPointerInInternalField(1,
reinterpret_cast<void*>(deleter));
compatibility_set_pointer_internal_field(ret, 0, i);
compatibility_set_pointer_internal_field
(ret, 1, reinterpret_cast<void*>(deleter));
}
return ret;

View File

@ -19,64 +19,77 @@ compatibility_return_type eina_value_set(compatibility_callback_info_type args)
try {
*static_cast<value*>(ptr) = value_cast<value>(args[0]);
} catch(const std::bad_cast &e) {
v8::Local<v8::Object> je = v8::Object::New(isolate);
je->Set(v8::String::NewFromUtf8(isolate, "code"),
v8::String::NewFromUtf8(isolate, "std::bad_cast"));
isolate->ThrowException(je);
v8::Local<v8::Object> je = compatibility_new<v8::Object>(isolate);
je->Set(compatibility_new<v8::String>(isolate, "code"),
compatibility_new<v8::String>(isolate, "std::bad_cast"));
return compatibility_throw(isolate, je);
} catch(const ::efl::eina::system_error &e) {
v8::Local<v8::Object> je = v8::Object::New(isolate);
je->Set(v8::String::NewFromUtf8(isolate, "code"),
v8::String::NewFromUtf8(isolate, "std::error_code"));
je->Set(v8::String::NewFromUtf8(isolate, "category"),
v8::String::NewFromUtf8(isolate, e.code().category().name()));
je->Set(v8::String::NewFromUtf8(isolate, "value"),
v8::Integer::New(isolate, e.code().value()));
isolate->ThrowException(je);
v8::Local<v8::Object> je = compatibility_new<v8::Object>(isolate);
je->Set(compatibility_new<v8::String>(isolate, "code"),
compatibility_new<v8::String>(isolate, "std::error_code"));
je->Set(compatibility_new<v8::String>(isolate, "category"),
compatibility_new<v8::String>(isolate, e.code().category().name()));
je->Set(compatibility_new<v8::String>(isolate, "value"),
compatibility_new<v8::Integer>(isolate, e.code().value()));
return compatibility_throw(isolate, je);
}
return compatibility_return();
}
compatibility_return_type eina_value_get(compatibility_callback_info_type args)
{
void *ptr = args.Holder()->GetAlignedPointerFromInternalField(0);
void *ptr = compatibility_get_pointer_internal_field(args.Holder(), 0);
auto &value = *static_cast<eina::value*>(ptr);
args.GetReturnValue().Set(value_cast<v8::Local<v8::Value>>
(value, args.GetIsolate()));
return compatibility_return
(value_cast<v8::Local<v8::Value>>(value, args.GetIsolate()), args);
}
compatibility_return_type eina_value_constructor(compatibility_callback_info_type args)
{
std::cout << __func__ << ':' << __LINE__ << std::endl;
if (args.Length() != 1)
return;
return compatibility_return();
std::cout << __func__ << ':' << __LINE__ << std::endl;
v8::Isolate* isolate = args.GetIsolate();
try {
std::cout << __func__ << ':' << __LINE__ << std::endl;
std::unique_ptr<value>
ptr(new value(value_cast<value>(args[0])));
args.This()->SetAlignedPointerInInternalField(0, ptr.get());
std::cout << __func__ << ':' << __LINE__ << std::endl;
compatibility_set_pointer_internal_field(args.This(), 0, ptr.get());
std::cout << __func__ << ':' << __LINE__ << std::endl;
ptr.release();
} catch(const std::bad_cast &e) {
v8::Local<v8::Object> je = v8::Object::New(isolate);
je->Set(v8::String::NewFromUtf8(isolate, "code"),
v8::String::NewFromUtf8(isolate, "std::bad_cast"));
isolate->ThrowException(je);
std::cout << __func__ << ':' << __LINE__ << std::endl;
v8::Local<v8::Object> je = compatibility_new<v8::Object>(isolate);
std::cout << __func__ << ':' << __LINE__ << std::endl;
je->Set(compatibility_new<v8::String>(isolate, "code"),
compatibility_new<v8::String>(isolate, "std::bad_cast"));
std::cout << __func__ << ':' << __LINE__ << std::endl;
return compatibility_throw(isolate,je);
} catch(const ::efl::eina::system_error &e) {
v8::Local<v8::Object> je = v8::Object::New(isolate);
je->Set(v8::String::NewFromUtf8(isolate, "code"),
v8::String::NewFromUtf8(isolate, "std::error_code"));
je->Set(v8::String::NewFromUtf8(isolate, "category"),
v8::String::NewFromUtf8(isolate, e.code().category().name()));
je->Set(v8::String::NewFromUtf8(isolate, "value"),
v8::Integer::New(isolate, e.code().value()));
isolate->ThrowException(je);
std::cout << __func__ << ':' << __LINE__ << std::endl;
v8::Local<v8::Object> je = compatibility_new<v8::Object>(isolate);
std::cout << __func__ << ':' << __LINE__ << std::endl;
je->Set(compatibility_new<v8::String>(isolate, "code"),
compatibility_new<v8::String>(isolate, "std::error_code"));
std::cout << __func__ << ':' << __LINE__ << std::endl;
je->Set(compatibility_new<v8::String>(isolate, "category"),
compatibility_new<v8::String>(isolate, e.code().category().name()));
std::cout << __func__ << ':' << __LINE__ << std::endl;
je->Set(compatibility_new<v8::String>(isolate, "value"),
compatibility_new<v8::Integer>(isolate, e.code().value()));
std::cout << __func__ << ':' << __LINE__ << std::endl;
return compatibility_throw(isolate, je);
}
}
}
EAPI
void register_make_value(v8::Isolate *isolate, v8::Handle<v8::Object> global,
void register_value(v8::Isolate *isolate, v8::Handle<v8::Object> global,
v8::Handle<v8::String> name)
{
using v8::Isolate;
@ -112,25 +125,22 @@ EAPI
void register_destroy_value(v8::Isolate *isolate, v8::Handle<v8::Object> global,
v8::Handle<v8::String> name)
{
using v8::Local;
using v8::Value;
using v8::FunctionTemplate;
using v8::FunctionCallbackInfo;
typedef ::efl::eina::value value_type;
typedef value_type *ptr_type;
auto dtor = [](const FunctionCallbackInfo<Value> &info) {
auto dtor = [](compatibility_callback_info_type info) -> js::compatibility_return_type
{
if (info.Length() != 1)
return;
return compatibility_return();
auto o = info[0]->ToObject();
delete static_cast<ptr_type>(o->GetAlignedPointerFromInternalField(0));
o->SetAlignedPointerInInternalField(0, nullptr);
assert(o->GetAlignedPointerFromInternalField(0) == nullptr);
};
delete static_cast<ptr_type>(compatibility_get_pointer_internal_field(o, 0));
compatibility_set_pointer_internal_field<void*>(o, 0, nullptr);
assert(compatibility_get_pointer_internal_field(o, 0) == nullptr);
return compatibility_return();
};
global->Set(name, FunctionTemplate::New(isolate, dtor)->GetFunction());
global->Set(name, compatibility_new<v8::FunctionTemplate>(isolate, dtor)->GetFunction());
}
} } } // namespace efl { namespace js {

View File

@ -134,8 +134,8 @@ value_cast(const v8::Handle<v8::Value> &v)
- The returned object has a `set()` method, which can be used to change the
wrapped value.
*/
void register_make_value(v8::Isolate *isolate, v8::Handle<v8::Object> global,
v8::Handle<v8::String> name);
void register_value(v8::Isolate *isolate, v8::Handle<v8::Object> global,
v8::Handle<v8::String> name);
void register_destroy_value(v8::Isolate *isolate, v8::Handle<v8::Object> global,
v8::Handle<v8::String> name);

View File

@ -1,104 +0,0 @@
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <cassert>
#include <eina_js_accessor.hh>
#include <eina_js_compatibility.hh>
#include <Eo.hh>
#include <Eina.hh>
#include <iostream>
efl::eina::js::compatibility_return_type print(efl::eina::js::compatibility_callback_info_type args)
{
bool first = true;
for (int i = 0; i < args.Length(); i++) {
efl::eina::js::compatibility_handle_scope handle_scope(args.GetIsolate());
if (first) {
first = false;
} else {
printf(" ");
}
v8::String::Utf8Value str(args[i]);
std::cout << std::string(*str, str.length());
}
printf("\n");
fflush(stdout);
return efl::eina::js::compatibility_return();
}
static const char script[] =
"function assert(test, message) { if (test !== true) throw message; };"
"assert(acc.get(0) === 42, '#1');"
"assert(acc.get(1) === 24, '#2');"
"destroy_accessor(acc);"
;
int main(int argc, char *argv[])
{
efl::eina::eina_init eina_init;
efl::eo::eo_init eo_init;
efl::eina::js::compatibility_initialize();
v8::V8::SetFlagsFromCommandLine(&argc, argv, true);
v8::Isolate* isolate = v8::Isolate::New();
v8::Isolate::Scope isolate_scope(isolate);
efl::eina::js::compatibility_handle_scope handle_scope(isolate);
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;
}
{
// Enter the execution environment before evaluating any code.
v8::Context::Scope context_scope(context);
v8::Handle<v8::Object> global = context->Global();
global->Set(efl::eina::js::compatibility_new<v8::String>(isolate, "print"),
efl::eina::js::compatibility_new<v8::FunctionTemplate>(isolate, print)->GetFunction());
efl::eina::js::register_destroy_accessor
(isolate, global
, efl::eina::js::compatibility_new<v8::String>(isolate, "destroy_accessor"));
Eina_Array *array = [](){
static int impl[2] = {42, 24};
Eina_Array *a = eina_array_new(2);
eina_array_push(a, impl);
eina_array_push(a, impl+1);
return a;
}();
efl::eina::accessor<int> acc(eina_array_accessor_new(array));
v8::Local<v8::Object> wrapped_acc = efl::eina::js::export_accessor(acc, isolate);
global->Set(efl::eina::js::compatibility_new<v8::String>(isolate, "acc"), wrapped_acc);
assert(efl::eina::js::import_accessor<int>(wrapped_acc)[0] == 42);
{
efl::eina::js::compatibility_handle_scope h(isolate);
v8::TryCatch try_catch;
auto source = efl::eina::js::compatibility_new<v8::String>(isolate, script);
v8::Handle<v8::Script> script = v8::Script::Compile(std::move(source));
assert(!script.IsEmpty());
/*v8::Handle<v8::Value> result = */script->Run();
if (try_catch.HasCaught()) {
v8::String::Utf8Value message(try_catch.Message()->Get());
std::cerr << std::string(*message, message.length()) << std::endl;
std::abort();
}
}
eina_array_free(array);
}
}

View File

@ -1,102 +0,0 @@
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <cassert>
#include <eina_js_error.hh>
#include <eina_js_compatibility.hh>
#include <Eo.hh>
efl::eina::js::compatibility_return_type print(efl::eina::js::compatibility_callback_info_type args)
{
bool first = true;
for (int i = 0; i < args.Length(); i++) {
efl::eina::js::compatibility_handle_scope handle_scope(args.GetIsolate());
if (first) {
first = false;
} else {
printf(" ");
}
v8::String::Utf8Value str(args[i]);
std::cout << std::string(*str, str.length());
}
printf("\n");
fflush(stdout);
return efl::eina::js::compatibility_return();
}
static const char script[] =
"function assert(test, message) { if (test !== true) throw message; };"
"var captured = false;"
"try {"
" clear_eina_error();"
"} catch(e) {"
" captured = true;"
"}"
"assert(captured === false, '#1');"
"captured = false;"
"try {"
" set_eina_error();"
"} catch(e) {"
" assert(e.code === 'Eina_Error', '#2');"
" assert(e.value === 'foobar', '#3');"
" captured = true;"
"}"
"assert(captured === true, '#4');"
;
int main(int argc, char *argv[])
{
efl::eina::eina_init eina_init;
efl::eo::eo_init eo_init;
efl::eina::js::compatibility_initialize();
v8::V8::SetFlagsFromCommandLine(&argc, argv, true);
v8::Isolate* isolate = v8::Isolate::New();
v8::Isolate::Scope isolate_scope(isolate);
efl::eina::js::compatibility_handle_scope handle_scope(isolate);
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;
}
{
// Enter the execution environment before evaluating any code.
v8::Context::Scope context_scope(context);
v8::Handle<v8::Object> global = context->Global();
global->Set(efl::eina::js::compatibility_new<v8::String>(isolate, "print"),
efl::eina::js::compatibility_new<v8::FunctionTemplate>(isolate, print)->GetFunction());
global->Set(efl::eina::js::compatibility_new<v8::String>(isolate, "clear_eina_error"),
efl::eina::js::compatibility_new<v8::FunctionTemplate>(isolate, clear_eina_error)
->GetFunction());
global->Set(efl::eina::js::compatibility_new<v8::String>(isolate, "set_eina_error"),
efl::eina::js::compatibility_new<v8::FunctionTemplate>(isolate, set_eina_error)
->GetFunction());
{
efl::eina::js::compatibility_handle_scope handle_scope(isolate);
v8::TryCatch try_catch;
auto source = efl::eina::js::compatibility_new<v8::String>(isolate, script);
v8::Handle<v8::Script> script = v8::Script::Compile(std::move(source));
assert(!script.IsEmpty());
/*v8::Handle<v8::Value> result = */script->Run();
if (try_catch.HasCaught()) {
v8::String::Utf8Value message(try_catch.Message()->Get());
std::cerr << std::string(*message, message.length()) << std::endl;
std::abort();
}
}
}
}

View File

@ -1,103 +0,0 @@
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <cassert>
#include <eina_js_iterator.hh>
#include <eina_js_compatibility.hh>
#include <Eo.hh>
#include <Eina.hh>
#include <iostream>
efl::eina::js::compatibility_return_type print(efl::eina::js::compatibility_callback_info_type args)
{
bool first = true;
for (int i = 0; i < args.Length(); i++) {
efl::eina::js::compatibility_handle_scope handle_scope(args.GetIsolate());
if (first) {
first = false;
} else {
printf(" ");
}
v8::String::Utf8Value str(args[i]);
std::cout << std::string(*str, str.length());
}
printf("\n");
fflush(stdout);
return efl::eina::js::compatibility_return();
}
static const char script[] =
"function assert(test, message) { if (test !== true) throw message; };"
"assert(it.next().value === 42, '#1');"
"assert(it.next().value === 24, '#2');"
"destroy_iterator(it);"
;
int main(int argc, char *argv[])
{
efl::eina::eina_init eina_init;
efl::eo::eo_init eo_init;
efl::eina::js::compatibility_initialize();
v8::V8::SetFlagsFromCommandLine(&argc, argv, true);
v8::Isolate* isolate = v8::Isolate::New();
v8::Isolate::Scope isolate_scope(isolate);
efl::eina::js::compatibility_handle_scope handle_scope(isolate);
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;
}
{
// Enter the execution environment before evaluating any code.
v8::Context::Scope context_scope(context);
v8::Handle<v8::Object> global = context->Global();
global->Set(efl::eina::js::compatibility_new<v8::String>(isolate, "print"),
efl::eina::js::compatibility_new<v8::FunctionTemplate>(isolate, print)->GetFunction());
efl::eina::js::register_destroy_iterator
(isolate, global
, efl::eina::js::compatibility_new<v8::String>(isolate, "destroy_iterator"));
Eina_Array *array = [](){
static int impl[3] = {42, 24, 0};
Eina_Array *a = eina_array_new(3);
eina_array_push(a, impl);
eina_array_push(a, impl+1);
eina_array_push(a, impl+2);
return a;
}();
efl::eina::iterator<int> it(eina_array_iterator_new(array));
v8::Local<v8::Object> wrapped_it = efl::eina::js::export_iterator(&it, isolate);
global->Set(efl::eina::js::compatibility_new<v8::String>(isolate, "it"), wrapped_it);
assert(**efl::eina::js::import_iterator<int>(wrapped_it) == 42);
{
efl::eina::js::compatibility_handle_scope handle_scope(isolate);
v8::TryCatch try_catch;
auto source = efl::eina::js::compatibility_new<v8::String>(isolate, script);
v8::Handle<v8::Script> script = v8::Script::Compile(std::move(source));
assert(!script.IsEmpty());
/*v8::Handle<v8::Value> result = */script->Run();
if (try_catch.HasCaught()) {
v8::String::Utf8Value message(try_catch.Message()->Get());
std::cerr << std::string(*message, message.length()) << std::endl;
std::abort();
}
}
}
}

View File

@ -1,295 +0,0 @@
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <string>
#include <cassert>
#include <eina_js_value.hh>
#include <eina_js_log.hh>
#include <Eo.hh>
efl::eina::js::compatibility_return_type print(efl::eina::js::compatibility_callback_info_type args)
{
bool first = true;
for (int i = 0; i < args.Length(); i++) {
efl::eina::js::compatibility_handle_scope handle_scope(args.GetIsolate());
if (first) {
first = false;
} else {
printf(" ");
}
v8::String::Utf8Value str(args[i]);
std::cout << std::string(*str, str.length());
}
printf("\n");
fflush(stdout);
return efl::eina::js::compatibility_return();
}
static void eina_log_print_cb_js_test(const Eina_Log_Domain *d,
Eina_Log_Level level, const char *file,
const char *fnc, int line,
const char */*fmt*/, void */*data*/,
va_list args)
{
static int index = 0;
const char *msg = va_arg(args, const char*);
std::string domains[] = {"", "", "", "mydomain", "mydomain2"};
int levels[] = {EINA_LOG_LEVEL_DBG, EINA_LOG_LEVEL_CRITICAL,
EINA_LOG_LEVEL_WARN, EINA_LOG_LEVEL_INFO,
EINA_LOG_LEVEL_ERR};
std::string functions[] = {"f1", "", "f2", "f3", ""};
int lines[] = {2, 3, 4, 5, 9};
std::string messages[] = {"I changed again", "Cool to Hate",
"One Fine Day", "Never Gonna Find Me",
"The Kids Aren't Alright"};
assert(std::string(d->name, d->namelen) == domains[index]);
assert(level == levels[index]);
assert(file == std::string("offspring.js"));
assert(fnc == functions[index]);
assert(line == lines[index]);
assert(msg == messages[index]);
++index;
}
#define X(x, y) "function " x "(){" y "};" x "();"
static const char raw_script[] =
"function assert(test, message) { if (test !== true) throw message; };\n"
X("f1", "log_print(LOG_DOMAIN_GLOBAL, LOG_LEVEL_DBG, 'I changed again');\n")
"log_print(LOG_DOMAIN_GLOBAL, LOG_LEVEL_CRITICAL, 'Cool to Hate');\n"
X("f2", "log_print(LOG_DOMAIN_GLOBAL, LOG_LEVEL_WARN, 'One Fine Day');\n")
X("f3", "log_print(mydomain, LOG_LEVEL_INFO, 'Never Gonna Find Me');\n")
"mydomain2 = log_domain_register('mydomain2', '');\n"
"log_domain_registered_level_set(mydomain2, LOG_LEVEL_DBG);\n"
"assert(log_domain_registered_level_get(mydomain2) === LOG_LEVEL_DBG,"
" '#1');\n"
"log_print(mydomain2, LOG_LEVEL_ERR, \"The Kids Aren't Alright\");\n"
"log_domain_unregister(mydomain2);\n"
"mydomain2 = undefined;\n"
"log_color_disable_set(true);\n"
"assert(log_color_disable_get() === true, '#2');\n"
"log_color_disable_set(false);\n"
"assert(log_color_disable_get() === false, '#3');\n"
"log_file_disable_set(true);\n"
"assert(log_file_disable_get() === true, '#4');\n"
"log_file_disable_set(false);\n"
"assert(log_file_disable_get() === false, '#5');\n"
"log_function_disable_set(true);\n"
"assert(log_function_disable_get() === true, '#6');\n"
"log_function_disable_set(false);\n"
"assert(log_function_disable_get() === false, '#7');\n"
"log_abort_on_critical_set(true);\n"
"assert(log_abort_on_critical_get() === true, '#8');\n"
"log_abort_on_critical_set(false);\n"
"assert(log_abort_on_critical_get() === false, '#9');\n"
"var entered = false;\n"
"log_print_cb_set(function(domain, color, level, file, func, line, msg) {"
" assert(domain === 'mydomain', '#10');\n"
" assert(color === '', '#11');\n"
" assert(level === LOG_LEVEL_WARN, '#12');\n"
" assert(file === 'offspring.js', '#13');\n"
" assert(func === 'f4', '#14');\n"
" assert(line === 33, '#15');\n"
" assert(msg === 'What Happened To You', '#16');\n"
" entered = true;"
"});\n"
X("f4", "log_print(mydomain, LOG_LEVEL_WARN, 'What Happened To You');\n")
"assert(entered === true, '#17');\n"
"assert(log_level_get() === LOG_LEVEL_DBG, '#18');\n"
"assert(log_level_check(LOG_LEVEL_INFO) === true, '#19');\n"
"log_level_set(LOG_LEVEL_CRITICAL);\n"
"assert(log_level_get() === LOG_LEVEL_CRITICAL, '#20');\n"
"assert(log_level_check(LOG_LEVEL_INFO) === false, '#21');\n"
"assert(log_abort_on_critical_get() === false, '#22');\n"
"log_abort_on_critical_set(true);\n"
"assert(log_abort_on_critical_get() === true, '#23');\n"
"log_abort_on_critical_level_set(LOG_LEVEL_CRITICAL);"
"assert(log_abort_on_critical_level_get() == LOG_LEVEL_CRITICAL, '#24');\n"
"log_abort_on_critical_level_set(LOG_LEVEL_ERR);"
"assert(log_abort_on_critical_level_get() == LOG_LEVEL_ERR, '#25');\n"
"log_domain_level_set('mydomain', LOG_LEVEL_WARN);\n"
"assert(log_domain_level_get('mydomain') === LOG_LEVEL_WARN, '#26');\n"
"log_domain_level_set('mydomain', LOG_LEVEL_INFO);\n"
"assert(log_domain_level_get('mydomain') === LOG_LEVEL_INFO, '#27');\n"
"assert(typeof(LOG_STATE_START) === 'number', '#28');\n"
"assert(typeof(LOG_STATE_STOP) === 'number', '#29');\n"
"assert(typeof(log_timing) === 'function', '#30');\n"
;
#undef X
int main(int argc, char *argv[])
{
efl::eina::eina_init eina_init;
efl::eo::eo_init eo_init;
efl::eina::js::compatibility_initialize();
v8::V8::SetFlagsFromCommandLine(&argc, argv, true);
v8::Isolate* isolate = v8::Isolate::New();
v8::Isolate::Scope isolate_scope(isolate);
efl::eina::js::compatibility_handle_scope handle_scope(isolate);
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;
}
{
// Enter the execution environment before evaluating any code.
v8::Context::Scope context_scope(context);
v8::Handle<v8::Object> global = context->Global();
global->Set(efl::eina::js::compatibility_new<v8::String>(isolate, "print"),
efl::eina::js::compatibility_new<v8::FunctionTemplate>(isolate, print)->GetFunction());
{
int d = eina_log_domain_register("mydomain", "");
eina_log_domain_registered_level_set(d, EINA_LOG_LEVEL_DBG);
global->Set(efl::eina::js::compatibility_new<v8::String>(isolate, "mydomain"),
efl::eina::js::value_cast<v8::Local<v8::Value>>(d, isolate));
}
efl::eina::js::register_log_level_critical
(isolate, global,
efl::eina::js::compatibility_new<v8::String>
(isolate, "LOG_LEVEL_CRITICAL"));
efl::eina::js::register_log_level_err
(isolate, global,
efl::eina::js::compatibility_new<v8::String>(isolate, "LOG_LEVEL_ERR"));
efl::eina::js::register_log_level_warn
(isolate, global,
efl::eina::js::compatibility_new<v8::String>(isolate, "LOG_LEVEL_WARN"));
efl::eina::js::register_log_level_info
(isolate, global,
efl::eina::js::compatibility_new<v8::String>(isolate, "LOG_LEVEL_INFO"));
efl::eina::js::register_log_level_dbg
(isolate, global,
efl::eina::js::compatibility_new<v8::String>(isolate, "LOG_LEVEL_DBG"));
efl::eina::js::register_log_domain_global
(isolate, global,
efl::eina::js::compatibility_new<v8::String>(isolate, "LOG_DOMAIN_GLOBAL"));
efl::eina::js::register_log_print
(isolate, global,
efl::eina::js::compatibility_new<v8::String>(isolate, "log_print"));
efl::eina::js::register_log_domain_register
(isolate, global,
efl::eina::js::compatibility_new<v8::String>(isolate, "log_domain_register"));
efl::eina::js::register_log_domain_unregister
(isolate, global,
efl::eina::js::compatibility_new<v8::String>(isolate, "log_domain_unregister"));
efl::eina::js::register_log_domain_registered_level_get
(isolate, global,
efl::eina::js::compatibility_new<v8::String>(isolate, "log_domain_registered_level_get"));
efl::eina::js::register_log_domain_registered_level_set
(isolate, global,
efl::eina::js::compatibility_new<v8::String>(isolate, "log_domain_registered_level_set"));
efl::eina::js::register_log_print_cb_set
(isolate, global,
efl::eina::js::compatibility_new<v8::String>(isolate, "log_print_cb_set"));
efl::eina::js::register_log_level_set
(isolate, global,
efl::eina::js::compatibility_new<v8::String>(isolate, "log_level_set"));
efl::eina::js::register_log_level_get
(isolate, global,
efl::eina::js::compatibility_new<v8::String>(isolate, "log_level_get"));
efl::eina::js::register_log_level_check
(isolate, global,
efl::eina::js::compatibility_new<v8::String>(isolate, "log_level_check"));
efl::eina::js::register_log_color_disable_set
(isolate, global,
efl::eina::js::compatibility_new<v8::String>(isolate, "log_color_disable_set"));
efl::eina::js::register_log_color_disable_get
(isolate, global,
efl::eina::js::compatibility_new<v8::String>(isolate, "log_color_disable_get"));
efl::eina::js::register_log_file_disable_set
(isolate, global,
efl::eina::js::compatibility_new<v8::String>(isolate, "log_file_disable_set"));
efl::eina::js::register_log_file_disable_get
(isolate, global,
efl::eina::js::compatibility_new<v8::String>(isolate, "log_file_disable_get"));
efl::eina::js::register_log_abort_on_critical_set
(isolate, global,
efl::eina::js::compatibility_new<v8::String>(isolate, "log_abort_on_critical_set"));
efl::eina::js::register_log_abort_on_critical_get
(isolate, global,
efl::eina::js::compatibility_new<v8::String>(isolate, "log_abort_on_critical_get"));
efl::eina::js::register_log_function_disable_set
(isolate, global,
efl::eina::js::compatibility_new<v8::String>(isolate, "log_function_disable_set"));
efl::eina::js::register_log_function_disable_get
(isolate, global,
efl::eina::js::compatibility_new<v8::String>(isolate, "log_function_disable_get"));
efl::eina::js::register_log_abort_on_critical_set
(isolate, global,
efl::eina::js::compatibility_new<v8::String>(isolate, "log_abort_on_critical_set"));
efl::eina::js::register_log_abort_on_critical_get
(isolate, global,
efl::eina::js::compatibility_new<v8::String>(isolate, "log_abort_on_critical_get"));
efl::eina::js::register_log_abort_on_critical_level_set
(isolate, global,
efl::eina::js::compatibility_new<v8::String>(isolate, "log_abort_on_critical_level_set"));
efl::eina::js::register_log_abort_on_critical_level_get
(isolate, global,
efl::eina::js::compatibility_new<v8::String>(isolate, "log_abort_on_critical_level_get"));
efl::eina::js::register_log_domain_level_set
(isolate, global,
efl::eina::js::compatibility_new<v8::String>(isolate, "log_domain_level_set"));
efl::eina::js::register_log_domain_level_get
(isolate, global,
efl::eina::js::compatibility_new<v8::String>(isolate, "log_domain_level_get"));
efl::eina::js::register_log_state_start
(isolate, global,
efl::eina::js::compatibility_new<v8::String>(isolate, "LOG_STATE_START"));
efl::eina::js::register_log_state_stop
(isolate, global,
efl::eina::js::compatibility_new<v8::String>(isolate, "LOG_STATE_STOP"));
efl::eina::js::register_log_timing
(isolate, global,
efl::eina::js::compatibility_new<v8::String>(isolate, "log_timing"));
eina_log_print_cb_set(eina_log_print_cb_js_test, NULL);
eina_log_level_set(EINA_LOG_LEVEL_DBG);
eina_log_abort_on_critical_set(EINA_FALSE);
{
efl::eina::js::compatibility_handle_scope handle_scope(isolate);
v8::TryCatch try_catch;
v8::Handle<v8::Script> script = [&]() {
auto source = efl::eina::js::compatibility_new<v8::String>(isolate, raw_script);
auto filename = efl::eina::js::compatibility_new<v8::String>(isolate, "offspring.js");
return v8::Script::Compile(source, filename);
}();
assert(!script.IsEmpty());
/*v8::Handle<v8::Value> result = */script->Run();
if (try_catch.HasCaught()) {
v8::String::Utf8Value message(try_catch.Message()->Get());
std::cerr << std::string(*message, message.length()) << std::endl;
std::abort();
}
}
}
}

View File

@ -160,9 +160,9 @@ void test_setup(v8::Handle<v8::Object> exports)
std::cerr << __LINE__ << std::endl;
efl::eina::js::register_make_value
efl::eina::js::register_value
(isolate, exports
, efl::eina::js::compatibility_new<v8::String>(isolate, "make_value"));
, efl::eina::js::compatibility_new<v8::String>(isolate, "value"));
std::cerr << __LINE__ << std::endl;
}

View File

@ -59,43 +59,66 @@ assert (s2.length == 2);
assert (s2[0] == l1[1]);
assert (s2[1] == l1[2]);
console.log("Finished containers");
// error tests
var captured = false;
console.log("#1");
try {
console.log("#2");
suite.clear_eina_error();
console.log("#3");
} catch(e) {
console.log("#4");
console.log("Exception ", e.toString());
console.log("#5");
captured = true;
}
console.log("#6");
assert(captured === false, 'error #1');
console.log("#7");
captured = false;
try {
console.log("#1");
suite.set_eina_error();
console.log("#2");
} catch(e) {
console.log("#3");
console.log("Exception ", e.toString());
console.log("#4");
assert(e.code === 'Eina_Error', 'error #2');
console.log("#5");
assert(e.value === 'foobar', 'error #3');
console.log("#6");
captured = true;
console.log("#7");
}
console.log("#8");
assert(captured === true, 'error #4');
console.log("Finished errors");
// accessor tests
assert(suite.acc.get(0) === 42, 'accessor #1');
assert(suite.acc.get(1) === 24, 'accessor #2');
console.log("Finished accessors");
// iterator tests
assert(suite.it.next().value === 42, 'iterator #1');
assert(suite.it.next().value === 24, 'iterator #2');
console.log("Finished iterators");
// value tests
console.log("x");
var my_value = new suite.make_value(1);
var my_value = new suite.value(1);
console.log("x");
var wrapped = my_value.get();
console.log("x");
@ -151,14 +174,14 @@ captured = false;
console.log("x");
try {
console.log("x");
my_value = make_value({type: 'complex object'});
my_value = new suite.value({type: 'complex object'});
console.log("x");
} catch(e) {
console.log("e.code ", e.code, ' ', typeof e);
console.log("e.code ", e.code, ' ', typeof e);
assert(e.code === 'std::bad_cast', 'value #11');
console.log("x");
console.log("x");
captured = true;
console.log("x");
console.log("x");
}
assert(captured === true, 'value #12');

View File

@ -1,283 +0,0 @@
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <cassert>
#include <eina_js_value.hh>
#include <Eo.hh>
efl::eina::js::compatibility_return_type print(efl::eina::js::compatibility_callback_info_type args)
{
bool first = true;
for (int i = 0; i < args.Length(); i++) {
efl::eina::js::compatibility_handle_scope handle_scope(args.GetIsolate());
if (first) {
first = false;
} else {
printf(" ");
}
v8::String::Utf8Value str(args[i]);
std::cout << std::string(*str, str.length());
}
printf("\n");
fflush(stdout);
return efl::eina::js::compatibility_return();
}
static const char script[] =
"function assert(test, message) { if (test !== true) throw message; };"
"var my_value = make_value(1);"
"var wrapped = my_value.get();"
"assert(typeof(wrapped) === 'number', '#1');"
"assert(wrapped === 1, '#2');"
"my_value.set(2);"
"assert(wrapped === 1, '#3');"
"wrapped = my_value.get();"
"assert(typeof(wrapped) === 'number', '#4');"
"assert(wrapped === 2, '#5');"
"my_value.set(true);"
"assert(wrapped === 2, '#6');"
"wrapped = my_value.get();"
// boolean is represented as integer in the efl::eina::value layer
"assert(typeof(wrapped) === 'number', '#7');"
"assert(wrapped === 1, '#8');"
"var captured = false;"
"try {"
" my_value.set({type: 'complex object'});"
"} catch(e) {"
" assert(e.code === 'std::bad_cast', '#9');"
" captured = true;"
"}"
"assert(captured === true, '#10');"
"captured = false;"
"try {"
" my_value = make_value({type: 'complex object'});"
"} catch(e) {"
" assert(e.code === 'std::bad_cast', '#11');"
" captured = true;"
"}"
"assert(captured === true, '#12');"
"destroy_value(my_value);"
;
int main(int argc, char *argv[])
{
efl::eina::eina_init eina_init;
efl::eo::eo_init eo_init;
efl::eina::js::compatibility_initialize();
v8::V8::SetFlagsFromCommandLine(&argc, argv, true);
v8::Isolate* isolate = v8::Isolate::New();
v8::Isolate::Scope isolate_scope(isolate);
efl::eina::js::compatibility_handle_scope handle_scope(isolate);
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;
}
{
// Enter the execution environment before evaluating any code.
v8::Context::Scope context_scope(context);
v8::Handle<v8::Object> global = context->Global();
global->Set(efl::eina::js::compatibility_new<v8::String>(isolate, "print"),
efl::eina::js::compatibility_new<v8::FunctionTemplate>(isolate, print)->GetFunction());
efl::eina::js::register_make_value
(isolate, global
, efl::eina::js::compatibility_new<v8::String>(isolate, "make_value"));
efl::eina::js::register_destroy_value
(isolate, global
, efl::eina::js::compatibility_new<v8::String>(isolate, "destroy_value"));
assert(efl::eina::js::value_cast<v8::Local<v8::Value>>
(efl::eina::value(std::numeric_limits<uint64_t>::max()),
isolate)->IsNumber());
assert(efl::eina::js::value_cast<v8::Local<v8::Value>>
(efl::eina::value(std::numeric_limits<uint64_t>::max()),
isolate)->NumberValue()
== double(UINT64_MAX));
assert(efl::eina::js::value_cast<v8::Local<v8::Value>>
(efl::eina::value(std::numeric_limits<unsigned char>::max()),
isolate)->IsUint32());
assert(efl::eina::js::value_cast<v8::Local<v8::Value>>
(efl::eina::value(std::numeric_limits<unsigned char>::max()),
isolate)->Uint32Value() == UINT8_MAX);
if (sizeof(short) > sizeof(int32_t)) {
assert(efl::eina::js::value_cast<v8::Local<v8::Value>>
(efl::eina::value(std::numeric_limits<unsigned short>::max()),
isolate)->IsNumber());
assert(efl::eina::js::value_cast<v8::Local<v8::Value>>
(efl::eina::value(std::numeric_limits<unsigned short>::max()),
isolate)->NumberValue()
== double(std::numeric_limits<unsigned short>::max()));
assert(efl::eina::js::value_cast<v8::Local<v8::Value>>
(efl::eina::value(std::numeric_limits<short>::max()),
isolate)->IsNumber());
assert(efl::eina::js::value_cast<v8::Local<v8::Value>>
(efl::eina::value(std::numeric_limits<short>::max()),
isolate)->NumberValue()
== double(std::numeric_limits<short>::max()));
} else {
assert(efl::eina::js::value_cast<v8::Local<v8::Value>>
(efl::eina::value(std::numeric_limits<unsigned short>::max()),
isolate)->IsUint32());
assert(efl::eina::js::value_cast<v8::Local<v8::Value>>
(efl::eina::value(std::numeric_limits<unsigned short>::max()),
isolate)->Uint32Value()
== std::numeric_limits<unsigned short>::max());
assert(efl::eina::js::value_cast<v8::Local<v8::Value>>
(efl::eina::value(std::numeric_limits<short>::max()),
isolate)->IsInt32());
assert(efl::eina::js::value_cast<v8::Local<v8::Value>>
(efl::eina::value(std::numeric_limits<short>::max()),
isolate)->Int32Value()
== std::numeric_limits<short>::max());
}
if (sizeof(int) > sizeof(int32_t)) {
assert(efl::eina::js::value_cast<v8::Local<v8::Value>>
(efl::eina::value(std::numeric_limits<unsigned int>::max()),
isolate)->IsNumber());
assert(efl::eina::js::value_cast<v8::Local<v8::Value>>
(efl::eina::value(std::numeric_limits<unsigned int>::max()),
isolate)->NumberValue()
== double(std::numeric_limits<unsigned int>::max()));
assert(efl::eina::js::value_cast<v8::Local<v8::Value>>
(efl::eina::value(std::numeric_limits<int>::max()),
isolate)->IsNumber());
assert(efl::eina::js::value_cast<v8::Local<v8::Value>>
(efl::eina::value(std::numeric_limits<int>::max()),
isolate)->NumberValue()
== double(std::numeric_limits<int>::max()));
} else {
assert(efl::eina::js::value_cast<v8::Local<v8::Value>>
(efl::eina::value(std::numeric_limits<unsigned int>::max()),
isolate)->IsUint32());
assert(efl::eina::js::value_cast<v8::Local<v8::Value>>
(efl::eina::value(std::numeric_limits<unsigned int>::max()),
isolate)->Uint32Value()
== std::numeric_limits<unsigned int>::max());
assert(efl::eina::js::value_cast<v8::Local<v8::Value>>
(efl::eina::value(std::numeric_limits<int>::max()),
isolate)->IsInt32());
assert(efl::eina::js::value_cast<v8::Local<v8::Value>>
(efl::eina::value(std::numeric_limits<int>::max()),
isolate)->Int32Value()
== std::numeric_limits<int>::max());
}
if (sizeof(long) > sizeof(int32_t)) {
assert(efl::eina::js::value_cast<v8::Local<v8::Value>>
(efl::eina::value(std::numeric_limits<unsigned long>::max()),
isolate)->IsNumber());
assert(efl::eina::js::value_cast<v8::Local<v8::Value>>
(efl::eina::value(std::numeric_limits<unsigned long>::max()),
isolate)->NumberValue()
== double(std::numeric_limits<unsigned long>::max()));
assert(efl::eina::js::value_cast<v8::Local<v8::Value>>
(efl::eina::value(std::numeric_limits<long>::max()),
isolate)->IsNumber());
assert(efl::eina::js::value_cast<v8::Local<v8::Value>>
(efl::eina::value(std::numeric_limits<long>::max()),
isolate)->NumberValue()
== double(std::numeric_limits<long>::max()));
} else {
assert(efl::eina::js::value_cast<v8::Local<v8::Value>>
(efl::eina::value(std::numeric_limits<unsigned long>::max()),
isolate)->IsUint32());
assert(efl::eina::js::value_cast<v8::Local<v8::Value>>
(efl::eina::value(std::numeric_limits<unsigned long>::max()),
isolate)->Uint32Value()
== std::numeric_limits<unsigned long>::max());
assert(efl::eina::js::value_cast<v8::Local<v8::Value>>
(efl::eina::value(std::numeric_limits<long>::max()),
isolate)->IsInt32());
assert(efl::eina::js::value_cast<v8::Local<v8::Value>>
(efl::eina::value(std::numeric_limits<long>::max()),
isolate)->Int32Value()
== std::numeric_limits<long>::max());
}
assert(efl::eina::js::value_cast<v8::Local<v8::Value>>
(efl::eina::value(std::numeric_limits<float>::max()),
isolate)->IsNumber());
assert(efl::eina::js::value_cast<v8::Local<v8::Value>>
(efl::eina::value(std::numeric_limits<float>::max()),
isolate)->NumberValue()
== double(std::numeric_limits<float>::max()));
assert(efl::eina::js::value_cast<v8::Local<v8::Value>>
(efl::eina::value(42.42), isolate)->IsNumber());
assert(efl::eina::js::value_cast<v8::Local<v8::Value>>
(efl::eina::value(42.42), isolate)->NumberValue()
== 42.42);
assert(efl::eina::get<int>
(efl::eina::js::value_cast<efl::eina::value>
(efl::eina::js::compatibility_new<v8::Boolean>(isolate, true))) == 1);
assert(efl::eina::get<int32_t>
(efl::eina::js::value_cast<efl::eina::value>
(efl::eina::js::compatibility_new<v8::Integer>(isolate, INT32_MAX))) == INT32_MAX);
assert(efl::eina::get<uint32_t>
(efl::eina::js::value_cast<efl::eina::value>
(efl::eina::js::compatibility_new<v8::Integer>(isolate, UINT32_MAX)))
== UINT32_MAX);
assert(efl::eina::get<double>
(efl::eina::js::value_cast<efl::eina::value>
(efl::eina::js::compatibility_new<v8::Number>(isolate,
std::numeric_limits<double>::max())))
== std::numeric_limits<double>::max());
{
const char utf8_data[] = "Matroška";
assert(efl::eina::js::value_cast<efl::eina::value>
(efl::eina::js::compatibility_new<v8::String>(isolate, utf8_data))
== efl::eina::value(std::string(utf8_data)));
assert(efl::eina::js::value_cast<v8::Local<v8::Value>>
(efl::eina::value(std::string(utf8_data)), isolate)
->StrictEquals(efl::eina::js::compatibility_new<v8::String>(isolate, utf8_data)));
#ifndef EINA_JS_TEST_SKIP_STRINGSHARE
assert(efl::eina::js::value_cast<v8::Local<v8::Value>>
(efl::eina::value(efl::eina::stringshare(utf8_data)), isolate)
->StrictEquals(efl::eina::js::compatibility_new<v8::String>(isolate, utf8_data)));
#endif // EINA_JS_TEST_SKIP_STRINGSHARE
}
{
efl::eina::js::compatibility_handle_scope handle_scope(isolate);
v8::TryCatch try_catch;
auto source = efl::eina::js::compatibility_new<v8::String>(isolate, script);
v8::Handle<v8::Script> script = v8::Script::Compile(std::move(source));
assert(!script.IsEmpty());
/*v8::Handle<v8::Value> result = */script->Run();
if (try_catch.HasCaught()) {
v8::String::Utf8Value message(try_catch.Message()->Get());
std::cerr << std::string(*message, message.length()) << std::endl;
std::abort();
}
}
}
}