functionalscript 0.3.14 → 0.4.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.
- package/package.json +2 -3
- package/types/bit_vec/module.f.js +6 -5
- package/com/cpp/module.f.d.ts +0 -10
- package/com/cpp/module.f.js +0 -106
- package/com/cpp/test.f.d.ts +0 -2
- package/com/cpp/test.f.js +0 -40
- package/com/cpp/testlib.f.d.ts +0 -2
- package/com/cpp/testlib.f.js +0 -5
- package/com/cs/module.f.d.ts +0 -12
- package/com/cs/module.f.js +0 -79
- package/com/cs/test.f.d.ts +0 -2
- package/com/cs/test.f.js +0 -43
- package/com/cs/testlib.f.d.ts +0 -2
- package/com/cs/testlib.f.js +0 -5
- package/com/rust/module.f.d.ts +0 -15
- package/com/rust/module.f.js +0 -164
- package/com/rust/test.f.d.ts +0 -2
- package/com/rust/test.f.js +0 -123
- package/com/rust/testlib.f.d.ts +0 -2
- package/com/rust/testlib.f.js +0 -5
- package/com/test/build.f.d.ts +0 -20
- package/com/test/build.f.js +0 -58
- package/com/types/module.f.d.ts +0 -28
- package/com/types/module.f.js +0 -7
- package/com/types/testlib.f.d.ts +0 -44
- package/com/types/testlib.f.js +0 -28
- package/commonjs/build/module.f.d.ts +0 -6
- package/commonjs/build/module.f.js +0 -66
- package/commonjs/build/test.f.d.ts +0 -2
- package/commonjs/build/test.f.js +0 -92
- package/commonjs/module/function/module.f.d.ts +0 -8
- package/commonjs/module/function/module.f.js +0 -4
- package/commonjs/module/module.f.d.ts +0 -18
- package/commonjs/module/module.f.js +0 -11
- package/commonjs/module.f.d.ts +0 -6
- package/commonjs/module.f.js +0 -1
- package/commonjs/package/dependencies/module.f.d.ts +0 -7
- package/commonjs/package/dependencies/module.f.js +0 -13
- package/commonjs/package/dependencies/test.f.d.ts +0 -2
- package/commonjs/package/dependencies/test.f.js +0 -15
- package/commonjs/package/module.f.d.ts +0 -17
- package/commonjs/package/module.f.js +0 -18
- package/commonjs/package/test.f.d.ts +0 -2
- package/commonjs/package/test.f.js +0 -27
- package/commonjs/path/module.f.d.ts +0 -24
- package/commonjs/path/module.f.js +0 -112
- package/commonjs/path/test.f.d.ts +0 -25
- package/commonjs/path/test.f.js +0 -221
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "functionalscript",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"files": [
|
|
6
6
|
"**/*.f.d.ts",
|
|
@@ -13,7 +13,6 @@
|
|
|
13
13
|
"test20": "npm run prepack && node --trace-uncaught ./dev/test.js",
|
|
14
14
|
"test22": "tsc && node --experimental-strip-types --trace-uncaught ./dev/test.ts",
|
|
15
15
|
"test": "tsc && node --trace-uncaught ./dev/test.ts",
|
|
16
|
-
"comtest": "node ./com/test/build.ts",
|
|
17
16
|
"index": "node ./dev/index.ts",
|
|
18
17
|
"version": "node ./nodejs/version/main.ts",
|
|
19
18
|
"fsc": "node ./main.ts",
|
|
@@ -40,6 +39,6 @@
|
|
|
40
39
|
"homepage": "https://github.com/functionalscript/functionalscript#readme",
|
|
41
40
|
"devDependencies": {
|
|
42
41
|
"@types/node": "^22.10.5",
|
|
43
|
-
"typescript": "^5.7.
|
|
42
|
+
"typescript": "^5.7.3"
|
|
44
43
|
}
|
|
45
44
|
}
|
|
@@ -1,14 +1,15 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* @module
|
|
3
|
-
* @description
|
|
4
|
-
* ```
|
|
5
2
|
* MSb is most-significant bit first.
|
|
3
|
+
* ```
|
|
6
4
|
* - byte: 0x53 = 0b0101_0011
|
|
7
|
-
* -
|
|
5
|
+
* - 0123_4567
|
|
6
|
+
* ```
|
|
8
7
|
* LSb is least-significant bit first.
|
|
8
|
+
* ```
|
|
9
9
|
* - byte: 0x53 = 0b0101_0011
|
|
10
|
-
* -
|
|
10
|
+
* - 7654_3210
|
|
11
11
|
* ```
|
|
12
|
+
* @module
|
|
12
13
|
*/
|
|
13
14
|
import { log2, mask } from "../bigint/module.f.js";
|
|
14
15
|
import { flip } from "../function/module.f.js";
|
package/com/cpp/module.f.d.ts
DELETED
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* This module generates C++ code blocks, including structs, interfaces, and method headers,
|
|
3
|
-
* based on a COM library of type definitions.
|
|
4
|
-
*/
|
|
5
|
-
import { type Library } from '../types/module.f.ts';
|
|
6
|
-
import * as text from '../../text/module.f.ts';
|
|
7
|
-
/**
|
|
8
|
-
* Generates the C++ code for a library.
|
|
9
|
-
*/
|
|
10
|
-
export declare const cpp: (name: string) => (lib: Library) => text.Block;
|
package/com/cpp/module.f.js
DELETED
|
@@ -1,106 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* This module generates C++ code blocks, including structs, interfaces, and method headers,
|
|
3
|
-
* based on a COM library of type definitions.
|
|
4
|
-
*/
|
|
5
|
-
import { paramList, result, } from "../types/module.f.js";
|
|
6
|
-
import * as text from "../../text/module.f.js";
|
|
7
|
-
import { map, flatMap, flat } from "../../types/list/module.f.js";
|
|
8
|
-
import { join } from "../../types/string/module.f.js";
|
|
9
|
-
const { entries } = Object;
|
|
10
|
-
const struct = (name) => (body) => [`struct ${name}`, '{', body, '};'];
|
|
11
|
-
const baseTypeMap = {
|
|
12
|
-
u8: 'uint8_t',
|
|
13
|
-
i8: 'int8_t',
|
|
14
|
-
u16: 'uint16_t',
|
|
15
|
-
i16: 'int16_t',
|
|
16
|
-
u32: 'uint32_t',
|
|
17
|
-
i32: 'int32_t',
|
|
18
|
-
u64: 'uint64_t',
|
|
19
|
-
i64: 'int64_t',
|
|
20
|
-
usize: 'size_t',
|
|
21
|
-
isize: 'ptrdiff_t',
|
|
22
|
-
f32: 'float',
|
|
23
|
-
f64: 'double',
|
|
24
|
-
bool: 'bool',
|
|
25
|
-
};
|
|
26
|
-
const baseType = (t) => baseTypeMap[t];
|
|
27
|
-
const resultVoid = result('void');
|
|
28
|
-
const namespace = text.curly('namespace');
|
|
29
|
-
const comRef = (id) => `::nanocom::ref<${id}>`;
|
|
30
|
-
const ptr = (id) => `${id} const*`;
|
|
31
|
-
const ref = (id) => `${id} const&`;
|
|
32
|
-
const paramName = ([name]) => name;
|
|
33
|
-
const mapParamName = map(paramName);
|
|
34
|
-
const joinComma = join(', ');
|
|
35
|
-
/**
|
|
36
|
-
* Generates the C++ code for a library.
|
|
37
|
-
*/
|
|
38
|
-
export const cpp = (name) => (lib) => {
|
|
39
|
-
const interface_ = (t) => {
|
|
40
|
-
if (!(t instanceof Array) || t.length !== 1) {
|
|
41
|
-
return null;
|
|
42
|
-
}
|
|
43
|
-
const [name] = t;
|
|
44
|
-
return 'interface' in lib[name] ? name : null;
|
|
45
|
-
};
|
|
46
|
-
const objectType = (i) => (t) => {
|
|
47
|
-
if (typeof (t) === 'string') {
|
|
48
|
-
return baseType(t);
|
|
49
|
-
}
|
|
50
|
-
if (t.length === 2) {
|
|
51
|
-
return `${type(t[1])} const*`;
|
|
52
|
-
}
|
|
53
|
-
const [id] = t;
|
|
54
|
-
return 'interface' in lib[id] ? i(id) : id;
|
|
55
|
-
};
|
|
56
|
-
const type = objectType(comRef);
|
|
57
|
-
const resultType = objectType(ptr);
|
|
58
|
-
const field = ([name, t]) => `${type(t)} ${name};`;
|
|
59
|
-
const mapField = map(field);
|
|
60
|
-
const defStruct = (s) => mapField(entries(s.struct));
|
|
61
|
-
const cppResult = resultVoid(resultType);
|
|
62
|
-
const param = ([name, t]) => `${objectType(ref)(t)} ${name}`;
|
|
63
|
-
const mapParam = map(param);
|
|
64
|
-
const methodHeader = (result) => (paramArrayStr) => (name) => `virtual ${result} ${name}${paramArrayStr} const noexcept = 0;`;
|
|
65
|
-
const method = ([name, paramArray]) => {
|
|
66
|
-
const result = cppResult(paramArray);
|
|
67
|
-
const paramL = paramList(paramArray);
|
|
68
|
-
const paramArrayStr = `(${joinComma(mapParam(paramL))})`;
|
|
69
|
-
const m = methodHeader(result)(paramArrayStr);
|
|
70
|
-
const resultName = interface_(paramArray._);
|
|
71
|
-
if (resultName === null) {
|
|
72
|
-
return [m(name)];
|
|
73
|
-
}
|
|
74
|
-
return [
|
|
75
|
-
m(`${name}_`),
|
|
76
|
-
`${comRef(resultName)} ${name}${paramArrayStr} const noexcept`,
|
|
77
|
-
'{',
|
|
78
|
-
[`return ::nanocom::move_to_ref(${name}_(${joinComma(mapParamName(paramL))}));`],
|
|
79
|
-
'}',
|
|
80
|
-
];
|
|
81
|
-
};
|
|
82
|
-
const mapMethod = flatMap(method);
|
|
83
|
-
const defInterface = ({ guid, interface: i }) => {
|
|
84
|
-
const g = guid.replaceAll('-', '');
|
|
85
|
-
const lo = g.substring(0, 16);
|
|
86
|
-
const hi = g.substring(16);
|
|
87
|
-
return flat([
|
|
88
|
-
[`constexpr static ::nanocom::GUID const guid = ::nanocom::GUID(0x${lo}, 0x${hi});`],
|
|
89
|
-
mapMethod(entries(i))
|
|
90
|
-
]);
|
|
91
|
-
};
|
|
92
|
-
const def = ([name, d]) => 'interface' in d ?
|
|
93
|
-
[`class ${name} : public ::nanocom::IUnknown`,
|
|
94
|
-
'{',
|
|
95
|
-
'public:',
|
|
96
|
-
defInterface(d),
|
|
97
|
-
'};'
|
|
98
|
-
] :
|
|
99
|
-
struct(name)(defStruct(d));
|
|
100
|
-
const forward = ([name]) => [`struct ${name};`];
|
|
101
|
-
const e = entries(lib);
|
|
102
|
-
return flat([
|
|
103
|
-
['#pragma once', ''],
|
|
104
|
-
namespace(name)(flat([flatMap(forward)(e), flatMap(def)(e)]))
|
|
105
|
-
]);
|
|
106
|
-
};
|
package/com/cpp/test.f.d.ts
DELETED
package/com/cpp/test.f.js
DELETED
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
import cpp from "./testlib.f.js";
|
|
2
|
-
const f = () => {
|
|
3
|
-
const e = '#pragma once\n' +
|
|
4
|
-
'\n' +
|
|
5
|
-
'namespace My\n' +
|
|
6
|
-
'{\n' +
|
|
7
|
-
' struct Slice;\n' +
|
|
8
|
-
' struct ManagedStruct;\n' +
|
|
9
|
-
' struct IMy;\n' +
|
|
10
|
-
' struct Slice\n' +
|
|
11
|
-
' {\n' +
|
|
12
|
-
' uint8_t const* Start;\n' +
|
|
13
|
-
' size_t Size;\n' +
|
|
14
|
-
' };\n' +
|
|
15
|
-
' struct ManagedStruct\n' +
|
|
16
|
-
' {\n' +
|
|
17
|
-
' ::nanocom::ref<IMy> M;\n' +
|
|
18
|
-
' };\n' +
|
|
19
|
-
' class IMy : public ::nanocom::IUnknown\n' +
|
|
20
|
-
' {\n' +
|
|
21
|
-
' public:\n' +
|
|
22
|
-
' constexpr static ::nanocom::GUID const guid = ::nanocom::GUID(0xC66FB2702D8049AD, 0xBB6E88C1F90B805D);\n' +
|
|
23
|
-
' virtual Slice GetSlice() const noexcept = 0;\n' +
|
|
24
|
-
' virtual void SetSlice(Slice slice) const noexcept = 0;\n' +
|
|
25
|
-
' virtual bool const* GetUnsafe() const noexcept = 0;\n' +
|
|
26
|
-
' virtual void SetUnsafe(Slice const* p, uint32_t size) const noexcept = 0;\n' +
|
|
27
|
-
' virtual bool Some(IMy const& p) const noexcept = 0;\n' +
|
|
28
|
-
' virtual IMy const* GetIMy_(uint16_t a, int16_t b) const noexcept = 0;\n' +
|
|
29
|
-
' ::nanocom::ref<IMy> GetIMy(uint16_t a, int16_t b) const noexcept\n' +
|
|
30
|
-
' {\n' +
|
|
31
|
-
' return ::nanocom::move_to_ref(GetIMy_(a, b));\n' +
|
|
32
|
-
' }\n' +
|
|
33
|
-
' virtual void SetManagedStruct(ManagedStruct a) const noexcept = 0;\n' +
|
|
34
|
-
' };\n' +
|
|
35
|
-
'}';
|
|
36
|
-
if (cpp() !== e) {
|
|
37
|
-
throw cpp;
|
|
38
|
-
}
|
|
39
|
-
};
|
|
40
|
-
export default f;
|
package/com/cpp/testlib.f.d.ts
DELETED
package/com/cpp/testlib.f.js
DELETED
package/com/cs/module.f.d.ts
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* This module generates C# code blocks for COM interop from a high-level type library definition.
|
|
3
|
-
*
|
|
4
|
-
* The module maps type definitions (e.g., structs, interfaces, and methods) into C# constructs
|
|
5
|
-
* with appropriate attributes for COM interop, such as `[StructLayout]`, `[Guid]`, and `[InterfaceType]`.
|
|
6
|
-
*/
|
|
7
|
-
import { type Library } from '../types/module.f.ts';
|
|
8
|
-
import { type Block } from '../../text/module.f.ts';
|
|
9
|
-
/**
|
|
10
|
-
* Generates the C# code for a library.
|
|
11
|
-
*/
|
|
12
|
-
export declare const cs: (name: string) => (library: Library) => Block;
|
package/com/cs/module.f.js
DELETED
|
@@ -1,79 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* This module generates C# code blocks for COM interop from a high-level type library definition.
|
|
3
|
-
*
|
|
4
|
-
* The module maps type definitions (e.g., structs, interfaces, and methods) into C# constructs
|
|
5
|
-
* with appropriate attributes for COM interop, such as `[StructLayout]`, `[Guid]`, and `[InterfaceType]`.
|
|
6
|
-
*/
|
|
7
|
-
import { result, paramList, } from "../types/module.f.js";
|
|
8
|
-
import { curly } from "../../text/module.f.js";
|
|
9
|
-
import { flat, map, some, flatMap } from "../../types/list/module.f.js";
|
|
10
|
-
import { join } from "../../types/string/module.f.js";
|
|
11
|
-
const { entries } = Object;
|
|
12
|
-
const using = (v) => `using ${v};`;
|
|
13
|
-
const typeDef = (attributes) => (type) => (name) => (body) => flat([
|
|
14
|
-
map(v => `[${v}]`)(attributes),
|
|
15
|
-
curly(`public ${type}`)(name)(body)
|
|
16
|
-
]);
|
|
17
|
-
const baseTypeMap = {
|
|
18
|
-
bool: 'byte',
|
|
19
|
-
f32: 'float',
|
|
20
|
-
f64: 'double',
|
|
21
|
-
i16: 'short',
|
|
22
|
-
i32: 'int',
|
|
23
|
-
i64: 'long',
|
|
24
|
-
i8: 'sbyte',
|
|
25
|
-
isize: 'IntPtr',
|
|
26
|
-
u16: 'ushort',
|
|
27
|
-
u32: 'uint',
|
|
28
|
-
u64: 'ulong',
|
|
29
|
-
u8: 'byte',
|
|
30
|
-
usize: 'UIntPtr',
|
|
31
|
-
};
|
|
32
|
-
const baseType = (t) => baseTypeMap[t];
|
|
33
|
-
const unsafe = (isUnsafe) => isUnsafe ? 'unsafe ' : '';
|
|
34
|
-
const fullType = (t) => typeof (t) === 'string' ?
|
|
35
|
-
[false, baseType(t)] :
|
|
36
|
-
t.length === 1 ?
|
|
37
|
-
[false, t[0]] :
|
|
38
|
-
[true, `${type(t[1])}*`];
|
|
39
|
-
const type = (t) => fullType(t)[1];
|
|
40
|
-
const param = ([name, t]) => `${type(t)} ${name}`;
|
|
41
|
-
const mapParam = map(param);
|
|
42
|
-
const field = ([name, comType]) => {
|
|
43
|
-
const [isUnsafe, t] = fullType(comType);
|
|
44
|
-
return `public ${unsafe(isUnsafe)}${t} ${name};`;
|
|
45
|
-
};
|
|
46
|
-
const isUnsafeField = (field) => fullType(field[1])[0];
|
|
47
|
-
const mapIsUnsafeField = map(isUnsafeField);
|
|
48
|
-
const resultVoid = result('void');
|
|
49
|
-
const joinComma = join(', ');
|
|
50
|
-
const method = ([name, m]) => {
|
|
51
|
-
const paramAndResultList = entries(m);
|
|
52
|
-
const pl = paramList(m);
|
|
53
|
-
const isUnsafe = some(mapIsUnsafeField(paramAndResultList));
|
|
54
|
-
return [
|
|
55
|
-
'[PreserveSig]',
|
|
56
|
-
`${unsafe(isUnsafe)}${resultVoid(type)(m)} ${name}(${joinComma(mapParam(pl))});`
|
|
57
|
-
];
|
|
58
|
-
};
|
|
59
|
-
const struct = typeDef(['StructLayout(LayoutKind.Sequential)'])('struct');
|
|
60
|
-
const mapField = map(field);
|
|
61
|
-
const flatMapMethod = flatMap(method);
|
|
62
|
-
const def = ([n, d]) => !('interface' in d) ?
|
|
63
|
-
struct(n)(mapField(entries(d.struct))) :
|
|
64
|
-
typeDef([`Guid("${d.guid}")`, 'InterfaceType(ComInterfaceType.InterfaceIsIUnknown)'])('interface')(n)(flatMapMethod(entries(d.interface)));
|
|
65
|
-
const flatMapDef = flatMap(def);
|
|
66
|
-
const namespace = curly('namespace');
|
|
67
|
-
const header = [
|
|
68
|
-
using('System'),
|
|
69
|
-
using('System.Runtime.InteropServices'),
|
|
70
|
-
''
|
|
71
|
-
];
|
|
72
|
-
/**
|
|
73
|
-
* Generates the C# code for a library.
|
|
74
|
-
*/
|
|
75
|
-
export const cs = (name) => (library) => {
|
|
76
|
-
const v = flatMapDef(entries(library));
|
|
77
|
-
const ns = namespace(name)(v);
|
|
78
|
-
return flat([header, ns]);
|
|
79
|
-
};
|
package/com/cs/test.f.d.ts
DELETED
package/com/cs/test.f.js
DELETED
|
@@ -1,43 +0,0 @@
|
|
|
1
|
-
import cs from "./testlib.f.js";
|
|
2
|
-
const f = () => {
|
|
3
|
-
const e = 'using System;\n' +
|
|
4
|
-
'using System.Runtime.InteropServices;\n' +
|
|
5
|
-
'\n' +
|
|
6
|
-
'namespace My\n' +
|
|
7
|
-
'{\n' +
|
|
8
|
-
' [StructLayout(LayoutKind.Sequential)]\n' +
|
|
9
|
-
' public struct Slice\n' +
|
|
10
|
-
' {\n' +
|
|
11
|
-
' public unsafe byte* Start;\n' +
|
|
12
|
-
' public UIntPtr Size;\n' +
|
|
13
|
-
' }\n' +
|
|
14
|
-
' [StructLayout(LayoutKind.Sequential)]\n' +
|
|
15
|
-
' public struct ManagedStruct\n' +
|
|
16
|
-
' {\n' +
|
|
17
|
-
' public IMy M;\n' +
|
|
18
|
-
' }\n' +
|
|
19
|
-
' [Guid("C66FB270-2D80-49AD-BB6E-88C1F90B805D")]\n' +
|
|
20
|
-
' [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]\n' +
|
|
21
|
-
' public interface IMy\n' +
|
|
22
|
-
' {\n' +
|
|
23
|
-
' [PreserveSig]\n' +
|
|
24
|
-
' Slice GetSlice();\n' +
|
|
25
|
-
' [PreserveSig]\n' +
|
|
26
|
-
' void SetSlice(Slice slice);\n' +
|
|
27
|
-
' [PreserveSig]\n' +
|
|
28
|
-
' unsafe byte* GetUnsafe();\n' +
|
|
29
|
-
' [PreserveSig]\n' +
|
|
30
|
-
' unsafe void SetUnsafe(Slice* p, uint size);\n' +
|
|
31
|
-
' [PreserveSig]\n' +
|
|
32
|
-
' byte Some(IMy p);\n' +
|
|
33
|
-
' [PreserveSig]\n' +
|
|
34
|
-
' IMy GetIMy(ushort a, short b);\n' +
|
|
35
|
-
' [PreserveSig]\n' +
|
|
36
|
-
' void SetManagedStruct(ManagedStruct a);\n' +
|
|
37
|
-
' }\n' +
|
|
38
|
-
'}';
|
|
39
|
-
if (cs !== e) {
|
|
40
|
-
throw cs;
|
|
41
|
-
}
|
|
42
|
-
};
|
|
43
|
-
export default f;
|
package/com/cs/testlib.f.d.ts
DELETED
package/com/cs/testlib.f.js
DELETED
package/com/rust/module.f.d.ts
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* This module generates Rust code for COM interop from a high-level type library definition.
|
|
3
|
-
*
|
|
4
|
-
* The module provides functions to define structs, traits, and implementations in Rust,
|
|
5
|
-
* specifically tailored for `nanocom` interpretation.
|
|
6
|
-
*/
|
|
7
|
-
import { type Library } from '../types/module.f.ts';
|
|
8
|
-
import type * as text from '../../text/module.f.ts';
|
|
9
|
-
/**
|
|
10
|
-
* Generates Rust code for the given type library.
|
|
11
|
-
*
|
|
12
|
-
* @param library - The library of type definitions to generate Rust code for.
|
|
13
|
-
* @returns A block of Rust code representing the library.
|
|
14
|
-
*/
|
|
15
|
-
export declare const rust: (library: Library) => text.Block;
|
package/com/rust/module.f.js
DELETED
|
@@ -1,164 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* This module generates Rust code for COM interop from a high-level type library definition.
|
|
3
|
-
*
|
|
4
|
-
* The module provides functions to define structs, traits, and implementations in Rust,
|
|
5
|
-
* specifically tailored for `nanocom` interpretation.
|
|
6
|
-
*/
|
|
7
|
-
import { paramList } from "../types/module.f.js";
|
|
8
|
-
import { flat, map, flatMap } from "../../types/list/module.f.js";
|
|
9
|
-
import { fn } from "../../types/function/module.f.js";
|
|
10
|
-
import { join } from "../../types/string/module.f.js";
|
|
11
|
-
const { entries } = Object;
|
|
12
|
-
const rustField = (field) => `pub ${field},`;
|
|
13
|
-
const mapRustField = map(rustField);
|
|
14
|
-
const rustStruct = (b) => (name) => [`#[repr(C)]`, `pub struct ${name} {`, mapRustField(b), `}`];
|
|
15
|
-
const commaJoin = join(', ');
|
|
16
|
-
const ref = (name) => `${name}::Ref`;
|
|
17
|
-
const obj = (name) => `&${name}::Object`;
|
|
18
|
-
const self = ['&self'];
|
|
19
|
-
const paramName = ([n]) => n;
|
|
20
|
-
const callList = (p) => map(paramName)(paramList(p));
|
|
21
|
-
const call = (p) => commaJoin(callList(p));
|
|
22
|
-
const virtualCall = (p) => commaJoin(flat([['self'], callList(p)]));
|
|
23
|
-
const super_ = 'super::';
|
|
24
|
-
const assign = ([n]) => `${n}: Self::${n},`;
|
|
25
|
-
const mapAssign = map(assign);
|
|
26
|
-
const this_ = ['this: &Object'];
|
|
27
|
-
const rustType = (n) => `pub type ${n} = nanocom::${n}<Interface>;`;
|
|
28
|
-
const whereContent = (h) => (wh) => {
|
|
29
|
-
const w = 'where' in wh ? [
|
|
30
|
-
h,
|
|
31
|
-
`where`,
|
|
32
|
-
mapComma(wh.where),
|
|
33
|
-
'{'
|
|
34
|
-
] : [`${h} {`];
|
|
35
|
-
const x = [
|
|
36
|
-
wh.content,
|
|
37
|
-
'}',
|
|
38
|
-
];
|
|
39
|
-
return flat([w, x]);
|
|
40
|
-
};
|
|
41
|
-
const rustImpl = (i) => {
|
|
42
|
-
const p = 'param' in i ? `<${i.param}>` : '';
|
|
43
|
-
const header = `impl${p} ${i.trait} for ${i.type}`;
|
|
44
|
-
return whereContent(header)(i);
|
|
45
|
-
};
|
|
46
|
-
const comma = s => `${s},`;
|
|
47
|
-
const mapComma = map(comma);
|
|
48
|
-
const trait = (t) => {
|
|
49
|
-
const p = t.pub === true ? 'pub ' : '';
|
|
50
|
-
const h = `${p}trait ${t.type}`;
|
|
51
|
-
return whereContent(h)(t);
|
|
52
|
-
};
|
|
53
|
-
const traitImpl = (t) => {
|
|
54
|
-
const i = rustImpl({
|
|
55
|
-
param: 'T',
|
|
56
|
-
trait: t.type,
|
|
57
|
-
type: 'T',
|
|
58
|
-
where,
|
|
59
|
-
content: [],
|
|
60
|
-
});
|
|
61
|
-
return flat([trait({ ...t, where }), i]);
|
|
62
|
-
};
|
|
63
|
-
const where = ['Self: nanocom::Class<Interface = Interface>', 'nanocom::CObject<Self>: Ex'];
|
|
64
|
-
/**
|
|
65
|
-
* Generates Rust code for the given type library.
|
|
66
|
-
*
|
|
67
|
-
* @param library - The library of type definitions to generate Rust code for.
|
|
68
|
-
* @returns A block of Rust code representing the library.
|
|
69
|
-
*/
|
|
70
|
-
export const rust = (library) => {
|
|
71
|
-
const type = (p) => {
|
|
72
|
-
const f = (o) => (t) => {
|
|
73
|
-
if (typeof t === 'string') {
|
|
74
|
-
return t;
|
|
75
|
-
}
|
|
76
|
-
if (t.length === 2) {
|
|
77
|
-
return `*const ${f(ref)(t[1])}`;
|
|
78
|
-
}
|
|
79
|
-
const [id] = t;
|
|
80
|
-
const fullId = `${p}${id}`;
|
|
81
|
-
return 'interface' in library[id] ? o(fullId) : fullId;
|
|
82
|
-
};
|
|
83
|
-
return f;
|
|
84
|
-
};
|
|
85
|
-
const pf = (p) => (o) => ([name, t]) => `${name}: ${type(p)(o)(t)}`;
|
|
86
|
-
const param = pf(super_)(obj);
|
|
87
|
-
const mapParam = map(param);
|
|
88
|
-
const mapField = map(pf('')(ref));
|
|
89
|
-
const struct = fn(entries)
|
|
90
|
-
.then(mapField)
|
|
91
|
-
.then(rustStruct)
|
|
92
|
-
.result;
|
|
93
|
-
const func = (first) => (p) => {
|
|
94
|
-
const resultStr = '_' in p ? ` -> ${type(super_)(ref)(p._)}` : '';
|
|
95
|
-
const params = commaJoin(flat([first, mapParam(paramList(p))]));
|
|
96
|
-
return `(${params})${resultStr}`;
|
|
97
|
-
};
|
|
98
|
-
const virtualFnType = (n) => (p) => `extern "system" fn${n}${func(this_)(p)}`;
|
|
99
|
-
const virtualFn = ([n, p]) => `${n}: unsafe ${virtualFnType('')(p)}`;
|
|
100
|
-
const mapVirtualFn = map(virtualFn);
|
|
101
|
-
const headerFn = ([n, p]) => `fn ${n}${func(self)(p)}`;
|
|
102
|
-
const traitFn = (m) => `${headerFn(m)};`;
|
|
103
|
-
const mapTraitFn = map(traitFn);
|
|
104
|
-
const implFn = (m) => {
|
|
105
|
-
const [n, p] = m;
|
|
106
|
-
return [
|
|
107
|
-
`${headerFn(m)} {`,
|
|
108
|
-
[`unsafe { (self.interface().${n})(${virtualCall(p)}) }`],
|
|
109
|
-
'}'
|
|
110
|
-
];
|
|
111
|
-
};
|
|
112
|
-
const flatMapImplFn = flatMap(implFn);
|
|
113
|
-
const impl = ([n, p]) => {
|
|
114
|
-
const type = virtualFnType(` ${n}`)(p);
|
|
115
|
-
return [
|
|
116
|
-
`${type} {`,
|
|
117
|
-
[`unsafe { nanocom::CObject::from_object_unchecked(this) }.${n}(${call(p)})`],
|
|
118
|
-
'}'
|
|
119
|
-
];
|
|
120
|
-
};
|
|
121
|
-
const flatMapImpl = flatMap(impl);
|
|
122
|
-
const interface_ = ({ interface: i, guid }) => (name) => {
|
|
123
|
-
const e = entries(i);
|
|
124
|
-
return [
|
|
125
|
-
`pub mod ${name} {`,
|
|
126
|
-
[
|
|
127
|
-
rustType('Object'),
|
|
128
|
-
rustType('Ref'),
|
|
129
|
-
rustType('Vmt'),
|
|
130
|
-
],
|
|
131
|
-
rustStruct(mapVirtualFn(e))('Interface'),
|
|
132
|
-
rustImpl({
|
|
133
|
-
trait: 'nanocom::Interface',
|
|
134
|
-
type: 'Interface',
|
|
135
|
-
content: [`const GUID: nanocom::GUID = 0x${guid.replaceAll('-', '_')};`]
|
|
136
|
-
}),
|
|
137
|
-
trait({ pub: true, type: 'Ex', content: mapTraitFn(e) }),
|
|
138
|
-
rustImpl({
|
|
139
|
-
trait: 'Ex',
|
|
140
|
-
type: 'Object',
|
|
141
|
-
content: flatMapImplFn(e)
|
|
142
|
-
}),
|
|
143
|
-
traitImpl({
|
|
144
|
-
pub: true,
|
|
145
|
-
type: 'ClassEx',
|
|
146
|
-
content: ['const VMT: Vmt = Vmt {',
|
|
147
|
-
['iunknown: nanocom::CObject::<Self>::IUNKNOWN,',
|
|
148
|
-
'interface: Interface {',
|
|
149
|
-
mapAssign(e),
|
|
150
|
-
'},',
|
|
151
|
-
],
|
|
152
|
-
'};'
|
|
153
|
-
]
|
|
154
|
-
}),
|
|
155
|
-
traitImpl({
|
|
156
|
-
type: 'PrivateClassEx',
|
|
157
|
-
content: flatMapImpl(e)
|
|
158
|
-
}),
|
|
159
|
-
'}'
|
|
160
|
-
];
|
|
161
|
-
};
|
|
162
|
-
const def = ([name, type]) => ('interface' in type ? interface_(type) : struct(type.struct))(name);
|
|
163
|
-
return flat([['#![allow(non_snake_case)]'], flatMap(def)(entries(library))]);
|
|
164
|
-
};
|
package/com/rust/test.f.d.ts
DELETED