circuitscript 0.1.5 → 0.1.8

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.
Files changed (97) hide show
  1. package/dist/cjs/BaseVisitor.js +127 -73
  2. package/dist/cjs/SemanticTokenVisitor.js +19 -13
  3. package/dist/cjs/antlr/CircuitScriptParser.js +711 -671
  4. package/dist/cjs/builtinMethods.js +29 -25
  5. package/dist/cjs/environment.js +118 -0
  6. package/dist/cjs/execute.js +53 -12
  7. package/dist/cjs/export.js +0 -5
  8. package/dist/cjs/geometry.js +1 -0
  9. package/dist/cjs/globals.js +11 -6
  10. package/dist/cjs/helpers.js +152 -127
  11. package/dist/cjs/index.js +5 -0
  12. package/dist/cjs/layout.js +86 -44
  13. package/dist/cjs/main.js +31 -19
  14. package/dist/cjs/objects/ExecutionScope.js +33 -0
  15. package/dist/cjs/objects/ParamDefinition.js +15 -15
  16. package/dist/cjs/parser.js +27 -21
  17. package/dist/cjs/regenerate-tests.js +14 -10
  18. package/dist/cjs/render.js +3 -1
  19. package/dist/cjs/sizing.js +5 -58
  20. package/dist/cjs/utils.js +85 -30
  21. package/dist/cjs/validate/SymbolTable.js +96 -0
  22. package/dist/cjs/validate/SymbolValidatorResolveVisitor.js +14 -0
  23. package/dist/cjs/validate/SymbolValidatorVisitor.js +170 -0
  24. package/dist/cjs/validate.js +71 -44
  25. package/dist/cjs/visitor.js +140 -24
  26. package/dist/esm/{BaseVisitor.mjs → BaseVisitor.js} +98 -45
  27. package/dist/esm/{SemanticTokenVisitor.mjs → SemanticTokenVisitor.js} +17 -11
  28. package/dist/esm/antlr/{CircuitScriptParser.mjs → CircuitScriptParser.js} +711 -671
  29. package/dist/esm/{builtinMethods.mjs → builtinMethods.js} +20 -16
  30. package/dist/esm/{draw_symbols.mjs → draw_symbols.js} +7 -7
  31. package/dist/esm/environment.js +110 -0
  32. package/dist/esm/{execute.mjs → execute.js} +66 -25
  33. package/dist/esm/{export.mjs → export.js} +2 -7
  34. package/dist/esm/{geometry.mjs → geometry.js} +6 -5
  35. package/dist/esm/{globals.mjs → globals.js} +6 -1
  36. package/dist/esm/helpers.js +394 -0
  37. package/dist/esm/index.js +20 -0
  38. package/dist/esm/{layout.mjs → layout.js} +72 -53
  39. package/dist/esm/{lexer.mjs → lexer.js} +2 -2
  40. package/dist/esm/{main.mjs → main.js} +33 -21
  41. package/dist/esm/objects/{ClassComponent.mjs → ClassComponent.js} +5 -4
  42. package/dist/esm/objects/{ExecutionScope.mjs → ExecutionScope.js} +33 -0
  43. package/dist/esm/objects/{Frame.mjs → Frame.js} +1 -1
  44. package/dist/esm/objects/{ParamDefinition.mjs → ParamDefinition.js} +1 -1
  45. package/dist/esm/objects/{PinDefinition.mjs → PinDefinition.js} +1 -1
  46. package/dist/esm/parser.js +71 -0
  47. package/dist/esm/{regenerate-tests.mjs → regenerate-tests.js} +15 -11
  48. package/dist/esm/{render.mjs → render.js} +11 -9
  49. package/dist/esm/{sizing.mjs → sizing.js} +6 -34
  50. package/dist/esm/{utils.mjs → utils.js} +61 -17
  51. package/dist/esm/validate/SymbolTable.js +90 -0
  52. package/dist/esm/validate/SymbolValidatorResolveVisitor.js +10 -0
  53. package/dist/esm/validate/SymbolValidatorVisitor.js +163 -0
  54. package/dist/esm/validate.js +105 -0
  55. package/dist/esm/{visitor.mjs → visitor.js} +151 -35
  56. package/dist/fonts/Arial.ttf +0 -0
  57. package/dist/fonts/Inter-Bold.ttf +0 -0
  58. package/dist/fonts/Inter-Regular.ttf +0 -0
  59. package/dist/fonts/OpenSans-Regular.ttf +0 -0
  60. package/dist/fonts/Roboto-Regular.ttf +0 -0
  61. package/dist/libs/lib.cst +423 -0
  62. package/dist/types/BaseVisitor.d.ts +34 -21
  63. package/dist/types/SemanticTokenVisitor.d.ts +6 -5
  64. package/dist/types/antlr/CircuitScriptParser.d.ts +4 -2
  65. package/dist/types/builtinMethods.d.ts +3 -2
  66. package/dist/types/environment.d.ts +31 -0
  67. package/dist/types/globals.d.ts +4 -1
  68. package/dist/types/helpers.d.ts +12 -14
  69. package/dist/types/index.d.ts +5 -0
  70. package/dist/types/layout.d.ts +2 -2
  71. package/dist/types/objects/ClassComponent.d.ts +1 -0
  72. package/dist/types/objects/ExecutionScope.d.ts +11 -0
  73. package/dist/types/objects/types.d.ts +6 -1
  74. package/dist/types/parser.d.ts +7 -11
  75. package/dist/types/sizing.d.ts +0 -3
  76. package/dist/types/utils.d.ts +30 -6
  77. package/dist/types/validate/SymbolTable.d.ts +40 -0
  78. package/dist/types/validate/SymbolValidatorResolveVisitor.d.ts +7 -0
  79. package/dist/types/validate/SymbolValidatorVisitor.d.ts +32 -0
  80. package/dist/types/validate.d.ts +1 -1
  81. package/package.json +15 -14
  82. package/dist/cjs/SymbolValidatorVisitor.js +0 -233
  83. package/dist/esm/SymbolValidatorVisitor.mjs +0 -222
  84. package/dist/esm/helpers.mjs +0 -364
  85. package/dist/esm/index.mjs +0 -15
  86. package/dist/esm/parser.mjs +0 -64
  87. package/dist/esm/validate.mjs +0 -74
  88. package/dist/types/SymbolValidatorVisitor.d.ts +0 -61
  89. /package/dist/esm/antlr/{CircuitScriptLexer.mjs → CircuitScriptLexer.js} +0 -0
  90. /package/dist/esm/antlr/{CircuitScriptVisitor.mjs → CircuitScriptVisitor.js} +0 -0
  91. /package/dist/esm/{fonts.mjs → fonts.js} +0 -0
  92. /package/dist/esm/{logger.mjs → logger.js} +0 -0
  93. /package/dist/esm/objects/{Net.mjs → Net.js} +0 -0
  94. /package/dist/esm/objects/{PinTypes.mjs → PinTypes.js} +0 -0
  95. /package/dist/esm/objects/{Wire.mjs → Wire.js} +0 -0
  96. /package/dist/esm/objects/{types.mjs → types.js} +0 -0
  97. /package/dist/esm/{server.mjs → server.js} +0 -0
package/dist/cjs/main.js CHANGED
@@ -6,14 +6,13 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
6
6
  Object.defineProperty(exports, "__esModule", { value: true });
7
7
  const commander_1 = require("commander");
8
8
  const figlet_1 = __importDefault(require("figlet"));
9
- const path_1 = __importDefault(require("path"));
10
9
  const fs_1 = require("fs");
11
- const sizing_js_1 = require("./sizing.js");
12
10
  const helpers_js_1 = require("./helpers.js");
11
+ const environment_js_1 = require("./environment.js");
13
12
  async function main() {
14
- const fontsPath = (0, helpers_js_1.getFontsPath)();
15
- const defaultLibsPath = (0, helpers_js_1.getDefaultLibsPath)();
16
- const version = (0, helpers_js_1.getPackageVersion)();
13
+ const env = new environment_js_1.NodeScriptEnvironment();
14
+ environment_js_1.NodeScriptEnvironment.setInstance(env);
15
+ const version = env.getPackageVersion();
17
16
  commander_1.program
18
17
  .description('generate graphical output from circuitscript files')
19
18
  .version(version)
@@ -38,11 +37,13 @@ async function main() {
38
37
  const watchFileChanges = options.watch;
39
38
  const dumpNets = options.dumpNets;
40
39
  const dumpData = options.dumpData;
41
- let currentDirectory = options.currentDirectory ?? null;
40
+ if (options.currentDirectory) {
41
+ throw "Parameter not supported yet";
42
+ }
42
43
  if (watchFileChanges) {
43
44
  console.log('watching for file changes...');
44
45
  }
45
- await (0, sizing_js_1.prepareSVGEnvironment)(fontsPath);
46
+ await env.prepareSVGEnvironment();
46
47
  let inputFilePath = "";
47
48
  if (args.length > 2) {
48
49
  console.log("Error: Extra arguments passed");
@@ -51,11 +52,8 @@ async function main() {
51
52
  let scriptData;
52
53
  if (args.length > 0 && args[0]) {
53
54
  inputFilePath = args[0];
54
- if ((0, fs_1.existsSync)(inputFilePath)) {
55
- scriptData = (0, fs_1.readFileSync)(inputFilePath, { encoding: 'utf-8' });
56
- if (currentDirectory === null) {
57
- currentDirectory = path_1.default.dirname(inputFilePath);
58
- }
55
+ if ((await env.exists(inputFilePath))) {
56
+ scriptData = await env.readFile(inputFilePath, { encoding: 'utf-8' });
59
57
  }
60
58
  else {
61
59
  console.error("Error: File could not be found");
@@ -70,29 +68,43 @@ async function main() {
70
68
  return;
71
69
  }
72
70
  const scriptOptions = {
73
- currentDirectory,
74
- defaultLibsPath,
75
71
  dumpNets,
76
72
  dumpData,
77
73
  showStats: options.stats,
74
+ environment: env,
78
75
  };
79
76
  let outputPath = null;
80
77
  if (args.length > 0 && args[1]) {
81
78
  outputPath = args[1];
82
79
  }
83
- const output = (0, helpers_js_1.renderScript)(scriptData, outputPath, scriptOptions);
80
+ const output = await parseFile(scriptData, outputPath, scriptOptions);
84
81
  if (outputPath === null && output && (options.skipOutput === undefined)) {
85
82
  console.log(output);
86
83
  }
87
84
  if (watchFileChanges) {
88
- (0, fs_1.watch)(inputFilePath, event => {
85
+ (0, fs_1.watch)(inputFilePath, async (event) => {
89
86
  if (event === 'change') {
90
- const scriptData = (0, fs_1.readFileSync)(inputFilePath, { encoding: 'utf-8' });
91
- (0, helpers_js_1.renderScript)(scriptData, outputPath, scriptOptions);
92
- console.log('done');
87
+ const scriptData = await env.readFile(inputFilePath, { encoding: 'utf-8' });
88
+ parseFile(scriptData, outputPath, scriptOptions);
93
89
  }
94
90
  });
95
91
  }
96
92
  }
97
93
  exports.default = main;
94
+ async function parseFile(scriptData, outputPath, scriptOptions) {
95
+ try {
96
+ const { svgOutput: output, errors } = await (0, helpers_js_1.renderScript)(scriptData, outputPath, scriptOptions);
97
+ errors.forEach((err, index) => {
98
+ console.log(`[${index}] ${err}`);
99
+ });
100
+ if (errors.length > 0) {
101
+ console.log('Render failed due to syntax or parsing errors');
102
+ }
103
+ return output;
104
+ }
105
+ catch (error) {
106
+ console.error(`Unexpected Error: ${error}`);
107
+ }
108
+ return null;
109
+ }
98
110
  main();
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.ActiveObject = exports.FrameAction = exports.SequenceAction = exports.ExecutionScope = void 0;
4
+ const CircuitScriptParser_js_1 = require("../antlr/CircuitScriptParser.js");
4
5
  class ExecutionScope {
5
6
  constructor(scopeId) {
6
7
  this.nets = [];
@@ -9,6 +10,8 @@ class ExecutionScope {
9
10
  this.variables = new Map();
10
11
  this.symbols = new Map();
11
12
  this.blockStack = new Map();
13
+ this.contextStack = [];
14
+ this.onPropertyHandler = [];
12
15
  this.breakStack = [];
13
16
  this.wires = [];
14
17
  this.frames = [];
@@ -123,6 +126,36 @@ class ExecutionScope {
123
126
  this.currentPin = null;
124
127
  }
125
128
  }
129
+ enterContext(context) {
130
+ this.contextStack.push(context);
131
+ }
132
+ exitContext() {
133
+ return this.contextStack.pop();
134
+ }
135
+ findPropertyKeyTree() {
136
+ const keyNames = [];
137
+ for (let i = this.contextStack.length - 1; i >= 0; i--) {
138
+ const ctx = this.contextStack[i];
139
+ if (ctx instanceof CircuitScriptParser_js_1.Property_key_exprContext) {
140
+ keyNames.push([ctx, ctx.getText()]);
141
+ }
142
+ else if (typeof ctx === 'number') {
143
+ keyNames.push(['index', ctx]);
144
+ }
145
+ }
146
+ return keyNames.reverse();
147
+ }
148
+ setOnPropertyHandler(handler) {
149
+ this.onPropertyHandler.push(handler);
150
+ }
151
+ popOnPropertyHandler() {
152
+ return this.onPropertyHandler.pop();
153
+ }
154
+ triggerPropertyHandler(value, valueCtx) {
155
+ const lastHandler = this.onPropertyHandler[this.onPropertyHandler.length - 1];
156
+ const propertyTree = this.findPropertyKeyTree();
157
+ lastHandler && lastHandler(propertyTree, value, valueCtx);
158
+ }
126
159
  }
127
160
  exports.ExecutionScope = ExecutionScope;
128
161
  ExecutionScope.scopeId = 0;
@@ -1,7 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.NumberOperator = exports.WrappedNumber = exports.PercentageValue = exports.numeric = exports.NumericValue = exports.ParamDefinition = void 0;
4
- const utils_1 = require("../utils");
4
+ const utils_js_1 = require("../utils.js");
5
5
  const big_js_1 = require("big.js");
6
6
  class ParamDefinition {
7
7
  constructor(paramName, paramValue) {
@@ -17,7 +17,7 @@ class NumericValue {
17
17
  const matches = value.match(/^([\d]+(?:.[\d]+)?)([\w]*)$/);
18
18
  if (matches) {
19
19
  this.valuePart = new big_js_1.Big(matches[1]);
20
- this.prefixPart = (0, utils_1.getNumberExponential)(matches[2]);
20
+ this.prefixPart = (0, utils_js_1.getNumberExponential)(matches[2]);
21
21
  }
22
22
  else {
23
23
  throw new Error("Invalid numeric value: " + value);
@@ -27,7 +27,7 @@ class NumericValue {
27
27
  this.valuePart = new big_js_1.Big(value);
28
28
  this.prefixPart = prefix;
29
29
  this.value = this.valuePart.toString()
30
- + (0, utils_1.getNumberExponentialText)(prefix);
30
+ + (0, utils_js_1.getNumberExponentialText)(prefix);
31
31
  }
32
32
  }
33
33
  toString() {
@@ -39,7 +39,7 @@ class NumericValue {
39
39
  }
40
40
  else {
41
41
  return this.valuePart.toString()
42
- + (0, utils_1.getNumberExponentialText)(this.prefixPart);
42
+ + (0, utils_js_1.getNumberExponentialText)(this.prefixPart);
43
43
  }
44
44
  }
45
45
  toNumber() {
@@ -52,7 +52,7 @@ class NumericValue {
52
52
  if (typeof value === 'number') {
53
53
  value = numeric(value);
54
54
  }
55
- return (0, utils_1.resolveToNumericValue)(this.toBigNumber().div(value.toBigNumber()));
55
+ return (0, utils_js_1.resolveToNumericValue)(this.toBigNumber().div(value.toBigNumber()));
56
56
  }
57
57
  half() {
58
58
  return this.div(2);
@@ -61,28 +61,28 @@ class NumericValue {
61
61
  if (typeof value === 'number') {
62
62
  value = numeric(value);
63
63
  }
64
- return (0, utils_1.resolveToNumericValue)(this.toBigNumber().mul(value.toBigNumber()));
64
+ return (0, utils_js_1.resolveToNumericValue)(this.toBigNumber().mul(value.toBigNumber()));
65
65
  }
66
66
  add(value) {
67
67
  if (typeof value === 'number') {
68
68
  value = numeric(value);
69
69
  }
70
- return (0, utils_1.resolveToNumericValue)(this.toBigNumber().add(value.toBigNumber()));
70
+ return (0, utils_js_1.resolveToNumericValue)(this.toBigNumber().add(value.toBigNumber()));
71
71
  }
72
72
  sub(value) {
73
73
  if (typeof value === 'number') {
74
74
  value = numeric(value);
75
75
  }
76
- return (0, utils_1.resolveToNumericValue)(this.toBigNumber().sub(value.toBigNumber()));
76
+ return (0, utils_js_1.resolveToNumericValue)(this.toBigNumber().sub(value.toBigNumber()));
77
77
  }
78
78
  mod(value) {
79
79
  if (typeof value === 'number') {
80
80
  value = numeric(value);
81
81
  }
82
- return (0, utils_1.resolveToNumericValue)(this.toBigNumber().mod(value.toBigNumber()));
82
+ return (0, utils_js_1.resolveToNumericValue)(this.toBigNumber().mod(value.toBigNumber()));
83
83
  }
84
84
  neg() {
85
- return (0, utils_1.resolveToNumericValue)(this.toBigNumber().neg());
85
+ return (0, utils_js_1.resolveToNumericValue)(this.toBigNumber().neg());
86
86
  }
87
87
  eq(value) {
88
88
  return this.toBigNumber().eq(value.toBigNumber());
@@ -129,27 +129,27 @@ class NumberOperator {
129
129
  multiply(value1, value2) {
130
130
  const big1 = new big_js_1.Big(value1.toNumber());
131
131
  const big2 = new big_js_1.Big(value2.toNumber());
132
- return (0, utils_1.resolveToNumericValue)(big1.mul(big2));
132
+ return (0, utils_js_1.resolveToNumericValue)(big1.mul(big2));
133
133
  }
134
134
  divide(value1, value2) {
135
135
  const big1 = new big_js_1.Big(value1.toNumber());
136
136
  const big2 = new big_js_1.Big(value2.toNumber());
137
- return (0, utils_1.resolveToNumericValue)(big1.div(big2));
137
+ return (0, utils_js_1.resolveToNumericValue)(big1.div(big2));
138
138
  }
139
139
  addition(value1, value2) {
140
140
  const big1 = new big_js_1.Big(value1.toNumber());
141
141
  const big2 = new big_js_1.Big(value2.toNumber());
142
- return (0, utils_1.resolveToNumericValue)(big1.add(big2));
142
+ return (0, utils_js_1.resolveToNumericValue)(big1.add(big2));
143
143
  }
144
144
  subtraction(value1, value2) {
145
145
  const big1 = new big_js_1.Big(value1.toNumber());
146
146
  const big2 = new big_js_1.Big(value2.toNumber());
147
- return (0, utils_1.resolveToNumericValue)(big1.sub(big2));
147
+ return (0, utils_js_1.resolveToNumericValue)(big1.sub(big2));
148
148
  }
149
149
  modulus(value1, value2) {
150
150
  const big1 = new big_js_1.Big(value1.toNumber());
151
151
  const big2 = new big_js_1.Big(value2.toNumber());
152
- return (0, utils_1.resolveToNumericValue)(big1.mod(big2));
152
+ return (0, utils_js_1.resolveToNumericValue)(big1.mod(big2));
153
153
  }
154
154
  }
155
155
  exports.NumberOperator = NumberOperator;
@@ -1,59 +1,65 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.CircuitscriptParserErrorListener = exports.TempErrorStrategy = exports.parseFileWithVisitor = void 0;
3
+ exports.CircuitscriptParserErrorListener = exports.parseFileWithVisitor = void 0;
4
4
  const CircuitScriptParser_js_1 = require("./antlr/CircuitScriptParser.js");
5
5
  const lexer_js_1 = require("./lexer.js");
6
6
  const utils_js_1 = require("./utils.js");
7
7
  const antlr4ng_1 = require("antlr4ng");
8
- function parseFileWithVisitor(visitor, data) {
8
+ async function parseFileWithVisitor(visitor, data) {
9
+ const lexerErrorListener = new CircuitscriptParserErrorListener(visitor.onErrorHandler);
10
+ const parserErrorListener = new CircuitscriptParserErrorListener(visitor.onErrorHandler);
9
11
  const chars = antlr4ng_1.CharStream.fromString(data);
10
12
  const lexer = new lexer_js_1.MainLexer(chars);
13
+ lexer.removeErrorListeners();
14
+ lexer.addErrorListener(lexerErrorListener);
11
15
  const lexerTimer = new utils_js_1.SimpleStopwatch();
12
16
  const tokens = new antlr4ng_1.CommonTokenStream(lexer);
13
17
  tokens.fill();
14
18
  const lexerTimeTaken = lexerTimer.lap();
15
19
  const parserTimer = new utils_js_1.SimpleStopwatch();
16
20
  const parser = new CircuitScriptParser_js_1.CircuitScriptParser(tokens);
21
+ parser.removeErrorListeners();
22
+ parser.addErrorListener(parserErrorListener);
17
23
  const tree = parser.script();
18
- let hasError = false;
19
24
  try {
20
- visitor.visit(tree);
25
+ await visitor.visitAsync(tree);
21
26
  }
22
- catch (err) {
23
- console.log(err);
24
- hasError = true;
27
+ catch (error) {
28
+ if (visitor.onErrorHandler) {
29
+ if (error instanceof utils_js_1.RuntimeExecutionError) {
30
+ visitor.onErrorHandler(error.message, null, error);
31
+ }
32
+ else {
33
+ throw error;
34
+ }
35
+ }
25
36
  }
26
37
  const parserTimeTaken = parserTimer.lap();
27
38
  return {
28
39
  tree, parser,
29
40
  hasParseError: false,
30
- hasError,
41
+ hasError: false,
31
42
  parserTimeTaken,
32
43
  lexerTimeTaken,
33
44
  };
34
45
  }
35
46
  exports.parseFileWithVisitor = parseFileWithVisitor;
36
- class TempErrorStrategy extends antlr4ng_1.DefaultErrorStrategy {
37
- recover(recognizer, e) {
38
- throw new Error('Method not implemented.');
39
- }
40
- reportError(recognizer, e) {
41
- throw new Error('Method not implemented.');
42
- }
43
- }
44
- exports.TempErrorStrategy = TempErrorStrategy;
45
47
  class CircuitscriptParserErrorListener {
46
48
  constructor(onErrorHandler = null) {
47
49
  this.syntaxErrorCounter = 0;
48
50
  this.onErrorHandler = null;
49
51
  this.onErrorHandler = onErrorHandler;
50
52
  }
51
- syntaxError(recognizer, offendingSymbol, line, charPositionInLine, msg, e) {
52
- if (this.onErrorHandler) {
53
- this.onErrorHandler(line, charPositionInLine, msg, e);
53
+ syntaxError(recognizer, offendingSymbol, line, column, msg, e) {
54
+ if (this.onErrorHandler && e) {
55
+ this.onErrorHandler(msg, e.ctx, e);
54
56
  }
55
57
  else {
56
- console.log("Syntax error at line", line, ':', charPositionInLine, ' - ', msg);
58
+ if (offendingSymbol && msg.match("extraneous input 'import' expecting")) {
59
+ msg = "Invalid import statement";
60
+ throw new utils_js_1.ParseSyntaxError("Invalid import statement", offendingSymbol);
61
+ }
62
+ console.log("Syntax error at line", line, ':', column, ' - ', msg);
57
63
  }
58
64
  this.syntaxErrorCounter++;
59
65
  }
@@ -5,12 +5,12 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  const fs_1 = __importDefault(require("fs"));
7
7
  const helpers_js_1 = require("./helpers.js");
8
- const sizing_js_1 = require("./sizing.js");
8
+ const environment_js_1 = require("./environment.js");
9
9
  const mainDir = './__tests__/renderData/';
10
- const fontsPath = (0, helpers_js_1.getFontsPath)();
11
- const defaultLibsPath = (0, helpers_js_1.getDefaultLibsPath)();
10
+ const env = new environment_js_1.NodeScriptEnvironment();
11
+ environment_js_1.NodeScriptEnvironment.setInstance(env);
12
12
  async function regenerateTests(extra = "") {
13
- await (0, sizing_js_1.prepareSVGEnvironment)(fontsPath);
13
+ await env.prepareSVGEnvironment();
14
14
  const cstFiles = [];
15
15
  const files = fs_1.default.readdirSync(mainDir);
16
16
  files.forEach(file => {
@@ -18,16 +18,20 @@ async function regenerateTests(extra = "") {
18
18
  cstFiles.push(file);
19
19
  }
20
20
  });
21
- cstFiles.forEach(file => {
21
+ for (let i = 0; i < cstFiles.length; i++) {
22
+ const file = cstFiles[i];
22
23
  const inputPath = mainDir + file;
23
24
  const scriptData = fs_1.default.readFileSync(inputPath, { encoding: 'utf-8' });
24
25
  const outputPath = mainDir + 'svgs/' + file + extra + '.svg';
25
- (0, helpers_js_1.renderScript)(scriptData, outputPath, {
26
- currentDirectory: mainDir,
27
- defaultLibsPath,
26
+ env.setModuleDirectory(mainDir);
27
+ env.setDefaultLibsPath(mainDir + '../../libs/');
28
+ await (0, helpers_js_1.renderScript)(scriptData, outputPath, {
29
+ dumpNets: false,
30
+ dumpData: false,
31
+ showStats: false,
32
+ environment: env,
28
33
  });
29
- console.log('generated ', outputPath);
30
- });
34
+ }
31
35
  return cstFiles;
32
36
  }
33
37
  (async () => {
@@ -11,11 +11,13 @@ const globals_js_1 = require("./globals.js");
11
11
  const ParamDefinition_js_1 = require("./objects/ParamDefinition.js");
12
12
  const utils_js_1 = require("./utils.js");
13
13
  const helpers_js_1 = require("./helpers.js");
14
+ const environment_js_1 = require("./environment.js");
14
15
  const svg_to_pdfkit_1 = __importDefault(require("svg-to-pdfkit"));
15
16
  const Frame_js_1 = require("./objects/Frame.js");
16
17
  const draw_symbols_js_1 = require("./draw_symbols.js");
17
18
  function createSvgCanvas() {
18
- const window = (0, sizing_js_1.getCreateSVGWindow)()();
19
+ const env = environment_js_1.NodeScriptEnvironment.getInstance();
20
+ const window = env.createSVGWindow();
19
21
  const document = window.document;
20
22
  (0, svg_js_1.registerWindow)(window, document);
21
23
  const canvas = (0, svg_js_1.SVG)(document.documentElement);
@@ -1,71 +1,18 @@
1
1
  "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
- Object.defineProperty(o, "default", { enumerable: true, value: v });
15
- }) : function(o, v) {
16
- o["default"] = v;
17
- });
18
- var __importStar = (this && this.__importStar) || function (mod) {
19
- if (mod && mod.__esModule) return mod;
20
- var result = {};
21
- if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
- __setModuleDefault(result, mod);
23
- return result;
24
- };
25
2
  Object.defineProperty(exports, "__esModule", { value: true });
26
- exports.measureTextSize2 = exports.applyFontsToSVG = exports.getCreateSVGWindow = exports.prepareSVGEnvironment = void 0;
27
- const svg_js_1 = require("@svgdotjs/svg.js");
3
+ exports.measureTextSize2 = exports.applyFontsToSVG = void 0;
28
4
  const big_js_1 = require("big.js");
29
5
  const geometry_js_1 = require("./geometry.js");
30
6
  const globals_js_1 = require("./globals.js");
31
- const helpers_js_1 = require("./helpers.js");
32
- let MainCanvas = null;
33
- const supportedFonts = {
34
- 'Arial': 'Arial.ttf',
35
- };
36
- let globalCreateSVGWindow;
37
- async function prepareSVGEnvironment(fontsPath) {
38
- const moduleType = (0, helpers_js_1.detectJSModuleType)();
39
- if (moduleType === helpers_js_1.JSModuleType.CommonJs) {
40
- const { config, createSVGWindow } = await Promise.resolve().then(() => __importStar(require('svgdom')));
41
- globalCreateSVGWindow = createSVGWindow;
42
- if (fontsPath !== null) {
43
- await config.setFontDir(fontsPath)
44
- .setFontFamilyMappings(supportedFonts)
45
- .preloadFonts();
46
- }
47
- }
48
- }
49
- exports.prepareSVGEnvironment = prepareSVGEnvironment;
50
- function getCreateSVGWindow() {
51
- if (globalCreateSVGWindow === undefined) {
52
- throw "SVG environment is not set up yet";
53
- }
54
- return globalCreateSVGWindow;
55
- }
56
- exports.getCreateSVGWindow = getCreateSVGWindow;
7
+ const environment_js_1 = require("./environment.js");
57
8
  function applyFontsToSVG(canvas) {
58
9
  }
59
10
  exports.applyFontsToSVG = applyFontsToSVG;
60
11
  const measureTextSizeCache = {};
61
12
  const measureTextSizeCacheHits = {};
62
13
  function measureTextSize2(text, fontFamily, fontSize, fontWeight = 'regular', anchor = geometry_js_1.HorizontalAlign.Left, vanchor = geometry_js_1.VerticalAlign.Bottom) {
63
- if (MainCanvas === null) {
64
- const window = getCreateSVGWindow()();
65
- const { document } = window;
66
- (0, svg_js_1.registerWindow)(window, document);
67
- MainCanvas = (0, svg_js_1.SVG)(document.documentElement);
68
- }
14
+ const environment = environment_js_1.NodeScriptEnvironment.getInstance();
15
+ const mainCanvas = environment.getCanvasWindow();
69
16
  const key = `${text}-${fontFamily}-${fontSize}-${fontWeight}-${anchor}-${vanchor}`;
70
17
  if (measureTextSizeCache[key] === undefined) {
71
18
  let dominantBaseline = geometry_js_1.VerticalAlignProp.Hanging;
@@ -93,7 +40,7 @@ function measureTextSize2(text, fontFamily, fontSize, fontWeight = 'regular', an
93
40
  break;
94
41
  }
95
42
  fontFamily = globals_js_1.defaultFont;
96
- const tmpTextElement = MainCanvas.text(text).font({
43
+ const tmpTextElement = mainCanvas.text(text).font({
97
44
  family: fontFamily,
98
45
  size: fontSize,
99
46
  anchor: useAnchor,