少し前に C# で C インタープリターを作成しましたが、現在は Javascript への変換を開始しています。jsにスリープ機能がないことに気付くまで、すべてがうまくいっていました。私のインタープリターは再帰パーサーを使用しており、いくつかの関数が深くネストされている間、ユーザー入力のために一時停止します (C# では、2 番目のスレッドで waithandle を使用しました)。setInterval と setTimeout を見てきましたが、非同期/ノンブロッキングです。もちろん、ビジーウェイトは問題外であり、SOで見つけたtimed_queueの実装を見ましたが、運がありませんでした。メイン ウィンドウと Webworker の両方でパーサーを試しました。jQueryを使用しています。私はjsの経験が限られており、追求するアイデアを探しています. 私は継続渡しのスタイルや譲歩についてほとんど知りません。以下は、制御スクリプトの一部を示すためにコードから少し切り取ったものです。アイデアをください。
var STATE = {
START: "START",
RUN: "RUN", //take continuous steps at waitTime delay
STEP: "STEP", //take 1 step
PAUSE: "PAUSE",//wait for next step command
STOP: "STOP",
ERROR: "ERROR"
}
var state = state.STOP;
function parsing_process() //long process we may want to pause or wait in
{
while(token !== end_of_file)//
{
//do lots of stuff - much of it recursive
//the call to getNextToken will be encountered a lot in the recursion
getNextToken();
if (state === STATE.STOP)
break;
}
}
function getNextToken()
{
//retrieve next token from lexer array
if (token === end_of_line)
{
//tell the gui to highlight the current line
if (state === STATE.STOP)
return;
if (state === STATE.STEP)//wait for next step
{
//mimick wait for user input by using annoying alert
alert("click me to continue")
}
if (state === STATE.RUN) {
//a delay here - set by a slider in the window
//a busy wait haults processing of the window
}
}
}
task.js を使用して Firefox で動作するようにしました
<html>
<head>
<title>task.js examples: sleep</title>
<script type="application/javascript" src="task.js"></script>
</head>
<body>
Only works in FIREFOX
<button onclick="step()">Step</button>
<button onclick="run()">Run</button>
<button onclick="stop()">Stop</button>
<pre style="border: solid 1px black; width: 300px; height: 200px;" id="out">
</pre>
<script type="application/javascript;version=1.8">
function start() {
process();
}
function step() {
if (state === STATE.STOP)
start();
state = STATE.STEP;
}
function run() {
if (state === STATE.STOP)
start();
state = STATE.RUN;
}
function stop() {
state = STATE.STOP;
}
var STATE = {
START: "START",
RUN: "RUN", //take continuous steps at sleepTime delay
STEP: "STEP", //take 1 step
PAUSE: "PAUSE",//wait for next step command
STOP: "STOP",
ERROR: "ERROR"
}
var state = STATE.STOP;
var sleepTime = 500;
function process() {
var { spawn, choose, sleep } = task;
var out = document.getElementById("out");
var i=0;
out.innerHTML = "i="+i;
var sp = spawn(function() {
while(state !== STATE.STOP)
{
i++;
out.innerHTML = "i="+i;
if (state === STATE.RUN)
{
yield sleep(sleepTime);
}
if (state === STATE.STEP)
state = STATE.PAUSE;
while (state===STATE.PAUSE)
{
yield;
}
}
});
}
</script>
</body>
</html>
約束について何かを知っている人が私にいくつかの手がかりを与えることができれば幸いです. 私のアプリケーションは消費者向けのものではありませんが、Firefox 以外で実行できればいいのですが