How to work with Application Objects in binding generators
Binding generator must detect functions that accept application objects.
Application object interaction protocol and functions that accept app objects have the following signatures in core:
It means when a function accepts an application-implemented object, from that point library will have access to the methods of this object.
How to detect
Function contains parameter with generic type AppObject<foo_module.ParamsOfAppFoo, foo_module.ResultOfAppFoo>
.
Generated code
Generator must produce:
interface declaration;
interface dispatcher;
function that accept application object.
Interface declaration
Interface
Foo
is extracted from the name of the first generic arg of theAppObject<foo_module.ParamsOfAppFoo, foo_module.ResultOfAppFoo>
. Note that a generic arg name is a fully qualified name so you must remove the module name first. In the example above the first arg name isfoo_module.ParamsOfAppFoo.
After removing module name we haveParamsOfAppFoo
. Then we must remove the prefixParamsOf
. The rest of the name contains the interface nameFoo
.To collect a list of interface methods we must collect variant names from enum
foo_module.ParamsOfAppFoo
. Each variant ofParamsOfAppFoo
represents the interface method. Respectively each variant ofResultOfAppFoo
represents the result of the interface method. If interface method has noResultOfAppFoo
then such method is a notify method – no waiting for the response is needed.The name of the interface method is constructed from the name of the variant by using a simple rule
PascalStyleName
→snake_style_name
. So the variantCallWithParamsAndResult
will be converted tocall_with_params_and_result
.If a function has a result then this function must return
Promise
and perform asynchronous execution.
Interface dispatcher
The implementation of the wrapper method is more difficult than regular. It must pass dispatching responseHandler
to the library. Library will call this handler every time when it requires to call the application object. Two response types are used for calling application objects: 3
for calling methods which return result and 4
for notifiyng without awaiting any result. When response type 4
is passed, data contains enum ParamsOfAppFoo
. When 3
is passed, data contains struct ParamsOfAppRequest
where request_data
field contains ParamsOfAppFoo
Generator must define special dispatch helper for application object invocation:
Functions with application object
The obj
parameter must be declared instead of the source obj: AppObject<ParamsOfAppFoo, ResultOfAppFoo>
.
Wrapper implementation with dispatcher must be generated as:
Last updated