sebastienros / jint

Javascript Interpreter for .NET
BSD 2-Clause "Simplified" License
4.04k stars 558 forks source link

Unexpected token using modules #1848

Closed calico-crusade closed 4 months ago

calico-crusade commented 4 months ago

Version

With which version did you encounter this behavior. Please check against 3.x beta if you haven't yet, it might have the fix already.

Version: 3.1.1 (Nuget) No beta version for 3.1.1

Describe the bug

Exception thrown: Esprima.ParserException: 'Line 1: Unexpected token' when attempting to use modules and evaluate

To Reproduce

using Jint;

const string MODULE = @"export const add = (a, b) => return a + b";
const string CODE = @"import { add } from 'maths';
return add(1, 2);";

var engine = new Engine();
engine.Modules.Add("maths", MODULE);
var result = engine.Evaluate(CODE);
Console.WriteLine("Result: " + result);

Expected behavior

Code should execute and output Result: 3

Additional context

Tested in versions 3.1.1 and 3.1.0 and neither worked.

calico-crusade commented 4 months ago

Nevermind. Didn't read the discussions first!

For anyone else; Evaluate and Execute are not run in the scope of a module so it cannot access modules.

Examples of how to do it here