Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/bindings #27

Merged
merged 6 commits into from
Jul 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions bindings/c++/greet.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#include <string>

extern "C" {
const char* greet(const char* name) {
std::string greeting = "Hello, " + std::string(name) + "!";
return greeting.c_str();
}
}
5 changes: 5 additions & 0 deletions bindings/rust/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[lib]
crate-type = ["cdylib"]

[dependencies]
wasm-bindgen = "0.2"
6 changes: 6 additions & 0 deletions bindings/rust/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
use wasm_bindgen::prelude::*;

#[wasm_bindgen]
pub fn greet(name: &str) -> String {
format!("Hello, {}!", name)
}
14 changes: 14 additions & 0 deletions bindings/ts/cpp_wasm.js
Original file line number Diff line number Diff line change
@@ -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;
}
5 changes: 5 additions & 0 deletions bindings/ts/index.ts
Original file line number Diff line number Diff line change
@@ -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++'));
9 changes: 9 additions & 0 deletions bindings/ts/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"strict": true,
"esModuleInterop": true,
"skipLibCheck": true
}
}
Loading