builtin_weakmap_test.go 557 B

12345678910111213141516171819202122232425262728293031323334
  1. package goja
  2. import (
  3. "runtime"
  4. "testing"
  5. )
  6. func TestWeakMapExpiry(t *testing.T) {
  7. vm := New()
  8. _, err := vm.RunString(`
  9. var m = new WeakMap();
  10. var key = {};
  11. m.set(key, true);
  12. if (!m.has(key)) {
  13. throw new Error("has");
  14. }
  15. if (m.get(key) !== true) {
  16. throw new Error("value does not match");
  17. }
  18. key = undefined;
  19. `)
  20. if err != nil {
  21. t.Fatal(err)
  22. }
  23. runtime.GC()
  24. runtime.GC()
  25. wmo := vm.Get("m").ToObject(vm).self.(*weakMapObject)
  26. wmo.m.Lock()
  27. l := len(wmo.m.data)
  28. wmo.m.Unlock()
  29. if l > 0 {
  30. t.Fatal("Object has not been removed")
  31. }
  32. }