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

Implement IDL enum feature #110

Merged
merged 1 commit into from
Oct 8, 2017
Merged

Implement IDL enum feature #110

merged 1 commit into from
Oct 8, 2017

Conversation

hwanseung
Copy link
Contributor

@hwanseung hwanseung commented Oct 6, 2017

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

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
Copy link
Contributor Author

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.

Copy link
Contributor

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 !

@@ -133,4 +133,34 @@ struct NativeTypeTraits<IDLString> : public NativeTypeTraitsBase<IDLString> {
}
};

// FIXME(Hwansung): should be generated automatically
Copy link
Contributor Author

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.

Copy link
Contributor

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

@@ -133,4 +133,34 @@ struct NativeTypeTraits<IDLString> : public NativeTypeTraitsBase<IDLString> {
}
};

// FIXME(Hwansung): should be generated automatically
Copy link
Contributor

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

.ThrowAsJavaScriptException();
return OperationType::NONE;
}
};
Copy link
Contributor

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! :)

Copy link
Contributor Author

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.

Copy link
Contributor

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.

Copy link
Contributor

@yjaeseok yjaeseok Oct 6, 2017

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;
    }
};

@romandev
Copy link
Member

romandev commented Oct 7, 2017

IMHO, the IDL enum type is just string type in Javascript.
On the other hand, enum type in C++ is integer type.
So, it might be unexpected behavior in JS developer side.

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?

@hwanseung
Copy link
Contributor Author

@romandev
did you mean we don't have to make enum type automatically and provide it to user?
and is it role of cpp user to convert string to enum?

i had wanted to provide automatically converting both side.
actually i am not sure what is more convenient and easy to understand to user.
but your suggestion seem like more easy to understand.
so i will change this patch to your suggestion.

@romandev
Copy link
Member

romandev commented Oct 8, 2017

@hwanseung Hmm, we can discuss about this in Slack in Korean :)

@hwanseung
Copy link
Contributor Author

i changed it to pass string type instead of enum type.

@romandev romandev changed the title Implement Enum feature. Implement IDL enum feature. Oct 8, 2017
@romandev romandev changed the title Implement IDL enum feature. Implement IDL enum feature Oct 8, 2017
Copy link
Member

@romandev romandev left a 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.
Copy link
Member

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', () => {
Copy link
Member

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,
Copy link
Member

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.

@romandev romandev merged commit aaa4167 into lunchclass:master Oct 8, 2017
@hwanseung hwanseung deleted the enum branch October 8, 2017 12:46
romandev pushed a commit that referenced this pull request Oct 18, 2017
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants