-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathAppRuntime_QuickJS.cpp
More file actions
55 lines (47 loc) · 1.32 KB
/
AppRuntime_QuickJS.cpp
File metadata and controls
55 lines (47 loc) · 1.32 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
50
51
52
53
54
55
#include "AppRuntime.h"
#include <napi/env.h>
#ifdef _WIN32
#pragma warning(push)
// cast from int64 to int32
#pragma warning(disable : 4244)
#endif
#include <quickjs.h>
#ifdef _WIN32
#pragma warning(pop)
#endif
namespace Babylon
{
void AppRuntime::RunEnvironmentTier(const char* /*executablePath*/)
{
// Create the runtime.
JSRuntime* runtime = JS_NewRuntime();
if (!runtime)
{
throw std::runtime_error{"Failed to create QuickJS runtime"};
}
// Create the context.
JSContext* context = JS_NewContext(runtime);
if (!context)
{
JS_FreeRuntime(runtime);
throw std::runtime_error{"Failed to create QuickJS context"};
}
// Use the context within a scope.
{
Napi::Env env = Napi::Attach(context);
// Install microtask processing as a post-tick callback
SetPostTickCallback([runtime]() {
JSContext* pending_ctx;
while (JS_ExecutePendingJob(runtime, &pending_ctx) > 0)
{
}
});
Run(env);
SetPostTickCallback(nullptr);
Napi::Detach(env);
}
// Destroy the context and runtime.
JS_FreeContext(context);
JS_FreeRuntime(runtime);
}
}