Skip to content

Commit

Permalink
opslang: block delay
Browse files Browse the repository at this point in the history
  • Loading branch information
kobkaz committed Jan 26, 2024
1 parent ebe352f commit 95f2d7b
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 6 deletions.
12 changes: 9 additions & 3 deletions tmtc-c2a/devtools_frontend/src/components/CommandView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,7 @@ export const CommandView: React.FC = () => {
| {
success: true;
status: opslang.ControlStatus;
requestedDelay: number;
executionContext: opslang.ExecutionContext | undefined;
}
| { success: false; error: unknown };
Expand All @@ -455,8 +456,9 @@ export const CommandView: React.FC = () => {
);
const status = result.status;
const executionContext = result.execution_context;
const requestedDelay = result.requestedDelay;
result.free();
return { success: true, status, executionContext };
return { success: true, status, requestedDelay, executionContext };
} catch (error) {
return { success: false, error };
}
Expand Down Expand Up @@ -528,6 +530,12 @@ export const CommandView: React.FC = () => {
return [false, undefined];
}

const delayLength = Math.max(result.requestedDelay, 250);
const delay = new Promise((resolve) =>
setTimeout(resolve, delayLength),
);
await delay;

if (result.status === opslang.ControlStatus.Breaked) {
return [false, result.executionContext];
}
Expand All @@ -551,8 +559,6 @@ export const CommandView: React.FC = () => {
editor.setPosition(nextPosition);
editor.revealLine(lineno + 1);
}
const delay = new Promise((resolve) => setTimeout(resolve, 250));
await delay;
return [true, result.executionContext];
};

Expand Down
23 changes: 20 additions & 3 deletions tmtc-c2a/devtools_frontend/wasm-opslang/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -697,8 +697,15 @@ impl Runner {
args?,
)
.await?;
//TODO: apply delay
Ok(ExecutionResult::executed())

//TODO: 毎回評価せず、ブロックの最初のstatementを実行する前に評価する
let delay = if let Some(delay) = block_context.delay {
let delay = self.expr(delay)?.duration()?;
delay.num_milliseconds() as usize
} else {
0
};
Ok(ExecutionResult::executed().request_delay(delay))
}
Let(l) => {
let value = self.expr(&l.rhs)?;
Expand Down Expand Up @@ -756,6 +763,8 @@ pub struct ExecutionContext {
#[derive(Debug)]
pub struct ExecutionResult {
pub status: ControlStatus,
#[wasm_bindgen(js_name = "requestedDelay")]
pub requested_delay: usize,
execution_context: Option<ExecutionContext>,
}

Expand All @@ -771,23 +780,31 @@ impl ExecutionResult {
fn breaked() -> Self {
ExecutionResult {
status: ControlStatus::Breaked,
requested_delay: 0,
execution_context: None,
}
}

fn executed() -> Self {
ExecutionResult {
status: ControlStatus::Executed,
requested_delay: 0,
execution_context: None,
}
}

fn retry(execution_context: ExecutionContext) -> Self {
ExecutionResult {
status: ControlStatus::Retry,
requested_delay: 0,
execution_context: Some(execution_context),
}
}

fn request_delay(mut self, delay: usize) -> Self {
self.requested_delay = delay;
self
}
}

//TODO: reimplement this
Expand Down Expand Up @@ -888,7 +905,7 @@ impl ParsedCode {
initial_execution_time_ms: current_time_ms,
evaluated_durations: vec![],
});
let result = self
let mut result = self
.execute_line_(driver, context, stop_on_break, line_num, current_time_ms)
.await;
match &result {
Expand Down

0 comments on commit 95f2d7b

Please sign in to comment.