eclipsesource / eclipsesource/J2V8

Issue with terminateExecution

Open
#452 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Java
Stars
2.6k
Forks
387
PR merge metrics
No merged PRs in 30d

Description

Hello,
I currently have a problem with the `terminateExecution()` method. The V8 instance is created in a new thread and should later be killed using the method 'shutdown()' in the class below. It calls `terminateExecution()`, yet the script keeps on running.

```
package me.benfah.spigotcomputers.util;

import java.lang.reflect.Method;

import com.eclipsesource.v8.V8;
import com.eclipsesource.v8.V8Object;
import com.google.common.util.concurrent.UncaughtExceptionHandlers;

import me.benfah.spigotcomputers.computer.Computer;
import me.benfah.spigotcomputers.computer.IComputerAPI;
import me.benfah.spigotcomputers.computer.Script;
import me.benfah.spigotcomputers.computer.apis.Scriptable;
import me.benfah.spigotcomputers.computer.type.AbstractComputer;

public class V8Sandbox extends Thread
{

public final static int MAX_MEMORY = 100 * 1024;


V8 sandbox = V8.createV8Runtime();
Thread terminateThread;
boolean terminated = false;
public V8 getSandbox()
{
return sandbox;
}

AbstractComputer computer;
Script script;
String scriptId;

public V8Sandbox(AbstractComputer computer, Script script, String scriptId)
{
this.computer = computer;
this.script = script;
this.scriptId = scriptId;
setSandboxParams();
setUncaughtExceptionHandler(new UncaughtExceptionHandler()
{

@Override
public void uncaughtException(Thread t, Throwable e)
{
System.out.println("Uncaught exception: " + e);
}
});
}

public AbstractComputer getComputer()
{
return computer;
}

public Script getScript()
{
return script;
}

public String getScriptId()
{
return scriptId;
}

public void setSandboxParams()
{
for(IComputerAPI api : computer.getAllAPIs())
{
V8Object obj = new V8Object(sandbox);
sandbox.add(api.getCallLabel(), obj);
Method[] methods = api.getClass().getMethods();

for(Method m : methods)
{
Scriptable scriptable = m.getAnnotation(Scriptable.class);
if(scriptable != null)
{
String funcName = m.getName();
if(!scriptable.functionName().isEmpty())
funcName = scriptable.functionName();

obj.registerJavaMethod(api, m.getName(), funcName, m.getParameterTypes());
}

}
obj.release();
}

sandbox.registerJavaMethod(System.out, "println", "print", new Class[] {String.class});
sandbox.getLocker().release();
}

public boolean isTerminated()
{
return terminated;
}

public void shutdown() // <- This method is being called on the main thread
{
sandbox.terminateExecution();
//terminated = true;
}

public void run()
{
sandbox.getLocker().acquire();
try
{
sandbox.executeScript(script.getCode());
} catch(RuntimeException e)
{

}
finally
{
sandbox.release();
}
}



}

```
How can I properly stop my script from executing?

Thanks in advance 😄

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.