WebAssembly / WebAssembly/component-model

CABI: Add a `resource.consume` method for owned handles

Open
#238 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
WebAssembly
Stars
1.4k
Forks
130
Avg merge
2d 1h
Merged PRs (30d)
15

Description

Motivation

At the moment, there is no way to convert an owned resource handle inside the resource-exporting component back into an owned value of the type that implements the resource. While the specifics of performing this conversion can be left to wit-bindgen (see, e.g., https://github.com/bytecodealliance/wit-bindgen/issues/641#issuecomment-1686866481), it requires a canonical ABI primitive to consume an owned resource handle.

Detail

The canonical ABI defines a new method, resource.consume, which might be implemented as follows:

### `canon resource.consume`

def canon_resource_consume(inst, rt, i):
  # similar to resource.drop, but only for owned handles
  h = inst.handles.remove(rt, i)
  trap_if(!h.own)
  assert(h.scope is None)
  trap_if(h.lend_count != 0)
  trap_if(inst is not rt.impl and not rt.impl.may_enter)

  # note: do *not* call h's destructor

  # similar to resource.rep
  return h.rep

In particular, resource.consume, which has a (handwavy) signature of fn(owned_handle: i32) -> isize:

  • takes an owned handle
  • deallocates the handle, like resource.drop
  • does not call the resource's destructor, since this method may be used to reobtain ownership of the resource's implementing value
  • returns the handle's representation, like resource.rep

Closing Notes

Thank you for considering this extension to the canonical ABI :)

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reading the canonical ABI behavior of resource.drop and resource.rep, then review the linked wit-bindgen discussion for the proposed conversion use case. Done means the canonical ABI specifies resource.consume's ownership, lending, destructor, trapping, and representation semantics clearly enough for wit-bindgen to rely on.

Written by the indexing model from the issue text.

Assessment

Tech stack
wasm
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.