-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathjsr.h
More file actions
72 lines (63 loc) · 1.82 KB
/
Copy pathjsr.h
File metadata and controls
72 lines (63 loc) · 1.82 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
//
// Created by Ammar Ahmed on 01/12/2024.
//
#ifndef TEST_APP_JSR_H
#define TEST_APP_JSR_H
#include <map>
#include "ConcurrentMap.h"
#include "js_native_api.h"
#include "jsr_common.h"
#include "mutex"
#include "quicks-runtime.h"
class JSR {
public:
JSR();
std::recursive_mutex js_mutex;
// Depth of nested JS scopes entered from the host (see NapiScope). We drain
// the pending-job (microtask) queue once this returns to 0, i.e. when the
// native call stack has fully unwound back out of JS.
int jsEnterState = 0;
void lock() { js_mutex.lock(); }
void unlock() { js_mutex.unlock(); }
static tns::SimpleMap<napi_env, JSR*> env_to_jsr_cache;
};
class NapiScope {
public:
explicit NapiScope(napi_env env, bool open_handle = true) : env_(env) {
js_lock_env(env_);
qjs_update_stack_top(env);
jsr_ = JSR::env_to_jsr_cache.Get(env_);
if (jsr_) {
jsr_->jsEnterState++;
}
if (open_handle) {
napi_open_handle_scope(env_, &napiHandleScope_);
} else {
napiHandleScope_ = nullptr;
}
}
~NapiScope() {
// Drain the microtask queue only when the outermost JS scope unwinds so
// that promise continuations (async/await) run — mirroring how a JS
// engine empties its job queue once control returns to the host.
// Draining at a nested depth would run continuations while JS is still
// on the stack. A throwing job must never escape a destructor.
if (jsr_ && --jsr_->jsEnterState <= 0) {
jsr_->jsEnterState = 0;
try {
js_execute_pending_jobs(env_);
} catch (...) {
}
}
if (napiHandleScope_) {
napi_close_handle_scope(env_, napiHandleScope_);
}
js_unlock_env(env_);
}
private:
napi_env env_;
napi_handle_scope napiHandleScope_;
JSR* jsr_ = nullptr;
};
#define JSEnterScope
#endif // TEST_APP_JSR_H