|
| 1 | +//! Regression tests for the module-loading path that backs `global.loadModule`. |
| 2 | +//! |
| 3 | +//! `global.loadModule` (from @nativescript/core) routes through the runtime's |
| 4 | +//! `require` shim and the native `__nsResolveModulePath` / `__nsReadTextFile` |
| 5 | +//! host functions. When the module resolver can't find a registered module for a |
| 6 | +//! path (e.g. an unregistered CSS file like `app.css`), it produces a null module |
| 7 | +//! name. Passing that null down the chain must surface as a *catchable JS error* — |
| 8 | +//! it must never panic / abort the Rust runtime. |
| 9 | +//! |
| 10 | +//! Each test runs a self-contained JS try/catch that returns a sentinel string. |
| 11 | +//! If the native layer panicked instead of throwing, the panic cannot be caught by |
| 12 | +//! the JS `catch` and would abort the test binary — so a passing test proves the |
| 13 | +//! path throws rather than panics. |
| 14 | +
|
| 15 | +use crate::Runtime; |
| 16 | + |
| 17 | +/// Evaluate a JS expression and return its string result, or `<no result>` if the |
| 18 | +/// outer eval itself threw (it shouldn't — each snippet catches internally). |
| 19 | +fn eval(runtime: &mut Runtime, expr: &str) -> String { |
| 20 | + runtime |
| 21 | + .eval_script_to_string(expr) |
| 22 | + .unwrap_or_else(|| "<no result>".to_string()) |
| 23 | +} |
| 24 | + |
| 25 | +/// Wrap a call so a thrown error becomes `THREW:<message>` and a normal return |
| 26 | +/// becomes `NO_THROW:<value>`. A Rust panic in the call would abort the process. |
| 27 | +fn caught(call: &str) -> String { |
| 28 | + format!( |
| 29 | + r#"(function() {{ |
| 30 | + try {{ |
| 31 | + var r = {call}; |
| 32 | + return 'NO_THROW:' + String(r); |
| 33 | + }} catch (e) {{ |
| 34 | + return 'THREW:' + (e && e.message ? e.message : String(e)); |
| 35 | + }} |
| 36 | + }})()"# |
| 37 | + ) |
| 38 | +} |
| 39 | + |
| 40 | +#[test] |
| 41 | +fn require_null_throws_not_panics() { |
| 42 | + let mut runtime = Runtime::new("."); |
| 43 | + let result = eval(&mut runtime, &caught("globalThis.require(null)")); |
| 44 | + assert!( |
| 45 | + result.starts_with("THREW:"), |
| 46 | + "require(null) must throw, got: {result}" |
| 47 | + ); |
| 48 | +} |
| 49 | + |
| 50 | +#[test] |
| 51 | +fn require_undefined_throws_not_panics() { |
| 52 | + let mut runtime = Runtime::new("."); |
| 53 | + let result = eval(&mut runtime, &caught("globalThis.require(undefined)")); |
| 54 | + assert!( |
| 55 | + result.starts_with("THREW:"), |
| 56 | + "require(undefined) must throw, got: {result}" |
| 57 | + ); |
| 58 | +} |
| 59 | + |
| 60 | +#[test] |
| 61 | +fn require_empty_string_throws_not_panics() { |
| 62 | + let mut runtime = Runtime::new("."); |
| 63 | + let result = eval(&mut runtime, &caught("globalThis.require('')")); |
| 64 | + assert!( |
| 65 | + result.starts_with("THREW:"), |
| 66 | + "require('') must throw, got: {result}" |
| 67 | + ); |
| 68 | +} |
| 69 | + |
| 70 | +/// Mirrors how @nativescript/core calls `global.loadModule(resolvedModuleName)` |
| 71 | +/// where `resolvedModuleName` is null because no registered webpack module matched |
| 72 | +/// the CSS path. The runtime-owned `loadModule` (aliased to `require` here) must |
| 73 | +/// throw cleanly. |
| 74 | +#[test] |
| 75 | +fn load_module_null_resolved_name_throws_not_panics() { |
| 76 | + let mut runtime = Runtime::new("."); |
| 77 | + let result = eval( |
| 78 | + &mut runtime, |
| 79 | + &caught( |
| 80 | + "(function() { \ |
| 81 | + var loadModule = globalThis.loadModule || globalThis.require; \ |
| 82 | + return loadModule(null); \ |
| 83 | + })()", |
| 84 | + ), |
| 85 | + ); |
| 86 | + assert!( |
| 87 | + result.starts_with("THREW:"), |
| 88 | + "loadModule(null) must throw, got: {result}" |
| 89 | + ); |
| 90 | +} |
| 91 | + |
| 92 | +#[test] |
| 93 | +fn resolve_module_path_null_throws_not_panics() { |
| 94 | + let mut runtime = Runtime::new("."); |
| 95 | + let result = eval(&mut runtime, &caught("globalThis.__nsResolveModulePath(null)")); |
| 96 | + assert!( |
| 97 | + result.starts_with("THREW:"), |
| 98 | + "__nsResolveModulePath(null) must throw, got: {result}" |
| 99 | + ); |
| 100 | +} |
| 101 | + |
| 102 | +#[test] |
| 103 | +fn resolve_module_path_empty_throws_not_panics() { |
| 104 | + let mut runtime = Runtime::new("."); |
| 105 | + let result = eval(&mut runtime, &caught("globalThis.__nsResolveModulePath('')")); |
| 106 | + assert!( |
| 107 | + result.starts_with("THREW:"), |
| 108 | + "__nsResolveModulePath('') must throw, got: {result}" |
| 109 | + ); |
| 110 | +} |
| 111 | + |
| 112 | +#[test] |
| 113 | +fn read_text_file_null_throws_not_panics() { |
| 114 | + let mut runtime = Runtime::new("."); |
| 115 | + let result = eval(&mut runtime, &caught("globalThis.__nsReadTextFile(null)")); |
| 116 | + assert!( |
| 117 | + result.starts_with("THREW:"), |
| 118 | + "__nsReadTextFile(null) must throw, got: {result}" |
| 119 | + ); |
| 120 | +} |
| 121 | + |
| 122 | +#[test] |
| 123 | +fn read_text_file_empty_throws_not_panics() { |
| 124 | + let mut runtime = Runtime::new("."); |
| 125 | + let result = eval(&mut runtime, &caught("globalThis.__nsReadTextFile('')")); |
| 126 | + assert!( |
| 127 | + result.starts_with("THREW:"), |
| 128 | + "__nsReadTextFile('') must throw, got: {result}" |
| 129 | + ); |
| 130 | +} |
| 131 | + |
| 132 | +/// Hammer the bad-input path repeatedly: a latent panic / abort or memory-safety |
| 133 | +/// issue in the native handlers would surface as a crash rather than 50 clean throws. |
| 134 | +#[test] |
| 135 | +fn repeated_bad_module_loads_are_stable() { |
| 136 | + let mut runtime = Runtime::new("."); |
| 137 | + for _ in 0..50 { |
| 138 | + let result = eval(&mut runtime, &caught("globalThis.require(null)")); |
| 139 | + assert!(result.starts_with("THREW:"), "unexpected: {result}"); |
| 140 | + } |
| 141 | +} |
0 commit comments