Showing the changes to src/vm/sys/Sysmod.cpp (1 of 3 files). Show all
Unified · Split
| @@ -209,6 +209,29 @@ namespace · 208 unmodified lines | ||
| 209 | 209 | vm_fd_t *fd_stdout = nullptr; |
| 210 | 210 | vm_fd_t *fd_stderr = nullptr; |
| 211 | 211 | |
| 212 | + word_t printf_to_fd( | |
| 213 | + disvm::vm_t &vm, | |
| 214 | + vm_fd_t &fd, | |
| 215 | + const disvm::runtime::vm_string_t &msg_fmt, | |
| 216 | + disvm::runtime::byte_t *msg_args, | |
| 217 | + disvm::runtime::pointer_t base) | |
| 218 | + { | |
| 219 | + auto static_buffer = std::array<char, 1024>{}; | |
| 220 | + auto written = sys::printf_to_buffer(vm, msg_fmt, msg_args, base, static_buffer.size(), static_buffer.data()); | |
| 221 | + if (written >= 0) | |
| 222 | + { | |
| 223 | + fd.write(vm, written, static_buffer.data()); | |
| 224 | + } | |
| 225 | + else | |
| 226 | + { | |
| 227 | + auto dynamic_buffer = std::vector<char>(static_buffer.size() * 2); | |
| 228 | + written = sys::printf_to_dynamic_buffer(vm, msg_fmt, msg_args, base, dynamic_buffer); | |
| 229 | + fd.write(vm, written, dynamic_buffer.data()); | |
| 230 | + } | |
| 231 | + | |
| 232 | + return written; | |
| 233 | + } | |
| 234 | + | |
| 212 | 235 | std::ios::openmode convert_to_openmode(const word_t mode) |
| 213 | 236 | { |
| 214 | 237 | auto om = std::ios::openmode{}; |
| @@ -422,6 +445,7 @@ Sys_create(vm_registers_t &r, vm_t &vm) · 207 unmodified lines | ||
| 422 | 445 | } |
| 423 | 446 | |
| 424 | 447 | *fp.ret = fd_alloc->get_allocation(); |
| 448 | + fd_alloc->add_ref(); | |
| 425 | 449 | } |
| 426 | 450 | |
| 427 | 451 | void |
| @@ -492,8 +516,16 @@ Sys_file2chan(vm_registers_t &r, vm_t &vm) · 64 unmodified lines | ||
| 492 | 516 | void |
| 493 | 517 | Sys_fprint(vm_registers_t &r, vm_t &vm) |
| 494 | 518 | { |
| 519 | + auto fp_base = r.stack.peek_frame()->base(); | |
| 495 | 520 | auto &fp = r.stack.peek_frame()->base<F_Sys_fprint>(); |
| 496 | − throw vm_system_exception{ "Instruction not implemented" }; | |
| 521 | + auto fd_alloc = vm_alloc_t::from_allocation(fp.fd); | |
| 522 | + assert(fd_alloc->alloc_type == T_FD); | |
| 523 | + auto fd = fd_alloc->get_allocation<vm_fd_t>(); | |
| 524 | + auto str = vm_alloc_t::from_allocation<vm_string_t>(fp.s); | |
| 525 | + if (str == nullptr) | |
| 526 | + throw dereference_nil{ "Print string to FD" }; | |
| 527 | + | |
| 528 | + *fp.ret = printf_to_fd(vm, *fd, *str, &fp.vargs, fp_base); | |
| 497 | 529 | } |
| 498 | 530 | |
| 499 | 531 | void |
| @@ -582,6 +614,7 @@ Sys_open(vm_registers_t &r, vm_t &vm) · 82 unmodified lines | ||
| 582 | 614 | } |
| 583 | 615 | |
| 584 | 616 | *fp.ret = fd_alloc->get_allocation(); |
| 617 | + fd_alloc->add_ref(); | |
| 585 | 618 | } |
| 586 | 619 | |
| 587 | 620 | void |
| @@ -611,25 +644,10 @@ Sys_print(vm_registers_t &r, vm_t &vm) · 23 unmodified lines | ||
| 611 | 644 | auto fp_base = r.stack.peek_frame()->base(); |
| 612 | 645 | auto &fp = r.stack.peek_frame()->base<F_Sys_print>(); |
| 613 | 646 | auto str = vm_alloc_t::from_allocation<vm_string_t>(fp.s); |
| 614 | − | |
| 615 | 647 | if (str == nullptr) |
| 616 | 648 | throw dereference_nil{ "Print string" }; |
| 617 | 649 | |
| 618 | − auto static_buffer = std::array<char, 1024>{}; | |
| 619 | − auto msg_args = &fp.vargs; | |
| 620 | − auto written = sys::printf_to_buffer(vm, *str, msg_args, fp_base, static_buffer.size(), static_buffer.data()); | |
| 621 | − if (written >= 0) | |
| 622 | − { | |
| 623 | − fd_stdout->write(vm, written, static_buffer.data()); | |
| 624 | − } | |
| 625 | − else | |
| 626 | − { | |
| 627 | − auto dynamic_buffer = std::vector<char>(static_buffer.size() * 2); | |
| 628 | − written = sys::printf_to_dynamic_buffer(vm, *str, msg_args, fp_base, dynamic_buffer); | |
| 629 | − fd_stdout->write(vm, written, dynamic_buffer.data()); | |
| 630 | − } | |
| 631 | − | |
| 632 | − *fp.ret = written; | |
| 650 | + *fp.ret = printf_to_fd(vm, *fd_stdout, *str, &fp.vargs, fp_base); | |
| 633 | 651 | } |
| 634 | 652 | |
| 635 | 653 | void |
| @@ -680,7 +698,13 @@ void · 44 unmodified lines | ||
| 680 | 698 | Sys_remove(vm_registers_t &r, vm_t &vm) |
| 681 | 699 | { |
| 682 | 700 | auto &fp = r.stack.peek_frame()->base<F_Sys_remove>(); |
| 683 | − throw vm_system_exception{ "Instruction not implemented" }; | |
| 701 | + auto str = vm_alloc_t::from_allocation<vm_string_t>(fp.s); | |
| 702 | + if (str == nullptr) | |
| 703 | + throw dereference_nil{ "Remove path" }; | |
| 704 | + | |
| 705 | + // [PAL] There are a lot of implementation details with this function. | |
| 706 | + // Consider adding a PAL function to optimize per platform. | |
| 707 | + *fp.ret = std::remove(str->str()); | |
| 684 | 708 | } |
| 685 | 709 | |
| 686 | 710 | void |
| @@ -715,8 +739,29 @@ Sys_sleep(vm_registers_t &r, vm_t &vm) · 28 unmodified lines | ||
| 715 | 739 | void |
| 716 | 740 | Sys_sprint(vm_registers_t &r, vm_t &vm) |
| 717 | 741 | { |
| 742 | + auto fp_base = r.stack.peek_frame()->base(); | |
| 718 | 743 | auto &fp = r.stack.peek_frame()->base<F_Sys_sprint>(); |
| 719 | − throw vm_system_exception{ "Instruction not implemented" }; | |
| 744 | + auto str = vm_alloc_t::from_allocation<vm_string_t>(fp.s); | |
| 745 | + if (str == nullptr) | |
| 746 | + throw dereference_nil{ "Print string to string" }; | |
| 747 | + | |
| 748 | + vm_string_t *new_string; | |
| 749 | + auto static_buffer = std::array<char, 128>{}; | |
| 750 | + auto msg_args = &fp.vargs; | |
| 751 | + auto written = sys::printf_to_buffer(vm, *str, msg_args, fp_base, static_buffer.size(), static_buffer.data()); | |
| 752 | + if (written >= 0) | |
| 753 | + { | |
| 754 | + new_string = new vm_string_t{ static_cast<std::size_t>(written), reinterpret_cast<uint8_t *>(static_buffer.data()) }; | |
| 755 | + } | |
| 756 | + else | |
| 757 | + { | |
| 758 | + auto dynamic_buffer = std::vector<char>(static_buffer.size() * 2); | |
| 759 | + written = sys::printf_to_dynamic_buffer(vm, *str, msg_args, fp_base, dynamic_buffer); | |
| 760 | + new_string = new vm_string_t{ static_cast<std::size_t>(written), reinterpret_cast<uint8_t *>(dynamic_buffer.data()) }; | |
| 761 | + } | |
| 762 | + | |
| 763 | + assert(new_string != nullptr); | |
| 764 | + *fp.ret = new_string->get_allocation(); | |
| 720 | 765 | } |
| 721 | 766 | |
| 722 | 767 | void |