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

CUE and WebAssembly (Wasm) #2035

Open
myitcv opened this issue Oct 24, 2022 · 6 comments
Open

CUE and WebAssembly (Wasm) #2035

myitcv opened this issue Oct 24, 2022 · 6 comments
Assignees
Labels
NeedsDesign Functionality seems desirable, but not sure how it should look like.

Comments

@myitcv
Copy link
Member

myitcv commented Oct 24, 2022

This is an umbrella tracking issue for WebAssembly support in CUE.

Details to follow, but initial exploration is linked to #2007.

@4ad
Copy link
Contributor

4ad commented Oct 25, 2022

If you'd like to provide use cases and/or feedback on potential use of Wasm with CUE, please post to the discussion at #2045.

@4ad 4ad added the NeedsDesign Functionality seems desirable, but not sure how it should look like. label Feb 24, 2023
@4ad 4ad changed the title CUE and WebAssembly (WASM) CUE and WebAssembly (Wasm) Feb 24, 2023
cueckoo pushed a commit that referenced this issue May 17, 2023
Add a new package, cuelang.org/go/cue/interpreter/wasm that enables
Wasm support in CUE as an external interpreter. Code wishing to use
Wasm must use an `@extern("wasm")` package attribute. Individual
functions are imported from Wasm modules like so:

	add: _ @extern("foo.wasm", abi=c, sig="func(int64, int64): int64")
	mul: _ @extern("foo.wasm", abi=c, sig="func(float64, float64): float64")
	not: _ @extern("foo.wasm", abi=c, sig="func(bool): bool")

Where "foo.wasm" is a compiled Wasm module, and sig is the type
signature of the imported function.

So far, only the C ABI is supported, and only relatively few data
types can be exchanged with the Wasm module. Basically only fixed-sized
integers of any signness, fixed-sized floats, and booleans.

The Wasm module is instantiated in a sandbox, with no access to the
outside world. Users must ensure the functions exposed by the Wasm
modules are pure, that is, they always returns the same answer for
the same arguments. Functions may make use of global state for
memoization and other optimizations as long as purity is preserved
as viewed from the outside. Be aware that functions from the standard
libraries of many languages are often not pure.

Wasm is only enabled if the user explicitly imports
cuelang.org/go/cue/wasm, otherwise it is not available. The Go cue
package does not impart upon the user a dependency on the Wasm
runtime if Wasm is not explicitly requested.

Wasm is enabled and available in the command line tool.

Updates #2035.
Updates #2281.
Updates #2282.
Updates #2007.

Change-Id: I844ec1229ea465dfeca45bc54006e87ed8ef0460
Signed-off-by: Aram Hăvărneanu <aram@mgk.ro>
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/551738
Unity-Result: CUEcueckoo <cueckoo+gerrithub@cuelang.org>
TryBot-Result: CUEcueckoo <cueckoo+gerrithub@cuelang.org>
Reviewed-by: Marcel van Lohuizen <mpvl@gmail.com>
Reviewed-by: Roger Peppe <rogpeppe@gmail.com>
@myitcv myitcv added zGarden and removed zGarden labels Jun 13, 2023
@myitcv
Copy link
Member Author

myitcv commented Sep 15, 2023

For folks following along here, apologies we forgot to add notice of the CUE + Web Assembly Focus Session Call. It was well advertised elsewhere, but we failed to join the dots here.

cueckoo pushed a commit that referenced this issue Jan 8, 2024
…turn structs

Add support for structs in Wasm functions. A function declares that
is uses or returns a struct by referring to a CUE struct (defined
elsewhere) in its type signature. The CUE struct is a straightforward
translation of the native struct type with native scalars replaced
by their CUE counterpats (e.g. u32 in Rust becomes uint32 in CUE,
int16_t in C becomes int16 in CUE, etc). The order of fields in the
CUE struct must match that of the target language.

Only structs that contain scalars or other supported structs are
currently supported. Struct fields that are pointers in the target
language are not currently supported. This may change.

In order to support structs CUE needs access to the memory allocator
of the guest program. For this, the guest must export two Wasm
functions with the following C type signatures:

	void*	allocate(int n);
	void	deallocate(void *ptr, int n);

Allocate returns a Wasm pointer to a buffer of size n. Deallocate
takes a Wasm pointer and the size of the buffer it points to and
frees it.

Updates #2035.
Updates #2281.

Change-Id: Ic509448226b22d2a54b70d565db6c702b801a839
Signed-off-by: Aram Hăvărneanu <aram@mgk.ro>
cueckoo pushed a commit that referenced this issue Jan 8, 2024
…turn structs

Add support for structs in Wasm functions. A function declares that
is uses or returns a struct by referring to a CUE struct (defined
elsewhere) in its type signature. The CUE struct is a straightforward
translation of the native struct type with native scalars replaced
by their CUE counterpats (e.g. u32 in Rust becomes uint32 in CUE,
int16_t in C becomes int16 in CUE, etc). The order of fields in the
CUE struct must match that of the target language.

Only structs that contain scalars or other supported structs are
currently supported. Struct fields that are pointers in the target
language are not currently supported. This may change.

In order to support structs CUE needs access to the memory allocator
of the guest program. For this, the guest must export two Wasm
functions with the following C type signatures:

	void*	allocate(int n);
	void	deallocate(void *ptr, int n);

Allocate returns a Wasm pointer to a buffer of size n. Deallocate
takes a Wasm pointer and the size of the buffer it points to and
frees it.

Updates #2035.
Updates #2281.

Change-Id: Ic509448226b22d2a54b70d565db6c702b801a839
Signed-off-by: Aram Hăvărneanu <aram@mgk.ro>
cueckoo pushed a commit that referenced this issue Jan 16, 2024
…turn structs

Add support for structs in Wasm functions. A function declares that
is uses or returns a struct by referring to a CUE struct (defined
elsewhere) in its type signature. The CUE struct is a straightforward
translation of the native struct type with native scalars replaced
by their CUE counterpats (e.g. u32 in Rust becomes uint32 in CUE,
int16_t in C becomes int16 in CUE, etc). The order of fields in the
CUE struct must match that of the target language.

Only structs that contain scalars or other supported structs are
currently supported. Struct fields that are pointers in the target
language are not currently supported. This may change.

In order to support structs CUE needs access to the memory allocator
of the guest program. For this, the guest must export two Wasm
functions with the following C type signatures:

	void*	allocate(int n);
	void	deallocate(void *ptr, int n);

Allocate returns a Wasm pointer to a buffer of size n. Deallocate
takes a Wasm pointer and the size of the buffer it points to and
frees it.

Updates #2035.
Updates #2281.

Change-Id: Ic509448226b22d2a54b70d565db6c702b801a839
Signed-off-by: Aram Hăvărneanu <aram@mgk.ro>
cueckoo pushed a commit that referenced this issue Jan 17, 2024
…turn structs

Add support for structs in Wasm functions. A function declares that
is uses or returns a struct by referring to a CUE struct (defined
elsewhere) in its type signature. The CUE struct is a straightforward
translation of the native struct type with native scalars replaced
by their CUE counterpats (e.g. u32 in Rust becomes uint32 in CUE,
int16_t in C becomes int16 in CUE, etc). The order of fields in the
CUE struct must match that of the target language.

Only structs that contain scalars or other supported structs are
currently supported. Struct fields that are pointers in the target
language are not currently supported. This may change.

In order to support structs CUE needs access to the memory allocator
of the guest program. For this, the guest must export two Wasm
functions with the following C type signatures:

	void*	allocate(int n);
	void	deallocate(void *ptr, int n);

Allocate returns a Wasm pointer to a buffer of size n. Deallocate
takes a Wasm pointer and the size of the buffer it points to and
frees it.

Updates #2035.
Updates #2281.

Change-Id: Ic509448226b22d2a54b70d565db6c702b801a839
Signed-off-by: Aram Hăvărneanu <aram@mgk.ro>
cueckoo pushed a commit that referenced this issue Jan 18, 2024
…turn structs

Add support for structs in Wasm functions. A function declares that
is uses or returns a struct by referring to a CUE struct (defined
elsewhere) in its type signature. The CUE struct is a straightforward
translation of the native struct type with native scalars replaced
by their CUE counterpats (e.g. u32 in Rust becomes uint32 in CUE,
int16_t in C becomes int16 in CUE, etc). The order of fields in the
CUE struct must match that of the target language.

Only structs that contain scalars or other supported structs are
currently supported. Struct fields that are pointers in the target
language are not currently supported. This may change.

In order to support structs CUE needs access to the memory allocator
of the guest program. For this, the guest must export two Wasm
functions with the following C type signatures:

	void*	allocate(int n);
	void	deallocate(void *ptr, int n);

Allocate returns a Wasm pointer to a buffer of size n. Deallocate
takes a Wasm pointer and the size of the buffer it points to and
frees it.

Updates #2035.
Updates #2281.

Change-Id: Ic509448226b22d2a54b70d565db6c702b801a839
Signed-off-by: Aram Hăvărneanu <aram@mgk.ro>
cueckoo pushed a commit that referenced this issue Jan 18, 2024
…turn structs

Add support for structs in Wasm functions. A function declares that
is uses or returns a struct by referring to a CUE struct (defined
elsewhere) in its type signature. The CUE struct is a straightforward
translation of the native struct type with native scalars replaced
by their CUE counterpats (e.g. u32 in Rust becomes uint32 in CUE,
int16_t in C becomes int16 in CUE, etc). The order of fields in the
CUE struct must match that of the target language.

Only structs that contain scalars or other supported structs are
currently supported. Struct fields that are pointers in the target
language are not currently supported. This may change.

In order to support structs CUE needs access to the memory allocator
of the guest program. For this, the guest must export two Wasm
functions with the following C type signatures:

	void*	allocate(int n);
	void	deallocate(void *ptr, int n);

Allocate returns a Wasm pointer to a buffer of size n. Deallocate
takes a Wasm pointer and the size of the buffer it points to and
frees it.

Updates #2035.
Updates #2281.

Change-Id: Ic509448226b22d2a54b70d565db6c702b801a839
Signed-off-by: Aram Hăvărneanu <aram@mgk.ro>
cueckoo pushed a commit that referenced this issue Feb 12, 2024
…turn structs

Add support for structs in Wasm functions. A function declares that
is uses or returns a struct by referring to a CUE struct (defined
elsewhere) in its type signature. The CUE struct is a straightforward
translation of the native struct type with native scalars replaced
by their CUE counterpats (e.g. u32 in Rust becomes uint32 in CUE,
int16_t in C becomes int16 in CUE, etc). The order of fields in the
CUE struct must match that of the target language.

Only structs that contain scalars or other supported structs are
currently supported. Struct fields that are pointers in the target
language are not currently supported. This may change.

In order to support structs CUE needs access to the memory allocator
of the guest program. For this, the guest must export two Wasm
functions with the following C type signatures:

	void*	allocate(int n);
	void	deallocate(void *ptr, int n);

Allocate returns a Wasm pointer to a buffer of size n. Deallocate
takes a Wasm pointer and the size of the buffer it points to and
frees it.

Updates #2035.
Updates #2281.

Change-Id: Ic509448226b22d2a54b70d565db6c702b801a839
Signed-off-by: Aram Hăvărneanu <aram@mgk.ro>
cueckoo pushed a commit that referenced this issue Feb 12, 2024
…turn structs

Add support for structs in Wasm functions. A function declares that
is uses or returns a struct by referring to a CUE struct (defined
elsewhere) in its type signature. The CUE struct is a straightforward
translation of the native struct type with native scalars replaced
by their CUE counterpats (e.g. u32 in Rust becomes uint32 in CUE,
int16_t in C becomes int16 in CUE, etc). The order of fields in the
CUE struct must match that of the target language.

Only structs that contain scalars or other supported structs are
currently supported. Struct fields that are pointers in the target
language are not currently supported. This may change.

In order to support structs CUE needs access to the memory allocator
of the guest program. For this, the guest must export two Wasm
functions with the following C type signatures:

	void*	allocate(int n);
	void	deallocate(void *ptr, int n);

Allocate returns a Wasm pointer to a buffer of size n. Deallocate
takes a Wasm pointer and the size of the buffer it points to and
frees it.

Updates #2035.
Updates #2281.

Change-Id: Ic509448226b22d2a54b70d565db6c702b801a839
Signed-off-by: Aram Hăvărneanu <aram@mgk.ro>
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1173964
TryBot-Result: CUEcueckoo <cueckoo@gmail.com>
Unity-Result: CUE porcuepine <cue.porcuepine@gmail.com>
Reviewed-by: Roger Peppe <rogpeppe@gmail.com>
Reviewed-by: Marcel van Lohuizen <mpvl@gmail.com>
@4ad
Copy link
Contributor

4ad commented May 15, 2024

As we discussed in several community calls, we're pausing Wasm developement until the Wasm ecosystem matures, in particular, we're waiting for the Component Model.

@AndrewSav
Copy link

Will there be an alternate way to do custom functions?

@4ad
Copy link
Contributor

4ad commented May 16, 2024

We are open to the idea, although we don't have a concrete design in mind yet. Care to share an experience report, or a simple feature request?

@yoktobit
Copy link

yoktobit commented Oct 6, 2024

Would using an abstraction layer like extism help for integration of wasm? Then cue using wasm/wasi/wasipx wouldn't depend too much on their versions but more on the implementation in extism. https://github.com/extism/go-sdk

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
NeedsDesign Functionality seems desirable, but not sure how it should look like.
Projects
None yet
Development

No branches or pull requests

4 participants