feat: initial resources implementation #128
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Adds support for resources to jco.
There are two main resource cases: host-defined resources and component-defined resources. Host-defined resources are referred to as imported resources.
When lifting and lowering resources, for host-defined resources we capture the resource in the handle itself when assigning the handle index. For component-defined resources we maintain a handle relation via a private
resourceHandleSymbol
brand for the proxy object created back to the handle.Component-defined resource "proxy objects" have finalizers attached, finalizers are also unattached when the proxy object lifecycle ends. The private handle symbol is set to null to indicate a dead object so that a lifecycle error instead of an invalid handle lookup can be thrown if they are still used. This is done after all calls to JS that take borrowed resources from the component. Host-defined resources are captured and released so that we do not pin the GC longer than necessary. Own resources are released from import resource instance capture when lifted as that implies they are being transferred to the host code.
Finally, the distinction between the transpilation bindgen and function bindgen is maintained by taking a
resource_map
into the function bindgen, which provides a mapping from the resource type into its unique table id that can be used for the resource table objects -handleTable{i}
,resourceImportInstances{i}
, etc. under the assumption that these locals will be present. This allows the separation of instancing concerns from the transpilation bindgen and function bindgen, where only the transpilation bindgen needs to figure out the correct instancing maps (n tables for a resource used by n components).