Skip to content

Commit 5419792

Browse files
feat: implement arrow functions and array higher-order methods
Major Features: - Arrow function support with lambda generation - Array methods: forEach, find, findIndex, some, every, includes - Enhanced destructuring with object rest properties - Spread operator for arrays with concatenation - C++ reserved keyword escaping Runtime Enhancements: - Added arithmetic operators to js::any (*, /, -, %) - Added comparison operators to js::any (>, <, >=, <=, ==, !=) - Fixed object rest properties using lambda wrappers - Added entries() method to js::object Quality Improvements: - Better array method detection in code generator - Improved identifier mapping for reserved words - Corrected === operator mapping to == - Version bump to 0.7.0
1 parent c8346d8 commit 5419792

File tree

6 files changed

+474
-10
lines changed

6 files changed

+474
-10
lines changed

RELEASE_NOTES_v0.7.x.md

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
# Release Notes - v0.7.x Series
2+
3+
## Version 0.7.0 - Arrow Functions and Array Methods (2025-01-14)
4+
5+
Major update introducing arrow functions and comprehensive array higher-order methods support.
6+
7+
### Added
8+
9+
#### Arrow Functions
10+
- Full arrow function support with lambda generation
11+
- Lexical `this` binding with capture modes
12+
- Single expression and block body syntax
13+
- Type inference for arrow function parameters
14+
- Proper return type deduction
15+
16+
#### Array Higher-Order Methods
17+
- `forEach()` - Execute function for each element
18+
- `find()` - Find first element matching predicate
19+
- `findIndex()` - Find index of first matching element
20+
- `some()` - Test if at least one element matches
21+
- `every()` - Test if all elements match
22+
- `includes()` - Check if array contains value
23+
- Enhanced `map()`, `filter()`, `reduce()` with js::any support
24+
25+
#### Destructuring Enhancements
26+
- Object rest properties (`const { a, ...rest } = obj`)
27+
- Array rest elements (`const [first, ...rest] = arr`)
28+
- Lambda-wrapped rest object generation for global scope
29+
- Proper entries() method for object iteration
30+
31+
#### Spread Operator
32+
- Array spread in array literals (`[...arr1, ...arr2]`)
33+
- Concatenation support for spread operations
34+
- Empty array handling with explicit type specification
35+
36+
### Fixed
37+
38+
#### Code Generation
39+
- C++ reserved keywords now properly escaped with underscore suffix
40+
- Variable names checked against full C++ keyword list
41+
- Fixed object rest properties at global scope using lambda wrappers
42+
- Corrected === and !== operator mapping to == and !=
43+
44+
#### Runtime Library
45+
- Added missing arithmetic operators to js::any (*, /, -, %)
46+
- Added comparison operators to js::any (>, <, >=, <=, ==, !=)
47+
- Added operator overloads for js::any with js::number
48+
- Enhanced js::any to delegate array methods to underlying arrays
49+
- Added entries() method to js::object for iteration
50+
51+
### Enhanced
52+
53+
#### Developer Experience
54+
- Better array method detection in code generator
55+
- Improved identifier mapping for reserved words
56+
- More comprehensive operator support in runtime
57+
- Cleaner lambda generation for complex patterns
58+
59+
### Technical Details
60+
61+
#### Runtime Additions
62+
```cpp
63+
// New array methods in js::array<T>
64+
template<typename Func> void forEach(Func&& func) const;
65+
template<typename Func> T find(Func&& func) const;
66+
template<typename Func> int findIndex(Func&& func) const;
67+
template<typename Func> bool some(Func&& func) const;
68+
template<typename Func> bool every(Func&& func) const;
69+
bool includes(const T& value) const;
70+
71+
// New operators in js::any
72+
any operator*(const number& other) const;
73+
any operator/(const number& other) const;
74+
any operator-(const number& other) const;
75+
any operator%(const number& other) const;
76+
bool operator>(const number& other) const;
77+
bool operator<(const number& other) const;
78+
bool operator>=(const number& other) const;
79+
bool operator<=(const number& other) const;
80+
bool operator==(const number& other) const;
81+
bool operator!=(const number& other) const;
82+
```
83+
84+
#### Breaking Changes
85+
None - All changes are additive or bug fixes.
86+
87+
### Known Limitations
88+
89+
- Lambda functions cannot be directly stored as js::any (need std::function wrapper)
90+
- Some complex arrow function patterns may require manual adjustment
91+
- Object rest iteration requires C++17 structured bindings
92+
93+
### Migration Guide
94+
95+
No migration required. Existing code continues to work with enhanced functionality available.
96+
97+
### Contributors
98+
99+
This release represents significant progress in JavaScript feature parity, bringing modern array operations and arrow functions to the C++ transpilation.

TODO.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ This document tracks planned features and known issues for typescript2cxx.
44

55
## 📊 Implementation Status Summary
66

7-
**Current Version: v0.5.3**
7+
**Current Version: v0.7.0-dev**
88

99
### Overall Progress
1010

deno.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@wowemulation-dev/typescript2cxx",
3-
"version": "0.6.0",
3+
"version": "0.7.0",
44
"license": "MIT OR Apache-2.0",
55
"exports": {
66
".": "./src/mod.ts",

runtime/core.h

Lines changed: 218 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,68 @@ class array {
228228
std::vector<T> sliced(elements_.begin() + start, elements_.begin() + actualEnd);
229229
return array<T>(sliced);
230230
}
231+
232+
// forEach method - executes a function for each element
233+
template<typename Func>
234+
void forEach(Func&& func) const {
235+
for (const auto& elem : elements_) {
236+
func(elem);
237+
}
238+
}
239+
240+
// find method - returns first element that satisfies the predicate
241+
template<typename Func>
242+
T find(Func&& func) const {
243+
for (const auto& elem : elements_) {
244+
if (func(elem)) {
245+
return elem;
246+
}
247+
}
248+
return T(); // Return default value if not found
249+
}
250+
251+
// findIndex method - returns index of first element that satisfies the predicate
252+
template<typename Func>
253+
int findIndex(Func&& func) const {
254+
for (size_t i = 0; i < elements_.size(); ++i) {
255+
if (func(elements_[i])) {
256+
return static_cast<int>(i);
257+
}
258+
}
259+
return -1; // Return -1 if not found
260+
}
261+
262+
// some method - tests whether at least one element passes the test
263+
template<typename Func>
264+
bool some(Func&& func) const {
265+
for (const auto& elem : elements_) {
266+
if (func(elem)) {
267+
return true;
268+
}
269+
}
270+
return false;
271+
}
272+
273+
// every method - tests whether all elements pass the test
274+
template<typename Func>
275+
bool every(Func&& func) const {
276+
for (const auto& elem : elements_) {
277+
if (!func(elem)) {
278+
return false;
279+
}
280+
}
281+
return true;
282+
}
283+
284+
// includes method - checks if array includes a certain value
285+
bool includes(const T& value) const {
286+
for (const auto& elem : elements_) {
287+
if (elem == value) {
288+
return true;
289+
}
290+
}
291+
return false;
292+
}
231293
};
232294

233295
// Object class for JavaScript objects
@@ -261,6 +323,11 @@ class object {
261323
bool has(const std::string& key) const {
262324
return properties_.find(key) != properties_.end();
263325
}
326+
327+
// Get all entries as a range for iteration
328+
const std::unordered_map<std::string, std::any>& entries() const {
329+
return properties_;
330+
}
264331
};
265332

266333
// Console class
@@ -466,8 +533,159 @@ class any {
466533
return any(toString() + other);
467534
}
468535

536+
// Arithmetic operators with numbers
537+
any operator*(const number& other) const {
538+
if (is<number>()) {
539+
return any(number(get<number>().value() * other.value()));
540+
}
541+
return undefined;
542+
}
543+
544+
any operator/(const number& other) const {
545+
if (is<number>()) {
546+
return any(number(get<number>().value() / other.value()));
547+
}
548+
return undefined;
549+
}
550+
551+
any operator-(const number& other) const {
552+
if (is<number>()) {
553+
return any(number(get<number>().value() - other.value()));
554+
}
555+
return undefined;
556+
}
557+
558+
any operator%(const number& other) const {
559+
if (is<number>()) {
560+
return any(number(std::fmod(get<number>().value(), other.value())));
561+
}
562+
return undefined;
563+
}
564+
565+
// Comparison operators
566+
bool operator>(const number& other) const {
567+
if (is<number>()) {
568+
return get<number>().value() > other.value();
569+
}
570+
return false;
571+
}
572+
573+
bool operator<(const number& other) const {
574+
if (is<number>()) {
575+
return get<number>().value() < other.value();
576+
}
577+
return false;
578+
}
579+
580+
bool operator>=(const number& other) const {
581+
if (is<number>()) {
582+
return get<number>().value() >= other.value();
583+
}
584+
return false;
585+
}
586+
587+
bool operator<=(const number& other) const {
588+
if (is<number>()) {
589+
return get<number>().value() <= other.value();
590+
}
591+
return false;
592+
}
593+
594+
bool operator==(const number& other) const {
595+
if (is<number>()) {
596+
return get<number>().value() == other.value();
597+
}
598+
return false;
599+
}
600+
601+
bool operator!=(const number& other) const {
602+
if (is<number>()) {
603+
return get<number>().value() != other.value();
604+
}
605+
return true;
606+
}
607+
469608
// Property assignment for objects - this method should not be used directly
470609
// Use explicit assignment through the object reference instead
610+
611+
// Array methods - delegate to underlying array if this contains an array
612+
template<typename Func>
613+
auto map(Func&& func) const {
614+
if (is<array<any>>()) {
615+
return get<array<any>>().map(std::forward<Func>(func));
616+
}
617+
return array<any>(); // Return empty array for non-arrays
618+
}
619+
620+
template<typename Func>
621+
auto filter(Func&& func) const {
622+
if (is<array<any>>()) {
623+
return get<array<any>>().filter(std::forward<Func>(func));
624+
}
625+
return array<any>(); // Return empty array for non-arrays
626+
}
627+
628+
template<typename Func, typename Init>
629+
auto reduce(Func&& func, Init&& init) const {
630+
if (is<array<any>>()) {
631+
return get<array<any>>().reduce(std::forward<Func>(func), std::forward<Init>(init));
632+
}
633+
return init; // Return initial value for non-arrays
634+
}
635+
636+
template<typename Func>
637+
void forEach(Func&& func) const {
638+
if (is<array<any>>()) {
639+
get<array<any>>().forEach(std::forward<Func>(func));
640+
}
641+
}
642+
643+
template<typename Func>
644+
any find(Func&& func) const {
645+
if (is<array<any>>()) {
646+
return get<array<any>>().find(std::forward<Func>(func));
647+
}
648+
return undefined; // Return undefined for non-arrays
649+
}
650+
651+
template<typename Func>
652+
number findIndex(Func&& func) const {
653+
if (is<array<any>>()) {
654+
return number(get<array<any>>().findIndex(std::forward<Func>(func)));
655+
}
656+
return number(-1); // Return -1 for non-arrays
657+
}
658+
659+
template<typename Func>
660+
bool some(Func&& func) const {
661+
if (is<array<any>>()) {
662+
return get<array<any>>().some(std::forward<Func>(func));
663+
}
664+
return false; // Return false for non-arrays
665+
}
666+
667+
template<typename Func>
668+
bool every(Func&& func) const {
669+
if (is<array<any>>()) {
670+
return get<array<any>>().every(std::forward<Func>(func));
671+
}
672+
return true; // Return true for non-arrays (vacuous truth)
673+
}
674+
675+
bool includes(const any& value) const {
676+
if (is<array<any>>()) {
677+
return get<array<any>>().includes(value);
678+
}
679+
return false; // Return false for non-arrays
680+
}
681+
682+
// As object for iteration
683+
object as_object() const {
684+
if (is<object>()) {
685+
return get<object>();
686+
}
687+
return object(); // Return empty object for non-objects
688+
}
471689
};
472690

473691
// Typedef for array<any>

0 commit comments

Comments
 (0)