pocketlang/docs/wasm/main.c

44 lines
1.1 KiB
C
Raw Normal View History

2021-05-12 18:57:35 +08:00
/*
* Copyright (c) 2021 Thakee Nathees
* Licensed under: MIT License
*/
#include <emscripten.h>
#include <pocketlang.h>
#include <string.h>
2021-05-12 18:57:35 +08:00
// IO api functions.
extern void js_errorPrint(int type, int line, const char* message);
2021-05-12 18:57:35 +08:00
extern void js_writeFunction(const char* message);
void errorPrint(PKVM* vm, PkErrorType type, const char* file, int line,
2021-05-12 18:57:35 +08:00
const char* message) {
// No need to pass the file (since there is only script that'll ever run on
// the browser.
js_errorPrint((int)type, line, message);
2021-05-12 18:57:35 +08:00
}
void writeFunction(PKVM* vm, const char* text) {
js_writeFunction(text);
}
EMSCRIPTEN_KEEPALIVE
int runSource(const char* source) {
2021-06-23 13:21:18 +08:00
2021-06-04 22:55:06 +08:00
PkConfiguration config = pkNewConfiguration();
2021-05-12 18:57:35 +08:00
config.error_fn = errorPrint;
config.write_fn = writeFunction;
config.load_script_fn = NULL;
config.resolve_path_fn = NULL;
2021-05-12 18:57:35 +08:00
PKVM* vm = pkNewVM(&config);
2021-06-23 13:21:18 +08:00
PkStringPtr src = { source, NULL, NULL };
PkStringPtr module = { "$(TRY)", NULL, NULL };
2021-06-13 13:33:18 +08:00
PkResult result = pkInterpretSource(vm, src, module, NULL);
pkFreeVM(vm);
2021-05-12 18:57:35 +08:00
return (int)result;
}