titanpl 6.0.0 → 7.0.0

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.
Files changed (227) hide show
  1. package/package.json +2 -6
  2. package/packages/cli/index.js +25 -11
  3. package/packages/cli/package.json +4 -4
  4. package/packages/cli/src/commands/build-ext.js +157 -0
  5. package/packages/cli/src/commands/build.js +12 -0
  6. package/packages/cli/src/commands/create.js +160 -0
  7. package/packages/cli/src/commands/init.js +5 -11
  8. package/packages/cli/src/commands/run-ext.js +104 -0
  9. package/{titanpl-sdk → packages/core-source}/LICENSE +1 -1
  10. package/packages/core-source/README.md +128 -0
  11. package/packages/core-source/V8_SERIALIZATION.md +125 -0
  12. package/packages/core-source/configure.js +50 -0
  13. package/packages/core-source/globals.d.ts +2238 -0
  14. package/packages/core-source/index.d.ts +515 -0
  15. package/packages/core-source/index.js +639 -0
  16. package/packages/core-source/jsconfig.json +12 -0
  17. package/packages/core-source/mkctx.config.json +7 -0
  18. package/packages/core-source/native/Cargo.lock +1559 -0
  19. package/packages/core-source/native/Cargo.toml +30 -0
  20. package/packages/core-source/native/src/crypto_impl.rs +139 -0
  21. package/packages/core-source/native/src/lib.rs +702 -0
  22. package/packages/core-source/native/src/storage_impl.rs +73 -0
  23. package/packages/core-source/native/src/v8_impl.rs +93 -0
  24. package/packages/core-source/package-lock.json +1464 -0
  25. package/packages/core-source/package.json +53 -0
  26. package/packages/core-source/tests/buffer.test.js +78 -0
  27. package/packages/core-source/tests/cookies.test.js +117 -0
  28. package/packages/core-source/tests/crypto.test.js +142 -0
  29. package/packages/core-source/tests/fs.test.js +176 -0
  30. package/packages/core-source/tests/ls.test.js +149 -0
  31. package/packages/core-source/tests/net.test.js +84 -0
  32. package/packages/core-source/tests/os.test.js +81 -0
  33. package/packages/core-source/tests/path.test.js +102 -0
  34. package/packages/core-source/tests/response.test.js +146 -0
  35. package/packages/core-source/tests/session.test.js +110 -0
  36. package/packages/core-source/tests/setup.js +325 -0
  37. package/packages/core-source/tests/time.test.js +57 -0
  38. package/packages/core-source/tests/url.test.js +82 -0
  39. package/packages/core-source/titan-ext.d.ts +2 -0
  40. package/packages/core-source/titan.json +9 -0
  41. package/packages/core-source/vitest.config.js +8 -0
  42. package/packages/engine-darwin-arm64/README.md +0 -2
  43. package/packages/engine-darwin-arm64/package.json +1 -1
  44. package/packages/engine-linux-x64/README.md +0 -2
  45. package/packages/engine-linux-x64/package.json +1 -1
  46. package/packages/engine-win32-x64/README.md +0 -1
  47. package/packages/engine-win32-x64/bin/titan-server.exe +0 -0
  48. package/packages/engine-win32-x64/package.json +1 -1
  49. package/packages/native/README.md +0 -1
  50. package/packages/native/index.d.ts +25 -4
  51. package/packages/native/index.js +7 -0
  52. package/packages/native/package.json +2 -2
  53. package/packages/native/t.native.d.ts +167 -2
  54. package/packages/packet/index.js +103 -94
  55. package/packages/packet/package.json +1 -1
  56. package/packages/route/package.json +1 -1
  57. package/packages/sdk/index.js +2 -0
  58. package/packages/sdk/package.json +18 -0
  59. package/packages/sdk/test/index.js +120 -0
  60. package/templates/common/_tanfig.json +19 -13
  61. package/templates/extension/index.d.ts +26 -22
  62. package/templates/extension/index.js +15 -15
  63. package/templates/extension/native/Cargo.toml +5 -3
  64. package/templates/extension/native/src/lib.rs +2 -3
  65. package/templates/extension/package.json +10 -20
  66. package/templates/extension/titan.json +5 -16
  67. package/templates/extension/utils/registerExtension.js +44 -0
  68. package/templates/js/package.json +8 -8
  69. package/templates/rust-js/package.json +5 -5
  70. package/templates/rust-ts/package.json +5 -5
  71. package/templates/ts/package.json +8 -8
  72. package/packages/packet/node_modules/typescript/LICENSE.txt +0 -55
  73. package/packages/packet/node_modules/typescript/README.md +0 -50
  74. package/packages/packet/node_modules/typescript/SECURITY.md +0 -41
  75. package/packages/packet/node_modules/typescript/ThirdPartyNoticeText.txt +0 -193
  76. package/packages/packet/node_modules/typescript/bin/tsc +0 -2
  77. package/packages/packet/node_modules/typescript/bin/tsserver +0 -2
  78. package/packages/packet/node_modules/typescript/lib/_tsc.js +0 -133818
  79. package/packages/packet/node_modules/typescript/lib/_tsserver.js +0 -659
  80. package/packages/packet/node_modules/typescript/lib/_typingsInstaller.js +0 -222
  81. package/packages/packet/node_modules/typescript/lib/cs/diagnosticMessages.generated.json +0 -2122
  82. package/packages/packet/node_modules/typescript/lib/de/diagnosticMessages.generated.json +0 -2122
  83. package/packages/packet/node_modules/typescript/lib/es/diagnosticMessages.generated.json +0 -2122
  84. package/packages/packet/node_modules/typescript/lib/fr/diagnosticMessages.generated.json +0 -2122
  85. package/packages/packet/node_modules/typescript/lib/it/diagnosticMessages.generated.json +0 -2122
  86. package/packages/packet/node_modules/typescript/lib/ja/diagnosticMessages.generated.json +0 -2122
  87. package/packages/packet/node_modules/typescript/lib/ko/diagnosticMessages.generated.json +0 -2122
  88. package/packages/packet/node_modules/typescript/lib/lib.d.ts +0 -22
  89. package/packages/packet/node_modules/typescript/lib/lib.decorators.d.ts +0 -384
  90. package/packages/packet/node_modules/typescript/lib/lib.decorators.legacy.d.ts +0 -22
  91. package/packages/packet/node_modules/typescript/lib/lib.dom.asynciterable.d.ts +0 -41
  92. package/packages/packet/node_modules/typescript/lib/lib.dom.d.ts +0 -39429
  93. package/packages/packet/node_modules/typescript/lib/lib.dom.iterable.d.ts +0 -571
  94. package/packages/packet/node_modules/typescript/lib/lib.es2015.collection.d.ts +0 -147
  95. package/packages/packet/node_modules/typescript/lib/lib.es2015.core.d.ts +0 -597
  96. package/packages/packet/node_modules/typescript/lib/lib.es2015.d.ts +0 -28
  97. package/packages/packet/node_modules/typescript/lib/lib.es2015.generator.d.ts +0 -77
  98. package/packages/packet/node_modules/typescript/lib/lib.es2015.iterable.d.ts +0 -605
  99. package/packages/packet/node_modules/typescript/lib/lib.es2015.promise.d.ts +0 -81
  100. package/packages/packet/node_modules/typescript/lib/lib.es2015.proxy.d.ts +0 -128
  101. package/packages/packet/node_modules/typescript/lib/lib.es2015.reflect.d.ts +0 -144
  102. package/packages/packet/node_modules/typescript/lib/lib.es2015.symbol.d.ts +0 -46
  103. package/packages/packet/node_modules/typescript/lib/lib.es2015.symbol.wellknown.d.ts +0 -326
  104. package/packages/packet/node_modules/typescript/lib/lib.es2016.array.include.d.ts +0 -116
  105. package/packages/packet/node_modules/typescript/lib/lib.es2016.d.ts +0 -21
  106. package/packages/packet/node_modules/typescript/lib/lib.es2016.full.d.ts +0 -23
  107. package/packages/packet/node_modules/typescript/lib/lib.es2016.intl.d.ts +0 -31
  108. package/packages/packet/node_modules/typescript/lib/lib.es2017.arraybuffer.d.ts +0 -21
  109. package/packages/packet/node_modules/typescript/lib/lib.es2017.d.ts +0 -26
  110. package/packages/packet/node_modules/typescript/lib/lib.es2017.date.d.ts +0 -31
  111. package/packages/packet/node_modules/typescript/lib/lib.es2017.full.d.ts +0 -23
  112. package/packages/packet/node_modules/typescript/lib/lib.es2017.intl.d.ts +0 -44
  113. package/packages/packet/node_modules/typescript/lib/lib.es2017.object.d.ts +0 -49
  114. package/packages/packet/node_modules/typescript/lib/lib.es2017.sharedmemory.d.ts +0 -135
  115. package/packages/packet/node_modules/typescript/lib/lib.es2017.string.d.ts +0 -45
  116. package/packages/packet/node_modules/typescript/lib/lib.es2017.typedarrays.d.ts +0 -53
  117. package/packages/packet/node_modules/typescript/lib/lib.es2018.asyncgenerator.d.ts +0 -77
  118. package/packages/packet/node_modules/typescript/lib/lib.es2018.asynciterable.d.ts +0 -53
  119. package/packages/packet/node_modules/typescript/lib/lib.es2018.d.ts +0 -24
  120. package/packages/packet/node_modules/typescript/lib/lib.es2018.full.d.ts +0 -24
  121. package/packages/packet/node_modules/typescript/lib/lib.es2018.intl.d.ts +0 -83
  122. package/packages/packet/node_modules/typescript/lib/lib.es2018.promise.d.ts +0 -30
  123. package/packages/packet/node_modules/typescript/lib/lib.es2018.regexp.d.ts +0 -37
  124. package/packages/packet/node_modules/typescript/lib/lib.es2019.array.d.ts +0 -79
  125. package/packages/packet/node_modules/typescript/lib/lib.es2019.d.ts +0 -24
  126. package/packages/packet/node_modules/typescript/lib/lib.es2019.full.d.ts +0 -24
  127. package/packages/packet/node_modules/typescript/lib/lib.es2019.intl.d.ts +0 -23
  128. package/packages/packet/node_modules/typescript/lib/lib.es2019.object.d.ts +0 -33
  129. package/packages/packet/node_modules/typescript/lib/lib.es2019.string.d.ts +0 -37
  130. package/packages/packet/node_modules/typescript/lib/lib.es2019.symbol.d.ts +0 -24
  131. package/packages/packet/node_modules/typescript/lib/lib.es2020.bigint.d.ts +0 -765
  132. package/packages/packet/node_modules/typescript/lib/lib.es2020.d.ts +0 -27
  133. package/packages/packet/node_modules/typescript/lib/lib.es2020.date.d.ts +0 -42
  134. package/packages/packet/node_modules/typescript/lib/lib.es2020.full.d.ts +0 -24
  135. package/packages/packet/node_modules/typescript/lib/lib.es2020.intl.d.ts +0 -474
  136. package/packages/packet/node_modules/typescript/lib/lib.es2020.number.d.ts +0 -28
  137. package/packages/packet/node_modules/typescript/lib/lib.es2020.promise.d.ts +0 -47
  138. package/packages/packet/node_modules/typescript/lib/lib.es2020.sharedmemory.d.ts +0 -99
  139. package/packages/packet/node_modules/typescript/lib/lib.es2020.string.d.ts +0 -44
  140. package/packages/packet/node_modules/typescript/lib/lib.es2020.symbol.wellknown.d.ts +0 -41
  141. package/packages/packet/node_modules/typescript/lib/lib.es2021.d.ts +0 -23
  142. package/packages/packet/node_modules/typescript/lib/lib.es2021.full.d.ts +0 -24
  143. package/packages/packet/node_modules/typescript/lib/lib.es2021.intl.d.ts +0 -166
  144. package/packages/packet/node_modules/typescript/lib/lib.es2021.promise.d.ts +0 -48
  145. package/packages/packet/node_modules/typescript/lib/lib.es2021.string.d.ts +0 -33
  146. package/packages/packet/node_modules/typescript/lib/lib.es2021.weakref.d.ts +0 -78
  147. package/packages/packet/node_modules/typescript/lib/lib.es2022.array.d.ts +0 -121
  148. package/packages/packet/node_modules/typescript/lib/lib.es2022.d.ts +0 -25
  149. package/packages/packet/node_modules/typescript/lib/lib.es2022.error.d.ts +0 -75
  150. package/packages/packet/node_modules/typescript/lib/lib.es2022.full.d.ts +0 -24
  151. package/packages/packet/node_modules/typescript/lib/lib.es2022.intl.d.ts +0 -145
  152. package/packages/packet/node_modules/typescript/lib/lib.es2022.object.d.ts +0 -26
  153. package/packages/packet/node_modules/typescript/lib/lib.es2022.regexp.d.ts +0 -39
  154. package/packages/packet/node_modules/typescript/lib/lib.es2022.string.d.ts +0 -25
  155. package/packages/packet/node_modules/typescript/lib/lib.es2023.array.d.ts +0 -924
  156. package/packages/packet/node_modules/typescript/lib/lib.es2023.collection.d.ts +0 -21
  157. package/packages/packet/node_modules/typescript/lib/lib.es2023.d.ts +0 -22
  158. package/packages/packet/node_modules/typescript/lib/lib.es2023.full.d.ts +0 -24
  159. package/packages/packet/node_modules/typescript/lib/lib.es2023.intl.d.ts +0 -56
  160. package/packages/packet/node_modules/typescript/lib/lib.es2024.arraybuffer.d.ts +0 -65
  161. package/packages/packet/node_modules/typescript/lib/lib.es2024.collection.d.ts +0 -29
  162. package/packages/packet/node_modules/typescript/lib/lib.es2024.d.ts +0 -26
  163. package/packages/packet/node_modules/typescript/lib/lib.es2024.full.d.ts +0 -24
  164. package/packages/packet/node_modules/typescript/lib/lib.es2024.object.d.ts +0 -29
  165. package/packages/packet/node_modules/typescript/lib/lib.es2024.promise.d.ts +0 -35
  166. package/packages/packet/node_modules/typescript/lib/lib.es2024.regexp.d.ts +0 -25
  167. package/packages/packet/node_modules/typescript/lib/lib.es2024.sharedmemory.d.ts +0 -68
  168. package/packages/packet/node_modules/typescript/lib/lib.es2024.string.d.ts +0 -29
  169. package/packages/packet/node_modules/typescript/lib/lib.es5.d.ts +0 -4601
  170. package/packages/packet/node_modules/typescript/lib/lib.es6.d.ts +0 -23
  171. package/packages/packet/node_modules/typescript/lib/lib.esnext.array.d.ts +0 -35
  172. package/packages/packet/node_modules/typescript/lib/lib.esnext.collection.d.ts +0 -96
  173. package/packages/packet/node_modules/typescript/lib/lib.esnext.d.ts +0 -29
  174. package/packages/packet/node_modules/typescript/lib/lib.esnext.decorators.d.ts +0 -28
  175. package/packages/packet/node_modules/typescript/lib/lib.esnext.disposable.d.ts +0 -193
  176. package/packages/packet/node_modules/typescript/lib/lib.esnext.error.d.ts +0 -24
  177. package/packages/packet/node_modules/typescript/lib/lib.esnext.float16.d.ts +0 -445
  178. package/packages/packet/node_modules/typescript/lib/lib.esnext.full.d.ts +0 -24
  179. package/packages/packet/node_modules/typescript/lib/lib.esnext.intl.d.ts +0 -21
  180. package/packages/packet/node_modules/typescript/lib/lib.esnext.iterator.d.ts +0 -148
  181. package/packages/packet/node_modules/typescript/lib/lib.esnext.promise.d.ts +0 -34
  182. package/packages/packet/node_modules/typescript/lib/lib.esnext.sharedmemory.d.ts +0 -25
  183. package/packages/packet/node_modules/typescript/lib/lib.scripthost.d.ts +0 -322
  184. package/packages/packet/node_modules/typescript/lib/lib.webworker.asynciterable.d.ts +0 -41
  185. package/packages/packet/node_modules/typescript/lib/lib.webworker.d.ts +0 -13150
  186. package/packages/packet/node_modules/typescript/lib/lib.webworker.importscripts.d.ts +0 -23
  187. package/packages/packet/node_modules/typescript/lib/lib.webworker.iterable.d.ts +0 -340
  188. package/packages/packet/node_modules/typescript/lib/pl/diagnosticMessages.generated.json +0 -2122
  189. package/packages/packet/node_modules/typescript/lib/pt-br/diagnosticMessages.generated.json +0 -2122
  190. package/packages/packet/node_modules/typescript/lib/ru/diagnosticMessages.generated.json +0 -2122
  191. package/packages/packet/node_modules/typescript/lib/tr/diagnosticMessages.generated.json +0 -2122
  192. package/packages/packet/node_modules/typescript/lib/tsc.js +0 -8
  193. package/packages/packet/node_modules/typescript/lib/tsserver.js +0 -8
  194. package/packages/packet/node_modules/typescript/lib/tsserverlibrary.d.ts +0 -17
  195. package/packages/packet/node_modules/typescript/lib/tsserverlibrary.js +0 -21
  196. package/packages/packet/node_modules/typescript/lib/typesMap.json +0 -497
  197. package/packages/packet/node_modules/typescript/lib/typescript.d.ts +0 -11437
  198. package/packages/packet/node_modules/typescript/lib/typescript.js +0 -200276
  199. package/packages/packet/node_modules/typescript/lib/typingsInstaller.js +0 -8
  200. package/packages/packet/node_modules/typescript/lib/watchGuard.js +0 -53
  201. package/packages/packet/node_modules/typescript/lib/zh-cn/diagnosticMessages.generated.json +0 -2122
  202. package/packages/packet/node_modules/typescript/lib/zh-tw/diagnosticMessages.generated.json +0 -2122
  203. package/packages/packet/node_modules/typescript/package.json +0 -120
  204. package/titanpl-sdk/README.md +0 -111
  205. package/titanpl-sdk/assets/titanpl-sdk.png +0 -0
  206. package/titanpl-sdk/bin/run.js +0 -274
  207. package/titanpl-sdk/index.js +0 -5
  208. package/titanpl-sdk/package-lock.json +0 -28
  209. package/titanpl-sdk/package.json +0 -40
  210. package/titanpl-sdk/templates/app/actions/hello.js +0 -5
  211. package/titanpl-sdk/templates/app/app.js +0 -7
  212. package/titanpl-sdk/templates/jsconfig.json +0 -19
  213. package/titanpl-sdk/templates/server/Cargo.toml +0 -52
  214. package/titanpl-sdk/templates/server/src/action_management.rs +0 -175
  215. package/titanpl-sdk/templates/server/src/errors.rs +0 -12
  216. package/titanpl-sdk/templates/server/src/extensions/builtin.rs +0 -1060
  217. package/titanpl-sdk/templates/server/src/extensions/external.rs +0 -338
  218. package/titanpl-sdk/templates/server/src/extensions/mod.rs +0 -580
  219. package/titanpl-sdk/templates/server/src/extensions/titan_core.js +0 -249
  220. package/titanpl-sdk/templates/server/src/fast_path.rs +0 -719
  221. package/titanpl-sdk/templates/server/src/main.rs +0 -607
  222. package/titanpl-sdk/templates/server/src/runtime.rs +0 -284
  223. package/titanpl-sdk/templates/server/src/utils.rs +0 -33
  224. package/titanpl-sdk/templates/titan/bundle.js +0 -259
  225. package/titanpl-sdk/templates/titan/dev.js +0 -390
  226. package/titanpl-sdk/templates/titan/error-box.js +0 -277
  227. package/titanpl-sdk/templates/titan/titan.js +0 -129
@@ -1,338 +0,0 @@
1
- //! External native extension loading and FFI.
2
- //!
3
- //! Supports loading `.dll` / `.so` extensions defined in `titan.json` files.
4
-
5
- use v8;
6
- use std::path::PathBuf;
7
- use std::collections::HashMap;
8
- use std::fs;
9
- use std::sync::{Mutex, Arc};
10
- use walkdir::WalkDir;
11
- use libloading::Library;
12
- use crate::utils::{blue, green, red};
13
- use super::{TitanRuntime, v8_str, throw};
14
- use serde_json::Value;
15
-
16
- pub static REGISTRY: Mutex<Option<Registry>> = Mutex::new(None);
17
-
18
- #[allow(dead_code)]
19
- pub struct Registry {
20
- pub _libs: Vec<Library>,
21
- pub modules: Vec<ModuleDef>,
22
- pub natives: Vec<NativeFnEntry>,
23
- }
24
-
25
- #[derive(Clone)]
26
- pub struct ModuleDef {
27
- pub name: String,
28
- pub js: String,
29
- pub native_indices: HashMap<String, usize>,
30
- }
31
-
32
- #[derive(Clone, Debug, PartialEq)]
33
- pub enum ParamType {
34
- String, F64, Bool, Json, Buffer,
35
- }
36
-
37
- #[derive(Clone, Debug, PartialEq)]
38
- pub enum ReturnType {
39
- String, F64, Bool, Json, Buffer, Void,
40
- }
41
-
42
- #[derive(Clone, Debug)]
43
- pub struct Signature {
44
- pub params: Vec<ParamType>,
45
- pub ret: ReturnType,
46
- }
47
-
48
- pub struct NativeFnEntry {
49
- pub symbol_ptr: usize,
50
- pub sig: Signature,
51
- }
52
-
53
- #[derive(serde::Deserialize)]
54
- struct TitanConfig {
55
- name: String,
56
- main: String,
57
- native: Option<TitanNativeConfig>,
58
- }
59
-
60
- #[derive(serde::Deserialize)]
61
- struct TitanNativeConfig {
62
- path: String,
63
- functions: HashMap<String, TitanNativeFunc>,
64
- }
65
-
66
- #[derive(serde::Deserialize)]
67
- struct TitanNativeFunc {
68
- symbol: String,
69
- #[serde(default)]
70
- parameters: Vec<String>,
71
- #[serde(default)]
72
- result: String,
73
- }
74
-
75
- fn parse_type(s: &str) -> ParamType {
76
- match s {
77
- "string" => ParamType::String,
78
- "f64" => ParamType::F64,
79
- "bool" => ParamType::Bool,
80
- "json" => ParamType::Json,
81
- "buffer" => ParamType::Buffer,
82
- _ => ParamType::Json,
83
- }
84
- }
85
-
86
- fn parse_return(s: &str) -> ReturnType {
87
- match s {
88
- "string" => ReturnType::String,
89
- "f64" => ReturnType::F64,
90
- "bool" => ReturnType::Bool,
91
- "json" => ReturnType::Json,
92
- "buffer" => ReturnType::Buffer,
93
- "void" => ReturnType::Void,
94
- _ => ReturnType::Void,
95
- }
96
- }
97
-
98
- pub fn load_project_extensions(root: PathBuf) {
99
- let mut modules = Vec::new();
100
- let mut libs = Vec::new();
101
- let mut all_natives = Vec::new();
102
-
103
- let mut node_modules = root.join("node_modules");
104
- if !node_modules.exists() {
105
- if let Some(parent) = root.parent() {
106
- let parent_modules = parent.join("node_modules");
107
- if parent_modules.exists() { node_modules = parent_modules; }
108
- }
109
- }
110
-
111
- // Generic scanner helper
112
- let scan_dir = |path: PathBuf, modules: &mut Vec<ModuleDef>, libs: &mut Vec<Library>, all_natives: &mut Vec<NativeFnEntry>| {
113
- if !path.exists() { return; }
114
- for entry in WalkDir::new(&path).follow_links(true).min_depth(1).max_depth(4) {
115
- let entry = match entry { Ok(e) => e, Err(_) => continue };
116
- if entry.file_type().is_file() && entry.file_name() == "titan.json" {
117
- let dir = entry.path().parent().unwrap();
118
- let config_content = fs::read_to_string(entry.path()).unwrap_or_default();
119
- let config: TitanConfig = match serde_json::from_str(&config_content) {
120
- Ok(c) => c,
121
- Err(_) => continue,
122
- };
123
- let mut mod_natives_map = HashMap::new();
124
- if let Some(native_conf) = config.native {
125
- let lib_path = dir.join(&native_conf.path);
126
- unsafe {
127
- // Try loading library
128
- let lib_load = Library::new(&lib_path);
129
- // If failed, try resolving relative to current dir or LD_LIBRARY_PATH implicit
130
- // But usually absolute path from `dir` works.
131
- match lib_load {
132
- Ok(lib) => {
133
- for (fn_name, fn_conf) in native_conf.functions {
134
- let params = fn_conf.parameters.iter().map(|p| parse_type(&p.to_lowercase())).collect();
135
- let ret = parse_return(&fn_conf.result.to_lowercase());
136
- if let Ok(symbol) = lib.get::<*const ()>(fn_conf.symbol.as_bytes()) {
137
- let idx = all_natives.len();
138
- all_natives.push(NativeFnEntry { symbol_ptr: *symbol as usize, sig: Signature { params, ret } });
139
- mod_natives_map.insert(fn_name, idx);
140
- } else {
141
- println!("{} {} {} -> {}", blue("[Titan]"), red("Symbol not found:"), fn_conf.symbol, config.name);
142
- }
143
- }
144
- libs.push(lib);
145
- },
146
- Err(e) => {
147
- println!("{} {} {} -> {:?}", blue("[Titan]"), red("Failed to load native lib:"), config.name, e);
148
- }
149
- }
150
- }
151
- }
152
- let js_path = dir.join(&config.main);
153
- modules.push(ModuleDef { name: config.name.clone(), js: fs::read_to_string(js_path).unwrap_or_default(), native_indices: mod_natives_map });
154
- println!("{} {} {}", blue("[Titan]"), green("Extension loaded:"), config.name);
155
- }
156
- }
157
- };
158
-
159
- // Scan node_modules
160
- if node_modules.exists() {
161
- scan_dir(node_modules, &mut modules, &mut libs, &mut all_natives);
162
- }
163
-
164
- // Scan .ext (Production / Docker)
165
- let ext_dir = root.join(".ext");
166
- if ext_dir.exists() {
167
- scan_dir(ext_dir, &mut modules, &mut libs, &mut all_natives);
168
- }
169
-
170
- *REGISTRY.lock().unwrap() = Some(Registry { _libs: libs, modules, natives: all_natives });
171
- }
172
-
173
- pub fn inject_external_extensions(scope: &mut v8::HandleScope, global: v8::Local<v8::Object>, t_obj: v8::Local<v8::Object>) {
174
- let invoke_fn = v8::Function::new(scope, native_invoke_extension).unwrap();
175
- let invoke_key = v8_str(scope, "__titan_invoke_native");
176
- global.set(scope, invoke_key.into(), invoke_fn.into());
177
-
178
- let modules = if let Ok(guard) = REGISTRY.lock() {
179
- guard.as_ref().map(|r| r.modules.clone()).unwrap_or_default()
180
- } else { vec![] };
181
-
182
- for module in modules {
183
- let mod_obj = v8::Object::new(scope);
184
- for (fn_name, &idx) in &module.native_indices {
185
- let code = format!("(function(...args) {{ return __titan_invoke_native({}, args); }})", idx);
186
- let code_str = v8_str(scope, &code);
187
- if let Some(script) = v8::Script::compile(scope, code_str, None) {
188
- if let Some(val) = script.run(scope) {
189
- let key = v8_str(scope, fn_name);
190
- mod_obj.set(scope, key.into(), val);
191
- }
192
- }
193
- }
194
- let mod_key = v8_str(scope, &module.name);
195
- t_obj.set(scope, mod_key.into(), mod_obj.into());
196
-
197
- let act_key = v8_str(scope, "__titan_action");
198
- let act_val = v8_str(scope, &module.name);
199
- global.set(scope, act_key.into(), act_val.into());
200
-
201
- let wrapped_js = format!("(function(t) {{ {} }})", module.js);
202
- let wrapped_js_str = v8_str(scope, &wrapped_js);
203
- let tc = &mut v8::TryCatch::new(scope);
204
- if let Some(script) = v8::Script::compile(tc, wrapped_js_str, None) {
205
- if let Some(func_val) = script.run(tc) {
206
- if let Ok(func) = v8::Local::<v8::Function>::try_from(func_val) {
207
- let receiver = v8::undefined(&mut *tc).into();
208
- let args = [t_obj.into()];
209
- func.call(&mut *tc, receiver, &args);
210
- }
211
- }
212
- }
213
- }
214
- }
215
-
216
- fn native_invoke_extension(scope: &mut v8::HandleScope, args: v8::FunctionCallbackArguments, mut retval: v8::ReturnValue) {
217
- let fn_idx = args.get(0).to_integer(scope).unwrap().value() as usize;
218
- let js_args_val = args.get(1);
219
- let (ptr, sig) = if let Ok(guard) = REGISTRY.lock() {
220
- if let Some(entry) = guard.as_ref().and_then(|r| r.natives.get(fn_idx)) {
221
- (entry.symbol_ptr, entry.sig.clone())
222
- } else { return; }
223
- } else { return; };
224
-
225
- if ptr == 0 { throw(scope, "Native function not found"); return; }
226
-
227
- let js_args = if js_args_val.is_array() {
228
- v8::Local::<v8::Array>::try_from(js_args_val).unwrap()
229
- } else { v8::Array::new(scope, 0) };
230
-
231
- let argc = sig.params.len();
232
- unsafe {
233
- let mut vals = Vec::new();
234
- for (i, param) in sig.params.iter().enumerate() {
235
- let val = js_args.get_index(scope, i as u32).unwrap_or_else(|| v8::undefined(scope).into());
236
- vals.push(arg_from_v8(scope, val, param));
237
- }
238
-
239
- let res_val: serde_json::Value = match argc {
240
- 0 => { dispatch_ret!(ptr, sig.ret, (), ()) },
241
- 1 => {
242
- let v0 = vals.remove(0);
243
- match sig.params[0] {
244
- ParamType::String => {
245
- let c = std::ffi::CString::new(v0.as_str().unwrap_or("")).unwrap();
246
- dispatch_ret!(ptr, sig.ret, (*const std::os::raw::c_char), (c.as_ptr()))
247
- },
248
- ParamType::F64 => { dispatch_ret!(ptr, sig.ret, (f64), (v0.as_f64().unwrap_or(0.0))) },
249
- ParamType::Bool => { dispatch_ret!(ptr, sig.ret, (bool), (v0.as_bool().unwrap_or(false))) },
250
- ParamType::Json => {
251
- let c = std::ffi::CString::new(v0.to_string()).unwrap();
252
- dispatch_ret!(ptr, sig.ret, (*const std::os::raw::c_char), (c.as_ptr()))
253
- },
254
- ParamType::Buffer => {
255
- let a0: Vec<u8> = v0.as_array().map(|a| a.iter().map(|v| v.as_u64().unwrap_or(0) as u8).collect()).unwrap_or_default();
256
- dispatch_ret!(ptr, sig.ret, (Vec<u8>), (a0))
257
- },
258
- }
259
- },
260
- 2 => {
261
- let v0 = vals.remove(0); let v1 = vals.remove(0);
262
- match (sig.params[0].clone(), sig.params[1].clone()) {
263
- (ParamType::String, ParamType::String) => {
264
- let c0 = std::ffi::CString::new(v0.as_str().unwrap_or("")).unwrap();
265
- let c1 = std::ffi::CString::new(v1.as_str().unwrap_or("")).unwrap();
266
- dispatch_ret!(ptr, sig.ret, (*const std::os::raw::c_char, *const std::os::raw::c_char), (c0.as_ptr(), c1.as_ptr()))
267
- },
268
- (ParamType::String, ParamType::F64) => {
269
- let c0 = std::ffi::CString::new(v0.as_str().unwrap_or("")).unwrap();
270
- dispatch_ret!(ptr, sig.ret, (*const std::os::raw::c_char, f64), (c0.as_ptr(), v1.as_f64().unwrap_or(0.0)))
271
- },
272
- _ => serde_json::Value::Null
273
- }
274
- },
275
- _ => serde_json::Value::Null
276
- };
277
- retval.set(js_from_value(scope, &sig.ret, res_val));
278
- }
279
- }
280
-
281
- fn arg_from_v8(scope: &mut v8::HandleScope, val: v8::Local<v8::Value>, ty: &ParamType) -> serde_json::Value {
282
- match ty {
283
- ParamType::String => serde_json::Value::String(val.to_rust_string_lossy(scope)),
284
- ParamType::F64 => serde_json::json!(val.to_number(scope).map(|n| n.value()).unwrap_or(0.0)),
285
- ParamType::Bool => serde_json::json!(val.boolean_value(scope)),
286
- ParamType::Json => {
287
- v8::json::stringify(scope, val).map(|s| serde_json::from_str(&s.to_rust_string_lossy(scope)).unwrap_or(Value::Null)).unwrap_or(Value::Null)
288
- },
289
- ParamType::Buffer => {
290
- if let Ok(u8arr) = v8::Local::<v8::Uint8Array>::try_from(val) {
291
- let store = v8::ArrayBuffer::get_backing_store(&u8arr.buffer(scope).unwrap());
292
- let offset = usize::from(u8arr.byte_offset());
293
- let length = usize::from(u8arr.byte_length());
294
- let vec_u8: Vec<Value> = store[offset..offset+length].iter().map(|b| Value::from(b.get() as u64)).collect();
295
- Value::Array(vec_u8)
296
- } else { Value::Array(vec![]) }
297
- }
298
- }
299
- }
300
-
301
- fn js_from_value<'a>(scope: &mut v8::HandleScope<'a>, ret_type: &ReturnType, val: serde_json::Value) -> v8::Local<'a, v8::Value> {
302
- match ret_type {
303
- ReturnType::String => v8_str(scope, val.as_str().unwrap_or("")).into(),
304
- ReturnType::F64 => v8::Number::new(scope, val.as_f64().unwrap_or(0.0)).into(),
305
- ReturnType::Bool => v8::Boolean::new(scope, val.as_bool().unwrap_or(false)).into(),
306
- ReturnType::Json => {
307
- let s = v8_str(scope, &val.to_string());
308
- v8::json::parse(scope, s).unwrap_or_else(|| v8::null(scope).into())
309
- },
310
- ReturnType::Buffer => v8::undefined(scope).into(),
311
- ReturnType::Void => v8::undefined(scope).into(),
312
- }
313
- }
314
-
315
- macro_rules! dispatch_ret {
316
- ($ptr:expr, $ret:expr, ($($arg_ty:ty),*), ($($arg:expr),*)) => {
317
- match $ret {
318
- ReturnType::String => {
319
- let f: extern "C" fn($($arg_ty),*) -> *mut std::os::raw::c_char = unsafe { std::mem::transmute($ptr) };
320
- let ptr = f($($arg),*);
321
- if ptr.is_null() { Value::String(String::new()) } else { Value::String(unsafe { std::ffi::CStr::from_ptr(ptr).to_string_lossy().into_owned() }) }
322
- },
323
- ReturnType::F64 => { let f: extern "C" fn($($arg_ty),*) -> f64 = unsafe { std::mem::transmute($ptr) }; serde_json::json!(f($($arg),*)) },
324
- ReturnType::Bool => { let f: extern "C" fn($($arg_ty),*) -> bool = unsafe { std::mem::transmute($ptr) }; serde_json::json!(f($($arg),*)) },
325
- ReturnType::Json => {
326
- let f: extern "C" fn($($arg_ty),*) -> *mut std::os::raw::c_char = unsafe { std::mem::transmute($ptr) };
327
- let ptr = f($($arg),*);
328
- if ptr.is_null() { Value::Null } else { serde_json::from_str(&unsafe { std::ffi::CStr::from_ptr(ptr).to_string_lossy() }).unwrap_or(Value::Null) }
329
- },
330
- ReturnType::Buffer => {
331
- let f: extern "C" fn($($arg_ty),*) -> Vec<u8> = unsafe { std::mem::transmute($ptr) };
332
- Value::Array(f($($arg),*).into_iter().map(Value::from).collect())
333
- },
334
- ReturnType::Void => { let f: extern "C" fn($($arg_ty),*) = unsafe { std::mem::transmute($ptr) }; f($($arg),*); Value::Null },
335
- }
336
- }
337
- }
338
- pub(crate) use dispatch_ret;