2021-05-12 18:57:35 +08:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2021 Thakee Nathees
|
|
|
|
* Licensed under: MIT License
|
|
|
|
*/
|
|
|
|
|
|
|
|
// var rs = Module.cwrap('runSource', 'number', ['string'])
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include <emscripten.h>
|
|
|
|
#include "pocketlang.h"
|
|
|
|
|
2021-05-14 18:44:57 +08:00
|
|
|
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);
|
|
|
|
extern const char* js_loadScript();
|
|
|
|
|
|
|
|
void errorPrint(PKVM* vm, PKErrorType type, const char* file, int line,
|
|
|
|
const char* message) {
|
2021-05-14 18:44:57 +08:00
|
|
|
// No need to pass 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);
|
|
|
|
}
|
|
|
|
|
2021-05-18 19:21:38 +08:00
|
|
|
pkStringPtr resolvePath(PKVM* vm, const char* from, const char* name) {
|
|
|
|
pkStringPtr result;
|
|
|
|
result.string = NULL;
|
2021-05-12 18:57:35 +08:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-05-18 19:21:38 +08:00
|
|
|
pkStringPtr loadScript(PKVM* vm, const char* path) {
|
|
|
|
pkStringPtr result;
|
|
|
|
result.string = NULL;
|
2021-05-12 18:57:35 +08:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
EMSCRIPTEN_KEEPALIVE
|
|
|
|
int runSource(const char* source) {
|
|
|
|
|
2021-05-19 02:59:09 +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 = loadScript;
|
|
|
|
config.resolve_path_fn = resolvePath;
|
|
|
|
|
|
|
|
PKVM* vm = pkNewVM(&config);
|
2021-05-18 19:21:38 +08:00
|
|
|
PKInterpretResult result = pkInterpretSource(vm, source, "@try");
|
2021-05-15 17:29:44 +08:00
|
|
|
|
|
|
|
pkFreeVM(vm);
|
2021-05-12 18:57:35 +08:00
|
|
|
|
|
|
|
return (int)result;
|
|
|
|
}
|