Skip to content

Commit

Permalink
implement enum
Browse files Browse the repository at this point in the history
  • Loading branch information
hwanseung committed Oct 8, 2017
1 parent 47cd9e0 commit 63bc717
Show file tree
Hide file tree
Showing 10 changed files with 135 additions and 0 deletions.
2 changes: 2 additions & 0 deletions core/idl_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,7 @@ 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> {};

#endif // CORE_IDL_TYPES_H_
39 changes: 39 additions & 0 deletions core/native_type_traits.h
Original file line number Diff line number Diff line change
Expand Up @@ -160,4 +160,43 @@ 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;
}
};

#endif // CORE_NATIVE_TYPE_TRAITS_H_
15 changes: 15 additions & 0 deletions examples/calculator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,18 @@ double Calculator::Div(double number1, double number2) {
bool Calculator::IsEquals(int16_t number1, int16_t number2) {
return number1 == number2;
}

double Calculator::Calculate(const std::string& operatorStr,
double number1,
double number2) {
if (operatorStr.compare("add") == 0) {
return number1 + number2;
} else if (operatorStr.compare("sub") == 0) {
return number1 - number2;
} else if (operatorStr.compare("mul") == 0) {
return number1 * number2;
} else if (operatorStr.compare("div") == 0) {
return number1 / number2;
}
return 0;
}
3 changes: 3 additions & 0 deletions examples/calculator.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ class Calculator {
int64_t Mul(int64_t number1, int64_t number2);
double Div(double number1, double number2);
bool IsEquals(int16_t number1, int16_t number2);
double Calculate(const std::string& operatorStr,
double number1,
double number2);
};

#endif // EXAMPLES_CALCULATOR_H_
8 changes: 8 additions & 0 deletions examples/calculator.idl
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@
* limitations under the License.
*/

enum OperationType {
"add",
"sub",
"mul",
"div"
};

[
Constructor(),
Constructor(long createdTime),
Expand All @@ -27,6 +34,7 @@ interface Calculator {
double mul(double number1, double number2);
double div(double number1, double number2);
bool isEquals(short number1, short number2);
double calculate(OperationType type, double number1, double number2);
};

interface TernaryCalculator {
Expand Down
21 changes: 21 additions & 0 deletions examples/calculator.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,3 +144,24 @@ describe('generate one more bridge classes from one more interfaces', () => {
assert.equal(ternary_calculator.add(1, 2, 3), 1 + 2 + 3);
});
});

describe('enum type test', () => {
it('should be equal with expected value', () => {
assert.equal(calculator.calculate('add', 1, 2), 3);
assert.equal(calculator.calculate('sub', 2, 1), 1);
assert.equal(calculator.calculate('mul', 2, 1), 2);
assert.equal(calculator.calculate('div', 4, 1), 4);
});

it('should be throw error when invalid argument passed.', () => {
assert.throws(() => {
calculator.calculate('add', 1, 1, 1);
}, RangeError);
assert.throws(() => {
calculator.calculate(1, 1, 1);
}, TypeError);
assert.throws(() => {
calculator.calculate('not_match_value', 1, 1);
}, TypeError);
});
});
4 changes: 4 additions & 0 deletions generator/parser/idl_definition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,8 @@ export default abstract class IDLDefinition {
public isIDLInterface(): boolean {
return this.raw_idl_definition_info['type'] == 'interface';
}

public isIDLEnum(): boolean {
return this.raw_idl_definition_info['type'] == 'enum';
}
}
6 changes: 6 additions & 0 deletions generator/parser/idl_definition_factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,15 @@
*/

import IDLDefinition from './idl_definition';
import IDLEnum from './idl_enum';
import IDLInterface from './idl_interface';

export default class IDLDefinitionFactory {
static create(raw_idl_definition_info: {}): IDLDefinition {
if (this.isIDLInterface(raw_idl_definition_info)) {
return new IDLInterface(raw_idl_definition_info);
} else if (this.isIDLEnum(raw_idl_definition_info)) {
return new IDLEnum(raw_idl_definition_info);
}

return null;
Expand All @@ -29,4 +32,7 @@ export default class IDLDefinitionFactory {
private static isIDLInterface(raw_idl_definition_info: {}): boolean {
return raw_idl_definition_info['type'] == 'interface';
}
private static isIDLEnum(raw_idl_definition_info: {}): boolean {
return raw_idl_definition_info['type'] == 'enum';
}
}
34 changes: 34 additions & 0 deletions generator/parser/idl_enum.ts
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.
*/

import IDLDefinition from './idl_definition';

export default class IDLEnum extends IDLDefinition {
values: string[];

constructor(raw_idl_enum_info: {}) {
super(raw_idl_enum_info['name'], raw_idl_enum_info);

this.values = [];
raw_idl_enum_info['values'].forEach(raw_value_info => {
this.values.push(raw_value_info);
});
}

render(): void {
// TODO(zino): We should implement this function.
}
}
3 changes: 3 additions & 0 deletions template/interface_cpp.njk
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ Napi::Value {{name}}Bridge::{{member.name | camelcase}}(const Napi::CallbackInfo
{% for argument in member.arguments %}
auto {{argument.name}} = NativeTypeTraits<IDL{{argument.type | camelcase-}}
>::NativeValue(info.Env(), info[{{loop.index - 1}}]);
if (info.Env().IsExceptionPending()) {
return Napi::Value();
}
{% endfor %}

{% if member.type != "void" %}
Expand Down

1 comment on commit 63bc717

@yjaeseok
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

Please sign in to comment.