WebAssembly / WebAssembly/component-model

First-class functions/closures as resources

Open
#278 4 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

I was wandering, now that resources/ownership/borrowing/etc are properly part of the component model; In what way are closures not "just" single-method resources? Can they piggy back off of the semantics and restrictions defined for resources?


For example, given the imaginary interface (& syntax):

interface A {
	register-event-handler: func(handler: fn(event));
	map: func(items: list<string>, mapper: borrow<fn(string) -> string>) -> list<string>;
}

Assuming that the following resource types are (automatically?) defined somewhere

resource fn-event { // fn(event)
	call: func(a1: event);
}

resource fn-string-string { // fn(string) -> string
	call: func(a1: string) -> string;
}

how is the first example different than this:

interface A {
	register-event-handler: func(handler: fn-event);
		// Fn is passed as "owned" handle. Ie. it's the responsibility of the
		// implementation of `A` to properly drop the resource/closure.
		// On the flip side, `A` can call `handler` as much as it wants even
		// after `register-event-handler` has returned.

	map: func(items: list<string>, mapper: borrow<fn-string-string>) -> list<string>;
		// Fn is borrowed. Ie. `mapper` may only be called during the
		// invocation of `map`, and must be dropped before returning.
}

?


By (re/ab)using resource semantics, this would keep function "references" in the realm of acyclic dependencies a.k.a. "no global GC required", right?

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

The issue names no files, tests, or entry points. Start by reviewing the component model's existing resource, ownership, and borrowing semantics, then compare them with the proposed closure examples. Done would require a settled design decision on whether closures can use resource semantics and what restrictions apply.

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
Needs clarification
Newbie friendliness
18/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.