用 Rust 实现 JS API
对于 JavaScript 开发者来说,将 Rust 函数结合到 JavaScript API 中是非常有用的。这样做,使得开发者在用”纯 JavaScript”编写程序的同时,也可使用高性能的 Rust 函数。你可以使用 WasmEdge Runtime 做到这一点。
在开始之前,克隆 GitHub 仓库 wasmedge-quickjs,并切换到目录 examples/embed_js。该操作如下所示。
git clone https://github.com/second-state/wasmedge-quickjs cd examples/embed_js
对于如何将 JavaScript 嵌入到 Rust 中,embed_js 列出了几个不同的例子。你可以按如下的命令构建并运行这些示例。
cargo build --target wasm32-wasi --release wasmedge --dir .:. target/wasm32-wasi/release/embed_js.wasm
注:命令行中的
--dir .:.给予了 wasmedge 读取文件系统的本地目录的权限。
创建一个 JavaScript 函数 API
下面这个代码片段定义了一个 Rust 函数,该函数可以作为一个 API 结合到 JavaScript 解释器中。
2. #![allow(unused)]
3. fn main() {
4. fn run_rust_function(ctx: &mut Context) {
6. struct HelloFn;
7. impl JsFn for HelloFn {
8. fn call(_ctx: &mut Context, _this_val: JsValue, argv: &[JsValue]) -> JsValue {
9. println!("hello from rust");
10. println!("argv={:?}", argv);
11. JsValue::UnDefined
12. }
13. }
15. ...
16. }
17. }
下面的代码展示了如何把这个 Rust 函数添加到 JavaScript 解释器中,并为该函数的 JavaScript API 指定一个签名 hi(),随之在 JavaScript 代码中调用它。
2. #![allow(unused)]
3. fn main() {
4. fn run_rust_function(ctx: &mut Context) {
5. ...
7. let f = ctx.new_function::<HelloFn>("hello");
8. ctx.get_global().set("hi", f.into());
9. let code = r#"hi(1,2,3)"#;
10. let r = ctx.eval_global_str(code);
11. println!("return value:{:?}", r);
12. }
13. }
执行的结果如下所示。
hello from rust argv=[Int(1), Int(2), Int(3)] return value:UnDefined
通过这个方法,你可以创建出一个带有自定义 API 函数的 JavaScript 解释器。这个解释器是运行在 WasmEdge 里的,并且可以通过 CLI 或者网络来执行调用了这些 API 函数的 JavaScript 代码。
创建一个 JavaScript 对象 API
在 JavaScript API 的设计中,我们有时需要提供一个封装了数据以及函数的对象。在下面的示例中,我们为 JavaScript API 定义了一个 Rust 函数。
2. #![allow(unused)]
3. fn main() {
4. fn rust_new_object_and_js_call(ctx: &mut Context) {
5. struct ObjectFn;
6. impl JsFn for ObjectFn {
7. fn call(_ctx: &mut Context, this_val: JsValue, argv: &[JsValue]) -> JsValue {
8. println!("hello from rust");
9. println!("argv={:?}", argv);
10. if let JsValue::Object(obj) = this_val {
11. let obj_map = obj.to_map();
12. println!("this={:#?}", obj_map);
13. }
14. JsValue::UnDefined
15. }
16. }
18. ...
19. }
20. }
然后,我们在 Rust 端创建一个”对象”,设置它的数据域,然后将该 Rust 函数注册为关联到该对象的 JavaScript 函数。
2. #![allow(unused)]
3. fn main() {
4. let mut obj = ctx.new_object();
5. obj.set("a", 1.into());
6. obj.set("b", ctx.new_string("abc").into());
8. let f = ctx.new_function::<ObjectFn>("anything");
9. obj.set("f", f.into());
10. }
接下来,我们使这个 Rust “对象” 能在 JavaScript 解释器中作为 JavaScript 对象 test_obj 使用。
2. #![allow(unused)]
3. fn main() {
4. ctx.get_global().set("test_obj", obj.into());
5. }
在 JavaScript 代码中,你可以直接将 test_obj 作为 API 的一部分使用了。
2. #![allow(unused)]
3. fn main() {
4. let code = r#"
5. print('test_obj keys=',Object.keys(test_obj))
6. print('test_obj.a=',test_obj.a)
7. print('test_obj.b=',test_obj.b)
8. test_obj.f(1,2,3,"hi")
9. "#;
11. ctx.eval_global_str(code);
12. }
代码执行的结果如下。
test_obj keys= a,b,f test_obj.a= 1 test_obj.b= abc hello from rust argv=[Int(1), Int(2), Int(3), String(JsString(hi))] this=Ok( { "a": Int( 1, ), "b": String( JsString( abc, ), ), "f": Function( JsFunction( function anything() { [native code] }, ), ), }, )
一个完整的 JavaScript 对象 API
在前面的示例中,我们简单地演示了如何用 Rust 创建 JavaScript API。在这个例子中,我们将会创建一个完整的 Rust 模块并让它成为一个 JavaScript 对象 API。这个项目在文件夹 examples/embed_rust_module 中。你可以构建并将其当做一个标准的 Rust 应用,在 WasmEdge 中运行它。
cargo build --target wasm32-wasi --release wasmedge --dir .:. target/wasm32-wasi/release/embed_rust_module.wasm
该对象的 Rust 实现,如下面这个模块所示。它含有数据域、构造函数、访问器、设置器以及函数。
2. #![allow(unused)]
3. fn main() {
4. mod point {
5. use wasmedge_quickjs::*;
7. #[derive(Debug)]
8. struct Point(i32, i32);
10. struct PointDef;
12. impl JsClassDef<Point> for PointDef {
13. const CLASS_NAME: &'static str = "Point\0";
14. const CONSTRUCTOR_ARGC: u8 = 2;
16. fn constructor(_: &mut Context, argv: &[JsValue]) -> Option<Point> {
17. println!("rust-> new Point {:?}", argv);
18. let x = argv.get(0);
19. let y = argv.get(1);
20. if let ((Some(JsValue::Int(ref x)), Some(JsValue::Int(ref y)))) = (x, y) {
21. Some(Point(*x, *y))
22. } else {
23. None
24. }
25. }
27. fn proto_init(p: &mut JsClassProto<Point, PointDef>) {
28. struct X;
29. impl JsClassGetterSetter<Point> for X {
30. const NAME: &'static str = "x\0";
32. fn getter(_: &mut Context, this_val: &mut Point) -> JsValue {
33. println!("rust-> get x");
34. this_val.0.into()
35. }
37. fn setter(_: &mut Context, this_val: &mut Point, val: JsValue) {
38. println!("rust-> set x:{:?}", val);
39. if let JsValue::Int(x) = val {
40. this_val.0 = x
41. }
42. }
43. }
45. struct Y;
46. impl JsClassGetterSetter<Point> for Y {
47. const NAME: &'static str = "y\0";
49. fn getter(_: &mut Context, this_val: &mut Point) -> JsValue {
50. println!("rust-> get y");
51. this_val.1.into()
52. }
54. fn setter(_: &mut Context, this_val: &mut Point, val: JsValue) {
55. println!("rust-> set y:{:?}", val);
56. if let JsValue::Int(y) = val {
57. this_val.1 = y
58. }
59. }
60. }
62. struct FnPrint;
63. impl JsMethod<Point> for FnPrint {
64. const NAME: &'static str = "pprint\0";
65. const LEN: u8 = 0;
67. fn call(_: &mut Context, this_val: &mut Point, _argv: &[JsValue]) -> JsValue {
68. println!("rust-> pprint: {:?}", this_val);
69. JsValue::Int(1)
70. }
71. }
73. p.add_getter_setter(X);
74. p.add_getter_setter(Y);
75. p.add_function(FnPrint);
76. }
77. }
79. struct PointModule;
80. impl ModuleInit for PointModule {
81. fn init_module(ctx: &mut Context, m: &mut JsModuleDef) {
82. m.add_export("Point\0", PointDef::class_value(ctx));
83. }
84. }
86. pub fn init_point_module(ctx: &mut Context) {
87. ctx.register_class(PointDef);
88. ctx.register_module("point\0", PointModule, &["Point\0"]);
89. }
90. }
91. }
在解释器的实现中,我们先调用 point::init_point_module 以将 Rust 模块注册到 JavaScript 的上下文中,然后运行一个使用了对象 point 的 JavaScript 程序。
1. use wasmedge_quickjs::*;
2. fn main() {
3. let mut ctx = Context::new();
4. point::init_point_module(&mut ctx);
6. let code = r#"
7. import('point').then((point)=>{
8. let p0 = new point.Point(1,2)
9. print("js->",p0.x,p0.y)
10. p0.pprint()
11. try{
12. let p = new point.Point()
13. print("js-> p:",p)
14. print("js->",p.x,p.y)
15. p.x=2
16. p.pprint()
17. } catch(e) {
18. print("An error has been caught");
19. print(e)
20. }
21. })
22. "#;
24. ctx.eval_global_str(code);
25. ctx.promise_loop_poll();
26. }
上面这个应用的执行结果如下所示。
rust-> new Point [Int(1), Int(2)] rust-> get x rust-> get y js-> 1 2 rust-> pprint: Point(1, 2) rust-> new Point [] js-> p: undefined An error has been caught TypeError: cannot read property 'x' of undefined
