functionalscript 0.2.0 → 0.2.2

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 (248) hide show
  1. package/dev/module.mjs +1 -1
  2. package/jsr.json +59 -59
  3. package/out/com/cpp/module.f.mjs +123 -0
  4. package/out/com/cpp/test.f.mjs +40 -0
  5. package/out/com/cpp/testlib.f.mjs +7 -0
  6. package/out/com/cs/module.f.mjs +95 -0
  7. package/out/com/cs/test.f.mjs +43 -0
  8. package/out/com/cs/testlib.f.mjs +7 -0
  9. package/out/com/rust/module.f.mjs +213 -0
  10. package/out/com/rust/test.f.mjs +123 -0
  11. package/out/com/rust/testlib.f.mjs +7 -0
  12. package/out/com/test/build.f.mjs +98 -0
  13. package/out/com/test/build.mjs +40 -0
  14. package/out/com/types/module.f.mjs +51 -0
  15. package/out/com/types/testlib.f.mjs +30 -0
  16. package/out/commonjs/build/module.f.mjs +107 -0
  17. package/out/commonjs/build/test.f.mjs +102 -0
  18. package/out/commonjs/module/function/module.f.mjs +15 -0
  19. package/out/commonjs/module/module.f.mjs +48 -0
  20. package/out/commonjs/module.f.mjs +10 -0
  21. package/out/commonjs/module.mjs +26 -0
  22. package/out/commonjs/package/dependencies/module.f.mjs +21 -0
  23. package/out/commonjs/package/dependencies/test.f.mjs +15 -0
  24. package/out/commonjs/package/module.f.mjs +40 -0
  25. package/out/commonjs/package/test.f.mjs +27 -0
  26. package/out/commonjs/path/module.f.mjs +171 -0
  27. package/out/commonjs/path/test.f.mjs +231 -0
  28. package/out/commonjs/test.mjs +87 -0
  29. package/out/dev/index.mjs +2 -0
  30. package/out/dev/module.f.mjs +2 -0
  31. package/out/dev/module.mjs +167 -0
  32. package/out/dev/test/module.f.mjs +134 -0
  33. package/out/dev/test.f.mjs +58 -0
  34. package/out/dev/test.mjs +52 -0
  35. package/out/djs/module.f.mjs +75 -0
  36. package/out/djs/parser/module.f.mjs +432 -0
  37. package/out/djs/parser/test.f.mjs +535 -0
  38. package/out/djs/test.f.mjs +84 -0
  39. package/out/djs/tokenizer/module.f.mjs +87 -0
  40. package/out/djs/tokenizer/test.f.mjs +480 -0
  41. package/out/fsc/module.f.mjs +105 -0
  42. package/out/fsc/test.f.mjs +19 -0
  43. package/out/fsm/module.f.mjs +80 -0
  44. package/out/fsm/test.f.mjs +138 -0
  45. package/out/html/module.f.mjs +94 -0
  46. package/out/html/test.f.mjs +45 -0
  47. package/out/issues/test.f.mjs +66 -0
  48. package/out/js/tokenizer/module.f.mjs +686 -0
  49. package/out/js/tokenizer/test.f.mjs +844 -0
  50. package/out/json/module.f.mjs +89 -0
  51. package/out/json/parser/module.f.mjs +224 -0
  52. package/out/json/parser/test.f.mjs +321 -0
  53. package/out/json/serializer/module.f.mjs +67 -0
  54. package/out/json/serializer/test.f.mjs +87 -0
  55. package/out/json/test.f.mjs +61 -0
  56. package/out/json/tokenizer/module.f.mjs +78 -0
  57. package/out/json/tokenizer/test.f.mjs +420 -0
  58. package/out/nanvm-lib/tests/test.f.mjs +87 -0
  59. package/out/nodejs/version/main.mjs +3 -0
  60. package/out/nodejs/version/module.f.mjs +34 -0
  61. package/out/nodejs/version/test.f.mjs +97 -0
  62. package/out/prime_field/module.f.mjs +87 -0
  63. package/out/prime_field/test.f.mjs +145 -0
  64. package/out/secp/module.f.mjs +113 -0
  65. package/out/secp/test.f.mjs +63 -0
  66. package/out/sha2/module.f.mjs +172 -0
  67. package/out/sha2/test.f.mjs +86 -0
  68. package/out/text/ascii/module.f.mjs +154 -0
  69. package/out/text/ascii/test.f.mjs +14 -0
  70. package/out/text/module.f.mjs +19 -0
  71. package/out/text/sgr/module.f.mjs +17 -0
  72. package/out/text/test.f.mjs +19 -0
  73. package/out/text/utf16/module.f.mjs +86 -0
  74. package/out/text/utf16/test.f.mjs +145 -0
  75. package/out/text/utf8/module.f.mjs +126 -0
  76. package/out/text/utf8/test.f.mjs +175 -0
  77. package/out/types/array/module.f.mjs +95 -0
  78. package/out/types/array/test.f.mjs +116 -0
  79. package/out/types/bigfloat/module.f.mjs +77 -0
  80. package/out/types/bigfloat/test.f.mjs +349 -0
  81. package/out/types/bigint/module.f.mjs +114 -0
  82. package/out/types/bigint/test.f.mjs +192 -0
  83. package/out/types/btree/find/module.f.mjs +137 -0
  84. package/out/types/btree/find/test.f.mjs +156 -0
  85. package/out/types/btree/module.f.mjs +34 -0
  86. package/out/types/btree/remove/module.f.mjs +209 -0
  87. package/out/types/btree/remove/test.f.mjs +638 -0
  88. package/out/types/btree/set/module.f.mjs +114 -0
  89. package/out/types/btree/set/test.f.mjs +390 -0
  90. package/out/types/btree/test.f.mjs +83 -0
  91. package/out/types/btree/types/module.f.mjs +50 -0
  92. package/out/types/byte_set/module.f.mjs +42 -0
  93. package/out/types/byte_set/test.f.mjs +123 -0
  94. package/out/types/function/compare/module.f.mjs +22 -0
  95. package/out/types/function/compare/test.f.mjs +8 -0
  96. package/out/types/function/module.f.mjs +44 -0
  97. package/out/types/function/operator/module.f.mjs +60 -0
  98. package/out/types/function/test.f.mjs +15 -0
  99. package/out/types/list/module.f.mjs +269 -0
  100. package/out/types/list/test.f.mjs +401 -0
  101. package/out/types/map/module.f.mjs +54 -0
  102. package/out/types/map/test.f.mjs +115 -0
  103. package/out/types/nibble_set/module.f.mjs +19 -0
  104. package/out/types/nibble_set/test.f.mjs +90 -0
  105. package/out/types/nullable/module.f.mjs +9 -0
  106. package/out/types/nullable/test.f.mjs +12 -0
  107. package/out/types/number/module.f.mjs +12 -0
  108. package/out/types/number/test.f.mjs +126 -0
  109. package/out/types/object/module.f.mjs +27 -0
  110. package/out/types/object/test.f.mjs +17 -0
  111. package/out/types/range/module.f.mjs +6 -0
  112. package/out/types/range/test.f.mjs +18 -0
  113. package/out/types/range_map/module.f.mjs +84 -0
  114. package/out/types/range_map/test.f.mjs +201 -0
  115. package/out/types/result/module.f.mjs +25 -0
  116. package/out/types/result/module.mjs +16 -0
  117. package/out/types/sorted_list/module.f.mjs +102 -0
  118. package/out/types/sorted_list/test.f.mjs +66 -0
  119. package/out/types/sorted_set/module.f.mjs +29 -0
  120. package/out/types/sorted_set/test.f.mjs +80 -0
  121. package/out/types/string/module.f.mjs +17 -0
  122. package/out/types/string/test.f.mjs +58 -0
  123. package/out/types/string_set/module.f.mjs +29 -0
  124. package/out/types/string_set/test.f.mjs +65 -0
  125. package/package.json +5 -4
  126. package/tsconfig.json +2 -2
  127. /package/{com → out/com}/cpp/module.f.d.mts +0 -0
  128. /package/{com → out/com}/cpp/test.f.d.mts +0 -0
  129. /package/{com → out/com}/cpp/testlib.f.d.mts +0 -0
  130. /package/{com → out/com}/cs/module.f.d.mts +0 -0
  131. /package/{com → out/com}/cs/test.f.d.mts +0 -0
  132. /package/{com → out/com}/cs/testlib.f.d.mts +0 -0
  133. /package/{com → out/com}/rust/module.f.d.mts +0 -0
  134. /package/{com → out/com}/rust/test.f.d.mts +0 -0
  135. /package/{com → out/com}/rust/testlib.f.d.mts +0 -0
  136. /package/{com → out/com}/test/build.d.mts +0 -0
  137. /package/{com → out/com}/test/build.f.d.mts +0 -0
  138. /package/{com → out/com}/types/module.f.d.mts +0 -0
  139. /package/{com → out/com}/types/testlib.f.d.mts +0 -0
  140. /package/{commonjs → out/commonjs}/build/module.f.d.mts +0 -0
  141. /package/{commonjs → out/commonjs}/build/test.f.d.mts +0 -0
  142. /package/{commonjs → out/commonjs}/module/function/module.f.d.mts +0 -0
  143. /package/{commonjs → out/commonjs}/module/module.f.d.mts +0 -0
  144. /package/{commonjs → out/commonjs}/module.d.mts +0 -0
  145. /package/{commonjs → out/commonjs}/module.f.d.mts +0 -0
  146. /package/{commonjs → out/commonjs}/package/dependencies/module.f.d.mts +0 -0
  147. /package/{commonjs → out/commonjs}/package/dependencies/test.f.d.mts +0 -0
  148. /package/{commonjs → out/commonjs}/package/module.f.d.mts +0 -0
  149. /package/{commonjs → out/commonjs}/package/test.f.d.mts +0 -0
  150. /package/{commonjs → out/commonjs}/path/module.f.d.mts +0 -0
  151. /package/{commonjs → out/commonjs}/path/test.f.d.mts +0 -0
  152. /package/{commonjs → out/commonjs}/test.d.mts +0 -0
  153. /package/{dev → out/dev}/index.d.mts +0 -0
  154. /package/{dev → out/dev}/module.d.mts +0 -0
  155. /package/{dev → out/dev}/module.f.d.mts +0 -0
  156. /package/{dev → out/dev}/test/module.f.d.mts +0 -0
  157. /package/{dev → out/dev}/test.d.mts +0 -0
  158. /package/{dev → out/dev}/test.f.d.mts +0 -0
  159. /package/{djs → out/djs}/module.f.d.mts +0 -0
  160. /package/{djs → out/djs}/parser/module.f.d.mts +0 -0
  161. /package/{djs → out/djs}/parser/test.f.d.mts +0 -0
  162. /package/{djs → out/djs}/test.f.d.mts +0 -0
  163. /package/{djs → out/djs}/tokenizer/module.f.d.mts +0 -0
  164. /package/{djs → out/djs}/tokenizer/test.f.d.mts +0 -0
  165. /package/{fsc → out/fsc}/module.f.d.mts +0 -0
  166. /package/{fsc → out/fsc}/test.f.d.mts +0 -0
  167. /package/{fsm → out/fsm}/module.f.d.mts +0 -0
  168. /package/{fsm → out/fsm}/test.f.d.mts +0 -0
  169. /package/{html → out/html}/module.f.d.mts +0 -0
  170. /package/{html → out/html}/test.f.d.mts +0 -0
  171. /package/{issues → out/issues}/test.f.d.mts +0 -0
  172. /package/{js → out/js}/tokenizer/module.f.d.mts +0 -0
  173. /package/{js → out/js}/tokenizer/test.f.d.mts +0 -0
  174. /package/{json → out/json}/module.f.d.mts +0 -0
  175. /package/{json → out/json}/parser/module.f.d.mts +0 -0
  176. /package/{json → out/json}/parser/test.f.d.mts +0 -0
  177. /package/{json → out/json}/serializer/module.f.d.mts +0 -0
  178. /package/{json → out/json}/serializer/test.f.d.mts +0 -0
  179. /package/{json → out/json}/test.f.d.mts +0 -0
  180. /package/{json → out/json}/tokenizer/module.f.d.mts +0 -0
  181. /package/{json → out/json}/tokenizer/test.f.d.mts +0 -0
  182. /package/{nanvm-lib → out/nanvm-lib}/tests/test.f.d.mts +0 -0
  183. /package/{nodejs → out/nodejs}/version/main.d.mts +0 -0
  184. /package/{nodejs → out/nodejs}/version/module.f.d.mts +0 -0
  185. /package/{nodejs → out/nodejs}/version/test.f.d.mts +0 -0
  186. /package/{prime_field → out/prime_field}/module.f.d.mts +0 -0
  187. /package/{prime_field → out/prime_field}/test.f.d.mts +0 -0
  188. /package/{secp → out/secp}/module.f.d.mts +0 -0
  189. /package/{secp → out/secp}/test.f.d.mts +0 -0
  190. /package/{sha2 → out/sha2}/module.f.d.mts +0 -0
  191. /package/{sha2 → out/sha2}/test.f.d.mts +0 -0
  192. /package/{text → out/text}/ascii/module.f.d.mts +0 -0
  193. /package/{text → out/text}/ascii/test.f.d.mts +0 -0
  194. /package/{text → out/text}/module.f.d.mts +0 -0
  195. /package/{text → out/text}/sgr/module.f.d.mts +0 -0
  196. /package/{text → out/text}/test.f.d.mts +0 -0
  197. /package/{text → out/text}/utf16/module.f.d.mts +0 -0
  198. /package/{text → out/text}/utf16/test.f.d.mts +0 -0
  199. /package/{text → out/text}/utf8/module.f.d.mts +0 -0
  200. /package/{text → out/text}/utf8/test.f.d.mts +0 -0
  201. /package/{types → out/types}/array/module.f.d.mts +0 -0
  202. /package/{types → out/types}/array/test.f.d.mts +0 -0
  203. /package/{types → out/types}/bigfloat/module.f.d.mts +0 -0
  204. /package/{types → out/types}/bigfloat/test.f.d.mts +0 -0
  205. /package/{types → out/types}/bigint/module.f.d.mts +0 -0
  206. /package/{types → out/types}/bigint/test.f.d.mts +0 -0
  207. /package/{types → out/types}/btree/find/module.f.d.mts +0 -0
  208. /package/{types → out/types}/btree/find/test.f.d.mts +0 -0
  209. /package/{types → out/types}/btree/module.f.d.mts +0 -0
  210. /package/{types → out/types}/btree/remove/module.f.d.mts +0 -0
  211. /package/{types → out/types}/btree/remove/test.f.d.mts +0 -0
  212. /package/{types → out/types}/btree/set/module.f.d.mts +0 -0
  213. /package/{types → out/types}/btree/set/test.f.d.mts +0 -0
  214. /package/{types → out/types}/btree/test.f.d.mts +0 -0
  215. /package/{types → out/types}/btree/types/module.f.d.mts +0 -0
  216. /package/{types → out/types}/byte_set/module.f.d.mts +0 -0
  217. /package/{types → out/types}/byte_set/test.f.d.mts +0 -0
  218. /package/{types → out/types}/function/compare/module.f.d.mts +0 -0
  219. /package/{types → out/types}/function/compare/test.f.d.mts +0 -0
  220. /package/{types → out/types}/function/module.f.d.mts +0 -0
  221. /package/{types → out/types}/function/operator/module.f.d.mts +0 -0
  222. /package/{types → out/types}/function/test.f.d.mts +0 -0
  223. /package/{types → out/types}/list/module.f.d.mts +0 -0
  224. /package/{types → out/types}/list/test.f.d.mts +0 -0
  225. /package/{types → out/types}/map/module.f.d.mts +0 -0
  226. /package/{types → out/types}/map/test.f.d.mts +0 -0
  227. /package/{types → out/types}/nibble_set/module.f.d.mts +0 -0
  228. /package/{types → out/types}/nibble_set/test.f.d.mts +0 -0
  229. /package/{types → out/types}/nullable/module.f.d.mts +0 -0
  230. /package/{types → out/types}/nullable/test.f.d.mts +0 -0
  231. /package/{types → out/types}/number/module.f.d.mts +0 -0
  232. /package/{types → out/types}/number/test.f.d.mts +0 -0
  233. /package/{types → out/types}/object/module.f.d.mts +0 -0
  234. /package/{types → out/types}/object/test.f.d.mts +0 -0
  235. /package/{types → out/types}/range/module.f.d.mts +0 -0
  236. /package/{types → out/types}/range/test.f.d.mts +0 -0
  237. /package/{types → out/types}/range_map/module.f.d.mts +0 -0
  238. /package/{types → out/types}/range_map/test.f.d.mts +0 -0
  239. /package/{types → out/types}/result/module.d.mts +0 -0
  240. /package/{types → out/types}/result/module.f.d.mts +0 -0
  241. /package/{types → out/types}/sorted_list/module.f.d.mts +0 -0
  242. /package/{types → out/types}/sorted_list/test.f.d.mts +0 -0
  243. /package/{types → out/types}/sorted_set/module.f.d.mts +0 -0
  244. /package/{types → out/types}/sorted_set/test.f.d.mts +0 -0
  245. /package/{types → out/types}/string/module.f.d.mts +0 -0
  246. /package/{types → out/types}/string/test.f.d.mts +0 -0
  247. /package/{types → out/types}/string_set/module.f.d.mts +0 -0
  248. /package/{types → out/types}/string_set/test.f.d.mts +0 -0
@@ -0,0 +1,213 @@
1
+ // @ts-self-types="./module.f.d.mts"
2
+ import * as types from '../types/module.f.mjs';
3
+ const { paramList } = types;
4
+ import * as Text from '../../text/module.f.mjs';
5
+ import * as O from '../../types/object/module.f.mjs';
6
+ import * as list from '../../types/list/module.f.mjs';
7
+ const { flat, map, flatMap } = list;
8
+ const { entries } = Object;
9
+ import * as func from '../../types/function/module.f.mjs';
10
+ const { fn } = func;
11
+ import * as string from '../../types/string/module.f.mjs';
12
+ const { join } = string;
13
+ /** @type {(field: string) => string} */
14
+ const rustField = field => `pub ${field},`;
15
+ const mapRustField = map(rustField);
16
+ /** @type {(b: list.Thunk<string>) => (name: string) => Text.Block} */
17
+ const rustStruct = b => name => [`#[repr(C)]`, `pub struct ${name} {`, mapRustField(b), `}`];
18
+ const commaJoin = join(', ');
19
+ /** @type {(name: string) => string} */
20
+ const ref = name => `${name}::Ref`;
21
+ /** @type {(name: string) => string} */
22
+ const obj = name => `&${name}::Object`;
23
+ const self = ['&self'];
24
+ /** @type {(p: types.Field) => string} */
25
+ const paramName = ([n]) => n;
26
+ /** @type {(p: types.FieldArray) => list.Thunk<string>} */
27
+ const callList = p => map(paramName)(paramList(p));
28
+ /** @type {(p: types.FieldArray) => string} */
29
+ const call = p => commaJoin(callList(p));
30
+ /** @type {(p: types.FieldArray) => string} */
31
+ const virtualCall = p => commaJoin(flat([['self'], callList(p)]));
32
+ const super_ = 'super::';
33
+ /** @type {(m: types.Method) => string} */
34
+ const assign = ([n]) => `${n}: Self::${n},`;
35
+ const mapAssign = map(assign);
36
+ const this_ = ['this: &Object'];
37
+ /** @type {(n: string) => string} */
38
+ const rustType = n => `pub type ${n} = nanocom::${n}<Interface>;`;
39
+ /**
40
+ * @template T
41
+ * @typedef {T|{}} OptionalProperty
42
+ */
43
+ /** @typedef {{readonly where: readonly string[]}} Where */
44
+ /**
45
+ * @typedef {OptionalProperty<Where> & {readonly content: Text.Block}} WhereContent
46
+ */
47
+ /** @type {(h: string) => (wh: WhereContent) => Text.Block} */
48
+ const whereContent = h => wh => {
49
+ const w = 'where' in wh ? [
50
+ h,
51
+ `where`,
52
+ mapComma(wh.where),
53
+ '{'
54
+ ] : [`${h} {`];
55
+ const x = [
56
+ wh.content,
57
+ '}',
58
+ ];
59
+ return flat([w, x]);
60
+ };
61
+ /**
62
+ * @typedef {{
63
+ * readonly param?: string
64
+ * readonly trait: string
65
+ * readonly type: string
66
+ * readonly where?: readonly string[]
67
+ * readonly content: Text.Block
68
+ * }} Impl
69
+ */
70
+ /** @type {(impl: Impl) => Text.Block} */
71
+ const rustImpl = i => {
72
+ const p = 'param' in i ? `<${i.param}>` : '';
73
+ const header = `impl${p} ${i.trait} for ${i.type}`;
74
+ return whereContent(header)(i);
75
+ };
76
+ /**
77
+ * @typedef {{
78
+ * readonly pub?: true
79
+ * readonly type: string
80
+ * readonly where?: readonly string[]
81
+ * readonly content: Text.Block
82
+ * }} Trait
83
+ */
84
+ /** @type {(s: string) => string} */
85
+ const comma = s => `${s},`;
86
+ const mapComma = map(comma);
87
+ /** @type {(t: Trait) => Text.Block} */
88
+ const trait = t => {
89
+ const p = t.pub === true ? 'pub ' : '';
90
+ const h = `${p}trait ${t.type}`;
91
+ return whereContent(h)(t);
92
+ };
93
+ /** @type {(t: Trait) => Text.Block} */
94
+ const traitImpl = t => {
95
+ const i = rustImpl({
96
+ param: 'T',
97
+ trait: t.type,
98
+ type: 'T',
99
+ where,
100
+ content: [],
101
+ });
102
+ return flat([trait({ ...t, where }), i]);
103
+ };
104
+ const where = ['Self: nanocom::Class<Interface = Interface>', 'nanocom::CObject<Self>: Ex'];
105
+ /** @type {(library: types.Library) => Text.Block} */
106
+ export const rust = library => {
107
+ /** @type {(p: string) => (o: (_: string) => string) => (t: types.Type) => string} */
108
+ const type = p => {
109
+ /** @type {(o: (_: string) => string) => (t: types.Type) => string} */
110
+ const f = o => t => {
111
+ if (typeof t === 'string') {
112
+ return t;
113
+ }
114
+ if (t.length === 2) {
115
+ return `*const ${f(ref)(t[1])}`;
116
+ }
117
+ const [id] = t;
118
+ const fullId = `${p}${id}`;
119
+ return 'interface' in library[id] ? o(fullId) : fullId;
120
+ };
121
+ return f;
122
+ };
123
+ /** @type {(p: string) => (o: (_: string) => string) => (f: types.Field) => string} */
124
+ const pf = p => o => ([name, t]) => `${name}: ${type(p)(o)(t)}`;
125
+ const param = pf(super_)(obj);
126
+ const mapParam = map(param);
127
+ const mapField = map(pf('')(ref));
128
+ /** @type {(fa: types.FieldArray) => (name: string) => Text.Block} */
129
+ const struct = fn(entries)
130
+ .then(mapField)
131
+ .then(rustStruct)
132
+ .result;
133
+ /** @type {(first: readonly string[]) => (p: types.FieldArray) => string} */
134
+ const func = first => p => {
135
+ const resultStr = '_' in p ? ` -> ${type(super_)(ref)(p._)}` : '';
136
+ const params = commaJoin(flat([first, mapParam(paramList(p))]));
137
+ return `(${params})${resultStr}`;
138
+ };
139
+ /** @type {(n: string) => (p: types.FieldArray) => string} */
140
+ const virtualFnType = n => p => `extern "system" fn${n}${func(this_)(p)}`;
141
+ /** @type {(m: types.Method) => string} */
142
+ const virtualFn = ([n, p]) => `${n}: unsafe ${virtualFnType('')(p)}`;
143
+ const mapVirtualFn = map(virtualFn);
144
+ /** @type {(m: types.Method) => string} */
145
+ const headerFn = ([n, p]) => `fn ${n}${func(self)(p)}`;
146
+ /** @type {(m: types.Method) => string} */
147
+ const traitFn = m => `${headerFn(m)};`;
148
+ const mapTraitFn = map(traitFn);
149
+ /** @type {(m: types.Method) => Text.Block} */
150
+ const implFn = m => {
151
+ const [n, p] = m;
152
+ return [
153
+ `${headerFn(m)} {`,
154
+ [`unsafe { (self.interface().${n})(${virtualCall(p)}) }`],
155
+ '}'
156
+ ];
157
+ };
158
+ const flatMapImplFn = flatMap(implFn);
159
+ /** @type {(m: types.Method) => Text.Block} */
160
+ const impl = ([n, p]) => {
161
+ const type = virtualFnType(` ${n}`)(p);
162
+ return [
163
+ `${type} {`,
164
+ [`unsafe { nanocom::CObject::from_object_unchecked(this) }.${n}(${call(p)})`],
165
+ '}'
166
+ ];
167
+ };
168
+ const flatMapImpl = flatMap(impl);
169
+ /** @type {(i: types.Interface) => (name: string) => Text.Block} */
170
+ const interface_ = ({ interface: i, guid }) => name => {
171
+ const e = entries(i);
172
+ return [
173
+ `pub mod ${name} {`,
174
+ [
175
+ rustType('Object'),
176
+ rustType('Ref'),
177
+ rustType('Vmt'),
178
+ ],
179
+ rustStruct(mapVirtualFn(e))('Interface'),
180
+ rustImpl({
181
+ trait: 'nanocom::Interface',
182
+ type: 'Interface',
183
+ content: [`const GUID: nanocom::GUID = 0x${guid.replaceAll('-', '_')};`]
184
+ }),
185
+ trait({ pub: true, type: 'Ex', content: mapTraitFn(e) }),
186
+ rustImpl({
187
+ trait: 'Ex',
188
+ type: 'Object',
189
+ content: flatMapImplFn(e)
190
+ }),
191
+ traitImpl({
192
+ pub: true,
193
+ type: 'ClassEx',
194
+ content: ['const VMT: Vmt = Vmt {',
195
+ ['iunknown: nanocom::CObject::<Self>::IUNKNOWN,',
196
+ 'interface: Interface {',
197
+ mapAssign(e),
198
+ '},',
199
+ ],
200
+ '};'
201
+ ]
202
+ }),
203
+ traitImpl({
204
+ type: 'PrivateClassEx',
205
+ content: flatMapImpl(e)
206
+ }),
207
+ '}'
208
+ ];
209
+ };
210
+ /** @type {(type: O.Entry<types.Definition>) => Text.Block} */
211
+ const def = ([name, type]) => ('interface' in type ? interface_(type) : struct(type.struct))(name);
212
+ return flat([['#![allow(non_snake_case)]'], flatMap(def)(entries(library))]);
213
+ };
@@ -0,0 +1,123 @@
1
+ import rust from './testlib.f.mjs';
2
+ export default () => {
3
+ const e = '#![allow(non_snake_case)]\n' +
4
+ '#[repr(C)]\n' +
5
+ 'pub struct Slice {\n' +
6
+ ' pub Start: *const u8,\n' +
7
+ ' pub Size: usize,\n' +
8
+ '}\n' +
9
+ '#[repr(C)]\n' +
10
+ 'pub struct ManagedStruct {\n' +
11
+ ' pub M: IMy::Ref,\n' +
12
+ '}\n' +
13
+ 'pub mod IMy {\n' +
14
+ ' pub type Object = nanocom::Object<Interface>;\n' +
15
+ ' pub type Ref = nanocom::Ref<Interface>;\n' +
16
+ ' pub type Vmt = nanocom::Vmt<Interface>;\n' +
17
+ ' #[repr(C)]\n' +
18
+ ' pub struct Interface {\n' +
19
+ ' pub GetSlice: unsafe extern "system" fn(this: &Object) -> super::Slice,\n' +
20
+ ' pub SetSlice: unsafe extern "system" fn(this: &Object, slice: super::Slice),\n' +
21
+ ' pub GetUnsafe: unsafe extern "system" fn(this: &Object) -> *const bool,\n' +
22
+ ' pub SetUnsafe: unsafe extern "system" fn(this: &Object, p: *const super::Slice, size: u32),\n' +
23
+ ' pub Some: unsafe extern "system" fn(this: &Object, p: &super::IMy::Object) -> bool,\n' +
24
+ ' pub GetIMy: unsafe extern "system" fn(this: &Object, a: u16, b: i16) -> super::IMy::Ref,\n' +
25
+ ' pub SetManagedStruct: unsafe extern "system" fn(this: &Object, a: super::ManagedStruct),\n' +
26
+ ' }\n' +
27
+ ' impl nanocom::Interface for Interface {\n' +
28
+ ' const GUID: nanocom::GUID = 0xC66FB270_2D80_49AD_BB6E_88C1F90B805D;\n' +
29
+ ' }\n' +
30
+ ' pub trait Ex {\n' +
31
+ ' fn GetSlice(&self) -> super::Slice;\n' +
32
+ ' fn SetSlice(&self, slice: super::Slice);\n' +
33
+ ' fn GetUnsafe(&self) -> *const bool;\n' +
34
+ ' fn SetUnsafe(&self, p: *const super::Slice, size: u32);\n' +
35
+ ' fn Some(&self, p: &super::IMy::Object) -> bool;\n' +
36
+ ' fn GetIMy(&self, a: u16, b: i16) -> super::IMy::Ref;\n' +
37
+ ' fn SetManagedStruct(&self, a: super::ManagedStruct);\n' +
38
+ ' }\n' +
39
+ ' impl Ex for Object {\n' +
40
+ ' fn GetSlice(&self) -> super::Slice {\n' +
41
+ ' unsafe { (self.interface().GetSlice)(self) }\n' +
42
+ ' }\n' +
43
+ ' fn SetSlice(&self, slice: super::Slice) {\n' +
44
+ ' unsafe { (self.interface().SetSlice)(self, slice) }\n' +
45
+ ' }\n' +
46
+ ' fn GetUnsafe(&self) -> *const bool {\n' +
47
+ ' unsafe { (self.interface().GetUnsafe)(self) }\n' +
48
+ ' }\n' +
49
+ ' fn SetUnsafe(&self, p: *const super::Slice, size: u32) {\n' +
50
+ ' unsafe { (self.interface().SetUnsafe)(self, p, size) }\n' +
51
+ ' }\n' +
52
+ ' fn Some(&self, p: &super::IMy::Object) -> bool {\n' +
53
+ ' unsafe { (self.interface().Some)(self, p) }\n' +
54
+ ' }\n' +
55
+ ' fn GetIMy(&self, a: u16, b: i16) -> super::IMy::Ref {\n' +
56
+ ' unsafe { (self.interface().GetIMy)(self, a, b) }\n' +
57
+ ' }\n' +
58
+ ' fn SetManagedStruct(&self, a: super::ManagedStruct) {\n' +
59
+ ' unsafe { (self.interface().SetManagedStruct)(self, a) }\n' +
60
+ ' }\n' +
61
+ ' }\n' +
62
+ ' pub trait ClassEx\n' +
63
+ ' where\n' +
64
+ ' Self: nanocom::Class<Interface = Interface>,\n' +
65
+ ' nanocom::CObject<Self>: Ex,\n' +
66
+ ' {\n' +
67
+ ' const VMT: Vmt = Vmt {\n' +
68
+ ' iunknown: nanocom::CObject::<Self>::IUNKNOWN,\n' +
69
+ ' interface: Interface {\n' +
70
+ ' GetSlice: Self::GetSlice,\n' +
71
+ ' SetSlice: Self::SetSlice,\n' +
72
+ ' GetUnsafe: Self::GetUnsafe,\n' +
73
+ ' SetUnsafe: Self::SetUnsafe,\n' +
74
+ ' Some: Self::Some,\n' +
75
+ ' GetIMy: Self::GetIMy,\n' +
76
+ ' SetManagedStruct: Self::SetManagedStruct,\n' +
77
+ ' },\n' +
78
+ ' };\n' +
79
+ ' }\n' +
80
+ ' impl<T> ClassEx for T\n' +
81
+ ' where\n' +
82
+ ' Self: nanocom::Class<Interface = Interface>,\n' +
83
+ ' nanocom::CObject<Self>: Ex,\n' +
84
+ ' {\n' +
85
+ ' }\n' +
86
+ ' trait PrivateClassEx\n' +
87
+ ' where\n' +
88
+ ' Self: nanocom::Class<Interface = Interface>,\n' +
89
+ ' nanocom::CObject<Self>: Ex,\n' +
90
+ ' {\n' +
91
+ ' extern "system" fn GetSlice(this: &Object) -> super::Slice {\n' +
92
+ ' unsafe { nanocom::CObject::from_object_unchecked(this) }.GetSlice()\n' +
93
+ ' }\n' +
94
+ ' extern "system" fn SetSlice(this: &Object, slice: super::Slice) {\n' +
95
+ ' unsafe { nanocom::CObject::from_object_unchecked(this) }.SetSlice(slice)\n' +
96
+ ' }\n' +
97
+ ' extern "system" fn GetUnsafe(this: &Object) -> *const bool {\n' +
98
+ ' unsafe { nanocom::CObject::from_object_unchecked(this) }.GetUnsafe()\n' +
99
+ ' }\n' +
100
+ ' extern "system" fn SetUnsafe(this: &Object, p: *const super::Slice, size: u32) {\n' +
101
+ ' unsafe { nanocom::CObject::from_object_unchecked(this) }.SetUnsafe(p, size)\n' +
102
+ ' }\n' +
103
+ ' extern "system" fn Some(this: &Object, p: &super::IMy::Object) -> bool {\n' +
104
+ ' unsafe { nanocom::CObject::from_object_unchecked(this) }.Some(p)\n' +
105
+ ' }\n' +
106
+ ' extern "system" fn GetIMy(this: &Object, a: u16, b: i16) -> super::IMy::Ref {\n' +
107
+ ' unsafe { nanocom::CObject::from_object_unchecked(this) }.GetIMy(a, b)\n' +
108
+ ' }\n' +
109
+ ' extern "system" fn SetManagedStruct(this: &Object, a: super::ManagedStruct) {\n' +
110
+ ' unsafe { nanocom::CObject::from_object_unchecked(this) }.SetManagedStruct(a)\n' +
111
+ ' }\n' +
112
+ ' }\n' +
113
+ ' impl<T> PrivateClassEx for T\n' +
114
+ ' where\n' +
115
+ ' Self: nanocom::Class<Interface = Interface>,\n' +
116
+ ' nanocom::CObject<Self>: Ex,\n' +
117
+ ' {\n' +
118
+ ' }\n' +
119
+ '}';
120
+ if (rust !== e) {
121
+ throw rust;
122
+ }
123
+ };
@@ -0,0 +1,7 @@
1
+ import * as text from '../../text/module.f.mjs';
2
+ const { flat } = text;
3
+ import * as string from '../../types/string/module.f.mjs';
4
+ const { join } = string;
5
+ import { rust } from './module.f.mjs';
6
+ import library from '../types/testlib.f.mjs';
7
+ export default join('\n')(flat(' ')(rust(library)));
@@ -0,0 +1,98 @@
1
+ import * as list from '../../types/list/module.f.mjs';
2
+ const { flat } = list;
3
+ import cppContent from '../cpp/testlib.f.mjs';
4
+ import csContent from '../cs/testlib.f.mjs';
5
+ import rustContent from '../rust/testlib.f.mjs';
6
+ /**
7
+ * @typedef {|
8
+ * 'aix' |
9
+ * 'android' |
10
+ * 'darwin' |
11
+ * 'freebsd' |
12
+ * 'haiku' |
13
+ * 'linux' |
14
+ * 'openbsd' |
15
+ * 'sunos' |
16
+ * 'win32' |
17
+ * 'cygwin' |
18
+ * 'netbsd'
19
+ * } Platform
20
+ */
21
+ /**
22
+ * @typedef {{
23
+ * readonly dirname: string
24
+ * readonly platform: Platform
25
+ * }} NodeJs
26
+ */
27
+ /**
28
+ * @typedef {{
29
+ * readonly file: {
30
+ * readonly name: string
31
+ * readonly content: string
32
+ * }
33
+ * readonly line: list.List<list.List<string>>
34
+ * }} Output
35
+ */
36
+ /** @typedef {(nodejs: NodeJs) => Output} Func */
37
+ /** @type {(platform: Platform) => readonly string[]} */
38
+ const flags = platform => {
39
+ switch (platform) {
40
+ case 'win32':
41
+ return [];
42
+ case 'linux':
43
+ return ['-std=c++17', '-lstdc++', '-fPIC'];
44
+ default:
45
+ return ['-std=c++17', '-lc++'];
46
+ }
47
+ };
48
+ /** @type {(platform: Platform) => (name: string) => string} */
49
+ const output = platform => name => {
50
+ switch (platform) {
51
+ case 'win32': return `${name}.dll`;
52
+ case 'darwin': return `lib${name}.dylib`;
53
+ default: return `lib${name}.so`;
54
+ }
55
+ };
56
+ /** @type {Func} */
57
+ const cpp = ({ dirname, platform }) => ({
58
+ file: {
59
+ name: `${dirname}/cpp/_result.hpp`,
60
+ content: cppContent(),
61
+ },
62
+ line: [
63
+ flat([
64
+ ['clang', '-shared', '-o', output(platform)('testc')],
65
+ flags(platform),
66
+ [`${dirname}/cpp/main.cpp`],
67
+ ]),
68
+ ],
69
+ });
70
+ /** @type {Func} */
71
+ const cs = ({ dirname, platform }) => ({
72
+ file: {
73
+ name: `${dirname}/cs/_result.cs`,
74
+ content: csContent,
75
+ },
76
+ line: [
77
+ platform === 'win32'
78
+ ? ['dotnet', 'run', '--project', `${dirname}/cs/cs.csproj`]
79
+ // .Net on Linux and MacOS doesn't properly support COM object marshalling
80
+ : ['dotnet', 'build', `${dirname}/cs/cs.csproj`]
81
+ ],
82
+ });
83
+ /** @type {Func} */
84
+ const rust = ({ dirname }) => ({
85
+ file: {
86
+ name: `${dirname}/rust/src/_result.rs`,
87
+ content: rustContent,
88
+ },
89
+ line: [['cargo', 'build' /**, '--locked' */]]
90
+ });
91
+ export default {
92
+ /** @readonly */
93
+ cpp,
94
+ /** @readonly */
95
+ cs,
96
+ /** @readonly */
97
+ rust,
98
+ };
@@ -0,0 +1,40 @@
1
+ import { writeFileSync } from 'node:fs';
2
+ import { execSync } from 'node:child_process';
3
+ import { platform, exit } from 'node:process';
4
+ import build, * as Build from './build.f.mjs';
5
+ const { cpp, cs, rust } = build;
6
+ import * as string from '../../types/string/module.f.mjs';
7
+ const { join } = string;
8
+ const { log, error } = console;
9
+ import * as sgr from '../../text/sgr/module.f.mjs';
10
+ const { bold, reset } = sgr.codes;
11
+ import * as list from '../../types/list/module.f.mjs';
12
+ import { fileURLToPath } from 'node:url';
13
+ import { dirname } from 'node:path';
14
+ const __filename = fileURLToPath(import.meta.url);
15
+ const __dirname = dirname(__filename);
16
+ const nodeJs = {
17
+ dirname: __dirname,
18
+ platform,
19
+ };
20
+ /** @type {(f: Build.Func) => void} */
21
+ const run = f => {
22
+ const { file: { name, content }, line } = f(nodeJs);
23
+ log(`${bold}writing: ${name}${reset}`);
24
+ writeFileSync(name, content);
25
+ for (const i of list.iterable(line)) {
26
+ const cmd = join(' ')(i);
27
+ log(`${bold}running: ${cmd}${reset}`);
28
+ try {
29
+ log(execSync(cmd).toString());
30
+ }
31
+ catch (e) {
32
+ // @ts-ignore
33
+ error(e.output.toString());
34
+ exit(-1);
35
+ }
36
+ }
37
+ };
38
+ run(cpp);
39
+ run(rust);
40
+ run(cs);
@@ -0,0 +1,51 @@
1
+ // @ts-self-types="./module.f.d.mts"
2
+ import * as O from '../../types/object/module.f.mjs';
3
+ import * as list from '../../types/list/module.f.mjs';
4
+ import * as f from '../../types/function/module.f.mjs';
5
+ const { compose } = f;
6
+ const { filter } = list;
7
+ const { entries } = Object;
8
+ /** @typedef {{readonly[k in string]: Definition}} Library */
9
+ /** @typedef {Struct|Interface} Definition */
10
+ /**
11
+ * @typedef {{
12
+ * readonly struct: FieldArray
13
+ * }} Struct
14
+ */
15
+ /** @typedef {{readonly[k in string]: Type}} FieldArray */
16
+ /** @typedef {O.Entry<Type>} Field */
17
+ /**
18
+ * @typedef {{
19
+ * readonly interface: MethodArray
20
+ * readonly guid: string
21
+ * }} Interface
22
+ */
23
+ /** @typedef {{readonly[k in string]: FieldArray}} MethodArray */
24
+ /** @typedef {O.Entry<FieldArray>} Method */
25
+ /** @typedef {BaseType|Id|Pointer} Type */
26
+ /** @typedef {readonly[string]} Id */
27
+ /**
28
+ * @typedef {|
29
+ * 'u8'|
30
+ * 'i8'|
31
+ * 'u16'|
32
+ * 'i16'|
33
+ * 'u32'|
34
+ * 'i32'|
35
+ * 'u64'|
36
+ * 'i64'|
37
+ * 'usize'|
38
+ * 'isize'|
39
+ * 'f32'|
40
+ * 'f64'|
41
+ * 'bool'
42
+ * } BaseType
43
+ */
44
+ /** @typedef {readonly['*', Type]} Pointer */
45
+ /** @type {(kv: O.Entry<Type>) => boolean} */
46
+ const isParam = ([name]) => name !== '_';
47
+ const filterParam = filter(isParam);
48
+ /** @type {(fa: FieldArray) => list.List<Field> } */
49
+ export const paramList = compose(entries)(filterParam);
50
+ /** @type {<T>(v: T) => (f: (type: Type) => T) => (fa: FieldArray) => T} */
51
+ export const result = v => f => fa => '_' in fa ? f(fa._) : v;
@@ -0,0 +1,30 @@
1
+ import * as _ from './module.f.mjs';
2
+ /** @type {_.Library} */
3
+ export default {
4
+ Slice: {
5
+ struct: {
6
+ Start: ['*', 'u8'],
7
+ Size: 'usize',
8
+ },
9
+ },
10
+ ManagedStruct: {
11
+ struct: {
12
+ M: ['IMy']
13
+ }
14
+ },
15
+ IMy: {
16
+ guid: 'C66FB270-2D80-49AD-BB6E-88C1F90B805D',
17
+ interface: {
18
+ GetSlice: { _: ['Slice'] },
19
+ SetSlice: { slice: ['Slice'] },
20
+ GetUnsafe: { _: ['*', 'bool'] },
21
+ SetUnsafe: {
22
+ p: ['*', ['Slice']],
23
+ size: 'u32'
24
+ },
25
+ Some: { p: ['IMy'], _: 'bool' },
26
+ GetIMy: { a: 'u16', b: 'i16', _: ['IMy'] },
27
+ SetManagedStruct: { a: ['ManagedStruct'] },
28
+ },
29
+ }
30
+ };
@@ -0,0 +1,107 @@
1
+ // @ts-self-types="./module.f.d.mts"
2
+ import * as package_ from '../package/module.f.mjs';
3
+ import * as module from '../module/module.f.mjs';
4
+ const { idToString, dir } = module;
5
+ import * as function_ from '../module/function/module.f.mjs';
6
+ import * as map from '../../types/map/module.f.mjs';
7
+ const { empty: mapEmpty, setReplace } = map;
8
+ import * as object from '../../types/object/module.f.mjs';
9
+ const { fromMap } = object;
10
+ import * as path from '../path/module.f.mjs';
11
+ const { parseAndFind } = path;
12
+ import * as stringSet from '../../types/string_set/module.f.mjs';
13
+ const { set: setSet, contains: setContains, empty: stringSetEmpty } = stringSet;
14
+ /**
15
+ * @template M
16
+ * @typedef {{
17
+ * readonly packageGet: package_.Get
18
+ * readonly moduleMapInterface: module.MapInterface<M>
19
+ * readonly moduleId: module.Id
20
+ * readonly moduleMap: M
21
+ * }} Config
22
+ */
23
+ /**
24
+ * @template M
25
+ * @typedef {readonly[module.State, M]} Result
26
+ */
27
+ /** @type {<M>(moduleMap: M) => Result<M>} */
28
+ const notFound = moduleMap => [['error', ['file not found']], moduleMap];
29
+ /**
30
+ * @type {(compile: function_.Compile) =>
31
+ * (packageGet: package_.Get) =>
32
+ * <M>(moduleMapInterface: module.MapInterface<M>) =>
33
+ * (moduleId: module.Id) =>
34
+ * (moduleMap: M) =>
35
+ * Result<M>
36
+ * }
37
+ */
38
+ export const getOrBuild = compile => packageGet => moduleMapInterface => {
39
+ /** @typedef {typeof moduleMapInterface extends module.MapInterface<infer M> ? M : never} M */
40
+ /**
41
+ * @type {(buildSet: stringSet.StringSet) =>
42
+ * (moduleId: module.Id) =>
43
+ * (source: string) =>
44
+ * (moduleMap: M) =>
45
+ * Result<M>}
46
+ */
47
+ const build = buildSet => moduleId => {
48
+ const moduleIdStr = idToString(moduleId);
49
+ const buildSet1 = setSet(moduleIdStr)(buildSet);
50
+ const moduleDir = dir(moduleId);
51
+ /** @type {function_.Require<readonly[map.Map<string>, M]>} */
52
+ const require_ = p => ([requireMap, m]) => {
53
+ /** @type {(e: unknown) => function_.Result<readonly[map.Map<string>, M]>} */
54
+ const error = e => [['error', e], [requireMap, m]];
55
+ if (moduleDir === null) {
56
+ return error('file not found');
57
+ }
58
+ const r = parseAndFind(packageGet)(moduleDir)(p);
59
+ if (r === null) {
60
+ return error('file not found');
61
+ }
62
+ const rIdStr = idToString(r.id);
63
+ if (setContains(rIdStr)(buildSet1)) {
64
+ return error('circular reference');
65
+ }
66
+ const [state, m1] = build(buildSet1)(r.id)(r.source)(m);
67
+ return [state[0] === 'error' ? state : ['ok', state[1].exports], [setReplace(p)(rIdStr)(requireMap), m1]];
68
+ };
69
+ return source => moduleMap => {
70
+ /** @type {(s: module.State) => (m: M) => Result<M>} */
71
+ const set = s => m => [s, moduleMapInterface.setReplace(moduleIdStr)(s)(m)];
72
+ /** @type {(e: module.Error) => (m: M) => Result<M>} */
73
+ const error = e => set(['error', e]);
74
+ // check compilation
75
+ const [kind, result] = compile(source);
76
+ if (kind === 'error') {
77
+ return error(['compilation error', result])(moduleMap);
78
+ }
79
+ // build
80
+ const [[state, value], [requireMap, moduleMap2]] = result(require_)([mapEmpty, moduleMap]);
81
+ const x = state === 'error' ?
82
+ error(['runtime error', value]) :
83
+ set(['ok', { exports: value, requireMap: fromMap(requireMap) }]);
84
+ return x(moduleMap2);
85
+ };
86
+ };
87
+ /** @type {(moduleId: module.Id) => (moduleMap: M) => Result<M>} */
88
+ const f = moduleId => moduleMap => {
89
+ const moduleIdStr = idToString(moduleId);
90
+ // check moduleMap
91
+ {
92
+ const m = moduleMapInterface.at(moduleIdStr)(moduleMap);
93
+ if (m !== null) {
94
+ return [m, moduleMap];
95
+ }
96
+ }
97
+ // check package
98
+ const p = packageGet(moduleId.package);
99
+ if (p === null) {
100
+ return notFound(moduleMap);
101
+ }
102
+ // check file
103
+ const source = p.file(moduleId.path.join('/'));
104
+ return (source === null ? notFound : build(stringSetEmpty)(moduleId)(source))(moduleMap);
105
+ };
106
+ return f;
107
+ };