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

docs/guides: additional resources for WebAssembly #352

Merged
merged 1 commit into from
May 17, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions content/docs/guides/webassembly/resources.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
---
title: "Additional Resources"
weight: 4
description: |
Additional Resources for using TinyGo with WASM/WASI
---

Here are a few resources for helping learn more about using TinyGo with WebAssembly.

**Wasm By Example**
https://wasmbyexample.dev/

**Are We Wasm Yet ? - Part 1**
https://elewis.dev/are-we-wasm-yet-part-1

**Are We Wasm Yet ? - Part 2**
https://elewis.dev/are-we-wasm-yet-part-2

**Writing a WebAssembly Service in TinyGo for Wagi and Spin**
https://www.fermyon.com/blog/tinygo-webassembly-favicon-server

**wazero - TinyGo**
https://wazero.io/languages/tinygo/
Comment on lines +22 to +23
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one contains some outdated information on malloc and free. I'm not sure we should encourage people reading this? (Or perhaps send a message to the blog post writers to update?)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or perhaps send a message to the blog post writers to update?

That for sure! cc @codefromthecrypt

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the jury is still out on the malloc/free stuff, but coming in soon. We need to see benchmark before after and it is nearly there.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

basically there are a lot of people using the existing approach (which was reviewed by several) we owe it when doing an update to say what the new is and what it will cost you tetratelabs/wazero#1390

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@codefromthecrypt I'm not sure what you mean? malloc and free has been safe for use since tinygo-org/tinygo#3148 and I doubt we'll ever change that back.
A hand rolled malloc will have a much bigger chance of being subtly wrong.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@aykevl Thanks. We we were missing the context

What you said "malloc and free has been safe for use since tinygo-org/tinygo#3148 and I doubt we'll ever change that back." should be in the PR desc as rationale to use the CGO as the only way and remove the others, especially as the benchmarks don't show any perf regression. The same PR can update the site docs to only say one way (CGO malloc/free). @lburgazzoli can you update tetratelabs/wazero#1390 accordingly? Once that's in, I'll we can remove the manual exports from tinymem, leaving it only used for slice helper functions.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What you said "malloc and free has been safe for use since tinygo-org/tinygo#3148 and I doubt we'll ever change that back." should be in the PR desc as rationale to use the CGO as the only way and remove the others, especially as the benchmarks don't show any perf regression.

@codefromthecrypt I'm not sure I follow. Was this a reply to me, or which PR are you referring to?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lburgazzoli can you update tetratelabs/wazero#1390 accordingly?

I think @codefromthecrypt was asking @lburgazzoli to update tetratelabs/wazero#1390 accordingly. 😸


**WasmEdge Runtime - Go**
https://wasmedge.org/book/en/write_wasm/go.html

**WASI and Node.js**
https://k33g.hashnode.dev/series/wasi-nodejs

**Wazero, first steps**
https://k33g.hashnode.dev/series/wazero-first-steps
Comment on lines +31 to +32
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some things I noticed:

// readBufferFromMemory returns a buffer from WebAssembly
func readBufferFromMemory(bufferPosition *uint32, length uint32) []byte {
    subjectBuffer := make([]byte, length)
    pointer := uintptr(unsafe.Pointer(bufferPosition))
    for i := 0; i < int(length); i++ {
        s := *(*int32)(unsafe.Pointer(pointer + uintptr(i)))
        subjectBuffer[i] = byte(s)
    }
    return subjectBuffer
}

This is one example where unsafe.Slice would be perfect (although I'm not sure about the uint32 vs byte conversion, or what is supposed to be happening there):

// readBufferFromMemory returns a buffer from WebAssembly
func readBufferFromMemory(bufferPosition *byte, wordLength uint32) []byte {
    length := wordLength * 4
    subjectBuffer := make([]byte, length)
    copy(subjectBuffer, unsafe.Slice(bufferPosition, length))
    return subjectBuffer
}