engine262 icon indicating copy to clipboard operation
engine262 copied to clipboard

Runtime customizability (for JS-based template engine)

Open brettz9 opened this issue 4 years ago • 3 comments

Hi,

Was wondering whether it might be considered in scope to allow handlers to be called at runtime to override behaviors, e.g., for your README example, being able to do something like:

const realm = new ManagedRealm({
  handlers: {
    evaluate (node, next) {
      if (node.type === 'DoExpression') {
        return yield* Evaluate_Block(node.Block);
      }
      next();
    },
    parsePrimaryExpression (type, next) {
      if (type === Token.DO) {
        const node = this.startNode();
        this.next();
        node.Block = this.parseBlock();
        return this.finishNode(node, 'DoExpression');
      }
      next();
    }
  }
});

This might help not only for my use case of a JS-based template engine (being able to circumscribe, extend, and track allowable features), but could allow easier maintenance of projects which could similarly update their engine262 dep. without needing to reapply changes to source to add features or log features of interest.

brettz9 avatar Jan 16 '21 05:01 brettz9

This extensibility is something I've been thinking about for years, but I'm not sure how to execute on it. what I really want is the ability for spec proposals to include packages which can be loaded into engine262, but I'm not sure how that would be done.

devsnek avatar Jan 16 '21 16:01 devsnek

Is something wrong with just import?

const rules = await Promise.all(['doRule/implementation', 'reactivityRule'].map((rule) => import(rule)));
const realm = new ManagedRealm({rules});

...where the rules have callbacks that get executed with relevant arguments as with my example with handlers? There could be multiple handlers for a given function as necessary too, of course.

brettz9 avatar Jan 17 '21 00:01 brettz9

The problem is that it is unclear how abstract ops would be overridden. As an example, there are multiple regexp proposals, some of which have modifications to RegExpBuiltinExec. If we add the simple ability to override abstract ops on a per-function basis, that still means only one proposal would work if you tried to load multiple at the same time.

devsnek avatar Mar 04 '21 16:03 devsnek