From 3cd8c473a561f8a84b311eb789e95a48354febb2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?22388o=E2=9A=A1=EF=B8=8F?= <83122757+22388o@users.noreply.github.com> Date: Fri, 19 Jul 2024 09:42:11 -0300 Subject: [PATCH 1/6] Create Cargo.toml --- bindings/rust/Cargo.toml | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 bindings/rust/Cargo.toml diff --git a/bindings/rust/Cargo.toml b/bindings/rust/Cargo.toml new file mode 100644 index 00000000..fa9fb10c --- /dev/null +++ b/bindings/rust/Cargo.toml @@ -0,0 +1,5 @@ +[lib] +crate-type = ["cdylib"] + +[dependencies] +wasm-bindgen = "0.2" From 09868b7c659f1d7cc6ca62c7e252276ad736c6ad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?22388o=E2=9A=A1=EF=B8=8F?= <83122757+22388o@users.noreply.github.com> Date: Fri, 19 Jul 2024 09:42:36 -0300 Subject: [PATCH 2/6] Create lib.rs --- bindings/rust/lib.rs | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 bindings/rust/lib.rs diff --git a/bindings/rust/lib.rs b/bindings/rust/lib.rs new file mode 100644 index 00000000..0151f413 --- /dev/null +++ b/bindings/rust/lib.rs @@ -0,0 +1,6 @@ +use wasm_bindgen::prelude::*; + +#[wasm_bindgen] +pub fn greet(name: &str) -> String { + format!("Hello, {}!", name) +} From 4a049d8c798907de47a36824b90277895567fe91 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?22388o=E2=9A=A1=EF=B8=8F?= <83122757+22388o@users.noreply.github.com> Date: Fri, 19 Jul 2024 09:43:10 -0300 Subject: [PATCH 3/6] Create greet.cpp --- bindings/c++/greet.cpp | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 bindings/c++/greet.cpp diff --git a/bindings/c++/greet.cpp b/bindings/c++/greet.cpp new file mode 100644 index 00000000..d43e339c --- /dev/null +++ b/bindings/c++/greet.cpp @@ -0,0 +1,8 @@ +#include + +extern "C" { + const char* greet(const char* name) { + std::string greeting = "Hello, " + std::string(name) + "!"; + return greeting.c_str(); + } +} From 11c598bfa147e1d1e255f0d394b5081b50270c52 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?22388o=E2=9A=A1=EF=B8=8F?= <83122757+22388o@users.noreply.github.com> Date: Fri, 19 Jul 2024 09:43:45 -0300 Subject: [PATCH 4/6] Create tsconfig.json --- bindings/ts/tsconfig.json | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 bindings/ts/tsconfig.json diff --git a/bindings/ts/tsconfig.json b/bindings/ts/tsconfig.json new file mode 100644 index 00000000..c33bd4b7 --- /dev/null +++ b/bindings/ts/tsconfig.json @@ -0,0 +1,9 @@ +{ + "compilerOptions": { + "target": "es5", + "module": "commonjs", + "strict": true, + "esModuleInterop": true, + "skipLibCheck": true + } +} From f6efa21e28ec40a8badccd5f95a8a9c02b3a3204 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?22388o=E2=9A=A1=EF=B8=8F?= <83122757+22388o@users.noreply.github.com> Date: Fri, 19 Jul 2024 09:44:23 -0300 Subject: [PATCH 5/6] Create index.ts --- bindings/ts/index.ts | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 bindings/ts/index.ts diff --git a/bindings/ts/index.ts b/bindings/ts/index.ts new file mode 100644 index 00000000..015f8211 --- /dev/null +++ b/bindings/ts/index.ts @@ -0,0 +1,5 @@ +import { greet as rustGreet } from '../rust_wasm/rust_wasm'; +import { greet as cppGreet } from '../cpp_wasm'; + +console.log(rustGreet('Rust')); +console.log(cppGreet('C++')); From 24326b685760f0f2421a3fcf601eb8741b17cbf7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?22388o=E2=9A=A1=EF=B8=8F?= <83122757+22388o@users.noreply.github.com> Date: Fri, 19 Jul 2024 09:44:52 -0300 Subject: [PATCH 6/6] Create cpp_wasm.js --- bindings/ts/cpp_wasm.js | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 bindings/ts/cpp_wasm.js diff --git a/bindings/ts/cpp_wasm.js b/bindings/ts/cpp_wasm.js new file mode 100644 index 00000000..434b22a5 --- /dev/null +++ b/bindings/ts/cpp_wasm.js @@ -0,0 +1,14 @@ +mergeInto(LibraryManager.library, { + greet: function(namePtr) { + var name = UTF8ToString(namePtr); + var greeting = Module.greet(name); + return allocateUTF8(greeting); + } +}); + +export function greet(name) { + const namePtr = allocateUTF8(name); + const resultPtr = _greet(namePtr); + const result = UTF8ToString(resultPtr); + return result; +}