Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enabling ES6? #164

Open
lukehutch opened this issue Dec 20, 2024 · 3 comments
Open

Enabling ES6? #164

lukehutch opened this issue Dec 20, 2024 · 3 comments

Comments

@lukehutch
Copy link

lukehutch commented Dec 20, 2024

Can this sandbox start Nashorn as follows?:

ScriptEngine engine = new NashornScriptEngineFactory().getScriptEngine("--language=es6");

And even if you can get an engine, how do you turn the engine into a sandbox, so you can call setMaxCPUTime etc.?

@mxro
Copy link
Collaborator

mxro commented Dec 22, 2024

Thank you for your question!

Yes, that is supported, see example:

package delight.nashornsandbox;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import javax.script.ScriptException;

import org.junit.Test;

import delight.nashornsandbox.exceptions.ScriptCPUAbuseException;

public class TestES6 {

	@Test
	public void test() throws ScriptCPUAbuseException, ScriptException {
		NashornSandbox sandbox = NashornSandboxes.create("--language=es6");
		sandbox.setMaxCPUTime(100);
		sandbox.setMaxMemory(1000 * 1000);
		ExecutorService executor = Executors.newSingleThreadExecutor();
		sandbox.setExecutor(executor);
		sandbox.eval("let greetUsers = (users) => {"+
  "  users.forEach((args) => {"+
    "  console.log(`Hello, ${args.name}! You are ${args.age} years old.`);"+
  "});"+
"};");

		sandbox.getExecutor().shutdown();

	}

}

Note that Nashorn does not support all ES6 features. Not sure where the latest documentation is, but this one gives some idea: https://openjdk.org/jeps/292

@lukehutch
Copy link
Author

Thank you -- you should probably document this in the main README.md, it will be very commonly needed.

@mxro
Copy link
Collaborator

mxro commented Dec 24, 2024

Great idea! I've added this into the polyfill PR as well, see here https://github.com/javadelight/delight-nashorn-sandbox/pull/167/files#diff-b335630551682c19a781afebcf4d07bf978fb1f8ac04c6bf87428ed5106870f5

I thought easiest would just be to change the default in all the examples to es6 - given there should be little downside in using that, and should be easy enough to remove?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants