Skip to content

Commit

Permalink
Added Runtime.NewArray()
Browse files Browse the repository at this point in the history
  • Loading branch information
dop251 authored and tsedgwick committed Apr 14, 2021
1 parent fd3988b commit 390bc9b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
8 changes: 8 additions & 0 deletions runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,14 @@ func (r *Runtime) CreateObject(proto *Object) *Object {
return r.newBaseObject(proto, classObject).val
}

func (r *Runtime) NewArray(items ...interface{}) *Object {
values := make([]Value, len(items))
for i, item := range items {
values[i] = r.ToValue(item)
}
return r.newArrayValues(values)
}

func (r *Runtime) NewTypeError(args ...interface{}) *Object {
msg := ""
if len(args) > 0 {
Expand Down
18 changes: 18 additions & 0 deletions runtime_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1801,6 +1801,24 @@ func ExampleObject_SetSymbol() {
// Output: 1 2 3 4 5 6 7 8 9 10
}

func ExampleRuntime_NewArray() {
vm := New()
array := vm.NewArray(1, 2, true)
vm.Set("array", array)
res, err := vm.RunString(`
var acc = "";
for (var v of array) {
acc += v + " ";
}
acc;
`)
if err != nil {
panic(err)
}
fmt.Println(res)
// Output: 1 2 true
}

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

0 comments on commit 390bc9b

Please sign in to comment.