diff --git a/jit/src/instructions.rs b/jit/src/instructions.rs index a4cebd7cc..f75b37e89 100644 --- a/jit/src/instructions.rs +++ b/jit/src/instructions.rs @@ -25,6 +25,7 @@ enum JitValue { Int(Value), Float(Value), Bool(Value), + None, } impl JitValue { @@ -41,12 +42,14 @@ impl JitValue { JitValue::Int(_) => Some(JitType::Int), JitValue::Float(_) => Some(JitType::Float), JitValue::Bool(_) => Some(JitType::Bool), + JitValue::None => None, } } fn into_value(self) -> Option { match self { JitValue::Int(val) | JitValue::Float(val) | JitValue::Bool(val) => Some(val), + JitValue::None => None, } } } @@ -122,6 +125,7 @@ impl<'a, 'b> FunctionCompiler<'a, 'b> { Ok(self.builder.ins().bint(types::I8, val)) } JitValue::Bool(val) => Ok(val), + JitValue::None => Ok(self.builder.ins().iconst(types::I8, 0)), } } @@ -193,6 +197,10 @@ impl<'a, 'b> FunctionCompiler<'a, 'b> { self.stack.push(JitValue::Bool(val)); Ok(()) } + BorrowedConstant::None => { + self.stack.push(JitValue::None); + Ok(()) + } _ => Err(JitCompileError::NotSupported), } } diff --git a/jit/tests/lib.rs b/jit/tests/lib.rs index d21c216c7..aa5f0f22d 100644 --- a/jit/tests/lib.rs +++ b/jit/tests/lib.rs @@ -4,3 +4,4 @@ mod bool_tests; mod float_tests; mod int_tests; mod misc_tests; +mod none_tests; diff --git a/jit/tests/none_tests.rs b/jit/tests/none_tests.rs new file mode 100644 index 000000000..561f1f01c --- /dev/null +++ b/jit/tests/none_tests.rs @@ -0,0 +1,24 @@ +#[test] +fn test_not() { + let not_ = jit_function! { not_(x: i64) -> bool => r##" + def not_(x: int): + return not None + "## }; + + assert_eq!(not_(0), Ok(true)); +} + +#[test] +fn test_if_not() { + let if_not = jit_function! { if_not(x: i64) -> i64 => r##" + def if_not(x: int): + if not None: + return 1 + else: + return 0 + + return -1 + "## }; + + assert_eq!(if_not(0), Ok(1)); +}