你好呀,
我想知道你为什么不走这条路,先把 typeript 写到 rust transpiler,然后在wasm-pack的帮助下生成 js 代码
从:
export greet(){
let x: boolean = true;
if(x){
console.log("Greetings, x is true");
}
}
(AST 在两者之间遍历)
到:
#[wasm_bindgen]
pub fn greet() {
let x: bool = true;
if x {
println!("Greetings, x is true");
}
}
有什么原因吗?
@solidsnail自动内存管理怎么样(AssemblyScript 提供而 Rust 不提供)? 首先。 :眨眼:
@solidsnail立即回答第二个问题。 AssemblyScript 为不同大小和功能的运行时支持库提供了四个选项 (https://www.assemblyscript.org/runtime.html)。 你对用 Rust 重新实现它的想法是什么(我假设使用 no-std)?
在 AssemblyScript 的情况下,一种与 Rust 完全不同的语言,转换为 Rust 将有效地用一个不同的 Rust 低级层替换 Binaryen IR 的低级层,该低级层不是专门为该任务设计的。 可以在一定程度上完成,能够有效地通过 LLVM 进行管道传输的轻微好处(我预计这种好处会随着时间的推移而减少),但这也意味着两层标准库和两层编译器,很可能会产生次优且不必要的笨拙最终 WebAssembly 代码。 缺点很快就会超过好处(如果有的话),即使整个东西下载更大,更难设置,使编译更慢,最终无法引导编译器。
也是 Alon 描述的为什么我们使用Binaryen 而不是 LLVM 或基于 LLVM 的语言的重要原因
我明白了,谢谢你的见解。
我实际上更热衷于从 DX 的角度看待事物,按照以下思路思考:
计数器.tsx:
import { WASM, Component, i32 } from "./lib";
<strong i="9">@Component</strong>
export class Counter {
wasm_state = {
count: 1,
};
@WASM({
getter: true,
})
getCount() {
return this.wasm_state.count;
}
@WASM({
setter: true,
})
setCount(count: i32) {
this.wasm_state.count = count;
}
render() {
return (
<div>
<h1>Count: {this.getCount()}</h1>
</div>
);
}
}
AST 打字稿遍历、转换和生成文件:
counter.wasm:
extern crate cfg_if;
extern crate wasm_bindgen;
mod utils;
use cfg_if::cfg_if;
use wasm_bindgen::prelude::*;
cfg_if! {
if #[cfg(feature = "wee_alloc")] {
extern crate wee_alloc;
#[global_allocator]
static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;
}
}
#[wasm_bindgen]
pub struct State {
count: i32,
}
#[wasm_bindgen]
impl State {
#[wasm_bindgen(constructor)]
pub fn new(count: i32) -> State {
State { count }
}
#[wasm_bindgen(getter)]
pub fn getCount(&self) -> i32 {
self.count
}
#[wasm_bindgen(setter)]
pub fn setCount(&mut self, count: i32) {
self.count = count;
}
}
计数器.js:
import {html, render} from 'lit-html';
import { State } from './counter.wasm';
export class Counter extends HTMLElement{
constructor(){
super();
State.setCount(1)
this.renderHtml()
}
renderHtml(){
render(html`
<div>
<h1>Count: ${State.getCount()}</h1>
</div>
`, this);
}
}