Skip to content

Commit

Permalink
Merge pull request #51 from ValKmjolnir/develop
Browse files Browse the repository at this point in the history
🐛 fix bug in subprocess and dylib
  • Loading branch information
ValKmjolnir committed Jun 19, 2024
2 parents 78dd535 + 858ffdc commit 1c537f0
Show file tree
Hide file tree
Showing 28 changed files with 486 additions and 286 deletions.
6 changes: 3 additions & 3 deletions doc/tutorial.md
Original file line number Diff line number Diff line change
Expand Up @@ -639,11 +639,11 @@ Windows(`.dll`):
`g++ -shared -o libfib.dll fib.o`
Then we write a test nasal file to run this fib function, using `os.platform()` we could write a cross-platform program:
Then we write a test nasal file to run this fib function:
```javascript
use std.dylib;
var dlhandle = dylib.dlopen("libfib."~(os.platform()=="windows"?"dll":"so"));
var dlhandle = dylib.dlopen("libfib");
var fib = dlhandle.fib;
for(var i = 1; i<30; i += 1)
println(dylib.dlcall(fib, i));
Expand All @@ -660,7 +660,7 @@ dylib.dlclose(dlhandle.lib);
```javascript
use std.dylib;
var dlhandle = dylib.dlopen("libfib."~(os.platform()=="windows"?"dll":"so"));
var dlhandle = dylib.dlopen("libfib");
var fib = dlhandle.fib;
var invoke = dylib.limitcall(1); # this means the called function has only one parameter
for(var i = 1; i<30; i += 1)
Expand Down
7 changes: 3 additions & 4 deletions doc/tutorial_zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -618,12 +618,11 @@ Windows(`.dll`):
`g++ -shared -o libfib.dll fib.o`
好了,那么我们可以写一个测试用的nasal代码来运行这个斐波那契函数了。
下面例子中`os.platform()`是用来检测当前运行的系统环境的,这样可以实现跨平台:
好了,那么我们可以写一个测试用的nasal代码来运行这个斐波那契函数了:
```javascript
use std.dylib;
var dlhandle = dylib.dlopen("libfib."~(os.platform()=="windows"?"dll":"so"));
var dlhandle = dylib.dlopen("libfib");
var fib = dlhandle.fib;
for(var i = 1; i<30; i += 1)
println(dylib.dlcall(fib, i));
Expand All @@ -640,7 +639,7 @@ dylib.dlclose(dlhandle.lib);
```javascript
use std.dylib;
var dlhandle = dylib.dlopen("libfib."~(os.platform()=="windows"?"dll":"so"));
var dlhandle = dylib.dlopen("libfib");
var fib = dlhandle.fib;
var invoke = dylib.limitcall(1); # this means the called function has only one parameter
for(var i = 1; i<30; i += 1)
Expand Down
2 changes: 2 additions & 0 deletions makefile
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,8 @@ build/dylib_lib.o: \
src/nasal.h\
src/nasal_type.h\
src/nasal_gc.h\
src/util/util.h\
src/util/fs.h\
src/natives/dylib_lib.h src/natives/dylib_lib.cpp | build
$(CXX) $(CXXFLAGS) src/natives/dylib_lib.cpp -o build/dylib_lib.o

Expand Down
2 changes: 1 addition & 1 deletion module/libfib.nas
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std.dylib;
use std.os;

var _dl = dylib.dlopen("libfib."~(os.platform()=="windows"?"dll":"so"));
var _dl = dylib.dlopen("libfib");

var _fib = _dl.fib;

Expand Down
2 changes: 1 addition & 1 deletion module/libkey.nas
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ var (
getch,
nonblock
) = func {
var lib = dylib.dlopen("libkey"~(os.platform()=="windows"? ".dll":".so"));
var lib = dylib.dlopen("libkey");
var kb = lib.nas_kbhit;
var gt = lib.nas_getch;
var nb = lib.nas_noblock;
Expand Down
2 changes: 1 addition & 1 deletion module/libmat.nas
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std.dylib;
use std.os;

var _dl = dylib.dlopen("libmat."~(os.platform()=="windows"?"dll":"so"));
var _dl = dylib.dlopen("libmat");

var _vec2 = _dl.nas_vec2;

Expand Down
2 changes: 1 addition & 1 deletion module/libnasock.nas
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std.dylib;
use std.os;

var socket = func() {
var lib = dylib.dlopen("libnasock"~(os.platform()=="windows"? ".dll":".so"));
var lib = dylib.dlopen("libnasock");

var sock = lib.nas_socket;
var closesocket = lib.nas_closesocket;
Expand Down
1 change: 1 addition & 0 deletions src/nasal_codegen.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ class codegen {
"__chdir", "__environ", "__getcwd", "__getenv",
// subprocess
"__subprocess_create",
"__subprocess_active",
"__subprocess_terminate"
};

Expand Down
2 changes: 1 addition & 1 deletion src/nasal_dbg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ void operand_line_counter::dump_operand_count() const {
break;
}
std::clog << " ";
std::clog << operand_name_table.at(static_cast<op_code_type>(i.first));
std::clog << operand_name_table.at(static_cast<opcode_type>(i.first));
std::clog << " : " << i.second << " (" << rate << "%)\n";
}
std::clog << " total : " << total << '\n';
Expand Down
2 changes: 1 addition & 1 deletion src/nasal_dbg.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace nasal {
// and show them before each line of the source file
class operand_line_counter {
private:
static const usize operand_size = op_code_type::op_ret + 1;
static const usize operand_size = opcode_type::op_ret + 1;
u64 operand_counter[operand_size];
std::vector<std::string> file_name_list;
std::vector<std::vector<u64>> file_line_counter;
Expand Down
22 changes: 13 additions & 9 deletions src/nasal_gc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -315,16 +315,20 @@ void gc::info() const {
indent_string += "";
}
const auto first_line = "" + indent_string + "" +
indent_string + "" +
indent_string + "" +
indent_string + "" +
indent_string + "" +
indent_string + "";
const auto second_line = "" + indent_string + "" +
indent_string + "" +
indent_string + "" +
indent_string + "";
const auto mid_line = "" + indent_string + "" +
indent_string + "" +
indent_string + "" +
indent_string + "" +
indent_string + "" +
indent_string + "";
const auto another_mid_line = "" + indent_string + "" +
indent_string + "" +
indent_string + "" +
indent_string + "" +
indent_string + "" +
indent_string + "";
const auto last_line = "" + indent_string + "" +
indent_string + "" +
Expand All @@ -336,7 +340,7 @@ void gc::info() const {
std::clog << "" << left << setw(indent) << setfill(' ') << "gc count";
std::clog << "" << left << setw(indent) << setfill(' ') << "alloc count";
std::clog << "" << left << setw(indent) << setfill(' ') << "memory size";
std::clog << "\n" << mid_line << "\n";
std::clog << "\n" << second_line << "\n";

double total = 0;
for(u8 i = 0; i<gc_type_size; ++i) {
Expand All @@ -355,8 +359,8 @@ void gc::info() const {
const auto den = std::chrono::high_resolution_clock::duration::period::den;
std::clog << "" << left << setw(indent) << setfill(' ') << "detail";
std::clog << "" << left << setw(indent) << setfill(' ') << "time spend";
std::clog << " " << left << setw(indent) << setfill('x') << "x";
std::clog << " " << left << setw(indent) << setfill('x') << "x";
std::clog << " " << left << setw(indent) << setfill(' ') << " ";
std::clog << " " << left << setw(indent) << setfill(' ') << " ";
std::clog << "\n" << another_mid_line << "\n";

const auto gc_time = worktime*1.0/den*1000;
Expand Down
18 changes: 11 additions & 7 deletions src/nasal_opcode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,12 @@ void codestream::dump(std::ostream& out) const {

// dump immediate number(hex format)
for(i32 i = 64-8; i>=0; i -= 8) {
out << hex << setw(2) << setfill('0') << ((num>>i)&0xff) << dec << " ";
auto this_byte = ((num>>i)&0xff);
out << hex << setw(2) << setfill('0') << this_byte << dec << " ";
}

// dump operand name
out << " " << operand_name_table.at(static_cast<op_code_type>(op)) << " ";
out << " " << operand_name_table.at(static_cast<opcode_type>(op)) << " ";

switch(op) {
case op_addeq:
Expand All @@ -56,7 +57,7 @@ void codestream::dump(std::ostream& out) const {
break;
case op_lnkeqc:
out << hex << "0x" << num << dec;
out << " (" << util::rawstr(const_string[num], 16) << ")";
out << " (\"" << util::rawstr(const_string[num], 32) << "\")";
break;
case op_addecp:
case op_subecp:
Expand All @@ -67,7 +68,7 @@ void codestream::dump(std::ostream& out) const {
break;
case op_lnkecp:
out << hex << "0x" << num << dec;
out << " (" << util::rawstr(const_string[num], 16) << ") sp-1";
out << " (\"" << util::rawstr(const_string[num], 32) << "\") sp-1";
break;
case op_addc:
case op_subc:
Expand Down Expand Up @@ -100,8 +101,11 @@ void codestream::dump(std::ostream& out) const {
case op_loadl:
out << hex << "0x" << num << dec; break;
case op_callb:
out << hex << "0x" << num << dec << " ["
<< natives[num].name << "]"; break;
out << hex << "0x" << num << dec;
out << " <" << natives[num].name << "@0x";
out << hex << reinterpret_cast<u64>(natives[num].func) << dec;
out << ">";
break;
case op_upval:
case op_mupval:
case op_loadu:
Expand All @@ -116,7 +120,7 @@ void codestream::dump(std::ostream& out) const {
case op_deft:
case op_dyn:
out << hex << "0x" << num << dec;
out << " (" << util::rawstr(const_string[num], 16) << ")";
out << " (\"" << util::rawstr(const_string[num], 32) << "\")";
break;
default:
if (files) {
Expand Down
Loading

0 comments on commit 1c537f0

Please sign in to comment.