Repository navigation
Conversation
There was a problem hiding this comment.
Code Review
This pull request implements requirements 14 to 18 of the Craft functions specification, introducing standard functions for splitting, joining, replacing, regular expressions (via a custom Pike VM for linear-time matching), parsing numbers from strings, and date calculations. It also adds metered functions that count execution steps against the runtime's budget to prevent CPU exhaustion. A critical bug was identified in regex.dart where Int32List.sublistView is incorrectly called with element indices instead of byte offsets, which would cause runtime errors; using sublist is recommended instead.
| Int32List.sublistView(points, 0, count), | ||
| Int32List.sublistView(offsets, 0, count + 1), |
There was a problem hiding this comment.
Using Int32List.sublistView with element indices count and count + 1 as the end parameter is a critical bug. In Dart, TypedData.sublistView expects byte offsets rather than element indices. Since Int32List has 4 bytes per element, this will throw an ArgumentError unless the index happens to be a multiple of 4, and even then, it will return a truncated view.
Using sublist(0, count) is much safer, correct, and allows the garbage collector to reclaim the unused portion of the pre-allocated lists.
points.sublist(0, count),
offsets.sublist(0, count + 1),There was a problem hiding this comment.
Code Review
This pull request implements standard functions for text manipulation (splitting, joining, replacing), regex operations (using a custom Pike VM), number parsing, and date calculations, along with step-metering to prevent denial-of-service attacks. A critical issue was identified in daysToDate where passing a non-finite double (like NaN or Infinity) can cause a crash because truncateToDouble() throws an UnsupportedError on non-finite values. A fix was suggested to check !days.isFinite first.
| String? _date(num days) { | ||
| if (days is double && days != days.truncateToDouble()) return null; | ||
| if (days < _firstDay || days > _lastDay) return null; |
There was a problem hiding this comment.
Calling truncateToDouble() on a non-finite double (such as double.nan or double.infinity) throws an UnsupportedError in Dart. This can cause the evaluation to crash unexpectedly when an untrusted template passes a non-finite number to daysToDate. Checking !days.isFinite first will safely prevent this crash.
| String? _date(num days) { | |
| if (days is double && days != days.truncateToDouble()) return null; | |
| if (days < _firstDay || days > _lastDay) return null; | |
| String? _date(num days) { | |
| if (!days.isFinite) return null; | |
| if (days is double && days != days.truncateToDouble()) return null; | |
| if (days < _firstDay || days > _lastDay) return null; |
340b93b to
5aaf10a
Compare
…dates Adds the standard functions that form templates needed and hosts had to register themselves (dogfooding: regex, split, toInt, ISO dates, list length): - split, join, replace (literal, linear-time search), which with toInt and toNumber count steps for the code units of their arguments and results; - regex, regexFind, regexCapture, regexReplace in a regex dialect: a subset of ECMAScript patterns (u flag, no lookaround, backreferences or quantified atoms that can match the empty string) run by a Pike VM in time linear in the value and identically on the VM, dart2js and dart2wasm; results are ECMAScript's (leftmost-first priority, captures cleared at each iteration). Patterns are limited to 256 code points and size 1,000, and compiling a pattern and each search count steps against maxSteps; - toInt and toNumber (strict decimals, exact 64-bit range check, exactly rounded doubles); - dateToDays, daysToDate and daysBetween (calendar-valid YYYY-MM-DD); - length of a list. craft_functions gains requirements 14 to 18 and the stepsExceeded evaluation error; craft_limits counts the steps of these functions in an expansion and a firing. The conformance harness now compares doubles with their sign, so -0.0 is not 0.0. Names follow A2UI's basic and MCP catalogs where they overlap (regex, split, regexCapture, regexReplace); the blueprint lists the differences.
5aaf10a to
3ea5b40
Compare
Description
Adds the standard functions templates were missing for real forms: text, number parsing, regular expressions and dates. Stacked on #57 (stack #51).
split,join, literalreplace;lengthnow also counts list elements.toInt(strict[+-]?digits, exact 64-bit range) andtoNumber(strict decimals, exactly rounded, ties to even, negative zero kept); absent on failure.regex(is there a match),regexFind,regexCapture,regexReplace. Names follow A2UI's catalog functions where they overlap; the blueprint lists the differences. Craft uses its own linear-time engine (an RE2-style Pike VM), never the platformRegExp, so results are identical on VM, JS and Wasm and no pattern can blow up. The dialect is an ECMAScript (u) subset without lookaround or backreferences; pattern size and length are capped.dateToDays,daysToDate,daysBetweenfor calendar-validYYYY-MM-DD(years 0000–9999).stepsExceedederror.dart test -p node; two integer-vs-double cases are JS-only skips.An independent review checked: 225k regex pattern/value pairs against ECMAScript (matches, captures, replace); a reimplementation from the blueprint alone that agrees on every case; linear time; 180k
toNumberinputs bit-identical to a reference; every date in range; identical results on VM, JS and Wasm.Pre-launch Checklist
python3 ./scripts/fix_licenses.py --check)../scripts/fix_format.sh --check).