Skip to content

Commit

Permalink
Merge branch 'main' into sockets
Browse files Browse the repository at this point in the history
  • Loading branch information
syumai committed Oct 22, 2023
2 parents 7f46a93 + 3a8e631 commit f1469c0
Show file tree
Hide file tree
Showing 14 changed files with 136 additions and 104 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/pull_request.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ jobs:
ci:
strategy:
matrix:
go-version: [ 1.18, 1.19 ]
go-version: [ 1.21.3 ]
os: [ ubuntu-latest ]
runs-on: ${{ matrix.os }}
steps:
Expand Down
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,15 @@ First, please install the following tools:
* Node.js (and npm)
* [wrangler](https://developers.cloudflare.com/workers/wrangler/)
- You can install it by running `npm install -g wrangler`.
* tinygo
* tinygo 0.29.0 or later
* [gonew](https://pkg.go.dev/golang.org/x/tools/cmd/gonew)
- You can install it by running `go install golang.org/x/tools/cmd/gonew@latest`

After installation, please run the following commands.

```console
wrangler generate my-app syumai/workers/_templates/cloudflare/worker-tinygo
gonew github.com/syumai/workers/_templates/cloudflare/worker-tinygo your.module/my-app # e.g. github.com/syumai/my-app
cd my-app
go mod init
go mod tidy
make dev # start running dev server
curl http://localhost:8787/hello # outputs "Hello!"
Expand Down
2 changes: 1 addition & 1 deletion _templates/cloudflare/pages-tinygo/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ dev:

.PHONY: build
build:
go run github.com/syumai/workers/cmd/workers-assets-gen@latest
go run github.com/syumai/workers/cmd/workers-assets-gen@v0.18.0
tinygo build -o ./build/app.wasm -target wasm -no-debug ./...

.PHONY: deploy
Expand Down
14 changes: 11 additions & 3 deletions _templates/cloudflare/pages-tinygo/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,22 @@
- Node.js
- [wrangler](https://developers.cloudflare.com/workers/wrangler/)
- just run `npm install -g wrangler`
- tinygo
* tinygo 0.29.0 or later

## Getting Started

* If not already installed, please install the [gonew](https://pkg.go.dev/golang.org/x/tools/cmd/gonew) command.

```console
go install golang.org/x/tools/cmd/gonew@latest
```

* Create a new project using this template.
- Second argument passed to `gonew` is a module path of your new app.

```console
wrangler generate my-app syumai/workers/_templates/cloudflare/pages-tinygo
gonew github.com/syumai/workers/_templates/cloudflare/pages-tinygo your.module/my-app # e.g. github.com/syumai/my-app
cd my-app
go mod init
go mod tidy
make dev # start running dev server
curl http://localhost:8787/api/hello # outputs "Hello, Pages Functions!"
Expand Down
2 changes: 1 addition & 1 deletion _templates/cloudflare/pages-tinygo/go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module github.com/syumai/workers/_examples/pages-functions
module github.com/syumai/workers/_examples/pages-tinygo

go 1.18

Expand Down
2 changes: 1 addition & 1 deletion _templates/cloudflare/worker-go/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ dev:

.PHONY: build
build:
go run github.com/syumai/workers/cmd/workers-assets-gen@latest -mode=go
go run github.com/syumai/workers/cmd/workers-assets-gen@v0.18.0 -mode=go
GOOS=js GOARCH=wasm go build -o ./build/app.wasm .

.PHONY: deploy
Expand Down
14 changes: 11 additions & 3 deletions _templates/cloudflare/worker-go/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,22 @@
- Node.js
- [wrangler](https://developers.cloudflare.com/workers/wrangler/)
- just run `npm install -g wrangler`
- Go
- Go 1.21.0 or later

## Getting Started

* If not already installed, please install the [gonew](https://pkg.go.dev/golang.org/x/tools/cmd/gonew) command.

```console
go install golang.org/x/tools/cmd/gonew@latest
```

* Create a new project using this template.
- Second argument passed to `gonew` is a module path of your new app.

```console
wrangler generate my-app syumai/workers/_templates/cloudflare/worker-go
gonew github.com/syumai/workers/_templates/cloudflare/worker-go your.module/my-app # e.g. github.com/syumai/my-app
cd my-app
go mod init
go mod tidy
make dev # start running dev server
curl http://localhost:8787/hello # outputs "Hello!"
Expand Down
3 changes: 3 additions & 0 deletions _templates/cloudflare/worker-go/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/syumai/workers/_templates/cloudflare/worker-go

go 1.21.1
2 changes: 1 addition & 1 deletion _templates/cloudflare/worker-tinygo/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ dev:

.PHONY: build
build:
go run github.com/syumai/workers/cmd/workers-assets-gen@latest
go run github.com/syumai/workers/cmd/workers-assets-gen@v0.18.0
tinygo build -o ./build/app.wasm -target wasm -no-debug ./...

.PHONY: deploy
Expand Down
14 changes: 11 additions & 3 deletions _templates/cloudflare/worker-tinygo/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,22 @@
- Node.js
- [wrangler](https://developers.cloudflare.com/workers/wrangler/)
- just run `npm install -g wrangler`
- tinygo
- tinygo 0.29.0 or later

## Getting Started

* If not already installed, please install the [gonew](https://pkg.go.dev/golang.org/x/tools/cmd/gonew) command.

```console
go install golang.org/x/tools/cmd/gonew@latest
```

* Create a new project using this template.
- Second argument passed to `gonew` is a module path of your new app.

```console
wrangler generate my-app syumai/workers/_templates/cloudflare/worker-tinygo
gonew github.com/syumai/workers/_templates/cloudflare/worker-tinygo your.module/my-app # e.g. github.com/syumai/my-app
cd my-app
go mod init
go mod tidy
make dev # start running dev server
curl http://localhost:8787/hello # outputs "Hello!"
Expand Down
3 changes: 3 additions & 0 deletions _templates/cloudflare/worker-tinygo/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/syumai/workers/_templates/cloudflare/worker-tinygo

go 1.21.1
15 changes: 11 additions & 4 deletions cmd/workers-assets-gen/assets/wasm_exec_go.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
outputBuf += decoder.decode(buf);
const nl = outputBuf.lastIndexOf("\n");
if (nl != -1) {
console.log(outputBuf.substr(0, nl));
outputBuf = outputBuf.substr(nl + 1);
console.log(outputBuf.substring(0, nl));
outputBuf = outputBuf.substring(nl + 1);
}
return buf.length;
},
Expand Down Expand Up @@ -113,6 +113,10 @@
this.mem.setUint32(addr + 4, Math.floor(v / 4294967296), true);
}

const setInt32 = (addr, v) => {
this.mem.setUint32(addr + 0, v, true);
}

const getInt64 = (addr) => {
const low = this.mem.getUint32(addr + 0, true);
const high = this.mem.getInt32(addr + 4, true);
Expand Down Expand Up @@ -206,7 +210,10 @@

const timeOrigin = Date.now() - performance.now();
this.importObject = {
go: {
_gotest: {
add: (a, b) => a + b,
},
gojs: {
// Go's SP does not change as long as no Go code is running. Some operations (e.g. calls, getters and setters)
// may synchronously trigger a Go event handler. This makes Go code get executed in the middle of the imported
// function. A goroutine can switch to a new stack if the current stack is too small (see morestack function).
Expand Down Expand Up @@ -269,7 +276,7 @@
this._resume();
}
},
getInt64(sp + 8) + 1, // setTimeout has been seen to fire up to 1 millisecond early
getInt64(sp + 8),
));
this.mem.setInt32(sp + 16, id, true);
},
Expand Down
Loading

0 comments on commit f1469c0

Please sign in to comment.