Skip to content

Commit

Permalink
Merge pull request #158 from MiSawa/fix-update-overlapping-path
Browse files Browse the repository at this point in the history
Fix update-assignment operator on overlapping paths (fix #156)
  • Loading branch information
MiSawa authored Oct 29, 2022
2 parents ec5e318 + 9de04c3 commit 48e1116
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 8 deletions.
25 changes: 17 additions & 8 deletions src/compile/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1411,7 +1411,7 @@ impl Compiler {
next: Address,
) -> Result<Address> {
let slot = compiler.allocate_variable();
let tmp_slot = compiler.allocate_variable();
let path_slot = compiler.allocate_variable();
let del_slot = compiler.allocate_variable();
let label_slot = compiler.current_scope_mut().allocate_label();

Expand Down Expand Up @@ -1453,22 +1453,31 @@ impl Compiler {
);
let next = modification.compile(compiler, next)?;
let next = compiler.emitter.emit_fork(on_empty, next);
// value, path, indexed value
let next = compiler.emitter.emit_normal_op(
ByteCode::Intrinsic1(NamedFn1 {
name: "getpath",
func: intrinsic::get_path,
}),
next,
);
let next = compiler
.emitter
.emit_normal_op(ByteCode::Load(tmp_slot), next);
let next = compiler.emitter.emit_normal_op(ByteCode::Swap, next);
.emit_normal_op(ByteCode::Load(path_slot), next);
let next = compiler.emitter.emit_normal_op(ByteCode::Load(slot), next);
let next = compiler
.emitter
.emit_normal_op(ByteCode::ExitPathTracking, next);
.emit_normal_op(ByteCode::Load(path_slot), next);
let next = compiler.emitter.emit_fork(on_empty, next);
let next = compiler.emitter.emit_normal_op(ByteCode::Load(slot), next);
let next = compiler
.emitter
.emit_normal_op(ByteCode::Store(tmp_slot), next);
let next = compiler.emitter.emit_normal_op(ByteCode::Dup, next);
.emit_normal_op(ByteCode::ForkLabel(label_slot), next);
let next = compiler
.emitter
.emit_normal_op(ByteCode::ForkLabel(label_slot), next);
.emit_normal_op(ByteCode::Store(path_slot), next);
let next = compiler
.emitter
.emit_normal_op(ByteCode::ExitPathTracking, next);
let next = compiler.compile_query(path_expression, next)?;
let next = compiler
.emitter
Expand Down
13 changes: 13 additions & 0 deletions tests/hand_written/assignments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,3 +120,16 @@ test!(
[1,2,null,null,5,3]
"#
);

test!(
overlapping_update,
r#"
(.foo,.foo,.foo) |= . + 1
"#,
r#"
null
"#,
r#"
{"foo":3}
"#
);

0 comments on commit 48e1116

Please sign in to comment.