Skip to content

Commit 51290e5

Browse files
aykevldeadprogram
authored andcommitted
wasm: support -scheduler=none
Previously, -scheduler=none wasn't possible for WASM targets: $ tinygo run -target=wasm -scheduler=none ./testdata/stdlib.go src/runtime/runtime_wasm_js.go:34:2: attempted to start a goroutine without a scheduler With this commit, it works just fine: $ tinygo run -target=wasm -scheduler=none ./testdata/stdlib.go stdin: /dev/stdin stdout: /dev/stdout stderr: /dev/stderr pseudorandom number: 1298498081 strings.IndexByte: 2 strings.Replace: An-example-string Supporting `-scheduler=none` has some benefits: * it reduces file size a lot compared to having a scheduler * it allows JavaScript to call exported functions
1 parent 22c35c8 commit 51290e5

File tree

3 files changed

+33
-29
lines changed

3 files changed

+33
-29
lines changed

main_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ func runPlatTests(options compileopts.Options, tests []string, t *testing.T) {
164164
t.Parallel()
165165
runTest("env.go", options, t, []string{"first", "second"}, []string{"ENV1=VALUE1", "ENV2=VALUE2"})
166166
})
167-
if options.Target == "wasi" {
167+
if options.Target == "wasi" || options.Target == "wasm" {
168168
t.Run("alias.go-scheduler-none", func(t *testing.T) {
169169
t.Parallel()
170170
options := compileopts.Options(options)

src/runtime/runtime_wasm_js.go

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -29,34 +29,6 @@ func setEventHandler(fn func()) {
2929
handleEvent = fn
3030
}
3131

32-
//export resume
33-
func resume() {
34-
go func() {
35-
handleEvent()
36-
}()
37-
38-
if wasmNested {
39-
minSched()
40-
return
41-
}
42-
43-
wasmNested = true
44-
scheduler()
45-
wasmNested = false
46-
}
47-
48-
//export go_scheduler
49-
func go_scheduler() {
50-
if wasmNested {
51-
minSched()
52-
return
53-
}
54-
55-
wasmNested = true
56-
scheduler()
57-
wasmNested = false
58-
}
59-
6032
func ticksToNanoseconds(ticks timeUnit) int64 {
6133
// The JavaScript API works in float64 milliseconds, so convert to
6234
// nanoseconds first before converting to a timeUnit (which is a float64),
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
//go:build wasm && !wasi && !scheduler.none
2+
// +build wasm,!wasi,!scheduler.none
3+
4+
package runtime
5+
6+
//export resume
7+
func resume() {
8+
go func() {
9+
handleEvent()
10+
}()
11+
12+
if wasmNested {
13+
minSched()
14+
return
15+
}
16+
17+
wasmNested = true
18+
scheduler()
19+
wasmNested = false
20+
}
21+
22+
//export go_scheduler
23+
func go_scheduler() {
24+
if wasmNested {
25+
minSched()
26+
return
27+
}
28+
29+
wasmNested = true
30+
scheduler()
31+
wasmNested = false
32+
}

0 commit comments

Comments
 (0)