mirror of
https://github.com/bytedream/litbwraw.git
synced 2025-05-09 04:05:12 +02:00
update book to rust edition 2024
This commit is contained in:
parent
3f9f4cd106
commit
9121c75f36
@ -17,8 +17,6 @@ fn main() {
|
|||||||
|
|
||||||
println!("cargo:rustc-link-arg=-sEXPORTED_RUNTIME_METHODS=['cwrap','ccall']");
|
println!("cargo:rustc-link-arg=-sEXPORTED_RUNTIME_METHODS=['cwrap','ccall']");
|
||||||
println!("cargo:rustc-link-arg=-sEXPORT_ES6=1");
|
println!("cargo:rustc-link-arg=-sEXPORT_ES6=1");
|
||||||
println!("cargo:rustc-link-arg=-sERROR_ON_UNDEFINED_SYMBOLS=0");
|
|
||||||
println!("cargo:rustc-link-arg=--no-entry");
|
|
||||||
println!(
|
println!(
|
||||||
"cargo:rustc-link-arg=-o{}.js",
|
"cargo:rustc-link-arg=-o{}.js",
|
||||||
book_output_path
|
book_output_path
|
||||||
|
@ -17,7 +17,6 @@ fn main() {
|
|||||||
|
|
||||||
println!("cargo:rustc-link-arg=-sEXPORTED_RUNTIME_METHODS=['cwrap','ccall']");
|
println!("cargo:rustc-link-arg=-sEXPORTED_RUNTIME_METHODS=['cwrap','ccall']");
|
||||||
println!("cargo:rustc-link-arg=-sEXPORT_ES6=1");
|
println!("cargo:rustc-link-arg=-sEXPORT_ES6=1");
|
||||||
println!("cargo:rustc-link-arg=-sERROR_ON_UNDEFINED_SYMBOLS=0");
|
|
||||||
println!("cargo:rustc-link-arg=--no-entry");
|
println!("cargo:rustc-link-arg=--no-entry");
|
||||||
println!(
|
println!(
|
||||||
"cargo:rustc-link-arg=-o{}.js",
|
"cargo:rustc-link-arg=-o{}.js",
|
||||||
|
@ -5,8 +5,8 @@ Adding logic on the wasm / Rust side is very much just like writing a (C compati
|
|||||||
Let's begin simple.
|
Let's begin simple.
|
||||||
This function creates a [Lua](https://docs.rs/mlua/latest/mlua/struct.Lua.html) instance and returns the raw pointer to it.
|
This function creates a [Lua](https://docs.rs/mlua/latest/mlua/struct.Lua.html) instance and returns the raw pointer to it.
|
||||||
```rust,ignore
|
```rust,ignore
|
||||||
#[no_mangle]
|
#[unsafe(no_mangle)]
|
||||||
pub extern "C" fn lua_new() -> *mut mlua::Lua {
|
pub unsafe extern "C" fn lua_new() -> *mut mlua::Lua {
|
||||||
let lua = mlua::Lua::new();
|
let lua = mlua::Lua::new();
|
||||||
Box::into_raw(Box::new(lua))
|
Box::into_raw(Box::new(lua))
|
||||||
}
|
}
|
||||||
@ -18,12 +18,12 @@ Now we have a Lua instance, but no way to use it, so let us create one.
|
|||||||
The function takes the pointer to the Lua struct we create in the `new_lua` function, as well as an arbitrary string, which should be lua code, as parameters.
|
The function takes the pointer to the Lua struct we create in the `new_lua` function, as well as an arbitrary string, which should be lua code, as parameters.
|
||||||
It then executes this string via the Lua instance and may write to `stderr` if an error occurs.
|
It then executes this string via the Lua instance and may write to `stderr` if an error occurs.
|
||||||
```rust,ignore
|
```rust,ignore
|
||||||
#[no_mangle]
|
#[unsafe(no_mangle)]
|
||||||
pub unsafe extern "C" fn lua_execute(lua: *mut mlua::Lua, to_execute: *const std::ffi::c_char) {
|
pub unsafe extern "C" fn lua_execute(lua: *mut mlua::Lua, to_execute: *const std::ffi::c_char) {
|
||||||
// casting the raw pointer of the created lua instance back to a usable Rust struct
|
// casting the raw pointer of the created lua instance back to a usable Rust struct
|
||||||
let lua: &mut mlua::Lua = &mut *lua;
|
let lua: &mut mlua::Lua = unsafe { &mut *lua };
|
||||||
// converting the c string into a `CStr` (which then can be converted to a `String`)
|
// converting the c string into a `CStr` (which then can be converted to a `String`)
|
||||||
let to_execute = std::ffi::CStr::from_ptr(to_execute);
|
let to_execute = unsafe { std::ffi::CStr::from_ptr(to_execute) };
|
||||||
|
|
||||||
// execute the input code via the lua interpreter
|
// execute the input code via the lua interpreter
|
||||||
if let Err(err) = lua.load(&to_execute.to_string_lossy().to_string()).exec() {
|
if let Err(err) = lua.load(&to_execute.to_string_lossy().to_string()).exec() {
|
||||||
|
@ -29,7 +29,7 @@ The `vendored` feature is always required for wasm.
|
|||||||
[package]
|
[package]
|
||||||
name = "my-project"
|
name = "my-project"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
edition = "2021"
|
edition = "2024"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
mlua = { version = ">=0.9.3", features = ["lua51", "vendored"] }
|
mlua = { version = ">=0.9.3", features = ["lua51", "vendored"] }
|
||||||
@ -48,7 +48,6 @@ mlua = { version = ">=0.9.3", features = ["lua51", "vendored"] }
|
|||||||
You need to set some additional compiler flags to be able to call your wasm code from Javascript:
|
You need to set some additional compiler flags to be able to call your wasm code from Javascript:
|
||||||
- `-sEXPORTED_RUNTIME_METHODS=['cwrap','ccall']`: this exports the `cwrap` and `ccall` Javascript functions which allows us to call our library methods
|
- `-sEXPORTED_RUNTIME_METHODS=['cwrap','ccall']`: this exports the `cwrap` and `ccall` Javascript functions which allows us to call our library methods
|
||||||
- `-sEXPORT_ES6=1`: this makes the created js glue ES6 compatible. It is not mandatory in general but needed as this tutorial/examples utilizes ES6 imports
|
- `-sEXPORT_ES6=1`: this makes the created js glue ES6 compatible. It is not mandatory in general but needed as this tutorial/examples utilizes ES6 imports
|
||||||
- `-sERROR_ON_UNDEFINED_SYMBOLS=0` (_optional for binary crates_): this ignores undefined symbols. Typically undefined symbols are not really undefined but the linker just can't find them, which is always the case if your crate is a library
|
|
||||||
|
|
||||||
> If your package is a library, you have to add some additional options:
|
> If your package is a library, you have to add some additional options:
|
||||||
> - `--no-entry`: this defines that the compiled wasm has no main function
|
> - `--no-entry`: this defines that the compiled wasm has no main function
|
||||||
@ -79,7 +78,6 @@ fn main() {
|
|||||||
> .unwrap()
|
> .unwrap()
|
||||||
> .join(pkg_name);
|
> .join(pkg_name);
|
||||||
>
|
>
|
||||||
> println!("cargo:rustc-link-arg=-sERROR_ON_UNDEFINED_SYMBOLS=0");
|
|
||||||
> println!("cargo:rustc-link-arg=--no-entry");
|
> println!("cargo:rustc-link-arg=--no-entry");
|
||||||
> println!("cargo:rustc-link-arg=-o{}.js", target_path.to_string_lossy());
|
> println!("cargo:rustc-link-arg=-o{}.js", target_path.to_string_lossy());
|
||||||
> ```
|
> ```
|
||||||
|
Loading…
x
Reference in New Issue
Block a user