Skip to content

Commit

Permalink
updated README
Browse files Browse the repository at this point in the history
  • Loading branch information
danbugs committed Sep 9, 2021
1 parent d78c64c commit 9ba8bbb
Showing 1 changed file with 18 additions and 26 deletions.
44 changes: 18 additions & 26 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,11 @@ html_t hello_world()
// ^^^ functions that return content to be rendered
// must return the html_t or char* types

html_t tmp = malloc(128 * sizeof(char));
html_t tmp;
// ^^^ you don't need to free this malloc,
// the framework will do it for you.

sprintf(tmp, "%s", "Hello, World!");
asprintf(&tmp, "%s", "Hello, World!");
// ^^^ this works because html_t expands
// to char*

Expand Down Expand Up @@ -96,7 +96,7 @@ To test the project, run: `tapm run`. Navigating to `http://127.0.0.1:4000` in y
![getting-started-1](https://i.imgur.com/zGWqSow.png)
For a more complex example, view the project in the `example/` folder of the repo!
For more complex examples, view the project in the `examples/` folder of the repo!
## Appendix
Expand Down Expand Up @@ -129,24 +129,30 @@ html_t html = HTML(
);
```

### Building from source
...or:

The root level `Makefile` was designed for POSIX-compliant systems — `tapm build` will not work on Windows outside of WSL.

### Binding a C function to a DOM event

![binding-a-c-function-to-a-dom-event](https://camo.githubusercontent.com/339f5dbb4a8a9945034f5b1ba5efc7e5e2780a5353361c2fbea52b5ae47c7c4b/68747470733a2f2f692e696d6775722e636f6d2f6c766e6f646e442e676966)
```
html_t html = HTMLIFY(" \
<div> \
<h1 class='red'>${hello_world()}$</h1> \
</div> \
<style> \
.red { \
color: red; \
} \
</style> \
");
```

### Returning other types as `html_t`

```
EMSCRIPTEN_KEEPALIVE
html_t add_two_numbers(int a, int b)
{
char *tmp = malloc(128 * sizeof(int));
char *tmp;
int result = a + b;
sprintf(tmp, "%d", result);
html_t expands to char*
asprintf(&tmp, "%d", result);
return tmp;
}
```
Expand All @@ -164,20 +170,6 @@ html_t hello_world()
```
... would also work. Thing is, it is not the best because the framework will try to free a pointer that wasn't malloc-ed. While this doesn't cause an error, I wouldn't call it a best practice.

### Are you updating HTML you've already rendered?

If that's the case, you must remove the old html before you update your state. For example, when updating the counter in `example/src/hello_world.c`, I do:

```C
EMSCRIPTEN_KEEPALIVE
void increase_counter()
{
remove_html(main_h);
counter++;
display_html(main_h);
}
```

### Notes to Self

To check for any problems (i.e., mem leaks and whatnot) in the code at runtime, compile like so: `emcc hello_world.c ../../src/dcw.c --js-library ../../src/dcw.js -fsanitize=address -s ALLOW_MEMORY_GROWTH -s INITIAL_MEMORY=285212672 -gsource-map --source-map-base http://127.0.0.1:4000`. After that, run with: `tapm run`.

0 comments on commit 9ba8bbb

Please sign in to comment.