🐛 fix bug that gc cannot mark values in coroutine & add maketimer_sim.nas

update props_sim.nas

update auto_crash.nas
This commit is contained in:
ValKmjolnir 2022-05-23 20:23:20 +08:00
parent ebfacd9197
commit 190014a3e5
7 changed files with 147 additions and 39 deletions

View File

@ -514,15 +514,15 @@ void nasal_gc::mark()
{ {
std::queue<nasal_ref> bfs; std::queue<nasal_ref> bfs;
// this make sure values on main stack can be scanned // scan coroutine process stack when coroutine ptr is not null
if(!coroutine) // scan main process stack when coroutine ptr is null
{ // this scan process must execute because when running coroutine,
for(nasal_ref* i=stack;i<=top;++i) // the nasal_co related to it will not update it's context until the coroutine suspends or exits.
bfs.push(*i); for(nasal_ref* i=stack;i<=top;++i)
bfs.push(funcr); bfs.push(*i);
bfs.push(upvalr); bfs.push(funcr);
} bfs.push(upvalr);
else if(coroutine) // scan main process stack
{ {
for(nasal_ref* i=main_ctx.stack;i<=main_ctx.top;++i) for(nasal_ref* i=main_ctx.stack;i<=main_ctx.top;++i)
bfs.push(*i); bfs.push(*i);

View File

@ -1,5 +1,7 @@
# Road check and auto pilot by ValKmjolnir # Road check and auto pilot by ValKmjolnir
import("test/props_sim.nas"); import("./test/maketimer_sim.nas");
import("./test/props_sim.nas");
var dt=0.01; var dt=0.01;
var intergral=0; var intergral=0;
var derivative=0; var derivative=0;
@ -67,6 +69,12 @@ var road_check_func = func(){
props.getNode("/", 1).setValue("/controls/flight/rudder",Kp*error*error+Ki*intergral+Kd*derivative); props.getNode("/", 1).setValue("/controls/flight/rudder",Kp*error*error+Ki*intergral+Kd*derivative);
else else
props.getNode("/", 1).setValue("/controls/flight/rudder",0); props.getNode("/", 1).setValue("/controls/flight/rudder",0);
# for simulation test, in fg these three lines are deleted
println(" rudder :",props.getNode("/controls/flight/rudder",1).getValue());
println(" dt :",dt,'\tintergral :',intergral,'\tderivative :',derivative);
println(" prev-err :",previous_error,'\terror :',error);
previous_error=error; previous_error=error;
} }
}; };
@ -77,11 +85,17 @@ var toggle_auto_pilot = func(){
{ {
intergral=0; intergral=0;
road_check_timer.start(); road_check_timer.start();
props.getNode("/sim/messages/copilot",1).setValue("ze dong sheng teaan see tong yee tse yung. Auto Sheng Teaan System Activated!"); props.getNode("/sim/messages/copilot",1).setValue('/',"ze dong sheng teaan see tong yee tse yung. Auto Sheng Teaan System Activated!");
} }
else else
{ {
road_check_timer.stop(); road_check_timer.stop();
props.getNode("/sim/messages/copilot",1).setValue("ze dong sheng teaan see tong yee guan bee. Auto Sheng Teaan System is off."); props.getNode("/sim/messages/copilot",1).setValue('/',"ze dong sheng teaan see tong yee guan bee. Auto Sheng Teaan System is off.");
} }
} }
# this is used to simulate the running process in fg
# when using in fg, delete these lines below
toggle_auto_pilot();
road_check_timer.restart(1);
simulation();

View File

@ -48,6 +48,7 @@ var testfile=[
"test/lexer.nas ", "test/lexer.nas ",
"test/life.nas ", "test/life.nas ",
"test/loop.nas ", "test/loop.nas ",
"test/maketimer_sim.nas",
"test/mandel.nas ", "test/mandel.nas ",
"test/mandelbrot.nas ", "test/mandelbrot.nas ",
"test/md5.nas ", "test/md5.nas ",

87
test/maketimer_sim.nas Normal file
View File

@ -0,0 +1,87 @@
var process=nil;
var task={};
var event={};
var add_event=func(name,interval,function){
event[name]=coroutine.create(func{
unix.sleep(interval);
println("[event] ",name);
function();
});
}
var add_task=func(name,interval,function){
task[name]=coroutine.create(func{
while(1){
unix.sleep(interval);
println("[task] ",name);
function();
coroutine.yield();
}
});
}
var remove_task=func(name){
if(contains(task,name))
delete(task,name);
}
var remove_event=func(name){
if(contains(event,name))
delete(event,name);
}
var maketimer=func(interval,function){
var name="nasal-timer-";
var res={
start:func{
if(me.isRunning)
return;
me.isRunning=1;
if(me.singleShot){
add_event(name,interval,function);
}else{
add_task(name,interval,function);
}
},
stop:func{
if(me.isRunning){
remove_task(name);
me.isRunning=0;
}
},
restart:func(itv){
interval=itv;
me.stop();
me.start();
},
singleShot:0,
isRunning:0,
simulatedTime:0
};
name~=id(res);
return res;
}
var simulation=func(){
var running=1;
while(running){
running=0;
foreach(var i;keys(task)){
if(coroutine.resume(task[i])!=nil){
running=1;
}else{
remove_task(i);
}
}
foreach(var i;keys(event)){
if(coroutine.resume(event[i])!=nil){
running=1;
}else{
remove_event(i);
}
}
}
}

View File

@ -79,6 +79,7 @@ var filechecksum=func(){
"./test/lexer.nas ", "./test/lexer.nas ",
"./test/life.nas ", "./test/life.nas ",
"./test/loop.nas ", "./test/loop.nas ",
"./test/maketimer_sim.nas",
"./test/mandel.nas ", "./test/mandel.nas ",
"./test/mandelbrot.nas ", "./test/mandelbrot.nas ",
"./test/md5.nas ", "./test/md5.nas ",

View File

@ -1,25 +1,7 @@
var geodinfo=func(lat,lon){ var geodinfo=func(lat,lon){
return {}; return [nil,{
} names:["Road","Freeway"]
var maketimer=func(interval,function){ }];
return {
isRunning:0,
start:func(){
me.isRunning=1;
while(1){
unix.sleep(interval);
function();
}
},
stop:func(){
me.isRunning=0;
},
restart:func(interval){
},
singleShot:0,
simulatedTime:0
};
} }
var props= var props=
@ -175,5 +157,20 @@ props.getNode('/test/in',0).setValue('/','true');
props.getNode('/test/in',1).setValue('/','false'); props.getNode('/test/in',1).setValue('/','false');
props.getNode('/test/in',2).setValue('/','welcome aboard,need help? use help->tutorial'); props.getNode('/test/in',2).setValue('/','welcome aboard,need help? use help->tutorial');
props.getNode('/test/in',3).setValue('/',2147483648); props.getNode('/test/in',3).setValue('/',2147483648);
props.getNode("/sim",1).addChild("messages");
props.getNode("/sim/messages",1).addChild("copilot");
props.getNode("/position",1).addChild("latitude-deg");
props.getNode("/position",1).addChild("longitude-deg");
props.getNode("/orientation",1).addChild("heading-deg");
props.getNode("/controls",1).addChild("flight");
props.getNode("/controls/flight",1).addChild("rudder");
props.getNode("/sim/messages/copilot",1).setValue('/',"nothing");
props.getNode("/position/latitude-deg",1).setValue('/',90);
props.getNode("/position/longitude-deg",1).setValue('/',90);
props.getNode("/orientation/heading-deg",1).setValue('/',90);
props.getNode("/controls/flight/rudder",1).setValue('/',0.114);
props.globals.debug(); props.globals.debug();
println(props.getNode('/test/in',3).getPath());
println("-----------------------------------");

View File

@ -165,8 +165,7 @@ var game=func(x,y){
gameover=2; gameover=2;
}, },
move:func(c){ move:func(c){
if(c=='w' or c=='a' or c=='s' or c=='d') move=c;
move=c;
}, },
gameover:func(){ gameover:func(){
return gameover; return gameover;
@ -174,6 +173,15 @@ var game=func(x,y){
} }
} }
var co=coroutine.create(func(){
var t=maketimestamp();
while(1){
t.stamp();
while(t.elapsedMSec()<20);
coroutine.yield();
}
});
var main=func(){ var main=func(){
if(os.platform()=="windows") if(os.platform()=="windows")
system("chcp 65001"); system("chcp 65001");
@ -184,7 +192,7 @@ var main=func(){
print("\rpress any key to start..."); print("\rpress any key to start...");
libkey.getch(); libkey.getch();
print("\r \r"); print("\r \r");
var counter=12; var counter=20;
while(1){ while(1){
var ch=libkey.nonblock(); var ch=libkey.nonblock();
if(ch!=nil){ if(ch!=nil){
@ -197,15 +205,15 @@ var main=func(){
} }
g.move(chr(ch)); g.move(chr(ch));
} }
unix.sleep(0.02);
counter-=1; counter-=1;
if(counter==0){ if(!counter){
counter=20; counter=20;
g.next(); g.next();
if(g.gameover()) if(g.gameover())
break; break;
g.print(); g.print();
} }
coroutine.resume(co);
} }
println(g.gameover()<=1?"game over.":"you win!"); println(g.gameover()<=1?"game over.":"you win!");