Skip to content

Commit

Permalink
change
Browse files Browse the repository at this point in the history
  • Loading branch information
hwanseung committed Oct 12, 2017
1 parent 36c30e4 commit 1f60422
Show file tree
Hide file tree
Showing 10 changed files with 160 additions and 93 deletions.
4 changes: 2 additions & 2 deletions binding.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -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' ],
},
Expand Down
1 change: 1 addition & 0 deletions core/core.gypi
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
{
'variables': {
'core_cpp_files': [
'enum_validator.h'
'idl_base.h',
'idl_types.h',
'js_type_traits.h',
Expand Down
34 changes: 34 additions & 0 deletions core/enum_validator.h
Original file line number Diff line number Diff line change
@@ -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 <set>
#include <string>

class EnumValidator {
public:
static bool isValildEnum(const std::string value,
const std::set<std::string> enum_values) {
if (enum_values.count(value) > 0) {
return true;
}
return false;
}
};

#endif // CORE_ENUM_VALIDATOR_H_
42 changes: 32 additions & 10 deletions generator/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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) => {
Expand All @@ -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]) {
Expand All @@ -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;
}
Expand Down
39 changes: 39 additions & 0 deletions generator/parser/enum_types.ts
Original file line number Diff line number Diff line change
@@ -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;
}
}
3 changes: 3 additions & 0 deletions generator/parser/idl_interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

Expand Down
14 changes: 7 additions & 7 deletions template/idl_enum_type.njk → template/enum_types_cpp.njk
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string> {};
{% endfor %}
const std::set<std::string> EnumTypes::{{enum.name | snakecase}}_enum_set = {
{% for value in enum.values %}
"{{value}}",
{% endfor %}
};

#endif // GEN_IDL_ENUM_TYPES_H_
{% endfor %}
30 changes: 30 additions & 0 deletions template/enum_types_header.njk
Original file line number Diff line number Diff line change
@@ -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 <set>
#include <string>

class EnumTypes {
public:
{% for enum in enums %}
const static std::set<std::string> {{enum.name | snakecase}}_enum_set;
{% endfor %}
};

#endif // GEN_ENUM_TYPES_H_
13 changes: 12 additions & 1 deletion template/interface_cpp.njk
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down Expand Up @@ -72,11 +73,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<IDLString
>::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<IDL{{argument.type | camelcase-}}
>::NativeValue(info.Env(), info[{{loop.index0}}]);
if (info.Env().IsExceptionPending()) {
return Napi::Value();
}
{% endif %}
{% endfor %}

{% if member.type != "void" %}
Expand Down
73 changes: 0 additions & 73 deletions template/native_enum_type_traits.njk

This file was deleted.

0 comments on commit 1f60422

Please sign in to comment.