Created
December 13, 2021 10:15
-
-
Save stefanhuber/ca1bbe954b60fd35f68366b827c60dbc to your computer and use it in GitHub Desktop.
Emscripten
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Module._free(arrayPointer);emcc C:\00_projects\wasm-experiment\main.c -o C:\00_projects\wasm-experiment\www\sort.html -s NO_EXIT_RUNTIME=1 -s "EXPORTED_RUNTIME_METHODS=['ccall','cwrap']" -s "EXPORTED_FUNCTIONS=['_malloc','_free']" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const items = [-12, 4, 0, 2]; | |
const arrayLength = items.length; | |
const bytesPerElement = Module.HEAP32.BYTES_PER_ELEMENT; | |
const arrayPointer = Module._malloc((arrayLength * bytesPerElement)); | |
Module.HEAP32.set(items, (arrayPointer / bytesPerElement)); | |
Module.ccall('selection_sort', | |
null, | |
['number', 'number'], | |
[arrayPointer, arrayLength] | |
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <emscripten.h> | |
EMSCRIPTEN_KEEPALIVE void selection_sort (int *a, int n) { | |
int i, j, m, t; | |
for (i = 0; i < n; i++) | |
printf("%d%s", a[i], i == n - 1 ? "\n" : " "); | |
for (i = 0; i < n; i++) { | |
for (j = i, m = i; j < n; j++) { | |
if (a[j] < a[m]) { | |
m = j; | |
} | |
} | |
t = a[i]; | |
a[i] = a[m]; | |
a[m] = t; | |
} | |
for (i = 0; i < n; i++) | |
printf("%d%s", a[i], i == n - 1 ? "\n" : " "); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment