-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathjsr.cpp
More file actions
116 lines (92 loc) · 3.54 KB
/
jsr.cpp
File metadata and controls
116 lines (92 loc) · 3.54 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#include "jsr.h"
#include "js_runtime.h"
using namespace facebook::jsi;
std::unordered_map<napi_env, JSR *> JSR::env_to_jsr_cache;
JSR::JSR() {
hermes::vm::RuntimeConfig config =
hermes::vm::RuntimeConfig::Builder().withMicrotaskQueue(true).withES6Class(
true).withES6Promise(true).withArrayBuffer(true).withEnableEval(true).build();
threadSafeRuntime = facebook::hermes::makeThreadSafeHermesRuntime(config);
facebook::jsi::Function abc = facebook::jsi::Function::createFromHostFunction(
threadSafeRuntime->getUnsafeRuntime(),
facebook::jsi::PropNameID::forAscii(threadSafeRuntime->getUnsafeRuntime(),
"directFunction"), 0,
[](Runtime &rt, const Value &thisVal, const Value *args, size_t count) -> Value {
return Value::undefined();
});
threadSafeRuntime->getUnsafeRuntime().global().setProperty(
threadSafeRuntime->getUnsafeRuntime(), "directFunction", abc);
rt = (facebook::hermes::HermesRuntime *) &threadSafeRuntime->getUnsafeRuntime();
}
napi_status js_create_runtime(napi_runtime *runtime) {
if (runtime == nullptr) return napi_invalid_arg;
*runtime = new napi_runtime__();
(*runtime)->hermes = new JSR();
return napi_ok;
}
napi_status js_lock_env(napi_env env) {
auto itFound = JSR::env_to_jsr_cache.find(env);
if (itFound == JSR::env_to_jsr_cache.end()) {
return napi_invalid_arg;
}
itFound->second->lock();
return napi_ok;
}
napi_status js_unlock_env(napi_env env) {
auto itFound = JSR::env_to_jsr_cache.find(env);
if (itFound == JSR::env_to_jsr_cache.end()) {
return napi_invalid_arg;
}
itFound->second->unlock();
return napi_ok;
}
napi_status js_create_napi_env(napi_env *env, napi_runtime runtime) {
if (env == nullptr) return napi_invalid_arg;
*env = (napi_env)runtime->hermes->rt->createNodeApiEnv(9);
JSR::env_to_jsr_cache.insert(std::make_pair(*env, runtime->hermes));
return napi_ok;
}
napi_status js_set_runtime_flags(const char *flags) {
return napi_ok;
}
napi_status js_free_napi_env(napi_env env) {
JSR::env_to_jsr_cache.erase(env);
return napi_ok;
}
napi_status js_free_runtime(napi_runtime runtime) {
if (runtime == nullptr) return napi_invalid_arg;
runtime->hermes->threadSafeRuntime.reset();
runtime->hermes->rt = nullptr;
delete runtime->hermes;
delete runtime;
return napi_ok;
}
napi_status js_execute_script(napi_env env,
napi_value script,
const char *file,
napi_value *result) {
return jsr_run_script(env, script, file, result);
}
napi_status js_execute_pending_jobs(napi_env env) {
bool result;
return jsr_drain_microtasks(env, 0, &result);
}
napi_status js_get_engine_ptr(napi_env env, int64_t *engine_ptr) {
return napi_ok;
}
napi_status
js_adjust_external_memory(napi_env env, int64_t changeInBytes, int64_t *externalMemory) {
napi_adjust_external_memory(env, changeInBytes, externalMemory);
return napi_ok;
}
napi_status js_cache_script(napi_env env, const char *source, const char *file) {
return napi_ok;
}
napi_status js_run_cached_script(napi_env env, const char *file, napi_value script, void *cache,
napi_value *result) {
return napi_ok;
}
napi_status js_get_runtime_version(napi_env env, napi_value *version) {
napi_create_string_utf8(env, "Hermes", NAPI_AUTO_LENGTH, version);
return napi_ok;
}