summaryrefslogtreecommitdiff
path: root/src/bindings/eo_js/eo_js_get_value.hh
blob: 10835809158fb51a7254a88d2b3f110949d135a2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#ifndef EFL_EO_JS_GET_VALUE_HH
#define EFL_EO_JS_GET_VALUE_HH

#include <eina_js_compatibility.hh>

#include V8_INCLUDE_HEADER

#include <type_traits>
#include <cstdlib>
#include <iostream>
#include <typeinfo>

namespace efl { namespace eo { namespace js {

template <typename T>
struct value_tag
{
  typedef T type;
};

template <typename T>
inline int get_value_from_javascript
  (v8::Local<v8::Value> v
   , v8::Isolate* isolate
   , value_tag<T>
   , typename std::enable_if<(std::is_integral<T>::value && !std::is_same<T, Eina_Bool>::value)>::type* = 0)
{
  if(v->IsInt32())
    return v->Int32Value();
  else if(v->IsUint32())
    return v->Uint32Value();
  else
    {
      compatibility_throw
        (isolate, v8::Exception::TypeError
         (compatibility_new<v8::String>(isolate, "Type expected is different. Expected Integral type")));

      throw std::logic_error("");
    }
  return 0;
}

inline char* get_value_from_javascript
  (v8::Local<v8::Value> v
   , v8::Isolate* isolate
   , value_tag<char*>)
{
  if(v->IsNull())
    return nullptr;
  else if(v->IsString())
    {
      v8::String::Utf8Value str(v->ToString());
      char* string = strdup(*str); // TODO: leaks
      std::cerr << "String " << string << std::endl;
      return string;
    }
  else
    {
      compatibility_throw
        (isolate, v8::Exception::TypeError
         (compatibility_new<v8::String>(isolate, "Type expected is different. Expected Integral type")));

      throw std::logic_error("");
    }
  return 0;
}
   
inline const char* get_value_from_javascript
  (v8::Local<v8::Value> v
   , v8::Isolate* isolate
   , value_tag<const char*>)
{
  return get_value_from_javascript(v, isolate, value_tag<char*>());
}

inline Eo* get_value_from_javascript
  (v8::Local<v8::Value> v
   , v8::Isolate* isolate
   , value_tag<Eo*>)
{
  if(v->IsNull())
    return nullptr;
  else if(v->IsObject())
    {
      v8::Local<v8::Object> object = v->ToObject();
      if(object->InternalFieldCount() == 1)
        {
          v8::Local<v8::Value> r = object->GetInternalField(0);
          if(v8::External* external = v8::External::Cast(*r))
            {
              return static_cast<Eo*>(external->Value());
            }
        }
    }
  compatibility_throw
    (isolate, v8::Exception::TypeError
     (compatibility_new<v8::String>(isolate, "Type expected is different. Expected floating point type")));
  throw std::logic_error("");
  return nullptr;
}

template <typename T>
inline T get_value_from_javascript
  (v8::Local<v8::Value> v
   , v8::Isolate* isolate
   , value_tag<T>
   , typename std::enable_if<std::is_enum<T>::value>::type* = 0)
{
  if(v->IsInt32())
    return static_cast<T>(v->Int32Value());
  else if(v->IsUint32())
    return static_cast<T>(v->Uint32Value());
  else
    {
      compatibility_throw
        (isolate, v8::Exception::TypeError
         (compatibility_new<v8::String>(isolate, "Type expected is different. Expected Integral type")));

      throw std::logic_error("");
    }
  return T();
}
      
inline int get_value_from_javascript
  (v8::Local<v8::Value> v
   , v8::Isolate* isolate
   , value_tag<Eina_Bool>)
{
  if(v->IsBoolean() || v->IsBooleanObject())
    {
      return v->BooleanValue();
    }
  else
    {
      compatibility_throw
        (isolate, v8::Exception::TypeError
         (compatibility_new<v8::String>(isolate, "Type expected is different. Expected Boolean type")));

      throw std::logic_error("");
    }
  return 0;
}

template <typename T>
inline double get_value_from_javascript
  (v8::Local<v8::Value> v
   , v8::Isolate* isolate
   , value_tag<T>
   , typename std::enable_if<std::is_floating_point<T>::value>::type* = 0)
{
  if(v->IsNumber())
    {
      return v->NumberValue();
    }
  else
    {
      compatibility_throw
        (isolate, v8::Exception::TypeError
         (compatibility_new<v8::String>(isolate, "Type expected is different. Expected floating point type")));
      throw std::logic_error("");
    }
  return 0.0;
}

template <typename T>
inline T get_value_from_javascript
  (v8::Local<v8::Value>, v8::Isolate* isolate, value_tag<T>
   , typename std::enable_if<!std::is_floating_point<T>::value && !std::is_integral<T>::value
   && !std::is_enum<T>::value>::type* = 0)
{
  std::cerr << "Trying to convert to " << typeid(T).name() << " to call a C function" << std::endl;
  compatibility_throw
    (isolate, v8::Exception::TypeError
     (compatibility_new<v8::String>(isolate, "Not implemented yet")));
  throw std::logic_error("");
}
      
} } }

#endif