[go-nuts] GOOS=js/GOARCH=wasm

2021-07-27 Thread W. Michael Petullo
I have been building applications using the GOOS=js/GOARCH=wasm for around 
two years. I really like being able to write both client and server code in 
Go. I have four questions that I have not yet found a clear answer for:

(1) The syscall/js package is marked EXPERIMENTAL. Is there any consensus 
as to when this package might become stable?

(2) The rules surrounding Go routines are different from the expectations 
of Go itself. There is a comment in src/syscall/js/func.go concerning this:

// As a consequence, if one wrapped function blocks, JavaScript's event loop 
// is blocked until that function returns. Hence, calling any async 
JavaScript 
// API, which requires the event loop, like fetch (http.Client), will cause 
an 
// immediate deadlock. Therefore a blocking function should explicitly 
start a 
// new goroutine. 

Is this the expected behavior in the long term? I often get caught up in 
the rules here.

(3) I have not yet found a clean way to read a file from the browser (i.e., 
in response to a file HTML input). I presently use a JavaScript function 
that uses a FileReader to read the file into a global variable data before 
setting another global Boolean done to true. On the Go side, I poll the 
global Boolean done, waiting for it to become true before pulling the file 
out of the global data. Does anyone have an example of a better way to do 
this?

(4) I have a Wasm Go routine that updates a progress bar as it executes. 
However, the progress bar does not update until the Go routine completes. I 
suspect this is related to (2). Is there a pattern for getting this to work?

--
Mike

:wq

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/e568cb0c-c9a9-4155-ae30-ea29fb7f2ea9n%40googlegroups.com.


[go-nuts] WebAssembly and Goroutines

2020-11-17 Thread W. Michael Petullo
I am having trouble understanding the model for Goroutines within
WebAssembly.

I have a "click" callback with the following form:

errChan := make(chan error, 1)

go func() {
// Call http.PostForm and read response.

if err != nil {
errChan <- err
return
}

// ...
}()

select {
case err := <-errChan:
if err != nil {
// Handle error.
}
case <-time.After(timeout):
// Handle timeout.
}

I had expected http.PostForm to proceed, with the timeout expiring only
if http.PostForm took too long. Yet it seems that http.PostForm blocks
until after the timeout expires. Indeed, the http.PostForm call will not
return until the timer expires, no matter what value I set the timer to.

I setup the above callback (named fork) using:

wasm.AddEventListener(link, "click", js.FuncOf(
func(this js.Value, args []js.Value) interface{} {
return js.FuncOf(fork).Invoke(file)
}))

Does anyone have any recommended reading for performing HTTP requests
from WebAssembly callbacks?

-- 
Mike

:wq

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/20201117083647.GA254219%40imp.flyn.org.


Re: [go-nuts] Generating Mermaid graph in web assembly

2020-04-02 Thread W. Michael Petullo
>> I have been using Go to define programs that I compile to web assembly.
>> This works well in general, but I do not yet understanding how to express
>> mermaid graphs (https://mermaid-js.github.io/mermaid/) from Go.
>> [...]

> I don't know mermaid but it sounded interesting so I took a look, and,
> mermaid listens to the page load event and when fired (when the page
> has loaded) it will locate the graphs on the page and transform them to
> svg files.

> (https://mermaid-js.github.io/mermaid/#/usage)

> Presumably your code runs after the page load event, and mermaid has
> already done its thing by then. You would need to tell mermaid to look
> again for graphs after generating them.

> https://mermaid-js.github.io/mermaid/#/usage?id=calling-mermaidinit
> looks relevant.

> (Not really Go related, but sending to the list anyway for clarity.)

Exactly the insight I needed. I overlooked the timing of things,
and calling

js.Global().Get("mermaid").Call("init")

at the end of my web assembly code does indeed fix my problem.

Thank you for the quick response!

-- 
Mike

:wq

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/20200402181254.GC2193783%40imp.


[go-nuts] Generating Mermaid graph in web assembly

2020-04-02 Thread W. Michael Petullo
I have been using Go to define programs that I compile to web assembly.
This works well in general, but I do not yet understanding how to express
mermaid graphs (https://mermaid-js.github.io/mermaid/) from Go.

A hard-coded graph looks like this:


graph TD;
  A-->B;
  click A "https://www.flyn.org"; "FC";
  A-->C;
  B-->D;
  C-->D;


followed by including the Mermaid JavaScript.

In Go, I am doing this:

div := document.Call("createElement", "div")
div.Call("setAttribute", "class", "mermaid")

parent.Call("appendChild", div)
graph := document.Call("createTextNode", `
graph TD;
  A-->B;
  click A "https://www.flyn.org"; "FC"
  A-->C;
  B-->D;
  C-->D;`)

div.Call("appendChild", graph)

I also tried:

div.Set("innerHTML", `
graph TD;
  [...]`)

Neither of my approaches in Go seem to work. What is printed by the
browser is the graph's textual syntax rather than a rendered graph.
In the case of my hard-coded HTML example, the browser prints a rendered
graph.

As best as I can tell, the Mermaid syntax is whitespace/newline sensitive,
and I think Go or JavaScript might be changing the whitespace/newlines
from those in the graph literal string I provide.

Does anyone know how I should go about this? Am I missing something
obvious?

-- 
Mike

:wq

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/20200402155449.GA2192046%40imp.


[go-nuts] EAGAIN from Run is an os.PathError?

2020-03-28 Thread W. Michael Petullo
I am playing with Go and setrlimit(). Here is some code from a test 
that executes after setting RLIMIT_NPROC to 0:


err = exec.Command("true").Run()
	if e, ok := err.(*os.PathError); !ok || e.Err.(unix.Errno) != 
unix.EAGAIN {

t.Error(err.Error())
}

	/* Test succeeded: Run failed with EAGAIN as the process (thread) 
limit is exceeded. */


This seems to work, but I am surprised that Run() returns an 
os.PathError rather than an os.SyscallError. I would expect EAGAIN to 
be the latter.


Is this intentional? I guess I am looking for the reason for this so 
that my mind can more easily remember it.


Thank you!

--
Mike

:wq

--
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/20200328150650.E38C24E9%40smtp-auth.no-ip.com.


Re: [go-nuts] Go NaCl and C NaCl/libsodium

2019-07-14 Thread W. Michael Petullo
> Does anyone have any sample code which shows interoperability between Go's 
> golang.org/x/crypto/nacl/box and C's NaCl or libsodium? I have been having 
> trouble boxing with Go and opening with NaCl (and vice versa). I have used 
> NaCl from C a lot, but the Go NaCl packages are new to me. I am a little 
> unclear as to whether the Go routines are compatible with crypto_box_open 
> or libsodium's crypto_box_open_easy, for example.

After some experimentation, I found that the Go implementation is
indeed compatible with libsodium's "easy" API rather than DJB's original
NaCl API. I submitted an issue ticket requesting clarification in the
documentation:

https://github.com/golang/go/issues/33109

With some interest, I could probably write up a proposal and submit a
merge request. For now, my issue describes what I found, and it provides
example programs in Go and C.

-- 
Mike

:wq

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/20190714225222.GA19957%40imp.flyn.org.
For more options, visit https://groups.google.com/d/optout.