1
0
mirror of https://github.com/robertkrimen/otto synced 2025-10-12 20:27:30 +08:00

Merge pull request #103 from multiplay/multi-return

Support go multiple return values as an array
This commit is contained in:
Robert Krimen 2015-02-15 16:28:43 -08:00
commit c2346f4ada
2 changed files with 20 additions and 7 deletions

View File

@ -213,9 +213,14 @@ func Test_reflectStruct(t *testing.T) {
abc.FuncEllipsis("abc", "def", "ghi");
`, 3)
test(`raise:
abc.FuncReturn2();
`, "TypeError")
test(`
ret = abc.FuncReturn2();
if (ret && ret.length && ret.length == 2 && ret[0] == "def" && ret[1] === undefined) {
true;
} else {
false;
}
`, true)
test(`
abc.FuncReturnStruct();

View File

@ -285,13 +285,21 @@ func (self *_runtime) toValue(value interface{}) Value {
}
out := value.Call(in)
if len(out) == 1 {
return self.toValue(out[0].Interface())
} else if len(out) == 0 {
l := len(out)
switch l {
case 0:
return Value{}
case 1:
return self.toValue(out[0].Interface())
}
panic(call.runtime.panicTypeError())
// Return an array of the values to emulate multi value return.
// In the future this can be used along side destructuring assignment.
s := make([]interface{}, l)
for i, v := range out {
s[i] = self.toValue(v.Interface())
}
return self.toValue(s)
}))
case reflect.Struct:
return toValue_object(self.newGoStructObject(value))