@nova-energy/openamx 0.19.0 → 0.22.0

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.
@@ -0,0 +1,171 @@
1
+ import { AmxError } from '../diagnostics/errors';
2
+ import { evaluateStatements, validateRuntimeValue } from './evaluateExpression';
3
+ function runtimeFailure(plan, file, phase, error) {
4
+ const message = error instanceof Error ? error.message : String(error);
5
+ throw new AmxError({
6
+ code: 'AMX3025',
7
+ message: `TypeScript ${phase} failed: ${message}`,
8
+ file,
9
+ line: plan.node.contentSource.line,
10
+ column: plan.node.contentSource.column
11
+ });
12
+ }
13
+ function supportsReference(reference, plan, seen = new Set()) {
14
+ if (reference.type === 'listType' || reference.type === 'nullableType') {
15
+ return !!reference.element && supportsReference(reference.element, plan, seen);
16
+ }
17
+ const name = reference.name;
18
+ if (!name)
19
+ return false;
20
+ if (name === 'Number' || name === 'String' || name === 'Boolean' || name === 'DateTime')
21
+ return true;
22
+ if (plan.visibleEnums.has(name))
23
+ return true;
24
+ const declaration = plan.visibleTypes.get(name);
25
+ if (!declaration || seen.has(name) || declaration.fields.some(field => field.optional))
26
+ return false;
27
+ const nextSeen = new Set(seen).add(name);
28
+ return declaration.fields.every(field => supportsReference(field.annotation, plan, nextSeen));
29
+ }
30
+ function supportsType(type, plan, seen = new Set()) {
31
+ if (type.kind === 'list' || type.kind === 'nullable')
32
+ return supportsType(type.element, plan, seen);
33
+ if (type.kind !== 'named')
34
+ return false;
35
+ if (type.name === 'Number' || type.name === 'String' || type.name === 'Boolean' || type.name === 'DateTime')
36
+ return true;
37
+ if (plan.visibleEnums.has(type.name))
38
+ return true;
39
+ const declaration = plan.visibleTypes.get(type.name);
40
+ if (!declaration || seen.has(type.name) || declaration.fields.some(field => field.optional))
41
+ return false;
42
+ const nextSeen = new Set(seen).add(type.name);
43
+ return declaration.fields.every(field => supportsReference(field.annotation, plan, nextSeen));
44
+ }
45
+ function enumValueObject(declaration) {
46
+ const values = declaration.resolvedValues;
47
+ if (!values || values.length !== declaration.members.length) {
48
+ throw new Error(`Enum '${declaration.name}' has unresolved runtime values`);
49
+ }
50
+ const valueObject = Object.create(null);
51
+ declaration.members.forEach((member, index) => {
52
+ Object.defineProperty(valueObject, member.name, {
53
+ value: values[index],
54
+ enumerable: true,
55
+ writable: false,
56
+ configurable: false
57
+ });
58
+ });
59
+ return Object.freeze(valueObject);
60
+ }
61
+ function cloneData(value, copies = new WeakMap()) {
62
+ if (value === null || typeof value !== 'object')
63
+ return value;
64
+ const prior = copies.get(value);
65
+ if (prior !== undefined)
66
+ return prior;
67
+ if (Array.isArray(value)) {
68
+ const copy = new Array(value.length);
69
+ copies.set(value, copy);
70
+ for (let index = 0; index < value.length; index++)
71
+ copy[index] = cloneData(value[index], copies);
72
+ return copy;
73
+ }
74
+ const prototype = Object.getPrototypeOf(value);
75
+ if (prototype !== Object.prototype && prototype !== null)
76
+ return value;
77
+ const copy = Object.create(prototype);
78
+ copies.set(value, copy);
79
+ for (const [field, item] of Object.entries(value))
80
+ copy[field] = cloneData(item, copies);
81
+ return copy;
82
+ }
83
+ export function prepareTypeScriptBlocks(plans, file) {
84
+ const prepared = new Map();
85
+ for (const [nodeIndex, plan] of plans) {
86
+ try {
87
+ const transpiler = new Bun.Transpiler({ loader: 'ts' });
88
+ prepared.set(nodeIndex, transpiler.transformSync(plan.node.content));
89
+ }
90
+ catch (error) {
91
+ runtimeFailure(plan, file, 'transpilation', error);
92
+ }
93
+ }
94
+ return prepared;
95
+ }
96
+ function evaluateTypeScriptBlock(plan, code, env, file) {
97
+ const visibleBindings = plan.visibleBindings.filter(binding => supportsType(binding.type, plan));
98
+ const mutableBindings = visibleBindings.filter(binding => binding.mutable);
99
+ const enumEntries = [...plan.visibleEnums.entries()];
100
+ const outputNames = plan.outputs.map(output => output.name);
101
+ const returnNames = [...outputNames, ...mutableBindings.map(binding => binding.name)];
102
+ const argumentNames = [
103
+ ...visibleBindings.map(binding => binding.name),
104
+ ...enumEntries.map(([name]) => name)
105
+ ];
106
+ let visibleValues;
107
+ let enumValues;
108
+ try {
109
+ visibleValues = visibleBindings.map(binding => env.get(binding.name, binding.source, file));
110
+ enumValues = enumEntries.map(([, declaration]) => enumValueObject(declaration));
111
+ }
112
+ catch (error) {
113
+ runtimeFailure(plan, file, 'bridge preparation', error);
114
+ }
115
+ let returned;
116
+ try {
117
+ const body = `"use strict";\n${code}\nreturn [${returnNames.join(', ')}];\n`;
118
+ returned = new Function(...argumentNames, body)(...visibleValues, ...enumValues);
119
+ }
120
+ catch (error) {
121
+ runtimeFailure(plan, file, 'execution', error);
122
+ }
123
+ if (!Array.isArray(returned) || returned.length !== returnNames.length) {
124
+ runtimeFailure(plan, file, 'bridge writeback', new Error('The TypeScript block returned an invalid bridge result'));
125
+ }
126
+ const outputValues = returned.slice(0, plan.outputs.length);
127
+ const mutableValues = returned.slice(plan.outputs.length);
128
+ for (let index = 0; index < plan.outputs.length; index++) {
129
+ const output = plan.outputs[index];
130
+ validateRuntimeValue(outputValues[index], output.type, env, output.source, file, output.name);
131
+ }
132
+ for (let index = 0; index < mutableBindings.length; index++) {
133
+ const binding = mutableBindings[index];
134
+ validateRuntimeValue(mutableValues[index], binding.type, env, binding.source, file, binding.name);
135
+ }
136
+ for (let index = 0; index < plan.outputs.length; index++) {
137
+ const output = plan.outputs[index];
138
+ const value = output.mutable ? outputValues[index] : cloneData(outputValues[index]);
139
+ env.set(output.name, value);
140
+ if (!output.mutable)
141
+ env.markImmutable(value);
142
+ }
143
+ for (let index = 0; index < mutableBindings.length; index++) {
144
+ const binding = mutableBindings[index];
145
+ env.update(binding.name, mutableValues[index], binding.source, file);
146
+ }
147
+ }
148
+ export function evaluateDocumentNodes(doc, env, file, plans, prepared) {
149
+ for (let nodeIndex = 0; nodeIndex < doc.nodes.length; nodeIndex++) {
150
+ const node = doc.nodes[nodeIndex];
151
+ env.currentDocumentNodeIndex = nodeIndex;
152
+ if (node.type === 'executableCodeBlock') {
153
+ evaluateStatements(node.statements, env, file);
154
+ continue;
155
+ }
156
+ if (node.type !== 'typescriptCodeBlock')
157
+ continue;
158
+ const plan = plans.get(nodeIndex);
159
+ const code = prepared.get(nodeIndex);
160
+ if (!plan || code === undefined) {
161
+ throw new AmxError({
162
+ code: 'AMX3022',
163
+ message: 'TypeScript runtime bridge was not prepared for this block',
164
+ file,
165
+ line: node.source.line,
166
+ column: node.source.column
167
+ });
168
+ }
169
+ evaluateTypeScriptBlock(plan, code, env, file);
170
+ }
171
+ }