eval Is Refused

Status: refused by design

eval is not FunctionalScript. eval("1") is valid JavaScript, and fjs compile refuses it by design: the demo below shows what it prints.

eval runs source text it receives at run time. That text is no part of the module the compiler reads, so nothing about it can be checked before it runs: not that it is FunctionalScript, not what it reads, not that it is pure. The built-ins register never admits it.

The row op.eval of refused constructs registers the refusal, and built-ins gives its reason.

Examples

export default eval("1 + 1");

The demo on this page compiles sample.mjs, the module above, with this build's fjs compile, and shows the line it prints and the token it refuses. Below that it shows what JavaScript answers for the same module: 2.

What to Write Instead

Write the code itself, where the compiler can check it, as alternative.f.js does:

export default 1 + 1;

It answers 2. The code is part of the module, so the compiler checks it as it checks the rest.

A Better Message

Today fjs compile prints reserved word at the refused token. The demo proposes what it should print instead: the reason at the same token, what JavaScript does there, and the allowed spelling, the line alternative.f.js writes, as help. The page compiles the alternative too, so the suggestion is one the compiler accepts.

Reference

Demo

Not allowed in FunctionalScript

This is valid JavaScript, and fjs compile refuses it, by design: it is not a FunctionalScript module.

What fjs compile sample.mjs output.js prints:

sample.mjs:1:16 - error: reserved word
1 | export default eval("1 + 1");
  |                ^

What it should print instead, pointing to what FunctionalScript allows (proposed):

sample.mjs:1:16 - error: `eval` is not FunctionalScript: it runs source text no compiler has read
1 | export default eval("1 + 1");
  |                ^^^^ JavaScript runs `"1 + 1"` here and answers `2`
  = help: write the code itself, where the compiler can check it:
          export default 1 + 1;
  = note: refused by design, see spec/built-ins/eval/README.md

JavaScript source, sample.mjs:

export default eval("1 + 1");

What JavaScript answers for the same module, its default export:

2

What to write instead

The FunctionalScript spelling, alternative.f.js:

/**
 * The FunctionalScript spelling of [`sample.mjs`](./sample.mjs): the source `eval` would run, written as code the compiler reads
 * ([`README.md`](./README.md)).
 *
 * @module
 */

export default 1 + 1;

What fjs compile alternative.f.js output.js writes:

export default 1+1;

What it answers, its default export:

2

Emergent Testing

Idle. Press Run to start the suite.

  • ./proof.f.mjs