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

feat(go): support direct implementation of jsii interfaces #2614

Merged
merged 14 commits into from
Mar 3, 2021

Conversation

RomainMuller
Copy link
Contributor

@RomainMuller RomainMuller commented Feb 26, 2021

Pure go implementations of jsii interfaces are detected upon being
passed as a parameter to a JavaScript call. When this happens, a
create call is sent to the @jsii/kernel process with the correct
list of implemented interface FQNs, and all necessary overrides records.
The object is then registered into the new ObjectStore for later
reference.

When a callback request interrupts the normal request/response flow, the
kernel.Client will handle the callback by getting the appropriate
receiver object, invoking the designated go implementation, and sending
the result to the @jsii/kernel process before resuming normal response
handling.

Related to #2048 (partial support)


Minor additional content:

  • yarn doc will start godoc (serving the go documentation - including
    for the runtime modules - at localhost:6060)
  • yarn lint now runs golint on top of go vet, for improved code quality

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

@RomainMuller RomainMuller added language/go Regarding GoLang bindings effort/large Large work item – several weeks of effort contribution/core This is a PR that came from AWS. labels Feb 26, 2021
@RomainMuller RomainMuller requested a review from a team February 26, 2021 17:26
@RomainMuller RomainMuller self-assigned this Feb 26, 2021
Pure go implementations of jsii interfaces are detected upon being
passed as a parameter to a JavaScript call. When this happens, a
`create` call is sent to the `@jsii/kernel` process with the correct
list of implemented interface FQNs, and all necessary overrides records.
The object is then registered into the new `ObjectStore` for later
reference.

When a callback request interrupts the normal request/response flow, the
`kernel.Client` will handle the callback by getting the appropriate
receiver object, invoking the designated go implementation, and sending
the result to the `@jsii/kernel` process before resuming normal response
handling.
@RomainMuller RomainMuller marked this pull request as ready for review March 1, 2021 09:13
Comment on lines 67 to 91

type Callback struct {
CallbackID *string `json:"cbid"`
Cookie *string `json:"cookie"`
Invoke InvokeCallback `json:"invoke"`
Get GetCallback `json:"get"`
Set SetCallback `json:"set"`
}

type InvokeCallback struct {
Method string `json:"method"`
Arguments []interface{} `json:"args"`
ObjRef ObjectRef `json:"objref"`
}

type GetCallback struct {
Property string `json:"property"`
ObjRef ObjectRef `json:"objref"`
}

type SetCallback struct {
Property string `json:"property"`
Value interface{} `json:"value"`
ObjRef ObjectRef `json:"objref"`
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Those were all moved & hidden as implementation details of the kernel response unmarshaling.

Comment on lines 25 to 26
JsiiMethod string `json:"method"`
GoMethod string `json:"cookie"`
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Renamed for better go-local semantics.

Comment on lines 34 to 35
JsiiProperty string `json:"property"`
GoGetter string `json:"cookie"`
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Renamed for better go-local semantics.

@@ -1,20 +1,146 @@
package kernel
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file was changed to no longer actually expose a user-accessible API... The reason being there is no use for users to call Callbacks directly, as this feature is currently unused, and in any case should be hidden from end-user code, and entirely managed by the runtime library.

This now handles "inline callback requests" received as part of the standard request-response flow, and does not actually handle asynchronous methods anymore (this is unused - and would be re-introduced in the future if needed).

types typeregistry.TypeRegistry
objects map[reflect.Value]string
types *typeregistry.TypeRegistry
objects *objectstore.ObjectStore
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re-factored into separate module, as keying on reflect.Value resulted in bugs, because they compare == if their values are ==, regardless of whether they are distinct object instances... But this map actually should track individual instances (i.e: pointers).

# Conflicts:
#	packages/@jsii/go-runtime/go.sum
#	packages/@jsii/go-runtime/jsii-runtime-go/kernel/callbacks.go
#	packages/@jsii/go-runtime/jsii-runtime-go/kernel/client.go
#	packages/@jsii/go-runtime/jsii-runtime-go/kernel/conversions.go
#	packages/@jsii/go-runtime/jsii-runtime-go/typeregistry/registration.go
#	packages/@jsii/go-runtime/jsii-runtime-go/typeregistry/type-registry.go
Comment on lines 19 to 23
for fqn, members := range t.typeMembers {
iface := t.fqnToType[fqn]
if iface.Kind == classType || !vt.AssignableTo(iface.Type) {
continue
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might be expensive (I don't know). I decided to KISS for now, but we should be able to add a cache layer around this to mitigate the cost.

@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

  • CodeBuild project: AutoBuildProject89A8053A-6Jw05QLuWWwe
  • Commit ID: 44262db
  • Result: SUCCEEDED
  • Build Logs (available for 30 days)

Powered by github-codebuild-logs, available on the AWS Serverless Application Repository

# Conflicts:
#	packages/@jsii/go-runtime/jsii-runtime-go/internal/api/api.go
#	packages/@jsii/go-runtime/jsii-runtime-go/internal/kernel/callbacks.go
#	packages/@jsii/go-runtime/jsii-runtime-go/internal/kernel/client.go
#	packages/@jsii/go-runtime/jsii-runtime-go/internal/kernel/json.go
#	packages/@jsii/go-runtime/jsii-runtime-go/internal/typeregistry/registration.go
#	packages/@jsii/go-runtime/jsii-runtime-go/internal/typeregistry/type-registry.go
#	packages/@jsii/go-runtime/jsii-runtime-go/runtime.go
This was referenced Mar 17, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
contribution/core This is a PR that came from AWS. effort/large Large work item – several weeks of effort language/go Regarding GoLang bindings
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants