def repl(): with environment(base=True, transpile=False): try: frame_manager = FrameManager() with frame_manager.capture("read") as previous_capturer: previous_capturer.attempt_capture_or_raise( *bootstraped_executor(frame_manager).invoke( NO_VALUE, frame_manager)) while True: try: raw_code = read_input() code = build_executor(raw_code) continuation = previous_capturer.value.continuation with frame_manager.capture() as new_capturer: new_capturer.attempt_capture_or_raise( *continuation.invoke(code, frame_manager)) if new_capturer.caught_break_mode == "read": previous_capturer = new_capturer else: print("command broke out by {}: {}".format( new_capturer.caught_break_mode, new_capturer.value)) except Exception as e: logger.exception("Error on input {}: {} {}".format( raw_code, type(e), e)) except KeyboardInterrupt as e: print("\nExiting...")
def test_7_fast(self): code = parse(""" function() => int { var isPrime = function(int number) => bool { for(var i from range(2, number / 2)) { if(number % i == 0) { return false; }; }; return true; }; int count = 1, test = 3; loop { if(isPrime(test)) { count = count + 1; if(count >= 100) { return test; }; }; test = test + 2; }; } """, debug=True) with environment(**fastest): _, result = bootstrap_function(code) self.assertEquals(result.value, 541)
def test_multiplication(self): code = parse(""" function() { return 21 * 2; } """) with environment(transpile=True, return_value_optimization=True): _, result = bootstrap_function(code) self.assertEquals(result.value, 42)
def test_loops(self): code = parse(""" function() { int result = 0; for(var i from range(0, 100)) { result = result + i; }; return result; } """) with environment(transpile=True, return_value_optimization=True): _, result = bootstrap_function(code) self.assertEquals(result.value, 4950)
def test_loops(self): start = time() code = parse(""" function() { int i = 0, j = 0; while(i < 20) { j = 0; while(j < 20) { int foo = i * j; int bar = i * j; int baz = i * j; j = j + 1; }; i = i + 1; }; return i * j; } """, debug=True) with environment(transpile=True, return_value_optimization=True): _, result = bootstrap_function(code) self.assertEquals(result.value, 20 * 20) end = time()
def test_loop_faster(self): start = time() code = parse(""" function() { int i = 0, j = 0; while(i < 100) { j = 0; while(j < 100) { int foo = i * j; int bar = i * j; int baz = i * j; j = j + 1; }; i = i + 1; }; return i * j; } """, debug=True) with environment(**fastest): _, result = bootstrap_function(code) self.assertEquals(result.value, 100 * 100) end = time() self.assertLess(end - start, 25)
help='consume python objects off') parser.add_argument('-r', action='store_false', help='return value optimization off') parser.add_argument('-T', action='store_true', help="transpile to python") parser.add_argument('-P', help='cProfile mode') args, unknown_args = parser.parse_known_args() sys.argv[1:] = unknown_args sys.setrecursionlimit(10000) if __name__ == "__main__": from lockdown.executor.test import * from lockdown.parser.test import * from lockdown.type_system.test import * from lockdown.utils.test import * with environment(rtti=args.t, frame_shortcut=args.s, validate_flow_control=args.f, opcode_bindings=args.o, consume_python_objects=args.p, return_value_optimization=args.r, transpile=args.T, base=True): if args.P: with profile(args.P): unittest.main() else: unittest.main()