From: Changgyu Choi Date: Mon, 9 Jan 2023 08:13:37 +0000 (+0900) Subject: Fix dart generator implementation X-Git-Tag: accepted/tizen/unified/20230420.153149~26 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=refs%2Fchanges%2F11%2F286511%2F9;p=platform%2Fcore%2Fappfw%2Ftidl.git Fix dart generator implementation 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 --- diff --git a/README.md b/README.md index 1ddaba0..8d78daf 100755 --- a/README.md +++ b/README.md @@ -495,15 +495,15 @@ abstract class ServiceBase { Future 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 services = []; final Map _methodHandlers = {}; - final InstanceBuilder _instanceBuilder; + final ServiceBuilder _serviceBuilder; } ``` diff --git a/idlc/gen/dart_proxy_gen.cc b/idlc/gen/dart_proxy_gen.cc index de1aac2..538f3b2 100644 --- a/idlc/gen/dart_proxy_gen.cc +++ b/idlc/gen/dart_proxy_gen.cc @@ -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, { { "", d->GetID() }, { "", std::move(method_name) }, - { "", d->GetType().ToString() }, + { "", GenReturnType(*d) }, { "", args }, + { "", GenAsync(*d) }, { "", GenMethodParcelWrite(*d) }, { "", 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) { { "", ConvertTypeToString(type) }, { "", p->GetID() } }); - code += NLine(1); } else if (type.IsUserDefinedType()) { code += ReplaceAll(CB_USER_DEFINED_PARCEL_WRITE, { { "", "parcel" }, { "", p->GetID() } }); - code += NLine(1); } else if (type.ToString() == "list" || type.ToString() == "array") { code += ReplaceAll(CB_LIST_PARCEL_WRITE, { { "", "parcel" }, { "", p->GetID() } }); - code += NLine(1); } else { code += ReplaceAll(CB_BASE_PARCEL_WRITE, { { "", "parcel" }, { "", ConvertTypeToParcelType(type.ToString()) }, { "" , 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( { "", "parcelReceived" }, { "", p->GetID() } }); - code += NLine(1); } else if (type.ToString() == "list" || type.ToString() == "array") { code += ReplaceAll(CB_LIST_PARCEL_READ, { { "", "parcelReceived" }, { "", p->GetID() } }); - code += NLine(1); } else { code += ReplaceAll(CB_BASE_PARCEL_READ, { { "", "parcelReceived" }, { "", ConvertTypeToParcelType(type.ToString()) }, { "" , p->GetID() } }); - code += NLine(1); } } + code += NLine(1); return code; } diff --git a/idlc/gen/dart_proxy_gen.h b/idlc/gen/dart_proxy_gen.h index 20cce6b..e9a6eef 100644 --- a/idlc/gen/dart_proxy_gen.h +++ b/idlc/gen/dart_proxy_gen.h @@ -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); diff --git a/idlc/gen/dart_proxy_gen_cb.h b/idlc/gen/dart_proxy_gen_cb.h index b8a0d61..4184c58 100644 --- a/idlc/gen/dart_proxy_gen_cb.h +++ b/idlc/gen/dart_proxy_gen_cb.h @@ -44,14 +44,6 @@ class _ 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"; - /** * The name of the interface. */ @@ -123,7 +115,7 @@ class extends ProxyBase { constexpr const char CB_METHOD_BASE[] = R"__dart_cb( /// This method is used to send '' request to the stub app. -Future<> () async { + () { } diff --git a/idlc/gen/dart_stub_gen_cb.h b/idlc/gen/dart_stub_gen_cb.h index 6d49533..4efaf45 100644 --- a/idlc/gen/dart_stub_gen_cb.h +++ b/idlc/gen/dart_stub_gen_cb.h @@ -86,11 +86,11 @@ class extends _Delegate { /// Invokes the delegate method of the client. Future invoke() 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 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); /// [] class for RPC. class extends StubBase { /// Constructor for this class. - ({required InstanceBuilder instanceBuilder}) - : _instanceBuilder = instanceBuilder, + ({required ServiceBuilder serviceBuilder}) + : _serviceBuilder = serviceBuilder, super('') { } @@ -130,13 +130,13 @@ class extends StubBase { /// The indexable collection of [ServiceBase] class. final List services = []; final Map _methodHandlers = {}; - final InstanceBuilder _instanceBuilder; + final ServiceBuilder _serviceBuilder; @override @visibleForOverriding @nonVirtual Future 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);