Skip to content

Instantly share code, notes, and snippets.

@ryuukk
Created December 6, 2021 23:14
Show Gist options
  • Save ryuukk/81d048e79b37bf7a5442ebe082170f59 to your computer and use it in GitHub Desktop.
Save ryuukk/81d048e79b37bf7a5442ebe082170f59 to your computer and use it in GitHub Desktop.
clang --target=wasm32 -std=c99 -emit-llvm -c main.c
llc -march=wasm32 -filetype=obj main.bc
wasm-ld --no-entry --export-all --allow-undefined -o test.wasm main.o
<script>
const memory = new WebAssembly.Memory({
initial: 1024,
maximum: 4096,
});
var env =
{
memory: memory,
ASSERT: (c) => {
console.log("assert: " + c);
}
};
const importObject = {
env: env,
};
WebAssembly.instantiateStreaming(fetch("test.wasm", { credentials: "same-origin" }), importObject)
.then(result => {
console.log('JS: Loaded wasm file');
const { exports } = result.instance;
exports._start();
})
.catch(error => {
console.error('there was some error; ', error)
});
</script>
#include <stdbool.h>
#include <stddef.h>
extern void ASSERT(bool c);
void *memcpy(void *dest, const void * src, size_t n)
{
unsigned char *d = dest;
const unsigned char *s = src;
for (; n; n--) *d++ = *s++;
return dest;
}
void* memset(void* str, int c, size_t n)
{
unsigned char* s = str;
while (n--)
{
*s = c;
s++;
}
return str;
}
struct Big
{
char data[512000];
};
struct Contain
{
int index;
struct Big array[100];
int test;
};
static struct Contain c;
static void add(struct Big val)
{
c.array[c.index] = val;
c.index += 1;
}
void _start()
{
c.index = 0;
c.test = 5;
ASSERT(c.index == 0);
ASSERT(c.test == 5);
struct Big val = {};
add(val);
ASSERT(c.index == 1);
ASSERT(c.test == 5);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment