Fix dart generator implementation 11/286511/9
authorChanggyu Choi <changyu.choi@samsung.com>
Mon, 9 Jan 2023 08:13:37 +0000 (17:13 +0900)
committerHwanKyu Jhun <h.jhun@samsung.com>
Thu, 12 Jan 2023 00:42:40 +0000 (00:42 +0000)
Changes:
 - Modify method type of sync method.
 - Changes some stub variable names.
 - Removes unnecessary new lines.
 - Removes OnDisconnected typedef from the proxy.

Change-Id: Id51bcc20515dbabb7c4674cca2de83a8d7ac965d
Signed-off-by: Changgyu Choi <changyu.choi@samsung.com>
README.md
idlc/gen/dart_proxy_gen.cc
idlc/gen/dart_proxy_gen.h
idlc/gen/dart_proxy_gen_cb.h
idlc/gen/dart_stub_gen_cb.h

index 1ddaba0..8d78daf 100755 (executable)
--- a/README.md
+++ b/README.md
@@ -495,15 +495,15 @@ abstract class ServiceBase {
   Future<int> onRun(Foo foo);
 }
 
-typedef InstanceBuilder = ServiceBase Function(String sender, String instance);
+typedef ServiceBuilder = ServiceBase Function(String sender, String instance);
 
 class Runnable extends StubBase {
-  Runnable({required InstanceBuilder instanceBuilder})
-      : _instanceBuilder = instanceBuilder,
+  Runnable({required ServiceBuilder serviceBuilder})
+      : _serviceBuilder = serviceBuilder,
       super('Runnable');
 
   final List<ServiceBase> services = <ServiceBase>[];
   final Map<int, dynamic> _methodHandlers = <int, dynamic>{};
-  final InstanceBuilder _instanceBuilder;
+  final ServiceBuilder _serviceBuilder;
 }
 ```
index de1aac2..538f3b2 100644 (file)
@@ -34,16 +34,11 @@ void DartProxyGen::OnInitGen(std::ofstream& stream) {
   GenDelegateId(stream);
   GenMethodId(stream);
   GenStructures(stream);
-  GenOnDisconnected(stream);
   GenInterfaces(stream);
 }
 
 void DartProxyGen::OnFiniGen(std::ofstream& stream) {}
 
-void DartProxyGen::GenOnDisconnected(std::ofstream& stream) {
-  stream << CB_ON_DISCONNECTED;
-}
-
 void DartProxyGen::GenInterfaces(std::ofstream& stream) {
   for (auto& i : GetDocument().GetBlocks()) {
     if (i->GetType() != Block::TYPE_INTERFACE)
@@ -100,8 +95,9 @@ std::string DartProxyGen::GenInterfaceMethods(const Interface& iface) {
     code += ReplaceAll(CB_METHOD_BASE, {
           { "<METHOD_NAME>", d->GetID() },
           { "<LOWER_METHOD_NAME>", std::move(method_name) },
-          { "<RETURN_TYPE>", d->GetType().ToString() },
+          { "<RETURN_TYPE>", GenReturnType(*d) },
           { "<ARGS>", args },
+          { "<ASYNC>", GenAsync(*d) },
           { "<METHOD_PARCEL_WRITE>", GenMethodParcelWrite(*d) },
           { "<METHOD_PARCEL_READ>", GenMethodParcelRead(*d) },
         });
@@ -110,6 +106,20 @@ std::string DartProxyGen::GenInterfaceMethods(const Interface& iface) {
   return RemoveLine(code);
 }
 
+std::string DartProxyGen::GenReturnType(const Declaration& decl) {
+  if (decl.GetMethodType() == Declaration::MethodType::ASYNC)
+    return "Future<" + decl.GetType().ToString() + ">";
+
+  return decl.GetType().ToString();
+}
+
+std::string DartProxyGen::GenAsync(const Declaration& decl) {
+  if (decl.GetMethodType() == Declaration::MethodType::ASYNC)
+    return "async ";
+
+  return "";
+}
+
 std::string DartProxyGen::GenMethodParcelWrite(const Declaration& decl) {
   std::string code;
   for (const auto& p : decl.GetParameters()) {
@@ -123,28 +133,25 @@ std::string DartProxyGen::GenMethodParcelWrite(const Declaration& decl) {
             { "<DELEGATE_TYPE>", ConvertTypeToString(type) },
             { "<DELEGATE_NAME>", p->GetID() }
           });
-      code += NLine(1);
     } else if (type.IsUserDefinedType()) {
       code += ReplaceAll(CB_USER_DEFINED_PARCEL_WRITE, {
             { "<PARCEL>", "parcel" },
             { "<NAME>", p->GetID() }
           });
-      code += NLine(1);
     } else if (type.ToString() == "list" || type.ToString() == "array") {
       code += ReplaceAll(CB_LIST_PARCEL_WRITE, {
             { "<PARCEL>", "parcel" },
             { "<NAME>", p->GetID() }
           });
-      code += NLine(1);
     } else {
       code += ReplaceAll(CB_BASE_PARCEL_WRITE, {
             { "<PARCEL>", "parcel" },
             { "<PARCEL_TYPE>", ConvertTypeToParcelType(type.ToString()) },
             { "<NAME>" , p->GetID() }
           });
-      code += NLine(1);
     }
 
+    code += NLine(1);
     auto private_sharing_code =
         GetPrivateSharingString(type, "port", p->GetID());
     if (!private_sharing_code.empty())
@@ -211,23 +218,21 @@ std::string DartProxyGen::GenMethodOutParamParcelRead(
             { "<PARCEL>", "parcelReceived" },
             { "<NAME>", p->GetID() }
           });
-      code += NLine(1);
     } else if (type.ToString() == "list" || type.ToString() == "array") {
       code += ReplaceAll(CB_LIST_PARCEL_READ, {
             { "<PARCEL>", "parcelReceived" },
             { "<NAME>", p->GetID() }
           });
-      code += NLine(1);
     } else {
       code += ReplaceAll(CB_BASE_PARCEL_READ, {
             { "<PARCEL>", "parcelReceived" },
             { "<PARCEL_TYPE>", ConvertTypeToParcelType(type.ToString()) },
             { "<NAME>" , p->GetID() }
           });
-      code += NLine(1);
     }
   }
 
+  code += NLine(1);
   return code;
 }
 
index 20cce6b..e9a6eef 100644 (file)
@@ -40,6 +40,8 @@ class DartProxyGen : public DartGeneratorBase {
   std::string GenInterfaceMethods(const Interface& iface);
   std::string GenMethodParcelRead(const Declaration& decl);
   std::string GenMethodInvoke(const Declaration& decl);
+  std::string GenReturnType(const Declaration& decl);
+  std::string GenAsync(const Declaration& decl);
   std::string GenMethodParcelWrite(const Declaration& decl);
   std::string GenMethodResultParcelRead(const Declaration& decl);
   std::string GenMethodOutParamParcelRead(const Declaration& decl);
index b8a0d61..4184c58 100644 (file)
@@ -44,14 +44,6 @@ class _<DELEGATE_NAME> extends _Delegate {
 }
 )__dart_cb";
 
-constexpr const char CB_ON_DISCONNECTED[] =
-R"__dart_cb(
-/// Called when the disconnected event is received on rpc port.
-///
-/// This is used by the connect() of interfaces.
-typedef OnDisconnected = void Function();
-)__dart_cb";
-
 /**
  * <INTERFACE_NAME> The name of the interface.
  */
@@ -123,7 +115,7 @@ class <INTERFACE_NAME> extends ProxyBase {
 constexpr const char CB_METHOD_BASE[] =
 R"__dart_cb(
 /// This method is used to send '<METHOD_NAME>' request to the stub app.
-Future<<RETURN_TYPE>> <LOWER_METHOD_NAME>(<ARGS>) async {
+<RETURN_TYPE> <LOWER_METHOD_NAME>(<ARGS>) <ASYNC>{
   <METHOD_PARCEL_WRITE>
   <METHOD_PARCEL_READ>
 }
index 6d49533..4efaf45 100644 (file)
@@ -86,11 +86,11 @@ class <DELEGATE_NAME> extends _Delegate {
   /// Invokes the delegate method of the client.
   Future<void> invoke(<DELEGATE_PARAMS>) async {
     if (_port == null) {
-      throw Exception('NotConnectedSocketException');
+      throw StateError('Must be connected first');
     }
 
     if (once && _wasInvoked) {
-      throw Exception('InvalidCallbackException');
+      throw Exception('Can be invoked only once');
     }
 
     final Parcel parcel = Parcel();
@@ -116,13 +116,13 @@ class <DELEGATE_NAME> extends _Delegate {
 constexpr const char CB_INTERFACE_BASE[] =
 R"__dart_cb(
 /// This is used when creating a service instance.
-typedef InstanceBuilder = ServiceBase Function(String sender, String instance);
+typedef ServiceBuilder = ServiceBase Function(String sender, String instance);
 
 /// [<INTERFACE_NAME>] class for RPC.
 class <INTERFACE_NAME> extends StubBase {
   /// Constructor for this class.
-  <INTERFACE_NAME>({required InstanceBuilder instanceBuilder})
-      : _instanceBuilder = instanceBuilder,
+  <INTERFACE_NAME>({required ServiceBuilder serviceBuilder})
+      : _serviceBuilder = serviceBuilder,
         super('<INTERFACE_NAME>') {
     <CTOR>
   }
@@ -130,13 +130,13 @@ class <INTERFACE_NAME> extends StubBase {
   /// The indexable collection of [ServiceBase] class.
   final List<ServiceBase> services = <ServiceBase>[];
   final Map<int, dynamic> _methodHandlers = <int, dynamic>{};
-  final InstanceBuilder _instanceBuilder;
+  final ServiceBuilder _serviceBuilder;
 
   @override
   @visibleForOverriding
   @nonVirtual
   Future<void> onConnectedEvent(String sender, String instance) async {
-    final ServiceBase service = _instanceBuilder(sender, instance);
+    final ServiceBase service = _serviceBuilder(sender, instance);
     service._port = getPort(instance, PortType.callback);
     await service.onCreate();
     services.add(service);