Skip to content

Commit

Permalink
feat: add early exit to global object in runtime
Browse files Browse the repository at this point in the history
  • Loading branch information
Gabri3l committed Jan 9, 2024
1 parent a4d8244 commit 165184e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
3 changes: 3 additions & 0 deletions runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,9 @@ func (r *Runtime) MemUsage(ctx *MemUsageContext) (memUsage uint64, newMemUsage u
if err != nil {
return memUsage, newMemUsage, err
}
if exceeded := ctx.MemUsageLimitExceeded(memUsage); exceeded {
return memUsage, newMemUsage, nil
}
}

if r.vm == nil {
Expand Down
26 changes: 23 additions & 3 deletions runtime_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3087,14 +3087,34 @@ func TestRuntimeMemUsage(t *testing.T) {
name: "should account for globalObject given a runtime with non-empty globalObject",
val: &Runtime{
globalObject: &Object{
self: &baseObject{propNames: []unistring.String{"test"}, values: map[unistring.String]Value{"test": valueInt(99)}},
self: &baseObject{
propNames: []unistring.String{"test0", "test1", "test2"},
values: map[unistring.String]Value{"test0": valueInt(99), "test1": valueInt(99), "test2": valueInt(99)},
},
},
},
memLimit: 100,
// baseObject overhead + key/value pair with string overhead
expectedMem: SizeEmptyStruct + (4 + SizeString + SizeInt),
expectedMem: SizeEmptyStruct + (5+SizeString+SizeInt)*3,
// baseObject overhead + key/value pair with string overhead
expectedNewMem: SizeEmptyStruct + (4 + SizeString + SizeInt),
expectedNewMem: SizeEmptyStruct + (5+SizeString+SizeInt)*3,
errExpected: nil,
},
{
name: "should exit early given a runtime with non-empty globalObject exceeding the memory limit",
val: &Runtime{
globalObject: &Object{
self: &baseObject{
propNames: []unistring.String{"test0", "test1", "test2"},
values: map[unistring.String]Value{"test0": valueInt(99), "test1": valueInt(99), "test2": valueInt(99)},
},
},
},
memLimit: 0,
// baseObject overhead + key/value pair with string overhead
expectedMem: SizeEmptyStruct + (5 + SizeString + SizeInt),
// baseObject overhead + key/value pair with string overhead
expectedNewMem: SizeEmptyStruct + (5 + SizeString + SizeInt),
errExpected: nil,
},
{
Expand Down

0 comments on commit 165184e

Please sign in to comment.