corba.js 0.1.4 → 0.1.7

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/README.md CHANGED
@@ -229,23 +229,22 @@ and deserialize them based on their IDL description:
229
229
  For each valuetype the IDL compiler will generate three variants
230
230
 
231
231
  * _value.ts: the valuetypes as TypeScript interfaces with only the attributes
232
- * _valuetype.ts: the values types as TypeScript interface with attributes and methods
233
- * _valueimpl.ts: the value types as classes with constructors, which can be initialized from JSON data
234
-
235
- The last one will be used by the ORB to convert the received JSON data
236
- into an object.
232
+ * _valuetype.ts: the valuestypes as TypeScript interface with attributes and methods
237
233
 
238
234
  Unlike a pure JSON exchange, corba.js will also instantiate a user specified
239
235
  class to represent the transmitted data, so that the object has methods we
240
236
  can call.
241
237
 
242
- Ie. the Rectangle class contains a method which needs to be implemented to
243
- be able to instantiate it:
238
+ Ie. for this the Rectangle class will need a special constructor:
244
239
 
245
240
  ```javascript
246
- export class Rectangle extends valueimpl.Rectangle {
247
- constructor(rectangle: Partial<valueimpl.Rectangle>) {
248
- super(rectangle)
241
+ import { GIOPDecoder } from "corba.js"
242
+ import * as value from "../myapp_value"
243
+ import * as valuetype from "../myapp_valuetype"
244
+
245
+ export class Rectangle implements valuetype.Rectangle {
246
+ constructor(init?: Partial<value.Rectangle> | GIOPDecoder) {
247
+ value.initRectangle(this, init)
249
248
  }
250
249
  contains(p: Point): boolean {
251
250
  return this.origin.x <= p.x && p.x <= this.origin.x + this.size.width &&
@@ -254,12 +253,12 @@ be able to instantiate it:
254
253
  }
255
254
  ```
256
255
 
257
- After the valuetypes have been registered in the ORB on client and server
256
+ After the valuetype implementations have been registered in the ORB on client and server
258
257
  side
259
258
 
260
259
  ```javascript
261
- ORB.registerValueType("Point", valueimpl.Point)
262
- ORB.registerValueType("Size", valueimpl.Size)
260
+ ORB.registerValueType("Point", Point)
261
+ ORB.registerValueType("Size", Size)
263
262
  ORB.registerValueType("Rectangle", Rectangle)
264
263
  ```
265
264
 
package/lib/idl/idl.cjs CHANGED
@@ -103,15 +103,16 @@ var Type;
103
103
  Type[Type["SYN_DECLARATORS"] = 76] = "SYN_DECLARATORS";
104
104
  Type[Type["SYN_MEMBER_LIST"] = 77] = "SYN_MEMBER_LIST";
105
105
  Type[Type["SYN_MEMBER"] = 78] = "SYN_MEMBER";
106
- Type[Type["SYN_OPERATION_DECLARATION"] = 79] = "SYN_OPERATION_DECLARATION";
107
- Type[Type["SYN_PARAMETER_DECLARATIONS"] = 80] = "SYN_PARAMETER_DECLARATIONS";
108
- Type[Type["SYN_PARAMETER_DECLARATION"] = 81] = "SYN_PARAMETER_DECLARATION";
109
- Type[Type["SYN_TYPENAME"] = 82] = "SYN_TYPENAME";
110
- Type[Type["SYN_LONGLONG"] = 83] = "SYN_LONGLONG";
111
- Type[Type["SYN_UNSIGNED_SHORT"] = 84] = "SYN_UNSIGNED_SHORT";
112
- Type[Type["SYN_UNSIGNED_LONG"] = 85] = "SYN_UNSIGNED_LONG";
113
- Type[Type["SYN_UNSIGNED_LONGLONG"] = 86] = "SYN_UNSIGNED_LONGLONG";
114
- Type[Type["SYN_LONG_DOUBLE"] = 87] = "SYN_LONG_DOUBLE";
106
+ Type[Type["SYN_SWITCH_BODY"] = 79] = "SYN_SWITCH_BODY";
107
+ Type[Type["SYN_OPERATION_DECLARATION"] = 80] = "SYN_OPERATION_DECLARATION";
108
+ Type[Type["SYN_PARAMETER_DECLARATIONS"] = 81] = "SYN_PARAMETER_DECLARATIONS";
109
+ Type[Type["SYN_PARAMETER_DECLARATION"] = 82] = "SYN_PARAMETER_DECLARATION";
110
+ Type[Type["SYN_TYPENAME"] = 83] = "SYN_TYPENAME";
111
+ Type[Type["SYN_LONGLONG"] = 84] = "SYN_LONGLONG";
112
+ Type[Type["SYN_UNSIGNED_SHORT"] = 85] = "SYN_UNSIGNED_SHORT";
113
+ Type[Type["SYN_UNSIGNED_LONG"] = 86] = "SYN_UNSIGNED_LONG";
114
+ Type[Type["SYN_UNSIGNED_LONGLONG"] = 87] = "SYN_UNSIGNED_LONGLONG";
115
+ Type[Type["SYN_LONG_DOUBLE"] = 88] = "SYN_LONG_DOUBLE";
115
116
  })(Type || (Type = {}));
116
117
  class Node {
117
118
  constructor(type, text) {
@@ -200,6 +201,7 @@ class Node {
200
201
  case Type.SYN_DECLARATORS: return "SYN_DECLARATORS";
201
202
  case Type.SYN_MEMBER_LIST: return "SYN_MEMBER_LIST";
202
203
  case Type.SYN_MEMBER: return "SYN_MEMBER";
204
+ case Type.SYN_SWITCH_BODY: return "SYN_SWITCH_BODY";
203
205
  case Type.SYN_OPERATION_DECLARATION: return "SYN_OPERATION_DECLARATION";
204
206
  case Type.SYN_PARAMETER_DECLARATIONS: return "SYN_PARAMETER_DECLARATIONS";
205
207
  case Type.SYN_PARAMETER_DECLARATION: return "SYN_PARAMETER_DECLARATION";
@@ -799,6 +801,9 @@ function type_dcl() {
799
801
  }
800
802
  lexer.unlex(t0);
801
803
  t0 = struct_type();
804
+ if (t0 !== undefined)
805
+ return t0;
806
+ t0 = union_type();
802
807
  if (t0 !== undefined)
803
808
  return t0;
804
809
  t0 = enum_type();
@@ -887,6 +892,8 @@ function template_type_spec() {
887
892
  }
888
893
  function constr_type_spec() {
889
894
  let t0 = struct_type();
895
+ if (t0 === undefined)
896
+ t0 = union_type();
890
897
  if (t0 === undefined)
891
898
  t0 = enum_type();
892
899
  return t0;
@@ -1101,7 +1108,7 @@ function struct_type() {
1101
1108
  expect('}');
1102
1109
  t2.type = t0.type;
1103
1110
  t2.text = t1.text;
1104
- scoper.addType(t2.text, t0);
1111
+ scoper.addType(t2.text, t2);
1105
1112
  return t2;
1106
1113
  }
1107
1114
  function member_list() {
@@ -1131,6 +1138,110 @@ function member() {
1131
1138
  node.append(t1);
1132
1139
  return node;
1133
1140
  }
1141
+ function union_type() {
1142
+ let t0 = lexer.lex();
1143
+ if (t0 === undefined)
1144
+ return undefined;
1145
+ if (t0.type !== Type.TKN_UNION) {
1146
+ lexer.unlex(t0);
1147
+ return undefined;
1148
+ }
1149
+ let t1 = identifier();
1150
+ if (t1 === undefined)
1151
+ throw Error(`expected identifier after 'union'`);
1152
+ let t2 = lexer.lex();
1153
+ if (t2 === undefined || t2.type !== Type.TKN_SWITCH) {
1154
+ throw Error(`expected 'switch' after union <identifier>`);
1155
+ }
1156
+ expect("(");
1157
+ let t3 = switch_type_spec();
1158
+ if (t3 === undefined) {
1159
+ throw Error(`expected switch type specifier after union <identifier> switch(`);
1160
+ }
1161
+ expect(")");
1162
+ expect("{");
1163
+ let t4 = switch_body();
1164
+ expect("}");
1165
+ t0.text = t1.text;
1166
+ t0.append(t3);
1167
+ t0.append(t4);
1168
+ scoper.addType(t0.text, t0);
1169
+ return t0;
1170
+ }
1171
+ function switch_type_spec() {
1172
+ let t0 = integer_type();
1173
+ if (t0 !== undefined)
1174
+ return t0;
1175
+ t0 = char_type();
1176
+ if (t0 !== undefined)
1177
+ return t0;
1178
+ t0 = boolean_type();
1179
+ if (t0 !== undefined)
1180
+ return t0;
1181
+ t0 = enum_type();
1182
+ if (t0 !== undefined)
1183
+ return t0;
1184
+ return scoped_name();
1185
+ }
1186
+ function switch_body() {
1187
+ let t0 = new Node(Type.SYN_SWITCH_BODY);
1188
+ while (true) {
1189
+ const t1 = _case();
1190
+ if (t1 === undefined)
1191
+ break;
1192
+ t0.append(t1);
1193
+ }
1194
+ return t0;
1195
+ }
1196
+ function _case() {
1197
+ let t0 = case_label();
1198
+ if (t0 === undefined)
1199
+ return t0;
1200
+ let t1 = element_spec();
1201
+ if (t1 === undefined)
1202
+ throw Error(`expected element specification after '${t0}'`);
1203
+ t0.append(t1.child[0]);
1204
+ t0.append(t1.child[1]);
1205
+ expect(";");
1206
+ return t0;
1207
+ }
1208
+ function case_label() {
1209
+ let t0 = lexer.lex();
1210
+ if (t0 === undefined)
1211
+ return undefined;
1212
+ if (t0.type === Type.TKN_CASE) {
1213
+ let t1 = identifier();
1214
+ if (t1 === undefined) {
1215
+ throw Error(`expected enum identifier after 'case'`);
1216
+ }
1217
+ const t = scoper.getType(t1.text);
1218
+ if (t?.typeParent?.type === Type.TKN_ENUM) ;
1219
+ else {
1220
+ throw Error(`unsupported value after 'case'`);
1221
+ }
1222
+ expect(":");
1223
+ t0.append(t);
1224
+ return t0;
1225
+ }
1226
+ if (t0.type === Type.TKN_DEFAULT) {
1227
+ expect(":");
1228
+ return t0;
1229
+ }
1230
+ lexer.unlex(t0);
1231
+ return undefined;
1232
+ }
1233
+ function element_spec() {
1234
+ let t0 = type_spec();
1235
+ if (t0 === undefined)
1236
+ return undefined;
1237
+ let t1 = declarator();
1238
+ if (t1 === undefined)
1239
+ throw Error(`expected declarator after ${t0}`);
1240
+ let t2 = new Node(Type.NONE);
1241
+ t2.append(t0);
1242
+ t2.append(t1);
1243
+ return t2;
1244
+ }
1134
1245
  function enum_type() {
1135
1246
  let t0 = lexer.lex();
1136
1247
  if (t0 !== undefined && t0.type == Type.TKN_ENUM) {
@@ -1139,18 +1250,20 @@ function enum_type() {
1139
1250
  throw Error("expected identifier after 'enum'");
1140
1251
  t0.text = t1.text;
1141
1252
  expect("{");
1142
- const set = new Set();
1253
+ const usedNames = new Set();
1143
1254
  while (true) {
1144
1255
  let t2 = lexer.lex();
1145
1256
  if (t2 === undefined)
1146
1257
  throw Error("unexpected end of file");
1147
1258
  if (t2.type !== Type.TKN_IDENTIFIER)
1148
1259
  throw Error("expected identifier but got ${t2}");
1149
- if (set.has(t2.text)) {
1260
+ if (usedNames.has(t2.text)) {
1150
1261
  throw Error(`Declaration of enumerator '${t2.text}' clashes with earlier declaration of enumerator '${t2.text}'`);
1151
1262
  }
1152
- set.add(t2.text);
1263
+ usedNames.add(t2.text);
1264
+ scoper.addType(t2.text, t2);
1153
1265
  t0.append(t2);
1266
+ t2.typeParent = t0;
1154
1267
  let t3 = lexer.lex();
1155
1268
  if (t3 === undefined)
1156
1269
  throw Error("unexpected end of file");
@@ -1238,6 +1351,9 @@ function op_dcl() {
1238
1351
  const t3 = parameter_dcls();
1239
1352
  if (t3 === undefined) {
1240
1353
  lexer.unlex(t2);
1354
+ if (t1.child.length != 0) {
1355
+ throw Error("expected parameter declaration after " + t2.toString());
1356
+ }
1241
1357
  lexer.unlex(t1);
1242
1358
  lexer.unlex(t0);
1243
1359
  return undefined;
@@ -1609,6 +1725,7 @@ function typeIDLtoTS(type, filetype = FileType.NONE) {
1609
1725
  break;
1610
1726
  case Type.SYN_INTERFACE:
1611
1727
  case Type.TKN_ENUM:
1728
+ case Type.TKN_UNION:
1612
1729
  if (filetype !== FileType.INTERFACE)
1613
1730
  name = `_interface${absolutePrefix}.${relativeName}`;
1614
1731
  else
@@ -1655,29 +1772,37 @@ function typeIDLtoTS(type, filetype = FileType.NONE) {
1655
1772
  throw Error(`no mapping from IDL type to TS type for ${type.toString()}`);
1656
1773
  }
1657
1774
  }
1658
- function typeIDLtoGIOP(type, arg = undefined) {
1775
+ function typeIDLtoGIOP(type, arg = undefined, filetype = FileType.NONE) {
1659
1776
  if (type === undefined)
1660
1777
  throw Error("internal error: parser delivered no type information");
1661
1778
  let name;
1662
1779
  switch (type.type) {
1663
1780
  case Type.TKN_SEQUENCE:
1664
1781
  return arg === undefined ?
1665
- `decoder.sequence(() => ${typeIDLtoGIOP(type.child[0])})` :
1666
- `encoder.sequence(${arg}, (item) => ${typeIDLtoGIOP(type.child[0], "item")})`;
1782
+ `decoder.sequence(() => ${typeIDLtoGIOP(type.child[0], undefined, filetype)})` :
1783
+ `encoder.sequence(${arg}, (item) => ${typeIDLtoGIOP(type.child[0], "item", filetype)})`;
1667
1784
  case Type.TKN_IDENTIFIER:
1668
1785
  case Type.TKN_MODULE:
1669
- return typeIDLtoGIOP(type.child[0], arg);
1786
+ return typeIDLtoGIOP(type.child[0], arg, filetype);
1670
1787
  case Type.TKN_VALUETYPE:
1671
1788
  return arg === undefined ? `decoder.value("${type.text}")` : `encoder.value(${arg})`;
1672
1789
  case Type.SYN_INTERFACE:
1673
- case Type.TKN_STRUCT:
1674
1790
  name = "object";
1675
1791
  break;
1792
+ case Type.TKN_UNION:
1793
+ case Type.TKN_STRUCT: {
1794
+ const prefix = filetype === FileType.INTERFACE ? "" : "_interface.";
1795
+ return arg === undefined ?
1796
+ `${prefix}decode${type.text}(decoder)` :
1797
+ `${prefix}encode${type.text}(encoder,${arg})`;
1798
+ }
1676
1799
  case Type.TKN_NATIVE:
1677
1800
  {
1678
1801
  const id = type.child[0].text;
1679
1802
  if (id.length > 4 && id.substring(id.length - 4) === "_ptr") {
1680
- return arg === undefined ? `decoder.value("${id.substring(0, id.length - 4)}")` : `encoder.value(${arg})`;
1803
+ return arg === undefined ?
1804
+ `decoder.value("${id.substring(0, id.length - 4)}")` :
1805
+ `encoder.value(${arg})`;
1681
1806
  }
1682
1807
  else {
1683
1808
  return `undefined`;
@@ -1789,6 +1914,7 @@ function hasNative(specification) {
1789
1914
  function writeTSInterface(specification) {
1790
1915
  let out = fs__namespace.createWriteStream(filenamePrefix + ".ts");
1791
1916
  out.write("// This file is generated by the corba.js IDL compiler from '" + filename + "'.\n\n");
1917
+ out.write(`import { ORB, GIOPDecoder, GIOPEncoder } from 'corba.js'\n`);
1792
1918
  if (hasValueType(specification)) {
1793
1919
  out.write("import * as valuetype from \"./" + filenameLocal + "_valuetype\"\n\n");
1794
1920
  }
@@ -1859,20 +1985,120 @@ function writeTSInterfaceDefinitions(out, specification, prefix = "", indent = 0
1859
1985
  break;
1860
1986
  case Type.TKN_STRUCT:
1861
1987
  {
1862
- const identifier = definition.text;
1988
+ let struct_type = definition;
1989
+ let identifier = struct_type.text;
1863
1990
  writeIndent(out, indent);
1864
1991
  out.write(`export interface ${identifier} {\n`);
1865
- const member_list = definition.child;
1866
- for (const member of member_list) {
1867
- const type_spec = member.child[0];
1868
- const declarators = member.child[1];
1869
- for (const declarator of declarators.child) {
1870
- writeIndent(out, indent + 1);
1871
- out.write(`${declarator.text}: ${typeIDLtoTS(type_spec, FileType.INTERFACE)}\n`);
1992
+ for (let i = 0; i < struct_type.child.length; ++i) {
1993
+ const member = struct_type.child[i];
1994
+ if (member.type === Type.SYN_MEMBER) {
1995
+ let type = member.child[0];
1996
+ let declarators = member.child[1];
1997
+ for (let declarator of declarators.child) {
1998
+ writeIndent(out, indent + 1);
1999
+ out.write(declarator.text + ": " + typeIDLtoTS(type, FileType.VALUE) + "\n");
2000
+ }
1872
2001
  }
1873
2002
  }
1874
2003
  writeIndent(out, indent);
1875
- out.write("}\n\n");
2004
+ out.write("}\n");
2005
+ out.write(`export function decode${identifier}(decoder: GIOPDecoder): ${identifier} {\n`);
2006
+ out.write(` return {\n`);
2007
+ for (let i = 0; i < struct_type.child.length; ++i) {
2008
+ const member = struct_type.child[i];
2009
+ if (member.type === Type.SYN_MEMBER) {
2010
+ let type = member.child[0];
2011
+ let declarators = member.child[1];
2012
+ for (let declarator of declarators.child) {
2013
+ writeIndent(out, indent + 2);
2014
+ out.write(declarator.text + ": " + typeIDLtoGIOP(type) + ",\n");
2015
+ }
2016
+ }
2017
+ }
2018
+ out.write(` }\n`);
2019
+ out.write(`}\n`);
2020
+ out.write(`export function encode${identifier}(encoder: GIOPEncoder, obj: ${identifier}) {\n`);
2021
+ for (let i = 0; i < struct_type.child.length; ++i) {
2022
+ const member = struct_type.child[i];
2023
+ if (member.type === Type.SYN_MEMBER) {
2024
+ let type = member.child[0];
2025
+ let declarators = member.child[1];
2026
+ for (let declarator of declarators.child) {
2027
+ writeIndent(out, indent + 1);
2028
+ out.write(typeIDLtoGIOP(type, `obj.${declarator.text}`) + "\n");
2029
+ }
2030
+ }
2031
+ }
2032
+ out.write(`}\n\n`);
2033
+ }
2034
+ break;
2035
+ case Type.TKN_UNION:
2036
+ {
2037
+ const identifier = definition.text;
2038
+ const union_type = definition;
2039
+ const switch_body = union_type.child[1];
2040
+ let composite = "";
2041
+ switch_body.child.forEach(x => {
2042
+ const case_label = x.child[0];
2043
+ const type_spec = x.child[1];
2044
+ const declarator = x.child[2];
2045
+ writeIndent(out, indent);
2046
+ out.write(`export type _${identifier}_${declarator.text} = {\n`);
2047
+ writeIndent(out, indent + 1);
2048
+ out.write(`type: ${case_label.typeParent.text}.${case_label.text}\n`);
2049
+ writeIndent(out, indent + 1);
2050
+ out.write(`${declarator.text}: ${typeIDLtoTS(type_spec, FileType.INTERFACE)}\n`);
2051
+ writeIndent(out, indent);
2052
+ out.write(`}\n`);
2053
+ if (composite.length !== 0)
2054
+ composite = `${composite} | `;
2055
+ composite = `${composite}_${identifier}_${declarator.text}`;
2056
+ });
2057
+ writeIndent(out, indent);
2058
+ out.write(`export type ${identifier} = ${composite}\n`);
2059
+ writeIndent(out, indent);
2060
+ out.write(`export function decode${identifier}(decoder: GIOPDecoder): ${identifier} {\n`);
2061
+ writeIndent(out, indent + 1);
2062
+ out.write(`switch(decoder.ulong() as ${union_type.child[0].text}) {\n`);
2063
+ switch_body.child.forEach(x => {
2064
+ const case_label = x.child[0];
2065
+ const type_spec = x.child[1];
2066
+ const declarator = x.child[2];
2067
+ writeIndent(out, indent + 2);
2068
+ out.write(`case ${case_label.typeParent.text}.${case_label.text}: return {\n`);
2069
+ writeIndent(out, indent + 3);
2070
+ out.write(`type: ${case_label.typeParent.text}.${case_label.text},\n`);
2071
+ writeIndent(out, indent + 3);
2072
+ out.write(`${declarator.text}: ${typeIDLtoGIOP(type_spec, undefined, FileType.INTERFACE)}\n`);
2073
+ writeIndent(out, indent + 2);
2074
+ out.write(`}\n`);
2075
+ });
2076
+ writeIndent(out, indent + 1);
2077
+ out.write(`}\n`);
2078
+ writeIndent(out, indent + 1);
2079
+ out.write(`throw Error("invalid union value")\n`);
2080
+ writeIndent(out, indent);
2081
+ out.write(`}\n`);
2082
+ writeIndent(out, indent);
2083
+ out.write(`export function encode${identifier}(encoder: GIOPEncoder, obj: ${identifier}) {\n`);
2084
+ writeIndent(out, indent + 1);
2085
+ out.write(`encoder.ulong(obj.type)\n`);
2086
+ writeIndent(out, indent + 1);
2087
+ out.write(`switch(obj.type) {\n`);
2088
+ switch_body.child.forEach(x => {
2089
+ const case_label = x.child[0];
2090
+ const type_spec = x.child[1];
2091
+ const declarator = x.child[2];
2092
+ writeIndent(out, indent + 2);
2093
+ out.write(`case ${case_label.typeParent.text}.${case_label.text}:\n`);
2094
+ writeIndent(out, indent + 3);
2095
+ out.write(typeIDLtoGIOP(type_spec, `obj.${declarator.text}`, FileType.INTERFACE) + "\n");
2096
+ writeIndent(out, indent + 3);
2097
+ out.write(`break\n`);
2098
+ });
2099
+ writeIndent(out, indent + 1);
2100
+ out.write(`}\n`);
2101
+ out.write(`}\n\n`);
1876
2102
  }
1877
2103
  break;
1878
2104
  case Type.TKN_ENUM:
@@ -2120,6 +2346,7 @@ function writeTSValue(specification) {
2120
2346
  let out = fs__namespace.createWriteStream(filenamePrefix + "_value.ts");
2121
2347
  out.write(`// This file is generated by the corba.js IDL compiler from '${filename}'.\n\n`);
2122
2348
  out.write(`import { ORB, GIOPDecoder, GIOPEncoder } from 'corba.js'\n`);
2349
+ out.write("import * as _interface from \"./" + filenameLocal + "\"\n\n");
2123
2350
  if (!hasValueType(specification)) {
2124
2351
  out.write("// no valuetype's defined in IDL");
2125
2352
  return;