MIRROR: javascript for ๐Ÿœ's, a tiny runtime with big ambitions
1
fork

Configure Feed

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

add non async fs

+269 -1
+1 -1
meson.build
··· 67 67 build_date = run_command('date', '+%Y-%m-%d', check: true).stdout().strip() 68 68 69 69 version_conf = configuration_data() 70 - version_conf.set('ANT_VERSION', '0.0.6.32') 70 + version_conf.set('ANT_VERSION', '0.0.6.33') 71 71 version_conf.set('ANT_GIT_HASH', git_hash) 72 72 version_conf.set('ANT_BUILD_DATE', build_date) 73 73
+210
src/modules/fs.c
··· 7 7 #include <uthash.h> 8 8 #include <utarray.h> 9 9 #include <unistd.h> 10 + #include <errno.h> 10 11 #include "modules/fs.h" 11 12 #include "runtime.h" 12 13 ··· 284 285 } 285 286 } 286 287 288 + static jsval_t builtin_fs_readFileSync(struct js *js, jsval_t *args, int nargs) { 289 + if (nargs < 1) return js_mkerr(js, "readFileSync() requires a path argument"); 290 + 291 + if (js_type(args[0]) != JS_STR) return js_mkerr(js, "readFileSync() path must be a string"); 292 + 293 + size_t path_len; 294 + char *path = js_getstr(js, args[0], &path_len); 295 + if (!path) return js_mkerr(js, "Failed to get path string"); 296 + 297 + char *path_cstr = strndup(path, path_len); 298 + if (!path_cstr) return js_mkerr(js, "Out of memory"); 299 + 300 + FILE *file = fopen(path_cstr, "rb"); 301 + if (!file) { 302 + char err_msg[256]; 303 + snprintf(err_msg, sizeof(err_msg), "Failed to open file: %s", strerror(errno)); 304 + free(path_cstr); 305 + return js_mkerr(js, err_msg); 306 + } 307 + 308 + fseek(file, 0, SEEK_END); 309 + long file_size = ftell(file); 310 + fseek(file, 0, SEEK_SET); 311 + 312 + if (file_size < 0) { 313 + fclose(file); 314 + free(path_cstr); 315 + return js_mkerr(js, "Failed to get file size"); 316 + } 317 + 318 + char *data = malloc(file_size + 1); 319 + if (!data) { 320 + fclose(file); 321 + free(path_cstr); 322 + return js_mkerr(js, "Out of memory"); 323 + } 324 + 325 + size_t bytes_read = fread(data, 1, file_size, file); 326 + fclose(file); 327 + free(path_cstr); 328 + 329 + if (bytes_read != (size_t)file_size) { 330 + free(data); 331 + return js_mkerr(js, "Failed to read entire file"); 332 + } 333 + 334 + data[file_size] = '\0'; 335 + jsval_t result = js_mkstr(js, data, file_size); 336 + free(data); 337 + 338 + return result; 339 + } 340 + 287 341 static jsval_t builtin_fs_readFile(struct js *js, jsval_t *args, int nargs) { 288 342 if (nargs < 1) return js_mkerr(js, "readFile() requires a path argument"); 289 343 ··· 318 372 return req->promise; 319 373 } 320 374 375 + static jsval_t builtin_fs_writeFileSync(struct js *js, jsval_t *args, int nargs) { 376 + if (nargs < 2) return js_mkerr(js, "writeFileSync() requires path and data arguments"); 377 + 378 + if (js_type(args[0]) != JS_STR) return js_mkerr(js, "writeFileSync() path must be a string"); 379 + if (js_type(args[1]) != JS_STR) return js_mkerr(js, "writeFileSync() data must be a string"); 380 + 381 + size_t path_len, data_len; 382 + char *path = js_getstr(js, args[0], &path_len); 383 + char *data = js_getstr(js, args[1], &data_len); 384 + 385 + if (!path || !data) return js_mkerr(js, "Failed to get arguments"); 386 + 387 + char *path_cstr = strndup(path, path_len); 388 + if (!path_cstr) return js_mkerr(js, "Out of memory"); 389 + 390 + FILE *file = fopen(path_cstr, "wb"); 391 + if (!file) { 392 + char err_msg[256]; 393 + snprintf(err_msg, sizeof(err_msg), "Failed to open file: %s", strerror(errno)); 394 + free(path_cstr); 395 + return js_mkerr(js, err_msg); 396 + } 397 + 398 + size_t bytes_written = fwrite(data, 1, data_len, file); 399 + fclose(file); 400 + free(path_cstr); 401 + 402 + if (bytes_written != data_len) { 403 + return js_mkerr(js, "Failed to write entire file"); 404 + } 405 + 406 + return js_mkundef(); 407 + } 408 + 321 409 static jsval_t builtin_fs_writeFile(struct js *js, jsval_t *args, int nargs) { 322 410 if (nargs < 2) return js_mkerr(js, "writeFile() requires path and data arguments"); 323 411 ··· 365 453 return req->promise; 366 454 } 367 455 456 + static jsval_t builtin_fs_unlinkSync(struct js *js, jsval_t *args, int nargs) { 457 + if (nargs < 1) return js_mkerr(js, "unlinkSync() requires a path argument"); 458 + 459 + if (js_type(args[0]) != JS_STR) return js_mkerr(js, "unlinkSync() path must be a string"); 460 + 461 + size_t path_len; 462 + char *path = js_getstr(js, args[0], &path_len); 463 + if (!path) return js_mkerr(js, "Failed to get path string"); 464 + 465 + char *path_cstr = strndup(path, path_len); 466 + if (!path_cstr) return js_mkerr(js, "Out of memory"); 467 + 468 + int result = unlink(path_cstr); 469 + free(path_cstr); 470 + 471 + if (result != 0) { 472 + char err_msg[256]; 473 + snprintf(err_msg, sizeof(err_msg), "Failed to unlink file: %s", strerror(errno)); 474 + return js_mkerr(js, err_msg); 475 + } 476 + 477 + return js_mkundef(); 478 + } 479 + 368 480 static jsval_t builtin_fs_unlink(struct js *js, jsval_t *args, int nargs) { 369 481 if (nargs < 1) return js_mkerr(js, "unlink() requires a path argument"); 370 482 ··· 399 511 return req->promise; 400 512 } 401 513 514 + static jsval_t builtin_fs_mkdirSync(struct js *js, jsval_t *args, int nargs) { 515 + if (nargs < 1) return js_mkerr(js, "mkdirSync() requires a path argument"); 516 + 517 + if (js_type(args[0]) != JS_STR) return js_mkerr(js, "mkdirSync() path must be a string"); 518 + 519 + size_t path_len; 520 + char *path = js_getstr(js, args[0], &path_len); 521 + if (!path) return js_mkerr(js, "Failed to get path string"); 522 + 523 + int mode = 0755; 524 + if (nargs >= 2 && js_type(args[1]) == JS_NUM) { 525 + mode = (int)js_getnum(args[1]); 526 + } 527 + 528 + char *path_cstr = strndup(path, path_len); 529 + if (!path_cstr) return js_mkerr(js, "Out of memory"); 530 + 531 + #ifdef _WIN32 532 + int result = _mkdir(path_cstr); 533 + #else 534 + int result = mkdir(path_cstr, mode); 535 + #endif 536 + free(path_cstr); 537 + 538 + if (result != 0) { 539 + char err_msg[256]; 540 + snprintf(err_msg, sizeof(err_msg), "Failed to create directory: %s", strerror(errno)); 541 + return js_mkerr(js, err_msg); 542 + } 543 + 544 + return js_mkundef(); 545 + } 546 + 402 547 static jsval_t builtin_fs_mkdir(struct js *js, jsval_t *args, int nargs) { 403 548 if (nargs < 1) return js_mkerr(js, "mkdir() requires a path argument"); 404 549 ··· 438 583 return req->promise; 439 584 } 440 585 586 + static jsval_t builtin_fs_rmdirSync(struct js *js, jsval_t *args, int nargs) { 587 + if (nargs < 1) return js_mkerr(js, "rmdirSync() requires a path argument"); 588 + 589 + if (js_type(args[0]) != JS_STR) return js_mkerr(js, "rmdirSync() path must be a string"); 590 + 591 + size_t path_len; 592 + char *path = js_getstr(js, args[0], &path_len); 593 + if (!path) return js_mkerr(js, "Failed to get path string"); 594 + 595 + char *path_cstr = strndup(path, path_len); 596 + if (!path_cstr) return js_mkerr(js, "Out of memory"); 597 + 598 + #ifdef _WIN32 599 + int result = _rmdir(path_cstr); 600 + #else 601 + int result = rmdir(path_cstr); 602 + #endif 603 + free(path_cstr); 604 + 605 + if (result != 0) { 606 + char err_msg[256]; 607 + snprintf(err_msg, sizeof(err_msg), "Failed to remove directory: %s", strerror(errno)); 608 + return js_mkerr(js, err_msg); 609 + } 610 + 611 + return js_mkundef(); 612 + } 613 + 441 614 static jsval_t builtin_fs_rmdir(struct js *js, jsval_t *args, int nargs) { 442 615 if (nargs < 1) return js_mkerr(js, "rmdir() requires a path argument"); 443 616 ··· 472 645 return req->promise; 473 646 } 474 647 648 + static jsval_t builtin_fs_statSync(struct js *js, jsval_t *args, int nargs) { 649 + if (nargs < 1) return js_mkerr(js, "statSync() requires a path argument"); 650 + 651 + if (js_type(args[0]) != JS_STR) return js_mkerr(js, "statSync() path must be a string"); 652 + 653 + size_t path_len; 654 + char *path = js_getstr(js, args[0], &path_len); 655 + if (!path) return js_mkerr(js, "Failed to get path string"); 656 + 657 + char *path_cstr = strndup(path, path_len); 658 + if (!path_cstr) return js_mkerr(js, "Out of memory"); 659 + 660 + struct stat st; 661 + int result = stat(path_cstr, &st); 662 + free(path_cstr); 663 + 664 + if (result != 0) { 665 + char err_msg[256]; 666 + snprintf(err_msg, sizeof(err_msg), "Failed to stat file: %s", strerror(errno)); 667 + return js_mkerr(js, err_msg); 668 + } 669 + 670 + jsval_t stat_obj = js_mkobj(js); 671 + js_set(js, stat_obj, "size", js_mknum((double)st.st_size)); 672 + js_set(js, stat_obj, "mode", js_mknum((double)st.st_mode)); 673 + js_set(js, stat_obj, "isFile", S_ISREG(st.st_mode) ? js_mktrue() : js_mkfalse()); 674 + js_set(js, stat_obj, "isDirectory", S_ISDIR(st.st_mode) ? js_mktrue() : js_mkfalse()); 675 + 676 + return stat_obj; 677 + } 678 + 475 679 static jsval_t builtin_fs_stat(struct js *js, jsval_t *args, int nargs) { 476 680 if (nargs < 1) return js_mkerr(js, "stat() requires a path argument"); 477 681 ··· 510 714 jsval_t lib = js_mkobj(js); 511 715 512 716 js_set(js, lib, "readFile", js_mkfun(builtin_fs_readFile)); 717 + js_set(js, lib, "readFileSync", js_mkfun(builtin_fs_readFileSync)); 513 718 js_set(js, lib, "writeFile", js_mkfun(builtin_fs_writeFile)); 719 + js_set(js, lib, "writeFileSync", js_mkfun(builtin_fs_writeFileSync)); 514 720 js_set(js, lib, "unlink", js_mkfun(builtin_fs_unlink)); 721 + js_set(js, lib, "unlinkSync", js_mkfun(builtin_fs_unlinkSync)); 515 722 js_set(js, lib, "mkdir", js_mkfun(builtin_fs_mkdir)); 723 + js_set(js, lib, "mkdirSync", js_mkfun(builtin_fs_mkdirSync)); 516 724 js_set(js, lib, "rmdir", js_mkfun(builtin_fs_rmdir)); 725 + js_set(js, lib, "rmdirSync", js_mkfun(builtin_fs_rmdirSync)); 517 726 js_set(js, lib, "stat", js_mkfun(builtin_fs_stat)); 727 + js_set(js, lib, "statSync", js_mkfun(builtin_fs_statSync)); 518 728 519 729 return lib; 520 730 }
+58
tests/test_fs_sync.js
··· 1 + import { readFileSync, writeFileSync, unlinkSync, mkdirSync, rmdirSync, statSync } from 'ant:fs'; 2 + 3 + console.log('Testing ant:fs with synchronous operations...\n'); 4 + 5 + function testFs() { 6 + const testDir = 'tests/.fs_test_tmp_sync'; 7 + const testFile = testDir + '/sync_test.txt'; 8 + const testData = 'Hello from sync ant:fs!'; 9 + 10 + try { 11 + // Test mkdirSync 12 + console.log('=== Test: mkdirSync ==='); 13 + try { 14 + mkdirSync(testDir); 15 + console.log('โœ“ Directory created'); 16 + } catch (e) { 17 + console.log('Directory might exist, continuing...'); 18 + } 19 + 20 + // Test writeFileSync 21 + console.log('\n=== Test: writeFileSync ==='); 22 + writeFileSync(testFile, testData); 23 + console.log('โœ“ File written'); 24 + 25 + // Test readFileSync 26 + console.log('\n=== Test: readFileSync ==='); 27 + const content = readFileSync(testFile); 28 + console.log('โœ“ File read, length:', content.length); 29 + if (content === testData) { 30 + console.log('โœ“ Content matches!'); 31 + } else { 32 + console.log('โœ— Content mismatch!'); 33 + } 34 + 35 + // Test statSync 36 + console.log('\n=== Test: statSync ==='); 37 + const stats = statSync(testFile); 38 + console.log('โœ“ File stats:'); 39 + console.log(' Size:', stats.size); 40 + console.log(' Is file:', stats.isFile); 41 + console.log(' Is directory:', stats.isDirectory); 42 + 43 + // Cleanup 44 + console.log('\n=== Cleanup ==='); 45 + unlinkSync(testFile); 46 + console.log('โœ“ File deleted'); 47 + 48 + rmdirSync(testDir); 49 + console.log('โœ“ Directory deleted'); 50 + 51 + console.log('\nโœ“โœ“โœ“ All tests passed! โœ“โœ“โœ“'); 52 + } catch (error) { 53 + console.error('\nโœ— Test failed:', error); 54 + } 55 + } 56 + 57 + // Run the synchronous function 58 + testFs();