qanglang-core 0.5.0

Core library for QangLang
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
use crate::{NativeFunctionError, Value, ValueKind, Vm};
use std::{
    fs::{self, OpenOptions},
    io::{Read, Seek, SeekFrom, Write},
    path::Path,
    time::UNIX_EPOCH,
};

pub fn qang_path_get_os_seperator(
    _arg_count: usize,
    vm: &mut Vm,
) -> Result<Option<Value>, NativeFunctionError> {
    let handle = vm.alloc.strings.intern(std::path::MAIN_SEPARATOR_STR);
    Ok(Some(Value::string(handle)))
}

pub fn qang_path_is_file(
    arg_count: usize,
    vm: &mut Vm,
) -> Result<Option<Value>, NativeFunctionError> {
    match vm
        .get_function_args(arg_count)
        .first()
        .copied()
        .map(|v| v.kind())
    {
        Some(ValueKind::String(handle)) => {
            let path = vm.alloc.strings.get(handle);
            let path = Path::new(path);
            Ok(Some(Value::boolean(path.is_file())))
        }
        None => Err(NativeFunctionError::new("Missing path")),
        _ => Err(NativeFunctionError::new("Path must be a string.")),
    }
}

pub fn qang_path_exists(
    arg_count: usize,
    vm: &mut Vm,
) -> Result<Option<Value>, NativeFunctionError> {
    match vm
        .get_function_args(arg_count)
        .first()
        .copied()
        .map(|v| v.kind())
    {
        Some(ValueKind::String(handle)) => {
            let path = vm.alloc.strings.get(handle);
            let path = Path::new(path);
            Ok(Some(Value::boolean(path.exists())))
        }
        None => Err(NativeFunctionError::new("Missing path")),
        _ => Err(NativeFunctionError::new("Path must be a string.")),
    }
}

pub fn qang_path_is_absolute(
    arg_count: usize,
    vm: &mut Vm,
) -> Result<Option<Value>, NativeFunctionError> {
    match vm
        .get_function_args(arg_count)
        .first()
        .copied()
        .map(|v| v.kind())
    {
        Some(ValueKind::String(handle)) => {
            let path = vm.alloc.strings.get(handle);
            let path = Path::new(path);
            Ok(Some(Value::boolean(path.is_absolute())))
        }
        None => Err(NativeFunctionError::new("Missing path")),
        _ => Err(NativeFunctionError::new("Path must be a string.")),
    }
}

pub fn qang_path_is_relative(
    arg_count: usize,
    vm: &mut Vm,
) -> Result<Option<Value>, NativeFunctionError> {
    match vm
        .get_function_args(arg_count)
        .first()
        .copied()
        .map(|v| v.kind())
    {
        Some(ValueKind::String(handle)) => {
            let path = vm.alloc.strings.get(handle);
            let path = Path::new(path);
            Ok(Some(Value::boolean(path.is_relative())))
        }
        None => Err(NativeFunctionError::new("Missing path")),
        _ => Err(NativeFunctionError::new("Path must be a string.")),
    }
}

pub fn qang_path_is_dir(
    arg_count: usize,
    vm: &mut Vm,
) -> Result<Option<Value>, NativeFunctionError> {
    match vm
        .get_function_args(arg_count)
        .first()
        .copied()
        .map(|v| v.kind())
    {
        Some(ValueKind::String(handle)) => {
            let path = vm.alloc.strings.get(handle);
            let path = Path::new(path);
            Ok(Some(Value::boolean(path.is_dir())))
        }
        None => Err(NativeFunctionError::new("Missing path")),
        _ => Err(NativeFunctionError::new("Path must be a string.")),
    }
}

pub fn qang_fs_read_file(
    args_count: usize,
    vm: &mut Vm,
) -> Result<Option<Value>, NativeFunctionError> {
    match vm
        .get_function_args(args_count)
        .first()
        .copied()
        .map(|v| v.kind())
    {
        Some(ValueKind::String(handle)) => {
            let path = vm.alloc.strings.get(handle);
            let path = Path::new(path);
            let contents =
                fs::read(path).map_err(|_| NativeFunctionError::new("Unable to read file."))?;
            let contents_str = std::str::from_utf8(&contents)
                .map_err(|_| NativeFunctionError::new("Unable to convert file to string"))?;
            let string_handle = vm.alloc.strings.intern(contents_str);
            Ok(Some(Value::string(string_handle)))
        }
        None => Err(NativeFunctionError::new("Missing path")),
        _ => Err(NativeFunctionError::new("Path must be a string.")),
    }
}

/// Reads a chunk of the file starting at the given byte offset.
/// Args: (path: string, offset: number, chunk_size: number)
/// Returns: object { content: string, bytes_read: number } or nil if EOF
pub fn qang_fs_read_chunk(
    args_count: usize,
    vm: &mut Vm,
) -> Result<Option<Value>, NativeFunctionError> {
    let args = vm.get_function_args(args_count);
    let path = args.first().copied().unwrap_or_else(|| Value::nil()).kind();
    let offset = args.get(1).copied().unwrap_or_else(|| Value::nil()).kind();
    let chunk_size = args.get(2).copied().unwrap_or_else(|| Value::nil()).kind();

    match (path, offset, chunk_size) {
        (
            ValueKind::String(path_handle),
            ValueKind::Number(offset),
            ValueKind::Number(chunk_size),
        ) => {
            let path = vm.alloc.strings.get(path_handle);
            let path = Path::new(path);

            let offset = offset.max(0.0) as u64;
            let chunk_size = chunk_size.max(0.0) as usize;

            // Open file and seek to offset
            let mut file = fs::File::open(path)
                .map_err(|_| NativeFunctionError::new("Unable to open file."))?;

            file.seek(SeekFrom::Start(offset))
                .map_err(|_| NativeFunctionError::new("Unable to seek in file."))?;

            // Read chunk_size bytes from current position
            let mut buffer = vec![0u8; chunk_size];
            let bytes_read = file
                .read(&mut buffer)
                .map_err(|_| NativeFunctionError::new("Unable to read from file."))?;

            // If we read 0 bytes, we're at EOF
            if bytes_read == 0 {
                return Ok(Some(Value::nil()));
            }

            // Use only the bytes we actually read
            let chunk = &buffer[..bytes_read];

            // Convert to string, handling UTF-8 boundaries
            // If we're in the middle of a UTF-8 character, adjust the slice
            let (chunk_str, actual_bytes_read) = match std::str::from_utf8(chunk) {
                Ok(s) => (s, chunk.len()),
                Err(e) => {
                    // Try to recover by finding a valid UTF-8 boundary
                    let valid_up_to = e.valid_up_to();
                    let valid_chunk = &chunk[..valid_up_to];
                    let s = std::str::from_utf8(valid_chunk).map_err(|_| {
                        NativeFunctionError::new("Unable to convert chunk to string")
                    })?;
                    (s, valid_up_to)
                }
            };

            // Create an object literal with both the content and the actual bytes read
            let string_handle = vm.alloc.strings.intern(chunk_str);
            let content_key = vm.alloc.strings.intern("content");
            let bytes_key = vm.alloc.strings.intern("bytes_read");

            let table_handle = vm.alloc.tables.new_hashmap();
            vm.alloc.tables.insert(
                table_handle,
                Value::string(content_key),
                Value::string(string_handle),
            );
            vm.alloc.tables.insert(
                table_handle,
                Value::string(bytes_key),
                Value::number(actual_bytes_read as f64),
            );

            Ok(Some(Value::object_literal(table_handle)))
        }
        (ValueKind::String(_), _, _) => Err(NativeFunctionError::new(
            "offset and chunk_size must be numbers",
        )),
        _ => Err(NativeFunctionError::new("Path must be a string.")),
    }
}

pub fn qang_fs_write_file(
    args_count: usize,
    vm: &mut Vm,
) -> Result<Option<Value>, NativeFunctionError> {
    let args = vm.get_function_args(args_count);
    let path = args.first().copied().unwrap_or_else(|| Value::nil()).kind();
    let data = args.get(1).copied().unwrap_or_else(|| Value::nil()).kind();

    match (path, data) {
        (ValueKind::String(path_handle), ValueKind::String(data_handle)) => {
            let path = vm.alloc.strings.get(path_handle);
            let path = Path::new(path);
            let data: &str = vm.alloc.strings.get(data_handle);

            Ok(Some(Value::boolean(fs::write(path, data).is_ok())))
        }
        (ValueKind::String(_), _) => Err(NativeFunctionError::new(
            "Can only write strings to file system.",
        )),
        _ => Err(NativeFunctionError::new("Path must be a string.")),
    }
}

pub fn qang_fs_remove_file(
    args_count: usize,
    vm: &mut Vm,
) -> Result<Option<Value>, NativeFunctionError> {
    match vm
        .get_function_args(args_count)
        .first()
        .copied()
        .map(|v| v.kind())
    {
        Some(ValueKind::String(handle)) => {
            let path = vm.alloc.strings.get(handle);
            let path = Path::new(path);
            Ok(Some(Value::boolean(fs::remove_file(path).is_ok())))
        }
        None => Err(NativeFunctionError::new("Missing path")),
        _ => Err(NativeFunctionError::new("Path must be a string.")),
    }
}

pub fn qang_fs_append_file(
    args_count: usize,
    vm: &mut Vm,
) -> Result<Option<Value>, NativeFunctionError> {
    let args = vm.get_function_args(args_count);
    let path = args.first().copied().unwrap_or_else(|| Value::nil()).kind();
    let data = args.get(1).copied().unwrap_or_else(|| Value::nil()).kind();

    match (path, data) {
        (ValueKind::String(path_handle), ValueKind::String(data_handle)) => {
            let path = vm.alloc.strings.get(path_handle);
            let path = Path::new(path);
            let data: &str = vm.alloc.strings.get(data_handle);

            let result = OpenOptions::new()
                .create(true)
                .append(true)
                .open(path)
                .and_then(|mut file| file.write_all(data.as_bytes()));

            Ok(Some(Value::boolean(result.is_ok())))
        }
        (ValueKind::String(_), _) => Err(NativeFunctionError::new(
            "Can only write strings to file system.",
        )),
        _ => Err(NativeFunctionError::new("Path must be a string.")),
    }
}

pub fn qang_fs_list(args_count: usize, vm: &mut Vm) -> Result<Option<Value>, NativeFunctionError> {
    match vm
        .get_function_args(args_count)
        .first()
        .copied()
        .map(|v| v.kind())
    {
        Some(ValueKind::String(handle)) => {
            let path = vm.alloc.strings.get(handle);
            let path = Path::new(path);

            match fs::read_dir(path) {
                Ok(entries) => {
                    let array_handle = vm.alloc.arrays.create_array(0);
                    for entry in entries {
                        if let Ok(entry) = entry {
                            if let Some(file_name) = entry.file_name().to_str() {
                                let string_handle = vm.alloc.strings.intern(file_name);
                                vm.alloc
                                    .arrays
                                    .push(array_handle, Value::string(string_handle));
                            }
                        }
                    }

                    Ok(Some(Value::array(array_handle)))
                }
                _ => Ok(Some(Value::nil())),
            }
        }
        None => Err(NativeFunctionError::new("Missing path")),
        _ => Err(NativeFunctionError::new("Path must be a string.")),
    }
}

pub fn qang_fs_create_dir(
    args_count: usize,
    vm: &mut Vm,
) -> Result<Option<Value>, NativeFunctionError> {
    match vm
        .get_function_args(args_count)
        .first()
        .copied()
        .map(|v| v.kind())
    {
        Some(ValueKind::String(handle)) => {
            let path = vm.alloc.strings.get(handle);
            let path = Path::new(path);
            Ok(Some(Value::boolean(fs::create_dir(path).is_ok())))
        }
        None => Err(NativeFunctionError::new("Missing path")),
        _ => Err(NativeFunctionError::new("Path must be a string.")),
    }
}

pub fn qang_fs_create_dirs(
    args_count: usize,
    vm: &mut Vm,
) -> Result<Option<Value>, NativeFunctionError> {
    match vm
        .get_function_args(args_count)
        .first()
        .copied()
        .map(|v| v.kind())
    {
        Some(ValueKind::String(handle)) => {
            let path = vm.alloc.strings.get(handle);
            let path = Path::new(path);
            Ok(Some(Value::boolean(fs::create_dir_all(path).is_ok())))
        }
        None => Err(NativeFunctionError::new("Missing path")),
        _ => Err(NativeFunctionError::new("Path must be a string.")),
    }
}

pub fn qang_fs_remove_dir(
    args_count: usize,
    vm: &mut Vm,
) -> Result<Option<Value>, NativeFunctionError> {
    match vm
        .get_function_args(args_count)
        .first()
        .copied()
        .map(|v| v.kind())
    {
        Some(ValueKind::String(handle)) => {
            let path = vm.alloc.strings.get(handle);
            let path = Path::new(path);
            Ok(Some(Value::boolean(fs::remove_dir(path).is_ok())))
        }
        None => Err(NativeFunctionError::new("Missing path")),
        _ => Err(NativeFunctionError::new("Path must be a string.")),
    }
}

pub fn qang_fs_remove_all(
    args_count: usize,
    vm: &mut Vm,
) -> Result<Option<Value>, NativeFunctionError> {
    match vm
        .get_function_args(args_count)
        .first()
        .copied()
        .map(|v| v.kind())
    {
        Some(ValueKind::String(handle)) => {
            let path = vm.alloc.strings.get(handle);
            let path = Path::new(path);
            Ok(Some(Value::boolean(fs::remove_dir_all(path).is_ok())))
        }
        None => Err(NativeFunctionError::new("Missing path")),
        _ => Err(NativeFunctionError::new("Path must be a string.")),
    }
}

pub fn qang_fs_get_file_size(
    args_count: usize,
    vm: &mut Vm,
) -> Result<Option<Value>, NativeFunctionError> {
    match vm
        .get_function_args(args_count)
        .first()
        .copied()
        .map(|v| v.kind())
    {
        Some(ValueKind::String(handle)) => {
            let path = vm.alloc.strings.get(handle);
            let path = Path::new(path);

            let metadata = fs::metadata(path);

            match metadata {
                Ok(metadata) => Ok(Some(Value::number(metadata.len() as f64))),
                Err(_) => Ok(Some(Value::nil())),
            }
        }
        None => Err(NativeFunctionError::new("Missing path")),
        _ => Err(NativeFunctionError::new("Path must be a string.")),
    }
}

pub fn qang_fs_get_file_modified_time(
    args_count: usize,
    vm: &mut Vm,
) -> Result<Option<Value>, NativeFunctionError> {
    match vm
        .get_function_args(args_count)
        .first()
        .copied()
        .map(|v| v.kind())
    {
        Some(ValueKind::String(handle)) => {
            let path = vm.alloc.strings.get(handle);
            let path = Path::new(path);

            let duration = fs::metadata(path)
                .map_err(|_| NativeFunctionError::new("Unable to get file metadata."))
                .and_then(|metadata| {
                    metadata
                        .modified()
                        .map_err(|_| NativeFunctionError::new("Unable to get modified time."))
                })
                .and_then(|modified| {
                    modified
                        .duration_since(UNIX_EPOCH)
                        .map_err(|_| NativeFunctionError::new("Unable to calculate timestamp."))
                });

            match duration {
                Ok(duration) => Ok(Some(Value::number(duration.as_millis() as f64))),
                Err(_) => Ok(Some(Value::nil())),
            }
        }
        None => Err(NativeFunctionError::new("Missing path")),
        _ => Err(NativeFunctionError::new("Path must be a string.")),
    }
}

pub fn qang_fs_copy_file(
    args_count: usize,
    vm: &mut Vm,
) -> Result<Option<Value>, NativeFunctionError> {
    let args = vm.get_function_args(args_count);
    let from = args.first().copied().unwrap_or_else(|| Value::nil()).kind();
    let to = args.get(1).copied().unwrap_or_else(|| Value::nil()).kind();

    match (from, to) {
        (ValueKind::String(from_handle), ValueKind::String(to_handle)) => {
            let from_path = vm.alloc.strings.get(from_handle);
            let from_path = Path::new(from_path);
            let to_path = vm.alloc.strings.get(to_handle);
            let to_path = Path::new(to_path);

            Ok(Some(Value::boolean(fs::copy(from_path, to_path).is_ok())))
        }
        (ValueKind::String(_), _) => Err(NativeFunctionError::new(
            "Both source and destination must be strings.",
        )),
        _ => Err(NativeFunctionError::new("Source path must be a string.")),
    }
}

pub fn qang_fs_move(args_count: usize, vm: &mut Vm) -> Result<Option<Value>, NativeFunctionError> {
    let args = vm.get_function_args(args_count);
    let from = args.first().copied().unwrap_or_else(|| Value::nil()).kind();
    let to = args.get(1).copied().unwrap_or_else(|| Value::nil()).kind();

    match (from, to) {
        (ValueKind::String(from_handle), ValueKind::String(to_handle)) => {
            let from_path = vm.alloc.strings.get(from_handle);
            let from_path = Path::new(from_path);
            let to_path = vm.alloc.strings.get(to_handle);
            let to_path = Path::new(to_path);

            Ok(Some(Value::boolean(fs::rename(from_path, to_path).is_ok())))
        }
        (ValueKind::String(_), _) => Err(NativeFunctionError::new(
            "Both source and destination must be strings.",
        )),
        _ => Err(NativeFunctionError::new("Source path must be a string.")),
    }
}

impl Vm {
    pub(crate) fn with_native_filesystem(&mut self) -> &mut Self {
        self.add_native_function("_path_get_os_seperator", 0, qang_path_get_os_seperator)
            .add_native_function("_path_exists", 1, qang_path_exists)
            .add_native_function("_path_is_file", 1, qang_path_is_file)
            .add_native_function("_path_is_dir", 1, qang_path_is_dir)
            .add_native_function("_path_is_absolute", 1, qang_path_is_absolute)
            .add_native_function("_path_is_relative", 1, qang_path_is_relative)
            .add_native_function("_fs_read_file", 1, qang_fs_read_file)
            .add_native_function("_fs_read_chunk", 3, qang_fs_read_chunk)
            .add_native_function("_fs_write_file", 2, qang_fs_write_file)
            .add_native_function("_fs_append_file", 2, qang_fs_append_file)
            .add_native_function("_fs_remove_file", 1, qang_fs_remove_file)
            .add_native_function("_fs_list", 1, qang_fs_list)
            .add_native_function("_fs_create_dir", 1, qang_fs_create_dir)
            .add_native_function("_fs_create_dirs", 1, qang_fs_create_dirs)
            .add_native_function("_fs_remove_dir", 1, qang_fs_remove_dir)
            .add_native_function("_fs_remove_all", 1, qang_fs_remove_all)
            .add_native_function("_fs_get_file_size", 1, qang_fs_get_file_size)
            .add_native_function(
                "_fs_get_file_modified_time",
                1,
                qang_fs_get_file_modified_time,
            )
            .add_native_function("_fs_move", 2, qang_fs_move)
            .add_native_function("_fs_copy_file", 2, qang_fs_copy_file)
    }
}