wasm: backport "garbage collect references to JavaScript values"
See commit:
54e6ba6724
Warning: this will drop support for Go 1.13 for WebAssembly targets!
I have modified the integration tests to specifically blacklist Go 1.13
instead of whitelisting any other version, to avoid accidentally not
testing WebAssembly.
Этот коммит содержится в:
родитель
0f9038ad2e
коммит
5674c35e14
2 изменённых файлов: 44 добавлений и 17 удалений
19
main_test.go
19
main_test.go
|
@ -13,12 +13,14 @@ import (
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"runtime"
|
"runtime"
|
||||||
"sort"
|
"sort"
|
||||||
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/tinygo-org/tinygo/builder"
|
"github.com/tinygo-org/tinygo/builder"
|
||||||
"github.com/tinygo-org/tinygo/compileopts"
|
"github.com/tinygo-org/tinygo/compileopts"
|
||||||
|
"github.com/tinygo-org/tinygo/goenv"
|
||||||
)
|
)
|
||||||
|
|
||||||
const TESTDATA = "testdata"
|
const TESTDATA = "testdata"
|
||||||
|
@ -71,9 +73,20 @@ func TestCompiler(t *testing.T) {
|
||||||
t.Run("ARM64Linux", func(t *testing.T) {
|
t.Run("ARM64Linux", func(t *testing.T) {
|
||||||
runPlatTests("aarch64--linux-gnu", matches, t)
|
runPlatTests("aarch64--linux-gnu", matches, t)
|
||||||
})
|
})
|
||||||
t.Run("WebAssembly", func(t *testing.T) {
|
goVersion, err := builder.GorootVersionString(goenv.Get("GOROOT"))
|
||||||
runPlatTests("wasm", matches, t)
|
if err != nil {
|
||||||
})
|
t.Error("could not get Go version:", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
minorVersion := strings.Split(goVersion, ".")[1]
|
||||||
|
if minorVersion != "13" {
|
||||||
|
// WebAssembly tests fail on Go 1.13, so skip them there. Versions
|
||||||
|
// below that are also not supported but still seem to pass, so
|
||||||
|
// include them in the tests for now.
|
||||||
|
t.Run("WebAssembly", func(t *testing.T) {
|
||||||
|
runPlatTests("wasm", matches, t)
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -202,26 +202,31 @@
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
let ref = this._refs.get(v);
|
let id = this._ids.get(v);
|
||||||
if (ref === undefined) {
|
if (id === undefined) {
|
||||||
ref = this._values.length;
|
id = this._idPool.pop();
|
||||||
this._values.push(v);
|
if (id === undefined) {
|
||||||
this._refs.set(v, ref);
|
id = this._values.length;
|
||||||
|
}
|
||||||
|
this._values[id] = v;
|
||||||
|
this._goRefCounts[id] = 0;
|
||||||
|
this._ids.set(v, id);
|
||||||
}
|
}
|
||||||
let typeFlag = 0;
|
this._goRefCounts[id]++;
|
||||||
|
let typeFlag = 1;
|
||||||
switch (typeof v) {
|
switch (typeof v) {
|
||||||
case "string":
|
case "string":
|
||||||
typeFlag = 1;
|
|
||||||
break;
|
|
||||||
case "symbol":
|
|
||||||
typeFlag = 2;
|
typeFlag = 2;
|
||||||
break;
|
break;
|
||||||
case "function":
|
case "symbol":
|
||||||
typeFlag = 3;
|
typeFlag = 3;
|
||||||
break;
|
break;
|
||||||
|
case "function":
|
||||||
|
typeFlag = 4;
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
mem().setUint32(addr + 4, nanHead | typeFlag, true);
|
mem().setUint32(addr + 4, nanHead | typeFlag, true);
|
||||||
mem().setUint32(addr, ref, true);
|
mem().setUint32(addr, id, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
const loadSlice = (array, len, cap) => {
|
const loadSlice = (array, len, cap) => {
|
||||||
|
@ -284,6 +289,13 @@
|
||||||
setTimeout(this._inst.exports.go_scheduler, timeout);
|
setTimeout(this._inst.exports.go_scheduler, timeout);
|
||||||
},
|
},
|
||||||
|
|
||||||
|
// func finalizeRef(v ref)
|
||||||
|
"syscall/js.finalizeRef": (sp) => {
|
||||||
|
// Note: TinyGo does not support finalizers so this should never be
|
||||||
|
// called.
|
||||||
|
console.error('syscall/js.finalizeRef not implemented');
|
||||||
|
},
|
||||||
|
|
||||||
// func stringVal(value string) ref
|
// func stringVal(value string) ref
|
||||||
"syscall/js.stringVal": (ret_ptr, value_ptr, value_len) => {
|
"syscall/js.stringVal": (ret_ptr, value_ptr, value_len) => {
|
||||||
const s = loadString(value_ptr, value_len);
|
const s = loadString(value_ptr, value_len);
|
||||||
|
@ -405,7 +417,7 @@
|
||||||
|
|
||||||
async run(instance) {
|
async run(instance) {
|
||||||
this._inst = instance;
|
this._inst = instance;
|
||||||
this._values = [ // TODO: garbage collection
|
this._values = [ // JS values that Go currently has references to, indexed by reference id
|
||||||
NaN,
|
NaN,
|
||||||
0,
|
0,
|
||||||
null,
|
null,
|
||||||
|
@ -414,8 +426,10 @@
|
||||||
global,
|
global,
|
||||||
this,
|
this,
|
||||||
];
|
];
|
||||||
this._refs = new Map();
|
this._goRefCounts = []; // number of references that Go has to a JS value, indexed by reference id
|
||||||
this.exited = false;
|
this._ids = new Map(); // mapping from JS values to reference ids
|
||||||
|
this._idPool = []; // unused ids that have been garbage collected
|
||||||
|
this.exited = false; // whether the Go program has exited
|
||||||
|
|
||||||
const mem = new DataView(this._inst.exports.memory.buffer)
|
const mem = new DataView(this._inst.exports.memory.buffer)
|
||||||
|
|
||||||
|
|
Загрузка…
Создание таблицы
Сослаться в новой задаче