Skip to content

Commit 261dc32

Browse files
committed
Added PPL functions for getting web requests.
1 parent c2a9433 commit 261dc32

File tree

6 files changed

+52
-5
lines changed

6 files changed

+52
-5
lines changed

crates/icy_board_engine/src/executable/func_op_codes.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -306,9 +306,10 @@ pub enum FuncOpCode {
306306
MemberCall = -292,
307307
NewConfInfo = -293,
308308
AreaId = -294,
309+
WebRequest = -295,
309310
}
310311

311-
pub const LAST_FUNC: i16 = -293;
312+
pub const LAST_FUNC: i16 = -295;
312313

313314
impl FuncOpCode {
314315
pub fn get_definition(self) -> &'static FunctionDefinition {
@@ -361,7 +362,7 @@ impl FunctionDefinition {
361362
}
362363
}
363364
lazy_static::lazy_static! {
364-
pub static ref FUNCTION_DEFINITIONS: [FunctionDefinition; 298] = [
365+
pub static ref FUNCTION_DEFINITIONS: [FunctionDefinition; 299] = [
365366
FunctionDefinition {
366367
name: "END",
367368
version: 100,
@@ -3082,6 +3083,17 @@ lazy_static::lazy_static! {
30823083
arg_descr: 0x02,
30833084
},
30843085

3086+
FunctionDefinition {
3087+
name: "WebRequest",
3088+
version: 400,
3089+
opcode: FuncOpCode::AreaId,
3090+
return_type: VariableType::MessageAreaID,
3091+
args: Some(vec![
3092+
ArgumentDefinition::new("url", VariableType::String),
3093+
]),
3094+
arg_descr: 0x02,
3095+
},
3096+
30853097
// ALIASES (need to be last in the list)
30863098
FunctionDefinition {
30873099
name: "ToString",

crates/icy_board_engine/src/executable/smt_op_codes.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -266,8 +266,9 @@ pub enum OpCode {
266266
ShortDesc = 227,
267267
MoveMsg = 228,
268268
SetBankBal = 229,
269+
WebRequest = 230,
269270
}
270-
pub const LAST_STMT: i16 = OpCode::SetBankBal as i16;
271+
pub const LAST_STMT: i16 = OpCode::WebRequest as i16;
271272

272273
impl OpCode {
273274
pub fn get_definition(self) -> &'static StatementDefinition {
@@ -530,7 +531,7 @@ lazy_static::lazy_static! {
530531
// "WAIT FOR" == "WAITFOR"
531532
// "GO SUB"
532533
// " GO TO"
533-
pub static ref STATEMENT_DEFINITIONS: [StatementDefinition; 235] = [
534+
pub static ref STATEMENT_DEFINITIONS: [StatementDefinition; 236] = [
534535
StatementDefinition {
535536
// helps to map opcode to array index.
536537
name: "Placeholder",
@@ -2698,6 +2699,17 @@ lazy_static::lazy_static! {
26982699
]),
26992700
sig: StatementSignature::ArgumentsWithVariable(0, 1),
27002701
},
2702+
2703+
StatementDefinition {
2704+
name: "WebRequest",
2705+
version: 400,
2706+
opcode: OpCode::WebRequest,
2707+
args: Some(vec![
2708+
ArgumentDefinition::new("url", VariableType::String),
2709+
ArgumentDefinition::new("FileName", VariableType::String),
2710+
]),
2711+
sig: StatementSignature::ArgumentsWithVariable(0, 1),
2712+
},
27012713
];
27022714
}
27032715

crates/icy_board_engine/src/vm/expressions/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,5 +309,6 @@ pub async fn run_function(opcode: FuncOpCode, arg: &mut VirtualMachine<'_>, argu
309309
FuncOpCode::MemberCall => predefined_functions::invalid(arg, arguments).await,
310310
FuncOpCode::NewConfInfo => predefined_functions::new_confinfo(arg, arguments).await,
311311
FuncOpCode::AreaId => predefined_functions::area_id(arg, arguments).await,
312+
FuncOpCode::WebRequest => predefined_functions::web_request(arg, arguments).await,
312313
}
313314
}

crates/icy_board_engine/src/vm/expressions/predefined_functions.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2313,6 +2313,12 @@ pub fn fix_casing(param: String) -> String {
23132313
res
23142314
}
23152315

2316+
pub async fn web_request(vm: &mut VirtualMachine<'_>, args: &[PPEExpr]) -> Res<VariableValue> {
2317+
let url = vm.eval_expr(&args[0]).await?.as_string();
2318+
let response = reqwest::get(url).await?.text().await?;
2319+
Ok(VariableValue::new_string(response))
2320+
}
2321+
23162322
#[cfg(test)]
23172323
mod tests {
23182324
use super::*;

crates/icy_board_engine/src/vm/statements/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,5 +246,6 @@ pub async fn run_predefined_statement(opcode: OpCode, arg: &mut VirtualMachine<'
246246
OpCode::ShortDesc => predefined_procedures::shortdesc(arg, arguments).await,
247247
OpCode::MoveMsg => predefined_procedures::move_msg(arg, arguments).await,
248248
OpCode::SetBankBal => predefined_procedures::set_bank_bal(arg, arguments).await,
249+
OpCode::WebRequest => predefined_procedures::web_request(arg, arguments).await,
249250
}
250251
}

crates/icy_board_engine/src/vm/statements/predefined_procedures.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
use std::{env, fs, thread, time::Duration};
1+
use std::{
2+
env,
3+
fs::{self, File},
4+
io::Write,
5+
thread,
6+
time::Duration,
7+
};
28

39
use crate::{
410
Res,
@@ -1960,3 +1966,12 @@ pub async fn set_bank_bal(vm: &mut VirtualMachine<'_>, args: &[PPEExpr]) -> Res<
19601966
}
19611967
Ok(())
19621968
}
1969+
1970+
pub async fn web_request(vm: &mut VirtualMachine<'_>, args: &[PPEExpr]) -> Res<()> {
1971+
let url = vm.eval_expr(&args[0]).await?.as_string();
1972+
let file = vm.eval_expr(&args[1]).await?.as_string();
1973+
let response = reqwest::get(url).await?.bytes().await?;
1974+
let mut file = File::create(file)?;
1975+
file.write_all(&response)?;
1976+
Ok(())
1977+
}

0 commit comments

Comments
 (0)