
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
51 строка
1,4 КиБ
Go
51 строка
1,4 КиБ
Go
//go:build wasm && !wasi
|
|
// +build wasm,!wasi
|
|
|
|
package runtime
|
|
|
|
import "unsafe"
|
|
|
|
type timeUnit float64 // time in milliseconds, just like Date.now() in JavaScript
|
|
|
|
// wasmNested is used to detect scheduler nesting (WASM calls into JS calls back into WASM).
|
|
// When this happens, we need to use a reduced version of the scheduler.
|
|
var wasmNested bool
|
|
|
|
//export _start
|
|
func _start() {
|
|
// These need to be initialized early so that the heap can be initialized.
|
|
heapStart = uintptr(unsafe.Pointer(&heapStartSymbol))
|
|
heapEnd = uintptr(wasm_memory_size(0) * wasmPageSize)
|
|
|
|
wasmNested = true
|
|
run()
|
|
wasmNested = false
|
|
}
|
|
|
|
var handleEvent func()
|
|
|
|
//go:linkname setEventHandler syscall/js.setEventHandler
|
|
func setEventHandler(fn func()) {
|
|
handleEvent = fn
|
|
}
|
|
|
|
func ticksToNanoseconds(ticks timeUnit) int64 {
|
|
// The JavaScript API works in float64 milliseconds, so convert to
|
|
// nanoseconds first before converting to a timeUnit (which is a float64),
|
|
// to avoid precision loss.
|
|
return int64(ticks * 1e6)
|
|
}
|
|
|
|
func nanosecondsToTicks(ns int64) timeUnit {
|
|
// The JavaScript API works in float64 milliseconds, so convert to timeUnit
|
|
// (which is a float64) first before dividing, to avoid precision loss.
|
|
return timeUnit(ns) / 1e6
|
|
}
|
|
|
|
// This function is called by the scheduler.
|
|
// Schedule a call to runtime.scheduler, do not actually sleep.
|
|
//export runtime.sleepTicks
|
|
func sleepTicks(d timeUnit)
|
|
|
|
//export runtime.ticks
|
|
func ticks() timeUnit
|