-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
91 lines (69 loc) · 2.47 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>使用Rust和WebAssembly整花活儿</title>
</head>
<body>
Hello, World!
<div id="message"></div>
</body>
<script type="module">
function addIt2(m, n) {
return m + n;
};
import init, { add, update_message, print_values, print_js_value, only_return_error_when_result, return_all_when_result, print_uint8_array, return_time, call_js_func, User } from './pkg/hello_wasm.js';
const run = async () => {
await init();
const result = add(1, 2);
console.log(`the result from rust is: ${result}`);
update_message('#message', '<h1>Hello, Rust!</h1>');
const jsNumber = 10;
const jsBoolean = true;
const jsUint8Array = new Uint8Array(3);
jsUint8Array[0] = 1;
jsUint8Array[1] = 2;
jsUint8Array[2] = 3;
const jsNumberArray = [30, 40, 50];
print_values(jsNumber, jsBoolean, jsUint8Array, jsNumberArray);
print_js_value(jsNumber);
print_js_value(jsBoolean);
print_js_value(jsUint8Array);
print_js_value(jsNumberArray);
try {
only_return_error_when_result(1);
console.log('1 is ok');
} catch (error) {
console.log('An error is reported when the input parameter is 1: ', error);
}
try {
only_return_error_when_result(100);
console.log('100 is ok');
} catch (error) {
console.log('An error is reported when the input parameter is 100: ', error);
}
try {
const res = return_all_when_result(1);
console.log(`get ${res}`);
} catch (error) {
console.log('An error is reported when the input parameter is 1: ', error);
}
try {
const res = return_all_when_result(100);
console.log(`get ${res}`);
} catch (error) {
console.log('An error is reported when the input parameter is 100: ', error);
}
console.log('print_uint8_array() : ', print_uint8_array(jsUint8Array));
console.log('current time: ', return_time());
console.log('call_js_func: ', call_js_func());
// call_js_func();
const user = new User('kuari', 20);
console.log('user: ', user);
user.set_age(21);
user.print_user();
}
run();
</script>
</html>