From 0190d3baa9bbfc28406b58ae1c1b2c54da5963e6 Mon Sep 17 00:00:00 2001 From: Dong-hee Na Date: Sun, 8 Oct 2017 23:43:13 +0900 Subject: [PATCH] Generate JSTypeTraits by macro functions (#123) 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 --- core/js_type_traits.h | 57 +++++++++++++++++++++---------------------- 1 file changed, 28 insertions(+), 29 deletions(-) diff --git a/core/js_type_traits.h b/core/js_type_traits.h index f307062..11fd95b 100644 --- a/core/js_type_traits.h +++ b/core/js_type_traits.h @@ -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_