( engine: ComputeEngine, table: SymbolDefinitions )
| 368 | |
| 369 | /** |
| 370 | * Set the symbol table of the current context (`engine.context`) to `table` |
| 371 | * |
| 372 | * `table` can be an array of symbol tables, in order to deal with circular |
| 373 | * dependencies: it is possible to partition a library into multiple |
| 374 | * symbol tables, to control the order in which they are processed and |
| 375 | * avoid having expressions in the definition of an entry reference a symbol |
| 376 | * or function name that has not yet been added to the symbol table. |
| 377 | * |
| 378 | */ |
| 379 | export function setSymbolDefinitions( |
| 380 | engine: ComputeEngine, |
| 381 | table: SymbolDefinitions |
| 382 | ): void { |
| 383 | const bindings = engine.context.lexicalScope.bindings; |
| 384 | |
| 385 | if (!engine.strict) { |
| 386 | // @fastpath @todo |
| 387 | } |
| 388 | |
| 389 | // |
| 390 | // Validate and add the symbols from the symbol table |
| 391 | // |
| 392 | // eslint-disable-next-line prefer-const |
| 393 | for (let [name, entry] of Object.entries(table)) { |
| 394 | try { |
| 395 | name = validateDefinitionName(name); |
| 396 | if (isValidOperatorDef(entry)) { |
| 397 | try { |
| 398 | if (bindings.has(name)) |
| 399 | throw new Error( |
| 400 | `Duplicate operator definition: "${name}"\n${JSON.stringify( |
| 401 | bindings.get(name)!, |
| 402 | undefined, |
| 403 | 4 |
| 404 | )}\n` |
| 405 | ); |
| 406 | |
| 407 | bindings.set(name, { |
| 408 | operator: new _BoxedOperatorDefinition(engine, name, entry), |
| 409 | }); |
| 410 | } catch (e) { |
| 411 | console.error( |
| 412 | [ |
| 413 | `\nError in operator definition`, |
| 414 | JSON.stringify(entry, undefined, 4), |
| 415 | '', |
| 416 | e instanceof Error ? e.message : String(e), |
| 417 | ].join('\n| ') + '\n' |
| 418 | ); |
| 419 | } |
| 420 | } else if (isValidValueDef(entry)) { |
| 421 | try { |
| 422 | if (bindings.has(name)) |
| 423 | throw new Error(`The symbol "${name}" is already defined`); |
| 424 | |
| 425 | bindings.set(name, { |
| 426 | value: new _BoxedValueDefinition(engine, name, entry, { |
| 427 | trustedLibraryDefinition: true, |
no test coverage detected