tsrun 0.1.23

A TypeScript interpreter designed for embedding in applications
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
//! C FFI for custom RegExp providers.
//!
//! This module allows C/C++ embedders to provide their own regex implementation
//! by registering callback functions.
//!
//! # Example
//!
//! ```c
//! // Define callbacks
//! static void* my_compile(void* userdata, const char* pattern, const char* flags,
//!                         const char** error_out) {
//!     // Compile pattern, return handle
//!     return my_regex_compile(pattern, flags);
//! }
//!
//! static int my_is_match(void* userdata, void* handle, const char* input,
//!                        size_t input_len, const char** error_out) {
//!     return my_regex_test(handle, input, input_len) ? 1 : 0;
//! }
//!
//! // ... other callbacks ...
//!
//! static void my_free(void* userdata, void* handle) {
//!     my_regex_free(handle);
//! }
//!
//! // Register provider
//! TsRunRegexCallbacks callbacks = {
//!     .compile = my_compile,
//!     .is_match = my_is_match,
//!     .find = my_find,
//!     .free = my_free,
//!     .userdata = NULL
//! };
//! tsrun_set_regexp_provider(ctx, &callbacks);
//! ```

extern crate alloc;

use alloc::rc::Rc;
use alloc::string::String;
use alloc::vec::Vec;
use core::cell::RefCell;
use core::ffi::{c_char, c_void};
use core::fmt;
use core::ptr;

use crate::platform::{CompiledRegex, RegExpProvider, RegexMatch};

use super::{TsRunContext, TsRunResult, c_str_to_str};

// ============================================================================
// C Callback Type Definitions
// ============================================================================

/// Compile a regex pattern.
///
/// # Parameters
/// - `userdata`: User-provided context pointer
/// - `pattern`: The regex pattern (UTF-8, null-terminated)
/// - `flags`: The regex flags string (e.g., "gi", null-terminated)
/// - `error_out`: On error, set to a static error message (valid until next call)
///
/// # Returns
/// - On success: An opaque handle to the compiled regex
/// - On error: NULL (and *error_out contains error message)
pub type TsRunRegexCompileFn = extern "C" fn(
    userdata: *mut c_void,
    pattern: *const c_char,
    flags: *const c_char,
    error_out: *mut *const c_char,
) -> *mut c_void;

/// Test if a regex matches the input string.
///
/// # Parameters
/// - `userdata`: User-provided context pointer
/// - `handle`: Compiled regex handle from `compile`
/// - `input`: The input string (UTF-8, not null-terminated)
/// - `input_len`: Length of input in bytes
/// - `error_out`: On error, set to a static error message
///
/// # Returns
/// - 1: Match found
/// - 0: No match
/// - -1: Error (check *error_out)
pub type TsRunRegexIsMatchFn = extern "C" fn(
    userdata: *mut c_void,
    handle: *mut c_void,
    input: *const c_char,
    input_len: usize,
    error_out: *mut *const c_char,
) -> i32;

/// Result from a regex find operation.
#[repr(C)]
#[derive(Debug, Clone)]
pub struct TsRunRegexMatch {
    /// Byte offset where match starts
    pub start: usize,
    /// Byte offset where match ends (exclusive)
    pub end: usize,
    /// Array of capture groups (start, end pairs)
    /// NULL if no captures or not supported
    pub captures: *mut TsRunRegexCapture,
    /// Number of capture groups (including group 0 = full match)
    pub capture_count: usize,
}

/// A single capture group result.
#[repr(C)]
#[derive(Debug, Clone, Copy)]
pub struct TsRunRegexCapture {
    /// Start byte offset (-1 if group didn't participate)
    pub start: isize,
    /// End byte offset (-1 if group didn't participate)
    pub end: isize,
}

/// Find the first match starting at a given position.
///
/// # Parameters
/// - `userdata`: User-provided context pointer
/// - `handle`: Compiled regex handle
/// - `input`: The input string (UTF-8, not null-terminated)
/// - `input_len`: Length of input in bytes
/// - `start_pos`: Byte offset to start searching from
/// - `match_out`: Output match result (caller-allocated)
/// - `error_out`: On error, set to a static error message
///
/// # Returns
/// - 1: Match found (match_out populated)
/// - 0: No match
/// - -1: Error
pub type TsRunRegexFindFn = extern "C" fn(
    userdata: *mut c_void,
    handle: *mut c_void,
    input: *const c_char,
    input_len: usize,
    start_pos: usize,
    match_out: *mut TsRunRegexMatch,
    error_out: *mut *const c_char,
) -> i32;

/// Free a compiled regex handle.
///
/// # Parameters
/// - `userdata`: User-provided context pointer
/// - `handle`: Compiled regex handle to free
pub type TsRunRegexFreeFn = extern "C" fn(userdata: *mut c_void, handle: *mut c_void);

/// Free a match result's captures array.
///
/// # Parameters
/// - `userdata`: User-provided context pointer
/// - `captures`: Captures array to free
/// - `count`: Number of captures
pub type TsRunRegexFreeCapturesFn =
    extern "C" fn(userdata: *mut c_void, captures: *mut TsRunRegexCapture, count: usize);

// ============================================================================
// C Callback Bundle
// ============================================================================

/// Bundle of regex callbacks for the C FFI.
#[repr(C)]
#[derive(Clone, Copy)]
pub struct TsRunRegexCallbacks {
    /// Compile a regex pattern
    pub compile: TsRunRegexCompileFn,
    /// Test if regex matches
    pub is_match: TsRunRegexIsMatchFn,
    /// Find first match at position
    pub find: TsRunRegexFindFn,
    /// Free a compiled regex
    pub free: TsRunRegexFreeFn,
    /// Free captures array (may be NULL if not needed)
    pub free_captures: Option<TsRunRegexFreeCapturesFn>,
    /// User-provided context pointer passed to all callbacks
    pub userdata: *mut c_void,
}

// ============================================================================
// C RegExp Provider
// ============================================================================

/// RegExp provider that delegates to C callbacks.
pub struct CRegExpProvider {
    callbacks: TsRunRegexCallbacks,
}

impl CRegExpProvider {
    /// Create a new C-backed RegExp provider.
    ///
    /// # Safety
    /// The callbacks must remain valid for the lifetime of this provider.
    pub unsafe fn new(callbacks: TsRunRegexCallbacks) -> Self {
        Self { callbacks }
    }
}

impl RegExpProvider for CRegExpProvider {
    fn compile(&self, pattern: &str, flags: &str) -> Result<Rc<dyn CompiledRegex>, String> {
        // Convert to C strings
        let pattern_cstr = match alloc::ffi::CString::new(pattern) {
            Ok(s) => s,
            Err(_) => return Err(String::from("Pattern contains null bytes")),
        };
        let flags_cstr = match alloc::ffi::CString::new(flags) {
            Ok(s) => s,
            Err(_) => return Err(String::from("Flags contain null bytes")),
        };

        let mut error_out: *const c_char = ptr::null();

        let handle = (self.callbacks.compile)(
            self.callbacks.userdata,
            pattern_cstr.as_ptr(),
            flags_cstr.as_ptr(),
            &mut error_out,
        );

        if handle.is_null() {
            let error_msg = if error_out.is_null() {
                String::from("Regex compilation failed")
            } else {
                unsafe { c_str_to_str(error_out) }
                    .map(String::from)
                    .unwrap_or_else(|| String::from("Regex compilation failed"))
            };
            return Err(error_msg);
        }

        Ok(Rc::new(CCompiledRegex {
            handle: RefCell::new(handle),
            callbacks: CRegexCallbacksRef {
                is_match: self.callbacks.is_match,
                find: self.callbacks.find,
                free: self.callbacks.free,
                free_captures: self.callbacks.free_captures,
                userdata: self.callbacks.userdata,
            },
            flags: String::from(flags),
        }))
    }
}

/// Reference to callbacks needed by CCompiledRegex.
#[derive(Clone, Copy)]
struct CRegexCallbacksRef {
    is_match: TsRunRegexIsMatchFn,
    find: TsRunRegexFindFn,
    free: TsRunRegexFreeFn,
    free_captures: Option<TsRunRegexFreeCapturesFn>,
    userdata: *mut c_void,
}

/// Compiled regex backed by C callbacks.
struct CCompiledRegex {
    handle: RefCell<*mut c_void>,
    callbacks: CRegexCallbacksRef,
    flags: String,
}

impl fmt::Debug for CCompiledRegex {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("CCompiledRegex")
            .field("flags", &self.flags)
            .finish()
    }
}

impl Drop for CCompiledRegex {
    fn drop(&mut self) {
        let handle = *self.handle.borrow();
        if !handle.is_null() {
            (self.callbacks.free)(self.callbacks.userdata, handle);
        }
    }
}

impl CompiledRegex for CCompiledRegex {
    fn is_match(&self, input: &str) -> Result<bool, String> {
        let handle = *self.handle.borrow();
        let mut error_out: *const c_char = ptr::null();

        let result = (self.callbacks.is_match)(
            self.callbacks.userdata,
            handle,
            input.as_ptr() as *const c_char,
            input.len(),
            &mut error_out,
        );

        match result {
            1 => Ok(true),
            0 => Ok(false),
            _ => {
                let error_msg = if error_out.is_null() {
                    String::from("Regex match failed")
                } else {
                    unsafe { c_str_to_str(error_out) }
                        .map(String::from)
                        .unwrap_or_else(|| String::from("Regex match failed"))
                };
                Err(error_msg)
            }
        }
    }

    fn find(&self, input: &str, start_pos: usize) -> Result<Option<RegexMatch>, String> {
        let handle = *self.handle.borrow();
        let mut error_out: *const c_char = ptr::null();
        let mut match_out = TsRunRegexMatch {
            start: 0,
            end: 0,
            captures: ptr::null_mut(),
            capture_count: 0,
        };

        let result = (self.callbacks.find)(
            self.callbacks.userdata,
            handle,
            input.as_ptr() as *const c_char,
            input.len(),
            start_pos,
            &mut match_out,
            &mut error_out,
        );

        match result {
            1 => {
                // Convert C match to Rust RegexMatch
                let captures = self.convert_captures(&match_out);

                // Free the C captures if needed
                if !match_out.captures.is_null()
                    && let Some(free_fn) = self.callbacks.free_captures
                {
                    free_fn(
                        self.callbacks.userdata,
                        match_out.captures,
                        match_out.capture_count,
                    );
                }

                Ok(Some(RegexMatch {
                    start: match_out.start,
                    end: match_out.end,
                    captures,
                }))
            }
            0 => Ok(None),
            _ => {
                let error_msg = if error_out.is_null() {
                    String::from("Regex find failed")
                } else {
                    unsafe { c_str_to_str(error_out) }
                        .map(String::from)
                        .unwrap_or_else(|| String::from("Regex find failed"))
                };
                Err(error_msg)
            }
        }
    }

    fn find_iter(&self, input: &str) -> Result<Vec<RegexMatch>, String> {
        let mut matches = Vec::new();
        let is_global = self.flags.contains('g');

        if !is_global {
            // Non-global: return at most one match
            if let Some(m) = self.find(input, 0)? {
                matches.push(m);
            }
            return Ok(matches);
        }

        // Global: find all matches
        let mut pos = 0;
        while pos <= input.len() {
            match self.find(input, pos)? {
                Some(m) => {
                    let next_pos = if m.start == m.end {
                        m.end + 1 // Prevent infinite loop on zero-width matches
                    } else {
                        m.end
                    };
                    matches.push(m);
                    pos = next_pos;
                }
                None => break,
            }
        }

        Ok(matches)
    }

    fn split(&self, input: &str) -> Result<Vec<String>, String> {
        // split() always iterates all matches, regardless of global flag
        let matches = self.find_all_matches(input)?;
        let mut result = Vec::new();
        let mut last_end = 0;

        for m in matches {
            if let Some(before) = input.get(last_end..m.start) {
                result.push(String::from(before));
            }
            last_end = m.end;
        }

        if let Some(rest) = input.get(last_end..) {
            result.push(String::from(rest));
        } else if last_end == 0 {
            result.push(String::from(input));
        }

        Ok(result)
    }

    fn replace(&self, input: &str, replacement: &str) -> Result<String, String> {
        match self.find(input, 0)? {
            Some(m) => {
                let expanded = self.expand_replacement(replacement, input, &m);
                let before = input.get(..m.start).unwrap_or("");
                let after = input.get(m.end..).unwrap_or("");
                Ok(alloc::format!("{}{}{}", before, expanded, after))
            }
            None => Ok(String::from(input)),
        }
    }

    fn replace_all(&self, input: &str, replacement: &str) -> Result<String, String> {
        let matches = self.find_iter(input)?;

        if matches.is_empty() {
            return Ok(String::from(input));
        }

        let mut result = String::new();
        let mut last_end = 0;

        for m in matches {
            if let Some(before) = input.get(last_end..m.start) {
                result.push_str(before);
            }
            let expanded = self.expand_replacement(replacement, input, &m);
            result.push_str(&expanded);
            last_end = m.end;
        }

        if let Some(rest) = input.get(last_end..) {
            result.push_str(rest);
        }

        Ok(result)
    }
}

impl CCompiledRegex {
    /// Find all matches regardless of global flag.
    /// Used by split() which always needs all matches.
    fn find_all_matches(&self, input: &str) -> Result<Vec<RegexMatch>, String> {
        let mut matches = Vec::new();
        let mut pos = 0;

        while pos <= input.len() {
            match self.find(input, pos)? {
                Some(m) => {
                    let next_pos = if m.start == m.end {
                        m.end + 1 // Prevent infinite loop on zero-width matches
                    } else {
                        m.end
                    };
                    matches.push(m);
                    pos = next_pos;
                }
                None => break,
            }
        }

        Ok(matches)
    }

    /// Convert C captures to Rust format.
    fn convert_captures(&self, match_out: &TsRunRegexMatch) -> Vec<Option<(usize, usize)>> {
        let mut captures = Vec::new();

        if match_out.captures.is_null() || match_out.capture_count == 0 {
            // No captures provided - use match bounds as group 0
            captures.push(Some((match_out.start, match_out.end)));
            return captures;
        }

        for i in 0..match_out.capture_count {
            let cap = unsafe { *match_out.captures.add(i) };
            if cap.start >= 0 && cap.end >= 0 {
                captures.push(Some((cap.start as usize, cap.end as usize)));
            } else {
                captures.push(None);
            }
        }

        captures
    }

    /// Expand replacement patterns like $1, $&, $$.
    fn expand_replacement(&self, replacement: &str, input: &str, m: &RegexMatch) -> String {
        let mut result = String::new();
        let chars: Vec<char> = replacement.chars().collect();
        let len = chars.len();
        let mut i = 0;

        while i < len {
            if chars.get(i) == Some(&'$') && i + 1 < len {
                match chars.get(i + 1) {
                    Some('$') => {
                        result.push('$');
                        i += 2;
                    }
                    Some('&') => {
                        if let Some(Some((start, end))) = m.captures.first()
                            && let Some(s) = input.get(*start..*end)
                        {
                            result.push_str(s);
                        }
                        i += 2;
                    }
                    Some('`') => {
                        if let Some(before) = input.get(..m.start) {
                            result.push_str(before);
                        }
                        i += 2;
                    }
                    Some('\'') => {
                        if let Some(after) = input.get(m.end..) {
                            result.push_str(after);
                        }
                        i += 2;
                    }
                    Some(c) if c.is_ascii_digit() => {
                        let group_num = (*c as usize) - ('0' as usize);
                        if group_num > 0
                            && let Some(Some((start, end))) = m.captures.get(group_num)
                            && let Some(s) = input.get(*start..*end)
                        {
                            result.push_str(s);
                        }
                        i += 2;
                    }
                    _ => {
                        result.push('$');
                        i += 1;
                    }
                }
            } else if let Some(c) = chars.get(i) {
                result.push(*c);
                i += 1;
            } else {
                break;
            }
        }

        result
    }
}

// ============================================================================
// FFI Functions
// ============================================================================

/// Set a custom RegExp provider using C callbacks.
///
/// # Safety
/// - `ctx` must be a valid `TsRunContext` pointer
/// - `callbacks` must be a valid pointer to a `TsRunRegexCallbacks` struct
/// - The callbacks must remain valid for as long as the context is used
///
/// # Returns
/// A `TsRunResult` indicating success or failure.
#[unsafe(no_mangle)]
pub extern "C" fn tsrun_set_regexp_provider(
    ctx: *mut TsRunContext,
    callbacks: *const TsRunRegexCallbacks,
) -> TsRunResult {
    if ctx.is_null() {
        return TsRunResult {
            ok: false,
            error: ptr::null(),
        };
    }

    if callbacks.is_null() {
        let ctx = unsafe { &mut *ctx };
        return TsRunResult::err(ctx, String::from("callbacks is null"));
    }

    let ctx = unsafe { &mut *ctx };
    let callbacks = unsafe { *callbacks };

    // Create provider
    let provider = unsafe { CRegExpProvider::new(callbacks) };

    // Set on interpreter
    ctx.interp.set_regexp_provider(Rc::new(provider));

    TsRunResult::success()
}