@lucent-lang/lucent 0.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/app.plugin.js +92 -0
- package/bin/lucent.cjs +16 -0
- package/core/index.cjs +11 -0
- package/dist/bench-CR3E7e2e.js +304 -0
- package/dist/build-CAIrRSYA.js +7 -0
- package/dist/build-check-QozuNJm5.js +92 -0
- package/dist/check-1SCmYlqA.js +7 -0
- package/dist/clean-BVwpbJ5K.js +35 -0
- package/dist/cli.js +406 -0
- package/dist/codes-CIuwXt1S.js +260 -0
- package/dist/compiler.js +4 -0
- package/dist/confirm-BYF5eY3q.js +34 -0
- package/dist/dashboard-B-NzjjaN.js +91 -0
- package/dist/dev-BYWMP7oC.js +221 -0
- package/dist/diagnostic-BM9sXmwl.js +30 -0
- package/dist/diff-BTDo3Sro.js +44 -0
- package/dist/doctor-C0PrfWT6.js +230 -0
- package/dist/doctor-DdR4JnVc.js +30 -0
- package/dist/explain--vV1oaRl.js +35 -0
- package/dist/format-D9fnIMEx.js +53 -0
- package/dist/init-gIabu1S_.js +207 -0
- package/dist/live-steps-BGcWFM9M.js +79 -0
- package/dist/new-module-C2mtb0op.js +66 -0
- package/dist/package-manager-BnxwCySi.js +30 -0
- package/dist/package.json +3 -0
- package/dist/packages-HyskKIsY.js +150 -0
- package/dist/pipeline-CH7-2cxu.js +232 -0
- package/dist/project-D8tEet0k.js +150 -0
- package/dist/sdk-coverage-BoW_3Efd.js +64 -0
- package/dist/sdk-prefetch-mo04JDZV.js +89 -0
- package/dist/sdk-search-Iv4K3-8a.js +124 -0
- package/dist/sdk-show-CkrdlqlP.js +74 -0
- package/dist/sdks-Xoyiy2E4.js +28 -0
- package/dist/src-BD7f1_aZ.js +11493 -0
- package/dist/steps--_h-FFS7.js +24 -0
- package/dist/theme-uv5bc0aK.js +134 -0
- package/dist/tsconfig-S4uyLNzf.js +59 -0
- package/dist/version-bqpIO5sP.js +26 -0
- package/gradle/lucent-classpath.gradle +38 -0
- package/gradle/lucent-classpath.init.gradle +6 -0
- package/gradle/lucent.gradle +24 -0
- package/lib/core.cjs +106 -0
- package/lib/globals.d.ts +36 -0
- package/lib/sdk/android.d.ts +8 -0
- package/lib/sdk/core.d.ts +23 -0
- package/lib/sdk/ios.d.ts +33 -0
- package/lib/sdk/platform.d.ts +8 -0
- package/lib/sdk/thread.d.ts +8 -0
- package/metro/index.cjs +72 -0
- package/metro/index.d.ts +6 -0
- package/metro/transformer.cjs +70 -0
- package/package.json +61 -0
- package/runtime/cpp/lucent/abort.cpp +61 -0
- package/runtime/cpp/lucent/abort.h +54 -0
- package/runtime/cpp/lucent/array.h +442 -0
- package/runtime/cpp/lucent/async.h +236 -0
- package/runtime/cpp/lucent/builtins.cpp +163 -0
- package/runtime/cpp/lucent/bytes.h +129 -0
- package/runtime/cpp/lucent/console.h +17 -0
- package/runtime/cpp/lucent/core.h +129 -0
- package/runtime/cpp/lucent/date.cpp +441 -0
- package/runtime/cpp/lucent/date.h +82 -0
- package/runtime/cpp/lucent/equality.h +87 -0
- package/runtime/cpp/lucent/function.h +61 -0
- package/runtime/cpp/lucent/generator.h +244 -0
- package/runtime/cpp/lucent/helpers.h +226 -0
- package/runtime/cpp/lucent/jserror.cpp +51 -0
- package/runtime/cpp/lucent/jserror.h +66 -0
- package/runtime/cpp/lucent/jsi/convert.cpp +202 -0
- package/runtime/cpp/lucent/jsi/convert.h +612 -0
- package/runtime/cpp/lucent/jsi/host.cpp +322 -0
- package/runtime/cpp/lucent/jsi/host.h +166 -0
- package/runtime/cpp/lucent/json.cpp +243 -0
- package/runtime/cpp/lucent/json.h +164 -0
- package/runtime/cpp/lucent/json_parse.h +138 -0
- package/runtime/cpp/lucent/jsstring.cpp +874 -0
- package/runtime/cpp/lucent/jsstring.h +152 -0
- package/runtime/cpp/lucent/lucent.h +24 -0
- package/runtime/cpp/lucent/map.h +344 -0
- package/runtime/cpp/lucent/native.cpp +76 -0
- package/runtime/cpp/lucent/native.h +116 -0
- package/runtime/cpp/lucent/number.cpp +545 -0
- package/runtime/cpp/lucent/number.h +190 -0
- package/runtime/cpp/lucent/ops.h +165 -0
- package/runtime/cpp/lucent/platform/android.cpp +437 -0
- package/runtime/cpp/lucent/platform/android.h +117 -0
- package/runtime/cpp/lucent/platform/ios.h +297 -0
- package/runtime/cpp/lucent/platform/ios.mm +31 -0
- package/runtime/cpp/lucent/regexp.cpp +440 -0
- package/runtime/cpp/lucent/regexp.h +107 -0
- package/runtime/cpp/lucent/scheduler.cpp +153 -0
- package/runtime/cpp/lucent/scheduler.h +160 -0
- package/runtime/cpp/lucent/unicode_data.inc +4051 -0
- package/runtime/cpp/rn/LucentModule.cpp +35 -0
- package/runtime/cpp/rn/LucentModule.h +37 -0
- package/runtime/cpp/third_party/quickjs/LICENSE +22 -0
- package/runtime/cpp/third_party/quickjs/README.md +11 -0
- package/runtime/cpp/third_party/quickjs/cutils.c +641 -0
- package/runtime/cpp/third_party/quickjs/cutils.h +457 -0
- package/runtime/cpp/third_party/quickjs/libregexp-opcode.h +73 -0
- package/runtime/cpp/third_party/quickjs/libregexp.c +3448 -0
- package/runtime/cpp/third_party/quickjs/libregexp.h +65 -0
- package/runtime/cpp/third_party/quickjs/libunicode-table.h +5206 -0
- package/runtime/cpp/third_party/quickjs/libunicode.c +2124 -0
- package/runtime/cpp/third_party/quickjs/libunicode.h +196 -0
- package/runtime/js/index.d.ts +7 -0
- package/runtime/js/index.js +60 -0
- package/runtime/native/LucentNative.podspec +28 -0
- package/runtime/native/android/CMakeLists.txt +51 -0
- package/runtime/native/android/build.gradle +22 -0
- package/runtime/native/android/include/lucentnative.h +16 -0
- package/runtime/native/android/src/main/java/dev/lucent/LucentPackage.java +25 -0
- package/runtime/native/android/src/main/java/dev/lucent/NativeProxy.java +132 -0
- package/runtime/native/ios/LucentRegistration.mm +23 -0
- package/runtime/native/package.json +6 -0
- package/runtime/native/react-native.config.js +19 -0
- package/runtime/test/jsi/harness.cpp +181 -0
- package/schemas/build.schema.json +152 -0
- package/schemas/check.schema.json +74 -0
- package/schemas/doctor.schema.json +40 -0
- package/schemas/lucent.schema.json +43 -0
- package/schemas/sdk-prefetch.schema.json +37 -0
- package/schemas/sdk-search.schema.json +54 -0
- package/ts-plugin/index.d.ts +4 -0
- package/ts-plugin/index.js +99 -0
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
// Lucent runtime — execution model.
|
|
2
|
+
//
|
|
3
|
+
// Lucent code runs one piece at a time, like JavaScript: every entry into
|
|
4
|
+
// Lucent code (a synchronous call from the JS thread, or a job on the Lucent
|
|
5
|
+
// thread) holds the Lucent lock. Async functions run on the Lucent thread and
|
|
6
|
+
// interleave only at `await`, so Lucent code never races with itself.
|
|
7
|
+
//
|
|
8
|
+
// Invariant: the Lucent thread never waits for the JS thread, so the JS
|
|
9
|
+
// thread may always block on the Lucent lock without deadlocking.
|
|
10
|
+
#pragma once
|
|
11
|
+
|
|
12
|
+
#include <atomic>
|
|
13
|
+
#include <chrono>
|
|
14
|
+
#include <condition_variable>
|
|
15
|
+
#include <cstdint>
|
|
16
|
+
#include <deque>
|
|
17
|
+
#include <exception>
|
|
18
|
+
#include <functional>
|
|
19
|
+
#include <mutex>
|
|
20
|
+
#include <queue>
|
|
21
|
+
#include <thread>
|
|
22
|
+
#include <vector>
|
|
23
|
+
|
|
24
|
+
namespace lucent {
|
|
25
|
+
|
|
26
|
+
/// The one place errors no Lucent code can catch are reported: thrown by a
|
|
27
|
+
/// job, or by a callback the platform made.
|
|
28
|
+
void reportUncaught(std::exception_ptr e, const char* where);
|
|
29
|
+
/// Logs an error where the platform shows an app's errors (logcat, the
|
|
30
|
+
/// unified log) and to stderr.
|
|
31
|
+
void logError(const char* message);
|
|
32
|
+
|
|
33
|
+
/// The Lucent lock: recursive, as a Lucent call may call back into
|
|
34
|
+
/// JavaScript that calls Lucent again. Every synchronous call from
|
|
35
|
+
/// JavaScript takes it, so the uncontended path is one compare-and-swap;
|
|
36
|
+
/// std::recursive_mutex (a pthread mutex) cost a third of a call. Waiters
|
|
37
|
+
/// block in atomic::wait (a futex, or __ulock on Apple platforms).
|
|
38
|
+
class LucentLock {
|
|
39
|
+
public:
|
|
40
|
+
void lock() {
|
|
41
|
+
std::thread::id self = std::this_thread::get_id();
|
|
42
|
+
// Only this thread stores its own id, so a relaxed read that sees it is
|
|
43
|
+
// exact; any other value means another owner or none.
|
|
44
|
+
if (owner_.load(std::memory_order_relaxed) == self) {
|
|
45
|
+
depth_++;
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
uint32_t c = kFree;
|
|
49
|
+
if (!state_.compare_exchange_strong(c, kLocked, std::memory_order_acquire, std::memory_order_relaxed)) {
|
|
50
|
+
if (c != kContended) c = state_.exchange(kContended, std::memory_order_acquire);
|
|
51
|
+
while (c != kFree) {
|
|
52
|
+
state_.wait(kContended, std::memory_order_relaxed);
|
|
53
|
+
c = state_.exchange(kContended, std::memory_order_acquire);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
owner_.store(self, std::memory_order_relaxed);
|
|
57
|
+
depth_ = 1;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
bool try_lock() {
|
|
61
|
+
std::thread::id self = std::this_thread::get_id();
|
|
62
|
+
if (owner_.load(std::memory_order_relaxed) == self) {
|
|
63
|
+
depth_++;
|
|
64
|
+
return true;
|
|
65
|
+
}
|
|
66
|
+
uint32_t c = kFree;
|
|
67
|
+
if (!state_.compare_exchange_strong(c, kLocked, std::memory_order_acquire, std::memory_order_relaxed)) return false;
|
|
68
|
+
owner_.store(self, std::memory_order_relaxed);
|
|
69
|
+
depth_ = 1;
|
|
70
|
+
return true;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
void unlock() {
|
|
74
|
+
if (--depth_ > 0) return;
|
|
75
|
+
owner_.store(std::thread::id(), std::memory_order_relaxed);
|
|
76
|
+
if (state_.exchange(kFree, std::memory_order_release) == kContended) state_.notify_one();
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
private:
|
|
80
|
+
static constexpr uint32_t kFree = 0, kLocked = 1, kContended = 2;
|
|
81
|
+
std::atomic<uint32_t> state_{kFree};
|
|
82
|
+
std::atomic<std::thread::id> owner_{};
|
|
83
|
+
unsigned depth_ = 0;
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
class Scheduler {
|
|
87
|
+
public:
|
|
88
|
+
using Job = std::function<void()>;
|
|
89
|
+
|
|
90
|
+
static Scheduler& instance() {
|
|
91
|
+
// Leaked on purpose: jobs may still reference the scheduler during
|
|
92
|
+
// static destruction at process exit.
|
|
93
|
+
static Scheduler* s = new Scheduler();
|
|
94
|
+
return *s;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/// Static, like the pending count: a call from JavaScript reaches both
|
|
98
|
+
/// without the scheduler's function-local static.
|
|
99
|
+
static LucentLock& lock() { return lock_; }
|
|
100
|
+
|
|
101
|
+
/// Runs `job` on the Lucent thread, holding the lock, then drains
|
|
102
|
+
/// microtasks.
|
|
103
|
+
void post(Job job);
|
|
104
|
+
void postDelayed(double ms, Job job);
|
|
105
|
+
/// Queues a promise continuation. Callers hold the lock.
|
|
106
|
+
void enqueueMicrotask(Job job);
|
|
107
|
+
/// Runs queued microtasks. Callers hold the lock.
|
|
108
|
+
void drainMicrotasks();
|
|
109
|
+
static bool hasMicrotasks() { return pendingMicrotasks_ > 0; }
|
|
110
|
+
|
|
111
|
+
bool onLucentThread() const { return std::this_thread::get_id() == threadId_; }
|
|
112
|
+
/// Jobs queued or running, and timers pending (for tests and shutdown).
|
|
113
|
+
size_t pendingWork();
|
|
114
|
+
/// Blocks until no jobs or timers are pending, or the timeout elapses.
|
|
115
|
+
bool waitIdle(double timeoutMs);
|
|
116
|
+
|
|
117
|
+
private:
|
|
118
|
+
Scheduler();
|
|
119
|
+
~Scheduler();
|
|
120
|
+
void run();
|
|
121
|
+
|
|
122
|
+
struct Timer {
|
|
123
|
+
std::chrono::steady_clock::time_point at;
|
|
124
|
+
uint64_t seq;
|
|
125
|
+
Job job;
|
|
126
|
+
bool operator>(const Timer& o) const { return at != o.at ? at > o.at : seq > o.seq; }
|
|
127
|
+
};
|
|
128
|
+
|
|
129
|
+
static inline LucentLock lock_;
|
|
130
|
+
std::mutex queueMutex_;
|
|
131
|
+
std::condition_variable cv_;
|
|
132
|
+
std::condition_variable idleCv_;
|
|
133
|
+
std::deque<Job> jobs_;
|
|
134
|
+
std::priority_queue<Timer, std::vector<Timer>, std::greater<Timer>> timers_;
|
|
135
|
+
uint64_t timerSeq_ = 0;
|
|
136
|
+
size_t running_ = 0;
|
|
137
|
+
std::deque<Job> microtasks_;
|
|
138
|
+
static inline size_t pendingMicrotasks_ = 0; // microtasks_.size(), under the lock
|
|
139
|
+
bool stopping_ = false;
|
|
140
|
+
std::thread thread_;
|
|
141
|
+
std::thread::id threadId_;
|
|
142
|
+
};
|
|
143
|
+
|
|
144
|
+
/// Entered for every call from JavaScript into Lucent code. Holds the Lucent
|
|
145
|
+
/// lock; on exit, hands pending microtasks to the Lucent thread.
|
|
146
|
+
class LucentScope {
|
|
147
|
+
public:
|
|
148
|
+
LucentScope() : guard_(Scheduler::lock()) {}
|
|
149
|
+
~LucentScope() {
|
|
150
|
+
if (Scheduler::hasMicrotasks()) handOffMicrotasks();
|
|
151
|
+
}
|
|
152
|
+
LucentScope(const LucentScope&) = delete;
|
|
153
|
+
LucentScope& operator=(const LucentScope&) = delete;
|
|
154
|
+
|
|
155
|
+
private:
|
|
156
|
+
static void handOffMicrotasks();
|
|
157
|
+
std::unique_lock<LucentLock> guard_;
|
|
158
|
+
};
|
|
159
|
+
|
|
160
|
+
} // namespace lucent
|