Piccolo - 用純Rust實(shí)現(xiàn)的無棧Lua虛擬機(jī)
Piccolo,原名luster,在經(jīng)過數(shù)年的中斷后,于2023年4月悄然恢復(fù)了開發(fā)。曾經(jīng)開發(fā)過 rlua 的 kyren,在底層gc-arenacrate 取得突破后,回到了piccolo項(xiàng)目。這兩個(gè)項(xiàng)目現(xiàn)在已經(jīng)(小心地)在生產(chǎn)中使用,比如 Fish Folk 用在了游戲腳本中,Ruffle 用于其 ActionScript 虛擬機(jī)。 如今兩個(gè)重要版本的發(fā)布了:piccolo v0.3.0 和 gc-arena v0.5。 Piccolo項(xiàng)目的目標(biāo),按大致優(yōu)先級降序排列:
成為一個(gè)可以工作、有用的 Lua 解釋器。
成為一種簡單而可靠的方式,用于安全地隔離不受信任的Lua腳本。
抵御來自不受信任腳本的 DoS 攻擊(腳本不能夠?qū)е陆忉屍鞅罎⒒蚴褂脽o限量的內(nèi)存,并應(yīng)保證在有限時(shí)間內(nèi)返回控制權(quán)給調(diào)用者)。
提供一種安全地將 Rust API 綁定到 Lua 的簡便方法,具有對異常情況和邊緣情況具有處理的綁定系統(tǒng),并且用戶類型可以安全地參與運(yùn)行時(shí)垃圾回收。
與某些版本的 PUC-Rio Lua 兼容。
不要過于緩慢(例如,避免不必要的抽象化)。
使用 Rust 生成 IP 地址的 PNG 圖片
看起來像是這樣:
原文是一個(gè)詳盡的教程,告訴讀者如何一步一步的實(shí)現(xiàn)這個(gè)例子,感興趣的可以查看原文。最終實(shí)現(xiàn)其實(shí)代碼很簡潔:
use std::{io::Cursor, net::SocketAddr, sync::OnceLock};
use axum::{
extract::ConnectInfo,
http::{header, StatusCode},
response::IntoResponse,
routing::get,
Router,
};
use image::{ImageBuffer, ImageOutputFormat, Rgb};
use imageproc::drawing::draw_text_mut;
use rusttype::{Font, Scale};
const X: i32 = 8;
const Y: i32 = 96;
const WIDTH: u32 = 256;
const HEIGHT: u32 = WIDTH;
const TEXT_COLOR: Rgb= Rgb([235, 219, 178]);
const BACKGROUND_COLOR: Rgb= Rgb([177, 98, 134]);
const SCALE: Scale = Scale { x: 32.0, y: 32.0 };
const FONT_DATA: &[u8] = include_bytes!(concat!(
env!("CARGO_MANIFEST_DIR"),
"/fonts/UbuntuMono-R.ttf"
));
#[derive(Debug, thiserror::Error)]
#[error("Failed to generate image: {0}")]
struct AvatarError(#[from] image::ImageError);
impl IntoResponse for AvatarError {
fn into_response(self) -> axum::response::Response {
(StatusCode::INTERNAL_SERVER_ERROR, self.to_string()).into_response()
}
}
fn font() -> &'static Font<'static> {
static FONT: OnceLock= OnceLock::new();
FONT.get_or_init(|| Font::try_from_bytes(FONT_DATA).expect("Built-in font data was invalid"))
}
async fn avatar(
ConnectInfo(addr): ConnectInfo,
) -> Result
// Wow, IPv6 causes a lot of headache.
let ip = addr.ip().to_canonical();
letmut img = ImageBuffer::from_pixel(WIDTH, HEIGHT, BACKGROUND_COLOR);
draw_text_mut(&mut img, TEXT_COLOR, X, Y, SCALE, font(), "Hello,");
let y = Y + SCALE.y asi32;
draw_text_mut(&mut img, TEXT_COLOR, X, y, SCALE, font(), &format!("{ip}!"));
letmut cursor = Cursor::new(vec![]);
img.write_to(&mut cursor, ImageOutputFormat::Png)?;
Ok(([(header::CONTENT_TYPE, "image/png")], cursor.into_inner()))
}
#[tokio::main]
async fn main() -> anyhow::Result<()> {
let app = Router::new().route("/avatar.png", get(avatar));
let listener = tokio::net::TcpListener::bind("[::]:3000").await?;
let make_service = app.into_make_service_with_connect_info::();
axum::serve(listener, make_service).await?;
Ok(())
}
審核編輯:劉清
-
虛擬機(jī)
+關(guān)注
關(guān)注
1文章
919瀏覽量
28318 -
Lua
+關(guān)注
關(guān)注
0文章
81瀏覽量
10570 -
解釋器
+關(guān)注
關(guān)注
0文章
103瀏覽量
6546 -
Rust
+關(guān)注
關(guān)注
1文章
229瀏覽量
6626
原文標(biāo)題:【Rust日報(bào)】Piccolo - 用純Rust實(shí)現(xiàn)的無棧Lua虛擬機(jī)
文章出處:【微信號:Rust語言中文社區(qū),微信公眾號:Rust語言中文社區(qū)】歡迎添加關(guān)注!文章轉(zhuǎn)載請注明出處。
發(fā)布評論請先 登錄
相關(guān)推薦
虛擬化數(shù)據(jù)恢復(fù)—XenServer虛擬機(jī)數(shù)據(jù)恢復(fù)案例
![<b class='flag-5'>虛擬</b>化數(shù)據(jù)恢復(fù)—XenServer<b class='flag-5'>虛擬機(jī)</b>數(shù)據(jù)恢復(fù)案例](https://file1.elecfans.com/web1/M00/F4/8B/wKgZoWctd42AfXpjAAbtwDYtgLM057.png)
虛擬化數(shù)據(jù)恢復(fù)——Hyper-V虛擬機(jī)數(shù)據(jù)恢復(fù)案例
![<b class='flag-5'>虛擬</b>化數(shù)據(jù)恢復(fù)——Hyper-V<b class='flag-5'>虛擬機(jī)</b>數(shù)據(jù)恢復(fù)案例](https://file1.elecfans.com/web2/M00/8F/DD/wKgZomTTN6qAWIF8AAM51-OjElE231.png)
Lua語法基礎(chǔ)教程(上篇)
虛擬機(jī)數(shù)據(jù)恢復(fù)—異常斷電導(dǎo)致XenServer虛擬機(jī)不可用的數(shù)據(jù)恢復(fù)案例
![<b class='flag-5'>虛擬機(jī)</b>數(shù)據(jù)恢復(fù)—異常斷電導(dǎo)致XenServer<b class='flag-5'>虛擬機(jī)</b>不可用的數(shù)據(jù)恢復(fù)案例](https://file1.elecfans.com/web2/M00/8F/99/wKgaomTQm3qAf9x-AATepdcm3zE240.png)
鴻蒙跨端實(shí)踐-JS虛擬機(jī)架構(gòu)實(shí)現(xiàn)
![鴻蒙跨端實(shí)踐-JS<b class='flag-5'>虛擬機(jī)</b>架構(gòu)<b class='flag-5'>實(shí)現(xiàn)</b>](https://file1.elecfans.com//web2/M00/08/BC/wKgZomb6SCqAaKbAAACBgMlQU1Y485.png)
虛擬化數(shù)據(jù)恢復(fù)—異常斷電導(dǎo)致VMware虛擬機(jī)文件丟失的數(shù)據(jù)恢復(fù)案例
![<b class='flag-5'>虛擬</b>化數(shù)據(jù)恢復(fù)—異常斷電導(dǎo)致VMware<b class='flag-5'>虛擬機(jī)</b>文件丟失的數(shù)據(jù)恢復(fù)案例](https://file1.elecfans.com//web2/M00/06/4A/wKgZombjz9yATbc8AACIBZQn0Ps165.jpg)
虛擬化數(shù)據(jù)恢復(fù)—EXSI虛擬機(jī)誤還原快照如何恢復(fù)數(shù)據(jù)?
![<b class='flag-5'>虛擬</b>化數(shù)據(jù)恢復(fù)—EXSI<b class='flag-5'>虛擬機(jī)</b>誤還原快照如何恢復(fù)數(shù)據(jù)?](https://file1.elecfans.com/web2/M00/E1/58/wKgaomY4adOAeUAFAAHkmHUEi4s929.png)
什么是虛擬機(jī)?什么是虛擬化?
創(chuàng)建ubuntu虛擬機(jī)
虛擬機(jī)數(shù)據(jù)恢復(fù)—KVM虛擬機(jī)被誤刪除的數(shù)據(jù)恢復(fù)案例
![<b class='flag-5'>虛擬機(jī)</b>數(shù)據(jù)恢復(fù)—KVM<b class='flag-5'>虛擬機(jī)</b>被誤刪除的數(shù)據(jù)恢復(fù)案例](https://file.elecfans.com/web2/M00/AD/F3/poYBAGSadTWACtx5AANUKGY87Ws406.png)
什么是虛擬機(jī)?虛擬機(jī)真的那么好用嗎?
![什么是<b class='flag-5'>虛擬機(jī)</b>?<b class='flag-5'>虛擬機(jī)</b>真的那么好用嗎?](https://file.elecfans.com/web2/M00/43/7B/pYYBAGJ-B6aAHuNPAAAf8J1Ebk4778.jpg)
虛擬化數(shù)據(jù)恢復(fù)—虛擬機(jī)誤還原快照的數(shù)據(jù)恢復(fù)案例
VMware虛擬機(jī)慢,卡頓,用遠(yuǎn)程桌面的方法變順暢
![VMware<b class='flag-5'>虛擬機(jī)</b>慢,卡頓,<b class='flag-5'>用</b>遠(yuǎn)程桌面的方法變順暢](https://file1.elecfans.com/web2/M00/E3/23/wKgZomY8Mf2AK7H8AACcWfJwpQE106.png)
評論