Skip to content

Commit

Permalink
init js polars; #83
Browse files Browse the repository at this point in the history
  • Loading branch information
ritchie46 committed Apr 13, 2021
1 parent 09a2a11 commit a9b679e
Show file tree
Hide file tree
Showing 9 changed files with 222 additions and 2 deletions.
6 changes: 6 additions & 0 deletions js-polars/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/target
**/*.rs.bk
Cargo.lock
bin/
pkg/
wasm-pack.log
48 changes: 48 additions & 0 deletions js-polars/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
[package]
name = "polars"
version = "0.1.0"
authors = ["Ritchie Vink"]
edition = "2018"
repository = "https://github.com/ritchie46/polars"
license = "MIT"
documentation = "https://ritchie46.github.io/polars-book/"
homepage = "https://github.com/ritchie46/polars"

[lib]
crate-type = ["cdylib", "rlib"]

[features]
default = ["console_error_panic_hook"]

[dependencies]
wasm-bindgen = "0.2"
# The `console_error_panic_hook` crate provides better debugging of panics by
# logging them with `console.error`. This is great for development, but requires
# all the `std::fmt` and `std::panicking` infrastructure, so isn't great for
# code size when deploying.
console_error_panic_hook = { version = "0.1.6", optional = true }

# `wee_alloc` is a tiny allocator for wasm that is only ~1K in code size
# compared to the default allocator's ~10K. It is slower than the default
# allocator, however.
#
# Unfortunately, `wee_alloc` requires nightly Rust when targeting wasm for now.
wee_alloc = { version = "0.4.5", optional = true }

[dependencies.polars-core]
path = "../polars/polars-core"
default-features = false
features = [
"strings",
"temporal",
"performant",
]

[dev-dependencies]
wasm-bindgen-test = "0.3.13"

[profile.release]
# Tell `rustc` to optimize for small code size.
opt-level = "s"

[workspace]
3 changes: 3 additions & 0 deletions js-polars/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@

build:
wasm-pack build --target nodejs
20 changes: 20 additions & 0 deletions js-polars/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
mod series;
mod utils;

use wasm_bindgen::prelude::*;

// When the `wee_alloc` feature is enabled, use `wee_alloc` as the global
// allocator.
#[cfg(feature = "wee_alloc")]
#[global_allocator]
static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;

#[wasm_bindgen]
extern "C" {
fn alert(s: &str);
}

#[wasm_bindgen]
pub fn greet() {
alert("Hello, {{project-name}}!");
}
119 changes: 119 additions & 0 deletions js-polars/src/series.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
use polars_core::prelude::*;
use std::ops::{BitAnd, BitOr};
use wasm_bindgen::prelude::*;

#[wasm_bindgen(js_name = Series)]
pub struct JsSeries {
series: Series,
}

impl From<Series> for JsSeries {
fn from(series: Series) -> Self {
Self { series }
}
}

#[wasm_bindgen]
impl JsSeries {
#[wasm_bindgen(constructor, js_name = newFlt)]
pub fn new_flt(name: &str, values: &[f64]) -> Self {
Self {
series: Series::new(name, values),
}
}

#[wasm_bindgen(constructor, js_name = newInt)]
pub fn new_int(name: &str, values: &[i64]) -> Self {
Self {
series: Series::new(name, values),
}
}

#[wasm_bindgen(constructor, js_name = newStr)]
pub fn new_str(name: &str, values: Box<[JsValue]>) -> Self {
let ca: Utf8Chunked = NewChunkedArray::<_, String>::new_from_iter(
name,
values.iter().map(|v| v.as_string().unwrap()),
);
Self { series: ca.into() }
}

#[wasm_bindgen(method)]
pub fn rechunk(&mut self, in_place: bool) -> Option<JsSeries> {
let series = self.series.rechunk();
if in_place {
self.series = series;
None
} else {
Some(series.into())
}
}

#[wasm_bindgen(method)]
pub fn bitand(&self, other: &JsSeries) -> Self {
let s = self
.series
.bool()
.expect("boolean")
.bitand(other.series.bool().expect("boolean"))
.into_series();
s.into()
}

#[wasm_bindgen(method)]
pub fn bitor(&self, other: &JsSeries) -> Self {
let s = self
.series
.bool()
.expect("boolean")
.bitor(other.series.bool().expect("boolean"))
.into_series();
s.into()
}

#[wasm_bindgen(method)]
pub fn cum_sum(&self, reverse: bool) -> Self {
self.series.cum_sum(reverse).into()
}

#[wasm_bindgen(method)]
pub fn cum_max(&self, reverse: bool) -> Self {
self.series.cum_max(reverse).into()
}

#[wasm_bindgen(method)]
pub fn cum_min(&self, reverse: bool) -> Self {
self.series.cum_min(reverse).into()
}

#[wasm_bindgen(method)]
pub fn chunk_lengths(&self) -> Vec<usize> {
self.series.chunk_lengths().clone()
}

#[wasm_bindgen(method)]
pub fn name(&self) -> String {
self.series.name().into()
}

#[wasm_bindgen(method)]
pub fn rename(&mut self, name: &str) {
self.series.rename(name);
}

#[wasm_bindgen(method)]
pub fn mean(&self) -> Option<f64> {
self.series.mean()
}

#[wasm_bindgen(method)]
pub fn n_chunks(&self) -> usize {
self.series.n_chunks()
}

#[wasm_bindgen(method)]
pub fn limit(&self, num_elements: usize) -> Self {
let series = self.series.limit(num_elements);
series.into()
}
}
10 changes: 10 additions & 0 deletions js-polars/src/utils.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
pub fn set_panic_hook() {
// When the `console_error_panic_hook` feature is enabled, we can call the
// `set_panic_hook` function at least once during initialization, and then
// we will get better error messages if our code ever panics.
//
// For more details see
// https://github.com/rustwasm/console_error_panic_hook#readme
#[cfg(feature = "console_error_panic_hook")]
console_error_panic_hook::set_once();
}
13 changes: 13 additions & 0 deletions js-polars/tests/web.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//! Test suite for the Web and headless browsers.
#![cfg(target_arch = "wasm32")]

extern crate wasm_bindgen_test;
use wasm_bindgen_test::*;

wasm_bindgen_test_configure!(run_in_browser);

#[wasm_bindgen_test]
fn pass() {
assert_eq!(1 + 1, 2);
}
4 changes: 2 additions & 2 deletions py-polars/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions py-polars/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ features = [
"random",
"object",
"ipc",
"csv-file",
"pretty_fmt",
"mimalloc",
"performant",
Expand Down

0 comments on commit a9b679e

Please sign in to comment.