functionalscript 0.0.435 → 0.0.437

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 obj = require('../../types/object/module.f.cjs')
5
5
  const list = require('../../types/list/module.f.cjs')
6
- const { map, flatMap } = list
6
+ const { map, flatMap, flat } = list
7
7
  const { join } = require('../../types/string/module.f.cjs')
8
8
  const { entries } = Object
9
9
 
@@ -77,9 +77,14 @@ const cpp = name => lib => {
77
77
  ? struct(name)(defStruct(d))
78
78
  : struct(`${name}: ::com::IUnknown`)(defInterface(d))
79
79
 
80
+ /** @type {(kv: obj.Entry<types.Definition>) => text.Block} */
81
+ const forward = ([name]) => [`struct ${name};`]
82
+
83
+ const e = entries(lib)
84
+
80
85
  return list.flat([
81
86
  ['#pragma once', ''],
82
- namespace(name)(flatMap(def)(entries(lib)))
87
+ namespace(name)(flat([flatMap(forward)(e), flatMap(def)(e)]))
83
88
  ])
84
89
  }
85
90
 
@@ -11,10 +11,16 @@ const f = () =>
11
11
  '\n' +
12
12
  'namespace My\n' +
13
13
  '{\n' +
14
+ ' struct Slice;\n' +
15
+ ' struct ManagedStruct;\n' +
16
+ ' struct IMy;\n' +
14
17
  ' struct Slice\n' +
15
18
  ' {\n' +
16
19
  ' uint8_t* Start;\n' +
17
20
  ' size_t Size;\n' +
21
+ ' };\n' +
22
+ ' struct ManagedStruct\n' +
23
+ ' {\n' +
18
24
  ' ::com::ref<IMy> M;\n' +
19
25
  ' };\n' +
20
26
  ' struct IMy: ::com::IUnknown\n' +
@@ -25,6 +31,7 @@ const f = () =>
25
31
  ' virtual void COM_STDCALL SetUnsafe(Slice* p, uint32_t size) = 0;\n' +
26
32
  ' virtual ::com::BOOL COM_STDCALL Some(IMy& p) = 0;\n' +
27
33
  ' virtual ::com::ref<IMy> COM_STDCALL GetIMy() = 0;\n' +
34
+ ' virtual void COM_STDCALL SetManagedStruct(ManagedStruct a) = 0;\n' +
28
35
  ' };\n' +
29
36
  '}'
30
37
  if (r !== e) { throw r }
package/com/cs/test.f.cjs CHANGED
@@ -17,6 +17,10 @@ const f = () =>
17
17
  ' {\n' +
18
18
  ' public unsafe byte* Start;\n' +
19
19
  ' public UIntPtr Size;\n' +
20
+ ' }\n' +
21
+ ' [StructLayout(LayoutKind.Sequential)]\n' +
22
+ ' public struct ManagedStruct\n' +
23
+ ' {\n' +
20
24
  ' public IMy M;\n' +
21
25
  ' }\n' +
22
26
  ' [Guid("C66FB270-2D80-49AD-BB6E-88C1F90B805D")]\n' +
@@ -35,9 +39,11 @@ const f = () =>
35
39
  ' bool Some(IMy p);\n' +
36
40
  ' [PreserveSig]\n' +
37
41
  ' IMy GetIMy();\n' +
42
+ ' [PreserveSig]\n' +
43
+ ' void SetManagedStruct(ManagedStruct a);\n' +
38
44
  ' }\n' +
39
45
  '}'
40
- if (cs !== e) { throw [cs,e] }
46
+ if (cs !== e) { throw cs }
41
47
  return cs
42
48
  }
43
49
 
@@ -11,6 +11,9 @@ module.exports = {
11
11
  'pub struct Slice {\n' +
12
12
  ' pub Start: *const u8,\n' +
13
13
  ' pub Size: usize,\n' +
14
+ '}\n' +
15
+ '#[repr(C)]\n' +
16
+ 'pub struct ManagedStruct {\n' +
14
17
  ' pub M: IMy::Ref,\n' +
15
18
  '}\n' +
16
19
  'pub mod IMy {\n' +
@@ -25,6 +28,7 @@ module.exports = {
25
28
  ' pub SetUnsafe: unsafe extern "system" fn(this: &Object, p: *const super::Slice, size: u32),\n' +
26
29
  ' pub Some: unsafe extern "system" fn(this: &Object, p: &super::IMy::Object) -> bool,\n' +
27
30
  ' pub GetIMy: unsafe extern "system" fn(this: &Object) -> super::IMy::Ref,\n' +
31
+ ' pub SetManagedStruct: unsafe extern "system" fn(this: &Object, a: super::ManagedStruct),\n' +
28
32
  ' }\n' +
29
33
  ' impl nanocom::Interface for Interface {\n' +
30
34
  ' const GUID: nanocom::GUID = 0xC66FB270_2D80_49AD_BB6E_88C1F90B805D;\n' +
@@ -36,6 +40,7 @@ module.exports = {
36
40
  ' fn SetUnsafe(&self, p: *const super::Slice, size: u32);\n' +
37
41
  ' fn Some(&self, p: &super::IMy::Object) -> bool;\n' +
38
42
  ' fn GetIMy(&self) -> super::IMy::Ref;\n' +
43
+ ' fn SetManagedStruct(&self, a: super::ManagedStruct);\n' +
39
44
  ' }\n' +
40
45
  ' impl Ex for Object {\n' +
41
46
  ' fn GetSlice(&self) -> super::Slice {\n' +
@@ -56,6 +61,9 @@ module.exports = {
56
61
  ' fn GetIMy(&self) -> super::IMy::Ref {\n' +
57
62
  ' unsafe { (self.interface().GetIMy)(self) }\n' +
58
63
  ' }\n' +
64
+ ' fn SetManagedStruct(&self, a: super::ManagedStruct) {\n' +
65
+ ' unsafe { (self.interface().SetManagedStruct)(self, a) }\n' +
66
+ ' }\n' +
59
67
  ' }\n' +
60
68
  ' pub trait ClassEx\n' +
61
69
  ' where\n' +
@@ -69,6 +77,7 @@ module.exports = {
69
77
  ' SetUnsafe: Self::SetUnsafe,\n' +
70
78
  ' Some: Self::Some,\n' +
71
79
  ' GetIMy: Self::GetIMy,\n' +
80
+ ' SetManagedStruct: Self::SetManagedStruct,\n' +
72
81
  ' };\n' +
73
82
  ' }\n' +
74
83
  ' impl<T> ClassEx for T\n' +
@@ -100,6 +109,9 @@ module.exports = {
100
109
  ' extern "system" fn GetIMy(this: &Object) -> super::IMy::Ref {\n' +
101
110
  ' unsafe { Self::to_cobject(this) }.GetIMy()\n' +
102
111
  ' }\n' +
112
+ ' extern "system" fn SetManagedStruct(this: &Object, a: super::ManagedStruct) {\n' +
113
+ ' unsafe { Self::to_cobject(this) }.SetManagedStruct(a)\n' +
114
+ ' }\n' +
103
115
  ' }\n' +
104
116
  ' impl<T> PrivateClassEx for T\n' +
105
117
  ' where\n' +
@@ -109,7 +121,7 @@ module.exports = {
109
121
  ' }\n' +
110
122
  '}'
111
123
  const r = join('\n')(flat(' ')(rust(library)))
112
- if (r !== e) { throw [e, r] }
124
+ if (r !== e) { throw r }
113
125
  return r
114
126
  }
115
127
  }
@@ -0,0 +1,37 @@
1
+ const fs = require('node:fs')
2
+ const cp = require('node:child_process')
3
+ const os = require('node:os');
4
+ const cpp = require('../cpp/test.f.cjs').result
5
+ const { string: { join }, list: { flat } } = require('../../types/module.f.cjs')
6
+
7
+ const dirname = __dirname
8
+
9
+ fs.writeFileSync(`${dirname}/cpp/_result.hpp`, cpp)
10
+ try {
11
+ const flags = os.platform() === 'win32' ? [] : ['-std=c++11', '-lc++']
12
+ const line = join(' ')(flat([['clang'], flags, [dirname + '/cpp/main.cpp']]))
13
+ console.log(cp.execSync(line).toString())
14
+ } catch (e) {
15
+ // @ts-ignore
16
+ console.error(e.output.toString())
17
+ }
18
+
19
+ const cs = require('../cs/test.f.cjs').result
20
+
21
+ fs.writeFileSync(`${dirname}/cs/_result.cs`, cs)
22
+ try {
23
+ console.log(cp.execSync(`dotnet build ${dirname}/cs/cs.csproj`).toString())
24
+ } catch (e) {
25
+ // @ts-ignore
26
+ console.error(e.output.toString())
27
+ }
28
+
29
+ const rust = require("../rust/test.f.cjs").result();
30
+
31
+ fs.writeFileSync(`${dirname}/rust/src/_result.rs`, rust);
32
+ try {
33
+ console.log(cp.execSync("cargo build").toString());
34
+ } catch (e) {
35
+ // @ts-ignore
36
+ console.error(e.output.toString());
37
+ }
@@ -1,4 +1,4 @@
1
- #include "../com.hpp"
1
+ #include "../../cpp/com.hpp"
2
2
  #include "_result.hpp"
3
3
 
4
4
  #include <iostream>
File without changes
@@ -6,9 +6,13 @@ module.exports = {
6
6
  struct: {
7
7
  Start: ['*', 'u8'],
8
8
  Size: 'usize',
9
- M: ['IMy'],
10
9
  },
11
10
  },
11
+ ManagedStruct: {
12
+ struct: {
13
+ M: ['IMy']
14
+ }
15
+ },
12
16
  IMy: {
13
17
  guid: 'C66FB270-2D80-49AD-BB6E-88C1F90B805D',
14
18
  interface: {
@@ -20,7 +24,8 @@ module.exports = {
20
24
  size: 'u32'
21
25
  },
22
26
  Some: { p: ['IMy'], _: 'bool' },
23
- GetIMy: { _: ['IMy'] }
27
+ GetIMy: { _: ['IMy'] },
28
+ SetManagedStruct: { a: ['ManagedStruct'] },
24
29
  },
25
30
  }
26
31
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "functionalscript",
3
- "version": "0.0.435",
3
+ "version": "0.0.437",
4
4
  "description": "FunctionalScript is a functional subset of JavaScript",
5
5
  "main": "module.f.cjs",
6
6
  "scripts": {
@@ -29,7 +29,7 @@
29
29
  },
30
30
  "homepage": "https://github.com/functionalscript/functionalscript#readme",
31
31
  "devDependencies": {
32
- "@types/node": "^18.7.18",
33
- "typescript": "^4.8.3"
32
+ "@types/node": "^18.8.3",
33
+ "typescript": "^4.8.4"
34
34
  }
35
35
  }
@@ -1,6 +1,7 @@
1
1
  const compare = require("../function/compare/module.f.cjs")
2
2
  const list = require("../list/module.f.cjs")
3
- const { next, toArray } = list
3
+ const option = require("../option/module.f.cjs")
4
+ const { next } = list
4
5
 
5
6
  /**
6
7
  * @template T
@@ -19,20 +20,52 @@ const { next, toArray } = list
19
20
  * @typedef {SortedList<[Byte, readonly string[]]>} RangeMap
20
21
  */
21
22
 
23
+ /**
24
+ * @template S
25
+ * @template T
26
+ * @typedef {(state: S) => (a: T) => (b: T) => readonly[option.Option<T>, compare.Sign, S]} ReduceOp
27
+ */
28
+
29
+ /**
30
+ * @template S
31
+ * @template T
32
+ * @typedef {(state: S) => (tail: list.List<T>) => list.List<T>} TailReduce
33
+ */
34
+
35
+ /**
36
+ * @template S
37
+ * @template T
38
+ * @typedef {(init: S) => (reduce: ReduceOp<S,T>) => (tailReduce: TailReduce<S, T>) => (a: list.List<T>) => (b: list.List<T>) => list.List<T>} GenericMerge
39
+ */
40
+
22
41
  /** @type {<T>(cmp: Cmp<T>) => (a: SortedList<T>) => (b: SortedList<T>) => SortedList<T>} */
23
- const merge = cmp => a => b => () => {
42
+ const merge = cmp => genericMerge(undefined)(cmpReduce(cmp))(mergeTail)
43
+
44
+ /** @type {<S,T>(cmp: Cmp<T>) => ReduceOp<S, T>} */
45
+ const cmpReduce = cmp => state => a => b => {
46
+ const sign = cmp(a)(b)
47
+ return [sign === 1 ? b : a, sign, state]
48
+ }
49
+
50
+ /** @type {<S,T>(state: S) => (tail: list.List<T>) => list.List<T>} */
51
+ const mergeTail = s => input => input
52
+
53
+ /** @type {<S,T>(init: S) => (reduce: ReduceOp<S,T>) => (tailReduce: TailReduce<S, T>) => (a: list.List<T>) => (b: list.List<T>) => list.List<T>} */
54
+ const genericMerge = init => reduce => tailReduce => a => b => () => {
24
55
  const aResult = next(a)
25
- if (aResult === undefined) { return b }
56
+ if (aResult === undefined) { return tailReduce(init)(b) }
26
57
  const bResult = next(b)
27
- if (bResult === undefined) { return a }
28
- switch (cmp(aResult.first)(bResult.first)) {
29
- case -1: return { first: aResult.first, tail: merge(cmp)(aResult.tail)(b) }
30
- case 0: return { first: aResult.first, tail: merge(cmp)(aResult.tail)(bResult.tail) }
31
- case 1: return { first: bResult.first, tail: merge(cmp)(a)(bResult.tail) }
32
- }
58
+ if (bResult === undefined) { return tailReduce(init)(a) }
59
+ const [result, sign, state] = reduce(init)(aResult.first)(bResult.first)
60
+ const aNext = sign === 1 ? a : aResult.tail
61
+ const bNext = sign === -1 ? b : bResult.tail
62
+ const mergeNext = genericMerge(state)(reduce)(tailReduce)(aNext)(bNext)
63
+ return result === undefined ? mergeNext : { first: result, tail: mergeNext }
33
64
  }
34
65
 
35
66
  module.exports = {
36
67
  /** @readonly */
37
- merge
68
+ merge,
69
+ /** @readonly */
70
+ genericMerge
38
71
  }
@@ -0,0 +1,43 @@
1
+ const compare = require("../function/compare/module.f.cjs")
2
+ const { toArray } = require("../list/module.f.cjs")
3
+ const sortedList = require("../sorted_list/module.f.cjs")
4
+ const { merge, genericMerge } = sortedList
5
+ const list = require("../list/module.f.cjs")
6
+ const option = require("../option/module.f.cjs")
7
+
8
+ /**
9
+ * @template T
10
+ * @typedef {readonly T[]} SortedSet
11
+ */
12
+
13
+ /**
14
+ * @template T
15
+ * @typedef {(a: T) => (b: T) => compare.Sign} Cmp
16
+ */
17
+
18
+ /** @typedef {number} Byte */
19
+
20
+ /** @type {<T>(cmp: Cmp<T>) => (a: SortedSet<T>) => (b: SortedSet<T>) => SortedSet<T>} */
21
+ const union = cmp => a => b => toArray(merge(cmp)(a)(b))
22
+
23
+ /** @type {<T>(cmp: Cmp<T>) => (a: SortedSet<T>) => (b: SortedSet<T>) => SortedSet<T>} */
24
+ const intersect = cmp => a => b => toArray(intersectMerge(cmp)(a)(b))
25
+
26
+ /** @type {<T>(cmp: Cmp<T>) => (a: sortedList.SortedList<T>) => (b: sortedList.SortedList<T>) => sortedList.SortedList<T>} */
27
+ const intersectMerge = cmp => genericMerge(undefined)(intersectReduce(cmp))(intersectTail)
28
+
29
+ /** @type {<S,T>(cmp: Cmp<T>) => sortedList.ReduceOp<S, T>} */
30
+ const intersectReduce = cmp => state => a => b => {
31
+ const sign = cmp(a)(b)
32
+ return [sign === 0 ? a : undefined, sign, state]
33
+ }
34
+
35
+ /** @type {<S,T>(state: S) => (tail: list.List<T>) => list.List<T>} */
36
+ const intersectTail = s => input => undefined
37
+
38
+ module.exports = {
39
+ /** @readonly */
40
+ union,
41
+ /** @readonly */
42
+ intersect
43
+ }
@@ -0,0 +1,44 @@
1
+ const _ = require('./module.f.cjs')
2
+ const { unsafeCmp } = require('../function/compare/module.f.cjs')
3
+ const json = require('../../json/module.f.cjs')
4
+ const { sort } = require('../../types/object/module.f.cjs')
5
+ const { toArray, countdown, length } = require('../list/module.f.cjs')
6
+ const map = require('../map/module.f.cjs')
7
+ const { flip } = require('../function/module.f.cjs')
8
+
9
+ /** @type {(a: readonly json.Unknown[]) => string} */
10
+ const stringify = a => json.stringify(sort)(a)
11
+
12
+ /** @type {<T>(a: T) => (b: T) => map.Sign} */
13
+ const reverseCmp = flip(unsafeCmp)
14
+
15
+ module.exports = {
16
+ union: [
17
+ () => {
18
+ const result = stringify(toArray(_.union(unsafeCmp)([2, 3, 4])([1, 3, 5])))
19
+ if (result !== '[1,2,3,4,5]') { throw result }
20
+ },
21
+ () => {
22
+ const result = stringify(toArray(_.union(unsafeCmp)([1, 2, 3])([])))
23
+ if (result !== '[1,2,3]') { throw result }
24
+ },
25
+ () => {
26
+ const n = 10_000
27
+ const sortedSet = toArray(countdown(n))
28
+ const result = _.union(reverseCmp)(sortedSet)(sortedSet)
29
+ const len = length(result)
30
+ if (len != n) { throw result }
31
+ }
32
+ ],
33
+ intersect: [
34
+ () => {
35
+ const result = stringify(toArray(_.intersect(unsafeCmp)([2, 3, 4])([1, 3, 5])))
36
+ console.log(result)
37
+ if (result !== '[3]') { throw result }
38
+ },
39
+ () => {
40
+ const result = stringify(toArray(_.intersect(unsafeCmp)([1, 2, 3])([])))
41
+ if (result !== '[]') { throw result }
42
+ }
43
+ ]
44
+ }
@@ -1,15 +0,0 @@
1
- const fs = require('node:fs')
2
- const cp = require('node:child_process')
3
- const os = require('node:os');
4
- const cpp = require('../../cpp/test.f.cjs').result
5
- const { string: { join }, list: { flat } } = require('../../../types/module.f.cjs')
6
-
7
- fs.writeFileSync('_result.hpp', cpp)
8
- try {
9
- const flags = os.platform() === 'win32' ? [] : ['-std=c++11', '-lc++']
10
- const line = join(' ')(flat([['clang'], flags, ['main.cpp']]))
11
- console.log(cp.execSync(line).toString())
12
- } catch (e) {
13
- // @ts-ignore
14
- console.error(e.output.toString())
15
- }
@@ -1,11 +0,0 @@
1
- const fs = require('node:fs')
2
- const cp = require('node:child_process')
3
- const cs = require('../../cs/test.f.cjs').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.error(e.output.toString())
11
- }