Repository navigation
Expand file tree
/
Copy pathjsr.cpp
More file actions
150 lines (124 loc) · 4.28 KB
/
Copy pathjsr.cpp
File metadata and controls
150 lines (124 loc) · 4.28 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#include "jsr.h"
#ifdef __ANDROID__
#include "File.h"
#include "NativeScriptAssert.h"
#include "bytecode_container.h"
#endif
#include "quicks-runtime.h"
JSR::JSR() = default;
tns::ConcurrentMap<napi_env, JSR *> JSR::env_to_jsr_cache;
// Engine-agnostic runtime handle for the jsr layer. QuickJS keeps its own
// napi_runtime (the real engine runtime defined in quickjs-api.c); this wrapper
// just points at it so the jsr API can speak jsr_ns_runtime while
// quickjs-api.c / quicks-runtime.h stay unchanged.
struct jsr_ns_runtime__ {
napi_runtime rt;
};
napi_status js_create_runtime(jsr_ns_runtime* runtime) {
if (!runtime) return napi_invalid_arg;
auto* wrapper = new jsr_ns_runtime__();
napi_status status = qjs_create_runtime(&wrapper->rt);
if (status != napi_ok) {
delete wrapper;
return status;
}
*runtime = wrapper;
return napi_ok;
}
napi_status js_create_napi_env(napi_env* env, jsr_ns_runtime runtime) {
napi_status status = qjs_create_napi_env(env, runtime->rt);
JSR::env_to_jsr_cache.Insert((*env), new JSR());
return status;
}
napi_status js_set_runtime_flags(const char *flags) {
return napi_ok;
}
napi_status js_lock_env(napi_env env) {
auto jsr = JSR::env_to_jsr_cache.Get(env);
if (jsr) jsr->lock();
return napi_ok;
}
napi_status js_unlock_env(napi_env env) {
auto jsr = JSR::env_to_jsr_cache.Get(env);
if (jsr) jsr->unlock();
return napi_ok;
}
napi_status js_free_napi_env(napi_env env) {
JSR* jsr = JSR::env_to_jsr_cache.Get(env);
#ifndef __ANDROID__
// Implemented by the Apple thread-safe-function layer; Android does not
// provide environment cleanup hooks for this backend.
js_run_env_cleanup_hooks(env);
#endif
JSR::env_to_jsr_cache.Remove(env);
delete jsr;
return qjs_free_napi_env(env);
}
napi_status js_free_runtime(jsr_ns_runtime runtime) {
napi_status status = qjs_free_runtime(runtime->rt);
delete runtime;
return status;
}
#ifdef __ANDROID__
#ifdef __QUICKJS_NG__
static const char* kBytecodeMagic = "NSBCNGS"; // 7 chars + NUL = 8-byte magic
static const char* kEngineName = "QuickJS-NG";
#else
static const char* kBytecodeMagic = "NSBCQJS";
static const char* kEngineName = "QuickJS";
#endif
napi_status js_run_bytecode_file(napi_env env, const char* file,
napi_value* result) {
std::string path;
if (!nsbc::ResolvePath(file, path)) {
DEBUG_WRITE("[bytecode] Unable to resolve file: %s", path.c_str());
return napi_cannot_run_js;
}
if (!nsbc::HasMagic(path, kBytecodeMagic)) {
DEBUG_WRITE("[bytecode] Unable to find %s header: %s", kEngineName,
path.c_str());
return napi_cannot_run_js;
}
int length = 0;
auto data = tns::File::ReadBinary(path, length);
if (!data) return napi_cannot_run_js;
if (static_cast<size_t>(length) <= nsbc::kHeaderLen) {
delete[] static_cast<uint8_t*>(data);
return napi_cannot_run_js;
}
DEBUG_WRITE("[bytecode] loading %s bytecode: %s (%d bytes)", kEngineName,
file, length);
// JS_ReadObject copies what it needs, so the buffer can be freed after.
napi_status status = qjs_run_bytecode(
env, static_cast<const uint8_t*>(data) + nsbc::kHeaderLen,
static_cast<size_t>(length) - nsbc::kHeaderLen, file, result);
delete[] static_cast<uint8_t*>(data);
return status;
}
#endif // __ANDROID__
napi_status js_execute_script(napi_env env, napi_value script, const char* file,
napi_value* result) {
#ifdef __ANDROID__
DEBUG_WRITE("[script] loading script: %s", file);
#endif
return qjs_execute_script(env, script, file, result);
}
napi_status js_execute_pending_jobs(napi_env env) {
return qjs_execute_pending_jobs(env);
}
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, "QuickJS", NAPI_AUTO_LENGTH, version);
return napi_ok;
}