Skip to content

Commit

Permalink
Generate JSTypeTraits by macro functions (#123)
Browse files Browse the repository at this point in the history
Generate JSTypeTraits by macro functions

In the future, we can auto-generate js_type_traits during bacardi build
time by using some information of types with template.

e.g
types = {
  'Number': {
    int32_t,
    int64_t,
    float,
    long
    .
    .
  },
  'String': {
     char*,
     std::string,
     .
   }
  
}
And this is the just pseudo code for concept.

if type in types['Number']:
   generate("JS_TYPE_TRAITS_NUMBER("+type+")");

if type in types['String']:
   generate("JS_TYPE_TRAITS_STRING("+type+")");

ISSUE=#120
  • Loading branch information
corona10 authored and romandev committed Oct 18, 2017
1 parent 0cc9490 commit f1d03a3
Showing 1 changed file with 28 additions and 29 deletions.
57 changes: 28 additions & 29 deletions core/js_type_traits.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,34 +25,33 @@ inline Napi::Value JSTypeTraits(Napi::Env env, T value) {
return T();
}

template <>
inline Napi::Value JSTypeTraits(Napi::Env env, bool value) {
return Napi::Boolean::New(env, value);
}

template <>
inline Napi::Value JSTypeTraits(Napi::Env env, double value) {
return Napi::Number::New(env, value);
}

template <>
inline Napi::Value JSTypeTraits(Napi::Env env, int16_t value) {
return Napi::Number::New(env, value);
}

template <>
inline Napi::Value JSTypeTraits(Napi::Env env, int32_t value) {
return Napi::Number::New(env, value);
}

template <>
inline Napi::Value JSTypeTraits(Napi::Env env, int64_t value) {
return Napi::Number::New(env, value);
}

template <>
inline Napi::Value JSTypeTraits(Napi::Env env, const std::string value) {
return Napi::String::New(env, value);
}
#define JS_TYPE_TRAITS_NUMBER(type) \
template <> \
inline Napi::Value JSTypeTraits(Napi::Env env, type value) { \
return Napi::Number::New(env, value); \
}

#define JS_TYPE_TRAITS_BOOLEAN(type) \
template <> \
inline Napi::Value JSTypeTraits(Napi::Env env, type value) { \
return Napi::Boolean::New(env, value); \
}

#define JS_TYPE_TRAITS_STRING(type) \
template <> \
inline Napi::Value JSTypeTraits(Napi::Env env, type value) { \
return Napi::String::New(env, value); \
}

// TODO(corona10): Auto generate JS_TYPE_TRAITS_XXXX.

JS_TYPE_TRAITS_NUMBER(int16_t);
JS_TYPE_TRAITS_NUMBER(int32_t);
JS_TYPE_TRAITS_NUMBER(int64_t);
JS_TYPE_TRAITS_NUMBER(double);

JS_TYPE_TRAITS_BOOLEAN(bool);

JS_TYPE_TRAITS_STRING(std::string);

#endif // CORE_JS_TYPE_TRAITS_H_

0 comments on commit f1d03a3

Please sign in to comment.