Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 29515de

Browse files
authored
Merge pull request metacall#411 from devraymondsh/develop
Improve rs_port stage 1
2 parents a59c465 + 21f7f6d commit 29515de

29 files changed

+1618
-436
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// For format details, see https://aka.ms/devcontainer.json. For config options, see the
2+
// README at: https://github.com/devcontainers/templates/tree/main/src/docker-existing-dockerfile
3+
{
4+
"name": "Existing Dockerfile",
5+
"build": {
6+
// Sets the run context to one level up instead of the .devcontainer folder.
7+
"context": "..",
8+
// Update the 'dockerFile' property if you aren't using the standard 'Dockerfile' filename.
9+
"dockerfile": "../Dockerfile"
10+
},
11+
// Features to add to the dev container. More info: https://containers.dev/features.
12+
// "features": {},
13+
// Use 'forwardPorts' to make a list of ports inside the container available locally.
14+
// "forwardPorts": [],
15+
// Uncomment the next line to run commands after the container is created.
16+
// "postCreateCommand": "cat /etc/os-release",
17+
// Configure tool-specific properties.
18+
// "customizations": {},
19+
// Uncomment to connect as an existing user other than the container default. More info: https://aka.ms/dev-containers-non-root.
20+
"remoteUser": "root"
21+
}

source/ports/rs_port/Cargo.toml

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "metacall"
3-
version = "0.3.0"
3+
version = "0.3.1"
44
repository = "https://github.com/metacall/core/tree/develop/source/ports/rs_port"
55
authors = ["Vicente Eduardo Ferrer Garcia <[email protected]>", "Swarnim Arun <[email protected]>"]
66
edition = "2021"
@@ -15,4 +15,10 @@ path = "src/lib.rs"
1515
edition = "2021"
1616

1717
[dependencies]
18-
metacall-inline = { path = "./inline", version = "0.1.0" }
18+
concat-idents = "1.1.4"
19+
metacall-inline = { path = "./inline", version = "0.1.1" }
20+
21+
[build-dependencies]
22+
bindgen = { version = "0.64.0", default-features = false, features = ["runtime", "logging", "which-rustfmt"]}
23+
git2 = { version = "0.16.1", default-features = false, features = ["https"] }
24+
tempfile = { version = "3.4.0", default-features = false }

source/ports/rs_port/Dockerfile

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
FROM devraymondsh/ubuntu-docker-rust
2+
3+
# Install dependencies
4+
RUN apt-get update \
5+
&& apt-get install -y --no-install-recommends build-essential cmake ca-certificates git nodejs npm pkg-config clang-11 clang-format-11 libclang-11-dev cmake valgrind libdw-dev libbfd-dev libdwarf-dev libffi-dev
6+
7+
WORKDIR /root/metacall-polyglot
8+
RUN git clone --branch v0.7.3 https://github.com/metacall/core
9+
RUN mkdir core/build
10+
11+
WORKDIR /root/metacall-polyglot/core/build
12+
RUN cmake \
13+
-DOPTION_BUILD_DETOURS=Off \
14+
-DOPTION_BUILD_EXAMPLES=Off \
15+
-DOPTION_BUILD_LOADERS_C=On \
16+
-DOPTION_BUILD_LOADERS_NODE=On \
17+
-DOPTION_BUILD_SANITIZER=ON \
18+
-DOPTION_BUILD_SCRIPTS=Off \
19+
-DOPTION_BUILD_SERIALS_RAPID_JSON=On \
20+
-DOPTION_BUILD_TESTS=Off \
21+
..
22+
RUN cmake --build . --target install
23+
RUN cd /usr/local/lib && ldconfig
24+
25+
RUN rustup component add rustfmt
26+
RUN rustup toolchain add nightly
27+
RUN cargo install cargo-valgrind
28+
29+
WORKDIR /root/metacall-polyglot
30+
CMD ["cargo", "test"]

source/ports/rs_port/README.md

Lines changed: 10 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -26,35 +26,16 @@ export function sum(a: number, b: number): number {
2626

2727
`main.rs`
2828
``` rust
29+
use metacall::{hooks, loaders, structs::Any, metacall};
30+
2931
fn main() {
30-
let _d = defer(metacall::destroy);
31-
32-
match metacall::initialize() {
33-
Err(e) => {
34-
println!("{}", e);
35-
panic!();
36-
}
37-
_ => println!("MetaCall initialized"),
38-
}
39-
40-
let scripts = ["sum.ts".to_string()];
41-
42-
if let Err(e) = metacall::load_from_file("ts", &scripts) {
43-
println!("{}", e);
44-
panic!();
45-
}
46-
47-
match metacall::metacall(
48-
"sum",
49-
&[metacall::Any::Double(1.0), metacall::Any::Double(2.0)],
50-
) {
51-
Err(e) => {
52-
println!("{}", e);
53-
panic!();
54-
}
55-
Ok(ret) => {
56-
println!("{:?}", ret);
57-
}
58-
}
32+
// Metacall automatically shuts down when it goes out of scope
33+
let _ = hooks::initialize().unwrap();
34+
35+
loaders::from_file("ts", ["sum.ts"]).unwrap();
36+
37+
let sum = metacall("sum", [Any::Double(1.0), Any::Double(2.0)]).unwrap();
38+
39+
println!("sum: {:?}", sum);
5940
}
6041
```

source/ports/rs_port/build.rs

Lines changed: 94 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,98 @@
1-
use std::env;
1+
use bindgen::builder;
2+
use git2::Repository;
3+
use std::{
4+
env, fs, io,
5+
path::{Path, PathBuf},
6+
};
7+
8+
const METACALL_CORE_REPO: &str = "https://github.com/metacall/core.git#v0.7.3";
9+
10+
pub fn copy_recursively(source: impl AsRef<Path>, destination: impl AsRef<Path>) -> io::Result<()> {
11+
fs::create_dir_all(&destination)?;
12+
for entry in fs::read_dir(source)? {
13+
let entry = entry?;
14+
let filetype = entry.file_type()?;
15+
16+
if filetype.is_dir() {
17+
copy_recursively(entry.path(), destination.as_ref().join(entry.file_name()))?;
18+
} else {
19+
fs::copy(entry.path(), destination.as_ref().join(entry.file_name()))?;
20+
}
21+
}
22+
Ok(())
23+
}
24+
fn get_bindings_dir() -> PathBuf {
25+
let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap());
26+
let bindings_dir = out_dir.join("bindings");
27+
28+
fs::create_dir_all(&bindings_dir).unwrap();
29+
30+
bindings_dir.canonicalize().unwrap()
31+
}
32+
fn download_the_headers<T: ToString>(bindings_dir: &PathBuf, headers: &[T]) {
33+
let mut need_for_clone = false;
34+
let bindings_str = bindings_dir.to_str().unwrap();
35+
36+
for header in headers {
37+
let path = PathBuf::from(format!("{}/{}", bindings_str, header.to_string()));
38+
if !path.exists() && !need_for_clone {
39+
need_for_clone = true;
40+
}
41+
}
42+
43+
if need_for_clone {
44+
let temp_dir = tempfile::tempdir().unwrap();
45+
Repository::clone(METACALL_CORE_REPO, &temp_dir).unwrap();
46+
47+
let tmp_dir = temp_dir.path().to_str().unwrap();
48+
let source = format!("{}/source/metacall", tmp_dir);
49+
let destination = bindings_dir.join("metacall");
50+
copy_recursively(source, destination).unwrap();
51+
}
52+
}
53+
fn generate_bindings<T: ToString>(bindings_dir: &PathBuf, headers: &[T]) {
54+
let bindings_dir_str = bindings_dir.to_str().unwrap();
55+
let mut builder = builder();
56+
57+
for header in headers {
58+
builder = builder.header(format!("{}/{}", bindings_dir_str, header.to_string()));
59+
}
60+
61+
builder = builder
62+
.detect_include_paths(true)
63+
.size_t_is_usize(true)
64+
.rustfmt_bindings(true)
65+
.generate_comments(true)
66+
.derive_hash(true);
67+
let bindings = builder.generate().unwrap();
68+
69+
bindings
70+
.write_to_file(bindings_dir.join("bindings.rs"))
71+
.unwrap();
72+
}
73+
274
fn main() {
75+
const HEADERS: [&str; 3] = [
76+
"metacall/include/metacall/metacall.h",
77+
"metacall/include/metacall/metacall_value.h",
78+
"metacall/include/metacall/metacall_error.h",
79+
];
80+
let bindings_dir = get_bindings_dir();
81+
82+
download_the_headers(&bindings_dir, &HEADERS);
83+
generate_bindings(&bindings_dir, &HEADERS);
84+
85+
for header in HEADERS {
86+
println!(
87+
"{}",
88+
format!(
89+
"cargo:rerun-if-changed={}/{}",
90+
bindings_dir.to_str().unwrap(),
91+
header
92+
)
93+
);
94+
}
95+
396
// when running tests
497
if let Ok(val) = env::var("CMAKE_BINARY_DIR") {
598
println!("cargo:rustc-link-search={val}");
@@ -23,8 +116,4 @@ fn main() {
23116
} else {
24117
println!("cargo:rustc-link-lib=metacall");
25118
}
26-
27-
// default install location
28-
29-
// user defined location
30119
}

source/ports/rs_port/inline/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "metacall-inline"
3-
version = "0.1.0"
3+
version = "0.1.1"
44
repository = "https://github.com/metacall/core/tree/develop/source/ports/rs_port"
55
edition = "2021"
66
license = "Apache-2.0"

source/ports/rs_port/inline/src/lib.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::env;
2+
13
use proc_macro::TokenStream;
24
use quote::quote;
35

@@ -10,7 +12,7 @@ macro_rules! gen_inline_macro {
1012
let buffer = token_stream_input.to_string();
1113

1214
let result = quote! {{
13-
::metacall::load_from_memory(stringify!($name), #buffer.to_string()).unwrap()
15+
::metacall::loaders::from_memory(stringify!($name), #buffer.to_string()).unwrap()
1416
}};
1517

1618
result.into()
@@ -20,3 +22,17 @@ macro_rules! gen_inline_macro {
2022
}
2123

2224
gen_inline_macro!(py, node, ts, cs, rb, cob, rpc, java, wasm);
25+
26+
#[proc_macro]
27+
pub fn include_bindings(_input: TokenStream) -> TokenStream {
28+
let out_dir = env::var("OUT_DIR").unwrap();
29+
let path = format!("{}/bindings/bindings.rs", out_dir);
30+
31+
let result = quote! {
32+
#[path = #path]
33+
#[allow(warnings)]
34+
mod generated_bindings;
35+
};
36+
37+
result.into()
38+
}

source/ports/rs_port/rust-toolchain

Lines changed: 0 additions & 1 deletion
This file was deleted.

source/ports/rs_port/src/abi.rs

Lines changed: 0 additions & 43 deletions
This file was deleted.

source/ports/rs_port/src/hooks.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
use crate::{
2+
bindings::{metacall_destroy, metacall_initialize},
3+
prelude::MetacallInitError,
4+
};
5+
use std::ffi::c_int;
6+
7+
pub fn destroy_manually() -> c_int {
8+
unsafe { metacall_destroy() }
9+
}
10+
pub fn initialize_manually() -> c_int {
11+
unsafe { metacall_initialize() }
12+
}
13+
14+
pub struct MetacallAutoDestroy(pub fn() -> c_int);
15+
impl Drop for MetacallAutoDestroy {
16+
fn drop(&mut self) {
17+
self.0();
18+
}
19+
}
20+
21+
pub fn initialize() -> Result<MetacallAutoDestroy, MetacallInitError> {
22+
if initialize_manually() != 0 {
23+
return Err(MetacallInitError::new());
24+
}
25+
26+
Ok(MetacallAutoDestroy(destroy_manually))
27+
}

0 commit comments

Comments
 (0)