Skip to content

Commit

Permalink
Added Runtime.CreateObject(). See dop251#45
Browse files Browse the repository at this point in the history
  • Loading branch information
dop251 committed Nov 5, 2017
1 parent 9bbb616 commit 507e36c
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
5 changes: 5 additions & 0 deletions runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,11 @@ func (r *Runtime) NewObject() (v *Object) {
return r.newBaseObject(r.global.ObjectPrototype, classObject).val
}

// CreateObject creates an object with given prototype. Equivalent of Object.create(proto).
func (r *Runtime) CreateObject(proto *Object) *Object {
return r.newBaseObject(proto, classObject).val
}

func (r *Runtime) NewTypeError(args ...interface{}) *Object {
msg := ""
if len(args) > 0 {
Expand Down
29 changes: 29 additions & 0 deletions runtime_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -972,6 +972,35 @@ func TestNativeConstruct(t *testing.T) {
}
}

func TestCreateObject(t *testing.T) {
const SCRIPT = `
inst instanceof C;
`

r := New()
c := r.ToValue(func(call ConstructorCall) *Object {
return nil
})

proto := c.(*Object).Get("prototype").(*Object)

inst := r.CreateObject(proto)

r.Set("C", c)
r.Set("inst", inst)

prg := MustCompile("test.js", SCRIPT, false)

res, err := r.RunProgram(prg)
if err != nil {
t.Fatal(err)
}

if !res.StrictEquals(valueTrue) {
t.Fatalf("Unexpected result: %v", res)
}
}

/*
func TestArrayConcatSparse(t *testing.T) {
function foo(a,b,c)
Expand Down

0 comments on commit 507e36c

Please sign in to comment.