functionalscript 0.0.469 → 0.0.470

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/cpp/com.hpp CHANGED
@@ -13,14 +13,16 @@
13
13
  #define COM_STDCALL __stdcall
14
14
  #endif
15
15
 
16
+ static_assert(sizeof(bool) == 1);
17
+
16
18
  namespace com
17
19
  {
18
20
  constexpr uint64_t byteswap(uint64_t v) noexcept
19
21
  {
20
- v = v >> 8 & 0x00FF00FF00FF00FF |
21
- v << 8 & 0xFF00FF00FF00FF00;
22
- v = v >> 16 & 0x0000FFFF0000FFFF |
23
- v << 16 & 0xFFFF0000FFFF0000;
22
+ v = v >> 8 & 0x00FF'00FF'00FF'00FF |
23
+ v << 8 & 0xFF00'FF00'FF00'FF00;
24
+ v = v >> 16 & 0x0000'FFFF'0000'FFFF |
25
+ v << 16 & 0xFFFF'0000'FFFF'0000;
24
26
  return v >> 32 | v << 32;
25
27
  }
26
28
 
@@ -46,14 +48,17 @@ namespace com
46
48
  }
47
49
  };
48
50
 
51
+ constexpr inline char hex_digit(uint64_t const v, int const i) noexcept
52
+ {
53
+ char const c = v >> i & 0x0F;
54
+ return c < 10 ? c + '0' : c + ('A' - 10);
55
+ }
56
+
49
57
  inline void guid_part(std::ostream &os, uint64_t const v)
50
58
  {
51
- for (int i = 64; i > 0;)
59
+ for (int i = 60; i >= 0; i -= 4)
52
60
  {
53
- i -= 4;
54
- char const c = (v >> i) & 0xF;
55
- char const x = c < 10 ? c + '0' : c + ('A' - 10);
56
- os << x;
61
+ os << hex_digit(v, i);
57
62
  }
58
63
  }
59
64
 
@@ -64,28 +69,27 @@ namespace com
64
69
  return os;
65
70
  }
66
71
 
67
- typedef uint32_t HRESULT;
68
-
69
- static HRESULT const E_NOINTERFACE = 0x80004002;
70
- static HRESULT const S_OK = 0;
72
+ enum class HRESULT : uint32_t
73
+ {
74
+ S_OK = 0,
75
+ E_NOINTERFACE = 0x80004002,
76
+ };
71
77
 
72
78
  typedef uint32_t ULONG;
73
79
 
74
- typedef int32_t BOOL;
75
-
76
80
  class IUnknown
77
81
  {
78
82
  public:
79
- virtual HRESULT COM_STDCALL QueryInterface(GUID const &riid, IUnknown **const ppvObject) noexcept = 0;
80
- virtual ULONG COM_STDCALL AddRef() noexcept = 0;
81
- virtual ULONG COM_STDCALL Release() noexcept = 0;
83
+ virtual HRESULT COM_STDCALL QueryInterface(GUID const &riid, IUnknown const **ppvObject) const noexcept = 0;
84
+ virtual ULONG COM_STDCALL AddRef() const noexcept = 0;
85
+ virtual ULONG COM_STDCALL Release() const noexcept = 0;
82
86
  };
83
87
 
84
88
  template <class I>
85
89
  class ref
86
90
  {
87
91
  public:
88
- explicit ref(I &other) noexcept : p(other)
92
+ explicit ref(I const &other) noexcept : p(other)
89
93
  {
90
94
  p.AddRef();
91
95
  }
@@ -97,28 +101,41 @@ namespace com
97
101
  p.Release();
98
102
  }
99
103
 
100
- template<class U>
104
+ template <class U>
101
105
  ref<U> upcast() const noexcept
102
106
  {
103
107
  return ref<U>(p);
104
108
  }
105
109
 
106
- I* operator->() const noexcept
110
+ I const *operator->() const noexcept
107
111
  {
108
112
  return &p;
109
113
  }
110
114
 
111
- I* unsafe_result() const noexcept
115
+ I const *copy_to_raw() const noexcept
112
116
  {
113
117
  p.AddRef();
114
118
  return &p;
115
119
  }
120
+
121
+ static ref move_to_ref(I const *const p)
122
+ {
123
+ return ref(p);
124
+ }
125
+
116
126
  private:
117
- I &p;
127
+ I const &p;
128
+ ref(I const *const p) : p(*p) {}
118
129
  };
119
130
 
120
- template<class I>
121
- ref<I> to_ref(I& p) noexcept
131
+ template <class I>
132
+ ref<I> move_to_ref(I const *const p)
133
+ {
134
+ return ref<I>::move_to_ref(p);
135
+ }
136
+
137
+ template <class I>
138
+ ref<I> to_ref(I const &p) noexcept
122
139
  {
123
140
  return ref<I>(p);
124
141
  }
@@ -130,39 +147,40 @@ namespace com
130
147
  class implementation : public T
131
148
  {
132
149
  public:
133
- template<class ...U>
134
- static T* create_raw(U... u)
135
- {
136
- return new implementation(u...);
137
- }
138
- template<class ...U>
150
+ template <class... U>
139
151
  static ref<T> create(U... u)
140
152
  {
141
- return to_ref(*create_raw(u...));
153
+ T const *const p = new implementation(u...);
154
+ return to_ref(*p);
142
155
  }
156
+
143
157
  private:
144
- HRESULT COM_STDCALL QueryInterface(GUID const &riid, IUnknown **const ppvObject) noexcept override
158
+ HRESULT COM_STDCALL QueryInterface(GUID const &riid, IUnknown const **const ppvObject) const noexcept override
145
159
  {
160
+ // std::cout << "riid: " << riid << std::endl;
161
+ // std::cout << "iunknown: " << iunknown_guid << std::endl;
162
+ // std::cout << "T::guid: " << T::guid << std::endl;
163
+ // std::cout << std::endl;
146
164
  if (riid != iunknown_guid && riid != T::guid)
147
165
  {
148
- return E_NOINTERFACE;
166
+ return HRESULT::E_NOINTERFACE;
149
167
  }
150
168
  add_ref();
151
169
  *ppvObject = this;
152
- return S_OK;
170
+ return HRESULT::S_OK;
153
171
  }
154
172
 
155
- ULONG add_ref() noexcept
173
+ ULONG add_ref() const noexcept
156
174
  {
157
175
  return counter.fetch_add(1);
158
176
  }
159
177
 
160
- ULONG COM_STDCALL AddRef() noexcept override
178
+ ULONG COM_STDCALL AddRef() const noexcept override
161
179
  {
162
180
  return add_ref() + 1;
163
181
  }
164
182
 
165
- ULONG COM_STDCALL Release() noexcept override
183
+ ULONG COM_STDCALL Release() const noexcept override
166
184
  {
167
185
  auto const c = counter.fetch_sub(1) - 1;
168
186
  if (c == 0)
@@ -172,9 +190,9 @@ namespace com
172
190
  return c;
173
191
  }
174
192
 
175
- template<class ...U>
176
- explicit implementation(U... u): T(u...) {}
193
+ template <class... U>
194
+ explicit implementation(U... u) : T(u...) {}
177
195
 
178
- std::atomic<ULONG> counter;
196
+ mutable std::atomic<ULONG> counter;
179
197
  };
180
198
  }
@@ -23,7 +23,7 @@ const baseTypeMap = {
23
23
  isize: 'ptrdiff_t',
24
24
  f32: 'float',
25
25
  f64: 'double',
26
- bool: '::com::BOOL',
26
+ bool: 'bool',
27
27
  }
28
28
 
29
29
  /** @type {(t: types.BaseType) => string} */
@@ -36,10 +36,13 @@ const namespace = text.curly('namespace')
36
36
  /** @type {(name: string) => (lib: types.Library) => text.Block} */
37
37
  const cpp = name => lib => {
38
38
 
39
+ /** @type {(t: types.Type) => boolean} */
40
+ const isInterface = t => t instanceof Array && t.length === 1 && lib[t[0]].interface !== undefined
41
+
39
42
  /** @type {(i: (t: string) => string) => (t: types.Type) => string} */
40
43
  const objectType = i => t => {
41
44
  if (typeof (t) === 'string') { return baseType(t) }
42
- if (t.length === 2) { return `${type(t[1])}*` }
45
+ if (t.length === 2) { return `${type(t[1])} const*` }
43
46
  const [id] = t
44
47
  if (lib[id].interface === undefined) { return id }
45
48
  return i(id)
@@ -47,7 +50,7 @@ const cpp = name => lib => {
47
50
 
48
51
  const type = objectType(id => `::com::ref<${id}>`)
49
52
 
50
- const resultType = objectType(id => `${id}*`)
53
+ const resultType = objectType(id => `${id} const*`)
51
54
 
52
55
  /** @type {(s: types.Field) => text.Item} */
53
56
  const field = ([name, t]) => `${type(t)} ${name};`
@@ -61,13 +64,18 @@ const cpp = name => lib => {
61
64
  const cppResult = resultVoid(resultType)
62
65
 
63
66
  /** @type {(p: types.Field) => string} */
64
- const param = ([name, t]) => `${objectType(id => `${id}&`)(t)} ${name}`
67
+ const param = ([name, t]) => `${objectType(id => `${id} const&`)(t)} ${name}`
65
68
 
66
69
  const mapParam = map(param)
67
70
 
71
+ /** @type {(m: types.Method) => string} */
72
+ const virtualName = ([name, paramArray]) => isInterface(paramArray._) ? `${name}_` : name
73
+
68
74
  /** @type {(m: types.Method) => text.Item} */
69
- const method = ([name, paramArray]) =>
70
- `virtual ${cppResult(paramArray)} COM_STDCALL ${name}(${join(', ')(mapParam(paramList(paramArray)))}) noexcept = 0;`
75
+ const method = m => {
76
+ const [, paramArray] = m
77
+ return `virtual ${cppResult(paramArray)} COM_STDCALL ${virtualName(m)}(${join(', ')(mapParam(paramList(paramArray)))}) const noexcept = 0;`
78
+ }
71
79
 
72
80
  const mapMethod = map(method)
73
81
 
@@ -85,7 +93,7 @@ const cpp = name => lib => {
85
93
  /** @type {(kv: obj.Entry<types.Definition>) => text.Block} */
86
94
  const def = ([name, d]) => d.interface === undefined
87
95
  ? struct(name)(defStruct(d))
88
- : struct(`${name} : ::com::IUnknown`)(defInterface(d))
96
+ : [`class ${name} : public ::com::IUnknown`, '{', 'public:', defInterface(d), '};']
89
97
 
90
98
  /** @type {(kv: obj.Entry<types.Definition>) => text.Block} */
91
99
  const forward = ([name]) => [`struct ${name};`]
@@ -12,23 +12,24 @@ const f = () =>
12
12
  ' struct IMy;\n' +
13
13
  ' struct Slice\n' +
14
14
  ' {\n' +
15
- ' uint8_t* Start;\n' +
15
+ ' uint8_t const* Start;\n' +
16
16
  ' size_t Size;\n' +
17
17
  ' };\n' +
18
18
  ' struct ManagedStruct\n' +
19
19
  ' {\n' +
20
20
  ' ::com::ref<IMy> M;\n' +
21
21
  ' };\n' +
22
- ' struct IMy : ::com::IUnknown\n' +
22
+ ' class IMy : public ::com::IUnknown\n' +
23
23
  ' {\n' +
24
+ ' public:\n' +
24
25
  ' constexpr static ::com::GUID const guid = ::com::GUID(0xC66FB2702D8049AD, 0xBB6E88C1F90B805D);\n' +
25
- ' virtual Slice COM_STDCALL GetSlice() noexcept = 0;\n' +
26
- ' virtual void COM_STDCALL SetSlice(Slice slice) noexcept = 0;\n' +
27
- ' virtual ::com::BOOL* COM_STDCALL GetUnsafe() noexcept = 0;\n' +
28
- ' virtual void COM_STDCALL SetUnsafe(Slice* p, uint32_t size) noexcept = 0;\n' +
29
- ' virtual ::com::BOOL COM_STDCALL Some(IMy& p) noexcept = 0;\n' +
30
- ' virtual IMy* COM_STDCALL GetIMy() noexcept = 0;\n' +
31
- ' virtual void COM_STDCALL SetManagedStruct(ManagedStruct a) noexcept = 0;\n' +
26
+ ' virtual Slice COM_STDCALL GetSlice() const noexcept = 0;\n' +
27
+ ' virtual void COM_STDCALL SetSlice(Slice slice) const noexcept = 0;\n' +
28
+ ' virtual bool const* COM_STDCALL GetUnsafe() const noexcept = 0;\n' +
29
+ ' virtual void COM_STDCALL SetUnsafe(Slice const* p, uint32_t size) const noexcept = 0;\n' +
30
+ ' virtual bool COM_STDCALL Some(IMy const& p) const noexcept = 0;\n' +
31
+ ' virtual IMy const* COM_STDCALL GetIMy_() const noexcept = 0;\n' +
32
+ ' virtual void COM_STDCALL SetManagedStruct(ManagedStruct a) const noexcept = 0;\n' +
32
33
  ' };\n' +
33
34
  '}'
34
35
  if (cpp !== e) { throw cpp }
@@ -25,7 +25,7 @@ const typeDef = attributes => type => name => body =>
25
25
  ])
26
26
 
27
27
  const baseTypeMap = {
28
- bool: 'bool',
28
+ bool: 'byte',
29
29
  f32: 'float',
30
30
  f64: 'double',
31
31
  i16: 'short',
package/com/cs/test.f.cjs CHANGED
@@ -28,11 +28,11 @@ const f = () =>
28
28
  ' [PreserveSig]\n' +
29
29
  ' void SetSlice(Slice slice);\n' +
30
30
  ' [PreserveSig]\n' +
31
- ' unsafe bool* GetUnsafe();\n' +
31
+ ' unsafe byte* GetUnsafe();\n' +
32
32
  ' [PreserveSig]\n' +
33
33
  ' unsafe void SetUnsafe(Slice* p, uint size);\n' +
34
34
  ' [PreserveSig]\n' +
35
- ' bool Some(IMy p);\n' +
35
+ ' byte Some(IMy p);\n' +
36
36
  ' [PreserveSig]\n' +
37
37
  ' IMy GetIMy();\n' +
38
38
  ' [PreserveSig]\n' +
@@ -46,7 +46,7 @@ const flags = platform => {
46
46
  case 'win32':
47
47
  return []
48
48
  case 'linux':
49
- return ['-lstdc++', '-fPIC']
49
+ return ['-std=c++17', '-lstdc++', '-fPIC']
50
50
  default:
51
51
  return ['-std=c++17', '-lc++']
52
52
  }
@@ -13,35 +13,39 @@ extern "C" int c_get()
13
13
  return 43;
14
14
  }
15
15
 
16
- class Impl: public My::IMy
16
+ class Impl : public My::IMy
17
17
  {
18
18
  public:
19
- My::Slice COM_STDCALL GetSlice() noexcept override
19
+ My::Slice COM_STDCALL GetSlice() const noexcept override
20
20
  {
21
21
  }
22
- void COM_STDCALL SetSlice(My::Slice slice) noexcept override
22
+ void COM_STDCALL SetSlice(My::Slice slice) const noexcept override
23
23
  {
24
24
  std::cout
25
25
  << "SetSlice: "
26
- << (slice.Start - static_cast<uint8_t*>(nullptr))
26
+ << (slice.Start - static_cast<uint8_t const *>(nullptr))
27
27
  << ", "
28
28
  << slice.Size
29
29
  << std::endl;
30
30
  }
31
- ::com::BOOL *COM_STDCALL GetUnsafe() noexcept override
31
+ bool const *COM_STDCALL GetUnsafe() const noexcept override
32
32
  {
33
33
  }
34
- void COM_STDCALL SetUnsafe(My::Slice *p, uint32_t size) noexcept override
34
+ void COM_STDCALL SetUnsafe(My::Slice const *p, uint32_t size) const noexcept override
35
35
  {
36
36
  }
37
- ::com::BOOL COM_STDCALL Some(My::IMy &p) noexcept override
37
+ bool COM_STDCALL Some(My::IMy const &p) const noexcept override
38
38
  {
39
39
  }
40
- My::IMy* COM_STDCALL GetIMy() noexcept override
40
+ My::IMy const *COM_STDCALL GetIMy_() const noexcept override
41
41
  {
42
- return ::com::to_ref(*this).unsafe_result();
42
+ return ::com::to_ref(*this).copy_to_raw();
43
43
  }
44
- void COM_STDCALL SetManagedStruct(My::ManagedStruct a) noexcept override
44
+ com::ref<My::IMy> GetIMy() const noexcept
45
+ {
46
+ return com::move_to_ref(GetIMy_());
47
+ }
48
+ void COM_STDCALL SetManagedStruct(My::ManagedStruct a) const noexcept override
45
49
  {
46
50
  }
47
51
  ~Impl()
@@ -51,15 +55,15 @@ public:
51
55
  };
52
56
 
53
57
  DLL_EXPORT
54
- extern "C" My::IMy* c_my_create()
58
+ extern "C" My::IMy const *c_my_create()
55
59
  {
56
60
  {
57
- auto const x = ::com::implementation<Impl>::create().unsafe_result();
61
+ auto const x = ::com::implementation<Impl>::create().copy_to_raw();
58
62
  x->Release();
59
63
  }
60
64
  {
61
65
  auto const x = ::com::implementation<Impl>::create().upcast<My::IMy>();
62
66
  x->SetSlice(My::Slice());
63
67
  }
64
- return ::com::implementation<Impl>::create().unsafe_result();
68
+ return ::com::implementation<Impl>::create().copy_to_raw();
65
69
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "functionalscript",
3
- "version": "0.0.469",
3
+ "version": "0.0.470",
4
4
  "description": "FunctionalScript is a functional subset of JavaScript",
5
5
  "main": "module.f.cjs",
6
6
  "scripts": {
@@ -30,7 +30,7 @@
30
30
  },
31
31
  "homepage": "https://github.com/functionalscript/functionalscript#readme",
32
32
  "devDependencies": {
33
- "@types/node": "^18.11.2",
33
+ "@types/node": "^18.11.9",
34
34
  "typescript": "^4.8.4"
35
35
  }
36
36
  }