Fork of daniellemaywood.uk/gleam — Wasm codegen work
2

Configure Feed

Select the types of activity you want to include in your feed.

improve int division and modulo on js

+184 -35
+5
CHANGELOG.md
··· 226 226 227 227 ([Surya Rose](https://github.com/GearsDatapacks)) 228 228 229 + <<<<<<< HEAD 229 230 - On the Erlang target each generated module enables inlining from the Erlang 230 231 compiler. 231 232 ([Giacomo Cavalieri](https://github.com/giacomocavalieri)) 232 233 233 234 - The code generated for floating point division on the JavaScript target has 234 235 been improved to avoid performing needless checks. 236 + ======= 237 + - The code generated for division and modulo operators on the JavaScript target 238 + has been improved to avoid performing needless checks. 239 + >>>>>>> 15e8a1280 (improve int division and modulo on js) 235 240 ([Giacomo Cavalieri](https://github.com/giacomocavalieri)) 236 241 237 242 ### Build tool
+30 -6
compiler-core/src/javascript/expression.rs
··· 1437 1437 } 1438 1438 1439 1439 fn div_int(&mut self, left: &'a TypedExpr, right: &'a TypedExpr) -> Document<'a> { 1440 - let left = 1440 + let left_doc = 1441 1441 self.not_in_tail_position(Some(Ordering::Strict), |this| this.child_expression(left)); 1442 - let right = 1442 + let right_doc = 1443 1443 self.not_in_tail_position(Some(Ordering::Strict), |this| this.child_expression(right)); 1444 + <<<<<<< HEAD 1444 1445 self.tracker.int_division_used = true; 1445 1446 docvec!["divideInt", wrap_arguments([left, right])] 1447 + ======= 1448 + 1449 + if right.non_zero_compile_time_number() { 1450 + let division = if let TypedExpr::BinOp { .. } = left { 1451 + docvec![left_doc.surround("(", ")"), " / ", right_doc] 1452 + } else { 1453 + docvec![left_doc, " / ", right_doc] 1454 + }; 1455 + docvec!["Math.trunc", wrap_args([division])] 1456 + } else { 1457 + self.tracker.int_division_used = true; 1458 + docvec!["divideInt", wrap_args([left_doc, right_doc])] 1459 + } 1460 + >>>>>>> 15e8a1280 (improve int division and modulo on js) 1446 1461 } 1447 1462 1448 1463 fn remainder_int(&mut self, left: &'a TypedExpr, right: &'a TypedExpr) -> Document<'a> { 1449 - let left = 1464 + let left_doc = 1450 1465 self.not_in_tail_position(Some(Ordering::Strict), |this| this.child_expression(left)); 1451 - let right = 1466 + let right_doc = 1452 1467 self.not_in_tail_position(Some(Ordering::Strict), |this| this.child_expression(right)); 1453 - self.tracker.int_remainder_used = true; 1454 - docvec!["remainderInt", wrap_arguments([left, right])] 1468 + 1469 + if right.non_zero_compile_time_number() { 1470 + if let TypedExpr::BinOp { .. } = left { 1471 + docvec![left_doc.surround("(", ")"), " % ", right_doc] 1472 + } else { 1473 + docvec![left_doc, " % ", right_doc] 1474 + } 1475 + } else { 1476 + self.tracker.int_remainder_used = true; 1477 + docvec!["remainderInt", wrap_arguments([left_doc, right_doc])] 1478 + } 1455 1479 } 1456 1480 1457 1481 fn div_float(&mut self, left: &'a TypedExpr, right: &'a TypedExpr) -> Document<'a> {
+54
compiler-core/src/javascript/tests/numbers.rs
··· 457 457 }" 458 458 ) 459 459 } 460 + 461 + #[test] 462 + fn division_by_zero_int() { 463 + assert_js!( 464 + "pub fn main() { 465 + 1 / 0 466 + }" 467 + ) 468 + } 469 + 470 + #[test] 471 + fn division_by_non_zero_int() { 472 + assert_js!( 473 + "pub fn main() { 474 + 1 / 2 475 + }" 476 + ) 477 + } 478 + 479 + #[test] 480 + fn complex_division_by_non_zero_int() { 481 + assert_js!( 482 + "pub fn main() { 483 + { 1 + 2 } / 3 484 + }" 485 + ) 486 + } 487 + 488 + #[test] 489 + fn remainder_by_zero_int() { 490 + assert_js!( 491 + "pub fn main() { 492 + 1 % 0 493 + }" 494 + ) 495 + } 496 + 497 + #[test] 498 + fn remainder_by_non_zero_int() { 499 + assert_js!( 500 + "pub fn main() { 501 + 1 % 2 502 + }" 503 + ) 504 + } 505 + 506 + #[test] 507 + fn complex_remainder_by_non_zero_int() { 508 + assert_js!( 509 + "pub fn main() { 510 + { 1 + 2 } % 3 511 + }" 512 + ) 513 + }
+1 -1
compiler-core/src/javascript/tests/snapshots/gleam_core__javascript__tests__case_clause_guards__int_division.snap
··· 16 16 import { divideInt } from "../gleam.mjs"; 17 17 18 18 export function main() { 19 - let $ = divideInt(5, 2); 19 + let $ = Math.trunc(5 / 2); 20 20 let x = $; 21 21 if (x === (divideInt(5, 2))) { 22 22 return true;
+13
compiler-core/src/javascript/tests/snapshots/gleam_core__javascript__tests__numbers__complex_division_by_non_zero_int.snap
··· 1 + --- 2 + source: compiler-core/src/javascript/tests/numbers.rs 3 + expression: "pub fn main() {\n { 1 + 2 } / 3\n}" 4 + --- 5 + ----- SOURCE CODE 6 + pub fn main() { 7 + { 1 + 2 } / 3 8 + } 9 + 10 + ----- COMPILED JAVASCRIPT 11 + export function main() { 12 + return Math.trunc((1 + 2) / 3); 13 + }
+13
compiler-core/src/javascript/tests/snapshots/gleam_core__javascript__tests__numbers__complex_remainder_by_non_zero_int.snap
··· 1 + --- 2 + source: compiler-core/src/javascript/tests/numbers.rs 3 + expression: "pub fn main() {\n { 1 + 2 } % 3\n}" 4 + --- 5 + ----- SOURCE CODE 6 + pub fn main() { 7 + { 1 + 2 } % 3 8 + } 9 + 10 + ----- COMPILED JAVASCRIPT 11 + export function main() { 12 + return (1 + 2) % 3; 13 + }
+13
compiler-core/src/javascript/tests/snapshots/gleam_core__javascript__tests__numbers__division_by_non_zero_int.snap
··· 1 + --- 2 + source: compiler-core/src/javascript/tests/numbers.rs 3 + expression: "pub fn main() {\n 1 / 2\n}" 4 + --- 5 + ----- SOURCE CODE 6 + pub fn main() { 7 + 1 / 2 8 + } 9 + 10 + ----- COMPILED JAVASCRIPT 11 + export function main() { 12 + return Math.trunc(1 / 2); 13 + }
+15
compiler-core/src/javascript/tests/snapshots/gleam_core__javascript__tests__numbers__division_by_zero_int.snap
··· 1 + --- 2 + source: compiler-core/src/javascript/tests/numbers.rs 3 + expression: "pub fn main() {\n 1 / 0\n}" 4 + --- 5 + ----- SOURCE CODE 6 + pub fn main() { 7 + 1 / 0 8 + } 9 + 10 + ----- COMPILED JAVASCRIPT 11 + import { divideInt } from "../gleam.mjs"; 12 + 13 + export function main() { 14 + return divideInt(1, 0); 15 + }
+2 -7
compiler-core/src/javascript/tests/snapshots/gleam_core__javascript__tests__numbers__int_divide_complex_expr.snap
··· 1 1 --- 2 2 source: compiler-core/src/javascript/tests/numbers.rs 3 - assertion_line: 74 4 3 expression: "\npub fn go() {\n case 1 >= 0 {\n True -> 2\n False -> 4\n } / 2\n}\n" 5 - snapshot_kind: text 6 4 --- 7 5 ----- SOURCE CODE 8 6 ··· 15 13 16 14 17 15 ----- COMPILED JAVASCRIPT 18 - import { divideInt } from "../gleam.mjs"; 19 - 20 16 export function go() { 21 - return divideInt( 17 + return Math.trunc( 22 18 (() => { 23 19 let $ = 1 >= 0; 24 20 if ($) { ··· 26 22 } else { 27 23 return 4; 28 24 } 29 - })(), 30 - 2 25 + })() / 2 31 26 ); 32 27 }
+8 -15
compiler-core/src/javascript/tests/snapshots/gleam_core__javascript__tests__numbers__int_mod_complex_expr.snap
··· 1 1 --- 2 2 source: compiler-core/src/javascript/tests/numbers.rs 3 - assertion_line: 88 4 3 expression: "\npub fn go() {\n case 1 >= 0 {\n True -> 2\n False -> 4\n } % 2\n}\n" 5 - snapshot_kind: text 6 4 --- 7 5 ----- SOURCE CODE 8 6 ··· 15 13 16 14 17 15 ----- COMPILED JAVASCRIPT 18 - import { remainderInt } from "../gleam.mjs"; 19 - 20 16 export function go() { 21 - return remainderInt( 22 - (() => { 23 - let $ = 1 >= 0; 24 - if ($) { 25 - return 2; 26 - } else { 27 - return 4; 28 - } 29 - })(), 30 - 2 31 - ); 17 + return (() => { 18 + let $ = 1 >= 0; 19 + if ($) { 20 + return 2; 21 + } else { 22 + return 4; 23 + } 24 + })() % 2; 32 25 }
+2 -6
compiler-core/src/javascript/tests/snapshots/gleam_core__javascript__tests__numbers__int_operators.snap
··· 1 1 --- 2 2 source: compiler-core/src/javascript/tests/numbers.rs 3 - assertion_line: 55 4 3 expression: "\npub fn go() {\n 1 + 1 // => 2\n 5 - 1 // => 4\n 5 / 2 // => 2\n 3 * 3 // => 9\n 5 % 2 // => 1\n 2 > 1 // => True\n 2 < 1 // => False\n 2 >= 1 // => True\n 2 <= 1 // => False\n}\n" 5 - snapshot_kind: text 6 4 --- 7 5 ----- SOURCE CODE 8 6 ··· 20 18 21 19 22 20 ----- COMPILED JAVASCRIPT 23 - import { remainderInt, divideInt } from "../gleam.mjs"; 24 - 25 21 export function go() { 26 22 1 + 1; 27 23 5 - 1; 28 - divideInt(5, 2); 24 + Math.trunc(5 / 2); 29 25 3 * 3; 30 - remainderInt(5, 2); 26 + 5 % 2; 31 27 2 > 1; 32 28 2 < 1; 33 29 2 >= 1;
+13
compiler-core/src/javascript/tests/snapshots/gleam_core__javascript__tests__numbers__remainder_by_non_zero_int.snap
··· 1 + --- 2 + source: compiler-core/src/javascript/tests/numbers.rs 3 + expression: "pub fn main() {\n 1 % 2\n}" 4 + --- 5 + ----- SOURCE CODE 6 + pub fn main() { 7 + 1 % 2 8 + } 9 + 10 + ----- COMPILED JAVASCRIPT 11 + export function main() { 12 + return 1 % 2; 13 + }
+15
compiler-core/src/javascript/tests/snapshots/gleam_core__javascript__tests__numbers__remainder_by_zero_int.snap
··· 1 + --- 2 + source: compiler-core/src/javascript/tests/numbers.rs 3 + expression: "pub fn main() {\n 1 % 0\n}" 4 + --- 5 + ----- SOURCE CODE 6 + pub fn main() { 7 + 1 % 0 8 + } 9 + 10 + ----- COMPILED JAVASCRIPT 11 + import { remainderInt } from "../gleam.mjs"; 12 + 13 + export function main() { 14 + return remainderInt(1, 0); 15 + }