compile for WASM
parent
398fa15f15
commit
d7831dd48d
@ -0,0 +1,11 @@
|
|||||||
|
install:
|
||||||
|
- appveyor-retry appveyor DownloadFile https://win.rustup.rs/ -FileName rustup-init.exe
|
||||||
|
- if not defined RUSTFLAGS rustup-init.exe -y --default-host x86_64-pc-windows-msvc --default-toolchain nightly
|
||||||
|
- set PATH=%PATH%;C:\Users\appveyor\.cargo\bin
|
||||||
|
- rustc -V
|
||||||
|
- cargo -V
|
||||||
|
|
||||||
|
build: false
|
||||||
|
|
||||||
|
test_script:
|
||||||
|
- cargo test --locked
|
@ -0,0 +1,6 @@
|
|||||||
|
/target
|
||||||
|
**/*.rs.bk
|
||||||
|
Cargo.lock
|
||||||
|
bin/
|
||||||
|
pkg/
|
||||||
|
wasm-pack.log
|
@ -0,0 +1,69 @@
|
|||||||
|
language: rust
|
||||||
|
sudo: false
|
||||||
|
|
||||||
|
cache: cargo
|
||||||
|
|
||||||
|
matrix:
|
||||||
|
include:
|
||||||
|
|
||||||
|
# Builds with wasm-pack.
|
||||||
|
- rust: beta
|
||||||
|
env: RUST_BACKTRACE=1
|
||||||
|
addons:
|
||||||
|
firefox: latest
|
||||||
|
chrome: stable
|
||||||
|
before_script:
|
||||||
|
- (test -x $HOME/.cargo/bin/cargo-install-update || cargo install cargo-update)
|
||||||
|
- (test -x $HOME/.cargo/bin/cargo-generate || cargo install --vers "^0.2" cargo-generate)
|
||||||
|
- cargo install-update -a
|
||||||
|
- curl https://rustwasm.github.io/wasm-pack/installer/init.sh -sSf | sh -s -- -f
|
||||||
|
script:
|
||||||
|
- cargo generate --git . --name testing
|
||||||
|
# Having a broken Cargo.toml (in that it has curlies in fields) anywhere
|
||||||
|
# in any of our parent dirs is problematic.
|
||||||
|
- mv Cargo.toml Cargo.toml.tmpl
|
||||||
|
- cd testing
|
||||||
|
- wasm-pack build
|
||||||
|
- wasm-pack test --chrome --firefox --headless
|
||||||
|
|
||||||
|
# Builds on nightly.
|
||||||
|
- rust: nightly
|
||||||
|
env: RUST_BACKTRACE=1
|
||||||
|
before_script:
|
||||||
|
- (test -x $HOME/.cargo/bin/cargo-install-update || cargo install cargo-update)
|
||||||
|
- (test -x $HOME/.cargo/bin/cargo-generate || cargo install --vers "^0.2" cargo-generate)
|
||||||
|
- cargo install-update -a
|
||||||
|
- rustup target add wasm32-unknown-unknown
|
||||||
|
script:
|
||||||
|
- cargo generate --git . --name testing
|
||||||
|
- mv Cargo.toml Cargo.toml.tmpl
|
||||||
|
- cd testing
|
||||||
|
- cargo check
|
||||||
|
- cargo check --target wasm32-unknown-unknown
|
||||||
|
- cargo check --no-default-features
|
||||||
|
- cargo check --target wasm32-unknown-unknown --no-default-features
|
||||||
|
- cargo check --no-default-features --features console_error_panic_hook
|
||||||
|
- cargo check --target wasm32-unknown-unknown --no-default-features --features console_error_panic_hook
|
||||||
|
- cargo check --no-default-features --features "console_error_panic_hook wee_alloc"
|
||||||
|
- cargo check --target wasm32-unknown-unknown --no-default-features --features "console_error_panic_hook wee_alloc"
|
||||||
|
|
||||||
|
# Builds on beta.
|
||||||
|
- rust: beta
|
||||||
|
env: RUST_BACKTRACE=1
|
||||||
|
before_script:
|
||||||
|
- (test -x $HOME/.cargo/bin/cargo-install-update || cargo install cargo-update)
|
||||||
|
- (test -x $HOME/.cargo/bin/cargo-generate || cargo install --vers "^0.2" cargo-generate)
|
||||||
|
- cargo install-update -a
|
||||||
|
- rustup target add wasm32-unknown-unknown
|
||||||
|
script:
|
||||||
|
- cargo generate --git . --name testing
|
||||||
|
- mv Cargo.toml Cargo.toml.tmpl
|
||||||
|
- cd testing
|
||||||
|
- cargo check
|
||||||
|
- cargo check --target wasm32-unknown-unknown
|
||||||
|
- cargo check --no-default-features
|
||||||
|
- cargo check --target wasm32-unknown-unknown --no-default-features
|
||||||
|
- cargo check --no-default-features --features console_error_panic_hook
|
||||||
|
- cargo check --target wasm32-unknown-unknown --no-default-features --features console_error_panic_hook
|
||||||
|
# Note: no enabling the `wee_alloc` feature here because it requires
|
||||||
|
# nightly for now.
|
@ -0,0 +1,34 @@
|
|||||||
|
[package]
|
||||||
|
name = "cozo-lib-wasm"
|
||||||
|
version = "0.1.0"
|
||||||
|
authors = ["Ziyang Hu <hu.ziyang@cantab.net>"]
|
||||||
|
edition = "2021"
|
||||||
|
license = "MPL-2.0"
|
||||||
|
|
||||||
|
[lib]
|
||||||
|
crate-type = ["cdylib", "rlib"]
|
||||||
|
|
||||||
|
[features]
|
||||||
|
default = ["console_error_panic_hook"]
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
wasm-bindgen = "0.2.63"
|
||||||
|
cozo = { version = "0.1.7", path = "../cozo-core", default-features = false, features = ["wasm", "graph-algo", "nothread"] }
|
||||||
|
|
||||||
|
# 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.
|
||||||
|
wee_alloc = { version = "0.4.5", optional = true }
|
||||||
|
|
||||||
|
[dev-dependencies]
|
||||||
|
wasm-bindgen-test = "0.3.13"
|
||||||
|
|
||||||
|
[profile.release]
|
||||||
|
# Tell `rustc` to optimize for small code size.
|
||||||
|
opt-level = "s"
|
@ -0,0 +1,41 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2022, The Cozo Project Authors.
|
||||||
|
*
|
||||||
|
* This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
|
||||||
|
* If a copy of the MPL was not distributed with this file,
|
||||||
|
* You can obtain one at https://mozilla.org/MPL/2.0/.
|
||||||
|
*/
|
||||||
|
|
||||||
|
use wasm_bindgen::prelude::*;
|
||||||
|
|
||||||
|
use cozo::*;
|
||||||
|
|
||||||
|
mod utils;
|
||||||
|
|
||||||
|
// 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 struct CozoDb {
|
||||||
|
db: Db<MemStorage>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[wasm_bindgen]
|
||||||
|
impl CozoDb {
|
||||||
|
pub fn new() -> Self {
|
||||||
|
utils::set_panic_hook();
|
||||||
|
let db = new_cozo_mem().unwrap();
|
||||||
|
Self { db }
|
||||||
|
}
|
||||||
|
pub fn run(&self, script: &str, params: &str) -> String {
|
||||||
|
self.db.run_script_str(script, params)
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,18 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2022, The Cozo Project Authors.
|
||||||
|
*
|
||||||
|
* This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
|
||||||
|
* If a copy of the MPL was not distributed with this file,
|
||||||
|
* You can obtain one at https://mozilla.org/MPL/2.0/.
|
||||||
|
*/
|
||||||
|
|
||||||
|
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();
|
||||||
|
}
|
@ -0,0 +1,21 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2022, The Cozo Project Authors.
|
||||||
|
*
|
||||||
|
* This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
|
||||||
|
* If a copy of the MPL was not distributed with this file,
|
||||||
|
* You can obtain one at https://mozilla.org/MPL/2.0/.
|
||||||
|
*/
|
||||||
|
|
||||||
|
//! 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);
|
||||||
|
}
|
Loading…
Reference in New Issue