MCPcopy Index your code
hub / github.com/aiscriptdev/aiscript / test_if_nil

Function test_if_nil

aiscript-vm/src/compiler/optimizer/dead_code.rs:152–175  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

150
151 #[test]
152 fn test_if_nil() {
153 let mut chunk = Chunk::new();
154 // if nil { print("bad"); } else { print("nil"); }
155 chunk.write_code(OpCode::Nil, 1); // condition
156 chunk.write_code(OpCode::JumpPopIfFalse(4), 1); // skip true branch
157 chunk.write_code(OpCode::GetGlobal(0), 1); // true: print
158 chunk.write_code(OpCode::Constant(1), 1); // "bad"
159 chunk.write_code(
160 OpCode::Call {
161 positional_count: 1,
162 keyword_count: 0,
163 },
164 1,
165 ); // print("bad")
166 chunk.write_code(OpCode::Pop(1), 1); // cleanup
167 chunk.write_code(OpCode::Jump(1), 1); // skip else
168 chunk.write_code(OpCode::Pop(1), 1); // else: cleanup
169 chunk.write_code(OpCode::Nil, 1); // else: return nil
170 chunk.write_code(OpCode::Return, 1); // return
171
172 let optimizer = DeadCodeEliminator;
173 assert!(!optimizer.optimize(&mut chunk)); // Should not optimize conditional code
174 assert_eq!(chunk.code.len(), 10); // Everything should be preserved
175 }
176
177 // #[test]
178 // fn test_unconditional_dead_code() {

Callers

nothing calls this directly

Calls 1

write_codeMethod · 0.80

Tested by

no test coverage detected