Skip to content

Commit

Permalink
auto genereate
Browse files Browse the repository at this point in the history
  • Loading branch information
hwanseung committed Oct 8, 2017
1 parent 0190d3b commit 33d398a
Show file tree
Hide file tree
Showing 7 changed files with 128 additions and 80 deletions.
2 changes: 2 additions & 0 deletions binding.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,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' ],
},
Expand Down
3 changes: 0 additions & 3 deletions core/idl_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,5 @@ struct IDLLongLong final : public IDLBaseHelper<int64_t> {};
struct IDLLong final : public IDLBaseHelper<int32_t> {};
struct IDLShort final : public IDLBaseHelper<int16_t> {};
struct IDLString final : public IDLBaseHelper<std::string> {};
// FIXME(Hwansung): should be generated automatically in another file.
struct IDLOperationType final : public IDLBaseHelper<std::string> {};
struct IDLTestEnum final : public IDLBaseHelper<std::string> {};

#endif // CORE_IDL_TYPES_H_
77 changes: 0 additions & 77 deletions core/native_type_traits.h
Original file line number Diff line number Diff line change
Expand Up @@ -160,81 +160,4 @@ struct NativeTypeTraits<IDLString> : public NativeTypeTraitsBase<IDLString> {
}
};

// FIXME(Hwansung): should be generated automatically in another file.
template <>
struct NativeTypeTraits<IDLOperationType>
: public NativeTypeTraitsBase<IDLOperationType> {
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<IDLTestEnum>
: public NativeTypeTraitsBase<IDLTestEnum> {
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_
26 changes: 26 additions & 0 deletions generator/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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;
}
Expand Down
26 changes: 26 additions & 0 deletions template/idl_enum_type.njk
Original file line number Diff line number Diff line change
@@ -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<std::string> {};
{% endfor %}

#endif // GEN_IDL_ENUM_TYPES_H_
1 change: 1 addition & 0 deletions template/interface_cpp.njk
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down
73 changes: 73 additions & 0 deletions template/native_enum_type_traits.njk
Original file line number Diff line number Diff line change
@@ -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 <napi.h>
#include <string>
#include <type_traits>
#include "core/native_type_traits.h"
#include "idl_enum_type.h"

{% for enum in enums %}
template <>
struct NativeTypeTraits<IDL{{enum.name}}>
: public NativeTypeTraitsBase<IDL{{enum.name}}> {
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_

0 comments on commit 33d398a

Please sign in to comment.