functionalscript 0.0.389 → 0.0.392

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.
@@ -1,37 +1,44 @@
1
1
  const types = require('../types/module.f.cjs')
2
+ const { paramList } = types
2
3
  const text = require('../../text/module.f.cjs')
3
4
  const obj = require('../../types/object/module.f.cjs')
4
- const { list } = require('../../types/module.f.cjs')
5
+ const list = require('../../types/list/module.f.cjs')
6
+ const { map, join, flatMap } = list
7
+ const { entries } = Object
5
8
 
6
9
  /** @type {(name: string) => (body: text.Block) => text.Block} */
7
10
  const struct = name => body => [`struct ${name}`, '{', body, '};']
8
11
 
9
- /** @type {(t: types.BaseType) => string} */
10
- const baseType = t => {
11
- switch (t) {
12
- case 'u8': return 'uint8_t'
13
- case 'i8': return 'int8_t'
14
- case 'u16': return 'uint16_t'
15
- case 'i16': return 'int16_t'
16
- case 'u32': return 'uint32_t'
17
- case 'i32': return 'int32_t'
18
- case 'u64': return 'uint64_t'
19
- case 'i64': return 'int64_t'
20
- case 'usize': return 'size_t'
21
- case 'isize': return 'ptrdiff_t'
22
- case 'f32': return 'float'
23
- case 'f64': return 'double'
24
- case 'bool': return '::com::BOOL'
25
- }
12
+ const baseTypeMap = {
13
+ u8: 'uint8_t',
14
+ i8: 'int8_t',
15
+ u16: 'uint16_t',
16
+ i16: 'int16_t',
17
+ u32: 'uint32_t',
18
+ i32: 'int32_t',
19
+ u64: 'uint64_t',
20
+ i64: 'int64_t',
21
+ usize: 'size_t',
22
+ isize: 'ptrdiff_t',
23
+ f32: 'float',
24
+ f64: 'double',
25
+ bool: '::com::BOOL',
26
26
  }
27
27
 
28
+ /** @type {(t: types.BaseType) => string} */
29
+ const baseType = t => baseTypeMap[t]
30
+
31
+ const resultVoid = types.result('void')
32
+
33
+ const namespace = text.curly('namespace')
34
+
28
35
  /** @type {(name: string) => (lib: types.Library) => text.Block} */
29
36
  const cpp = name => lib => {
30
37
 
31
38
  /** @type {(i: (t: string) => string) => (t: types.Type) => string} */
32
39
  const objectType = i => t => {
33
- if (typeof(t) === 'string') { return baseType(t) }
34
- if (t.length === 2) { return `${type(t[1])}*` }
40
+ if (typeof (t) === 'string') { return baseType(t) }
41
+ if (t.length === 2) { return `${type(t[1])}*` }
35
42
  const [id] = t
36
43
  if (lib[id].interface === undefined) { return id }
37
44
  return i(id)
@@ -42,33 +49,36 @@ const cpp = name => lib => {
42
49
  /** @type {(s: types.Field) => text.Item} */
43
50
  const field = ([name, t]) => `${type(t)} ${name};`
44
51
 
52
+ const mapField = map(field)
53
+
45
54
  /** @type {(s: types.Struct) => text.Block} */
46
- const defStruct = s => list.map(field)(Object.entries(s.struct))
55
+ const defStruct = s => mapField(entries(s.struct))
47
56
 
48
57
  /** @type {(fa: types.FieldArray) => string} */
49
- const result = types.result('void')(type)
58
+ const cppResult = resultVoid(type)
50
59
 
51
60
  /** @type {(p: types.Field) => string} */
52
61
  const param = ([name, t]) => `${objectType(id => `${id}&`)(t)} ${name}`
53
62
 
63
+ const mapParam = map(param)
64
+
54
65
  /** @type {(m: types.Method) => text.Item} */
55
- const method = ([name, paramArray]) =>
56
- `virtual ${result(paramArray)} COM_STDCALL ${name}(${list.join(', ')(list.map(param)(types.paramList(paramArray)))}) = 0;`
66
+ const method = ([name, paramArray]) =>
67
+ `virtual ${cppResult(paramArray)} COM_STDCALL ${name}(${join(', ')(mapParam(paramList(paramArray)))}) = 0;`
68
+
69
+ const mapMethod = map(method)
57
70
 
58
71
  /** @type {(i: types.Interface) => text.Block} */
59
- const defInterface = i => list.map(method)(Object.entries(i.interface))
72
+ const defInterface = i => mapMethod(entries(i.interface))
60
73
 
61
74
  /** @type {(kv: obj.Entry<types.Definition>) => text.Block} */
62
- const def = ([name, d]) => d.interface === undefined
63
- ? struct(name)(defStruct(d))
75
+ const def = ([name, d]) => d.interface === undefined
76
+ ? struct(name)(defStruct(d))
64
77
  : struct(`${name}: ::com::IUnknown`)(defInterface(d))
65
78
 
66
79
  return list.flat([
67
80
  ['#pragma once', ''],
68
- text.curly
69
- ('namespace')
70
- (name)
71
- (list.flatMap(def)(Object.entries(lib)))
81
+ namespace(name)(flatMap(def)(entries(lib)))
72
82
  ])
73
83
  }
74
84
 
@@ -1,7 +1,11 @@
1
1
  const types = require('../types/module.f.cjs')
2
+ const { result, paramList } = types
2
3
  const text = require('../../text/module.f.cjs')
4
+ const { curly } = text
3
5
  const list = require('../../types/list/module.f.cjs')
6
+ const { flat, map, some, join, flatMap } = list
4
7
  const obj = require('../../types/object/module.f.cjs')
8
+ const { entries } = Object
5
9
 
6
10
  /** @type {(v: string) => string} */
7
11
  const using = v => `using ${v};`
@@ -14,30 +18,30 @@ const using = v => `using ${v};`
14
18
  * list.List<text.Item>}
15
19
  */
16
20
  const typeDef = attributes => type => name => body =>
17
- list.flat([
18
- list.map(v => `[${v}]`)(attributes),
19
- text.curly(`public ${type}`)(name)(body)
21
+ flat([
22
+ map(v => `[${v}]`)(attributes),
23
+ curly(`public ${type}`)(name)(body)
20
24
  ])
21
25
 
22
- /** @type {(t: types.BaseType) => string} */
23
- const baseType = t => {
24
- switch (t) {
25
- case 'bool': return 'bool'
26
- case 'f32': return 'float'
27
- case 'f64': return 'double'
28
- case 'i16': return 'short'
29
- case 'i32': return 'int'
30
- case 'i64': return 'long'
31
- case 'i8': return 'sbyte'
32
- case 'isize': return 'IntPtr'
33
- case 'u16': return 'ushort'
34
- case 'u32': return 'uint'
35
- case 'u64': return 'ulong'
36
- case 'u8': return 'byte'
37
- case 'usize': return 'UIntPtr'
38
- }
26
+ const baseTypeMap = {
27
+ bool: 'bool',
28
+ f32: 'float',
29
+ f64: 'double',
30
+ i16: 'short',
31
+ i32: 'int',
32
+ i64: 'long',
33
+ i8: 'sbyte',
34
+ isize: 'IntPtr',
35
+ u16: 'ushort',
36
+ u32: 'uint',
37
+ u64: 'ulong',
38
+ u8: 'byte',
39
+ usize: 'UIntPtr',
39
40
  }
40
41
 
42
+ /** @type {(t: types.BaseType) => string} */
43
+ const baseType = t => baseTypeMap[t]
44
+
41
45
  /** @type {(isUnsafe: boolean) => string} */
42
46
  const unsafe = isUnsafe => isUnsafe ? 'unsafe ' : ''
43
47
 
@@ -53,6 +57,8 @@ const type = t => fullType(t)[1]
53
57
  /** @type {(f: types.Field) => string} */
54
58
  const param = ([name, t]) => `${type(t)} ${name}`
55
59
 
60
+ const mapParam = map(param)
61
+
56
62
  /** @type {(f: types.Field) => string} */
57
63
  const field = ([name, comType]) => {
58
64
  const [isUnsafe, t] = fullType(comType)
@@ -62,46 +68,59 @@ const field = ([name, comType]) => {
62
68
  /** @type {(field: types.Field) => boolean} */
63
69
  const isUnsafeField = field => fullType(field[1])[0]
64
70
 
71
+ const mapIsUnsafeField = map(isUnsafeField)
72
+
73
+ const resultVoid = result('void')
74
+
75
+ const joinComma = join(', ')
76
+
65
77
  /** @type {(e: obj.Entry<types.FieldArray>) => readonly string[]} */
66
78
  const method = ([name, m]) => {
67
- const paramAndResultList = Object.entries(m)
68
- const pl = types.paramList(m)
69
- const isUnsafe = list.some(list.map(isUnsafeField)(paramAndResultList))
79
+ const paramAndResultList = entries(m)
80
+ const pl = paramList(m)
81
+ const isUnsafe = some(mapIsUnsafeField(paramAndResultList))
70
82
  return [
71
83
  '[PreserveSig]',
72
- `${unsafe(isUnsafe)}${types.result('void')(type)(m)} ${name}(${list.join(', ')(list.map(param)(pl))});`
84
+ `${unsafe(isUnsafe)}${resultVoid(type)(m)} ${name}(${joinComma(mapParam(pl))});`
73
85
  ]
74
86
  }
75
87
 
88
+ const struct = typeDef
89
+ (['StructLayout(LayoutKind.Sequential)'])
90
+ ('struct')
91
+
92
+ const mapField = map(field)
93
+
94
+ const flatMapMethod = flatMap(method)
95
+
76
96
  /** @type {(e: obj.Entry<types.Definition>) => list.List<text.Item>} */
77
97
  const def = ([n, d]) => {
78
98
  const i = d.interface
79
99
  return i === undefined ?
80
- typeDef
81
- (['StructLayout(LayoutKind.Sequential)'])
82
- ('struct')
83
- (n)
84
- (list.map(field)(Object.entries(d.struct))) :
100
+ struct(n)(mapField(entries(d.struct))) :
85
101
  typeDef
86
102
  ([`Guid("${d.guid}")`, 'InterfaceType(ComInterfaceType.InterfaceIsIUnknown)'])
87
103
  ('interface')
88
104
  (n)
89
- (list.flatMap(method)(Object.entries(d.interface)))
105
+ (flatMapMethod(entries(i)))
90
106
  }
91
107
 
92
- /** @type {(name: string) => (library: types.Library) => text.Block} */
93
- const cs = name => library => {
94
- const v = list.flatMap(def)(Object.entries(library))
108
+ const flatMapDef = flatMap(def)
95
109
 
96
- /** @type {text.Block} */
97
- const h = [
98
- using('System'),
99
- using('System.Runtime.InteropServices'),
100
- ''
101
- ]
110
+ const namespace = curly('namespace')
111
+
112
+ /** @type {text.Block} */
113
+ const header = [
114
+ using('System'),
115
+ using('System.Runtime.InteropServices'),
116
+ ''
117
+ ]
102
118
 
103
- const ns = text.curly('namespace')(name)(() => v)
104
- return list.flat([h, ns])
119
+ /** @type {(name: string) => (library: types.Library) => text.Block} */
120
+ const cs = name => library => {
121
+ const v = flatMapDef(entries(library))
122
+ const ns = namespace(name)(v)
123
+ return flat([header, ns])
105
124
  }
106
125
 
107
126
  module.exports = {
@@ -1,6 +1,8 @@
1
1
  const obj = require('../../types/object/module.f.cjs')
2
2
  const list = require('../../types/list/module.f.cjs')
3
- const { types } = require('../module.f.cjs')
3
+ const { compose } = require('../../types/function/module.f.cjs')
4
+ const { filter } = list
5
+ const { entries } = Object
4
6
 
5
7
  /** @typedef {{readonly[k in string]: Definition}} Library */
6
8
 
@@ -55,8 +57,10 @@ const { types } = require('../module.f.cjs')
55
57
  /** @type {(kv: obj.Entry<Type>) => boolean} */
56
58
  const isParam = ([name]) => name !== '_'
57
59
 
60
+ const filterParam = filter(isParam)
61
+
58
62
  /** @type {(fa: FieldArray) => list.List<Field> } */
59
- const paramList = fa => list.filter(isParam)(Object.entries(fa))
63
+ const paramList = compose(entries)(filterParam)
60
64
 
61
65
  /** @type {<T>(v: T) => (f: (type: Type) => T) => (fa: FieldArray) => T} */
62
66
  const result = v => f => fa => {
@@ -1,10 +1,15 @@
1
1
  const package_ = require('../package/module.f.cjs')
2
2
  const module_ = require('../module/module.f.cjs')
3
+ const { idToString, dir } = module_
3
4
  const function_ = require('../module/function/module.f.cjs')
4
5
  const map = require('../../types/map/module.f.cjs')
6
+ const { set: mapSet } = map
5
7
  const object = require('../../types/object/module.f.cjs')
8
+ const { fromMap } = object
6
9
  const path = require('../path/module.f.cjs')
7
- const stringSet = require('../../types/stringset/module.f.cjs')
10
+ const { parseAndFind } = path
11
+ const stringSet = require('../../types/string_set/module.f.cjs')
12
+ const { set: setSet, contains: setContains } = stringSet
8
13
 
9
14
  /**
10
15
  * @template M
@@ -44,20 +49,20 @@ const getOrBuild = compile => packageGet => moduleMapInterface => {
44
49
  * Result<M>}
45
50
  */
46
51
  const build = buildSet => moduleId => {
47
- const moduleIdStr = module_.idToString(moduleId)
48
- const buildSet1 = stringSet.set(moduleIdStr)(buildSet)
49
- const dir = module_.dir(moduleId)
52
+ const moduleIdStr = idToString(moduleId)
53
+ const buildSet1 = setSet(moduleIdStr)(buildSet)
54
+ const moduleDir = dir(moduleId)
50
55
  /** @type {function_.Require<readonly[map.Map<string>, M]>} */
51
56
  const require_ = p => ([requireMap, m]) => {
52
57
  /** @type {(e: unknown) => function_.Result<readonly[map.Map<string>, M]>} */
53
58
  const error = e => [['error', 'file not found'], [requireMap, m]]
54
- if (dir === undefined) { return error('file not found') }
55
- const r = path.parseAndFind(packageGet)(dir)(p)
59
+ if (moduleDir === undefined) { return error('file not found') }
60
+ const r = parseAndFind(packageGet)(moduleDir)(p)
56
61
  if (r === undefined) { return error('file not found') }
57
- const rIdStr = module_.idToString(r.id)
58
- if (stringSet.contains(rIdStr)(buildSet1)) { return error('circular reference') }
62
+ const rIdStr = idToString(r.id)
63
+ if (setContains(rIdStr)(buildSet1)) { return error('circular reference') }
59
64
  const [state, m1] = build(buildSet1)(r.id)(r.source)(m)
60
- return [state[0] === 'error' ? state : ['ok', state[1].exports], [map.set(p)(rIdStr)(requireMap), m1]]
65
+ return [state[0] === 'error' ? state : ['ok', state[1].exports], [mapSet(p)(rIdStr)(requireMap), m1]]
61
66
  }
62
67
  return source => moduleMap => {
63
68
  /** @type {(s: module_.State) => (m: M) => Result<M>} */
@@ -68,16 +73,16 @@ const getOrBuild = compile => packageGet => moduleMapInterface => {
68
73
  const [kind, result] = compile(source)
69
74
  if (kind === 'error') { return error(['compilation error', result])(moduleMap) }
70
75
  // build
71
- const [r, [requireMap, moduleMap2]] = result(require_)([undefined, moduleMap])
72
- const x = r[0] === 'error' ?
73
- error(['runtime error', r[1]]) :
74
- set(['ok', { exports: r[1], requireMap: object.fromMap(requireMap) }])
76
+ const [[state, value], [requireMap, moduleMap2]] = result(require_)([undefined, moduleMap])
77
+ const x = state === 'error' ?
78
+ error(['runtime error', value]) :
79
+ set(['ok', { exports: value, requireMap: fromMap(requireMap) }])
75
80
  return x(moduleMap2)
76
81
  }
77
82
  }
78
83
  /** @type {(moduleId: module_.Id) => (moduleMap: M) => Result<M>} */
79
84
  const f = moduleId => moduleMap => {
80
- const moduleIdStr = module_.idToString(moduleId)
85
+ const moduleIdStr = idToString(moduleId)
81
86
  // check moduleMap
82
87
  {
83
88
  const m = moduleMapInterface.at(moduleIdStr)(moduleMap)
@@ -1,5 +1,8 @@
1
1
  const json = require('../../../json/module.f.cjs')
2
+ const { isObject } = json
2
3
  const list = require('../../../types/list/module.f.cjs')
4
+ const { map, every } = list
5
+ const { entries } = Object
3
6
 
4
7
  /** @typedef {readonly[string, string]} DependencyJson */
5
8
 
@@ -13,8 +16,8 @@ const isDependencyJson = ([, v]) => typeof v === 'string'
13
16
  /** @type {(j: json.Unknown|undefined) => j is DependenciesJson} */
14
17
  const isDependenciesJson = j => {
15
18
  if (j === undefined) { return true }
16
- if (!json.isObject(j)) { return false }
17
- return list.every(list.map(isDependencyJson)(Object.entries(j)))
19
+ if (!isObject(j)) { return false }
20
+ return every(map(isDependencyJson)(entries(j)))
18
21
  }
19
22
 
20
23
  module.exports = {
@@ -1,5 +1,7 @@
1
1
  const json = require('../../json/module.f.cjs')
2
+ const { isObject } = json
2
3
  const dependencies = require('./dependencies/module.f.cjs')
4
+ const { isDependenciesJson } = dependencies
3
5
 
4
6
  /**
5
7
  * @typedef {{
@@ -11,10 +13,10 @@ const dependencies = require('./dependencies/module.f.cjs')
11
13
 
12
14
  /** @type {(j: json.Unknown) => j is PackageJson} */
13
15
  const isPackageJson = j => {
14
- if (!json.isObject(j)) { return false }
16
+ if (!isObject(j)) { return false }
15
17
  if (typeof j.name !== 'string') { return false }
16
18
  if (typeof j.version !== 'string') { return false }
17
- if (!dependencies.isDependenciesJson(j.dependencies)) { return false }
19
+ if (!isDependenciesJson(j.dependencies)) { return false }
18
20
  return true
19
21
  }
20
22
 
@@ -1,4 +1,5 @@
1
1
  const list = require("../../types/list/module.f.cjs")
2
+ const { next, reduce, reverse, first, flat, toArray, filterMap, isEmpty, concat, join } = list
2
3
  const package_ = require("../package/module.f.cjs")
3
4
  const module_ = require("../module/module.f.cjs")
4
5
 
@@ -24,7 +25,7 @@ const normItemsOp = first => prior => {
24
25
  switch (first) {
25
26
  case '': case '.': { return prior }
26
27
  case '..': {
27
- const result = list.next(tail)
28
+ const result = next(tail)
28
29
  if (result === undefined) { return undefined }
29
30
  return [result.tail]
30
31
  }
@@ -36,19 +37,20 @@ const normItemsOp = first => prior => {
36
37
 
37
38
  /** @type {(items: list.List<string>) => OptionList} */
38
39
  const normItems = items => {
39
- const result = list.reduce(normItemsOp)([undefined])(items)
40
- if (result === undefined) { return result }
41
- return [list.reverse(result[0])]
40
+ const result = reduce(normItemsOp)([undefined])(items)
41
+ return result === undefined ? result : [reverse(result[0])]
42
42
  }
43
43
 
44
+ const firstUndefined = first(undefined)
45
+
44
46
  /** @type {(local: string) => (path: string) => LocalPath|undefined} */
45
47
  const parseLocal = local => {
46
48
  /** @type {(path: string) => readonly[boolean, boolean, list.List<string>]} */
47
49
  const fSeq = path => {
48
50
  const pathSeq = split(path)
49
51
  const dir = [undefined, '', '.', '..'].includes(pathSeq[pathSeq.length - 1])
50
- return /** @type {readonly (string|undefined)[]} */(['.', '..']).includes(list.first(undefined)(pathSeq)) ?
51
- [false, dir, list.flat([split(local), pathSeq])] :
52
+ return /** @type {readonly (string|undefined)[]} */(['.', '..']).includes(firstUndefined(pathSeq)) ?
53
+ [false, dir, flat([split(local), pathSeq])] :
52
54
  [true, dir, pathSeq]
53
55
  }
54
56
  /** @type {(path: string) => LocalPath|undefined} */
@@ -59,7 +61,7 @@ const parseLocal = local => {
59
61
  return {
60
62
  external,
61
63
  dir,
62
- items: list.toArray(n[0])
64
+ items: toArray(n[0])
63
65
  }
64
66
  }
65
67
  return f
@@ -70,7 +72,7 @@ const parseLocal = local => {
70
72
  /** @type {(prior: readonly[string|undefined, list.List<string>]) => list.Thunk<IdPath>} */
71
73
  const variants = prior => () => {
72
74
  const [a, b] = prior
73
- const r = list.next(b)
75
+ const r = next(b)
74
76
  if (r === undefined) { return undefined }
75
77
  const { first, tail } = r
76
78
  /** @type {IdPath} */
@@ -100,12 +102,12 @@ const mapDependency = d => ([external, internal]) => {
100
102
  */
101
103
  const parseGlobal = dependencies =>
102
104
  {
103
- const filterMap = list.filterMap(mapDependency(dependencies))
105
+ const fMap = filterMap(mapDependency(dependencies))
104
106
  return dir => items => {
105
107
  const v = variants([undefined, items])
106
- const r = list.first(undefined)(filterMap(v))
108
+ const r = firstUndefined(fMap(v))
107
109
  if (r === undefined) { return undefined }
108
- return { package: r[0], items: list.toArray(r[1]), dir }
110
+ return { package: r[0], items: toArray(r[1]), dir }
109
111
  }
110
112
  }
111
113
 
@@ -154,9 +156,9 @@ const parseAndFind = packageGet => moduleId => path => {
154
156
  return source === undefined ? undefined : { id: { package: p.package, path: file.split('/') }, source }
155
157
  }
156
158
  const file = p.items.join('/')
157
- const indexJs = list.join('/')(list.concat(p.items)(['index.js']))
158
- const fileList = p.dir || list.isEmpty(p.items) ? [indexJs] : [file, `${file}.js`, indexJs]
159
- return list.first(undefined)(list.filterMap(tryFile)(fileList))
159
+ const indexJs = join('/')(concat(p.items)(['index.js']))
160
+ const fileList = p.dir || isEmpty(p.items) ? [indexJs] : [file, `${file}.js`, indexJs]
161
+ return firstUndefined(filterMap(tryFile)(fileList))
160
162
  }
161
163
 
162
164
  module.exports = {
@@ -0,0 +1,19 @@
1
+ const { todo } = require('../module.f.cjs')
2
+ const list = require('../../types/list/module.f.cjs')
3
+ const function_ = require('../../commonjs/module/function/module.f.cjs')
4
+ const package_ = require('../../commonjs/package/module.f.cjs')
5
+
6
+ /**
7
+ * @type {(c: function_.Compile) =>
8
+ * (files: string) =>
9
+ * (packageGet: package_.Get) =>
10
+ * never}
11
+ */
12
+ const test = compile => files => packageGet => {
13
+ return todo()
14
+ }
15
+
16
+ module.exports = {
17
+ /** @readonly */
18
+ test,
19
+ }