InteropTests.cs 66 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Dynamic;
  5. using System.Globalization;
  6. using System.Linq;
  7. using System.Reflection;
  8. using Jint.Native;
  9. using Jint.Native.Array;
  10. using Jint.Native.Object;
  11. using Jint.Runtime.Descriptors;
  12. using Jint.Runtime.Interop;
  13. using Jint.Tests.Runtime.Converters;
  14. using Jint.Tests.Runtime.Domain;
  15. using Shapes;
  16. using Xunit;
  17. namespace Jint.Tests.Runtime
  18. {
  19. public class InteropTests : IDisposable
  20. {
  21. private readonly Engine _engine;
  22. public InteropTests()
  23. {
  24. _engine = new Engine(cfg => cfg.AllowClr(
  25. typeof(Shape).GetTypeInfo().Assembly,
  26. typeof(Console).GetTypeInfo().Assembly,
  27. typeof(System.IO.File).GetTypeInfo().Assembly))
  28. .SetValue("log", new Action<object>(Console.WriteLine))
  29. .SetValue("assert", new Action<bool>(Assert.True))
  30. .SetValue("equal", new Action<object, object>(Assert.Equal))
  31. ;
  32. }
  33. void IDisposable.Dispose()
  34. {
  35. }
  36. private void RunTest(string source)
  37. {
  38. _engine.Execute(source);
  39. }
  40. [Fact]
  41. public void PrimitiveTypesCanBeSet()
  42. {
  43. _engine.SetValue("x", 10);
  44. _engine.SetValue("y", true);
  45. _engine.SetValue("z", "foo");
  46. RunTest(@"
  47. assert(x === 10);
  48. assert(y === true);
  49. assert(z === 'foo');
  50. ");
  51. }
  52. [Fact]
  53. public void DelegatesCanBeSet()
  54. {
  55. _engine.SetValue("square", new Func<double, double>(x => x * x));
  56. RunTest(@"
  57. assert(square(10) === 100);
  58. ");
  59. }
  60. [Fact]
  61. public void DelegateWithNullableParameterCanBePassedANull()
  62. {
  63. _engine.SetValue("isnull", new Func<double?, bool>(x => x == null));
  64. RunTest(@"
  65. assert(isnull(null) === true);
  66. ");
  67. }
  68. [Fact]
  69. public void DelegateWithObjectParameterCanBePassedANull()
  70. {
  71. _engine.SetValue("isnull", new Func<object, bool>(x => x == null));
  72. RunTest(@"
  73. assert(isnull(null) === true);
  74. ");
  75. }
  76. [Fact]
  77. public void DelegateWithNullableParameterCanBePassedAnUndefined()
  78. {
  79. _engine.SetValue("isnull", new Func<double?, bool>(x => x == null));
  80. RunTest(@"
  81. assert(isnull(undefined) === true);
  82. ");
  83. }
  84. [Fact]
  85. public void DelegateWithObjectParameterCanBePassedAnUndefined()
  86. {
  87. _engine.SetValue("isnull", new Func<object, bool>(x => x == null));
  88. RunTest(@"
  89. assert(isnull(undefined) === true);
  90. ");
  91. }
  92. [Fact]
  93. public void DelegateWithNullableParameterCanBeExcluded()
  94. {
  95. _engine.SetValue("isnull", new Func<double?, bool>(x => x == null));
  96. RunTest(@"
  97. assert(isnull() === true);
  98. ");
  99. }
  100. [Fact]
  101. public void DelegateWithObjectParameterCanBeExcluded()
  102. {
  103. _engine.SetValue("isnull", new Func<object, bool>(x => x == null));
  104. RunTest(@"
  105. assert(isnull() === true);
  106. ");
  107. }
  108. [Fact]
  109. public void DynamicDelegateCanBeSet()
  110. {
  111. #if NETFRAMEWORK
  112. var parameters = new[]
  113. {
  114. System.Linq.Expressions.Expression.Parameter(typeof(int)),
  115. System.Linq.Expressions.Expression.Parameter(typeof(int))
  116. };
  117. var exp = System.Linq.Expressions.Expression.Add(parameters[0], parameters[1]);
  118. var del = System.Linq.Expressions.Expression.Lambda(exp, parameters).Compile();
  119. _engine.SetValue("add", del);
  120. RunTest(@"
  121. assert(add(1,1) === 2);
  122. ");
  123. #endif
  124. }
  125. [Fact]
  126. public void ExtraParametersAreIgnored()
  127. {
  128. _engine.SetValue("passNumber", new Func<int, int>(x => x));
  129. RunTest(@"
  130. assert(passNumber(123,'test',{},[],null) === 123);
  131. ");
  132. }
  133. private delegate string callParams(params object[] values);
  134. private delegate string callArgumentAndParams(string firstParam, params object[] values);
  135. [Fact]
  136. public void DelegatesWithParamsParameterCanBeInvoked()
  137. {
  138. var a = new A();
  139. _engine.SetValue("callParams", new callParams(a.Call13));
  140. _engine.SetValue("callArgumentAndParams", new callArgumentAndParams(a.Call14));
  141. RunTest(@"
  142. assert(callParams('1','2','3') === '1,2,3');
  143. assert(callParams('1') === '1');
  144. assert(callParams() === '');
  145. assert(callArgumentAndParams('a','1','2','3') === 'a:1,2,3');
  146. assert(callArgumentAndParams('a','1') === 'a:1');
  147. assert(callArgumentAndParams('a') === 'a:');
  148. assert(callArgumentAndParams() === ':');
  149. ");
  150. }
  151. [Fact]
  152. public void DelegateWithDefaultValueParametersCanBeInvoked()
  153. {
  154. var instance = new A();
  155. _engine.SetValue("Instance", instance);
  156. _engine.SetValue("Class", TypeReference.CreateTypeReference(_engine, typeof(A)));
  157. RunTest(@"
  158. assert(Instance.Call19() === 0);
  159. assert(Instance.Call19(1) === 1);
  160. assert(Instance.Call20(1) === 4);
  161. assert(Instance.Call20(1, 2) === 5);
  162. assert(Instance.Call20(1 , 2, 3) === 6);
  163. assert(Class.Call19Static() === 0);
  164. assert(Class.Call19Static(1) === 1);
  165. assert(Class.Call20Static(1) === 4);
  166. assert(Class.Call20Static(1, 2) === 5);
  167. assert(Class.Call20Static(1 , 2, 3) === 6);
  168. ");
  169. }
  170. [Fact]
  171. public void CanGetObjectProperties()
  172. {
  173. var p = new Person
  174. {
  175. Name = "Mickey Mouse"
  176. };
  177. _engine.SetValue("p", p);
  178. RunTest(@"
  179. assert(p.Name === 'Mickey Mouse');
  180. ");
  181. }
  182. [Fact]
  183. public void CanInvokeObjectMethods()
  184. {
  185. var p = new Person
  186. {
  187. Name = "Mickey Mouse"
  188. };
  189. _engine.SetValue("p", p);
  190. RunTest(@"
  191. assert(p.ToString() === 'Mickey Mouse');
  192. ");
  193. }
  194. [Fact]
  195. public void CanInvokeObjectMethodsWithPascalCase()
  196. {
  197. var p = new Person
  198. {
  199. Name = "Mickey Mouse"
  200. };
  201. _engine.SetValue("p", p);
  202. RunTest(@"
  203. assert(p.toString() === 'Mickey Mouse');
  204. ");
  205. }
  206. [Fact]
  207. public void CanSetObjectProperties()
  208. {
  209. var p = new Person
  210. {
  211. Name = "Mickey Mouse"
  212. };
  213. _engine.SetValue("p", p);
  214. RunTest(@"
  215. p.Name = 'Donald Duck';
  216. assert(p.Name === 'Donald Duck');
  217. ");
  218. Assert.Equal("Donald Duck", p.Name);
  219. }
  220. [Fact]
  221. public void CanGetIndexUsingStringKey()
  222. {
  223. var dictionary = new Dictionary<string, Person>();
  224. dictionary.Add("person1", new Person { Name = "Mickey Mouse" });
  225. dictionary.Add("person2", new Person { Name = "Goofy" });
  226. _engine.SetValue("dictionary", dictionary);
  227. RunTest(@"
  228. assert(dictionary['person1'].Name === 'Mickey Mouse');
  229. assert(dictionary['person2'].Name === 'Goofy');
  230. ");
  231. }
  232. [Fact]
  233. public void CanSetIndexUsingStringKey()
  234. {
  235. var dictionary = new Dictionary<string, Person>();
  236. dictionary.Add("person1", new Person { Name = "Mickey Mouse" });
  237. dictionary.Add("person2", new Person { Name = "Goofy" });
  238. _engine.SetValue("dictionary", dictionary);
  239. RunTest(@"
  240. dictionary['person2'].Name = 'Donald Duck';
  241. assert(dictionary['person2'].Name === 'Donald Duck');
  242. ");
  243. Assert.Equal("Donald Duck", dictionary["person2"].Name);
  244. }
  245. [Fact]
  246. public void CanGetIndexUsingIntegerKey()
  247. {
  248. var dictionary = new Dictionary<int, string>();
  249. dictionary.Add(1, "Mickey Mouse");
  250. dictionary.Add(2, "Goofy");
  251. _engine.SetValue("dictionary", dictionary);
  252. RunTest(@"
  253. assert(dictionary[1] === 'Mickey Mouse');
  254. assert(dictionary[2] === 'Goofy');
  255. ");
  256. }
  257. [Fact]
  258. public void CanSetIndexUsingIntegerKey()
  259. {
  260. var dictionary = new Dictionary<int, string>();
  261. dictionary.Add(1, "Mickey Mouse");
  262. dictionary.Add(2, "Goofy");
  263. _engine.SetValue("dictionary", dictionary);
  264. RunTest(@"
  265. dictionary[2] = 'Donald Duck';
  266. assert(dictionary[2] === 'Donald Duck');
  267. ");
  268. Assert.Equal("Mickey Mouse", dictionary[1]);
  269. Assert.Equal("Donald Duck", dictionary[2]);
  270. }
  271. private class DoubleIndexedClass
  272. {
  273. public int this[int index]
  274. {
  275. get { return index; }
  276. }
  277. public string this[string index]
  278. {
  279. get { return index; }
  280. }
  281. }
  282. [Fact]
  283. public void CanGetIndexUsingBothIntAndStringIndex()
  284. {
  285. var dictionary = new DoubleIndexedClass();
  286. _engine.SetValue("dictionary", dictionary);
  287. RunTest(@"
  288. assert(dictionary[1] === 1);
  289. assert(dictionary['test'] === 'test');
  290. ");
  291. }
  292. [Fact]
  293. public void CanUseGenericMethods()
  294. {
  295. var dictionary = new Dictionary<int, string>();
  296. dictionary.Add(1, "Mickey Mouse");
  297. _engine.SetValue("dictionary", dictionary);
  298. RunTest(@"
  299. dictionary.Add(2, 'Goofy');
  300. assert(dictionary[2] === 'Goofy');
  301. ");
  302. Assert.Equal("Mickey Mouse", dictionary[1]);
  303. Assert.Equal("Goofy", dictionary[2]);
  304. }
  305. [Fact]
  306. public void CanUseMultiGenericTypes()
  307. {
  308. RunTest(@"
  309. var type = System.Collections.Generic.Dictionary(System.Int32, System.String);
  310. var dictionary = new type();
  311. dictionary.Add(1, 'Mickey Mouse');
  312. dictionary.Add(2, 'Goofy');
  313. assert(dictionary[2] === 'Goofy');
  314. ");
  315. }
  316. [Fact]
  317. public void CanUseIndexOnCollection()
  318. {
  319. var collection = new System.Collections.ObjectModel.Collection<string>();
  320. collection.Add("Mickey Mouse");
  321. collection.Add("Goofy");
  322. _engine.SetValue("dictionary", collection);
  323. RunTest(@"
  324. dictionary[1] = 'Donald Duck';
  325. assert(dictionary[1] === 'Donald Duck');
  326. ");
  327. Assert.Equal("Mickey Mouse", collection[0]);
  328. Assert.Equal("Donald Duck", collection[1]);
  329. }
  330. [Fact]
  331. public void CanUseIndexOnList()
  332. {
  333. var list = new List<object>(2);
  334. list.Add("Mickey Mouse");
  335. list.Add("Goofy");
  336. _engine.SetValue("list", list);
  337. RunTest(@"
  338. list[1] = 'Donald Duck';
  339. assert(list[1] === 'Donald Duck');
  340. ");
  341. Assert.Equal("Mickey Mouse", list[0]);
  342. Assert.Equal("Donald Duck", list[1]);
  343. }
  344. [Fact]
  345. public void CanAccessAnonymousObject()
  346. {
  347. var p = new
  348. {
  349. Name = "Mickey Mouse",
  350. };
  351. _engine.SetValue("p", p);
  352. RunTest(@"
  353. assert(p.Name === 'Mickey Mouse');
  354. ");
  355. }
  356. [Fact]
  357. public void CanAccessAnonymousObjectProperties()
  358. {
  359. var p = new
  360. {
  361. Address = new
  362. {
  363. City = "Mouseton"
  364. }
  365. };
  366. _engine.SetValue("p", p);
  367. RunTest(@"
  368. assert(p.Address.City === 'Mouseton');
  369. ");
  370. }
  371. [Fact]
  372. public void PocosCanReturnJsValueDirectly()
  373. {
  374. var o = new
  375. {
  376. x = new JsNumber(1),
  377. y = new JsString("string"),
  378. };
  379. _engine.SetValue("o", o);
  380. RunTest(@"
  381. assert(o.x === 1);
  382. assert(o.y === 'string');
  383. ");
  384. }
  385. [Fact]
  386. public void PocosCanReturnObjectInstanceDirectly()
  387. {
  388. var x = new ObjectInstance(_engine);
  389. x.Set("foo", new JsString("bar"));
  390. var o = new
  391. {
  392. x
  393. };
  394. _engine.SetValue("o", o);
  395. RunTest(@"
  396. assert(o.x.foo === 'bar');
  397. ");
  398. }
  399. [Fact]
  400. public void DateTimeIsConvertedToDate()
  401. {
  402. var o = new
  403. {
  404. z = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)
  405. };
  406. _engine.SetValue("o", o);
  407. RunTest(@"
  408. assert(o.z.valueOf() === 0);
  409. ");
  410. }
  411. [Fact]
  412. public void DateTimeOffsetIsConvertedToDate()
  413. {
  414. var o = new
  415. {
  416. z = new DateTimeOffset(1970, 1, 1, 0, 0, 0, new TimeSpan())
  417. };
  418. _engine.SetValue("o", o);
  419. RunTest(@"
  420. assert(o.z.valueOf() === 0);
  421. ");
  422. }
  423. [Fact]
  424. public void EcmaValuesAreAutomaticallyConvertedWhenSetInPoco()
  425. {
  426. var p = new Person
  427. {
  428. Name = "foo",
  429. };
  430. _engine.SetValue("p", p);
  431. RunTest(@"
  432. assert(p.Name === 'foo');
  433. assert(p.Age === 0);
  434. p.Name = 'bar';
  435. p.Age = 10;
  436. ");
  437. Assert.Equal("bar", p.Name);
  438. Assert.Equal(10, p.Age);
  439. }
  440. [Fact]
  441. public void EcmaValuesAreAutomaticallyConvertedToBestMatchWhenSetInPoco()
  442. {
  443. var p = new Person
  444. {
  445. Name = "foo",
  446. };
  447. _engine.SetValue("p", p);
  448. RunTest(@"
  449. p.Name = 10;
  450. p.Age = '20';
  451. ");
  452. Assert.Equal("10", p.Name);
  453. Assert.Equal(20, p.Age);
  454. }
  455. [Fact]
  456. public void ShouldCallInstanceMethodWithoutArgument()
  457. {
  458. _engine.SetValue("a", new A());
  459. RunTest(@"
  460. assert(a.Call1() === 0);
  461. ");
  462. }
  463. [Fact]
  464. public void ShouldCallInstanceMethodOverloadArgument()
  465. {
  466. _engine.SetValue("a", new A());
  467. RunTest(@"
  468. assert(a.Call1(1) === 1);
  469. ");
  470. }
  471. [Fact]
  472. public void ShouldCallInstanceMethodWithString()
  473. {
  474. var p = new Person();
  475. _engine.SetValue("a", new A());
  476. _engine.SetValue("p", p);
  477. RunTest(@"
  478. p.Name = a.Call2('foo');
  479. assert(p.Name === 'foo');
  480. ");
  481. Assert.Equal("foo", p.Name);
  482. }
  483. [Fact]
  484. public void CanUseTrim()
  485. {
  486. var p = new Person { Name = "Mickey Mouse "};
  487. _engine.SetValue("p", p);
  488. RunTest(@"
  489. assert(p.Name === 'Mickey Mouse ');
  490. p.Name = p.Name.trim();
  491. assert(p.Name === 'Mickey Mouse');
  492. ");
  493. Assert.Equal("Mickey Mouse", p.Name);
  494. }
  495. [Fact]
  496. public void CanUseMathFloor()
  497. {
  498. var p = new Person();
  499. _engine.SetValue("p", p);
  500. RunTest(@"
  501. p.Age = Math.floor(1.6);p
  502. assert(p.Age === 1);
  503. ");
  504. Assert.Equal(1, p.Age);
  505. }
  506. [Fact]
  507. public void CanUseDelegateAsFunction()
  508. {
  509. var even = new Func<int, bool>(x => x % 2 == 0);
  510. _engine.SetValue("even", even);
  511. RunTest(@"
  512. assert(even(2) === true);
  513. ");
  514. }
  515. private struct TestStruct
  516. {
  517. public int Value;
  518. public TestStruct(int value)
  519. {
  520. Value = value;
  521. }
  522. }
  523. private class TestClass
  524. {
  525. public int? NullableInt { get; set; }
  526. public DateTime? NullableDate { get; set; }
  527. public bool? NullableBool { get; set; }
  528. public TestEnumInt32? NullableEnum { get; set; }
  529. public TestStruct? NullableStruct { get; set; }
  530. }
  531. [Fact]
  532. public void CanSetNullablePropertiesOnPocos()
  533. {
  534. var instance = new TestClass();
  535. _engine.SetValue("instance", instance);
  536. _engine.SetValue("TestStruct", typeof(TestStruct));
  537. RunTest(@"
  538. instance.NullableInt = 2;
  539. instance.NullableDate = new Date();
  540. instance.NullableBool = true;
  541. instance.NullableEnum = 1;
  542. instance.NullableStruct = new TestStruct(5);
  543. assert(instance.NullableInt===2);
  544. assert(instance.NullableDate!=null);
  545. assert(instance.NullableBool===true);
  546. assert(instance.NullableEnum===1);
  547. assert(instance.NullableStruct.Value===5);
  548. ");
  549. }
  550. private class ReadOnlyList : IReadOnlyList<Person>
  551. {
  552. private readonly Person[] _data;
  553. public ReadOnlyList(params Person[] data)
  554. {
  555. _data = data;
  556. }
  557. public IEnumerator<Person> GetEnumerator()
  558. {
  559. return ((IEnumerable<Person>) _data).GetEnumerator();
  560. }
  561. IEnumerator IEnumerable.GetEnumerator()
  562. {
  563. return _data.GetEnumerator();
  564. }
  565. public int Count => _data.Length;
  566. public Person this[int index] => _data[index];
  567. }
  568. [Fact]
  569. public void CanAddArrayPrototypeForArrayLikeClrObjects()
  570. {
  571. var e = new Engine(cfg => cfg
  572. .AllowClr(typeof(Person).Assembly)
  573. .SetWrapObjectHandler((engine, target) =>
  574. {
  575. var instance = new ObjectWrapper(engine, target);
  576. if (instance.IsArrayLike)
  577. {
  578. instance.SetPrototypeOf(engine.Array.PrototypeObject);
  579. }
  580. return instance;
  581. })
  582. );
  583. var person = new Person
  584. {
  585. Age = 12,
  586. Name = "John"
  587. };
  588. dynamic obj = new
  589. {
  590. values = new ReadOnlyList(person)
  591. };
  592. e.SetValue("o", obj);
  593. var name = e.Execute("o.values.filter(x => x.age == 12)[0].name").GetCompletionValue().ToString();
  594. Assert.Equal("John", name);
  595. }
  596. [Fact]
  597. public void CanAccessExpandoObject()
  598. {
  599. var engine = new Engine();
  600. dynamic expando = new ExpandoObject();
  601. expando.Name = "test";
  602. engine.SetValue("expando", expando);
  603. Assert.Equal("test", engine.Execute("expando.Name").GetCompletionValue().ToString());
  604. }
  605. [Fact]
  606. public void ShouldConvertArrayToArrayInstance()
  607. {
  608. var result = _engine
  609. .SetValue("values", new[] { 1, 2, 3, 4, 5, 6 })
  610. .Execute("values.filter(function(x){ return x % 2 == 0; })");
  611. var parts = result.GetCompletionValue().ToObject();
  612. Assert.True(parts.GetType().IsArray);
  613. Assert.Equal(3, ((object[])parts).Length);
  614. Assert.Equal(2d, ((object[])parts)[0]);
  615. Assert.Equal(4d, ((object[])parts)[1]);
  616. Assert.Equal(6d, ((object[])parts)[2]);
  617. }
  618. [Fact]
  619. public void ShouldConvertListsToArrayInstance()
  620. {
  621. var result = _engine
  622. .SetValue("values", new List<object> { 1, 2, 3, 4, 5, 6 })
  623. .Execute("new Array(values).filter(function(x){ return x % 2 == 0; })");
  624. var parts = result.GetCompletionValue().ToObject();
  625. Assert.True(parts.GetType().IsArray);
  626. Assert.Equal(3, ((object[])parts).Length);
  627. Assert.Equal(2d, ((object[])parts)[0]);
  628. Assert.Equal(4d, ((object[])parts)[1]);
  629. Assert.Equal(6d, ((object[])parts)[2]);
  630. }
  631. [Fact]
  632. public void ShouldConvertArrayInstanceToArray()
  633. {
  634. var result = _engine.Execute("'[email protected]'.split('@');");
  635. var parts = result.GetCompletionValue().ToObject();
  636. Assert.True(parts.GetType().IsArray);
  637. Assert.Equal(2, ((object[])parts).Length);
  638. Assert.Equal("foo", ((object[])parts)[0]);
  639. Assert.Equal("bar.com", ((object[])parts)[1]);
  640. }
  641. [Fact]
  642. public void ShouldLoopWithNativeEnumerator()
  643. {
  644. JsValue adder(JsValue argValue)
  645. {
  646. ArrayInstance args = argValue.AsArray();
  647. double sum = 0;
  648. foreach (var item in args)
  649. {
  650. if (item.IsNumber())
  651. {
  652. sum += item.AsNumber();
  653. }
  654. }
  655. return sum;
  656. }
  657. var result = _engine.SetValue("getSum", new Func<JsValue, JsValue>(adder))
  658. .Execute("getSum([1,2,3]);");
  659. Assert.True(result.GetCompletionValue() == 6);
  660. }
  661. [Fact]
  662. public void ShouldConvertBooleanInstanceToBool()
  663. {
  664. var result = _engine.Execute("new Boolean(true)");
  665. var value = result.GetCompletionValue().ToObject();
  666. Assert.Equal(typeof(bool), value.GetType());
  667. Assert.Equal(true, value);
  668. }
  669. [Fact]
  670. public void ShouldConvertDateInstanceToDateTime()
  671. {
  672. var result = _engine.Execute("new Date(0)");
  673. var value = result.GetCompletionValue().ToObject();
  674. Assert.Equal(typeof(DateTime), value.GetType());
  675. Assert.Equal(new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc), value);
  676. }
  677. [Fact]
  678. public void ShouldConvertNumberInstanceToDouble()
  679. {
  680. var result = _engine.Execute("new Number(10)");
  681. var value = result.GetCompletionValue().ToObject();
  682. Assert.Equal(typeof(double), value.GetType());
  683. Assert.Equal(10d, value);
  684. }
  685. [Fact]
  686. public void ShouldConvertStringInstanceToString()
  687. {
  688. var result = _engine.Execute("new String('foo')");
  689. var value = result.GetCompletionValue().ToObject();
  690. Assert.Equal(typeof(string), value.GetType());
  691. Assert.Equal("foo", value);
  692. }
  693. [Fact]
  694. public void ShouldConvertObjectInstanceToExpando()
  695. {
  696. _engine.Execute("var o = {a: 1, b: 'foo'}");
  697. var result = _engine.GetValue("o");
  698. dynamic value = result.ToObject();
  699. Assert.Equal(1, value.a);
  700. Assert.Equal("foo", value.b);
  701. var dic = (IDictionary<string, object>)result.ToObject();
  702. Assert.Equal(1d, dic["a"]);
  703. Assert.Equal("foo", dic["b"]);
  704. }
  705. [Fact]
  706. public void ShouldNotTryToConvertCompatibleTypes()
  707. {
  708. _engine.SetValue("a", new A());
  709. RunTest(@"
  710. assert(a.Call3('foo') === 'foo');
  711. assert(a.Call3(1) === '1');
  712. ");
  713. }
  714. [Fact]
  715. public void ShouldNotTryToConvertDerivedTypes()
  716. {
  717. _engine.SetValue("a", new A());
  718. _engine.SetValue("p", new Person { Name = "Mickey" });
  719. RunTest(@"
  720. assert(a.Call4(p) === 'Mickey');
  721. ");
  722. }
  723. [Fact]
  724. public void ShouldExecuteFunctionCallBackAsDelegate()
  725. {
  726. _engine.SetValue("a", new A());
  727. RunTest(@"
  728. assert(a.Call5(function(a,b){ return a+b }) === '1foo');
  729. ");
  730. }
  731. [Fact]
  732. public void ShouldExecuteFunctionCallBackAsFuncAndThisCanBeAssigned()
  733. {
  734. _engine.SetValue("a", new A());
  735. RunTest(@"
  736. assert(a.Call6(function(a,b){ return this+a+b }) === 'bar1foo');
  737. ");
  738. }
  739. [Fact]
  740. public void ShouldExecuteFunctionCallBackAsPredicate()
  741. {
  742. _engine.SetValue("a", new A());
  743. // Func<>
  744. RunTest(@"
  745. assert(a.Call8(function(){ return 'foo'; }) === 'foo');
  746. ");
  747. }
  748. [Fact]
  749. public void ShouldExecuteFunctionWithParameterCallBackAsPredicate()
  750. {
  751. _engine.SetValue("a", new A());
  752. // Func<,>
  753. RunTest(@"
  754. assert(a.Call7('foo', function(a){ return a === 'foo'; }) === true);
  755. ");
  756. }
  757. [Fact]
  758. public void ShouldExecuteActionCallBackAsPredicate()
  759. {
  760. _engine.SetValue("a", new A());
  761. // Action
  762. RunTest(@"
  763. var value;
  764. a.Call9(function(){ value = 'foo'; });
  765. assert(value === 'foo');
  766. ");
  767. }
  768. [Fact]
  769. public void ShouldExecuteActionWithParameterCallBackAsPredicate()
  770. {
  771. _engine.SetValue("a", new A());
  772. // Action<>
  773. RunTest(@"
  774. var value;
  775. a.Call10('foo', function(b){ value = b; });
  776. assert(value === 'foo');
  777. ");
  778. }
  779. [Fact]
  780. public void ShouldExecuteActionWithMultipleParametersCallBackAsPredicate()
  781. {
  782. _engine.SetValue("a", new A());
  783. // Action<,>
  784. RunTest(@"
  785. var value;
  786. a.Call11('foo', 'bar', function(a,b){ value = a + b; });
  787. assert(value === 'foobar');
  788. ");
  789. }
  790. [Fact]
  791. public void ShouldExecuteFunc()
  792. {
  793. _engine.SetValue("a", new A());
  794. // Func<int, int>
  795. RunTest(@"
  796. var result = a.Call12(42, function(a){ return a + a; });
  797. assert(result === 84);
  798. ");
  799. }
  800. [Fact]
  801. public void ShouldExecuteActionCallbackOnEventChanged()
  802. {
  803. var collection = new System.Collections.ObjectModel.ObservableCollection<string>();
  804. Assert.True(collection.Count == 0);
  805. _engine.SetValue("collection", collection);
  806. RunTest(@"
  807. var eventAction;
  808. collection.add_CollectionChanged(function(sender, eventArgs) { eventAction = eventArgs.Action; } );
  809. collection.Add('test');
  810. ");
  811. var eventAction = _engine.GetValue("eventAction").AsNumber();
  812. Assert.True(eventAction == 0);
  813. Assert.True(collection.Count == 1);
  814. }
  815. [Fact]
  816. public void ShouldUseSystemIO()
  817. {
  818. RunTest(@"
  819. var filename = System.IO.Path.GetTempFileName();
  820. var sw = System.IO.File.CreateText(filename);
  821. sw.Write('Hello World');
  822. sw.Dispose();
  823. var content = System.IO.File.ReadAllText(filename);
  824. System.Console.WriteLine(content);
  825. assert(content === 'Hello World');
  826. ");
  827. }
  828. [Fact]
  829. public void ShouldBeInstanceOfTypeReferenceType()
  830. {
  831. _engine.SetValue("A", typeof(A));
  832. RunTest(@"
  833. var a = new A();
  834. assert(a instanceof A);
  835. ");
  836. }
  837. [Fact]
  838. public void ShouldImportNamespace()
  839. {
  840. RunTest(@"
  841. var Shapes = importNamespace('Shapes');
  842. var circle = new Shapes.Circle();
  843. assert(circle.Radius === 0);
  844. assert(circle.Perimeter() === 0);
  845. ");
  846. }
  847. [Fact]
  848. public void ShouldConstructReferenceTypeWithParameters()
  849. {
  850. RunTest(@"
  851. var Shapes = importNamespace('Shapes');
  852. var circle = new Shapes.Circle(1);
  853. assert(circle.Radius === 1);
  854. assert(circle.Perimeter() === Math.PI);
  855. ");
  856. }
  857. [Fact]
  858. public void ShouldConstructValueTypeWithoutParameters()
  859. {
  860. RunTest(@"
  861. var guid = new System.Guid();
  862. assert('00000000-0000-0000-0000-000000000000' === guid.ToString());
  863. ");
  864. }
  865. [Fact]
  866. public void ShouldInvokeAFunctionByName()
  867. {
  868. RunTest(@"
  869. function add(x, y) { return x + y; }
  870. ");
  871. Assert.Equal(3, _engine.Invoke("add", 1, 2));
  872. }
  873. [Fact]
  874. public void ShouldNotInvokeNonFunctionValue()
  875. {
  876. RunTest(@"
  877. var x= 10;
  878. ");
  879. Assert.Throws<ArgumentException>(() => _engine.Invoke("x", 1, 2));
  880. }
  881. [Fact]
  882. public void CanGetField()
  883. {
  884. var o = new ClassWithField
  885. {
  886. Field = "Mickey Mouse"
  887. };
  888. _engine.SetValue("o", o);
  889. RunTest(@"
  890. assert(o.Field === 'Mickey Mouse');
  891. ");
  892. }
  893. [Fact]
  894. public void CanSetField()
  895. {
  896. var o = new ClassWithField();
  897. _engine.SetValue("o", o);
  898. RunTest(@"
  899. o.Field = 'Mickey Mouse';
  900. assert(o.Field === 'Mickey Mouse');
  901. ");
  902. Assert.Equal("Mickey Mouse", o.Field);
  903. }
  904. [Fact]
  905. public void CanGetStaticField()
  906. {
  907. RunTest(@"
  908. var domain = importNamespace('Jint.Tests.Runtime.Domain');
  909. var statics = domain.ClassWithStaticFields;
  910. assert(statics.Get == 'Get');
  911. ");
  912. }
  913. [Fact]
  914. public void CanSetStaticField()
  915. {
  916. RunTest(@"
  917. var domain = importNamespace('Jint.Tests.Runtime.Domain');
  918. var statics = domain.ClassWithStaticFields;
  919. statics.Set = 'hello';
  920. assert(statics.Set == 'hello');
  921. ");
  922. Assert.Equal(ClassWithStaticFields.Set, "hello");
  923. }
  924. [Fact]
  925. public void CanGetStaticAccessor()
  926. {
  927. RunTest(@"
  928. var domain = importNamespace('Jint.Tests.Runtime.Domain');
  929. var statics = domain.ClassWithStaticFields;
  930. assert(statics.Getter == 'Getter');
  931. ");
  932. }
  933. [Fact]
  934. public void CanSetStaticAccessor()
  935. {
  936. RunTest(@"
  937. var domain = importNamespace('Jint.Tests.Runtime.Domain');
  938. var statics = domain.ClassWithStaticFields;
  939. statics.Setter = 'hello';
  940. assert(statics.Setter == 'hello');
  941. ");
  942. Assert.Equal(ClassWithStaticFields.Setter, "hello");
  943. }
  944. [Fact]
  945. public void CantSetStaticReadonly()
  946. {
  947. RunTest(@"
  948. var domain = importNamespace('Jint.Tests.Runtime.Domain');
  949. var statics = domain.ClassWithStaticFields;
  950. statics.Readonly = 'hello';
  951. assert(statics.Readonly == 'Readonly');
  952. ");
  953. Assert.Equal(ClassWithStaticFields.Readonly, "Readonly");
  954. }
  955. [Fact]
  956. public void CanSetCustomConverters()
  957. {
  958. var engine1 = new Engine();
  959. engine1.SetValue("p", new { Test = true });
  960. engine1.Execute("var result = p.Test;");
  961. Assert.True((bool)engine1.GetValue("result").ToObject());
  962. var engine2 = new Engine(o => o.AddObjectConverter(new NegateBoolConverter()));
  963. engine2.SetValue("p", new { Test = true });
  964. engine2.Execute("var result = p.Test;");
  965. Assert.False((bool)engine2.GetValue("result").ToObject());
  966. }
  967. [Fact]
  968. public void CanConvertEnumsToString()
  969. {
  970. var engine1 = new Engine(o => o.AddObjectConverter(new EnumsToStringConverter()))
  971. .SetValue("assert", new Action<bool>(Assert.True));
  972. engine1.SetValue("p", new { Comparison = StringComparison.CurrentCulture });
  973. engine1.Execute("assert(p.Comparison === 'CurrentCulture');");
  974. engine1.Execute("var result = p.Comparison;");
  975. Assert.Equal("CurrentCulture", (string)engine1.GetValue("result").ToObject());
  976. }
  977. [Fact]
  978. public void CanUserIncrementOperator()
  979. {
  980. var p = new Person
  981. {
  982. Age = 1,
  983. };
  984. _engine.SetValue("p", p);
  985. RunTest(@"
  986. assert(++p.Age === 2);
  987. ");
  988. Assert.Equal(2, p.Age);
  989. }
  990. [Fact]
  991. public void CanOverwriteValues()
  992. {
  993. _engine.SetValue("x", 3);
  994. _engine.SetValue("x", 4);
  995. RunTest(@"
  996. assert(x === 4);
  997. ");
  998. }
  999. [Fact]
  1000. public void ShouldCreateGenericType()
  1001. {
  1002. RunTest(@"
  1003. var ListOfString = System.Collections.Generic.List(System.String);
  1004. var list = new ListOfString();
  1005. list.Add('foo');
  1006. list.Add(1);
  1007. assert(2 === list.Count);
  1008. ");
  1009. }
  1010. [Fact]
  1011. public void EnumComparesByName()
  1012. {
  1013. var o = new
  1014. {
  1015. r = Colors.Red,
  1016. b = Colors.Blue,
  1017. g = Colors.Green,
  1018. b2 = Colors.Red
  1019. };
  1020. _engine.SetValue("o", o);
  1021. _engine.SetValue("assertFalse", new Action<bool>(Assert.False));
  1022. RunTest(@"
  1023. var domain = importNamespace('Jint.Tests.Runtime.Domain');
  1024. var colors = domain.Colors;
  1025. assert(o.r === colors.Red);
  1026. assert(o.g === colors.Green);
  1027. assert(o.b === colors.Blue);
  1028. assertFalse(o.b2 === colors.Blue);
  1029. ");
  1030. }
  1031. [Fact]
  1032. public void ShouldSetEnumProperty()
  1033. {
  1034. var s = new Circle
  1035. {
  1036. Color = Colors.Red,
  1037. };
  1038. _engine.SetValue("s", s);
  1039. RunTest(@"
  1040. var domain = importNamespace('Jint.Tests.Runtime.Domain');
  1041. var colors = domain.Colors;
  1042. s.Color = colors.Blue;
  1043. assert(s.Color === colors.Blue);
  1044. ");
  1045. _engine.SetValue("s", s);
  1046. RunTest(@"
  1047. s.Color = colors.Blue | colors.Green;
  1048. assert(s.Color === colors.Blue | colors.Green);
  1049. ");
  1050. Assert.Equal(Colors.Blue | Colors.Green, s.Color);
  1051. }
  1052. enum TestEnumInt32 : int
  1053. {
  1054. None,
  1055. One = 1,
  1056. Min = int.MaxValue,
  1057. Max = int.MaxValue,
  1058. }
  1059. enum TestEnumUInt32 : uint
  1060. {
  1061. None,
  1062. One = 1,
  1063. Min = uint.MaxValue,
  1064. Max = uint.MaxValue,
  1065. }
  1066. enum TestEnumInt64 : long
  1067. {
  1068. None,
  1069. One = 1,
  1070. Min = long.MaxValue,
  1071. Max = long.MaxValue,
  1072. }
  1073. enum TestEnumUInt64 : ulong
  1074. {
  1075. None,
  1076. One = 1,
  1077. Min = ulong.MaxValue,
  1078. Max = ulong.MaxValue,
  1079. }
  1080. void TestEnum<T>(T enumValue)
  1081. {
  1082. object i = Convert.ChangeType(enumValue, Enum.GetUnderlyingType(typeof(T)));
  1083. string s = Convert.ToString(i, CultureInfo.InvariantCulture);
  1084. var o = new Tuple<T>(enumValue);
  1085. _engine.SetValue("o", o);
  1086. RunTest("assert(o.Item1 === " + s + ");");
  1087. }
  1088. [Fact]
  1089. public void ShouldWorkWithEnumInt32()
  1090. {
  1091. TestEnum(TestEnumInt32.None);
  1092. TestEnum(TestEnumInt32.One);
  1093. TestEnum(TestEnumInt32.Min);
  1094. TestEnum(TestEnumInt32.Max);
  1095. }
  1096. [Fact]
  1097. public void ShouldWorkWithEnumUInt32()
  1098. {
  1099. TestEnum(TestEnumUInt32.None);
  1100. TestEnum(TestEnumUInt32.One);
  1101. TestEnum(TestEnumUInt32.Min);
  1102. TestEnum(TestEnumUInt32.Max);
  1103. }
  1104. [Fact]
  1105. public void ShouldWorkWithEnumInt64()
  1106. {
  1107. TestEnum(TestEnumInt64.None);
  1108. TestEnum(TestEnumInt64.One);
  1109. TestEnum(TestEnumInt64.Min);
  1110. TestEnum(TestEnumInt64.Max);
  1111. }
  1112. [Fact]
  1113. public void ShouldWorkWithEnumUInt64()
  1114. {
  1115. TestEnum(TestEnumUInt64.None);
  1116. TestEnum(TestEnumUInt64.One);
  1117. TestEnum(TestEnumUInt64.Min);
  1118. TestEnum(TestEnumUInt64.Max);
  1119. }
  1120. [Fact]
  1121. public void EnumIsConvertedToNumber()
  1122. {
  1123. var o = new
  1124. {
  1125. r = Colors.Red,
  1126. b = Colors.Blue,
  1127. g = Colors.Green
  1128. };
  1129. _engine.SetValue("o", o);
  1130. RunTest(@"
  1131. assert(o.r === 0);
  1132. assert(o.g === 1);
  1133. assert(o.b === 10);
  1134. ");
  1135. }
  1136. [Fact]
  1137. public void ShouldConvertToEnum()
  1138. {
  1139. var s = new Circle
  1140. {
  1141. Color = Colors.Red,
  1142. };
  1143. _engine.SetValue("s", s);
  1144. RunTest(@"
  1145. assert(s.Color === 0);
  1146. s.Color = 10;
  1147. assert(s.Color === 10);
  1148. ");
  1149. _engine.SetValue("s", s);
  1150. RunTest(@"
  1151. s.Color = 11;
  1152. assert(s.Color === 11);
  1153. ");
  1154. Assert.Equal(Colors.Blue | Colors.Green, s.Color);
  1155. }
  1156. [Fact]
  1157. public void ShouldUseExplicitPropertyGetter()
  1158. {
  1159. _engine.SetValue("c", new Company("ACME"));
  1160. RunTest(@"
  1161. assert(c.Name === 'ACME');
  1162. ");
  1163. }
  1164. [Fact]
  1165. public void ShouldUseExplicitIndexerPropertyGetter()
  1166. {
  1167. var company = new Company("ACME");
  1168. ((ICompany)company)["Foo"] = "Bar";
  1169. _engine.SetValue("c", company);
  1170. RunTest(@"
  1171. assert(c.Foo === 'Bar');
  1172. ");
  1173. }
  1174. [Fact]
  1175. public void ShouldUseExplicitPropertySetter()
  1176. {
  1177. _engine.SetValue("c", new Company("ACME"));
  1178. RunTest(@"
  1179. c.Name = 'Foo';
  1180. assert(c.Name === 'Foo');
  1181. ");
  1182. }
  1183. [Fact]
  1184. public void ShouldUseExplicitIndexerPropertySetter()
  1185. {
  1186. var company = new Company("ACME");
  1187. ((ICompany)company)["Foo"] = "Bar";
  1188. _engine.SetValue("c", company);
  1189. RunTest(@"
  1190. c.Foo = 'Baz';
  1191. assert(c.Foo === 'Baz');
  1192. ");
  1193. }
  1194. [Fact]
  1195. public void ShouldUseExplicitMethod()
  1196. {
  1197. _engine.SetValue("c", new Company("ACME"));
  1198. RunTest(@"
  1199. assert(0 === c.CompareTo(c));
  1200. ");
  1201. }
  1202. [Fact]
  1203. public void ShouldCallInstanceMethodWithParams()
  1204. {
  1205. _engine.SetValue("a", new A());
  1206. RunTest(@"
  1207. assert(a.Call13('1','2','3') === '1,2,3');
  1208. assert(a.Call13('1') === '1');
  1209. assert(a.Call13(1) === '1');
  1210. assert(a.Call13() === '');
  1211. assert(a.Call14('a','1','2','3') === 'a:1,2,3');
  1212. assert(a.Call14('a','1') === 'a:1');
  1213. assert(a.Call14('a') === 'a:');
  1214. function call13wrapper(){ return a.Call13.apply(a, Array.prototype.slice.call(arguments)); }
  1215. assert(call13wrapper('1','2','3') === '1,2,3');
  1216. assert(a.Call13('1','2','3') === a.Call13(['1','2','3']));
  1217. ");
  1218. }
  1219. [Fact]
  1220. public void ShouldCallInstanceMethodWithJsValueParams()
  1221. {
  1222. _engine.SetValue("a", new A());
  1223. RunTest(@"
  1224. assert(a.Call16('1','2','3') === '1,2,3');
  1225. assert(a.Call16('1') === '1');
  1226. assert(a.Call16(1) === '1');
  1227. assert(a.Call16() === '');
  1228. assert(a.Call16('1','2','3') === a.Call16(['1','2','3']));
  1229. ");
  1230. }
  1231. [Fact]
  1232. public void NullValueAsArgumentShouldWork()
  1233. {
  1234. _engine.SetValue("a", new A());
  1235. RunTest(@"
  1236. var x = a.Call2(null);
  1237. assert(x === null);
  1238. ");
  1239. }
  1240. [Fact]
  1241. public void ShouldSetPropertyToNull()
  1242. {
  1243. var p = new Person { Name = "Mickey" };
  1244. _engine.SetValue("p", p);
  1245. RunTest(@"
  1246. assert(p.Name != null);
  1247. p.Name = null;
  1248. assert(p.Name == null);
  1249. ");
  1250. Assert.True(p.Name == null);
  1251. }
  1252. [Fact]
  1253. public void ShouldCallMethodWithNull()
  1254. {
  1255. _engine.SetValue("a", new A());
  1256. RunTest(@"
  1257. a.Call15(null);
  1258. var result = a.Call2(null);
  1259. assert(result == null);
  1260. ");
  1261. }
  1262. [Fact]
  1263. public void ShouldReturnUndefinedProperty()
  1264. {
  1265. _engine.SetValue("uo", new { foo = "bar" });
  1266. _engine.SetValue("ud", new Dictionary<string, object> { {"foo", "bar"} });
  1267. _engine.SetValue("ul", new List<string> { "foo", "bar" });
  1268. RunTest(@"
  1269. assert(!uo.undefinedProperty);
  1270. assert(!ul[5]);
  1271. assert(!ud.undefinedProperty);
  1272. ");
  1273. }
  1274. private class FailingObject2
  1275. {
  1276. public int this[int index] => throw new ArgumentException("index is bad", nameof(index));
  1277. }
  1278. [Fact]
  1279. public void ShouldPropagateIndexerExceptions()
  1280. {
  1281. var engine = new Engine();
  1282. engine.Execute(@"function f2(obj) { return obj[1]; }");
  1283. var failingObject = new FailingObject2();
  1284. Assert.Throws<ArgumentException>(() => engine.Invoke("f2", failingObject));
  1285. }
  1286. [Fact]
  1287. public void ShouldAutomaticallyConvertArraysToFindBestInteropResolution()
  1288. {
  1289. _engine.SetValue("a", new ArrayConverterTestClass());
  1290. _engine.SetValue("item1", new ArrayConverterItem(1));
  1291. _engine.SetValue("item2", new ArrayConverterItem(2));
  1292. RunTest(@"
  1293. assert(a.MethodAcceptsArrayOfInt([false, '1', 2]) === a.MethodAcceptsArrayOfInt([0, 1, 2]));
  1294. assert(a.MethodAcceptsArrayOfStrings(['1', 2]) === a.MethodAcceptsArrayOfStrings([1, 2]));
  1295. assert(a.MethodAcceptsArrayOfBool(['1', 0]) === a.MethodAcceptsArrayOfBool([true, false]));
  1296. assert(a.MethodAcceptsArrayOfStrings([item1, item2]) === a.MethodAcceptsArrayOfStrings(['1', '2']));
  1297. assert(a.MethodAcceptsArrayOfInt([item1, item2]) === a.MethodAcceptsArrayOfInt([1, 2]));
  1298. ");
  1299. }
  1300. [Fact]
  1301. public void ShouldImportNamespaceNestedType()
  1302. {
  1303. RunTest(@"
  1304. var shapes = importNamespace('Shapes.Circle');
  1305. var kinds = shapes.Kind;
  1306. assert(kinds.Unit === 0);
  1307. assert(kinds.Ellipse === 1);
  1308. assert(kinds.Round === 5);
  1309. ");
  1310. }
  1311. [Fact]
  1312. public void ShouldImportNamespaceNestedNestedType()
  1313. {
  1314. RunTest(@"
  1315. var meta = importNamespace('Shapes.Circle.Meta');
  1316. var usages = meta.Usage;
  1317. assert(usages.Public === 0);
  1318. assert(usages.Private === 1);
  1319. assert(usages.Internal === 11);
  1320. ");
  1321. }
  1322. [Fact]
  1323. public void ShouldGetNestedNestedProp()
  1324. {
  1325. RunTest(@"
  1326. var meta = importNamespace('Shapes.Circle');
  1327. var m = new meta.Meta();
  1328. assert(m.Description === 'descp');
  1329. ");
  1330. }
  1331. [Fact]
  1332. public void ShouldSetNestedNestedProp()
  1333. {
  1334. RunTest(@"
  1335. var meta = importNamespace('Shapes.Circle');
  1336. var m = new meta.Meta();
  1337. m.Description = 'hello';
  1338. assert(m.Description === 'hello');
  1339. ");
  1340. }
  1341. [Fact]
  1342. public void CanGetStaticNestedField()
  1343. {
  1344. RunTest(@"
  1345. var domain = importNamespace('Jint.Tests.Runtime.Domain.Nested');
  1346. var statics = domain.ClassWithStaticFields;
  1347. assert(statics.Get == 'Get');
  1348. ");
  1349. }
  1350. [Fact]
  1351. public void CanSetStaticNestedField()
  1352. {
  1353. RunTest(@"
  1354. var domain = importNamespace('Jint.Tests.Runtime.Domain.Nested');
  1355. var statics = domain.ClassWithStaticFields;
  1356. statics.Set = 'hello';
  1357. assert(statics.Set == 'hello');
  1358. ");
  1359. Assert.Equal(Nested.ClassWithStaticFields.Set, "hello");
  1360. }
  1361. [Fact]
  1362. public void CanGetStaticNestedAccessor()
  1363. {
  1364. RunTest(@"
  1365. var domain = importNamespace('Jint.Tests.Runtime.Domain.Nested');
  1366. var statics = domain.ClassWithStaticFields;
  1367. assert(statics.Getter == 'Getter');
  1368. ");
  1369. }
  1370. [Fact]
  1371. public void CanSetStaticNestedAccessor()
  1372. {
  1373. RunTest(@"
  1374. var domain = importNamespace('Jint.Tests.Runtime.Domain.Nested');
  1375. var statics = domain.ClassWithStaticFields;
  1376. statics.Setter = 'hello';
  1377. assert(statics.Setter == 'hello');
  1378. ");
  1379. Assert.Equal(Nested.ClassWithStaticFields.Setter, "hello");
  1380. }
  1381. [Fact]
  1382. public void CantSetStaticNestedReadonly()
  1383. {
  1384. RunTest(@"
  1385. var domain = importNamespace('Jint.Tests.Runtime.Domain.Nested');
  1386. var statics = domain.ClassWithStaticFields;
  1387. statics.Readonly = 'hello';
  1388. assert(statics.Readonly == 'Readonly');
  1389. ");
  1390. Assert.Equal(Nested.ClassWithStaticFields.Readonly, "Readonly");
  1391. }
  1392. [Fact]
  1393. public void ShouldExecuteFunctionWithValueTypeParameterCorrectly()
  1394. {
  1395. _engine.SetValue("a", new A());
  1396. // Func<int, int>
  1397. RunTest(@"
  1398. assert(a.Call17(function(value){ return value; }) === 17);
  1399. ");
  1400. }
  1401. [Fact]
  1402. public void ShouldExecuteActionWithValueTypeParameterCorrectly()
  1403. {
  1404. _engine.SetValue("a", new A());
  1405. // Action<int>
  1406. RunTest(@"
  1407. a.Call18(function(value){ assert(value === 18); });
  1408. ");
  1409. }
  1410. [Fact]
  1411. public void ShouldConvertToJsValue()
  1412. {
  1413. RunTest(@"
  1414. var now = System.DateTime.Now;
  1415. assert(new String(now) == now.toString());
  1416. var zero = System.Int32.MaxValue;
  1417. assert(new String(zero) == zero.toString());
  1418. ");
  1419. }
  1420. [Fact]
  1421. public void ShouldNotCatchClrExceptions()
  1422. {
  1423. var engine = new Engine()
  1424. .SetValue("throwMyException", new Action(() => { throw new NotSupportedException(); }))
  1425. .SetValue("Thrower", typeof(Thrower))
  1426. .Execute(@"
  1427. function throwException1(){
  1428. try {
  1429. throwMyException();
  1430. return;
  1431. }
  1432. catch(e) {
  1433. return;
  1434. }
  1435. }
  1436. function throwException2(){
  1437. try {
  1438. new Thrower().ThrowNotSupportedException();
  1439. return;
  1440. }
  1441. catch(e) {
  1442. return;
  1443. }
  1444. }
  1445. ");
  1446. Assert.ThrowsAny<NotSupportedException>(() => engine.Invoke("throwException1"));
  1447. Assert.ThrowsAny<NotSupportedException>(() => engine.Invoke("throwException2"));
  1448. }
  1449. [Fact]
  1450. public void ShouldCatchAllClrExceptions()
  1451. {
  1452. string exceptionMessage = "myExceptionMessage";
  1453. var engine = new Engine(o => o.CatchClrExceptions())
  1454. .SetValue("throwMyException", new Action(() => { throw new Exception(exceptionMessage); }))
  1455. .SetValue("Thrower", typeof(Thrower))
  1456. .Execute(@"
  1457. function throwException1(){
  1458. try {
  1459. throwMyException();
  1460. return '';
  1461. }
  1462. catch(e) {
  1463. return e.message;
  1464. }
  1465. }
  1466. function throwException2(){
  1467. try {
  1468. new Thrower().ThrowExceptionWithMessage('myExceptionMessage');
  1469. return;
  1470. }
  1471. catch(e) {
  1472. return e.message;
  1473. }
  1474. }
  1475. ");
  1476. Assert.Equal(engine.Invoke("throwException1").AsString(), exceptionMessage);
  1477. Assert.Equal(engine.Invoke("throwException2").AsString(), exceptionMessage);
  1478. }
  1479. class MemberExceptionTest
  1480. {
  1481. public MemberExceptionTest(bool throwOnCreate)
  1482. {
  1483. if (throwOnCreate)
  1484. throw new InvalidOperationException();
  1485. }
  1486. public JsValue ThrowingProperty1
  1487. {
  1488. get { throw new InvalidOperationException(); }
  1489. set { throw new InvalidOperationException(); }
  1490. }
  1491. public object ThrowingProperty2
  1492. {
  1493. get { throw new InvalidOperationException(); }
  1494. set { throw new InvalidOperationException(); }
  1495. }
  1496. public void ThrowingFunction()
  1497. {
  1498. throw new InvalidOperationException();
  1499. }
  1500. }
  1501. [Fact]
  1502. public void ShouldCatchClrMemberExceptions()
  1503. {
  1504. var engine = new Engine(cfg =>
  1505. {
  1506. cfg.AllowClr();
  1507. cfg.CatchClrExceptions();
  1508. });
  1509. engine.SetValue("assert", new Action<bool>(Assert.True));
  1510. engine.SetValue("log", new Action<object>(Console.WriteLine));
  1511. engine.SetValue("create", typeof(MemberExceptionTest));
  1512. engine.SetValue("instance", new MemberExceptionTest(throwOnCreate: false));
  1513. // Test calling a constructor that throws an exception
  1514. engine.Execute(@"
  1515. try
  1516. {
  1517. create(true);
  1518. assert(false);
  1519. }
  1520. catch (e)
  1521. {
  1522. assert(true);
  1523. }
  1524. ");
  1525. // Test calling a member function that throws an exception
  1526. engine.Execute(@"
  1527. try
  1528. {
  1529. instance.ThrowingFunction();
  1530. assert(false);
  1531. }
  1532. catch (e)
  1533. {
  1534. assert(true);
  1535. }
  1536. ");
  1537. // Test using a property getter that throws an exception
  1538. engine.Execute(@"
  1539. try
  1540. {
  1541. log(o.ThrowingProperty);
  1542. assert(false);
  1543. }
  1544. catch (e)
  1545. {
  1546. assert(true);
  1547. }
  1548. ");
  1549. // Test using a property setter that throws an exception
  1550. engine.Execute(@"
  1551. try
  1552. {
  1553. instance.ThrowingProperty1 = 123;
  1554. assert(false);
  1555. }
  1556. catch (e)
  1557. {
  1558. assert(true);
  1559. }
  1560. try
  1561. {
  1562. instance.ThrowingProperty2 = 456;
  1563. assert(false);
  1564. }
  1565. catch (e)
  1566. {
  1567. assert(true);
  1568. }
  1569. ");
  1570. }
  1571. [Fact]
  1572. public void ShouldCatchSomeExceptions()
  1573. {
  1574. string exceptionMessage = "myExceptionMessage";
  1575. var engine = new Engine(o => o.CatchClrExceptions(e => e is NotSupportedException))
  1576. .SetValue("throwMyException1", new Action(() => { throw new NotSupportedException(exceptionMessage); }))
  1577. .SetValue("throwMyException2", new Action(() => { throw new ArgumentNullException(); }))
  1578. .SetValue("Thrower", typeof(Thrower))
  1579. .Execute(@"
  1580. function throwException1(){
  1581. try {
  1582. throwMyException1();
  1583. return '';
  1584. }
  1585. catch(e) {
  1586. return e.message;
  1587. }
  1588. }
  1589. function throwException2(){
  1590. try {
  1591. throwMyException2();
  1592. return '';
  1593. }
  1594. catch(e) {
  1595. return e.message;
  1596. }
  1597. }
  1598. function throwException3(){
  1599. try {
  1600. new Thrower().ThrowNotSupportedExceptionWithMessage('myExceptionMessage');
  1601. return '';
  1602. }
  1603. catch(e) {
  1604. return e.message;
  1605. }
  1606. }
  1607. function throwException4(){
  1608. try {
  1609. new Thrower().ThrowArgumentNullException();
  1610. return '';
  1611. }
  1612. catch(e) {
  1613. return e.message;
  1614. }
  1615. }
  1616. ");
  1617. Assert.Equal(engine.Invoke("throwException1").AsString(), exceptionMessage);
  1618. Assert.Throws<ArgumentNullException>(() => engine.Invoke("throwException2"));
  1619. Assert.Equal(engine.Invoke("throwException3").AsString(), exceptionMessage);
  1620. Assert.Throws<ArgumentNullException>(() => engine.Invoke("throwException4"));
  1621. }
  1622. [Fact]
  1623. public void ArrayFromShouldConvertListToArrayLike()
  1624. {
  1625. var list = new List<Person>
  1626. {
  1627. new Person {Name = "Mike"},
  1628. new Person {Name = "Mika"}
  1629. };
  1630. _engine.SetValue("a", list);
  1631. RunTest(@"
  1632. var arr = new Array(a);
  1633. assert(arr.length === 2);
  1634. assert(arr[0].Name === 'Mike');
  1635. assert(arr[1].Name === 'Mika');
  1636. ");
  1637. RunTest(@"
  1638. var arr = Array.from(a);
  1639. assert(arr.length === 2);
  1640. assert(arr[0].Name === 'Mike');
  1641. assert(arr[1].Name === 'Mika');
  1642. ");
  1643. }
  1644. [Fact]
  1645. public void ArrayFromShouldConvertArrayToArrayLike()
  1646. {
  1647. var list = new []
  1648. {
  1649. new Person {Name = "Mike"},
  1650. new Person {Name = "Mika"}
  1651. };
  1652. _engine.SetValue("a", list);
  1653. RunTest(@"
  1654. var arr = new Array(a);
  1655. assert(arr.length === 2);
  1656. assert(arr[0].Name === 'Mike');
  1657. assert(arr[1].Name === 'Mika');
  1658. ");
  1659. RunTest(@"
  1660. var arr = Array.from(a);
  1661. assert(arr.length === 2);
  1662. assert(arr[0].Name === 'Mike');
  1663. assert(arr[1].Name === 'Mika');
  1664. ");
  1665. }
  1666. [Fact]
  1667. public void ArrayFromShouldConvertIEnumerable()
  1668. {
  1669. var enumerable = new []
  1670. {
  1671. new Person {Name = "Mike"},
  1672. new Person {Name = "Mika"}
  1673. }.Select(x => x);
  1674. _engine.SetValue("a", enumerable);
  1675. RunTest(@"
  1676. var arr = new Array(a);
  1677. assert(arr.length === 2);
  1678. assert(arr[0].Name === 'Mike');
  1679. assert(arr[1].Name === 'Mika');
  1680. ");
  1681. RunTest(@"
  1682. var arr = Array.from(a);
  1683. assert(arr.length === 2);
  1684. assert(arr[0].Name === 'Mike');
  1685. assert(arr[1].Name === 'Mika');
  1686. ");
  1687. }
  1688. [Fact]
  1689. public void ShouldBeAbleToPlusAssignStringProperty()
  1690. {
  1691. var p = new Person();
  1692. var engine = new Engine();
  1693. engine.SetValue("P", p);
  1694. engine.Execute("P.Name = 'b';");
  1695. engine.Execute("P.Name += 'c';");
  1696. Assert.Equal("bc", p.Name);
  1697. }
  1698. [Fact]
  1699. public void ShouldNotResolveToPrimitiveSymbol()
  1700. {
  1701. var engine = new Engine(options =>
  1702. options.AllowClr(typeof(FloatIndexer).GetTypeInfo().Assembly));
  1703. var c = engine.Execute(@"
  1704. var domain = importNamespace('Jint.Tests.Runtime.Domain');
  1705. return new domain.FloatIndexer();
  1706. ").GetCompletionValue();
  1707. Assert.NotNull(c.ToString());
  1708. Assert.Equal((uint)0, c.As<ObjectInstance>().Length);
  1709. }
  1710. class DictionaryWrapper
  1711. {
  1712. public IDictionary<string, object> Values { get; set; }
  1713. }
  1714. class DictionaryTest
  1715. {
  1716. public void Test1(IDictionary<string, object> values)
  1717. {
  1718. Assert.Equal(1, Convert.ToInt32(values["a"]));
  1719. }
  1720. public void Test2(DictionaryWrapper dictionaryObject)
  1721. {
  1722. Assert.Equal(1, Convert.ToInt32(dictionaryObject.Values["a"]));
  1723. }
  1724. }
  1725. [Fact]
  1726. public void ShouldBeAbleToPassDictionaryToMethod()
  1727. {
  1728. var engine = new Engine();
  1729. engine.SetValue("dictionaryTest", new DictionaryTest());
  1730. engine.Execute("dictionaryTest.test1({ a: 1 });");
  1731. }
  1732. [Fact]
  1733. public void ShouldBeAbleToPassDictionaryInObjectToMethod()
  1734. {
  1735. var engine = new Engine();
  1736. engine.SetValue("dictionaryTest", new DictionaryTest());
  1737. engine.Execute("dictionaryTest.test2({ values: { a: 1 } });");
  1738. }
  1739. [Fact]
  1740. public void ShouldSupportSpreadForDictionary()
  1741. {
  1742. var engine = new Engine();
  1743. var state = new Dictionary<string, object>
  1744. {
  1745. {"invoice", new Dictionary<string, object> {["number"] = "42"}}
  1746. };
  1747. engine.SetValue("state", state);
  1748. var result = (IDictionary<string, object>) engine
  1749. .Execute("({ supplier: 'S1', ...state.invoice })")
  1750. .GetCompletionValue()
  1751. .ToObject();
  1752. Assert.Equal("S1", result["supplier"]);
  1753. Assert.Equal("42", result["number"]);
  1754. }
  1755. [Fact]
  1756. public void ShouldSupportSpreadForDictionary2()
  1757. {
  1758. var engine = new Engine();
  1759. var state = new Dictionary<string, object>
  1760. {
  1761. {"invoice", new Dictionary<string, object> {["number"] = "42"}}
  1762. };
  1763. engine.SetValue("state", state);
  1764. var result = (IDictionary<string, object>) engine
  1765. .Execute("function getValue() { return {supplier: 'S1', ...state.invoice}; }")
  1766. .Invoke("getValue")
  1767. .ToObject();
  1768. Assert.Equal("S1", result["supplier"]);
  1769. Assert.Equal("42", result["number"]);
  1770. }
  1771. [Fact]
  1772. public void ShouldSupportSpreadForObject()
  1773. {
  1774. var engine = new Engine();
  1775. var person = new Person
  1776. {
  1777. Name = "Mike",
  1778. Age = 20
  1779. };
  1780. engine.SetValue("p", person);
  1781. var result = (IDictionary<string, object>) engine
  1782. .Execute("({ supplier: 'S1', ...p })")
  1783. .GetCompletionValue()
  1784. .ToObject();
  1785. Assert.Equal("S1", result["supplier"]);
  1786. Assert.Equal("Mike", result["Name"]);
  1787. Assert.Equal(20d, result["Age"]);
  1788. }
  1789. [Fact]
  1790. public void ShouldBeAbleToJsonStringifyClrObjects()
  1791. {
  1792. var engine = new Engine();
  1793. engine.Execute("var jsObj = { 'key1' :'value1', 'key2' : 'value2' }");
  1794. engine.SetValue("netObj", new Dictionary<string, object>()
  1795. {
  1796. {"key1", "value1"},
  1797. {"key2", "value2"},
  1798. });
  1799. var jsValue = engine.Execute("jsObj['key1']").GetCompletionValue().AsString();
  1800. var clrValue = engine.Execute("netObj['key1']").GetCompletionValue().AsString();
  1801. Assert.Equal(jsValue, clrValue);
  1802. jsValue = engine.Execute("JSON.stringify(jsObj)").GetCompletionValue().AsString();
  1803. clrValue = engine.Execute("JSON.stringify(netObj)").GetCompletionValue().AsString();
  1804. Assert.Equal(jsValue, clrValue);
  1805. // Write properties on screen using showProps function defined on https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Working_with_Objects
  1806. engine.Execute(@"function showProps(obj, objName) {
  1807. var result = """";
  1808. for (var i in obj) {
  1809. if (obj.hasOwnProperty(i)) {
  1810. result += objName + ""."" + i + "" = "" + obj[i] + ""\n"";
  1811. }
  1812. }
  1813. return result;
  1814. }");
  1815. jsValue = engine.Execute("showProps(jsObj, 'theObject')").GetCompletionValue().AsString();
  1816. clrValue = engine.Execute("showProps(jsObj, 'theObject')").GetCompletionValue().AsString();
  1817. Assert.Equal(jsValue, clrValue);
  1818. }
  1819. [Fact]
  1820. public void ShouldHideSpecificMembers()
  1821. {
  1822. var engine = new Engine(options => options.SetMemberAccessor((e, target, member) =>
  1823. {
  1824. if (target is HiddenMembers)
  1825. {
  1826. if (member == nameof(HiddenMembers.Member2) || member == nameof(HiddenMembers.Method2))
  1827. {
  1828. return JsValue.Undefined;
  1829. }
  1830. }
  1831. return null;
  1832. }));
  1833. engine.SetValue("m", new HiddenMembers());
  1834. Assert.Equal("Member1", engine.Execute("m.Member1").GetCompletionValue().ToString());
  1835. Assert.Equal("undefined", engine.Execute("m.Member2").GetCompletionValue().ToString());
  1836. Assert.Equal("Method1", engine.Execute("m.Method1()").GetCompletionValue().ToString());
  1837. // check the method itself, not its invokation as it would mean invoking "undefined"
  1838. Assert.Equal("undefined", engine.Execute("m.Method2").GetCompletionValue().ToString());
  1839. }
  1840. [Fact]
  1841. public void ShouldOverrideMembers()
  1842. {
  1843. var engine = new Engine(options => options.SetMemberAccessor((e, target, member) =>
  1844. {
  1845. if (target is HiddenMembers && member == nameof(HiddenMembers.Member1))
  1846. {
  1847. return "Orange";
  1848. }
  1849. return null;
  1850. }));
  1851. engine.SetValue("m", new HiddenMembers());
  1852. Assert.Equal("Orange", engine.Execute("m.Member1").GetCompletionValue().ToString());
  1853. }
  1854. }
  1855. }