Skip to content

Commit

Permalink
remove object early exits
Browse files Browse the repository at this point in the history
fix tests after removing early exit from objects

fix more tests
  • Loading branch information
nickpoindexter committed Jul 28, 2023
1 parent 10ed785 commit c4a8d72
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 60 deletions.
2 changes: 1 addition & 1 deletion builtin_map_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ func TestMapObjectMemUsage(t *testing.T) {
},
},
},
expected: 33,
expected: 60,
errExpected: errMemUsageExceedsLimitNil,
},
}
Expand Down
86 changes: 43 additions & 43 deletions memory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func TestMemCheck(t *testing.T) {
SizeEmpty + SizeEmpty + // outer object + reference to its prototype
(1 + SizeNumber) + // "a" and number
(1 + 4) + // "b" and string
(1 + SizeEmpty) + // "c" (object + prototype reference)
(1 + SizeEmpty + SizeEmpty) + // "c" (object + prototype reference)
SizeEmpty, // stack difference from popping null(8) and then adding outer obj(8) + "c" obj (8)
},
{
Expand Down Expand Up @@ -189,7 +189,7 @@ func TestMemCheck(t *testing.T) {
checkMem()
x[10] = "abc";
checkMem()`,
3, // 3 -> "abc" added to global memory | -1 difference on stack betwen "abc" and "abcd"
2, // 3 -> "abc" added to global memory | -1 difference on stack betwen "abc" and "abcd"
},
{
"sparse array with sparseArrayObject",
Expand All @@ -199,7 +199,7 @@ func TestMemCheck(t *testing.T) {
x[5001] = "abc";
checkMem()`,
SizeInt32 +
3, // 3 -> "abc" added to global memory | -1 difference on stack betwen "abc" and "abcd"
2, // 3 -> "abc" added to global memory | -1 difference on stack betwen "abc" and "abcd"
},
{
"array with non-numeric keys",
Expand All @@ -213,28 +213,28 @@ func TestMemCheck(t *testing.T) {
// len("abc") + len("a") + SizeNumber
3 + 1 + SizeNumber,
},
// {
// "reference to array",
// `x = []
// x[1] = "abcd";
// x[10] = "abc";
// checkMem()
// y = x;
// checkMem()`,
// // len("y") + reference to array
// 1 + SizeEmpty,
// },
// {
// "reference to sparse array",
// `x = []
// x[5000] = "abcb";
// x[5001] = "abc";
// checkMem()
// y = x;
// // len("y") + reference to array
// checkMem()`,
// 1 + SizeEmpty,
// },
{
"reference to array",
`x = []
x[1] = "abcd";
x[10] = "abc";
checkMem()
y = x;
checkMem()`,
// len("y") + reference to array
1 + SizeEmpty,
},
{
"reference to sparse array",
`x = []
x[5000] = "abcb";
x[5001] = "abc";
checkMem()
y = x;
// len("y") + reference to array
checkMem()`,
1 + SizeEmpty,
},
{
"Date object",
`
Expand Down Expand Up @@ -507,7 +507,7 @@ func TestMemArraysWithLenThreshold(t *testing.T) {
100,
memUsageLimit,
// len("j") + value 0 + Array overhead + estimate of property values
SizeEmpty + 200*SizeNumber,
1 + SizeNumber + SizeEmpty + 200*SizeNumber,
},
{
"mixed array under threshold",
Expand All @@ -521,22 +521,22 @@ func TestMemArraysWithLenThreshold(t *testing.T) {
200,
memUsageLimit,
// Array overhead, size of property values
SizeEmpty + (50 * SizeNumber) + (50 * SizeBool),
2 + SizeEmpty + SizeNumber + (50 * SizeNumber) + (50 * SizeBool),
},
{
"array under threshold but over limit",
`y = []
y.push([]);
checkMem();
for(i=0;i<10;i++){
y[0].push(true);
};
checkMem()`,
200,
40,
// Array overhead, size of property values, only 3 values before we hit the mem limit
2 + SizeEmpty + (3 * SizeBool),
},
// {
// "array under threshold but over limit",
// `y = []
// y.push([]);
// checkMem();
// for(i=0;i<10;i++){
// y[0].push(true);
// };
// checkMem()`,
// 200,
// 40,
// // Array overhead, size of property values, only 3 values before we hit the mem limit
// 2 + SizeEmpty + (3 * SizeBool),
// },
{
"mixed array over threshold",
`y = []
Expand All @@ -549,7 +549,7 @@ func TestMemArraysWithLenThreshold(t *testing.T) {
50,
memUsageLimit,
// Array overhead, size of property values
SizeEmpty + (50 * SizeNumber) + (50 * SizeBool),
2 + SizeEmpty + SizeNumber + (50 * SizeNumber) + (50 * SizeBool),
},
{
"mixed scattered array over threshold wcs",
Expand All @@ -563,7 +563,7 @@ func TestMemArraysWithLenThreshold(t *testing.T) {
50,
memUsageLimit,
// Array overhead, size of property values
SizeEmpty + (100 * SizeNumber),
2 + SizeEmpty + SizeNumber + (100 * SizeNumber),
},
} {
t.Run(fmt.Sprintf(tc.description), func(t *testing.T) {
Expand Down
4 changes: 0 additions & 4 deletions object.go
Original file line number Diff line number Diff line change
Expand Up @@ -1985,10 +1985,6 @@ func (o *baseObject) MemUsage(ctx *MemUsageContext) (uint64, error) {
if err != nil {
return total, err
}
//if we know that it has exceeded the threshold then we can just exit
if exceeded, err := ctx.MemUsageLimitExceeded(total); exceeded || err != nil {
return total, err
}
}
}

Expand Down
12 changes: 0 additions & 12 deletions runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -521,10 +521,6 @@ func (r *Runtime) MemUsage(ctx *MemUsageContext) (uint64, error) {
if err != nil {
return total, err
}
//if we know that it has exceeded the threshold then we can just exit
if exceeded, err := ctx.MemUsageLimitExceeded(total); exceeded || err != nil {
return total, err
}
}

for idx := range r.vm.callStack {
Expand All @@ -533,10 +529,6 @@ func (r *Runtime) MemUsage(ctx *MemUsageContext) (uint64, error) {
if err != nil {
return total, err
}
//if we know that it has exceeded the threshold then we can just exit
if exceeded, err := ctx.MemUsageLimitExceeded(total); exceeded || err != nil {
return total, err
}
}

if r.vm.stash != nil {
Expand All @@ -545,10 +537,6 @@ func (r *Runtime) MemUsage(ctx *MemUsageContext) (uint64, error) {
if err != nil {
return total, err
}
//if we know that it has exceeded the threshold then we can just exit
if exceeded, err := ctx.MemUsageLimitExceeded(total); exceeded || err != nil {
return total, err
}
}

if r.vm.stack != nil {
Expand Down

0 comments on commit c4a8d72

Please sign in to comment.