From 39101f20814d9964a6532d8c11fab24dd3214d64 Mon Sep 17 00:00:00 2001 From: Sambo Chea Date: Thu, 20 Aug 2020 14:51:06 +0700 Subject: [PATCH] Updated and function for main and sum tests Add build script --- build | 14 ++++++++++++++ src/lib.rs | 13 +++++++++++++ src/main.rs | 11 +++++++++++ 3 files changed, 38 insertions(+) create mode 100755 build create mode 100644 src/main.rs diff --git a/build b/build new file mode 100755 index 0000000..01e38d6 --- /dev/null +++ b/build @@ -0,0 +1,14 @@ +#!/bin/bash + +echo "build wasm pack..." +wasm-pack build + +if [ $? -eq 0 ] +then + echo -e "\e[32mBuild successfully!" +else + echo -e "\e[31mBuild failed for wasm-pack into script!" >&2 + exit 1 +fi + +echo -e "\e[39m" diff --git a/src/lib.rs b/src/lib.rs index 9d380c6..0aae62e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -3,6 +3,7 @@ use wasm_bindgen::prelude::*; extern crate web_sys; mod utils; +mod main; // When the `wee_alloc` feature is enabled, use `wee_alloc` as the global // allocator. @@ -20,11 +21,23 @@ extern { fn alert(s: &str); } +#[wasm_bindgen] +extern { + fn sum(a: i32, b: i32) -> i32; +} + #[wasm_bindgen] pub fn greet() { alert("Hey, CUBETIQ Solution!"); } +#[wasm_bindgen] +pub fn just_sum() -> i32 { + let sum = sum(100, 50); + log(&format!("Just Sum {} + {} = {}", 100, 50, sum)); + return sum; +} + #[wasm_bindgen] pub fn log(s: &str) { web_sys::console::log_1(&s.into()); diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..3b6fe19 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,11 @@ +fn main() { + let a = 10; + let b = 5; + let sum = sum(a, b); + + println!("Sum of {} + {} = {}", a, b, sum); +} + +pub fn sum(a: i32, b: i32) -> i32 { + a + b +} \ No newline at end of file