Skip to content

Instantly share code, notes, and snippets.

@stefanhuber
Created December 13, 2021 10:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stefanhuber/ca1bbe954b60fd35f68366b827c60dbc to your computer and use it in GitHub Desktop.
Save stefanhuber/ca1bbe954b60fd35f68366b827c60dbc to your computer and use it in GitHub Desktop.
Emscripten
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']"
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]
);
#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