functionalscript 0.0.456 → 0.0.458

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/Cargo.lock CHANGED
@@ -4,7 +4,7 @@ version = 3
4
4
 
5
5
  [[package]]
6
6
  name = "nanocom"
7
- version = "0.1.2"
7
+ version = "0.2.0"
8
8
 
9
9
  [[package]]
10
10
  name = "testrust"
@@ -196,7 +196,7 @@ const rust = library => {
196
196
  const type = virtualFnType(` ${n}`)(p)
197
197
  return [
198
198
  `${type} {`,
199
- [`unsafe { Self::to_cobject(this) }.${n}(${call(p)})`],
199
+ [`unsafe { nanocom::CObject::from_object_unchecked(this) }.${n}(${call(p)})`],
200
200
  '}'
201
201
  ]
202
202
  }
@@ -231,7 +231,7 @@ const rust = library => {
231
231
  pub: true,
232
232
  type: 'ClassEx',
233
233
  content: ['const VMT: Vmt = Vmt {',
234
- [ 'iunknown: Self::IUNKNOWN,',
234
+ [ 'iunknown: nanocom::CObject::<Self>::IUNKNOWN,',
235
235
  'interface: Interface {',
236
236
  mapAssign(e),
237
237
  '},',
@@ -1,6 +1,6 @@
1
1
  [package]
2
2
  name = "nanocom"
3
- version = "0.1.2"
3
+ version = "0.2.0"
4
4
  edition = "2021"
5
5
  description = "Nano-COM, extremly small subset of cross-platform COM"
6
6
  authors = ["NatFoam"]
@@ -1,14 +1,6 @@
1
- use crate::{iunknown::IUnknown, CObject, Interface, Object, Ref, Vmt};
1
+ use crate::{Interface, Vmt};
2
2
 
3
3
  pub trait Class: Sized {
4
4
  type Interface: Interface;
5
5
  fn static_vmt() -> &'static Vmt<Self::Interface>;
6
- const IUNKNOWN: IUnknown<Self::Interface> = CObject::<Self>::IUNKNOWN;
7
- fn cobject_new(self) -> Ref<Self::Interface> {
8
- CObject::new(self)
9
- }
10
- unsafe fn to_cobject(this: &Object<Self::Interface>) -> &CObject<Self> {
11
- let p = this as *const Object<Self::Interface> as *const CObject<Self>;
12
- &*p
13
- }
14
6
  }
@@ -7,33 +7,23 @@ use crate::{hresult::HRESULT, iunknown::IUnknown, Class, Interface, Object, Ref,
7
7
 
8
8
  #[repr(C)]
9
9
  pub struct CObject<T: Class> {
10
- vmt: &'static Vmt<T::Interface>,
10
+ object: Object<T::Interface>,
11
11
  counter: AtomicU32,
12
12
  pub value: T,
13
13
  }
14
14
 
15
15
  impl<T: Class> CObject<T> {
16
- pub fn new(value: T) -> Ref<T::Interface> {
17
- let c = CObject {
18
- vmt: T::static_vmt(),
19
- counter: Default::default(),
20
- value,
21
- };
22
- let p = Box::into_raw(Box::new(c));
23
- unsafe { &*p }.to_interface().into()
24
- }
25
-
26
- pub fn to_interface(&self) -> &Object<T::Interface> {
27
- let p = self as *const Self as *const Object<T::Interface>;
28
- unsafe { &*p }
29
- }
30
-
31
16
  pub const IUNKNOWN: IUnknown<T::Interface> = IUnknown {
32
17
  QueryInterface: Self::QueryInterface,
33
18
  AddRef: Self::AddRef,
34
19
  Release: Self::Release,
35
20
  };
36
21
 
22
+ pub unsafe fn from_object_unchecked(this: &Object<T::Interface>) -> &CObject<T> {
23
+ let p = this as *const Object<T::Interface> as *const CObject<T>;
24
+ &*p
25
+ }
26
+
37
27
  #[allow(non_snake_case)]
38
28
  extern "system" fn QueryInterface(
39
29
  this: &Object<T::Interface>,
@@ -52,7 +42,7 @@ impl<T: Class> CObject<T> {
52
42
 
53
43
  #[allow(non_snake_case)]
54
44
  extern "system" fn AddRef(this: &Object<T::Interface>) -> u32 {
55
- unsafe { T::to_cobject(this) }
45
+ unsafe { Self::from_object_unchecked(this) }
56
46
  .counter
57
47
  .fetch_add(1, Ordering::Relaxed)
58
48
  + 1
@@ -60,7 +50,7 @@ impl<T: Class> CObject<T> {
60
50
 
61
51
  #[allow(non_snake_case)]
62
52
  extern "system" fn Release(this: &Object<T::Interface>) -> u32 {
63
- let t = unsafe { T::to_cobject(this) };
53
+ let t = unsafe { Self::from_object_unchecked(this) };
64
54
  match t.counter.fetch_sub(1, Ordering::Relaxed) {
65
55
  1 => {
66
56
  let m = t as *const CObject<T> as *mut CObject<T>;
@@ -71,3 +61,30 @@ impl<T: Class> CObject<T> {
71
61
  }
72
62
  }
73
63
  }
64
+
65
+ pub trait CObjectEx: Class {
66
+ fn to_cobject(self) -> Ref<Self::Interface> {
67
+ let c = CObject {
68
+ object: Object::new(Self::static_vmt()),
69
+ counter: Default::default(),
70
+ value: self,
71
+ };
72
+ let p = Box::into_raw(Box::new(c));
73
+ let o = &unsafe { &*p }.object;
74
+ o.into()
75
+ }
76
+ }
77
+
78
+ impl<T: Class> CObjectEx for T {}
79
+
80
+ impl<'a, T: Class> From<&'a CObject<T>> for &'a Object<T::Interface> {
81
+ fn from(this: &'a CObject<T>) -> Self {
82
+ &this.object
83
+ }
84
+ }
85
+
86
+ impl<T: Class> From<&CObject<T>> for Ref<T::Interface> {
87
+ fn from(this: &CObject<T>) -> Self {
88
+ (&this.object).into()
89
+ }
90
+ }
@@ -9,6 +9,12 @@ mod r#ref;
9
9
  mod vmt;
10
10
 
11
11
  pub use crate::{
12
- class::Class, cobject::CObject, guid::GUID, interface::Interface, iunknown::IUnknown,
13
- object::Object, r#ref::Ref, vmt::Vmt,
12
+ class::Class,
13
+ cobject::{CObject, CObjectEx},
14
+ guid::GUID,
15
+ interface::Interface,
16
+ iunknown::IUnknown,
17
+ object::Object,
18
+ r#ref::Ref,
19
+ vmt::Vmt,
14
20
  };
@@ -5,6 +5,9 @@ use std::ptr::null;
5
5
  pub struct Object<I: 'static = ()>(&'static Vmt<I>);
6
6
 
7
7
  impl<I> Object<I> {
8
+ pub fn new(vmt: &'static Vmt<I>) -> Self {
9
+ Self(vmt)
10
+ }
8
11
  pub unsafe fn iunknown(&self) -> &'static IUnknown<I> {
9
12
  &self.0.iunknown
10
13
  }
@@ -46,10 +46,10 @@ mod library {
46
46
  CObject<Self>: IMyEx,
47
47
  {
48
48
  extern "system" fn A(this: &Object<IMy>) -> Ref<IMy> {
49
- unsafe { Self::to_cobject(this) }.A()
49
+ unsafe { CObject::from_object_unchecked(this) }.A()
50
50
  }
51
51
  extern "system" fn B(this: &Object<IMy>) -> u32 {
52
- unsafe { Self::to_cobject(this) }.B()
52
+ unsafe { CObject::from_object_unchecked(this) }.B()
53
53
  }
54
54
  }
55
55
 
@@ -68,7 +68,7 @@ mod number {
68
68
  type Interface = IMy;
69
69
  fn static_vmt() -> &'static Vmt<Self::Interface> {
70
70
  static V: Vmt<IMy> = Vmt {
71
- iunknown: X::IUNKNOWN,
71
+ iunknown: CObject::<X>::IUNKNOWN,
72
72
  interface: X::INTERFACE,
73
73
  };
74
74
  &V
@@ -78,7 +78,7 @@ mod number {
78
78
  #[allow(non_snake_case)]
79
79
  impl IMyEx for CObject<X> {
80
80
  fn A(&self) -> Ref<IMy> {
81
- self.to_interface().into()
81
+ self.into()
82
82
  }
83
83
  fn B(&self) -> u32 {
84
84
  self.value.0
@@ -87,17 +87,17 @@ mod number {
87
87
  }
88
88
 
89
89
  mod use_number {
90
- use nanocom::Class;
90
+ use nanocom::{CObjectEx, Class};
91
91
 
92
92
  use super::{library::IMyEx, number::X};
93
93
 
94
94
  #[test]
95
95
  fn test() {
96
- let a = X(42).cobject_new();
96
+ let a = X(42).to_cobject();
97
97
  let a1 = a.A();
98
98
  assert_eq!(a, a1);
99
99
  assert_eq!(a.B(), 42);
100
- let b = X(43).cobject_new();
100
+ let b = X(43).to_cobject();
101
101
  assert_ne!(a, b);
102
102
  assert_eq!(b.B(), 43);
103
103
  }
@@ -134,7 +134,7 @@ mod destructor {
134
134
  type Interface = IMy;
135
135
  fn static_vmt() -> &'static Vmt<Self::Interface> {
136
136
  static V: Vmt<IMy> = Vmt {
137
- iunknown: X::IUNKNOWN,
137
+ iunknown: CObject::<X>::IUNKNOWN,
138
138
  interface: X::INTERFACE,
139
139
  };
140
140
  &V
@@ -144,7 +144,7 @@ mod destructor {
144
144
  #[allow(non_snake_case)]
145
145
  impl IMyEx for CObject<X> {
146
146
  fn A(&self) -> Ref<IMy> {
147
- self.to_interface().into()
147
+ self.into()
148
148
  }
149
149
  fn B(&self) -> u32 {
150
150
  self.value.p.load(Ordering::Relaxed)
@@ -158,7 +158,7 @@ mod use_destructor {
158
158
  sync::atomic::{AtomicU32, Ordering},
159
159
  };
160
160
 
161
- use nanocom::Class;
161
+ use nanocom::CObjectEx;
162
162
 
163
163
  use super::{destructor::X, library::IMyEx};
164
164
 
@@ -167,14 +167,14 @@ mod use_destructor {
167
167
  let p = Rc::new(AtomicU32::default());
168
168
  {
169
169
  assert_eq!(p.load(Ordering::Relaxed), 0);
170
- let a = X::new(p.clone()).cobject_new();
170
+ let a = X::new(p.clone()).to_cobject();
171
171
  assert_eq!(p.load(Ordering::Relaxed), 1);
172
172
  let a1 = a.A();
173
173
  assert_eq!(p.load(Ordering::Relaxed), 1);
174
174
  assert_eq!(a, a1);
175
175
  assert_eq!(a.B(), 1);
176
176
  {
177
- let b = X::new(p.clone()).cobject_new();
177
+ let b = X::new(p.clone()).to_cobject();
178
178
  assert_eq!(p.load(Ordering::Relaxed), 2);
179
179
  assert_ne!(a, b);
180
180
  assert_eq!(b.B(), 2);
@@ -4,6 +4,8 @@
4
4
 
5
5
  mod library {
6
6
  pub mod IMy {
7
+ use nanocom::CObject;
8
+
7
9
  pub type Object = nanocom::Object<Interface>;
8
10
  pub type Ref = nanocom::Ref<Interface>;
9
11
  pub type Vmt = nanocom::Vmt<Interface>;
@@ -37,11 +39,11 @@ mod library {
37
39
  nanocom::CObject<Self>: Ex,
38
40
  {
39
41
  const VMT: Vmt = Vmt {
40
- iunknown: Self::IUNKNOWN,
42
+ iunknown: CObject::<Self>::IUNKNOWN,
41
43
  interface: Interface {
42
44
  A: Self::A,
43
45
  B: Self::B,
44
- }
46
+ },
45
47
  };
46
48
  }
47
49
 
@@ -52,10 +54,10 @@ mod library {
52
54
  nanocom::CObject<Self>: Ex,
53
55
  {
54
56
  extern "system" fn A(this: &Object) -> Ref {
55
- unsafe { Self::to_cobject(this) }.A()
57
+ unsafe { CObject::from_object_unchecked(this) }.A()
56
58
  }
57
59
  extern "system" fn B(this: &Object) -> u32 {
58
- unsafe { Self::to_cobject(this) }.B()
60
+ unsafe { CObject::from_object_unchecked(this) }.B()
59
61
  }
60
62
  }
61
63
 
@@ -83,7 +85,7 @@ mod number {
83
85
 
84
86
  impl IMy::Ex for CObject<X> {
85
87
  fn A(&self) -> IMy::Ref {
86
- self.to_interface().into()
88
+ self.into()
87
89
  }
88
90
  fn B(&self) -> u32 {
89
91
  self.value.0
@@ -92,7 +94,7 @@ mod number {
92
94
  }
93
95
 
94
96
  mod use_number {
95
- use nanocom::Class;
97
+ use nanocom::CObjectEx;
96
98
 
97
99
  use crate::library::IMy::Ex;
98
100
 
@@ -100,11 +102,11 @@ mod use_number {
100
102
 
101
103
  #[test]
102
104
  fn test() {
103
- let a = X(42).cobject_new();
105
+ let a = X(42).to_cobject();
104
106
  let a1 = a.A();
105
107
  assert_eq!(a, a1);
106
108
  assert_eq!(a.B(), 42);
107
- let b = X(43).cobject_new();
109
+ let b = X(43).to_cobject();
108
110
  assert_ne!(a, b);
109
111
  assert_eq!(b.B(), 43);
110
112
  }
@@ -149,7 +151,7 @@ mod destructor {
149
151
 
150
152
  impl IMy::Ex for CObject<X> {
151
153
  fn A(&self) -> IMy::Ref {
152
- self.to_interface().into()
154
+ self.into()
153
155
  }
154
156
  fn B(&self) -> u32 {
155
157
  self.value.p.load(Ordering::Relaxed)
@@ -163,7 +165,7 @@ mod use_destructor {
163
165
  sync::atomic::{AtomicU32, Ordering},
164
166
  };
165
167
 
166
- use nanocom::Class;
168
+ use nanocom::CObjectEx;
167
169
 
168
170
  use crate::library::IMy::Ex;
169
171
 
@@ -174,14 +176,14 @@ mod use_destructor {
174
176
  let p = Rc::new(AtomicU32::default());
175
177
  {
176
178
  assert_eq!(p.load(Ordering::Relaxed), 0);
177
- let a = X::new(p.clone()).cobject_new();
179
+ let a = X::new(p.clone()).to_cobject();
178
180
  assert_eq!(p.load(Ordering::Relaxed), 1);
179
181
  let a1 = a.A();
180
182
  assert_eq!(p.load(Ordering::Relaxed), 1);
181
183
  assert_eq!(a, a1);
182
184
  assert_eq!(a.B(), 1);
183
185
  {
184
- let b = X::new(p.clone()).cobject_new();
186
+ let b = X::new(p.clone()).to_cobject();
185
187
  assert_eq!(p.load(Ordering::Relaxed), 2);
186
188
  assert_ne!(a, b);
187
189
  assert_eq!(b.B(), 2);
@@ -67,7 +67,7 @@ module.exports = () => {
67
67
  ' nanocom::CObject<Self>: Ex,\n' +
68
68
  ' {\n' +
69
69
  ' const VMT: Vmt = Vmt {\n' +
70
- ' iunknown: Self::IUNKNOWN,\n' +
70
+ ' iunknown: nanocom::CObject::<Self>::IUNKNOWN,\n' +
71
71
  ' interface: Interface {\n' +
72
72
  ' GetSlice: Self::GetSlice,\n' +
73
73
  ' SetSlice: Self::SetSlice,\n' +
@@ -91,25 +91,25 @@ module.exports = () => {
91
91
  ' nanocom::CObject<Self>: Ex,\n' +
92
92
  ' {\n' +
93
93
  ' extern "system" fn GetSlice(this: &Object) -> super::Slice {\n' +
94
- ' unsafe { Self::to_cobject(this) }.GetSlice()\n' +
94
+ ' unsafe { nanocom::CObject::from_object_unchecked(this) }.GetSlice()\n' +
95
95
  ' }\n' +
96
96
  ' extern "system" fn SetSlice(this: &Object, slice: super::Slice) {\n' +
97
- ' unsafe { Self::to_cobject(this) }.SetSlice(slice)\n' +
97
+ ' unsafe { nanocom::CObject::from_object_unchecked(this) }.SetSlice(slice)\n' +
98
98
  ' }\n' +
99
99
  ' extern "system" fn GetUnsafe(this: &Object) -> *const bool {\n' +
100
- ' unsafe { Self::to_cobject(this) }.GetUnsafe()\n' +
100
+ ' unsafe { nanocom::CObject::from_object_unchecked(this) }.GetUnsafe()\n' +
101
101
  ' }\n' +
102
102
  ' extern "system" fn SetUnsafe(this: &Object, p: *const super::Slice, size: u32) {\n' +
103
- ' unsafe { Self::to_cobject(this) }.SetUnsafe(p, size)\n' +
103
+ ' unsafe { nanocom::CObject::from_object_unchecked(this) }.SetUnsafe(p, size)\n' +
104
104
  ' }\n' +
105
105
  ' extern "system" fn Some(this: &Object, p: &super::IMy::Object) -> bool {\n' +
106
- ' unsafe { Self::to_cobject(this) }.Some(p)\n' +
106
+ ' unsafe { nanocom::CObject::from_object_unchecked(this) }.Some(p)\n' +
107
107
  ' }\n' +
108
108
  ' extern "system" fn GetIMy(this: &Object) -> super::IMy::Ref {\n' +
109
- ' unsafe { Self::to_cobject(this) }.GetIMy()\n' +
109
+ ' unsafe { nanocom::CObject::from_object_unchecked(this) }.GetIMy()\n' +
110
110
  ' }\n' +
111
111
  ' extern "system" fn SetManagedStruct(this: &Object, a: super::ManagedStruct) {\n' +
112
- ' unsafe { Self::to_cobject(this) }.SetManagedStruct(a)\n' +
112
+ ' unsafe { nanocom::CObject::from_object_unchecked(this) }.SetManagedStruct(a)\n' +
113
113
  ' }\n' +
114
114
  ' }\n' +
115
115
  ' impl<T> PrivateClassEx for T\n' +
@@ -40,4 +40,4 @@ impl _result::IMy::Ex for nanocom::CObject<My> {
40
40
  fn SetManagedStruct(&self, a: _result::ManagedStruct) {
41
41
  todo!()
42
42
  }
43
- }
43
+ }
package/fsm/module.f.cjs CHANGED
@@ -14,15 +14,10 @@ const byteSet = require('../types/byte_set/module.f.cjs')
14
14
  * }} Dfa
15
15
  */
16
16
 
17
- /** @type {(faId: string) => string} */
18
- const escape = faId => faId.replaceAll('\\', '\\\\').replaceAll('|', '\\|')
19
-
20
17
  /** @type {(grammar: Grammar) => Dfa} */
21
18
  const dfa = grammar => todo()
22
19
 
23
20
  module.exports = {
24
- /** @readonly */
25
- escape,
26
21
  /** @readonly */
27
22
  dfa,
28
23
  }
package/fsm/test.f.cjs CHANGED
@@ -1,14 +1,5 @@
1
1
  const _ = require('./module.f.cjs')
2
2
 
3
3
  module.exports = {
4
- escape: [
5
- () => {
6
- const result = _.escape('abc')
7
- if (result !== 'abc') { throw result }
8
- },
9
- () => {
10
- const result = _.escape('\\a|b|c\\')
11
- if (result !== '\\\\a\\|b\\|c\\\\') { throw result }
12
- }
13
- ]
4
+
14
5
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "functionalscript",
3
- "version": "0.0.456",
3
+ "version": "0.0.458",
4
4
  "description": "FunctionalScript is a functional subset of JavaScript",
5
5
  "main": "module.f.cjs",
6
6
  "scripts": {
@@ -1,4 +1,8 @@
1
1
  const { fn } = require('../function/module.f.cjs')
2
+ const rangeMap = require('../range_map/module.f.cjs')
3
+ const sortedSet = require('../sorted_set/module.f.cjs')
4
+ const list = require('../list/module.f.cjs')
5
+ const { reverse, countdown, flat, map } = list
2
6
 
3
7
  /** @typedef {bigint} ByteSet */
4
8
  /** @typedef {number} Byte */
@@ -42,6 +46,20 @@ const setRange = fn(range).then(union).result
42
46
  /** @type {(n: Byte) => (s: ByteSet) => ByteSet} */
43
47
  const unset = n => s => difference(s)(one(n))
44
48
 
49
+ const counter = reverse(countdown(256))
50
+
51
+ /** @type {(n: ByteSet) => (s: string) => (i: number) => rangeMap.RangeMap<sortedSet.SortedSet<string>>} */
52
+ const toRangeMapOp = n => s => i =>
53
+ {
54
+ const current = has(i + 1)(n)
55
+ const prev = has(i)(n)
56
+ if (current === prev) { return undefined }
57
+ return [[prev ? [s] : [], i]]
58
+ }
59
+
60
+ /** @type {(n: ByteSet) => (s: string) => rangeMap.RangeMap<sortedSet.SortedSet<string>>} */
61
+ const toRangeMap = n => s => flat(map(toRangeMapOp(n)(s))(counter))
62
+
45
63
  module.exports = {
46
64
  /** @readonly */
47
65
  empty,
@@ -61,4 +79,6 @@ module.exports = {
61
79
  range,
62
80
  /** @readonly */
63
81
  complement,
82
+ /** @readonly */
83
+ toRangeMap,
64
84
  }
@@ -1,5 +1,11 @@
1
1
  const _ = require('./module.f.cjs')
2
2
  const { every, countdown, map } = require('../list/module.f.cjs')
3
+ const json = require('../../json/module.f.cjs')
4
+ const { sort } = require('../object/module.f.cjs')
5
+ const { toArray } = require('../list/module.f.cjs')
6
+
7
+ /** @type {(a: readonly json.Unknown[]) => string} */
8
+ const stringify = a => json.stringify(sort)(a)
3
9
 
4
10
  module.exports = {
5
11
  has: [
@@ -52,5 +58,25 @@ module.exports = {
52
58
  const r = _.complement(_.universe)
53
59
  if (r !== _.empty) { throw r }
54
60
  },
55
- }
61
+ },
62
+ toRangeMap: [
63
+ () => {
64
+ const result = stringify(toArray(_.toRangeMap(_.empty)('a')))
65
+ if (result !== '[]') { throw result }
66
+ },
67
+ () => {
68
+ const s = _.set(0)(_.empty)
69
+ const result = stringify(toArray(_.toRangeMap(s)('a')))
70
+ if (result !== '[[["a"],0]]') { throw result }
71
+ },
72
+ () => {
73
+ const s = _.setRange([1,2])(_.empty)
74
+ const result = stringify(toArray(_.toRangeMap(s)('a')))
75
+ if (result !== '[[[],0],[["a"],2]]') { throw result }
76
+ },
77
+ () => {
78
+ const result = stringify(toArray(_.toRangeMap(_.universe)('a')))
79
+ if (result !== '[[["a"],255]]') { throw result }
80
+ },
81
+ ]
56
82
  }