forked from dop251/goja
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Changed WeakMap implementation to avoid memory leaks in some common u…
…sage scenarios. Fixes dop251#250.
- Loading branch information
Showing
8 changed files
with
80 additions
and
283 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,38 @@ | ||
package goja | ||
|
||
import ( | ||
"runtime" | ||
"testing" | ||
) | ||
|
||
func TestWeakMapExpiry(t *testing.T) { | ||
func TestWeakMap(t *testing.T) { | ||
vm := New() | ||
_, err := vm.RunString(` | ||
var m = new WeakMap(); | ||
var m1 = new WeakMap(); | ||
var key = {}; | ||
m.set(key, true); | ||
m1.set(key, false); | ||
if (!m.has(key)) { | ||
throw new Error("has"); | ||
} | ||
if (m.get(key) !== true) { | ||
throw new Error("value does not match"); | ||
} | ||
key = undefined; | ||
if (!m1.has(key)) { | ||
throw new Error("has (m1)"); | ||
} | ||
if (m1.get(key) !== false) { | ||
throw new Error("m1 value does not match"); | ||
} | ||
m.delete(key); | ||
if (m.has(key)) { | ||
throw new Error("m still has after delete"); | ||
} | ||
if (!m1.has(key)) { | ||
throw new Error("m1 does not have after delete from m"); | ||
} | ||
`) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
runtime.GC() | ||
runtime.GC() | ||
vm.RunString("true") // this will trigger dead keys removal | ||
wmo := vm.Get("m").ToObject(vm).self.(*weakMapObject) | ||
l := len(wmo.m.data) | ||
if l > 0 { | ||
t.Fatal("Object has not been removed") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.