From b9de27439a6f060cb9373f4b5500f4ed98e58e2a Mon Sep 17 00:00:00 2001 From: Hwanseung Lee Date: Sun, 8 Oct 2017 20:45:26 +0900 Subject: [PATCH 1/5] auto genereate --- binding.gyp | 2 + core/idl_types.h | 3 -- core/native_type_traits.h | 77 ---------------------------- generator/main.ts | 26 ++++++++++ template/idl_enum_type.njk | 26 ++++++++++ template/interface_cpp.njk | 1 + template/native_enum_type_traits.njk | 73 ++++++++++++++++++++++++++ 7 files changed, 128 insertions(+), 80 deletions(-) create mode 100644 template/idl_enum_type.njk create mode 100644 template/native_enum_type_traits.njk diff --git a/binding.gyp b/binding.gyp index 89e4573..ce87fd1 100644 --- a/binding.gyp +++ b/binding.gyp @@ -60,6 +60,8 @@ '<@(test_cpp_files)', '<@(test_idl_output_files)', '<(SHARED_INTERMEDIATE_DIR)/bacardi.cc', + '<(SHARED_INTERMEDIATE_DIR)/idl_enum_type.h', + '<(SHARED_INTERMEDIATE_DIR)/native_enum_type_traits.h', ], 'defines': [ 'NAPI_DISABLE_CPP_EXCEPTIONS' ], }, diff --git a/core/idl_types.h b/core/idl_types.h index f435a63..b2b23da 100644 --- a/core/idl_types.h +++ b/core/idl_types.h @@ -25,8 +25,5 @@ struct IDLLongLong final : public IDLBaseHelper {}; struct IDLLong final : public IDLBaseHelper {}; struct IDLShort final : public IDLBaseHelper {}; struct IDLString final : public IDLBaseHelper {}; -// FIXME(Hwansung): should be generated automatically in another file. -struct IDLOperationType final : public IDLBaseHelper {}; -struct IDLTestEnum final : public IDLBaseHelper {}; #endif // CORE_IDL_TYPES_H_ diff --git a/core/native_type_traits.h b/core/native_type_traits.h index 1a77ff7..6635959 100644 --- a/core/native_type_traits.h +++ b/core/native_type_traits.h @@ -160,81 +160,4 @@ struct NativeTypeTraits : public NativeTypeTraitsBase { } }; -// FIXME(Hwansung): should be generated automatically in another file. -template <> -struct NativeTypeTraits - : public NativeTypeTraitsBase { - static std::string NativeValue(const Napi::Env& env, - const Napi::Value& js_value) { - if (!js_value.IsString()) { - Napi::TypeError::New(env, "It's an invalid string.") - .ThrowAsJavaScriptException(); - return std::string(); - } - - std::string value = js_value.ToString().Utf8Value(); - if (!IsValidValue(value)) { - Napi::TypeError::New(env, "it not matched with values of enum in idl.") - .ThrowAsJavaScriptException(); - return std::string(); - } - - return js_value.ToString().Utf8Value(); - } - - static bool IsTypeEquals(const Napi::Value& js_value) { - if (js_value.IsString()) { - std::string value = js_value.ToString().Utf8Value(); - return IsValidValue(value); - } - return false; - } - - static bool IsValidValue(std::string value) { - if (value.compare("add") == 0 || value.compare("sub") == 0 || - value.compare("mul") == 0 || value.compare("div") == 0) { - return true; - } - return false; - } -}; - -template <> -struct NativeTypeTraits - : public NativeTypeTraitsBase { - static std::string NativeValue(const Napi::Env& env, - const Napi::Value& js_value) { - if (!js_value.IsString()) { - Napi::TypeError::New(env, "It's an invalid string.") - .ThrowAsJavaScriptException(); - return std::string(); - } - - std::string value = js_value.ToString().Utf8Value(); - if (!IsValidValue(value)) { - Napi::TypeError::New(env, "it not matched with values of enum in idl.") - .ThrowAsJavaScriptException(); - return std::string(); - } - - return js_value.ToString().Utf8Value(); - } - - static bool IsTypeEquals(const Napi::Value& js_value) { - if (js_value.IsString()) { - std::string value = js_value.ToString().Utf8Value(); - return IsValidValue(value); - } - return false; - } - - static bool IsValidValue(std::string value) { - if (value.compare("value1") == 0 || value.compare("value2") == 0 || - value.compare("value3") == 0) { - return true; - } - return false; - } -}; - #endif // CORE_NATIVE_TYPE_TRAITS_H_ diff --git a/generator/main.ts b/generator/main.ts index 7fe934c..15304c3 100644 --- a/generator/main.ts +++ b/generator/main.ts @@ -72,6 +72,31 @@ async function generateInterface( }); } +async function generateEnumTypeTraits( + env: nunjucks.Environment, output_path: string, + definitions: IDLDefinition[]) { + const [enum_type_tmpl, traits_tmpl] = await Promise.all([ + file.read(path.resolve(TEMPLATE_DIR, 'idl_enum_type.njk')), + file.read(path.resolve(TEMPLATE_DIR, 'native_enum_type_traits.njk')) + ]); + + const enum_type_file_path = path.resolve(output_path, 'idl_enum_type.h'); + const type_traits_file_path = + path.resolve(output_path, 'native_enum_type_traits.h'); + + let idl_enum: IDLDefinition[] = []; + definitions.forEach(async (definition) => { + if (definition.isIDLEnum()) { + idl_enum.push(definition); + } + }); + + await file.write( + enum_type_file_path, env.renderString(enum_type_tmpl, {enums: idl_enum})); + await file.write( + type_traits_file_path, env.renderString(traits_tmpl, {enums: idl_enum})); +} + async function main([root_dir, out_dir, ...idl_files]) { // We expect that current working directory will be $BACARDI_PATH. But it // might not be in Windows platform. So, we should resolve the path here. @@ -96,6 +121,7 @@ async function main([root_dir, out_dir, ...idl_files]) { await Parser.parse(await reader.readAll(relative_idl_files)); await generateInterface(env, out_dir, definitions); await generateBacardi(env, out_dir, definitions); + await generateEnumTypeTraits(env, out_dir, definitions); return 0; } diff --git a/template/idl_enum_type.njk b/template/idl_enum_type.njk new file mode 100644 index 0000000..e553859 --- /dev/null +++ b/template/idl_enum_type.njk @@ -0,0 +1,26 @@ +/* + * 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. + */ + +#ifndef GEN_IDL_ENUM_TYPES_H_ +#define GEN_IDL_ENUM_TYPES_H_ + +#include "core/idl_base.h" + +{% for enum in enums %} +struct IDL{{enum.name}} final : public IDLBaseHelper {}; +{% endfor %} + +#endif // GEN_IDL_ENUM_TYPES_H_ \ No newline at end of file diff --git a/template/interface_cpp.njk b/template/interface_cpp.njk index b450e23..c754b35 100644 --- a/template/interface_cpp.njk +++ b/template/interface_cpp.njk @@ -18,6 +18,7 @@ #include "core/js_type_traits.h" #include "core/native_type_traits.h" +#include "native_enum_type_traits.h" void {{name}}Bridge::Init(Napi::Env env, Napi::Object exports) { Napi::Function js_constructor = diff --git a/template/native_enum_type_traits.njk b/template/native_enum_type_traits.njk new file mode 100644 index 0000000..170c5d9 --- /dev/null +++ b/template/native_enum_type_traits.njk @@ -0,0 +1,73 @@ +/* + * 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. + */ + +#ifndef GEN_NATIVE_ENUM_TYPE_TRAITS_H_ +#define GEN_NATIVE_ENUM_TYPE_TRAITS_H_ + +#include +#include +#include +#include "core/native_type_traits.h" +#include "idl_enum_type.h" + +{% for enum in enums %} +template <> +struct NativeTypeTraits + : public NativeTypeTraitsBase { + static std::string NativeValue(const Napi::Env& env, + const Napi::Value& js_value) { + if (!js_value.IsString()) { + Napi::TypeError::New(env, "It's an invalid string.") + .ThrowAsJavaScriptException(); + return std::string(); + } + + std::string value = js_value.ToString().Utf8Value(); + if (!IsValidValue(value)) { + Napi::TypeError::New(env, "it not matched with values of enum in idl.") + .ThrowAsJavaScriptException(); + return std::string(); + } + + return js_value.ToString().Utf8Value(); + } + + static bool IsTypeEquals(const Napi::Value& js_value) { + if (js_value.IsString()) { + std::string value = js_value.ToString().Utf8Value(); + return IsValidValue(value); + } + return false; + } + + static bool IsValidValue(std::string value) { + if ( + {% for value in enum.values %} + {% if loop.last %} + value.compare("{{value}}") == 0 + {% else %} + value.compare("{{value}}") == 0 || + {% endif %} + {% endfor %} + ) { + return true; + } + return false; + } +}; +{% endfor %} + +#endif // CORE_NATIVE_TYPE_TRAITS_H_ From d4458b6c0421477efe038bbdd2fac962a6289ca0 Mon Sep 17 00:00:00 2001 From: Hwanseung Lee Date: Tue, 10 Oct 2017 21:32:11 +0900 Subject: [PATCH 2/5] change --- binding.gyp | 4 +- core/core.gypi | 1 + core/enum_validator.h | 34 +++++++++ generator/main.ts | 42 ++++++++--- generator/parser/enum_types.ts | 39 ++++++++++ generator/parser/idl_interface.ts | 3 + .../{idl_enum_type.njk => enum_types_cpp.njk} | 14 ++-- template/enum_types_header.njk | 30 ++++++++ template/interface_cpp.njk | 13 +++- template/native_enum_type_traits.njk | 73 ------------------- 10 files changed, 160 insertions(+), 93 deletions(-) create mode 100644 core/enum_validator.h create mode 100644 generator/parser/enum_types.ts rename template/{idl_enum_type.njk => enum_types_cpp.njk} (77%) create mode 100644 template/enum_types_header.njk delete mode 100644 template/native_enum_type_traits.njk diff --git a/binding.gyp b/binding.gyp index ce87fd1..b369f82 100644 --- a/binding.gyp +++ b/binding.gyp @@ -60,8 +60,8 @@ '<@(test_cpp_files)', '<@(test_idl_output_files)', '<(SHARED_INTERMEDIATE_DIR)/bacardi.cc', - '<(SHARED_INTERMEDIATE_DIR)/idl_enum_type.h', - '<(SHARED_INTERMEDIATE_DIR)/native_enum_type_traits.h', + '<(SHARED_INTERMEDIATE_DIR)/enum_types.h', + '<(SHARED_INTERMEDIATE_DIR)/enum_types.cpp', ], 'defines': [ 'NAPI_DISABLE_CPP_EXCEPTIONS' ], }, diff --git a/core/core.gypi b/core/core.gypi index 669d5f5..db55004 100644 --- a/core/core.gypi +++ b/core/core.gypi @@ -15,6 +15,7 @@ { 'variables': { 'core_cpp_files': [ + 'enum_validator.h' 'idl_base.h', 'idl_types.h', 'js_type_traits.h', diff --git a/core/enum_validator.h b/core/enum_validator.h new file mode 100644 index 0000000..b6cec22 --- /dev/null +++ b/core/enum_validator.h @@ -0,0 +1,34 @@ +/* + * 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. + */ + +#ifndef CORE_ENUM_VALIDATOR_H_ +#define CORE_ENUM_VALIDATOR_H_ + +#include +#include + +class EnumValidator { + public: + static bool isValildEnum(const std::string value, + const std::set enum_values) { + if (enum_values.count(value) > 0) { + return true; + } + return false; + } +}; + +#endif // CORE_ENUM_VALIDATOR_H_ diff --git a/generator/main.ts b/generator/main.ts index 15304c3..906a9ce 100644 --- a/generator/main.ts +++ b/generator/main.ts @@ -23,7 +23,9 @@ import * as reader from './reader/simple_reader'; import snakeCase = require('snake-case'); +import EnumTypes from './parser/enum_types'; import IDLDefinition from './parser/idl_definition'; +import IDLInterface from './parser/idl_interface'; import Parser from './parser/parser'; const TEMPLATE_DIR = path.resolve(__dirname, '../../../template'); @@ -72,17 +74,16 @@ async function generateInterface( }); } -async function generateEnumTypeTraits( +async function generateEnumTypes( env: nunjucks.Environment, output_path: string, definitions: IDLDefinition[]) { - const [enum_type_tmpl, traits_tmpl] = await Promise.all([ - file.read(path.resolve(TEMPLATE_DIR, 'idl_enum_type.njk')), - file.read(path.resolve(TEMPLATE_DIR, 'native_enum_type_traits.njk')) + const [enum_type_header_tmpl, enum_type_cpp_tmpl] = await Promise.all([ + file.read(path.resolve(TEMPLATE_DIR, 'enum_types_header.njk')), + file.read(path.resolve(TEMPLATE_DIR, 'enum_types_cpp.njk')), ]); - const enum_type_file_path = path.resolve(output_path, 'idl_enum_type.h'); - const type_traits_file_path = - path.resolve(output_path, 'native_enum_type_traits.h'); + const enum_type_header_path = path.resolve(output_path, 'enum_types.h'); + const enum_type_cpp_path = path.resolve(output_path, 'enum_types.cpp'); let idl_enum: IDLDefinition[] = []; definitions.forEach(async (definition) => { @@ -92,9 +93,29 @@ async function generateEnumTypeTraits( }); await file.write( - enum_type_file_path, env.renderString(enum_type_tmpl, {enums: idl_enum})); + enum_type_header_path, + env.renderString(enum_type_header_tmpl, {enums: idl_enum})); await file.write( - type_traits_file_path, env.renderString(traits_tmpl, {enums: idl_enum})); + enum_type_cpp_path, + env.renderString(enum_type_cpp_tmpl, {enums: idl_enum})); +} + +// TODO(hwansueng) :: this function should be improved. +async function postProcessing(definitions: IDLDefinition[]) { + let enum_types: EnumTypes = new EnumTypes(definitions); + + for (const definition of definitions) { + if (definition.isIDLInterface()) { + const idl_interface: IDLInterface = definition as IDLInterface; + for (const member of idl_interface.members) { + for (let args of member.arguments) { + if (enum_types.isEnumType(args.type)) { + args.is_enum_type = true; + } + } + } + } + } } async function main([root_dir, out_dir, ...idl_files]) { @@ -119,9 +140,10 @@ async function main([root_dir, out_dir, ...idl_files]) { let definitions: IDLDefinition[] = await Parser.parse(await reader.readAll(relative_idl_files)); + await postProcessing(definitions); await generateInterface(env, out_dir, definitions); await generateBacardi(env, out_dir, definitions); - await generateEnumTypeTraits(env, out_dir, definitions); + await generateEnumTypes(env, out_dir, definitions); return 0; } diff --git a/generator/parser/enum_types.ts b/generator/parser/enum_types.ts new file mode 100644 index 0000000..0b0a42d --- /dev/null +++ b/generator/parser/enum_types.ts @@ -0,0 +1,39 @@ +/** + * 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. + */ + +import IDLDefinition from './idl_definition'; + +export default class EnumTypes { + types: string[]; + + constructor(definitions: IDLDefinition[]) { + this.types = []; + definitions.forEach((definition) => { + if (definition.isIDLEnum()) { + this.types.push(definition.name); + } + }); + } + + public isEnumType(source: string): boolean { + for (const type of this.types) { + if (type == source) { + return true; + } + } + return false; + } +} diff --git a/generator/parser/idl_interface.ts b/generator/parser/idl_interface.ts index 6906b80..70d3d90 100644 --- a/generator/parser/idl_interface.ts +++ b/generator/parser/idl_interface.ts @@ -22,10 +22,13 @@ import IDLIdentifier from './idl_identifier'; class Argument implements IDLIdentifier { readonly type: string; readonly name: string; + // TODO(hwansueng) :: this attribute is temporary to distinguish enum type. + is_enum_type: boolean; constructor(raw_argument_info: {}) { this.type = raw_argument_info['idlType']['idlType']; this.name = raw_argument_info['name']; + this.is_enum_type = false; } } diff --git a/template/idl_enum_type.njk b/template/enum_types_cpp.njk similarity index 77% rename from template/idl_enum_type.njk rename to template/enum_types_cpp.njk index e553859..06c0051 100644 --- a/template/idl_enum_type.njk +++ b/template/enum_types_cpp.njk @@ -14,13 +14,13 @@ * limitations under the License. */ -#ifndef GEN_IDL_ENUM_TYPES_H_ -#define GEN_IDL_ENUM_TYPES_H_ - -#include "core/idl_base.h" +#include "./enum_types.h" {% for enum in enums %} -struct IDL{{enum.name}} final : public IDLBaseHelper {}; -{% endfor %} +const std::set EnumTypes::{{enum.name | snakecase}}_enum_set = { + {% for value in enum.values %} + "{{value}}", + {% endfor %} + }; -#endif // GEN_IDL_ENUM_TYPES_H_ \ No newline at end of file +{% endfor %} diff --git a/template/enum_types_header.njk b/template/enum_types_header.njk new file mode 100644 index 0000000..dd9230f --- /dev/null +++ b/template/enum_types_header.njk @@ -0,0 +1,30 @@ +/* + * 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. + */ + +#ifndef GEN_ENUM_TYPES_H_ +#define GEN_ENUM_TYPES_H_ + +#include +#include + +class EnumTypes { +public: +{% for enum in enums %} + const static std::set {{enum.name | snakecase}}_enum_set; +{% endfor %} +}; + +#endif // GEN_ENUM_TYPES_H_ \ No newline at end of file diff --git a/template/interface_cpp.njk b/template/interface_cpp.njk index c754b35..a3dffcb 100644 --- a/template/interface_cpp.njk +++ b/template/interface_cpp.njk @@ -18,7 +18,8 @@ #include "core/js_type_traits.h" #include "core/native_type_traits.h" -#include "native_enum_type_traits.h" +#include "core/enum_validator.h" +#include "enum_types.h" void {{name}}Bridge::Init(Napi::Env env, Napi::Object exports) { Napi::Function js_constructor = @@ -107,11 +108,21 @@ Napi::Value {{name}}Bridge::{{member.name | camelcase}}(const Napi::CallbackInfo } {% for argument in member.arguments %} + {% if argument.is_enum_type == true %} + auto {{argument.name}} = NativeTypeTraits::NativeValue(info.Env(), info[{{loop.index0}}]); + if (!EnumValidator::isValildEnum({{argument.name}}, EnumTypes::{{argument.type | snakecase}}_enum_set)) { + Napi::TypeError::New(info.Env(), "it not matched with values of enum in idl.") + .ThrowAsJavaScriptException(); + return Napi::Value(); + } + {% else %} auto {{argument.name}} = NativeTypeTraits::NativeValue(info.Env(), info[{{loop.index0}}]); if (info.Env().IsExceptionPending()) { return Napi::Value(); } + {% endif %} {% endfor %} {% if member.type != "void" %} diff --git a/template/native_enum_type_traits.njk b/template/native_enum_type_traits.njk deleted file mode 100644 index 170c5d9..0000000 --- a/template/native_enum_type_traits.njk +++ /dev/null @@ -1,73 +0,0 @@ -/* - * 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. - */ - -#ifndef GEN_NATIVE_ENUM_TYPE_TRAITS_H_ -#define GEN_NATIVE_ENUM_TYPE_TRAITS_H_ - -#include -#include -#include -#include "core/native_type_traits.h" -#include "idl_enum_type.h" - -{% for enum in enums %} -template <> -struct NativeTypeTraits - : public NativeTypeTraitsBase { - static std::string NativeValue(const Napi::Env& env, - const Napi::Value& js_value) { - if (!js_value.IsString()) { - Napi::TypeError::New(env, "It's an invalid string.") - .ThrowAsJavaScriptException(); - return std::string(); - } - - std::string value = js_value.ToString().Utf8Value(); - if (!IsValidValue(value)) { - Napi::TypeError::New(env, "it not matched with values of enum in idl.") - .ThrowAsJavaScriptException(); - return std::string(); - } - - return js_value.ToString().Utf8Value(); - } - - static bool IsTypeEquals(const Napi::Value& js_value) { - if (js_value.IsString()) { - std::string value = js_value.ToString().Utf8Value(); - return IsValidValue(value); - } - return false; - } - - static bool IsValidValue(std::string value) { - if ( - {% for value in enum.values %} - {% if loop.last %} - value.compare("{{value}}") == 0 - {% else %} - value.compare("{{value}}") == 0 || - {% endif %} - {% endfor %} - ) { - return true; - } - return false; - } -}; -{% endfor %} - -#endif // CORE_NATIVE_TYPE_TRAITS_H_ From ec8cdfb93ce5c70a4f0ff6f2125891d7bad44804 Mon Sep 17 00:00:00 2001 From: Hwanseung Lee Date: Sun, 22 Oct 2017 16:10:56 +0900 Subject: [PATCH 3/5] replace "/*" with "/**" in in boilerplate --- core/enum_validator.h | 2 +- template/enum_types_cpp.njk | 2 +- template/enum_types_header.njk | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/core/enum_validator.h b/core/enum_validator.h index b6cec22..3af4b19 100644 --- a/core/enum_validator.h +++ b/core/enum_validator.h @@ -1,4 +1,4 @@ -/* +/** * Copyright (c) 2017 The Bacardi Authors. * * Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/template/enum_types_cpp.njk b/template/enum_types_cpp.njk index 06c0051..f9250e1 100644 --- a/template/enum_types_cpp.njk +++ b/template/enum_types_cpp.njk @@ -1,4 +1,4 @@ -/* +/** * Copyright (c) 2017 The Bacardi Authors. * * Licensed under the Apache License, Version 2.0 (the "License"); diff --git a/template/enum_types_header.njk b/template/enum_types_header.njk index dd9230f..36709ea 100644 --- a/template/enum_types_header.njk +++ b/template/enum_types_header.njk @@ -1,4 +1,4 @@ -/* +/** * Copyright (c) 2017 The Bacardi Authors. * * Licensed under the Apache License, Version 2.0 (the "License"); From d39cd402987493b8e704b16702076e5c677a2f75 Mon Sep 17 00:00:00 2001 From: Hwanseung Lee Date: Fri, 27 Oct 2017 23:49:52 +0900 Subject: [PATCH 4/5] fix compile error. --- generator/main.ts | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/generator/main.ts b/generator/main.ts index 906a9ce..67f30b6 100644 --- a/generator/main.ts +++ b/generator/main.ts @@ -108,9 +108,11 @@ async function postProcessing(definitions: IDLDefinition[]) { if (definition.isIDLInterface()) { const idl_interface: IDLInterface = definition as IDLInterface; for (const member of idl_interface.members) { - for (let args of member.arguments) { - if (enum_types.isEnumType(args.type)) { - args.is_enum_type = true; + if (member.arguments != null) { + for (let args of member.arguments) { + if (enum_types.isEnumType(args.type)) { + args.is_enum_type = true; + } } } } From 26d605152cb8bc58c019758c60c8acb0d6b6fd7c Mon Sep 17 00:00:00 2001 From: Hwanseung Lee Date: Mon, 30 Oct 2017 19:37:14 +0900 Subject: [PATCH 5/5] pass the string set into isValildEnum() directly --- binding.gyp | 2 -- generator/main.ts | 34 +++---------------------------- generator/parser/enum_types.ts | 18 ++++++++-------- generator/parser/idl_interface.ts | 6 +++--- template/enum_types_cpp.njk | 26 ----------------------- template/enum_types_header.njk | 30 --------------------------- template/interface_cpp.njk | 10 ++++++--- 7 files changed, 23 insertions(+), 103 deletions(-) delete mode 100644 template/enum_types_cpp.njk delete mode 100644 template/enum_types_header.njk diff --git a/binding.gyp b/binding.gyp index b369f82..89e4573 100644 --- a/binding.gyp +++ b/binding.gyp @@ -60,8 +60,6 @@ '<@(test_cpp_files)', '<@(test_idl_output_files)', '<(SHARED_INTERMEDIATE_DIR)/bacardi.cc', - '<(SHARED_INTERMEDIATE_DIR)/enum_types.h', - '<(SHARED_INTERMEDIATE_DIR)/enum_types.cpp', ], 'defines': [ 'NAPI_DISABLE_CPP_EXCEPTIONS' ], }, diff --git a/generator/main.ts b/generator/main.ts index 67f30b6..f895be2 100644 --- a/generator/main.ts +++ b/generator/main.ts @@ -25,6 +25,7 @@ import snakeCase = require('snake-case'); import EnumTypes from './parser/enum_types'; import IDLDefinition from './parser/idl_definition'; +import IDLEnum from './parser/idl_enum'; import IDLInterface from './parser/idl_interface'; import Parser from './parser/parser'; @@ -74,33 +75,7 @@ async function generateInterface( }); } -async function generateEnumTypes( - env: nunjucks.Environment, output_path: string, - definitions: IDLDefinition[]) { - const [enum_type_header_tmpl, enum_type_cpp_tmpl] = await Promise.all([ - file.read(path.resolve(TEMPLATE_DIR, 'enum_types_header.njk')), - file.read(path.resolve(TEMPLATE_DIR, 'enum_types_cpp.njk')), - ]); - - const enum_type_header_path = path.resolve(output_path, 'enum_types.h'); - const enum_type_cpp_path = path.resolve(output_path, 'enum_types.cpp'); - - let idl_enum: IDLDefinition[] = []; - definitions.forEach(async (definition) => { - if (definition.isIDLEnum()) { - idl_enum.push(definition); - } - }); - - await file.write( - enum_type_header_path, - env.renderString(enum_type_header_tmpl, {enums: idl_enum})); - await file.write( - enum_type_cpp_path, - env.renderString(enum_type_cpp_tmpl, {enums: idl_enum})); -} - -// TODO(hwansueng) :: this function should be improved. +// TODO(hwansueng): This function should be improved. async function postProcessing(definitions: IDLDefinition[]) { let enum_types: EnumTypes = new EnumTypes(definitions); @@ -110,9 +85,7 @@ async function postProcessing(definitions: IDLDefinition[]) { for (const member of idl_interface.members) { if (member.arguments != null) { for (let args of member.arguments) { - if (enum_types.isEnumType(args.type)) { - args.is_enum_type = true; - } + args.enum = enum_types.isEnumType(args.type); } } } @@ -145,7 +118,6 @@ async function main([root_dir, out_dir, ...idl_files]) { await postProcessing(definitions); await generateInterface(env, out_dir, definitions); await generateBacardi(env, out_dir, definitions); - await generateEnumTypes(env, out_dir, definitions); return 0; } diff --git a/generator/parser/enum_types.ts b/generator/parser/enum_types.ts index 0b0a42d..04ad9ce 100644 --- a/generator/parser/enum_types.ts +++ b/generator/parser/enum_types.ts @@ -15,25 +15,27 @@ */ import IDLDefinition from './idl_definition'; +import IDLEnum from './idl_enum'; + export default class EnumTypes { - types: string[]; + enums: IDLEnum[]; constructor(definitions: IDLDefinition[]) { - this.types = []; + this.enums = []; definitions.forEach((definition) => { if (definition.isIDLEnum()) { - this.types.push(definition.name); + this.enums.push(definition as IDLEnum); } }); } - public isEnumType(source: string): boolean { - for (const type of this.types) { - if (type == source) { - return true; + public isEnumType(source: string): IDLEnum { + for (const item of this.enums) { + if (item.name == source) { + return item; } } - return false; + return null; } } diff --git a/generator/parser/idl_interface.ts b/generator/parser/idl_interface.ts index 70d3d90..6566d6a 100644 --- a/generator/parser/idl_interface.ts +++ b/generator/parser/idl_interface.ts @@ -15,6 +15,7 @@ */ import IDLDefinition from './idl_definition'; +import IDLEnum from './idl_enum'; import IDLIdentifier from './idl_identifier'; // FIXME(zino): We should consider attribute and operation concept. @@ -22,13 +23,12 @@ import IDLIdentifier from './idl_identifier'; class Argument implements IDLIdentifier { readonly type: string; readonly name: string; - // TODO(hwansueng) :: this attribute is temporary to distinguish enum type. - is_enum_type: boolean; + enum?: IDLEnum; constructor(raw_argument_info: {}) { this.type = raw_argument_info['idlType']['idlType']; this.name = raw_argument_info['name']; - this.is_enum_type = false; + this.enum = null; } } diff --git a/template/enum_types_cpp.njk b/template/enum_types_cpp.njk deleted file mode 100644 index f9250e1..0000000 --- a/template/enum_types_cpp.njk +++ /dev/null @@ -1,26 +0,0 @@ -/** - * 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. - */ - -#include "./enum_types.h" - -{% for enum in enums %} -const std::set EnumTypes::{{enum.name | snakecase}}_enum_set = { - {% for value in enum.values %} - "{{value}}", - {% endfor %} - }; - -{% endfor %} diff --git a/template/enum_types_header.njk b/template/enum_types_header.njk deleted file mode 100644 index 36709ea..0000000 --- a/template/enum_types_header.njk +++ /dev/null @@ -1,30 +0,0 @@ -/** - * 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. - */ - -#ifndef GEN_ENUM_TYPES_H_ -#define GEN_ENUM_TYPES_H_ - -#include -#include - -class EnumTypes { -public: -{% for enum in enums %} - const static std::set {{enum.name | snakecase}}_enum_set; -{% endfor %} -}; - -#endif // GEN_ENUM_TYPES_H_ \ No newline at end of file diff --git a/template/interface_cpp.njk b/template/interface_cpp.njk index a3dffcb..a5f3c6f 100644 --- a/template/interface_cpp.njk +++ b/template/interface_cpp.njk @@ -19,7 +19,6 @@ #include "core/js_type_traits.h" #include "core/native_type_traits.h" #include "core/enum_validator.h" -#include "enum_types.h" void {{name}}Bridge::Init(Napi::Env env, Napi::Object exports) { Napi::Function js_constructor = @@ -108,10 +107,15 @@ Napi::Value {{name}}Bridge::{{member.name | camelcase}}(const Napi::CallbackInfo } {% for argument in member.arguments %} - {% if argument.is_enum_type == true %} + {% if argument.enum %} + const std::set enum_value_set = { + {% for value in argument.enum.values %} + "{{value}}", + {% endfor %} + }; auto {{argument.name}} = NativeTypeTraits::NativeValue(info.Env(), info[{{loop.index0}}]); - if (!EnumValidator::isValildEnum({{argument.name}}, EnumTypes::{{argument.type | snakecase}}_enum_set)) { + if (!EnumValidator::isValildEnum({{argument.name}}, enum_value_set)) { Napi::TypeError::New(info.Env(), "it not matched with values of enum in idl.") .ThrowAsJavaScriptException(); return Napi::Value();