Skip to content

Commit

Permalink
fix: appending to nil not creating new array
Browse files Browse the repository at this point in the history
  • Loading branch information
oxfeeefeee committed Apr 25, 2022
1 parent fd8c09d commit eac2646
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 6 deletions.
12 changes: 12 additions & 0 deletions engine/tests/group1/slice1.gos
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,16 @@ func copy_slice() {
}


func appendToNil() {
var a []int
b := []int{6,6,6}
a = append(a, b...)
a[0] = 123
assert(a[0] == 123)
assert(b[0] == 6)
}



func main() {
var s1 = [][]int{{0},{99},{2}}
Expand All @@ -105,4 +115,6 @@ func main() {
append_slice()

copy_slice()

appendToNil()
}
29 changes: 24 additions & 5 deletions vm/src/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2646,7 +2646,12 @@ pub trait Dispatcher {

fn slice_array(&self, arr: GosValue, begin: isize, end: isize) -> RuntimeResult<GosValue>;

fn slice_append(&self, this: GosValue, other: GosValue) -> GosValue;
fn slice_append(
&self,
this: GosValue,
other: GosValue,
gcv: &GcoVec,
) -> RuntimeResult<GosValue>;

fn slice_copy_from(&self, this: GosValue, other: GosValue) -> usize;

Expand Down Expand Up @@ -2793,19 +2798,33 @@ macro_rules! define_dispatcher {
}

#[inline]
fn slice_append(&self, this: GosValue, other: GosValue) -> GosValue {
fn slice_append(
&self,
this: GosValue,
other: GosValue,
gcv: &GcoVec,
) -> RuntimeResult<GosValue> {
let a = this.as_slice::<$elem>();
let b = other.as_slice::<$elem>();
match b {
Some(y) => match a {
Some(x) => {
let mut to = x.0.clone();
to.append(&y.0);
GosValue::new_slice(to, other.t_elem())
Ok(GosValue::new_slice(to, other.t_elem()))
}
None => {
let data = y.0.as_rust_slice().to_vec();
let arr = ArrayObj::<$elem>::with_raw_data(data);
let slice = SliceObj::<$elem>::with_array(
GosValue::new_array(arr, other.t_elem(), gcv),
0,
-1,
)?;
Ok(GosValue::new_slice(slice, other.t_elem()))
}
None => GosValue::new_slice(SliceObj::clone(&y.0), other.t_elem()),
},
None => this,
None => Ok(this),
}
}

Expand Down
5 changes: 4 additions & 1 deletion vm/src/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1351,7 +1351,10 @@ impl<'a> Fiber<'a> {
};
let b = stack.pop_value();
let a = stack.pop_value();
stack.push(dispatcher_a_s_for(inst.t0()).slice_append(a, b));
match dispatcher_a_s_for(inst.t0()).slice_append(a, b, gcv) {
Ok(slice) => stack.push(slice),
Err(e) => go_panic_str!(panic, &e, frame, code),
};
}
Opcode::COPY => {
let t2 = match inst.t2() {
Expand Down

0 comments on commit eac2646

Please sign in to comment.