Updated and function for main and sum tests

Add build script
This commit is contained in:
Sambo Chea 2020-08-20 14:51:06 +07:00
parent afa4f43eb1
commit 39101f2081
3 changed files with 38 additions and 0 deletions

14
build Executable file
View File

@ -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"

View File

@ -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());

11
src/main.rs Normal file
View File

@ -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
}