-
Notifications
You must be signed in to change notification settings - Fork 17
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
Implement IDL enum feature #110
Conversation
core/idl_types.h
Outdated
@@ -25,5 +25,8 @@ 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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i think this lines should be generated automatically at another files.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think so too ! should be generated automatically ! and another files !
core/native_type_traits.h
Outdated
@@ -133,4 +133,34 @@ struct NativeTypeTraits<IDLString> : public NativeTypeTraitsBase<IDLString> { | |||
} | |||
}; | |||
|
|||
// FIXME(Hwansung): should be generated automatically |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i think this lines should be generated automatically at another files.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thinks so too also
9f27ce3
to
260692b
Compare
core/native_type_traits.h
Outdated
@@ -133,4 +133,34 @@ struct NativeTypeTraits<IDLString> : public NativeTypeTraitsBase<IDLString> { | |||
} | |||
}; | |||
|
|||
// FIXME(Hwansung): should be generated automatically |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thinks so too also
core/native_type_traits.h
Outdated
.ThrowAsJavaScriptException(); | ||
return OperationType::NONE; | ||
} | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If my PR (#108) is merged before this PR,
This Type must be implement IsTypeEquals methods like below :
static bool IsTypeEquals(const Napi::Value& js_value) {
return js_value.IsOperationType();
}
otherwise, i should implement this method! :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
okay i will rebase it.
unfortunately we should implement more things in IsTypeEquals function instead of to use js_value.IsOperationType()
.
because js_value will be string type in this case. and it should be check that value can be Enum type.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If IsOperationType()
implementation is too large to apply this PR, I think the implementation can be divided another PR.
Other things LGTM.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you complete implement ationIsOperationType()
.
you can check argument first, and yiu can return value.
please refer to below code :
template <>
struct NativeTypeTraits<IDLOperationType>
: public NativeTypeTraitsBase<IDLOperationType> {
static OperationType NativeValue(const Napi::Env& env,
const Napi::Value& js_value) {
if (IsTypeEquals(js_value)) {
std::string value = js_value.ToString().Utf8Value();
if (value.compare("add") == 0) {
return OperationType::ADD;
} else if (value.compare("sub") == 0) {
return OperationType::SUB;
} else if (value.compare("mul") == 0) {
return OperationType::MUL;
} else if (value.compare("div") == 0) {
return OperationType::DIV;
}
} else {
Napi::TypeError::New(env, "It's an invalid string.")
.ThrowAsJavaScriptException();
return OperationType::NONE;
}
};
IMHO, the IDL enum type is just string type in Javascript. For example, // In IDL
enum OperationType {
"add",
"sub",
"mul",
"div"
}; // In Javascript
// JS developer just passes string.
calculator('add', 10, 20); // In C++
double Calculator::Calculate(const std::string& operator, double number1, double number2) {
// The following function can take only "add", "sub", "mul", and "div".
CalculateInternal(operator, number1, number2);
} // If the enum string is converted to C++ enum in binding side,
// C++ developer might need to convert it again.
double Calculator::Calculate(OperationType opType, double number1, double number2) {
// The following function can take only "add", "sub", "mul", and "div".
CalculateInternal(ToString(opType), number1, number2);
} WDYT? |
@romandev i had wanted to provide automatically converting both side. |
@hwanseung Hmm, we can discuss about this in Slack in Korean :) |
i changed it to pass string type instead of enum type. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay, I left some comments but lgtm.
Please follow my comments in follow-up CL.
@@ -160,4 +160,43 @@ struct NativeTypeTraits<IDLString> : public NativeTypeTraitsBase<IDLString> { | |||
} | |||
}; | |||
|
|||
// FIXME(Hwansung): should be generated automatically in another file. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay, for now as-is okay. But this is not exact NativeTypeTraits because the IDL enum type will always be string type in c++ side.
@@ -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', () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please rewrite a test in test/ directory and please use jest.
@@ -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, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please rewrite a test in test/ directory and please use jest.
Implement IDL enum feature. Some part of codes should be generated automatically at another files but this patch doesn't do that yet. So, it will be finished in follow-up patch. ISSUE=#80
Implement IDL enum feature. Some part of codes should be generated
automatically at another files but this patch doesn't do that yet.
So, it will be finished in follow-up patch.
ISSUE=#80