From 4df4fd8309d02e165f99a458ccba98604f59e27f Mon Sep 17 00:00:00 2001 From: Dong-hee Na Date: Mon, 9 Oct 2017 00:48:55 +0900 Subject: [PATCH] Generate JSTypeTraits automatically. By doing this we can easily generate various basic types. ISSUE=#125, #120 --- binding.gyp | 1 + generator/main.ts | 12 ++++++++++ generator/types/js_type_traits.ts | 18 +++++++++++++++ template/interface_cpp.njk | 2 +- .../jstypes_traits.njk | 23 ++++++++++--------- 5 files changed, 44 insertions(+), 12 deletions(-) create mode 100644 generator/types/js_type_traits.ts rename core/js_type_traits.h => template/jstypes_traits.njk (81%) diff --git a/binding.gyp b/binding.gyp index 58be9d0..29b26db 100644 --- a/binding.gyp +++ b/binding.gyp @@ -57,6 +57,7 @@ '<@(test_cpp_files)', '<@(test_idl_output_files)', '<(SHARED_INTERMEDIATE_DIR)/bacardi.cc', + '<(SHARED_INTERMEDIATE_DIR)/js_type_traits.h' ], 'defines': [ 'NAPI_DISABLE_CPP_EXCEPTIONS' ], }, diff --git a/generator/main.ts b/generator/main.ts index 213aec2..9fc5794 100644 --- a/generator/main.ts +++ b/generator/main.ts @@ -25,6 +25,7 @@ import snakeCase = require('snake-case'); import IDLDefinition from './parser/idl_definition'; import Parser from './parser/parser'; +import {JsTypeInfos} from './types/js_type_traits'; const TEMPLATE_DIR = path.resolve(__dirname, '../../../template'); @@ -46,6 +47,15 @@ async function generateBacardi( cpp_file_path, env.renderString(cpp_tmpl, {interfaces: idl_interfaces})); } +async function generateJsTypeTraits( + env: nunjucks.Environment, output_path: string) { + const js_type_tmpl = + await file.read(path.resolve(TEMPLATE_DIR, 'jstypes_traits.njk')); + const js_type_file_path = path.resolve(output_path, 'js_type_traits.h'); + await file.write( + js_type_file_path, env.renderString(js_type_tmpl, {types: JsTypeInfos})); +} + async function generateInterface( env: nunjucks.Environment, output_path: string, definitions: IDLDefinition[]) { @@ -94,6 +104,8 @@ async function main([root_dir, out_dir, ...idl_files]) { let definitions: IDLDefinition[] = await Parser.parse(await reader.readAll(relative_idl_files)); + + await generateJsTypeTraits(env, out_dir); await generateInterface(env, out_dir, definitions); await generateBacardi(env, out_dir, definitions); diff --git a/generator/types/js_type_traits.ts b/generator/types/js_type_traits.ts new file mode 100644 index 0000000..3364e9c --- /dev/null +++ b/generator/types/js_type_traits.ts @@ -0,0 +1,18 @@ +/* + * Copyright (c) 2017 The Bacardi Authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +export const JsTypeInfos = { 'number': ['int16_t', 'int32_t', 'int64_t', 'double', 'long'], + 'string': ['std::string'], 'boolean': ['bool'] }; diff --git a/template/interface_cpp.njk b/template/interface_cpp.njk index 33caa82..bfa60d4 100644 --- a/template/interface_cpp.njk +++ b/template/interface_cpp.njk @@ -15,8 +15,8 @@ */ #include "{{idl_dir_name}}/{{name | snakecase}}_bridge.h" +#include "js_type_traits.h" -#include "core/js_type_traits.h" #include "core/native_type_traits.h" void {{name}}Bridge::Init(Napi::Env env, Napi::Object exports) { diff --git a/core/js_type_traits.h b/template/jstypes_traits.njk similarity index 81% rename from core/js_type_traits.h rename to template/jstypes_traits.njk index 11fd95b..d4808d1 100644 --- a/core/js_type_traits.h +++ b/template/jstypes_traits.njk @@ -14,8 +14,8 @@ * limitations under the License. */ -#ifndef CORE_JS_TYPE_TRAITS_H_ -#define CORE_JS_TYPE_TRAITS_H_ +#ifndef JS_TYPE_TRAITS_H_ +#define JS_TYPE_TRAITS_H_ #include @@ -43,15 +43,16 @@ inline Napi::Value JSTypeTraits(Napi::Env env, T value) { return Napi::String::New(env, value); \ } -// TODO(corona10): Auto generate JS_TYPE_TRAITS_XXXX. +{% for type in types['number'] %} +JS_TYPE_TRAITS_NUMBER({{type}}); +{% endfor %} -JS_TYPE_TRAITS_NUMBER(int16_t); -JS_TYPE_TRAITS_NUMBER(int32_t); -JS_TYPE_TRAITS_NUMBER(int64_t); -JS_TYPE_TRAITS_NUMBER(double); +{% for type in types['string'] %} +JS_TYPE_TRAITS_STRING({{type}}); +{% endfor %} -JS_TYPE_TRAITS_BOOLEAN(bool); +{% for type in types['boolean'] %} +JS_TYPE_TRAITS_BOOLEAN({{type}}); +{% endfor %} -JS_TYPE_TRAITS_STRING(std::string); - -#endif // CORE_JS_TYPE_TRAITS_H_ +#endif // JS_TYPE_TRAITS_H_