functionalscript 0.0.353 → 0.0.356
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/cs/main.f.js +112 -0
- package/com/cs/test/Program.cs +2 -0
- package/com/cs/test/build.js +11 -0
- package/com/cs/test/test.csproj +11 -0
- package/com/{test.f.js → cs/test.f.js} +24 -6
- package/com/main.f.js +2 -111
- package/com/types/main.f.js +55 -0
- package/commonjs/path/README.md +17 -0
- package/commonjs/path/main.f.js +18 -11
- package/package.json +1 -1
- package/test.f.js +1 -1
package/com/cs/main.f.js
ADDED
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
const types = require('../types/main.f.js')
|
|
2
|
+
const text = require('../../text/main.f.js')
|
|
3
|
+
const list = require('../../types/list/main.f.js')
|
|
4
|
+
const obj = require('../../types/object/main.f.js')
|
|
5
|
+
|
|
6
|
+
/** @type {(v: string) => string} */
|
|
7
|
+
const csUsing = v => `using ${v};`
|
|
8
|
+
|
|
9
|
+
/** @type {(type: string) => (name: string) => (body: text.Block) => text.Block} */
|
|
10
|
+
const csBlock = type => name => body => [`${type} ${name}`, '{', body, '}']
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* @type {(attributes: list.List<string>) =>
|
|
14
|
+
* (type: string) =>
|
|
15
|
+
* (name: string) =>
|
|
16
|
+
* (body: text.Block) =>
|
|
17
|
+
* list.List<text.Item>}
|
|
18
|
+
*/
|
|
19
|
+
const csTypeDef = attributes => type => name => body =>
|
|
20
|
+
list.flat([
|
|
21
|
+
list.map(v => `[${v}]`)(attributes),
|
|
22
|
+
csBlock(`public ${type}`)(name)(body)
|
|
23
|
+
])
|
|
24
|
+
|
|
25
|
+
/** @type {(t: types.BaseType) => string} */
|
|
26
|
+
const csBaseType = t => {
|
|
27
|
+
switch (t) {
|
|
28
|
+
case 'bool': return 'bool'
|
|
29
|
+
case 'f32': return 'float'
|
|
30
|
+
case 'f64': return 'double'
|
|
31
|
+
case 'i16': return 'short'
|
|
32
|
+
case 'i32': return 'int'
|
|
33
|
+
case 'i64': return 'long'
|
|
34
|
+
case 'i8': return 'sbyte'
|
|
35
|
+
case 'isize': return 'IntPtr'
|
|
36
|
+
case 'u16': return 'ushort'
|
|
37
|
+
case 'u32': return 'uint'
|
|
38
|
+
case 'u64': return 'ulong'
|
|
39
|
+
case 'u8': return 'byte'
|
|
40
|
+
case 'usize': return 'UIntPtr'
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** @type {(isUnsafe: boolean) => string} */
|
|
45
|
+
const unsafe = isUnsafe => isUnsafe ? 'unsafe ' : ''
|
|
46
|
+
|
|
47
|
+
/** @type {(t: types.Type) => readonly[boolean, string]} */
|
|
48
|
+
const csType = t =>
|
|
49
|
+
typeof (t) === 'string' ? [false, csBaseType(t)] :
|
|
50
|
+
t instanceof Array ? [false, t[0]] :
|
|
51
|
+
[true, `${csType(t['*'])[1]}*`]
|
|
52
|
+
|
|
53
|
+
/** @type {(f: types.Field) => string} */
|
|
54
|
+
const csParam = ([name, type]) => `${csType(type)[1]} ${name}`
|
|
55
|
+
|
|
56
|
+
/** @type {(f: types.Field) => string} */
|
|
57
|
+
const csField = ([name, type]) => {
|
|
58
|
+
const [isUnsafe, t] = csType(type)
|
|
59
|
+
return `public ${unsafe(isUnsafe)}${t} ${name};`
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** @type {(m: types.Method) => readonly[boolean, string]} */
|
|
63
|
+
const csResult = m => m.length === 2 ? [false, 'void'] : csType(m[2])
|
|
64
|
+
|
|
65
|
+
/** @type {(field: types.Field) => boolean} */
|
|
66
|
+
const isUnsafeField = field => csType(field[1])[0]
|
|
67
|
+
|
|
68
|
+
/** @type {(m: types.Method) => readonly string[]} */
|
|
69
|
+
const csMethod = m => {
|
|
70
|
+
const result = csResult(m)
|
|
71
|
+
const isUnsafe = result[0] || list.some(list.map(isUnsafeField)(m[1]))
|
|
72
|
+
return [
|
|
73
|
+
'[PreserveSig]',
|
|
74
|
+
`${unsafe(isUnsafe)}${result[1]} ${m[0]}(${list.join(', ')(list.map(csParam)(m[1]))});`
|
|
75
|
+
]
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/** @type {(e: obj.Entry<types.Definition>) => list.List<text.Item>} */
|
|
79
|
+
const csDef = ([n, d]) => {
|
|
80
|
+
const i = d.interface
|
|
81
|
+
return i === undefined ?
|
|
82
|
+
csTypeDef
|
|
83
|
+
(['StructLayout(LayoutKind.Sequential)'])
|
|
84
|
+
('struct')
|
|
85
|
+
(n)
|
|
86
|
+
(() => list.map(csField)(d.struct)) :
|
|
87
|
+
csTypeDef
|
|
88
|
+
([`Guid("${d.guid}")`, 'InterfaceType(ComInterfaceType.InterfaceIsIUnknown)'])
|
|
89
|
+
('interface')
|
|
90
|
+
(n)
|
|
91
|
+
(() => list.flatMap(csMethod)(d.interface))
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/** @type {(name: string) => (library: types.Library) => text.Block} */
|
|
95
|
+
const cs = name => library => {
|
|
96
|
+
const v = list.flatMap(csDef)(Object.entries(library))
|
|
97
|
+
|
|
98
|
+
/** @type {text.Block} */
|
|
99
|
+
const h = [
|
|
100
|
+
csUsing('System'),
|
|
101
|
+
csUsing('System.Runtime.InteropServices'),
|
|
102
|
+
''
|
|
103
|
+
]
|
|
104
|
+
|
|
105
|
+
const ns = csBlock('namespace')(name)(() => v)
|
|
106
|
+
return () => list.flat([h, ns])
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
module.exports = {
|
|
110
|
+
/** @readonly */
|
|
111
|
+
cs,
|
|
112
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
const fs = require('node:fs')
|
|
2
|
+
const cp = require('node:child_process')
|
|
3
|
+
const cs = require('../test.f.js').result
|
|
4
|
+
|
|
5
|
+
fs.writeFileSync('_result.cs', cs)
|
|
6
|
+
try {
|
|
7
|
+
console.log(cp.execSync('dotnet build').toString())
|
|
8
|
+
} catch (e) {
|
|
9
|
+
// @ts-ignore
|
|
10
|
+
console.log(e.output.toString())
|
|
11
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
<Project Sdk="Microsoft.NET.Sdk">
|
|
2
|
+
|
|
3
|
+
<PropertyGroup>
|
|
4
|
+
<OutputType>Exe</OutputType>
|
|
5
|
+
<TargetFramework>net6.0</TargetFramework>
|
|
6
|
+
<ImplicitUsings>enable</ImplicitUsings>
|
|
7
|
+
<Nullable>enable</Nullable>
|
|
8
|
+
<AllowUnsafeBlocks>True</AllowUnsafeBlocks>
|
|
9
|
+
</PropertyGroup>
|
|
10
|
+
|
|
11
|
+
</Project>
|
|
@@ -1,8 +1,9 @@
|
|
|
1
|
+
const types = require('../types/main.f.js')
|
|
1
2
|
const _ = require('./main.f.js')
|
|
2
|
-
const list = require('
|
|
3
|
-
const text = require('
|
|
3
|
+
const list = require('../../types/list/main.f.js')
|
|
4
|
+
const text = require('../../text/main.f.js')
|
|
4
5
|
|
|
5
|
-
/** @type {
|
|
6
|
+
/** @type {types.Library} */
|
|
6
7
|
const library = {
|
|
7
8
|
Slice: {
|
|
8
9
|
struct: [
|
|
@@ -15,10 +16,13 @@ const library = {
|
|
|
15
16
|
interface: [
|
|
16
17
|
['GetSlice', [], ['Slice']],
|
|
17
18
|
['SetSlice', [['slice', ['Slice']]]],
|
|
19
|
+
['GetUnsafe', [], {'*': 'bool'}],
|
|
20
|
+
['SetUnsafe', [['p', {'*': ['Slice']}], ['size', 'u32']]],
|
|
18
21
|
],
|
|
19
22
|
}
|
|
20
23
|
}
|
|
21
24
|
|
|
25
|
+
const f = () =>
|
|
22
26
|
{
|
|
23
27
|
const cs = list.join('\n')(text.flat(' ')(_.cs('My')(library)))
|
|
24
28
|
const e =
|
|
@@ -30,16 +34,30 @@ const library = {
|
|
|
30
34
|
' [StructLayout(LayoutKind.Sequential)]\n' +
|
|
31
35
|
' public struct Slice\n' +
|
|
32
36
|
' {\n' +
|
|
33
|
-
' public byte* Start;\n' +
|
|
37
|
+
' public unsafe byte* Start;\n' +
|
|
34
38
|
' public UIntPtr Size;\n' +
|
|
35
39
|
' }\n' +
|
|
36
40
|
' [Guid("C66FB270-2D80-49AD-BB6E-88C1F90B805D")]\n' +
|
|
37
|
-
' [InterfaceType(ComInterfaceType.
|
|
41
|
+
' [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]\n' +
|
|
38
42
|
' public interface IMy\n' +
|
|
39
43
|
' {\n' +
|
|
44
|
+
' [PreserveSig]\n' +
|
|
45
|
+
' Slice GetSlice();\n' +
|
|
46
|
+
' [PreserveSig]\n' +
|
|
47
|
+
' void SetSlice(Slice slice);\n' +
|
|
48
|
+
' [PreserveSig]\n' +
|
|
49
|
+
' unsafe bool* GetUnsafe();\n' +
|
|
50
|
+
' [PreserveSig]\n' +
|
|
51
|
+
' unsafe void SetUnsafe(Slice* p, uint size);\n' +
|
|
40
52
|
' }\n' +
|
|
41
53
|
'}'
|
|
42
54
|
if (cs !== e) { throw [cs,e] }
|
|
55
|
+
return cs
|
|
43
56
|
}
|
|
44
57
|
|
|
45
|
-
|
|
58
|
+
const result = f()
|
|
59
|
+
|
|
60
|
+
module.exports = {
|
|
61
|
+
/** @readonly */
|
|
62
|
+
result,
|
|
63
|
+
}
|
package/com/main.f.js
CHANGED
|
@@ -1,115 +1,6 @@
|
|
|
1
|
-
const
|
|
2
|
-
const list = require('../types/list/main.f')
|
|
3
|
-
const obj = require('../types/object/main.f.js')
|
|
4
|
-
|
|
5
|
-
/** @typedef {{readonly[k in string]: Definition}} Library */
|
|
6
|
-
|
|
7
|
-
/** @typedef {Struct|Interface} Definition */
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* @typedef {{
|
|
11
|
-
* readonly interface?: undefined
|
|
12
|
-
* readonly struct: FieldArray
|
|
13
|
-
* }} Struct
|
|
14
|
-
*/
|
|
15
|
-
|
|
16
|
-
/** @typedef {readonly Field[]} FieldArray */
|
|
17
|
-
|
|
18
|
-
/** @typedef {[string, Type]} Field */
|
|
19
|
-
|
|
20
|
-
/**
|
|
21
|
-
* @typedef {{
|
|
22
|
-
* readonly interface: MethodArray
|
|
23
|
-
* readonly guid: string
|
|
24
|
-
* }} Interface
|
|
25
|
-
*/
|
|
26
|
-
|
|
27
|
-
/** @typedef {readonly Method[]} MethodArray */
|
|
28
|
-
|
|
29
|
-
/** @typedef {readonly[string, FieldArray, Type]} GetMethod */
|
|
30
|
-
|
|
31
|
-
/** @typedef {readonly[string, FieldArray]} SetMethod */
|
|
32
|
-
|
|
33
|
-
/** @typedef {GetMethod|SetMethod} Method */
|
|
34
|
-
|
|
35
|
-
/** @typedef {BaseType|Id|Pointer} Type */
|
|
36
|
-
|
|
37
|
-
/** @typedef {readonly[string]} Id */
|
|
38
|
-
|
|
39
|
-
/** @typedef {'u8'|'i8'|'u16'|'i16'|'u32'|'i32'|'u64'|'i64'|'usize'|'isize'|'f32'|'f64'|'bool'} BaseType */
|
|
40
|
-
|
|
41
|
-
/** @typedef {{readonly '*': Type}} Pointer */
|
|
42
|
-
|
|
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
|
-
/** @type {(attributes: list.List<string>) => (type: string) => (name: string) => (body: text.Block) => list.List<text.Item>} */
|
|
50
|
-
const csTypeDef = attributes => type => name => body =>
|
|
51
|
-
list.flat([
|
|
52
|
-
list.map(v=>`[${v}]`)(attributes),
|
|
53
|
-
csBlock(`public ${type}`)(name)(body)
|
|
54
|
-
])
|
|
55
|
-
|
|
56
|
-
/** @type {(t: BaseType) => string} */
|
|
57
|
-
const csBaseType = t => {
|
|
58
|
-
switch (t) {
|
|
59
|
-
case 'bool': return 'bool'
|
|
60
|
-
case 'f32': return 'float'
|
|
61
|
-
case 'f64': return 'double'
|
|
62
|
-
case 'i16': return 'short'
|
|
63
|
-
case 'i32': return 'int'
|
|
64
|
-
case 'i64': return 'long'
|
|
65
|
-
case 'i8': return 'sbyte'
|
|
66
|
-
case 'isize': return 'IntPtr'
|
|
67
|
-
case 'u16': return 'ushort'
|
|
68
|
-
case 'u32': return 'uint'
|
|
69
|
-
case 'u64': return 'ulong'
|
|
70
|
-
case 'u8': return 'byte'
|
|
71
|
-
case 'usize': return 'UIntPtr'
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
/** @type {(t: Type) => string} */
|
|
76
|
-
const csType = t => {
|
|
77
|
-
if (typeof(t) === 'string') {
|
|
78
|
-
return csBaseType(t)
|
|
79
|
-
}
|
|
80
|
-
if (t instanceof Array) {
|
|
81
|
-
return t[0]
|
|
82
|
-
}
|
|
83
|
-
return `${csType(t['*'])}*`
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
/** @type {(f: Field) => string} */
|
|
87
|
-
const csField = f => `public ${csType(f[1])} ${f[0]};`
|
|
88
|
-
|
|
89
|
-
/** @type {(e: obj.Entry<Definition>) => list.List<text.Item>} */
|
|
90
|
-
const csDef = ([n, d]) => {
|
|
91
|
-
const i = d.interface
|
|
92
|
-
return i === undefined ?
|
|
93
|
-
csTypeDef(['StructLayout(LayoutKind.Sequential)'])('struct')(n)(() => list.map(csField)(d.struct)) :
|
|
94
|
-
csTypeDef([`Guid("${d.guid}")`,'InterfaceType(ComInterfaceType.InterfaceIsUnknown)'])('interface')(n)([])
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
/** @type {(name: string) => (library: Library) => text.Block} */
|
|
98
|
-
const cs = name => library => {
|
|
99
|
-
const v = list.flatMap(csDef)(Object.entries(library))
|
|
100
|
-
|
|
101
|
-
/** @type {text.Block} */
|
|
102
|
-
const h = [
|
|
103
|
-
csUsing('System'),
|
|
104
|
-
csUsing('System.Runtime.InteropServices'),
|
|
105
|
-
''
|
|
106
|
-
]
|
|
107
|
-
|
|
108
|
-
const ns = csBlock('namespace')(name)(() => v)
|
|
109
|
-
return () => list.flat([h, ns])
|
|
110
|
-
}
|
|
1
|
+
const types = require('./types/main.f.js');
|
|
111
2
|
|
|
112
3
|
module.exports = {
|
|
113
4
|
/** @readonly */
|
|
114
|
-
|
|
5
|
+
types,
|
|
115
6
|
}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
/** @typedef {{readonly[k in string]: Definition}} Library */
|
|
2
|
+
|
|
3
|
+
/** @typedef {Struct|Interface} Definition */
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* @typedef {{
|
|
7
|
+
* readonly interface?: undefined
|
|
8
|
+
* readonly struct: FieldArray
|
|
9
|
+
* }} Struct
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
/** @typedef {readonly Field[]} FieldArray */
|
|
13
|
+
|
|
14
|
+
/** @typedef {readonly[string, Type]} Field */
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* @typedef {{
|
|
18
|
+
* readonly interface: MethodArray
|
|
19
|
+
* readonly guid: string
|
|
20
|
+
* }} Interface
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
/** @typedef {readonly Method[]} MethodArray */
|
|
24
|
+
|
|
25
|
+
/** @typedef {readonly[string, FieldArray, Type]} GetMethod */
|
|
26
|
+
|
|
27
|
+
/** @typedef {readonly[string, FieldArray]} SetMethod */
|
|
28
|
+
|
|
29
|
+
/** @typedef {GetMethod|SetMethod} Method */
|
|
30
|
+
|
|
31
|
+
/** @typedef {BaseType|Id|Pointer} Type */
|
|
32
|
+
|
|
33
|
+
/** @typedef {readonly[string]} Id */
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* @typedef {|
|
|
37
|
+
* 'u8'|
|
|
38
|
+
* 'i8'|
|
|
39
|
+
* 'u16'|
|
|
40
|
+
* 'i16'|
|
|
41
|
+
* 'u32'|
|
|
42
|
+
* 'i32'|
|
|
43
|
+
* 'u64'|
|
|
44
|
+
* 'i64'|
|
|
45
|
+
* 'usize'|
|
|
46
|
+
* 'isize'|
|
|
47
|
+
* 'f32'|
|
|
48
|
+
* 'f64'|
|
|
49
|
+
* 'bool'
|
|
50
|
+
* } BaseType
|
|
51
|
+
*/
|
|
52
|
+
|
|
53
|
+
/** @typedef {{readonly '*': Type}} Pointer */
|
|
54
|
+
|
|
55
|
+
module.exports = {}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
# Path
|
|
2
|
+
|
|
3
|
+
## Simplified Common.js
|
|
4
|
+
|
|
5
|
+
A path should be disambiguous.
|
|
6
|
+
|
|
7
|
+
Path limitations:
|
|
8
|
+
|
|
9
|
+
- Accepted file extensions: `.f.js`, `.json`.
|
|
10
|
+
- A file path should be fully specified. For example `../package/dependencies/test.f.js`.
|
|
11
|
+
- A package name can't contain `/`. For example
|
|
12
|
+
- `functionalscript/main.f.js` is allowed.
|
|
13
|
+
- `functionalscript/functionalscript/main.f.js` is not allowed.
|
|
14
|
+
|
|
15
|
+
`package.json` limitations:
|
|
16
|
+
|
|
17
|
+
- Only GitHub packages can be referenced. In the future, we may add generic Git repositories.
|
package/commonjs/path/main.f.js
CHANGED
|
@@ -98,11 +98,15 @@ const mapDependency = d => ([external, internal]) => {
|
|
|
98
98
|
* (items: list.List<string>) =>
|
|
99
99
|
* Path|undefined}
|
|
100
100
|
*/
|
|
101
|
-
const parseGlobal =
|
|
102
|
-
|
|
103
|
-
const
|
|
104
|
-
|
|
105
|
-
|
|
101
|
+
const parseGlobal = dependencies =>
|
|
102
|
+
{
|
|
103
|
+
const filterMap = list.filterMap(mapDependency(dependencies))
|
|
104
|
+
return dir => items => {
|
|
105
|
+
const v = variants([undefined, items])
|
|
106
|
+
const r = list.first(undefined)(filterMap(v))
|
|
107
|
+
if (r === undefined) { return undefined }
|
|
108
|
+
return { package: r[0], items: list.toArray(r[1]), dir }
|
|
109
|
+
}
|
|
106
110
|
}
|
|
107
111
|
|
|
108
112
|
/**
|
|
@@ -112,12 +116,15 @@ const parseGlobal = d => dir => items => {
|
|
|
112
116
|
* (path: string) =>
|
|
113
117
|
* Path|undefined }
|
|
114
118
|
*/
|
|
115
|
-
const parse = packageId => dependencies =>
|
|
116
|
-
const
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
119
|
+
const parse = packageId => dependencies => {
|
|
120
|
+
const pg = parseGlobal(dependencies)
|
|
121
|
+
return local => path => {
|
|
122
|
+
const parsed = parseLocal(local)(path)
|
|
123
|
+
if (parsed === undefined) { return undefined }
|
|
124
|
+
const {external, dir, items } = parsed
|
|
125
|
+
if (!external) { return { package: packageId, items, dir } }
|
|
126
|
+
return pg(dir)(items)
|
|
127
|
+
}
|
|
121
128
|
}
|
|
122
129
|
|
|
123
130
|
/**
|
package/package.json
CHANGED
package/test.f.js
CHANGED
|
@@ -17,7 +17,7 @@ require('./commonjs/build/test.f.js')
|
|
|
17
17
|
require('./types/range/test.f.js')
|
|
18
18
|
require('./html/test.f.js')
|
|
19
19
|
require('./text/test.f.js')
|
|
20
|
-
require('./com/test.f.js')
|
|
20
|
+
require('./com/cs/test.f.js')
|
|
21
21
|
|
|
22
22
|
/** @type {() => never} */
|
|
23
23
|
const assert = () => { throw 'assert' }
|