expressions.rs 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679
  1. use parity_scale_codec::{Decode, Encode};
  2. use parity_scale_codec_derive::{Decode, Encode};
  3. use crate::{
  4. build_solidity, build_solidity_with_overflow_check, first_error, no_errors, parse_and_resolve,
  5. };
  6. use num_bigint::BigInt;
  7. use num_bigint::Sign;
  8. use rand::Rng;
  9. use solang::Target;
  10. #[test]
  11. fn celcius_and_fahrenheit() {
  12. #[derive(Debug, PartialEq, Encode, Decode)]
  13. struct Val(u32);
  14. // parse
  15. let mut runtime = build_solidity(
  16. "
  17. contract test {
  18. function celcius2fahrenheit(int32 celcius) pure public returns (int32) {
  19. int32 fahrenheit = celcius * 9 / 5 + 32;
  20. return fahrenheit;
  21. }
  22. function fahrenheit2celcius(uint32 fahrenheit) pure public returns (uint32) {
  23. return (fahrenheit - 32) * 5 / 9;
  24. }
  25. }",
  26. );
  27. runtime.function("celcius2fahrenheit", Val(10).encode());
  28. assert_eq!(runtime.vm.output, Val(50).encode());
  29. runtime.function("fahrenheit2celcius", Val(50).encode());
  30. assert_eq!(runtime.vm.output, Val(10).encode());
  31. }
  32. #[test]
  33. fn digits() {
  34. #[derive(Debug, PartialEq, Encode, Decode)]
  35. struct Val32(u32);
  36. #[derive(Debug, PartialEq, Encode, Decode)]
  37. struct Val64(u64);
  38. // parse
  39. let mut runtime = build_solidity(
  40. "
  41. contract test {
  42. function digitslen(uint64 val) pure public returns (uint32) {
  43. uint32 count = 0;
  44. while (val > 0) {
  45. count++;
  46. val /= 10;
  47. }
  48. if (count == 0) {
  49. count = 1;
  50. }
  51. return count;
  52. }
  53. function sumdigits(int64 val) pure public returns (uint32) {
  54. uint32 sum = 0;
  55. while (val > 0) {
  56. sum += uint32(val % 10);
  57. val= val / 10;
  58. }
  59. return sum;
  60. }
  61. }",
  62. );
  63. runtime.function("digitslen", Val64(1234567).encode());
  64. assert_eq!(runtime.vm.output, Val32(7).encode());
  65. runtime.function("sumdigits", Val64(123456789).encode());
  66. assert_eq!(runtime.vm.output, Val32(45).encode());
  67. }
  68. #[test]
  69. fn large_loops() {
  70. #[derive(Debug, PartialEq, Encode, Decode)]
  71. struct Val32(u32);
  72. #[derive(Debug, PartialEq, Encode, Decode)]
  73. struct Val64(u64);
  74. // parse
  75. let mut runtime = build_solidity(
  76. "
  77. contract test {
  78. function foo(uint val) pure public returns (uint) {
  79. for (uint i =0 ; i < 100; i++ ) {
  80. val += i + 10;
  81. }
  82. return val;
  83. }
  84. function baz(int val) pure public returns (int) {
  85. return val * 1000_000;
  86. }
  87. function bar() public {
  88. assert(foo(10) == 5960);
  89. assert(baz(7_000_123) == 7_000_123_000_000);
  90. assert(baz(7_000_123_456_678) == 7_000_123_456_678_000_000);
  91. }
  92. }",
  93. );
  94. runtime.function("bar", Vec::new());
  95. let mut args = Val64(7000).encode();
  96. args.resize(32, 0);
  97. runtime.function("baz", args);
  98. let mut rets = Val64(7000000000).encode();
  99. rets.resize(32, 0);
  100. assert_eq!(runtime.vm.output, rets);
  101. }
  102. #[test]
  103. fn expressions() {
  104. #[derive(Debug, PartialEq, Encode, Decode)]
  105. struct Val16(u16);
  106. #[derive(Debug, PartialEq, Encode, Decode)]
  107. struct Val8(u8);
  108. // parse
  109. let mut runtime = build_solidity("
  110. contract test {
  111. // this is 2^254
  112. int constant large_value = 14474011154664524427946373126085988481658748083205070504932198000989141204992;
  113. function add_100(uint16 a) pure public returns (uint16) {
  114. a -= 200;
  115. a += 300;
  116. return a;
  117. }
  118. function clear_digit(uint8 a) pure public returns (uint8) {
  119. a /= 10;
  120. a *= 10;
  121. return a;
  122. }
  123. function low_digit(uint8 a) pure public returns (uint8) {
  124. a %= 10;
  125. return a;
  126. }
  127. function test_comparisons() pure public {
  128. {
  129. // test comparisons work, if will work even if sign/unsigned is broken
  130. uint64 left = 102;
  131. uint64 right = 103;
  132. assert(left < right);
  133. assert(left <= right);
  134. assert(left != right);
  135. assert(right > left);
  136. assert(right >= left);
  137. assert(right == 103);
  138. assert(left >= 102);
  139. assert(right <= 103);
  140. assert(!(right <= 102));
  141. }
  142. {
  143. // check if unsigned compare works correctly (will fail if signed compare is done)
  144. uint16 left = 102;
  145. uint16 right = 0x8001;
  146. assert(left < right);
  147. assert(left <= right);
  148. assert(left != right);
  149. assert(right > left);
  150. assert(right >= left);
  151. assert(right == 0x8001);
  152. assert(left >= 102);
  153. assert(right <= 0x8001);
  154. assert(!(right <= 102));
  155. }
  156. {
  157. // check if signed compare works correctly (will fail if unsigned compare is done)
  158. int left = -102;
  159. int right = large_value;
  160. assert(left < right);
  161. assert(left <= right);
  162. assert(left != right);
  163. assert(right > left);
  164. assert(right >= left);
  165. assert(right == large_value);
  166. assert(left >= -102);
  167. assert(right <= large_value);
  168. assert(!(right <= -102));
  169. }
  170. }
  171. function increments() public {
  172. uint a = 1;
  173. assert(a-- == 1);
  174. assert(a == 0);
  175. assert(a++ == 0);
  176. assert(a == 1);
  177. assert(--a == 0);
  178. assert(a == 0);
  179. assert(++a == 1);
  180. assert(a == 1);
  181. }
  182. }",
  183. );
  184. runtime.function("add_100", Val16(0xffc0).encode());
  185. assert_eq!(runtime.vm.output, Val16(36).encode());
  186. runtime.function("clear_digit", Val8(25).encode());
  187. assert_eq!(runtime.vm.output, Val8(20).encode());
  188. runtime.function("low_digit", Val8(25).encode());
  189. assert_eq!(runtime.vm.output, Val8(5).encode());
  190. runtime.function("test_comparisons", Vec::new());
  191. runtime.function("increments", Vec::new());
  192. }
  193. #[test]
  194. fn test_cast_errors() {
  195. let ns = parse_and_resolve(
  196. "contract test {
  197. function foo(uint bar) public {
  198. bool is_nonzero = bar;
  199. }
  200. }",
  201. Target::Substrate {
  202. address_length: 32,
  203. value_length: 16,
  204. },
  205. );
  206. assert_eq!(
  207. first_error(ns.diagnostics),
  208. "conversion from uint256 to bool not possible"
  209. );
  210. let ns = parse_and_resolve(
  211. "contract test {
  212. function foobar(uint foo, int bar) public returns (bool) {
  213. return (foo < bar);
  214. }
  215. }",
  216. Target::Substrate {
  217. address_length: 32,
  218. value_length: 16,
  219. },
  220. );
  221. assert_eq!(
  222. first_error(ns.diagnostics),
  223. "implicit conversion would change sign from uint256 to int256"
  224. );
  225. let ns = parse_and_resolve(
  226. "contract test {
  227. function foobar(int32 foo, uint16 bar) public returns (bool) {
  228. foo = bar;
  229. return false;
  230. }
  231. }",
  232. Target::Substrate {
  233. address_length: 32,
  234. value_length: 16,
  235. },
  236. );
  237. no_errors(ns.diagnostics);
  238. // int16 can be negative, so cannot be stored in uint32
  239. let ns = parse_and_resolve(
  240. "contract test {
  241. function foobar(uint32 foo, int16 bar) public returns (bool) {
  242. foo = bar;
  243. return false;
  244. }
  245. }",
  246. Target::Substrate {
  247. address_length: 32,
  248. value_length: 16,
  249. },
  250. );
  251. assert_eq!(
  252. first_error(ns.diagnostics),
  253. "implicit conversion would change sign from int16 to uint32"
  254. );
  255. let ns = parse_and_resolve(
  256. "contract foo {
  257. uint bar;
  258. function set_bar(uint32 b) public {
  259. bar = b;
  260. }
  261. function get_bar() public returns (uint32) {
  262. return uint32(bar);
  263. }
  264. }
  265. contract foo2 {
  266. enum X { Y1, Y2, Y3}
  267. X y;
  268. function set_x(uint32 b) public {
  269. y = X(b);
  270. }
  271. function get_x() public returns (uint32) {
  272. return uint32(y);
  273. }
  274. function set_enum_x(X b) public {
  275. set_x(uint32(b));
  276. }
  277. }",
  278. Target::Substrate {
  279. address_length: 32,
  280. value_length: 16,
  281. },
  282. );
  283. no_errors(ns.diagnostics);
  284. }
  285. #[test]
  286. #[should_panic]
  287. fn divisions_by_zero() {
  288. // parse
  289. let mut runtime = build_solidity(
  290. "
  291. contract test {
  292. function do_test() public returns (uint){
  293. uint256 val = 100;
  294. return (val / 0);
  295. }
  296. }",
  297. );
  298. runtime.function("do_test", Vec::new());
  299. }
  300. #[test]
  301. fn divisions() {
  302. // parse
  303. let mut runtime = build_solidity(
  304. "
  305. contract test {
  306. uint constant large = 101213131318098987934191741;
  307. function do_test() public returns (uint) {
  308. assert(large / 1 == large);
  309. assert(large / (large + 102) == 0);
  310. assert(large / large == 1);
  311. assert(large % 1 == 0);
  312. assert(large % (large + 102) == large);
  313. assert(large % large == 0);
  314. return 0;
  315. }
  316. }",
  317. );
  318. runtime.function("do_test", Vec::new());
  319. }
  320. #[test]
  321. fn divisions64() {
  322. // parse
  323. let mut runtime = build_solidity(
  324. "
  325. contract test {
  326. uint64 constant large = 101213131318098987;
  327. function do_test() public returns (uint) {
  328. assert(large / 1 == large);
  329. assert(large / (large + 102) == 0);
  330. assert(large / large == 1);
  331. assert(large % 1 == 0);
  332. assert(large % (large + 102) == large);
  333. assert(large % large == 0);
  334. return 0;
  335. }
  336. }",
  337. );
  338. runtime.function("do_test", Vec::new());
  339. }
  340. #[test]
  341. fn divisions128() {
  342. #[derive(Debug, PartialEq, Encode, Decode)]
  343. struct Args(i128, i128);
  344. #[derive(Debug, PartialEq, Encode, Decode)]
  345. struct Rets(i128);
  346. // parse
  347. let mut runtime = build_solidity(
  348. "
  349. contract test {
  350. uint128 constant large = 101213131318098987;
  351. uint128 constant small = 99;
  352. int128 constant signed_large = 101213131318098987;
  353. int128 constant neg_signed_large = -101213131318098987;
  354. int128 constant signed_small = 99;
  355. function do_test() public returns (uint) {
  356. assert(large / 1 == large);
  357. assert(large / (large + 102) == 0);
  358. assert(large / large == 1);
  359. assert(large % 1 == 0);
  360. assert(large % (large + 102) == large);
  361. assert(large % large == 0);
  362. assert(small / 10 == 9);
  363. assert(small % 10 == 9);
  364. assert(large / 100000 == 1012131313180);
  365. assert(large % 100000 == 98987);
  366. return 0;
  367. }
  368. function do_signed_test() public returns (uint) {
  369. assert(signed_large / 1 == signed_large);
  370. assert(signed_large / (signed_large + 102) == 0);
  371. assert(signed_large / signed_large == 1);
  372. assert(signed_large % 1 == 0);
  373. assert(signed_large % (signed_large + 102) == signed_large);
  374. assert(signed_large % signed_large == 0);
  375. assert(signed_small / 10 == 9);
  376. assert(signed_small % 10 == 9);
  377. assert(signed_large / 100000 == 1012131313180);
  378. assert(signed_large % 100000 == 98987);
  379. assert(neg_signed_large / -100000 == 1012131313180);
  380. assert(signed_large / -100000 == -1012131313180);
  381. assert(-signed_large / 100000 == -1012131313180);
  382. assert(signed_large % -100000 == 98987);
  383. assert(-signed_large % 100000 == -98987);
  384. assert(-signed_large % -100000 == -98987);
  385. return 0;
  386. }
  387. function do_div(int128 x, int128 y) public returns (int128) {
  388. return x / y;
  389. }
  390. function return_neg() public returns (int128) {
  391. return -100;
  392. }
  393. function return_pos() public returns (int128) {
  394. return 255;
  395. }
  396. }",
  397. );
  398. runtime.function("do_test", Vec::new());
  399. runtime.function("return_neg", Vec::new());
  400. if let Ok(Rets(r)) = Rets::decode(&mut &runtime.vm.output[..]) {
  401. assert_eq!(r, -100);
  402. } else {
  403. panic!();
  404. }
  405. runtime.function("return_pos", Vec::new());
  406. if let Ok(Rets(r)) = Rets::decode(&mut &runtime.vm.output[..]) {
  407. assert_eq!(r, 255);
  408. } else {
  409. panic!();
  410. }
  411. runtime.function("do_div", Args(-9900, -100).encode());
  412. if let Ok(Rets(r)) = Rets::decode(&mut &runtime.vm.output[..]) {
  413. assert_eq!(r, 99);
  414. } else {
  415. panic!();
  416. }
  417. runtime.function("do_div", Args(-101213131318098987, -100000).encode());
  418. if let Ok(Rets(r)) = Rets::decode(&mut &runtime.vm.output[..]) {
  419. assert_eq!(r, 1012131313180);
  420. } else {
  421. panic!();
  422. }
  423. runtime.function("do_signed_test", Vec::new());
  424. }
  425. #[test]
  426. fn divisions256() {
  427. // parse
  428. let mut runtime = build_solidity(
  429. "
  430. contract test {
  431. uint256 constant large = 101213131318098987;
  432. uint256 constant small = 99;
  433. function do_test() public returns (uint) {
  434. assert(large / 1 == large);
  435. assert(large / (large + 102) == 0);
  436. assert(large / large == 1);
  437. assert(large % 1 == 0);
  438. assert(large % (large + 102) == large);
  439. assert(large % large == 0);
  440. assert(small / 10 == 9);
  441. assert(small % 10 == 9);
  442. assert(large / 100000 == 1012131313180);
  443. assert(large % 100000 == 98987);
  444. return 0;
  445. }
  446. }",
  447. );
  448. runtime.function("do_test", Vec::new());
  449. }
  450. #[test]
  451. fn complement() {
  452. // parse
  453. let mut runtime = build_solidity(
  454. "
  455. contract test {
  456. function do_test() public {
  457. uint8 x1 = 0;
  458. assert(~x1 == 255);
  459. int32 x2 = 0x7fefabcd;
  460. assert(uint32(~x2) == 0x80105432);
  461. }
  462. function do_complement(uint256 foo) public returns (uint) {
  463. return ~foo;
  464. }
  465. }",
  466. );
  467. runtime.function("do_test", Vec::new());
  468. let mut args = Vec::new();
  469. args.resize(32, 0);
  470. runtime.function("do_complement", args);
  471. let ret = runtime.vm.output;
  472. assert!(ret.len() == 32);
  473. assert!(ret.into_iter().filter(|x| *x == 255).count() == 32);
  474. }
  475. #[test]
  476. fn bitwise() {
  477. // parse
  478. let mut runtime = build_solidity(
  479. "
  480. contract test {
  481. function do_test() public {
  482. uint8 x1 = 0xf0;
  483. uint8 x2 = 0x0f;
  484. assert(x1 | x2 == 0xff);
  485. assert(x1 ^ x2 == 0xff);
  486. assert(x1 & x2 == 0x00);
  487. assert(x1 ^ 0 == x1);
  488. int32 x3 = 0x7fefabcd;
  489. assert(x3 & 0xffff == 0xabcd);
  490. }
  491. function do_or(uint256 a, uint256 b) public returns (uint) {
  492. return a | b;
  493. }
  494. function do_and(uint256 a, uint256 b) public returns (uint) {
  495. return a & b;
  496. }
  497. function do_xor(uint256 a, uint256 b) public returns (uint) {
  498. return a ^ b;
  499. }
  500. }",
  501. );
  502. runtime.function("do_test", Vec::new());
  503. let mut args = Vec::new();
  504. args.resize(32, 0);
  505. args.resize(64, 0xff);
  506. runtime.function("do_xor", args);
  507. let ret = &runtime.vm.output;
  508. assert!(ret.len() == 32);
  509. assert!(ret.iter().filter(|x| **x == 255).count() == 32);
  510. let mut args = Vec::new();
  511. args.resize(32, 0);
  512. args.resize(64, 0xff);
  513. runtime.function("do_or", args);
  514. let ret = &runtime.vm.output;
  515. assert!(ret.len() == 32);
  516. assert!(ret.iter().filter(|x| **x == 255).count() == 32);
  517. let mut args = Vec::new();
  518. args.resize(32, 0);
  519. args.resize(64, 0xff);
  520. runtime.function("do_and", args);
  521. let ret = &runtime.vm.output;
  522. assert!(ret.len() == 32);
  523. assert!(ret.iter().filter(|x| **x == 0).count() == 32);
  524. }
  525. #[test]
  526. fn shift() {
  527. // parse
  528. let mut runtime = build_solidity(
  529. "
  530. contract test {
  531. function do_test() public {
  532. uint8 x1 = 0xf0;
  533. uint8 x2 = 0x0f;
  534. assert(x1 >> 4 == 0x0f);
  535. assert(x2 << 4 == 0xf0);
  536. int x3 = -16;
  537. assert(x3 >> 2 == -4);
  538. uint x5 = 0xdead_0000_0000_0000_0000;
  539. assert(x5 >> 64 == 0xdead);
  540. x5 = 0xdead;
  541. assert(x5 << 64 == 0xdead_0000_0000_0000_0000);
  542. }
  543. }",
  544. );
  545. runtime.function("do_test", Vec::new());
  546. }
  547. #[test]
  548. fn assign_bitwise() {
  549. // parse
  550. let mut runtime = build_solidity(
  551. "
  552. contract test {
  553. function do_test() public {
  554. uint8 x1 = 0xf0;
  555. uint8 x2 = 0x0f;
  556. x1 |= x2;
  557. assert(x1 == 0xff);
  558. x1 = 0xf0; x2 = 0x0f;
  559. x1 ^= x2;
  560. assert(x1 == 0xff);
  561. x1 = 0xf0; x2 = 0x0f;
  562. x1 &= x2;
  563. assert(x1 == 0x00);
  564. x1 = 0xf0; x2 = 0x0f;
  565. x1 ^= 0;
  566. assert(x1 == x1);
  567. int32 x3 = 0x7fefabcd;
  568. x3 &= 0xffff;
  569. assert(x3 == 0xabcd);
  570. }
  571. function do_or(uint256 a, uint256 b) public returns (uint) {
  572. a |= b;
  573. return a;
  574. }
  575. function do_and(uint256 a, uint256 b) public returns (uint) {
  576. a &= b;
  577. return a;
  578. }
  579. function do_xor(uint256 a, uint256 b) public returns (uint) {
  580. a ^= b;
  581. return a;
  582. }
  583. }",
  584. );
  585. runtime.function("do_test", Vec::new());
  586. let mut args = Vec::new();
  587. args.resize(32, 0);
  588. args.resize(64, 0xff);
  589. runtime.function("do_xor", args);
  590. let ret = &runtime.vm.output;
  591. assert!(ret.len() == 32);
  592. assert!(ret.iter().filter(|x| **x == 255).count() == 32);
  593. let mut args = Vec::new();
  594. args.resize(32, 0);
  595. args.resize(64, 0xff);
  596. runtime.function("do_or", args);
  597. let ret = &runtime.vm.output;
  598. assert!(ret.len() == 32);
  599. assert!(ret.iter().filter(|x| **x == 255).count() == 32);
  600. let mut args = Vec::new();
  601. args.resize(32, 0);
  602. args.resize(64, 0xff);
  603. runtime.function("do_and", args);
  604. let ret = &runtime.vm.output;
  605. assert!(ret.len() == 32);
  606. assert!(ret.iter().filter(|x| **x == 0).count() == 32);
  607. }
  608. #[test]
  609. fn assign_shift() {
  610. // parse
  611. let mut runtime = build_solidity(
  612. "
  613. contract test {
  614. function do_test() public {
  615. uint8 x1 = 0xf0;
  616. uint8 x2 = 0x0f;
  617. x1 >>= 4;
  618. x2 <<= 4;
  619. assert(x1 == 0x0f);
  620. assert(x2 == 0xf0);
  621. int x3 = -16;
  622. x3 >>= 2;
  623. assert(x3 == -4);
  624. uint x5 = 0xdead_0000_0000_0000_0000;
  625. x5 >>= 64;
  626. assert(x5 == 0xdead);
  627. x5 = 0xdead;
  628. x5 <<= 64;
  629. assert(x5 == 0xdead_0000_0000_0000_0000);
  630. }
  631. }",
  632. );
  633. runtime.function("do_test", Vec::new());
  634. }
  635. #[test]
  636. fn ternary() {
  637. // parse
  638. let mut runtime = build_solidity(
  639. "
  640. contract test {
  641. function do_test() public {
  642. uint8 x1 = 0xf0;
  643. uint8 x2 = 0x0f;
  644. assert((false ? x1 : x2) == x2);
  645. assert((true ? x1 : x2) == x1);
  646. }
  647. }",
  648. );
  649. runtime.function("do_test", Vec::new());
  650. }
  651. #[test]
  652. fn short_circuit_or() {
  653. // parse
  654. let mut runtime = build_solidity(
  655. "
  656. contract test {
  657. uint32 counter;
  658. function increase_counter() private returns (bool) {
  659. counter += 1;
  660. return true;
  661. }
  662. function increase_counter2() private returns (bool) {
  663. counter++;
  664. return true;
  665. }
  666. function do_test() public {
  667. assert(counter == 0);
  668. // if left of or is true, right is not evaluated
  669. assert(true || increase_counter());
  670. assert(counter == 0);
  671. assert(false || increase_counter2());
  672. assert(counter == 1);
  673. false && increase_counter();
  674. assert(counter == 1);
  675. true && increase_counter();
  676. assert(counter == 2);
  677. }
  678. }",
  679. );
  680. runtime.function("do_test", Vec::new());
  681. }
  682. #[test]
  683. fn short_circuit_and() {
  684. // parse
  685. let mut runtime = build_solidity(
  686. "
  687. contract test {
  688. uint32 counter;
  689. function increase_counter() private returns (bool) {
  690. counter |= 1;
  691. return false;
  692. }
  693. function increase_counter2() private returns (bool) {
  694. ++counter;
  695. return false;
  696. }
  697. function do_test() public {
  698. assert(counter == 0);
  699. increase_counter2();
  700. increase_counter2();
  701. assert(counter == 2);
  702. increase_counter();
  703. assert(counter == 3);
  704. counter = 0;
  705. // if left hand side is false, right hand side is not evaluated
  706. assert(!(false && increase_counter()));
  707. assert(counter == 0);
  708. assert(!(true && increase_counter2()));
  709. assert(counter == 1);
  710. false && increase_counter2();
  711. assert(counter == 1);
  712. counter = 0;
  713. true && increase_counter();
  714. assert(counter == 1);
  715. }
  716. }",
  717. );
  718. runtime.function("do_test", Vec::new());
  719. }
  720. #[test]
  721. fn power() {
  722. #[derive(Debug, PartialEq, Encode, Decode)]
  723. struct Val(u64);
  724. // parse
  725. let mut runtime = build_solidity(
  726. "
  727. contract c {
  728. function power(uint64 base, uint64 exp) public returns (uint64) {
  729. return base ** exp;
  730. }
  731. function power_with_cast() public returns (uint64) {
  732. return uint64(2 ** 32);
  733. }
  734. }",
  735. );
  736. // 4**5 = 1024
  737. let args = Val(4)
  738. .encode()
  739. .into_iter()
  740. .chain(Val(5).encode().into_iter())
  741. .collect();
  742. runtime.function("power", args);
  743. assert_eq!(runtime.vm.output, Val(1024).encode());
  744. // n ** 1 = n
  745. let args = Val(2345)
  746. .encode()
  747. .into_iter()
  748. .chain(Val(1).encode().into_iter())
  749. .collect();
  750. runtime.function("power", args);
  751. assert_eq!(runtime.vm.output, Val(2345).encode());
  752. // n ** 0 = 0
  753. let args = Val(0xdead_beef)
  754. .encode()
  755. .into_iter()
  756. .chain(Val(0).encode().into_iter())
  757. .collect();
  758. runtime.function("power", args);
  759. assert_eq!(runtime.vm.output, Val(1).encode());
  760. // 0 ** n = 0
  761. let args = Val(0)
  762. .encode()
  763. .into_iter()
  764. .chain(Val(0xdead_beef).encode().into_iter())
  765. .collect();
  766. runtime.function("power", args);
  767. assert_eq!(runtime.vm.output, Val(0).encode());
  768. runtime.function("power_with_cast", Vec::new());
  769. assert_eq!(runtime.vm.output, Val(0x1_0000_0000).encode());
  770. let ns = parse_and_resolve(
  771. "contract test {
  772. function power(uint64 base, int64 exp) public returns (uint64) {
  773. return base ** exp;
  774. }
  775. }",
  776. Target::Substrate {
  777. address_length: 32,
  778. value_length: 16,
  779. },
  780. );
  781. assert_eq!(
  782. first_error(ns.diagnostics),
  783. "exponation (**) is not allowed with signed types"
  784. );
  785. let ns = parse_and_resolve(
  786. "contract test {
  787. function power(int64 base, uint64 exp) public returns (int64) {
  788. return base ** exp;
  789. }
  790. }",
  791. Target::Substrate {
  792. address_length: 32,
  793. value_length: 16,
  794. },
  795. );
  796. assert_eq!(
  797. first_error(ns.diagnostics),
  798. "exponation (**) is not allowed with signed types"
  799. );
  800. let ns = parse_and_resolve(
  801. "contract test {
  802. function power(int64 base, int64 exp) public returns (int64) {
  803. return base ** exp;
  804. }
  805. }",
  806. Target::Substrate {
  807. address_length: 32,
  808. value_length: 16,
  809. },
  810. );
  811. assert_eq!(
  812. first_error(ns.diagnostics),
  813. "exponation (**) is not allowed with signed types"
  814. );
  815. }
  816. #[test]
  817. fn large_power() {
  818. #[derive(Debug, PartialEq, Encode, Decode)]
  819. struct Val(u128);
  820. // parse
  821. let mut runtime = build_solidity(
  822. "
  823. contract c {
  824. function power(uint128 base, uint128 exp) public returns (uint128) {
  825. return base ** exp;
  826. }
  827. }",
  828. );
  829. // 4**5 = 1024
  830. let args = Val(4)
  831. .encode()
  832. .into_iter()
  833. .chain(Val(5).encode().into_iter())
  834. .collect();
  835. runtime.function("power", args);
  836. assert_eq!(runtime.vm.output, Val(1024).encode());
  837. // n ** 1 = n
  838. let args = Val(2345)
  839. .encode()
  840. .into_iter()
  841. .chain(Val(1).encode().into_iter())
  842. .collect();
  843. runtime.function("power", args);
  844. assert_eq!(runtime.vm.output, Val(2345).encode());
  845. // n ** 0 = 0
  846. let args = Val(0xdeadbeef)
  847. .encode()
  848. .into_iter()
  849. .chain(Val(0).encode().into_iter())
  850. .collect();
  851. runtime.function("power", args);
  852. assert_eq!(runtime.vm.output, Val(1).encode());
  853. // 0 ** n = 0
  854. let args = Val(0)
  855. .encode()
  856. .into_iter()
  857. .chain(Val(0xdeadbeef).encode().into_iter())
  858. .collect();
  859. runtime.function("power", args);
  860. assert_eq!(runtime.vm.output, Val(0).encode());
  861. // 10 ** 36 = 1000000000000000000000000000000000000
  862. let args = Val(10)
  863. .encode()
  864. .into_iter()
  865. .chain(Val(36).encode().into_iter())
  866. .collect();
  867. runtime.function("power", args);
  868. assert_eq!(
  869. runtime.vm.output,
  870. Val(1000000000000000000000000000000000000).encode()
  871. );
  872. }
  873. #[test]
  874. fn multiply() {
  875. let mut rng = rand::thread_rng();
  876. let size = 32;
  877. let mut runtime = build_solidity(
  878. "
  879. contract c {
  880. function multiply(uint a, uint b) public returns (uint) {
  881. return a * b;
  882. }
  883. function multiply_with_cast() public returns (uint64) {
  884. return uint64(255 * 255);
  885. }
  886. }",
  887. );
  888. runtime.function("multiply_with_cast", Vec::new());
  889. assert_eq!(runtime.vm.output, 65025u64.encode());
  890. let mut rand = || -> (BigInt, Vec<u8>) {
  891. let length = rng.gen::<usize>() % size;
  892. let mut data = Vec::new();
  893. data.resize_with(length + 1, || rng.gen());
  894. data.resize(size, 0);
  895. (BigInt::from_bytes_le(Sign::Plus, &data), data)
  896. };
  897. for _ in 0..1000 {
  898. let (a, a_data) = rand();
  899. let (b, b_data) = rand();
  900. println!("in: a:{:?} b:{:?}", a_data, b_data);
  901. runtime.function(
  902. "multiply",
  903. a_data.into_iter().chain(b_data.into_iter()).collect(),
  904. );
  905. println!("out: res:{:?}", runtime.vm.output);
  906. let res = BigInt::from_bytes_le(Sign::Plus, &runtime.vm.output);
  907. println!("{} = {} * {}", res, a, b);
  908. // the result is truncated to $size bytes. We do this here by converting to Vec<u8> and truncating
  909. // it. A truncating bigint multiply would be nicer.
  910. let (_, mut res) = (a * b).to_bytes_le();
  911. res.resize(size, 0);
  912. assert_eq!(res, runtime.vm.output);
  913. }
  914. }
  915. #[test]
  916. fn bytes_bitwise() {
  917. #[derive(Debug, PartialEq, Encode, Decode)]
  918. struct Bytes3([u8; 3]);
  919. #[derive(Debug, PartialEq, Encode, Decode)]
  920. struct Bytes5([u8; 5]);
  921. #[derive(Debug, PartialEq, Encode, Decode)]
  922. struct BytesArray([u8; 7], u32);
  923. // parse
  924. let mut runtime = build_solidity(
  925. "
  926. contract c {
  927. function or(bytes5 x) public returns (bytes5 y) {
  928. y = x | hex\"80808080\";
  929. }
  930. function and(bytes5 x) public returns (bytes5) {
  931. return x & hex\"FFFF\";
  932. }
  933. function xor(bytes5 x) public returns (bytes5) {
  934. x ^= 0xFF00000000;
  935. return x;
  936. }
  937. function shift_left(bytes3 a) public returns (bytes5 b) {
  938. b = bytes5(a) << 8;
  939. }
  940. function shift_right(bytes3 a) public returns (bytes5 b) {
  941. b = bytes5(a) >> 8;
  942. }
  943. function shift_left2(bytes3 a) public returns (bytes5 b) {
  944. b = bytes5(a);
  945. b <<= 8;
  946. }
  947. function shift_right2(bytes3 a) public returns (bytes5 b) {
  948. b = bytes5(a);
  949. b >>= 8;
  950. }
  951. function bytes_length() public {
  952. bytes4 b4;
  953. assert(b4.length == 4);
  954. }
  955. function complement(bytes3 a) public returns (bytes3) {
  956. return ~a;
  957. }
  958. function bytes_array(bytes7 foo, uint32 index) public returns (bytes1) {
  959. return foo[index];
  960. }
  961. }",
  962. );
  963. runtime.function("or", Bytes5([0x01, 0x01, 0x01, 0x01, 0x01]).encode());
  964. assert_eq!(
  965. runtime.vm.output,
  966. Bytes5([0x81, 0x81, 0x81, 0x81, 0x01]).encode()
  967. );
  968. runtime.function("and", Bytes5([0x01, 0x01, 0x01, 0x01, 0x01]).encode());
  969. assert_eq!(runtime.vm.output, Bytes5([0x01, 0x01, 0, 0, 0]).encode());
  970. runtime.function("xor", Bytes5([0x01, 0x01, 0x01, 0x01, 0x01]).encode());
  971. assert_eq!(
  972. runtime.vm.output,
  973. Bytes5([0xfe, 0x01, 0x01, 0x01, 0x01]).encode()
  974. );
  975. // shifty-shift
  976. runtime.function("shift_left", Bytes3([0xf3, 0x7d, 0x03]).encode());
  977. assert_eq!(
  978. runtime.vm.output,
  979. Bytes5([0x7d, 0x03, 0x00, 0x00, 0x00]).encode()
  980. );
  981. runtime.function("shift_right", Bytes3([0xf3, 0x7d, 0x03]).encode());
  982. assert_eq!(
  983. runtime.vm.output,
  984. Bytes5([0x00, 0xf3, 0x7d, 0x03, 0x00]).encode()
  985. );
  986. // assignment versions
  987. runtime.function("shift_left2", Bytes3([0xf3, 0x7d, 0x03]).encode());
  988. assert_eq!(
  989. runtime.vm.output,
  990. Bytes5([0x7d, 0x03, 0x00, 0x00, 0x00]).encode()
  991. );
  992. runtime.function("shift_right2", Bytes3([0xf3, 0x7d, 0x03]).encode());
  993. assert_eq!(
  994. runtime.vm.output,
  995. Bytes5([0x00, 0xf3, 0x7d, 0x03, 0x00]).encode()
  996. );
  997. // check length
  998. runtime.function("bytes_length", Vec::new());
  999. // complement
  1000. runtime.function("complement", Bytes3([0xf3, 0x7d, 0x03]).encode());
  1001. assert_eq!(runtime.vm.output, Bytes3([0x0c, 0x82, 0xfc]).encode());
  1002. // array access
  1003. let bytes7 = *b"NAWABRA";
  1004. for i in 0..6 {
  1005. runtime.function("bytes_array", BytesArray(bytes7, i).encode());
  1006. assert_eq!(runtime.vm.output, [bytes7[i as usize]]);
  1007. }
  1008. }
  1009. #[test]
  1010. #[should_panic]
  1011. fn bytesn_underflow_index_acccess() {
  1012. #[derive(Debug, PartialEq, Encode, Decode)]
  1013. struct BytesArray([u8; 7], i32);
  1014. // parse
  1015. let mut runtime = build_solidity(
  1016. "
  1017. contract test {
  1018. function bytes_array(bytes7 foo, int32 index) public returns (bytes1) {
  1019. return foo[index];
  1020. }
  1021. }",
  1022. );
  1023. runtime.function("bytes_array", BytesArray(*b"nawabra", -1).encode());
  1024. }
  1025. #[test]
  1026. #[should_panic]
  1027. fn bytesn_overflow_index_acccess() {
  1028. #[derive(Debug, PartialEq, Encode, Decode)]
  1029. struct BytesArray([u8; 7], i32);
  1030. // parse
  1031. let mut runtime = build_solidity(
  1032. "
  1033. contract test {
  1034. function bytes_array(bytes7 foo, int32 index) public returns (byte) {
  1035. return foo[index];
  1036. }
  1037. }",
  1038. );
  1039. runtime.function("bytes_array", BytesArray(*b"nawabra", 7).encode());
  1040. }
  1041. #[test]
  1042. fn unaryminus_and_subtract() {
  1043. // The minus sign can be a unary negative or subtract.
  1044. let mut runtime = build_solidity(
  1045. r#"
  1046. contract c {
  1047. function test() public {
  1048. uint32 x = 10-10;
  1049. assert(x == 0);
  1050. int32 y = -10-10;
  1051. assert(y == -20);
  1052. }
  1053. }"#,
  1054. );
  1055. runtime.function("test", Vec::new());
  1056. }
  1057. #[test]
  1058. fn div() {
  1059. // The minus sign can be a unary negative or subtract.
  1060. let mut runtime = build_solidity(
  1061. r#"
  1062. contract c {
  1063. function test1() public {
  1064. // see https://solidity.readthedocs.io/en/latest/types.html#modulo
  1065. assert(int256(5) % int256(2) == int256(1));
  1066. assert(int256(5) % int256(-2) == int256(1));
  1067. assert(int256(-5) % int256(2) == int256(-1));
  1068. assert(int256(-5) % int256(-2) == int256(-1));
  1069. assert(int64(5) % int64(2) == int64(1));
  1070. assert(int64(5) % int64(-2) == int64(1));
  1071. assert(int64(-5) % int64(2) == int64(-1));
  1072. assert(int64(-5) % int64(-2) == int64(-1));
  1073. }
  1074. function test2() public {
  1075. // see https://github.com/hyperledger/burrow/pull/1367#issue-399914366
  1076. assert(int256(7) / int256(3) == int256(2));
  1077. assert(int256(7) / int256(-3) == int256(-2));
  1078. assert(int256(-7) / int256(3) == int256(-2));
  1079. assert(int256(-7) / int256(-3) == int256(2));
  1080. assert(int256(7) % int256(3) == int256(1));
  1081. assert(int256(7) % int256(-3) == int256(1));
  1082. assert(int256(-7) % int256(3) == int256(-1));
  1083. assert(int256(-7) % int256(-3) == int256(-1));
  1084. assert(int64(7) / int64(3) == int64(2));
  1085. assert(int64(7) / int64(-3) == int64(-2));
  1086. assert(int64(-7) / int64(3) == int64(-2));
  1087. assert(int64(-7) / int64(-3) == int64(2));
  1088. assert(int64(7) % int64(3) == int64(1));
  1089. assert(int64(7) % int64(-3) == int64(1));
  1090. assert(int64(-7) % int64(3) == int64(-1));
  1091. assert(int64(-7) % int64(-3) == int64(-1));
  1092. }
  1093. }"#,
  1094. );
  1095. runtime.function("test1", Vec::new());
  1096. runtime.function("test2", Vec::new());
  1097. }
  1098. #[test]
  1099. fn destructure() {
  1100. let ns = parse_and_resolve(
  1101. "contract test {
  1102. function foo(uint bar) public {
  1103. int a;
  1104. int b;
  1105. (a, b) = (1, 2, 3);
  1106. }
  1107. }",
  1108. Target::Substrate {
  1109. address_length: 32,
  1110. value_length: 16,
  1111. },
  1112. );
  1113. assert_eq!(
  1114. first_error(ns.diagnostics),
  1115. "destructuring assignment has 2 elements on the left and 3 on the right"
  1116. );
  1117. let ns = parse_and_resolve(
  1118. "contract test {
  1119. function foo(uint bar) public {
  1120. int a;
  1121. int b;
  1122. (c, b) = (1, 2);
  1123. }
  1124. }",
  1125. Target::Substrate {
  1126. address_length: 32,
  1127. value_length: 16,
  1128. },
  1129. );
  1130. assert_eq!(first_error(ns.diagnostics), "`c\' is not found");
  1131. let ns = parse_and_resolve(
  1132. "contract test {
  1133. function foo(uint bar) public {
  1134. int a;
  1135. int b;
  1136. (a memory, b) = (1, 2);
  1137. }
  1138. }",
  1139. Target::Substrate {
  1140. address_length: 32,
  1141. value_length: 16,
  1142. },
  1143. );
  1144. assert_eq!(
  1145. first_error(ns.diagnostics),
  1146. "storage modifier ‘memory’ not permitted on assignment"
  1147. );
  1148. let ns = parse_and_resolve(
  1149. "contract test {
  1150. function foo(uint bar) public {
  1151. int a;
  1152. int b;
  1153. (a , b) = (1, );
  1154. }
  1155. }",
  1156. Target::Substrate {
  1157. address_length: 32,
  1158. value_length: 16,
  1159. },
  1160. );
  1161. assert_eq!(first_error(ns.diagnostics), "stray comma");
  1162. // The minus sign can be a unary negative or subtract.
  1163. let mut runtime = build_solidity(
  1164. r#"
  1165. contract c {
  1166. function test() public {
  1167. int a;
  1168. int b;
  1169. // test one
  1170. (a, b) = (102, 3);
  1171. assert(b == 3 && a == 102);
  1172. // test missing one
  1173. (a, , b) = (1, 2, 3);
  1174. assert(a == 1 && b == 3);
  1175. // test single one
  1176. (a) = 5;
  1177. assert(a == 5);
  1178. // or like so
  1179. (a) = (105);
  1180. assert(a == 105);
  1181. }
  1182. function swap() public {
  1183. int32 a;
  1184. int32 b;
  1185. // test one
  1186. (a, b) = (102, 3);
  1187. // test swap
  1188. (b, a) = (a, b);
  1189. assert(a == 3 && b == 102);
  1190. }
  1191. }"#,
  1192. );
  1193. runtime.function("test", Vec::new());
  1194. runtime.function("swap", Vec::new());
  1195. // The minus sign can be a unary negative or subtract.
  1196. let mut runtime = build_solidity(
  1197. r#"
  1198. contract c {
  1199. function test() public {
  1200. // test one
  1201. (int32 a, int32 b) = (102, 3);
  1202. assert(b == 3 && a == 102);
  1203. // test missing one
  1204. (a, , b) = (1, 2, 3);
  1205. assert(a == 1 && b == 3);
  1206. // test single one
  1207. (a) = 5;
  1208. assert(a == 5);
  1209. // or like so
  1210. (a) = (105);
  1211. assert(a == 105);
  1212. }
  1213. }"#,
  1214. );
  1215. runtime.function("test", Vec::new());
  1216. }
  1217. #[test]
  1218. #[should_panic]
  1219. fn addition_overflow() {
  1220. let mut runtime = build_solidity_with_overflow_check(
  1221. r#"
  1222. contract overflow {
  1223. function foo(uint8 x) internal returns (uint8) {
  1224. uint8 y = x + 1;
  1225. return y;
  1226. }
  1227. function bar() public {
  1228. foo(255);
  1229. }
  1230. }
  1231. "#,
  1232. );
  1233. runtime.function("bar", Vec::new());
  1234. }
  1235. #[test]
  1236. fn unchecked_addition_overflow() {
  1237. let mut runtime = build_solidity_with_overflow_check(
  1238. r#"
  1239. contract overflow {
  1240. function foo(uint8 x) internal returns (uint8) {
  1241. unchecked {
  1242. uint8 y = x + 1;
  1243. return y;
  1244. }
  1245. }
  1246. function bar() public {
  1247. foo(255);
  1248. }
  1249. }
  1250. "#,
  1251. );
  1252. runtime.function("bar", Vec::new());
  1253. }
  1254. #[test]
  1255. #[should_panic]
  1256. fn subtraction_underflow() {
  1257. let mut runtime = build_solidity_with_overflow_check(
  1258. r#"
  1259. contract underflow {
  1260. function foo(uint64 x) internal returns (uint64) {
  1261. uint64 y = x - 1;
  1262. return y;
  1263. }
  1264. function bar() public {
  1265. foo(0);
  1266. }
  1267. }
  1268. "#,
  1269. );
  1270. runtime.function("bar", Vec::new());
  1271. }
  1272. #[test]
  1273. fn unchecked_subtraction_underflow() {
  1274. let mut runtime = build_solidity_with_overflow_check(
  1275. r#"
  1276. contract underflow {
  1277. function foo(uint64 x) internal returns (uint64) {
  1278. unchecked {
  1279. uint64 y = x - 1;
  1280. return y;
  1281. }
  1282. }
  1283. function bar() public {
  1284. foo(0);
  1285. }
  1286. }
  1287. "#,
  1288. );
  1289. runtime.function("bar", Vec::new());
  1290. }
  1291. #[test]
  1292. #[should_panic]
  1293. fn multiplication_overflow() {
  1294. let mut runtime = build_solidity_with_overflow_check(
  1295. r#"
  1296. contract overflow {
  1297. function foo(int8 x) internal returns (int8) {
  1298. int8 y = x * int8(64);
  1299. return y;
  1300. }
  1301. function bar() public {
  1302. foo(8);
  1303. }
  1304. }
  1305. "#,
  1306. );
  1307. runtime.function("bar", Vec::new());
  1308. }
  1309. #[test]
  1310. fn unchecked_multiplication_overflow() {
  1311. let mut runtime = build_solidity_with_overflow_check(
  1312. r#"
  1313. contract overflow {
  1314. function foo(int8 x) internal returns (int8) {
  1315. unchecked {
  1316. int8 y = x * int8(64);
  1317. return y;
  1318. }
  1319. }
  1320. function bar() public {
  1321. foo(8);
  1322. }
  1323. }
  1324. "#,
  1325. );
  1326. runtime.function("bar", Vec::new());
  1327. }