Skip to content

Commit ae8dc3a

Browse files
committed
feat(builtin): disassemble can show a file bytecode
1 parent ea7364b commit ae8dc3a

File tree

6 files changed

+35
-6
lines changed

6 files changed

+35
-6
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
### Changed
1414
- quotes are added around strings in type errors
15+
- `disassemble` can show a file bytecode
1516

1617
### Removed
1718

src/arkreactor/Builtins/Bytecode.cpp

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <Ark/Compiler/BytecodeReader.hpp>
44
#include <Ark/VM/VM.hpp>
55

6+
#include <Ark/Utils/Files.hpp>
67
#include <Ark/VM/DefaultValues.hpp>
78
#include <Ark/TypeChecker.hpp>
89

@@ -21,15 +22,28 @@ namespace Ark::internal::Builtins::Bytecode
2122
// cppcheck-suppress constParameterReference
2223
Value disassemble(std::vector<Value>& n, VM* vm)
2324
{
24-
if (!types::check(n, ValueType::PageAddr))
25+
if (!types::check(n, ValueType::PageAddr) && !types::check(n, ValueType::String))
2526
throw types::TypeCheckingError(
2627
"disassemble",
27-
{ { types::Contract { { types::Typedef("f", ValueType::PageAddr) } } } },
28+
{ { types::Contract { { types::Typedef("f", ValueType::PageAddr) } },
29+
types::Contract { { types::Typedef("filename", ValueType::String) } } } },
2830
n);
2931

3032
BytecodeReader bcr;
31-
bcr.feed(vm->bytecode());
32-
bcr.display(BytecodeSegment::Code, std::nullopt, std::nullopt, n[0].pageAddr());
33+
if (n[0].valueType() == ValueType::PageAddr)
34+
{
35+
bcr.feed(vm->bytecode());
36+
bcr.display(BytecodeSegment::Code, std::nullopt, std::nullopt, n[0].pageAddr());
37+
}
38+
else if (n[0].valueType() == ValueType::String)
39+
{
40+
const std::string filename = n[0].string();
41+
if (!Utils::fileExists(filename))
42+
throw Error(fmt::format("`disassemble': can not read file {}", filename));
43+
44+
bcr.feed(filename);
45+
bcr.display();
46+
}
3347

3448
return Nil;
3549
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
(disassemble "this_file_should_not_exist.1234")
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
`disassemble': can not read file this_file_should_not_exist.1234
2+
3+
In file tests/unittests/resources/DiagnosticsSuite/typeChecking/disassemble_not_existing_file.ark:1
4+
1 | (disassemble "this_file_should_not_exist.1234")
5+
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6+
2 |

tests/unittests/resources/DiagnosticsSuite/typeChecking/disassemble_num.expected

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,13 @@ Signature
66
Arguments
77
→ `f' (expected Function), got 1 (Number)
88

9+
Alternative 2:
10+
Signature
11+
↳ (disassemble filename)
12+
Arguments
13+
→ `filename' (expected String), got 1 (Number)
14+
915
In file tests/unittests/resources/DiagnosticsSuite/typeChecking/disassemble_num.ark:1
1016
1 | (disassemble 1)
1117
| ^~~~~~~~~~~~~~
12-
2 |
18+
2 |

tests/unittests/resources/LangSuite/builtins-tests.ark

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,5 @@
2424
(test:expect (and (<= r 2147483647) (>= r -2147483648))) })
2525

2626
(test:case "disassemble" {
27-
(disassemble foo) }) })
27+
(disassemble foo)
28+
(disassemble "tests/unittests/resources/DiagnosticsSuite/runtime/__arkscript__/assert.arkc") }) })

0 commit comments

Comments
 (0)