-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbindings.cpp
More file actions
49 lines (42 loc) · 1.59 KB
/
Copy pathbindings.cpp
File metadata and controls
49 lines (42 loc) · 1.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// Browser/WASM entry point for the CuffScript engine.
// Compiled by `make wasm` (see Makefile). Not part of the native `cuffc` build.
//
// print()/input() inside the interpreter talk to std::cout/std::cin as usual
// (see engine/interpreter/NativeFunctions.h); the Emscripten runtime forwards
// those through the `print` / `stdin` callbacks the host page passes in when
// instantiating the module, so no engine code changes were needed here.
#include <emscripten/bind.h>
#include "../engine/CuffEngine.h"
using namespace emscripten;
struct RunOutcome
{
bool success;
std::string error;
};
static RunOutcome toOutcome(const cuff::CuffEngine::Result &result)
{
return RunOutcome{result.success, result.error};
}
// Parses and executes `source`. `scriptDir` resolves relative
// `use <name> from <path>` imports against the virtual filesystem
// (see FS.mkdirTree/writeFile on the JS side).
RunOutcome cuffRun(const std::string &source, const std::string &scriptDir)
{
return toOutcome(cuff::CuffEngine::execute(source, scriptDir));
}
// Parses `source` only and writes the token/AST dump to stdout, mirroring
// `cuffc --ast`. Used by the IDE's "AST 보기" panel.
RunOutcome cuffDump(const std::string &source)
{
cuff::CuffEngine::Result result = cuff::CuffEngine::run(source);
cuff::CuffEngine::debugDump(result);
return toOutcome(result);
}
EMSCRIPTEN_BINDINGS(cuffscript_wasm)
{
value_object<RunOutcome>("RunOutcome")
.field("success", &RunOutcome::success)
.field("error", &RunOutcome::error);
function("cuffRun", &cuffRun);
function("cuffDump", &cuffDump);
}