Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate JSTypeTraits automatically. #127

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions binding.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -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' ],
},
Expand Down
12 changes: 12 additions & 0 deletions generator/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand All @@ -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[]) {
Expand Down Expand Up @@ -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);

Expand Down
18 changes: 18 additions & 0 deletions generator/types/js_type_traits.ts
Original file line number Diff line number Diff line change
@@ -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'],
'string': ['std::string'], 'boolean': ['bool'] };
2 changes: 1 addition & 1 deletion template/interface_cpp.njk
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
23 changes: 12 additions & 11 deletions core/js_type_traits.h → template/jstypes_traits.njk
Original file line number Diff line number Diff line change
Expand Up @@ -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 <napi.h>

Expand Down Expand Up @@ -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_