Add the glue code to call Bliss functions and return JS values

This commit is contained in:
Anton Liaposhchenko 2023-12-26 22:32:17 +02:00
parent b4d5019d83
commit 5fd2ef3902
5 changed files with 37 additions and 48 deletions

1
.gitignore vendored
View File

@ -1,2 +1,3 @@
target
node_modules
index.node

55
Cargo.lock generated
View File

@ -244,7 +244,7 @@ checksum = "c688fc74432808e3eb684cae8830a86be1d66a2bd58e1f248ed0960a590baf6f"
dependencies = [
"glob",
"libc",
"libloading 0.7.4",
"libloading",
]
[[package]]
@ -693,16 +693,6 @@ version = "0.2.151"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "302d7ab3130588088d277783b1e2d2e10c9e9e4a16dd9050e6ec93fb3e7048f4"
[[package]]
name = "libloading"
version = "0.6.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "351a32417a12d5f7e82c368a66781e307834dae04c6ce0cd4456d52989229883"
dependencies = [
"cfg-if",
"winapi 0.3.9",
]
[[package]]
name = "libloading"
version = "0.7.4"
@ -845,45 +835,29 @@ dependencies = [
[[package]]
name = "neon"
version = "0.10.1"
version = "1.0.0-alpha.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "28e15415261d880aed48122e917a45e87bb82cf0260bb6db48bbab44b7464373"
checksum = "8687031acf51f8b065aaf906b5a694f8d6b547c5c9430b6d636ab42422bfd0cc"
dependencies = [
"neon-build",
"libloading",
"neon-macros",
"neon-runtime",
"once_cell",
"semver",
"send_wrapper",
"smallvec",
]
[[package]]
name = "neon-build"
version = "0.10.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8bac98a702e71804af3dacfde41edde4a16076a7bbe889ae61e56e18c5b1c811"
[[package]]
name = "neon-macros"
version = "0.10.1"
version = "1.0.0-alpha.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b7288eac8b54af7913c60e0eb0e2a7683020dffa342ab3fd15e28f035ba897cf"
checksum = "facd664405d5140677a63d7b7c5762425dd9d4179e07d2e67da29089c50b1ddd"
dependencies = [
"quote",
"syn 1.0.109",
"syn-mid",
]
[[package]]
name = "neon-runtime"
version = "0.10.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4676720fa8bb32c64c3d9f49c47a47289239ec46b4bdb66d0913cc512cb0daca"
dependencies = [
"cfg-if",
"libloading 0.6.7",
"smallvec",
]
[[package]]
name = "noisy_float"
version = "0.2.0"
@ -1373,18 +1347,15 @@ checksum = "f98d2aa92eebf49b69786be48e4477826b256916e84a57ff2a4f21923b48eb4c"
[[package]]
name = "semver"
version = "0.9.0"
version = "1.0.20"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1d7eb9ef2c18661902cc47e535f9bc51b78acd254da71d375c2f6720d9a40403"
dependencies = [
"semver-parser",
]
checksum = "836fa6a3e1e547f9a2c4040802ec865b5d85f4014efe00555d7090a3dcaa1090"
[[package]]
name = "semver-parser"
version = "0.7.0"
name = "send_wrapper"
version = "0.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "388a1df253eca08550bef6c72392cfe7c30914bf41df5269b68cbd6ff8f570a3"
checksum = "cd0b0ec5f1c1ca621c432a25813d8d60c88abe6d3e08a3eb9cf37d97a0fe3d73"
[[package]]
name = "serde"

View File

@ -69,7 +69,7 @@ anyhow = { version = "1.0.58", optional = true }
indicatif = { version = "0.17.0", optional = true }
[dependencies.neon]
version = "0.10.1"
version = "1.0.0-alpha.4"
default-features = false
features = ["napi-6", "channel-api", "promise-api", "try-catch-api"]

Binary file not shown.

View File

@ -10,15 +10,32 @@ mod temporal;
mod timbral;
mod utils;
use neon::prelude::*;
use neon::{prelude::*, types::buffer::TypedArray};
use song::Song;
#[neon::main]
fn main(mut cx: ModuleContext) -> NeonResult<()> {
cx.export_function("test", test)?;
cx.export_function("analyze", analyze)?;
Ok(())
}
fn test(mut cx: FunctionContext) -> JsResult<JsNumber> {
Ok(cx.number(34))
/// Returns a Uint8Array, with the first 2 bytes being the version in platform endianness
/// and the rest (currently 80 bytes) being the analysis data in little endian
fn analyze(mut cx: FunctionContext) -> JsResult<JsUint8Array> {
let path = cx.argument::<JsString>(0)?.value(&mut cx);
let song = Song::from_path(path)
.or_else(|e| cx.throw_error(e.to_string()))?;
let version_bytes = song.features_version.to_ne_bytes();
let analysis_bytes = song.analysis.as_bytes();
let mut buffer_handle = JsUint8Array::new(
&mut cx,
analysis_bytes.len() + version_bytes.len(),
)?;
let buffer = buffer_handle.as_mut_slice(&mut cx);
buffer[0..version_bytes.len()].copy_from_slice(&version_bytes);
buffer[version_bytes.len()..].copy_from_slice(&analysis_bytes);
Ok(buffer_handle)
}