|
| 1 | +// This example tests for the BorrowMutError bug in deno_core 0.376.0 |
| 2 | +// |
| 3 | +// Issue: When executing JavaScript that throws a ReferenceError, |
| 4 | +// deno_core panics with "already borrowed: BorrowMutError" |
| 5 | +// instead of properly returning the error. |
| 6 | +// |
| 7 | +// Expected behavior: The error should be returned as Err(...) |
| 8 | +// Actual behavior (in buggy versions): Process panics/aborts |
| 9 | + |
| 10 | +use rustyscript::{Module, Runtime, RuntimeOptions}; |
| 11 | +use std::time::Duration; |
| 12 | + |
| 13 | +fn main() { |
| 14 | + println!("=== Testing for deno_core BorrowMutError bug ===\n"); |
| 15 | + |
| 16 | + // Create a basic Runtime with minimal configuration |
| 17 | + let options = RuntimeOptions { |
| 18 | + timeout: Duration::from_secs(5), |
| 19 | + ..Default::default() |
| 20 | + }; |
| 21 | + |
| 22 | + let mut runtime = Runtime::new(options).expect("Failed to create runtime"); |
| 23 | + |
| 24 | + // Test 1: ReferenceError from accessing undefined variable |
| 25 | + println!("Test 1: ReferenceError from accessing undefined variable"); |
| 26 | + test_error_handling( |
| 27 | + &mut runtime, |
| 28 | + r#" |
| 29 | + // This should throw a ReferenceError |
| 30 | + undefinedVariable.toString(); |
| 31 | + "#, |
| 32 | + "test_undefined_variable.js", |
| 33 | + ); |
| 34 | + |
| 35 | + // Test 2: TypeError from calling non-function |
| 36 | + println!("\nTest 2: TypeError from calling non-function"); |
| 37 | + test_error_handling( |
| 38 | + &mut runtime, |
| 39 | + r#" |
| 40 | + // This should throw a TypeError |
| 41 | + const notAFunction = "I am not a function"; |
| 42 | + notAFunction(); |
| 43 | + "#, |
| 44 | + "test_type_error.js", |
| 45 | + ); |
| 46 | + |
| 47 | + // Test 3: Syntax error |
| 48 | + println!("\nTest 3: Syntax error"); |
| 49 | + test_error_handling( |
| 50 | + &mut runtime, |
| 51 | + r#" |
| 52 | + // This should throw a SyntaxError |
| 53 | + const x = ; |
| 54 | + "#, |
| 55 | + "test_syntax_error.js", |
| 56 | + ); |
| 57 | + |
| 58 | + // Test 4: Range error |
| 59 | + println!("\nTest 4: Range error"); |
| 60 | + test_error_handling( |
| 61 | + &mut runtime, |
| 62 | + r#" |
| 63 | + // This should throw a RangeError |
| 64 | + function recursiveFunction() { |
| 65 | + recursiveFunction(); |
| 66 | + } |
| 67 | + recursiveFunction(); |
| 68 | + "#, |
| 69 | + "test_range_error.js", |
| 70 | + ); |
| 71 | + |
| 72 | + println!("\n=== All tests passed! No BorrowMutError detected ==="); |
| 73 | +} |
| 74 | + |
| 75 | +fn test_error_handling(runtime: &mut Runtime, code: &str, module_name: &str) { |
| 76 | + let module = Module::new(module_name, code); |
| 77 | + |
| 78 | + print!(" Loading module '{}' (expected to fail)... ", module_name); |
| 79 | + let result = runtime.load_module(&module); |
| 80 | + |
| 81 | + match result { |
| 82 | + Ok(_) => { |
| 83 | + println!("❌ UNEXPECTED: Module loaded successfully (should have failed)"); |
| 84 | + std::process::exit(1); |
| 85 | + } |
| 86 | + Err(e) => { |
| 87 | + println!("✓ Correctly returned error: {}", |
| 88 | + e.to_string().lines().next().unwrap_or("Unknown error")); |
| 89 | + } |
| 90 | + } |
| 91 | +} |
0 commit comments