apfl/webpage/playground/playground.c

40 lines
941 B
C
Raw Normal View History

#include <emscripten.h>
#include <assert.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include "../../src/repl.h"
EM_JS(void, web_writer, (int error, int handle, const unsigned char* str, int size), {
window.playground_write(handle, !!error, UTF8ToString(str, size));
});
static bool
web_fmt_write_ok(void *opaque, const unsigned char *buf, size_t len)
{
web_writer(false, (int)opaque, buf, len);
return true;
}
static bool
web_fmt_write_err(void *opaque, const unsigned char *buf, size_t len)
{
web_writer(true, (int)opaque, buf, len);
return true;
}
repl
repl_new_for_playground(void)
{
repl r = repl_new(apfl_allocator_default());
if (r == NULL) {
return NULL;
}
repl_set_w_out(r, (struct apfl_io_writer){.write = web_fmt_write_ok, .opaque = (void *)r});
repl_set_w_err(r, (struct apfl_io_writer){.write = web_fmt_write_err, .opaque = (void *)r});
return r;
}