functionalscript 0.0.351 → 0.0.354
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/com/main.f.js +110 -4
- package/com/test.f.js +31 -5
- package/commonjs/README.md +2 -2
- package/package.json +1 -1
- package/test.f.js +1 -0
- package/text/main.f.js +17 -11
- package/text/test.f.js +7 -6
- package/types/list/main.f.js +6 -2
- package/types/list/test.f.js +10 -0
package/com/main.f.js
CHANGED
|
@@ -1,8 +1,17 @@
|
|
|
1
|
+
const text = require('../text/main.f.js')
|
|
2
|
+
const list = require('../types/list/main.f')
|
|
3
|
+
const obj = require('../types/object/main.f.js')
|
|
4
|
+
|
|
1
5
|
/** @typedef {{readonly[k in string]: Definition}} Library */
|
|
2
6
|
|
|
3
7
|
/** @typedef {Struct|Interface} Definition */
|
|
4
8
|
|
|
5
|
-
/**
|
|
9
|
+
/**
|
|
10
|
+
* @typedef {{
|
|
11
|
+
* readonly interface?: undefined
|
|
12
|
+
* readonly struct: FieldArray
|
|
13
|
+
* }} Struct
|
|
14
|
+
*/
|
|
6
15
|
|
|
7
16
|
/** @typedef {readonly Field[]} FieldArray */
|
|
8
17
|
|
|
@@ -17,14 +26,111 @@
|
|
|
17
26
|
|
|
18
27
|
/** @typedef {readonly Method[]} MethodArray */
|
|
19
28
|
|
|
20
|
-
/** @typedef {readonly[string, FieldArray, Type]}
|
|
29
|
+
/** @typedef {readonly[string, FieldArray, Type]} GetMethod */
|
|
30
|
+
|
|
31
|
+
/** @typedef {readonly[string, FieldArray]} SetMethod */
|
|
32
|
+
|
|
33
|
+
/** @typedef {GetMethod|SetMethod} Method */
|
|
21
34
|
|
|
22
35
|
/** @typedef {BaseType|Id|Pointer} Type */
|
|
23
36
|
|
|
24
37
|
/** @typedef {readonly[string]} Id */
|
|
25
38
|
|
|
26
|
-
/** @typedef {'u8'|'i8'|'u16'|'i16'|'u32'|'i32'|'u64'|'i64'|'usize'|'isize'|'f32'|'f64'|'bool'
|
|
39
|
+
/** @typedef {'u8'|'i8'|'u16'|'i16'|'u32'|'i32'|'u64'|'i64'|'usize'|'isize'|'f32'|'f64'|'bool'} BaseType */
|
|
27
40
|
|
|
28
41
|
/** @typedef {{readonly '*': Type}} Pointer */
|
|
29
42
|
|
|
30
|
-
|
|
43
|
+
/** @type {(v: string) => string} */
|
|
44
|
+
const csUsing = v => `using ${v};`
|
|
45
|
+
|
|
46
|
+
/** @type {(type: string) => (name: string) => (body: text.Block) => text.Block} */
|
|
47
|
+
const csBlock = type => name => body => [`${type} ${name}`, '{', body, '}']
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* @type {(attributes: list.List<string>) =>
|
|
51
|
+
* (type: string) =>
|
|
52
|
+
* (name: string) =>
|
|
53
|
+
* (body: text.Block) =>
|
|
54
|
+
* list.List<text.Item>}
|
|
55
|
+
*/
|
|
56
|
+
const csTypeDef = attributes => type => name => body =>
|
|
57
|
+
list.flat([
|
|
58
|
+
list.map(v=>`[${v}]`)(attributes),
|
|
59
|
+
csBlock(`public ${type}`)(name)(body)
|
|
60
|
+
])
|
|
61
|
+
|
|
62
|
+
/** @type {(t: BaseType) => string} */
|
|
63
|
+
const csBaseType = t => {
|
|
64
|
+
switch (t) {
|
|
65
|
+
case 'bool': return 'bool'
|
|
66
|
+
case 'f32': return 'float'
|
|
67
|
+
case 'f64': return 'double'
|
|
68
|
+
case 'i16': return 'short'
|
|
69
|
+
case 'i32': return 'int'
|
|
70
|
+
case 'i64': return 'long'
|
|
71
|
+
case 'i8': return 'sbyte'
|
|
72
|
+
case 'isize': return 'IntPtr'
|
|
73
|
+
case 'u16': return 'ushort'
|
|
74
|
+
case 'u32': return 'uint'
|
|
75
|
+
case 'u64': return 'ulong'
|
|
76
|
+
case 'u8': return 'byte'
|
|
77
|
+
case 'usize': return 'UIntPtr'
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/** @type {(t: Type) => string} */
|
|
82
|
+
const csType = t =>
|
|
83
|
+
typeof(t) === 'string' ? csBaseType(t) :
|
|
84
|
+
t instanceof Array ? t[0] :
|
|
85
|
+
`${csType(t['*'])}*`
|
|
86
|
+
|
|
87
|
+
/** @type {(f: Field) => string} */
|
|
88
|
+
const csParam = ([name, type]) => `${csType(type)} ${name}`
|
|
89
|
+
|
|
90
|
+
/** @type {(f: Field) => string} */
|
|
91
|
+
const csField = f => `public ${csParam(f)};`
|
|
92
|
+
|
|
93
|
+
/** @type {(m: Method) => string} */
|
|
94
|
+
const csResult = m => m.length === 2 ? 'void' : csType(m[2])
|
|
95
|
+
|
|
96
|
+
/** @type {(m: Method) => readonly string[]} */
|
|
97
|
+
const csMethod = m => [
|
|
98
|
+
'[PreserveSig]',
|
|
99
|
+
`${csResult(m)} ${m[0]}(${list.join(',')(list.map(csParam)(m[1]))});`
|
|
100
|
+
]
|
|
101
|
+
|
|
102
|
+
/** @type {(e: obj.Entry<Definition>) => list.List<text.Item>} */
|
|
103
|
+
const csDef = ([n, d]) => {
|
|
104
|
+
const i = d.interface
|
|
105
|
+
return i === undefined ?
|
|
106
|
+
csTypeDef
|
|
107
|
+
(['StructLayout(LayoutKind.Sequential)'])
|
|
108
|
+
('struct')
|
|
109
|
+
(n)
|
|
110
|
+
(() => list.map(csField)(d.struct)) :
|
|
111
|
+
csTypeDef
|
|
112
|
+
([`Guid("${d.guid}")`,'InterfaceType(ComInterfaceType.InterfaceIsUnknown)'])
|
|
113
|
+
('interface')
|
|
114
|
+
(n)
|
|
115
|
+
(() => list.flatMap(csMethod)(d.interface))
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/** @type {(name: string) => (library: Library) => text.Block} */
|
|
119
|
+
const cs = name => library => {
|
|
120
|
+
const v = list.flatMap(csDef)(Object.entries(library))
|
|
121
|
+
|
|
122
|
+
/** @type {text.Block} */
|
|
123
|
+
const h = [
|
|
124
|
+
csUsing('System'),
|
|
125
|
+
csUsing('System.Runtime.InteropServices'),
|
|
126
|
+
''
|
|
127
|
+
]
|
|
128
|
+
|
|
129
|
+
const ns = csBlock('namespace')(name)(() => v)
|
|
130
|
+
return () => list.flat([h, ns])
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
module.exports = {
|
|
134
|
+
/** @readonly */
|
|
135
|
+
cs,
|
|
136
|
+
}
|
package/com/test.f.js
CHANGED
|
@@ -1,23 +1,49 @@
|
|
|
1
1
|
const _ = require('./main.f.js')
|
|
2
|
+
const list = require('../types/list/main.f.js')
|
|
3
|
+
const text = require('../text/main.f.js')
|
|
2
4
|
|
|
3
5
|
/** @type {_.Library} */
|
|
4
6
|
const library = {
|
|
5
7
|
Slice: {
|
|
6
8
|
struct: [
|
|
7
9
|
['Start', { '*': 'u8' }],
|
|
8
|
-
['
|
|
10
|
+
['Size', 'usize'],
|
|
9
11
|
]
|
|
10
12
|
},
|
|
11
13
|
IMy: {
|
|
12
14
|
guid: 'C66FB270-2D80-49AD-BB6E-88C1F90B805D',
|
|
13
15
|
interface: [
|
|
14
16
|
['GetSlice', [], ['Slice']],
|
|
15
|
-
['SetSlice',
|
|
16
|
-
[['slice', ['Slice']]],
|
|
17
|
-
''
|
|
18
|
-
],
|
|
17
|
+
['SetSlice', [['slice', ['Slice']]]],
|
|
19
18
|
],
|
|
20
19
|
}
|
|
21
20
|
}
|
|
22
21
|
|
|
22
|
+
{
|
|
23
|
+
const cs = list.join('\n')(text.flat(' ')(_.cs('My')(library)))
|
|
24
|
+
const e =
|
|
25
|
+
'using System;\n' +
|
|
26
|
+
'using System.Runtime.InteropServices;\n' +
|
|
27
|
+
'\n' +
|
|
28
|
+
'namespace My\n' +
|
|
29
|
+
'{\n' +
|
|
30
|
+
' [StructLayout(LayoutKind.Sequential)]\n' +
|
|
31
|
+
' public struct Slice\n' +
|
|
32
|
+
' {\n' +
|
|
33
|
+
' public byte* Start;\n' +
|
|
34
|
+
' public UIntPtr Size;\n' +
|
|
35
|
+
' }\n' +
|
|
36
|
+
' [Guid("C66FB270-2D80-49AD-BB6E-88C1F90B805D")]\n' +
|
|
37
|
+
' [InterfaceType(ComInterfaceType.InterfaceIsUnknown)]\n' +
|
|
38
|
+
' public interface IMy\n' +
|
|
39
|
+
' {\n' +
|
|
40
|
+
' [PreserveSig]\n' +
|
|
41
|
+
' Slice GetSlice();\n' +
|
|
42
|
+
' [PreserveSig]\n' +
|
|
43
|
+
' void SetSlice(Slice slice);\n' +
|
|
44
|
+
' }\n' +
|
|
45
|
+
'}'
|
|
46
|
+
if (cs !== e) { throw [cs,e] }
|
|
47
|
+
}
|
|
48
|
+
|
|
23
49
|
module.exports = {}
|
package/commonjs/README.md
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
## Package
|
|
4
4
|
|
|
5
|
-
[package/
|
|
5
|
+
[package/main.f.js](package/main.f.js)
|
|
6
6
|
|
|
7
7
|
```ts
|
|
8
8
|
// A dictionary of packages.
|
|
@@ -31,7 +31,7 @@ type Package = {
|
|
|
31
31
|
|
|
32
32
|
## Module
|
|
33
33
|
|
|
34
|
-
[module/
|
|
34
|
+
[module/main.f.js](module/main.f.js)
|
|
35
35
|
|
|
36
36
|
```ts
|
|
37
37
|
// A module map is a dictionary of modules.
|
package/package.json
CHANGED
package/test.f.js
CHANGED
package/text/main.f.js
CHANGED
|
@@ -1,21 +1,27 @@
|
|
|
1
1
|
const list = require('../types/list/main.f.js')
|
|
2
2
|
|
|
3
|
-
/** @typedef {
|
|
3
|
+
/** @typedef {ItemThunk|ItemArray} Block */
|
|
4
4
|
|
|
5
|
-
/** @typedef {
|
|
5
|
+
/** @typedef {readonly Item[]} ItemArray */
|
|
6
6
|
|
|
7
|
-
/** @
|
|
7
|
+
/** @typedef {() => list.List<Item>} ItemThunk */
|
|
8
|
+
|
|
9
|
+
/** @typedef {string|ItemArray|ItemThunk} Item */
|
|
10
|
+
|
|
11
|
+
/** @type {(indent: string) => (text: Block) => list.List<string>} */
|
|
8
12
|
const flat = indent => {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
const
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
13
|
+
|
|
14
|
+
/** @type {(prefix: string) => (text: Block) => list.List<string>} */
|
|
15
|
+
const f = prefix => {
|
|
16
|
+
/** @type {(item: Item) => list.List<string>} */
|
|
17
|
+
const g = item => typeof (item) === 'string' ? [`${prefix}${item}`] : f(`${prefix}${indent}`)(item)
|
|
18
|
+
return list.flatMap(g)
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
return f('')
|
|
16
22
|
}
|
|
17
23
|
|
|
18
24
|
module.exports = {
|
|
19
25
|
/** @readonly */
|
|
20
26
|
flat,
|
|
21
|
-
}
|
|
27
|
+
}
|
package/text/test.f.js
CHANGED
|
@@ -2,17 +2,18 @@ const _ = require('./main.f.js')
|
|
|
2
2
|
const list = require('../types/list/main.f.js')
|
|
3
3
|
|
|
4
4
|
{
|
|
5
|
-
/** @type {_.
|
|
5
|
+
/** @type {_.Block} */
|
|
6
6
|
const text = [
|
|
7
7
|
'a',
|
|
8
8
|
'b',
|
|
9
|
-
[
|
|
9
|
+
() => [
|
|
10
10
|
'c',
|
|
11
|
-
['d'],
|
|
12
|
-
]
|
|
11
|
+
() => ['d'],
|
|
12
|
+
],
|
|
13
|
+
'e',
|
|
13
14
|
]
|
|
14
|
-
const result = list.join('\n')(_.flat('
|
|
15
|
-
if (result !== 'a\nb\
|
|
15
|
+
const result = list.join('\n')(_.flat(':')(text))
|
|
16
|
+
if (result !== 'a\nb\n:c\n::d\ne') { throw result }
|
|
16
17
|
}
|
|
17
18
|
|
|
18
19
|
module.exports = {}
|
package/types/list/main.f.js
CHANGED
|
@@ -172,8 +172,7 @@ const drop = n => apply(dropStep(n))
|
|
|
172
172
|
/** @type {<D>(def: D) => <T>(input: List<T>) => D|T} */
|
|
173
173
|
const first = def => input => {
|
|
174
174
|
const result = next(input)
|
|
175
|
-
|
|
176
|
-
return result.first
|
|
175
|
+
return result === undefined ? def : result.first
|
|
177
176
|
}
|
|
178
177
|
|
|
179
178
|
/** @type {<D>(first: D) => <T>(tail: List<T>) => D|T} */
|
|
@@ -215,6 +214,9 @@ const countdown = count => () => {
|
|
|
215
214
|
return { first, tail: countdown(first) }
|
|
216
215
|
}
|
|
217
216
|
|
|
217
|
+
/** @type {<T>(v: T) => (c: number) => List<T>} */
|
|
218
|
+
const repeat = v => n => map(() => v)(countdown(n))
|
|
219
|
+
|
|
218
220
|
/** @type {<T>(list: List<T>) => List<T>} */
|
|
219
221
|
const cycle = list => () => {
|
|
220
222
|
const i = next(list)
|
|
@@ -355,6 +357,8 @@ module.exports = {
|
|
|
355
357
|
/** @readonly */
|
|
356
358
|
countdown,
|
|
357
359
|
/** @readonly */
|
|
360
|
+
repeat,
|
|
361
|
+
/** @readonly */
|
|
358
362
|
cycle,
|
|
359
363
|
/** @readonly */
|
|
360
364
|
scan,
|
package/types/list/test.f.js
CHANGED
|
@@ -354,6 +354,16 @@ const stress = () => {
|
|
|
354
354
|
}
|
|
355
355
|
}
|
|
356
356
|
|
|
357
|
+
{
|
|
358
|
+
const s = _.join('.')(_.repeat('x')(0))
|
|
359
|
+
if (s != '') { throw s }
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
{
|
|
363
|
+
const s = _.join('.')(_.repeat('x')(5))
|
|
364
|
+
if (s != 'x.x.x.x.x') { throw s }
|
|
365
|
+
}
|
|
366
|
+
|
|
357
367
|
// stress()
|
|
358
368
|
|
|
359
369
|
module.exports = {
|