Skip to content

Commit b516bd2

Browse files
committed
Formated and linted
1 parent a69d48c commit b516bd2

File tree

4 files changed

+76
-78
lines changed

4 files changed

+76
-78
lines changed

examples/cross_build/wasm/bindigs/CMakeLists.txt

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,6 @@ install(TARGETS wasm_example DESTINATION "."
1515
# Required for Emscripten + embind
1616
set(CMAKE_EXECUTABLE_SUFFIX ".html")
1717

18-
# set_target_properties(wasm_example PROPERTIES
19-
# LINK_FLAGS "-sEXPORTED_FUNCTIONS=['_malloc','_free'] -sEXPORTED_RUNTIME_METHODS=['ccall','cwrap','getValue','setValue'] -sENVIRONMENT=web -sALLOW_MEMORY_GROWTH=1 -sNO_EXIT_RUNTIME=1 --shell-file ${CMAKE_SOURCE_DIR}/shell.html"
20-
# )
21-
2218
set_target_properties(wasm_example PROPERTIES
2319
LINK_FLAGS
2420
"-sEXPORTED_FUNCTIONS=['_malloc','_free'] \

examples/cross_build/wasm/bindigs/main.cpp

Lines changed: 29 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1+
#include <Eigen/Core>
12
#include <cstdint>
23
#include <emscripten/emscripten.h>
34
#include <iostream>
4-
#include <Eigen/Core>
55
#include <string>
66

77
#ifdef __cplusplus
@@ -10,46 +10,42 @@
1010
#define EXTERN
1111
#endif
1212

13-
14-
EXTERN EMSCRIPTEN_KEEPALIVE
15-
uint32_t fib(uint32_t n) {
16-
std::cout << "Calculating Fibonacci for n = " << n << std::endl;
17-
if (n <= 1) return n;
18-
uint32_t a = 0, b = 1, c;
19-
for (uint32_t i = 2; i <= n; ++i) {
20-
c = a + b;
21-
a = b;
22-
b = c;
23-
}
24-
return c;
13+
EXTERN EMSCRIPTEN_KEEPALIVE uint32_t fib(uint32_t n) {
14+
std::cout << "Calculating Fibonacci for n = " << n << std::endl;
15+
if (n <= 1)
16+
return n;
17+
uint32_t a = 0, b = 1, c;
18+
for (uint32_t i = 2; i <= n; ++i) {
19+
c = a + b;
20+
a = b;
21+
b = c;
22+
}
23+
return c;
2524
}
2625

27-
EXTERN EMSCRIPTEN_KEEPALIVE
28-
void printMessage(const char* message) {
29-
std::cout << "Message from C: " << message << std::endl;
30-
std::string script = "alert('Message from C++: " + std::string(message) + "')";
31-
std::cout << "Executing script: " << script << std::endl;
32-
emscripten_run_script(script.c_str());
26+
EXTERN EMSCRIPTEN_KEEPALIVE void printMessage(const char *message) {
27+
std::cout << "Message from C: " << message << std::endl;
28+
std::string script =
29+
"alert('Message from C++: " + std::string(message) + "')";
30+
std::cout << "Executing script: " << script << std::endl;
31+
emscripten_run_script(script.c_str());
3332
}
3433

35-
EXTERN EMSCRIPTEN_KEEPALIVE
36-
void addOne(int32_t *input, int32_t *output){
37-
*output = *input + 1;
34+
EXTERN EMSCRIPTEN_KEEPALIVE void addOne(int32_t *input, int32_t *output) {
35+
*output = *input + 1;
3836
}
3937

40-
EXTERN EMSCRIPTEN_KEEPALIVE
41-
float sumArray(const float* data, int32_t size) {
42-
// print data input
43-
std::cout << "Data input: ";
44-
for (int i = 0; i < size; ++i) {
45-
std::cout << data[i] << " ";
46-
}
47-
std::cout << std::endl;
48-
Eigen::Map<const Eigen::ArrayXf> vec(data, size);
49-
return vec.sum();
38+
EXTERN EMSCRIPTEN_KEEPALIVE float sumArray(const float *data, int32_t size) {
39+
// print data input
40+
std::cout << "Data input: ";
41+
for (int i = 0; i < size; ++i) {
42+
std::cout << data[i] << " ";
43+
}
44+
std::cout << std::endl;
45+
Eigen::Map<const Eigen::ArrayXf> vec(data, size);
46+
return vec.sum();
5047
}
5148

52-
5349
int main() {
5450
std::cout << "Hello World!" << std::endl;
5551
auto data = new float[5]{1.0f, 2.0f, 3.0f, 4.0f, 5.0f};

examples/cross_build/wasm/bindigs/shell.html

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ <h1>Conan C++ WebAssembly Example</h1>
9696

9797
// Example of auto string handle by WASM on simple string parameter passing
9898
const printMessage = () => {
99-
var printer = Module.ccall(
99+
Module.ccall(
100100
"printMessage",
101101
null,
102102
["string"],
@@ -106,48 +106,51 @@ <h1>Conan C++ WebAssembly Example</h1>
106106

107107
// Example of a simple invocation to fibonachi
108108
const fibExample = () => {
109-
var fib = Module.cwrap("fib", "number", ["number"]); // returns a number
110-
var input = parseInt(document.getElementById("fibInput").value);
109+
const fib = Module.cwrap("fib", "number", ["number"]); // returns a number
110+
const input = parseInt(document.getElementById("fibInput").value);
111111
if (isNaN(input)) {
112112
alert("Please enter a valid integer.");
113113
return;
114114
}
115-
var result = fib(input);
115+
const result = fib(input);
116116
document.getElementById("fibResult").textContent =
117117
"Fibonacci of " + input + " is: " + result;
118118
};
119119

120120
var value = 0; // (static) value to increment by one
121121
const pressBtn = () => {
122-
var addOne = Module.cwrap("addOne", null, ["number", "number"]); // void function
122+
const addOne = Module.cwrap("addOne", null, ["number", "number"]); // void function
123123
// alloc 4 bytes of memory for the input and 4 for the output (32-bit integers)
124-
var input_ptr = Module._malloc(4);
125-
var output_ptr = Module._malloc(4);
124+
const inputPtr = Module._malloc(4);
125+
const outputPtr = Module._malloc(4);
126126

127-
Module.setValue(input_ptr, value, "i32"); // set the value in WASM memory
128-
addOne(input_ptr, output_ptr); // call the WASM function
129-
var result = Module.getValue(output_ptr, "i32"); // extract the result from WASM memory
127+
Module.setValue(inputPtr, value, "i32");
128+
addOne(inputPtr, outputPtr);
129+
const result = Module.getValue(outputPtr, "i32");
130130
value = result;
131131
document.getElementById("counterResult").textContent = "Sum: " + result;
132132

133133
// dealloc memory to avoid memory leaks
134-
Module._free(input_ptr);
135-
Module._free(output_ptr);
134+
Module._free(inputPtr);
135+
Module._free(outputPtr);
136136
};
137137

138138
const sumExample = () => {
139-
var sumArray = Module.cwrap("sumArray", "number", ["number", "number"]);
139+
const sumArray = Module.cwrap("sumArray", "number", [
140+
"number",
141+
"number",
142+
]);
140143
// Get the input string and split by commas
141144
const inputStr = document.getElementById("numbersInput").value;
142145
const numberStrings = inputStr.split(",").map((s) => s.trim());
143146

144147
// Convert to Float32Array
145148
const inputArray = new Float32Array(numberStrings.map(Number));
146-
var len = inputArray.length;
147-
var bytesPerElement = inputArray.BYTES_PER_ELEMENT;
148-
var inputPtr = Module._malloc(len * bytesPerElement);
149+
const len = inputArray.length;
150+
const bytesPerElement = inputArray.BYTES_PER_ELEMENT;
151+
const inputPtr = Module._malloc(len * bytesPerElement);
149152
Module.HEAPF32.set(inputArray, inputPtr / bytesPerElement);
150-
var result = sumArray(inputPtr, len);
153+
const result = sumArray(inputPtr, len);
151154
Module._free(inputPtr);
152155
document.getElementById("sumResult").textContent = "Sum: " + result;
153156
};
Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,37 @@
1-
#include <iostream>
2-
#include <vector>
31
#include <cstdlib>
42
#include <cstring>
3+
#include <iostream>
4+
#include <vector>
55

66
int main() {
7-
const size_t GIGABYTE = 1024 * 1024 * 1024;
8-
const size_t CHUNK_SIZE = 32 * 1024 * 1024; // 32 MiB
9-
std::vector<char*> allocations;
10-
size_t total_allocated = 0;
11-
12-
while (true) {
13-
char* block = static_cast<char*>(malloc(CHUNK_SIZE));
14-
if (!block) {
15-
std::cerr << "Failed after allocating " << total_allocated / (1024 * 1024) << " MiB ~ " << total_allocated / GIGABYTE << " GiB" << std::endl;
16-
break;
17-
}
7+
const size_t GIGABYTE = 1024 * 1024 * 1024;
8+
const size_t CHUNK_SIZE = 32 * 1024 * 1024; // 32 MiB
9+
std::vector<char *> allocations;
10+
size_t total_allocated = 0;
1811

19-
// Touch memory to ensure it's actually committed (not lazy-allocated)
20-
std::memset(block, 0xAB, CHUNK_SIZE);
21-
allocations.push_back(block);
22-
total_allocated += CHUNK_SIZE;
23-
if (total_allocated % GIGABYTE == 0) {
24-
std::cout << "Current allocated: " << total_allocated / GIGABYTE << " GiB" << std::endl;
25-
}
12+
while (true) {
13+
char *block = static_cast<char *>(malloc(CHUNK_SIZE));
14+
if (!block) {
15+
std::cerr << "Failed after allocating " << total_allocated / (1024 * 1024)
16+
<< " MiB ~ " << total_allocated / GIGABYTE << " GiB"
17+
<< std::endl;
18+
break;
2619
}
2720

28-
// Free the memory afterward
29-
for (char* ptr : allocations) {
30-
free(ptr);
21+
// Touch memory to ensure it's actually committed (not lazy-allocated)
22+
std::memset(block, 0xAB, CHUNK_SIZE);
23+
allocations.push_back(block);
24+
total_allocated += CHUNK_SIZE;
25+
if (total_allocated % GIGABYTE == 0) {
26+
std::cout << "Current allocated: " << total_allocated / GIGABYTE << " GiB"
27+
<< std::endl;
3128
}
29+
}
30+
31+
// Free the memory afterward
32+
for (char *ptr : allocations) {
33+
free(ptr);
34+
}
3235

33-
return 0;
36+
return 0;
3437
}

0 commit comments

Comments
 (0)