All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.ssssssss.script.MagicScriptDebugContext Maven / Gradle / Ivy

The newest version!
package org.ssssssss.script;

import org.ssssssss.script.runtime.Variables;

import java.util.*;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;

public class MagicScriptDebugContext extends MagicScriptContext {

	private final BlockingQueue producer = new LinkedBlockingQueue<>();
	private final BlockingQueue consumer = new LinkedBlockingQueue<>();
	public List breakpoints;
	private String id = UUID.randomUUID().toString().replace("-", "");
	private Consumer> callback;

	private int[] line;

	private int timeout = 60;

	private boolean stepInto = false;

	public MagicScriptDebugContext(List breakpoints) {
		this.breakpoints = breakpoints;
	}

	public void setCallback(Consumer> callback) {
		this.callback = callback;
	}

	public void setTimeout(int timeout) {
		this.timeout = timeout;
	}

	public void setBreakpoints(List breakpoints) {
		this.breakpoints = breakpoints;
	}

	@Override
	public synchronized void pause(int startRow, int startCol, int endRow, int endCol, Variables variables) throws InterruptedException {
		if(stepInto || breakpoints.contains(startRow)){
			this.line = new int[]{startRow, startCol, endRow, endCol};
			consumer.offer(this.id);
			Map varMap = new LinkedHashMap<>(getRootVariables());
			varMap.putAll(variables.getVariables(this));
			callback.accept(getDebugInfo(varMap));
			producer.poll(timeout, TimeUnit.SECONDS);
		}
	}

	public void await() throws InterruptedException {
		consumer.take();
	}

	public void singal() throws InterruptedException {
		producer.offer(this.id);
		await();
	}

	public void setStepInto(boolean stepInto) {
		this.stepInto = stepInto;
	}

	private Map getDebugInfo(Map variables) {
		List> varList = new ArrayList<>();
		Set> entries = variables.entrySet();
		for (Map.Entry entry : entries) {
			Object value = entry.getValue();
			Map variable = new HashMap<>();
			variable.put("name", entry.getKey());
			if (value != null) {
				variable.put("value", value);
				variable.put("type", value.getClass());
			} else {
				variable.put("value", "null");
			}
			varList.add(variable);
		}
		Collections.reverse(varList);
		Map info = new HashMap<>();
		info.put("variables", varList);
		info.put("range", line);
		return info;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy