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:
commit
c2346f4ada
|
@ -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();
|
||||
|
|
16
runtime.go
16
runtime.go
|
@ -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))
|
||||
|
|
Loading…
Reference in New Issue
Block a user