2021-05-12 18:57:35 +08:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2021 Thakee Nathees
|
|
|
|
* Licensed under: MIT License
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <emscripten.h>
|
2021-06-04 09:28:42 +08:00
|
|
|
#include <pocketlang.h>
|
|
|
|
#include <string.h>
|
2021-05-12 18:57:35 +08:00
|
|
|
|
2021-06-04 09:28:42 +08:00
|
|
|
// IO api functions.
|
2022-04-22 20:21:17 +08:00
|
|
|
extern void js_errorPrint(const char* message);
|
2021-05-12 18:57:35 +08:00
|
|
|
extern void js_writeFunction(const char* message);
|
|
|
|
|
2022-04-22 20:21:17 +08:00
|
|
|
void errorPrint(PKVM* vm, const char* message) {
|
2021-06-04 09:28:42 +08:00
|
|
|
// No need to pass the file (since there is only script that'll ever run on
|
|
|
|
// the browser.
|
2022-04-22 20:21:17 +08:00
|
|
|
js_errorPrint(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();
|
2022-04-22 20:21:17 +08:00
|
|
|
config.stderr_write = errorPrint;
|
|
|
|
config.stdout_write = writeFunction;
|
2021-06-20 08:49:35 +08:00
|
|
|
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
|
|
|
|
2022-04-22 20:21:17 +08:00
|
|
|
// FIXME:
|
|
|
|
// The bellow blocks of code can be simplified with
|
|
|
|
//
|
|
|
|
// PkResult result = pkInterpretSource(vm, src, path, NULL);
|
|
|
|
//
|
|
|
|
// But the path is printed on the error messages as "@(TRY)"
|
|
|
|
// If we create a module and compile it, then it won't have a
|
|
|
|
// path and the error message is simplified. This behavior needs
|
|
|
|
// to be changed in the error report function.
|
|
|
|
|
|
|
|
PkResult result;
|
|
|
|
|
|
|
|
PkHandle* module = pkNewModule(vm, "@(TRY)");
|
|
|
|
do {
|
|
|
|
PkStringPtr src = { .string = source };
|
|
|
|
result = pkCompileModule(vm, module, src, NULL);
|
|
|
|
if (result != PK_RESULT_SUCCESS) break;
|
|
|
|
|
|
|
|
PkHandle* _main = pkModuleGetMainFunction(vm, module);
|
2022-04-27 11:16:00 +08:00
|
|
|
result = pkRunFunction(vm, _main, 0, -1, -1);
|
2022-04-22 20:21:17 +08:00
|
|
|
pkReleaseHandle(vm, _main);
|
|
|
|
|
|
|
|
} while (false);
|
|
|
|
pkReleaseHandle(vm, module);
|
2021-05-15 17:29:44 +08:00
|
|
|
|
|
|
|
pkFreeVM(vm);
|
2021-05-12 18:57:35 +08:00
|
|
|
|
|
|
|
return (int)result;
|
|
|
|
}
|