Skip to content

Commit

Permalink
Proxy (#135)
Browse files Browse the repository at this point in the history
Proxy, Reflect, new.target, tons of refactoring.

Co-authored-by: noctarius <[email protected]>
  • Loading branch information
dop251 and noctarius authored Mar 24, 2020
1 parent d71b886 commit a13c43b
Show file tree
Hide file tree
Showing 53 changed files with 5,857 additions and 2,475 deletions.
515 changes: 306 additions & 209 deletions array.go

Large diffs are not rendered by default.

349 changes: 171 additions & 178 deletions array_sparse.go

Large diffs are not rendered by default.

83 changes: 77 additions & 6 deletions array_sparse_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package goja

import "testing"
import (
"testing"
)

func TestSparseArraySetLengthWithPropItems(t *testing.T) {
const SCRIPT = `
Expand All @@ -21,9 +23,18 @@ func TestSparseArraySetLengthWithPropItems(t *testing.T) {
}

func TestSparseArraySwitch(t *testing.T) {
const SCRIPT = `
vm := New()
_, err := vm.RunString(`
var a = [];
a[20470] = 5; // switch to sparse
a[20470] = 5; // switch to sparse`)
if err != nil {
t.Fatal(err)
}
a := vm.Get("a").(*Object)
if _, ok := a.self.(*sparseArrayObject); !ok {
t.Fatal("1: array is not sparse")
}
_, err = vm.RunString(`
var cutoffIdx = Math.round(20470 - 20470/8);
for (var i = a.length - 1; i >= cutoffIdx; i--) {
a[i] = i;
Expand All @@ -44,8 +55,14 @@ func TestSparseArraySwitch(t *testing.T) {
if (a[i] !== i) {
throw new Error("Invalid value at " + i + ": " + a[i]);
}
}`)
if err != nil {
t.Fatal(err)
}
if _, ok := a.self.(*arrayObject); !ok {
t.Fatal("2: array is not normal")
}
_, err = vm.RunString(`
// Now try to expand. Should stay a normal array
a[20471] = 20471;
if (a.length != 20472) {
Expand All @@ -62,8 +79,14 @@ func TestSparseArraySwitch(t *testing.T) {
if (a[i] !== i) {
throw new Error("Invalid value at " + i + ": " + a[i]);
}
}`)
if err != nil {
t.Fatal(err)
}
if _, ok := a.self.(*arrayObject); !ok {
t.Fatal("3: array is not normal")
}
_, err = vm.RunString(`
// Delete enough elements for it to become sparse again.
var cutoffIdx1 = Math.round(20472 - 20472/10);
for (var i = cutoffIdx; i < cutoffIdx1; i++) {
Expand Down Expand Up @@ -97,7 +120,55 @@ func TestSparseArraySwitch(t *testing.T) {
if (a[25590] !== 25590) {
throw new Error("Invalid value at 25590: " + a[25590]);
}
`)
if err != nil {
t.Fatal(err)
}
if _, ok := a.self.(*sparseArrayObject); !ok {
t.Fatal("4: array is not sparse")
}
}

func TestSparseArrayOwnKeys(t *testing.T) {
const SCRIPT = `
var a1 = [];
a1[500000] = 1;
var seen = false;
var count = 0;
var keys = Object.keys(a1);
keys.length === 1 && keys[0] === "500000";
`

testScript1(SCRIPT, valueTrue, t)
}

func TestSparseArrayEnumerate(t *testing.T) {
const SCRIPT = `
var a1 = [];
a1[500000] = 1;
var seen = false;
var count = 0;
for (var i in a1) {
if (i === "500000") {
if (seen) {
throw new Error("seen twice");
}
seen = true;
}
count++;
}
seen && count === 1;
`

testScript1(SCRIPT, valueTrue, t)
}

func TestArraySparseMaxLength(t *testing.T) {
const SCRIPT = `
var a = [];
a[4294967294]=1;
a.length === 4294967295 && a[4294967294] === 1;
`

testScript1(SCRIPT, _undefined, t)
testScript1(SCRIPT, valueTrue, t)
}
10 changes: 10 additions & 0 deletions ast/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,11 @@ type (
Idx file.Idx
Initializer Expression
}

MetaProperty struct {
Meta, Property *Identifier
Idx file.Idx
}
)

// _expressionNode
Expand All @@ -197,6 +202,7 @@ func (*StringLiteral) _expressionNode() {}
func (*ThisExpression) _expressionNode() {}
func (*UnaryExpression) _expressionNode() {}
func (*VariableExpression) _expressionNode() {}
func (*MetaProperty) _expressionNode() {}

// ========= //
// Statement //
Expand Down Expand Up @@ -413,6 +419,7 @@ func (self *StringLiteral) Idx0() file.Idx { return self.Idx }
func (self *ThisExpression) Idx0() file.Idx { return self.Idx }
func (self *UnaryExpression) Idx0() file.Idx { return self.Idx }
func (self *VariableExpression) Idx0() file.Idx { return self.Idx }
func (self *MetaProperty) Idx0() file.Idx { return self.Idx }

func (self *BadStatement) Idx0() file.Idx { return self.From }
func (self *BlockStatement) Idx0() file.Idx { return self.LeftBrace }
Expand Down Expand Up @@ -471,6 +478,9 @@ func (self *VariableExpression) Idx1() file.Idx {
}
return self.Initializer.Idx1()
}
func (self *MetaProperty) Idx1() file.Idx {
return self.Property.Idx1()
}

func (self *BadStatement) Idx1() file.Idx { return self.To }
func (self *BlockStatement) Idx1() file.Idx { return self.RightBrace + 1 }
Expand Down
Loading

0 comments on commit a13c43b

Please sign in to comment.