Closed fgm closed 1 month ago
Thanks, @fgm - I've copied in @4ad
Hmm, can you try without using CUE modules?
To answer your ancillary questions more specifically:
I am following this Rust path to evaluate WASM integration, but if I have to use WASM, I would prefer to use gc or tinygo because the functions I need to use are written in Go, and it would avoid having to rewrite them in Rust
Gc is impossible to use because it can't export functions yet (see golang/go/#65199).
Tinygo is not guaranteed to work because it doesn't have a stable, or a standard ABI. Realistically only C and Rust can be expected to work at this point.
Additionally, there is no support for strings yet, which I imagine you might need.
- I am exploring this WASM solution because the current mechanism is to
- fork cue
- add our functions in a
net2
Go package- add the package to
pkg/packages.txt
go generate ./pkg
- add that package to
pkg/register.go
until registration becomes automatic autogenerated as in theTODO
ingen.go#24
go build -o cue08 cuelang.org/go/cmd/cue
- We would very much prefer a dynamic solution that does not require maintaining a Cue fork
Do you need to use the CUE command line tool, or would a Go API to inject additional functions suffice? We could potentially make it easy to add functions via the API, but of course, if you need to use the command line tool rather than the API this would not help you.
@4ad Thanks for answering so fast. I get the same behavior if I remove the module (cue.mod/
), that was how I initially did it, actually : I only added the cue.mod/
to check if that fixed things.
Using the CUE CLI would be best, but using a Go API to inject additional functions might probably be sufficient: the devs in that team mostly use the CLI to debug their configurations, but the actual logic using the validations is in custom Go code, so that should not be a major issue.
I noticed you mention C and Rust: AIUI gccgo can work with the C ABI : is that correct and could that be a solution ?
I might be wrong, but I don't believe gccgo supports Wasm at all.
Additionally, the "C ABI" for Wasm is not defined anywhere really, so every compiler does something slightly different. Sometimes even the same compiler can do something different (see wasm32-wasi
vs. wasm32-unknown-unknown
above). That's the problem with Wasm, really, and the reason why we want to move to the Component Model as soon as it is feasible.
OK, so it would be either the existing fork-based solution (short term), or a way to add custom functions in Go for custom apps (second best, if you think that is something which is easily doable), or WASM from Rust once we figure out why I cannot run it yet, then.
Is there an existing issue I could look-at/help-with for the second solution ?
OK, so I found the problem: i was running, as shown: cue eval wasmer.cue
or cue08 eval wasmer.cue
, and got the errrors above.
Running simply cue eval
, cue08 eval
, cue eval .
, or cue08 eval .
there is no problem, the code works as expected. That was similar to running a single Go file vs running a package.
I found the info by dissecting what basic.txtar
actually did.
Ah, yes, that makes perfect sense.
FWIW, -buildmode=c-shared i
was just accepted in https://github.com/golang/go/issues/65199 so it's now only a matter of time before Go can export functions. So maybe that will mean the ability to use Go-sourced WASM for functions in some weeks/months.
Kind of. The Go compiler does not use the C ABI, so we will need to add a Go-specific ABI, it wouldn't Just Work. But yes.
Hypothetically, any idea of the amount of work it would take should I try to build this to Cue ?
The problem is that the only way to get the memory layout of Go structs is via the -asmhdr
flag of the Go compiler. Since we need this information it would most likely mean we need to compile the Go code on the user's behalf, since we can't expect the user to provide us with this information.
We also need a way for this information to find its way into CUE. Perhaps we could add it to CUE struct fields as an attribute. Of course we don't want users to have to write this by hand, so we would need some sort of converter from the -asmhdr
output to CUE.
On a technical side of things, making use of this information (once we have it in CUE) is probably quite easy. The more difficult part is making an ergonomic UI/UX around it for users, and plumbing all the pieces together.
In other words the difficulty lies in designing the feature rather than implementing it.
@4ad you just marked this as completed : is it just routine maintenance, or any specific change ? Related to #2035 AFAICS.
Apologies, I wrote a message but it somehow got erased when I closed the issue.
This wasn't really a bug as Wasm was intended to be used with full packages and not with individual files.
But we have removed Wasm support from the CUE command itself (API support still present), so this issue doesn't even serve as a feature request for the forseeable future. We're waiting for Component Model support and when (if) that will arrive it will necessarily have a different UX in CUE.
What version of CUE are you using (
cue version
)?Does this issue reproduce with the latest stable release?
Yes, but slightly differently.
What did you do?
Cargo.toml
is this:[dependencies]
[lib] crate-type = ["cdylib"]
cue eval wasmer.cue
, with no flags and various flags combinations, both with thecue
(current stable) andcue08
(master HEAD) binarycue eval wasmer.cue hello.wasm
, with no flags and various flags combinations, both with thecue
(current stable) andcue08
(master HEAD) binaryunknown file extension .wasm
sudo fs_usage -f pathname cue08
in another window to trace file system callsWhat did you expect to see?
fs_usage
output, at least a stat or open to the filehello.wasm
possibly at an incorrect path to explain the errorsWhat did you see instead?
fs_usage
window, after the prologue:Showing there was no attempt to stat or open the
hello.wasm
file.What more did you do ?
I ran
cue
master HEAD under debugger, and found out:@extern("hello.wasm")
imports happen inwasm.findFile
from the list of resolved files in the was minstance’sb.UnknownFiles
, which is empty.b.OrphanFiles
incmd.getDecoders
but that list is empty too.b.OrphanFiles
is populated inload.fp.add
but that function only ever receives thewasmer.cue
file and not thehellow.wasm
file, regardless of whether it was passed on the CLI.hello.wasm
is much larger than those in the test suite, at 170kB vs less that 1kB for the small ones and 15kB for the largest one. Possibly due to the presence of debug info, I'm not very familiar with Rust builds.Anything else ?
pkg/net
, especially regarding the mixed IPv4/IPv6 situations.net2
Go packagepkg/packages.txt
go generate ./pkg
pkg/register.go
until registration becomes automatic autogenerated as in theTODO
ingen.go#24
go build -o cue08 cuelang.org/go/cmd/cue