Showing the changes to src/vm/sys/Sysmod.cpp (1 of 17 files). Show all
Unified · Split
| @@ -166,26 +166,26 @@ namespace · 165 unmodified lines | |||
| 166 | return _fd_path; | 166 | return _fd_path; |
| 167 | } | 167 | } |
| 168 | 168 | ||
| 169 | word_t read(const word_t buffer_size_in_bytes, void *buffer) | 169 | word_t read(disvm::vm_t &vm, const word_t buffer_size_in_bytes, void *buffer) |
| 170 | { | 170 | { |
| 171 | if (_istream == nullptr) | 171 | if (_istream == nullptr) |
| 172 | throw vm_system_exception{ "File descriptor not open for read operation" }; | 172 | throw vm_system_exception{ "File descriptor not open for read operation" }; |
| 173 | 173 | ||
| 174 | _istream->read(static_cast<char *>(buffer), static_cast<const std::streamsize>(buffer_size_in_bytes)); | 174 | _istream->read(static_cast<char *>(buffer), static_cast<const std::streamsize>(buffer_size_in_bytes)); |
| 175 | if (_istream->bad()) | 175 | if (_istream->bad()) |
| 176 | throw vm_user_exception{ "Read operation error" }; | 176 | throw vm_syscall_exception{ vm, "Read operation error" }; |
| 177 | 177 | ||
| 178 | return static_cast<word_t>(_istream->gcount()); | 178 | return static_cast<word_t>(_istream->gcount()); |
| 179 | } | 179 | } |
| 180 | 180 | ||
| 181 | void write(const word_t buffer_size_in_bytes, void *buffer) | 181 | void write(disvm::vm_t &vm, const word_t buffer_size_in_bytes, void *buffer) |
| 182 | { | 182 | { |
| 183 | if (_ostream == nullptr) | 183 | if (_ostream == nullptr) |
| 184 | throw vm_system_exception{ "File descriptor not open for write operation" }; | 184 | throw vm_system_exception{ "File descriptor not open for write operation" }; |
| 185 | 185 | ||
| 186 | _ostream->write(static_cast<char *>(buffer), static_cast<const std::streamsize>(buffer_size_in_bytes)); | 186 | _ostream->write(static_cast<char *>(buffer), static_cast<const std::streamsize>(buffer_size_in_bytes)); |
| 187 | if (_ostream->bad()) | 187 | if (_ostream->bad()) |
| 188 | throw vm_user_exception{ "Write operation error" }; | 188 | throw vm_syscall_exception{ vm, "Write operation error" }; |
| 189 | } | 189 | } |
| 190 | 190 | ||
| 191 | big_t seek(const std::ios::seekdir start, const big_t offset) | 191 | big_t seek(const std::ios::seekdir start, const big_t offset) |
| @@ -470,7 +470,16 @@ void · 278 unmodified lines | |||
| 470 | Sys_fildes(vm_registers_t &r, vm_t &vm) | 470 | Sys_fildes(vm_registers_t &r, vm_t &vm) |
| 471 | { | 471 | { |
| 472 | auto &fp = r.stack.peek_frame()->base<F_Sys_fildes>(); | 472 | auto &fp = r.stack.peek_frame()->base<F_Sys_fildes>(); |
| 473 | throw vm_system_exception{ "Instruction not implemented" }; | 473 | |
| 474 | dec_ref_count_and_free(vm_alloc_t::from_allocation(*fp.ret)); | ||
| 475 | *fp.ret = nullptr; | ||
| 476 | |||
| 477 | auto fd = sys::fetch_fd_record(fp.fd); | ||
| 478 | if (fd != nullptr) | ||
| 479 | { | ||
| 480 | fd->add_ref(); | ||
| 481 | *fp.ret = fd->get_allocation(); | ||
| 482 | } | ||
| 474 | } | 483 | } |
| 475 | 484 | ||
| 476 | void | 485 | void |
| @@ -608,16 +617,16 @@ Sys_print(vm_registers_t &r, vm_t &vm) · 131 unmodified lines | |||
| 608 | 617 | ||
| 609 | auto static_buffer = std::array<char, 1024>{}; | 618 | auto static_buffer = std::array<char, 1024>{}; |
| 610 | auto msg_args = &fp.vargs; | 619 | auto msg_args = &fp.vargs; |
| 611 | auto written = sys::printf_to_buffer(*str, msg_args, fp_base, static_buffer.size(), static_buffer.data()); | 620 | auto written = sys::printf_to_buffer(vm, *str, msg_args, fp_base, static_buffer.size(), static_buffer.data()); |
| 612 | if (written >= 0) | 621 | if (written >= 0) |
| 613 | { | 622 | { |
| 614 | fd_stdout->write(written, static_buffer.data()); | 623 | fd_stdout->write(vm, written, static_buffer.data()); |
| 615 | } | 624 | } |
| 616 | else | 625 | else |
| 617 | { | 626 | { |
| 618 | auto dynamic_buffer = std::vector<char>(static_buffer.size() * 2); | 627 | auto dynamic_buffer = std::vector<char>(static_buffer.size() * 2); |
| 619 | written = sys::printf_to_dynamic_buffer(*str, msg_args, fp_base, dynamic_buffer); | 628 | written = sys::printf_to_dynamic_buffer(vm, *str, msg_args, fp_base, dynamic_buffer); |
| 620 | fd_stdout->write(written, dynamic_buffer.data()); | 629 | fd_stdout->write(vm, written, dynamic_buffer.data()); |
| 621 | } | 630 | } |
| 622 | 631 | ||
| 623 | *fp.ret = written; | 632 | *fp.ret = written; |
| @@ -657,7 +666,7 @@ Sys_read(vm_registers_t &r, vm_t &vm) · 33 unmodified lines | |||
| 657 | assert(fd_alloc->alloc_type == T_FD); | 666 | assert(fd_alloc->alloc_type == T_FD); |
| 658 | 667 | ||
| 659 | auto fd = fd_alloc->get_allocation<vm_fd_t>(); | 668 | auto fd = fd_alloc->get_allocation<vm_fd_t>(); |
| 660 | *fp.ret = fd->read(n, buffer->at(0)); | 669 | *fp.ret = fd->read(vm, n, buffer->at(0)); |
| 661 | } | 670 | } |
| 662 | 671 | ||
| 663 | void | 672 | void |
| @@ -729,19 +738,28 @@ namespace · 65 unmodified lines | |||
| 729 | bool is_delim(const runtime::rune_t c, const vm_string_t &delim) | 738 | bool is_delim(const runtime::rune_t c, const vm_string_t &delim) |
| 730 | { | 739 | { |
| 731 | const auto max = delim.get_length(); | 740 | const auto max = delim.get_length(); |
| 732 | assert(max > 0); | ||
| 733 | 741 | ||
| 734 | // Optimization for 3 or less delimiters | 742 | // Optimization for 3 or less delimiters |
| 735 | switch (max) | 743 | switch (max) |
| 736 | { | 744 | { |
| 737 | default: | 745 | default: |
| 738 | break; | 746 | break; |
| 739 | case 1: | ||
| 740 | return (delim.get_rune(0) == c); | ||
| 741 | case 2: | ||
| 742 | return (delim.get_rune(0) == c) || (delim.get_rune(1) == c); | ||
| 743 | case 3: | 747 | case 3: |
| 744 | return (delim.get_rune(0) == c) || (delim.get_rune(1) == c) || (delim.get_rune(2) == c); | 748 | if (delim.get_rune(2) != c) |
| 749 | { | ||
| 750 | case 2: | ||
| 751 | if (delim.get_rune(1) != c) | ||
| 752 | { | ||
| 753 | case 1: | ||
| 754 | if (delim.get_rune(0) != c) | ||
| 755 | { | ||
| 756 | case 0: | ||
| 757 | return false; | ||
| 758 | } | ||
| 759 | } | ||
| 760 | } | ||
| 761 | |||
| 762 | return true; | ||
| 745 | } | 763 | } |
| 746 | 764 | ||
| 747 | for (auto i = word_t{ 0 }; i < max; ++i) | 765 | for (auto i = word_t{ 0 }; i < max; ++i) |
| @@ -900,7 +918,7 @@ Sys_write(vm_registers_t &r, vm_t &vm) · 152 unmodified lines | |||
| 900 | assert(fd_alloc->alloc_type == T_FD); | 918 | assert(fd_alloc->alloc_type == T_FD); |
| 901 | 919 | ||
| 902 | auto fd = fd_alloc->get_allocation<vm_fd_t>(); | 920 | auto fd = fd_alloc->get_allocation<vm_fd_t>(); |
| 903 | fd->write(n, buffer->at(0)); | 921 | fd->write(vm, n, buffer->at(0)); |
| 904 | 922 | ||
| 905 | *fp.ret = n; | 923 | *fp.ret = n; |
| 906 | } | 924 | } |