functionalscript 0.0.427 → 0.0.428

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.
@@ -3,7 +3,7 @@ const { paramList } = types
3
3
  const text = require('../../text/module.f.cjs')
4
4
  const object = require('../../types/object/module.f.cjs')
5
5
  const list = require('../../types/list/module.f.cjs')
6
- const { flat, map, flatMap } = list
6
+ const { flat, map, flatMap, isEmpty } = list
7
7
  const { entries } = Object
8
8
  const { fn } = require('../../types/function/module.f.cjs')
9
9
  const { join } = require('../../types/string/module.f.cjs')
@@ -47,6 +47,83 @@ const mapAssign = map(assign)
47
47
 
48
48
  const this_ = ['this: &Object']
49
49
 
50
+ /** @type {(n: string) => string} */
51
+ const rustType = n => `pub type ${n} = nanocom::${n}<Interface>;`
52
+
53
+ /**
54
+ * @typedef {{
55
+ * readonly where?: readonly string[]
56
+ * readonly content: text.Block
57
+ * }} WhereContent
58
+ */
59
+
60
+ /** @type {(h: string) => (wh: WhereContent) => text.Block} */
61
+ const whereContent = h => ({ where, content }) => {
62
+ const w = isEmpty(where) ? [`${h} {`] : [
63
+ h,
64
+ `where`,
65
+ mapComma(where),
66
+ '{'
67
+ ]
68
+ const x = [
69
+ content,
70
+ '}',
71
+ ]
72
+ return flat([w, x])
73
+ }
74
+
75
+ /**
76
+ * @typedef {{
77
+ * readonly param?: string
78
+ * readonly trait: string
79
+ * readonly type: string
80
+ * readonly where?: readonly string[]
81
+ * readonly content: text.Block
82
+ * }} Impl
83
+ */
84
+
85
+ /** @type {(impl: Impl) => text.Block} */
86
+ const rustImpl = i => {
87
+ const p = i.param === undefined ? '' : `<${i.param}>`
88
+ const header = `impl${p} ${i.trait} for ${i.type}`
89
+ return whereContent(header)(i)
90
+ }
91
+
92
+ /**
93
+ * @typedef {{
94
+ * readonly pub?: true
95
+ * readonly type: string
96
+ * readonly where?: readonly string[]
97
+ * readonly content: text.Block
98
+ * }} Trait
99
+ */
100
+
101
+ /** @type {(s: string) => string} */
102
+ const comma = s => `${s},`
103
+
104
+ const mapComma = map(comma)
105
+
106
+ /** @type {(t: Trait) => text.Block} */
107
+ const trait = t => {
108
+ const p = t.pub === true ? 'pub ' : ''
109
+ const h = `${p}trait ${t.type}`
110
+ return whereContent(h)(t)
111
+ }
112
+
113
+ /** @type {(t: Trait) => text.Block} */
114
+ const traitImpl = t => {
115
+ const i = rustImpl({
116
+ param: 'T',
117
+ trait: t.type,
118
+ type: 'T',
119
+ where,
120
+ content: [],
121
+ })
122
+ return flat([trait({ ...t, where }), i])
123
+ }
124
+
125
+ const where = ['Self: nanocom::Class<Interface = Interface>', 'nanocom::CObject<Self>: Ex']
126
+
50
127
  /** @type {(library: types.Library) => text.Block} */
51
128
  const rust = library => {
52
129
 
@@ -134,38 +211,34 @@ const rust = library => {
134
211
  return [
135
212
  `pub mod ${name} {`,
136
213
  [
137
- 'pub type Object = nanocom::Object<Interface>;',
138
- 'pub type Ref = nanocom::Ref<Interface>;',
139
- 'pub type Vmt = nanocom::Vmt<Interface>;',
214
+ rustType('Object'),
215
+ rustType('Ref'),
216
+ rustType('Vmt'),
140
217
  ],
141
218
  rustStruct(mapVirtualFn(e))('Interface'),
142
- [ 'impl nanocom::Interface for Interface {',
143
- [ `const GUID: nanocom::GUID = 0x${guid.replaceAll('-', '_')};`],
144
- '}',
145
- 'pub trait Ex {',
146
- mapTraitFn(e),
147
- '}',
148
- 'impl Ex for Object {',
149
- flatMapImplFn(e),
150
- '}',
151
- 'pub trait ClassEx: nanocom::Class<Interface = Interface>',
152
- 'where',
153
- [ 'nanocom::CObject<Self>: Ex,'],
154
- '{',
155
- [ `const INTERFACE: Interface = Interface {`,
219
+ rustImpl({
220
+ trait: 'nanocom::Interface',
221
+ type: 'Interface',
222
+ content: [`const GUID: nanocom::GUID = 0x${guid.replaceAll('-', '_')};`]
223
+ }),
224
+ trait({ pub: true, type: 'Ex', content: mapTraitFn(e) }),
225
+ rustImpl({
226
+ trait: 'Ex',
227
+ type: 'Object',
228
+ content: flatMapImplFn(e)
229
+ }),
230
+ traitImpl({
231
+ pub: true,
232
+ type: 'ClassEx',
233
+ content: [`const INTERFACE: Interface = Interface {`,
156
234
  mapAssign(e),
157
235
  '};'
158
- ],
159
- '}',
160
- 'impl<T: nanocom::Class<Interface = Interface>> ClassEx for T where nanocom::CObject<T>: Ex {}',
161
- 'trait PrivateClassEx: nanocom::Class<Interface = Interface>',
162
- 'where',
163
- [ 'nanocom::CObject<Self>: Ex'],
164
- `{`,
165
- flatMapImpl(e),
166
- `}`,
167
- 'impl<T: nanocom::Class<Interface = Interface>> PrivateClassEx for T where nanocom::CObject<T>: Ex {}',
168
- ],
236
+ ]
237
+ }),
238
+ traitImpl({
239
+ type: 'PrivateClassEx',
240
+ content: flatMapImpl(e)
241
+ }),
169
242
  '}'
170
243
  ]
171
244
  }
@@ -57,8 +57,9 @@ module.exports = {
57
57
  ' unsafe { (self.interface().GetIMy)(self) }\n' +
58
58
  ' }\n' +
59
59
  ' }\n' +
60
- ' pub trait ClassEx: nanocom::Class<Interface = Interface>\n' +
60
+ ' pub trait ClassEx\n' +
61
61
  ' where\n' +
62
+ ' Self: nanocom::Class<Interface = Interface>,\n' +
62
63
  ' nanocom::CObject<Self>: Ex,\n' +
63
64
  ' {\n' +
64
65
  ' const INTERFACE: Interface = Interface {\n' +
@@ -70,10 +71,16 @@ module.exports = {
70
71
  ' GetIMy: Self::GetIMy,\n' +
71
72
  ' };\n' +
72
73
  ' }\n' +
73
- ' impl<T: nanocom::Class<Interface = Interface>> ClassEx for T where nanocom::CObject<T>: Ex {}\n' +
74
- ' trait PrivateClassEx: nanocom::Class<Interface = Interface>\n' +
74
+ ' impl<T> ClassEx for T\n' +
75
75
  ' where\n' +
76
- ' nanocom::CObject<Self>: Ex\n' +
76
+ ' Self: nanocom::Class<Interface = Interface>,\n' +
77
+ ' nanocom::CObject<Self>: Ex,\n' +
78
+ ' {\n' +
79
+ ' }\n' +
80
+ ' trait PrivateClassEx\n' +
81
+ ' where\n' +
82
+ ' Self: nanocom::Class<Interface = Interface>,\n' +
83
+ ' nanocom::CObject<Self>: Ex,\n' +
77
84
  ' {\n' +
78
85
  ' extern "system" fn GetSlice(this: &Object) -> super::Slice {\n' +
79
86
  ' unsafe { Self::to_cobject(this) }.GetSlice()\n' +
@@ -94,7 +101,12 @@ module.exports = {
94
101
  ' unsafe { Self::to_cobject(this) }.GetIMy()\n' +
95
102
  ' }\n' +
96
103
  ' }\n' +
97
- ' impl<T: nanocom::Class<Interface = Interface>> PrivateClassEx for T where nanocom::CObject<T>: Ex {}\n' +
104
+ ' impl<T> PrivateClassEx for T\n' +
105
+ ' where\n' +
106
+ ' Self: nanocom::Class<Interface = Interface>,\n' +
107
+ ' nanocom::CObject<Self>: Ex,\n' +
108
+ ' {\n' +
109
+ ' }\n' +
98
110
  '}'
99
111
  const r = join('\n')(flat(' ')(rust(library)))
100
112
  if (r !== e) { throw [e, r] }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "functionalscript",
3
- "version": "0.0.427",
3
+ "version": "0.0.428",
4
4
  "description": "FunctionalScript is a functional subset of JavaScript",
5
5
  "main": "module.f.cjs",
6
6
  "scripts": {