[quickjs-devel] Eval with custom local scope

  • From: Luca Adrian Lindhorst <info@xxxxxxxxxxxxxxxx>
  • To: quickjs-devel@xxxxxxxxxxxxx
  • Date: Mon, 22 Feb 2021 22:30:25 +0100

Hello,

I try to build a commonjs require for my project for compatibility reasons.
So I need to define __filename/__dirname/module/exports in the context of
each file. Currently I do
_script = '(function(' + Object.keys(ctx).join(',') + ') {'
+ _script +
'}).bind({})(...Object.values(this))';
evalThis(_script, file, ctx);
where evalThis is basically a call to "JS_EvalThis" with
JS_EVAL_TYPE_GLOBAL. This mostly works but is not ideal for multiple
reasons: for one if those files use ES6 imports they would break.

Currently the only way I could find is to heavily modifying JS_InternalEval
if not the parsing functions themselves.

Are there any better solutions to this?

Thanks
Luca Lindhorst

Other related posts:

  • » [quickjs-devel] Eval with custom local scope - Luca Adrian Lindhorst