ScopeTests.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  1. using Jint.Native;
  2. using Jint.Runtime.Debugger;
  3. namespace Jint.Tests.Runtime.Debugger
  4. {
  5. public class ScopeTests
  6. {
  7. private static JsValue AssertOnlyScopeContains(DebugScopes scopes, string name, DebugScopeType scopeType)
  8. {
  9. var containingScope = Assert.Single(scopes, s => s.ScopeType == scopeType && s.BindingNames.Contains(name));
  10. Assert.DoesNotContain(scopes, s => s != containingScope && s.BindingNames.Contains(name));
  11. return containingScope.GetBindingValue(name);
  12. }
  13. private static void AssertScope(DebugScope actual, DebugScopeType expectedType, params string[] expectedBindingNames)
  14. {
  15. Assert.Equal(expectedType, actual.ScopeType);
  16. // Global scope will have a number of intrinsic bindings that are outside the scope [no pun] of these tests
  17. if (actual.ScopeType != DebugScopeType.Global)
  18. {
  19. Assert.Equal(expectedBindingNames.Length, actual.BindingNames.Count);
  20. }
  21. foreach (string expectedName in expectedBindingNames)
  22. {
  23. Assert.Contains(expectedName, actual.BindingNames);
  24. }
  25. }
  26. [Fact]
  27. public void AllowsInspectionOfUninitializedGlobalBindings()
  28. {
  29. string script = @"
  30. debugger;
  31. const globalConstant = 'test';
  32. let globalLet = 'test';
  33. ";
  34. TestHelpers.TestAtBreak(script, info =>
  35. {
  36. // Uninitialized global block scoped ("script scoped") bindings return null (and, just as importantly, don't throw):
  37. Assert.Null(info.CurrentScopeChain[0].GetBindingValue("globalConstant"));
  38. Assert.Null(info.CurrentScopeChain[0].GetBindingValue("globalLet"));
  39. });
  40. }
  41. [Fact]
  42. public void AllowsInspectionOfUninitializedBlockBindings()
  43. {
  44. string script = @"
  45. function test()
  46. {
  47. debugger;
  48. const globalConstant = 'test';
  49. let globalLet = 'test';
  50. }
  51. test();
  52. ";
  53. TestHelpers.TestAtBreak(script, info =>
  54. {
  55. // Uninitialized block scoped bindings return null (and, just as importantly, don't throw):
  56. Assert.Null(info.CurrentScopeChain[0].GetBindingValue("globalConstant"));
  57. Assert.Null(info.CurrentScopeChain[0].GetBindingValue("globalLet"));
  58. });
  59. }
  60. [Fact]
  61. public void ScriptScopeIncludesGlobalConst()
  62. {
  63. string script = @"
  64. const globalConstant = 'test';
  65. debugger;
  66. ";
  67. TestHelpers.TestAtBreak(script, info =>
  68. {
  69. var value = AssertOnlyScopeContains(info.CurrentScopeChain, "globalConstant", DebugScopeType.Script);
  70. Assert.Equal("test", value.AsString());
  71. });
  72. }
  73. [Fact]
  74. public void ScriptScopeIncludesGlobalLet()
  75. {
  76. string script = @"
  77. let globalLet = 'test';
  78. debugger;";
  79. TestHelpers.TestAtBreak(script, info =>
  80. {
  81. var value = AssertOnlyScopeContains(info.CurrentScopeChain, "globalLet", DebugScopeType.Script);
  82. Assert.Equal("test", value.AsString());
  83. });
  84. }
  85. [Fact]
  86. public void GlobalScopeIncludesGlobalVar()
  87. {
  88. string script = @"
  89. var globalVar = 'test';
  90. debugger;";
  91. TestHelpers.TestAtBreak(script, info =>
  92. {
  93. var value = AssertOnlyScopeContains(info.CurrentScopeChain, "globalVar", DebugScopeType.Global);
  94. Assert.Equal("test", value.AsString());
  95. });
  96. }
  97. [Fact]
  98. public void TopLevelBlockScopeIsIdentified()
  99. {
  100. string script = @"
  101. function test()
  102. {
  103. const localConst = 'test';
  104. debugger;
  105. }
  106. test();";
  107. TestHelpers.TestAtBreak(script, info =>
  108. {
  109. Assert.Equal(3, info.CurrentScopeChain.Count);
  110. Assert.Equal(DebugScopeType.Block, info.CurrentScopeChain[0].ScopeType);
  111. Assert.True(info.CurrentScopeChain[0].IsTopLevel);
  112. });
  113. }
  114. [Fact]
  115. public void NonTopLevelBlockScopeIsIdentified()
  116. {
  117. string script = @"
  118. function test()
  119. {
  120. {
  121. const localConst = 'test';
  122. debugger;
  123. }
  124. }
  125. test();";
  126. TestHelpers.TestAtBreak(script, info =>
  127. {
  128. // We only have 3 scopes, because the function top level block scope is empty.
  129. Assert.Equal(3, info.CurrentScopeChain.Count);
  130. Assert.Equal(DebugScopeType.Block, info.CurrentScopeChain[0].ScopeType);
  131. Assert.False(info.CurrentScopeChain[0].IsTopLevel);
  132. });
  133. }
  134. [Fact]
  135. public void BlockScopeIncludesLocalConst()
  136. {
  137. string script = @"
  138. function test()
  139. {
  140. {
  141. const localConst = 'test';
  142. debugger;
  143. }
  144. }
  145. test();";
  146. TestHelpers.TestAtBreak(script, info =>
  147. {
  148. var value = AssertOnlyScopeContains(info.CurrentScopeChain, "localConst", DebugScopeType.Block);
  149. Assert.Equal("test", value.AsString());
  150. });
  151. }
  152. [Fact]
  153. public void BlockScopeIncludesLocalLet()
  154. {
  155. string script = @"
  156. function test()
  157. {
  158. {
  159. let localLet = 'test';
  160. debugger;
  161. }
  162. }
  163. test();";
  164. TestHelpers.TestAtBreak(script, info =>
  165. {
  166. var value = AssertOnlyScopeContains(info.CurrentScopeChain, "localLet", DebugScopeType.Block);
  167. Assert.Equal("test", value.AsString());
  168. });
  169. }
  170. [Fact]
  171. public void LocalScopeIncludesLocalVar()
  172. {
  173. string script = @"
  174. function test()
  175. {
  176. var localVar = 'test';
  177. debugger;
  178. }
  179. test();";
  180. TestHelpers.TestAtBreak(script, info =>
  181. {
  182. AssertOnlyScopeContains(info.CurrentScopeChain, "localVar", DebugScopeType.Local);
  183. });
  184. }
  185. [Fact]
  186. public void LocalScopeIncludesBlockVar()
  187. {
  188. string script = @"
  189. function test()
  190. {
  191. debugger;
  192. {
  193. var localVar = 'test';
  194. }
  195. }
  196. test();";
  197. TestHelpers.TestAtBreak(script, info =>
  198. {
  199. AssertOnlyScopeContains(info.CurrentScopeChain, "localVar", DebugScopeType.Local);
  200. });
  201. }
  202. [Fact]
  203. public void BlockScopedConstIsVisibleInsideBlock()
  204. {
  205. string script = @"
  206. 'dummy statement';
  207. {
  208. const blockConst = 'block';
  209. debugger; // const isn't initialized until declaration
  210. }";
  211. TestHelpers.TestAtBreak(script, info =>
  212. {
  213. AssertOnlyScopeContains(info.CurrentScopeChain, "blockConst", DebugScopeType.Block);
  214. });
  215. }
  216. [Fact]
  217. public void BlockScopedLetIsVisibleInsideBlock()
  218. {
  219. string script = @"
  220. 'dummy statement';
  221. {
  222. let blockLet = 'block';
  223. debugger; // let isn't initialized until declaration
  224. }";
  225. TestHelpers.TestAtBreak(script, info =>
  226. {
  227. AssertOnlyScopeContains(info.CurrentScopeChain, "blockLet", DebugScopeType.Block);
  228. });
  229. }
  230. [Fact]
  231. public void HasCorrectScopeChainForFunction()
  232. {
  233. string script = @"
  234. function add(a, b)
  235. {
  236. debugger;
  237. return a + b;
  238. }
  239. const x = 1;
  240. const y = 2;
  241. const z = add(x, y);";
  242. TestHelpers.TestAtBreak(script, info =>
  243. {
  244. Assert.Collection(info.CurrentScopeChain,
  245. scope => AssertScope(scope, DebugScopeType.Local, "arguments", "a", "b"),
  246. scope => AssertScope(scope, DebugScopeType.Script, "x", "y", "z"),
  247. scope => AssertScope(scope, DebugScopeType.Global, "add"));
  248. });
  249. }
  250. [Fact]
  251. public void HasCorrectScopeChainForNestedFunction()
  252. {
  253. string script = @"
  254. function add(a, b)
  255. {
  256. function power(a)
  257. {
  258. debugger;
  259. return a * a;
  260. }
  261. return power(a) + b;
  262. }
  263. const x = 1;
  264. const y = 2;
  265. const z = add(x, y);";
  266. TestHelpers.TestAtBreak(script, info =>
  267. {
  268. Assert.Collection(info.CurrentScopeChain,
  269. scope => AssertScope(scope, DebugScopeType.Local, "arguments", "a"),
  270. // a, arguments shadowed by local - but still exist in this scope
  271. scope => AssertScope(scope, DebugScopeType.Closure, "a", "arguments", "b", "power"),
  272. scope => AssertScope(scope, DebugScopeType.Script, "x", "y", "z"),
  273. scope => AssertScope(scope, DebugScopeType.Global, "add"));
  274. });
  275. }
  276. [Fact]
  277. public void HasCorrectScopeChainForBlock()
  278. {
  279. string script = @"
  280. function add(a, b)
  281. {
  282. if (a > 0)
  283. {
  284. const y = b / a;
  285. debugger;
  286. }
  287. return a + b;
  288. }
  289. const x = 1;
  290. const y = 2;
  291. const z = add(x, y);";
  292. TestHelpers.TestAtBreak(script, info =>
  293. {
  294. Assert.Collection(info.CurrentScopeChain,
  295. scope => AssertScope(scope, DebugScopeType.Block, "y"),
  296. scope => AssertScope(scope, DebugScopeType.Local, "arguments", "a", "b"),
  297. scope => AssertScope(scope, DebugScopeType.Script, "x", "y", "z"), // y is shadowed, but still in the scope
  298. scope => AssertScope(scope, DebugScopeType.Global, "add"));
  299. });
  300. }
  301. [Fact]
  302. public void HasCorrectScopeChainForModule()
  303. {
  304. string imported = @"
  305. function add(a, b)
  306. {
  307. debugger;
  308. return a + b;
  309. }
  310. export { add };";
  311. string main = @"
  312. import { add } from 'imported-module';
  313. const x = 1;
  314. const y = 2;
  315. add(x, y);";
  316. TestHelpers.TestAtBreak(engine =>
  317. {
  318. engine.Modules.Add("imported-module", imported);
  319. engine.Modules.Add("main", main);
  320. engine.Modules.Import("main");
  321. },
  322. info =>
  323. {
  324. Assert.Collection(info.CurrentScopeChain,
  325. scope => AssertScope(scope, DebugScopeType.Local, "arguments", "a", "b"),
  326. scope => AssertScope(scope, DebugScopeType.Module, "add"),
  327. scope => AssertScope(scope, DebugScopeType.Global));
  328. });
  329. }
  330. [Fact]
  331. public void HasCorrectScopeChainForNestedBlock()
  332. {
  333. string script = @"
  334. function add(a, b)
  335. {
  336. if (a > 0)
  337. {
  338. const y = b / a;
  339. if (y > 0)
  340. {
  341. const x = b / y;
  342. debugger;
  343. }
  344. }
  345. return a + b;
  346. }
  347. const x = 1;
  348. const y = 2;
  349. const z = add(x, y);";
  350. TestHelpers.TestAtBreak(script, info =>
  351. {
  352. Assert.Collection(info.CurrentScopeChain,
  353. scope => AssertScope(scope, DebugScopeType.Block, "x"),
  354. scope => AssertScope(scope, DebugScopeType.Block, "y"),
  355. scope => AssertScope(scope, DebugScopeType.Local, "arguments", "a", "b"),
  356. scope => AssertScope(scope, DebugScopeType.Script, "x", "y", "z"), // x, y are shadowed, but still in the scope
  357. scope => AssertScope(scope, DebugScopeType.Global, "add"));
  358. });
  359. }
  360. [Fact]
  361. public void HasCorrectScopeChainForCatch()
  362. {
  363. string script = @"
  364. function func()
  365. {
  366. let a = 1;
  367. try
  368. {
  369. throw new Error('test');
  370. }
  371. catch (error)
  372. {
  373. debugger;
  374. }
  375. }
  376. func();";
  377. TestHelpers.TestAtBreak(script, info =>
  378. {
  379. Assert.Collection(info.CurrentScopeChain,
  380. scope => AssertScope(scope, DebugScopeType.Catch, "error"),
  381. scope => AssertScope(scope, DebugScopeType.Block, "a"),
  382. scope => AssertScope(scope, DebugScopeType.Local, "arguments"),
  383. scope => AssertScope(scope, DebugScopeType.Global, "func"));
  384. });
  385. }
  386. [Fact]
  387. public void HasCorrectScopeChainForWith()
  388. {
  389. string script = @"
  390. const obj = { a: 2, b: 4 };
  391. with (obj)
  392. {
  393. const x = a;
  394. debugger;
  395. };";
  396. TestHelpers.TestAtBreak(script, info =>
  397. {
  398. Assert.Collection(info.CurrentScopeChain,
  399. scope => AssertScope(scope, DebugScopeType.Block, "x"),
  400. scope => AssertScope(scope, DebugScopeType.With, "a", "b"),
  401. scope => AssertScope(scope, DebugScopeType.Script, "obj"),
  402. scope => AssertScope(scope, DebugScopeType.Global));
  403. });
  404. }
  405. [Fact]
  406. public void ScopeChainIncludesNonEmptyScopes()
  407. {
  408. string script = @"
  409. const x = 2;
  410. if (x > 0)
  411. {
  412. const y = x;
  413. if (x > 1)
  414. {
  415. const z = x;
  416. debugger;
  417. }
  418. }";
  419. TestHelpers.TestAtBreak(script, info =>
  420. {
  421. Assert.Collection(info.CurrentScopeChain,
  422. scope => AssertScope(scope, DebugScopeType.Block, "z"),
  423. scope => AssertScope(scope, DebugScopeType.Block, "y"),
  424. scope => AssertScope(scope, DebugScopeType.Script, "x"),
  425. scope => AssertScope(scope, DebugScopeType.Global));
  426. });
  427. }
  428. [Fact]
  429. public void ScopeChainExcludesEmptyScopes()
  430. {
  431. string script = @"
  432. const x = 2;
  433. if (x > 0)
  434. {
  435. if (x > 1)
  436. {
  437. const z = x;
  438. debugger;
  439. }
  440. }";
  441. TestHelpers.TestAtBreak(script, info =>
  442. {
  443. Assert.Collection(info.CurrentScopeChain,
  444. scope => AssertScope(scope, DebugScopeType.Block, "z"),
  445. scope => AssertScope(scope, DebugScopeType.Script, "x"),
  446. scope => AssertScope(scope, DebugScopeType.Global));
  447. });
  448. }
  449. [Fact]
  450. public void ResolvesScopeChainsUpTheCallStack()
  451. {
  452. string script = @"
  453. const x = 1;
  454. function foo(a, c)
  455. {
  456. debugger;
  457. }
  458. function bar(b)
  459. {
  460. foo(b, 2);
  461. }
  462. bar(x);";
  463. TestHelpers.TestAtBreak(script, info =>
  464. {
  465. Assert.Collection(info.CallStack,
  466. frame => Assert.Collection(frame.ScopeChain,
  467. // in foo()
  468. scope => AssertScope(scope, DebugScopeType.Local, "arguments", "a", "c"),
  469. scope => AssertScope(scope, DebugScopeType.Script, "x"),
  470. scope => AssertScope(scope, DebugScopeType.Global, "foo", "bar")
  471. ),
  472. frame => Assert.Collection(frame.ScopeChain,
  473. // in bar()
  474. scope => AssertScope(scope, DebugScopeType.Local, "arguments", "b"),
  475. scope => AssertScope(scope, DebugScopeType.Script, "x"),
  476. scope => AssertScope(scope, DebugScopeType.Global, "foo", "bar")
  477. ),
  478. frame => Assert.Collection(frame.ScopeChain,
  479. // in global
  480. scope => AssertScope(scope, DebugScopeType.Script, "x"),
  481. scope => AssertScope(scope, DebugScopeType.Global, "foo", "bar")
  482. )
  483. );
  484. });
  485. }
  486. [Fact]
  487. public void InspectsModuleScopedBindings()
  488. {
  489. string main = @"const x = 1; debugger;";
  490. TestHelpers.TestAtBreak(engine =>
  491. {
  492. engine.Modules.Add("main", main);
  493. engine.Modules.Import("main");
  494. },
  495. info =>
  496. {
  497. // No need for imports - main module is module scoped too, duh.
  498. var value = AssertOnlyScopeContains(info.CurrentScopeChain, "x", DebugScopeType.Module);
  499. Assert.Equal(1, value.AsInteger());
  500. });
  501. }
  502. }
  503. }