<commit_before>package com.whileyweb;

import static wyc.Activator.WHILEY_PLATFORM;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ForkJoinPool;

import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpException;
import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.entity.StringEntity;
import org.apache.http.message.BasicHttpEntityEnclosingRequest;
import org.apache.http.protocol.HttpContext;

import com.whileyweb.Main.Registry;

import jwebkit.http.HttpMethodDispatchHandler;
import wybs.lang.Build;
import wybs.lang.SyntacticItem;
import wybs.util.AbstractCompilationUnit;
import wybs.util.AbstractCompilationUnit.Attribute;
import wybs.util.AbstractCompilationUnit.Attribute.Span;
import wybs.util.AbstractCompilationUnit.Value;
import wybs.util.SequentialBuildProject;
import wyc.Activator;
import wyc.cmd.QuickCheck;
import wyc.lang.WhileyFile;
import wycc.WyMain;
import wycc.cfg.Configuration;
import wycc.cfg.HashMapConfiguration;
import wyfs.lang.Content;
import wyfs.lang.Path;
import wyfs.util.Trie;
import wyfs.util.VirtualRoot;
import wyil.lang.WyilFile;
import wyil.lang.WyilFile.SyntaxError;
import wyjs.core.JavaScriptFile;

public class WhileyWebCompiler extends HttpMethodDispatchHandler {
	private static String WYRT_LIB = "lib/wystd-v0.2.3.jar".replace('/', File.separatorChar);

	public WhileyWebCompiler() {
		super(HttpMethodDispatchHandler.ALLOW_POST);
	}

	@Override
	public void post(HttpRequest request, HttpResponse response, HttpContext context)
			throws HttpException, IOException {
		HttpEntity entity = checkHasEntity(request);
		List<NameValuePair> params = URLEncodedUtils.parse(entity);
		String code = null;
		boolean verification = false;
		boolean counterexamples = false;
		boolean quickcheck = false;
		for (NameValuePair p : params) {
			if (p.getName().equals("code")) {
				code = p.getValue();
			} else if (p.getName().equals("verify")) {
				verification = Boolean.parseBoolean(p.getValue());
			} else if (p.getName().equals("counterexamples")) {
				counterexamples = Boolean.parseBoolean(p.getValue());
			} else if (p.getName().equals("quickcheck")) {
				quickcheck = Boolean.parseBoolean(p.getValue());
			}
		}
		if (code == null) {
			response.setStatusCode(HttpStatus.SC_BAD_REQUEST);
		} else {
			String r = compile(code, verification, counterexamples, quickcheck);
			response.setEntity(new StringEntity(r)); // ContentType.APPLICATION_JSON fails?
			response.setStatusCode(HttpStatus.SC_OK);
		}
	}

	private HttpEntity checkHasEntity(HttpRequest request) throws HttpException {
		if (request instanceof BasicHttpEntityEnclosingRequest) {
			BasicHttpEntityEnclosingRequest r = (BasicHttpEntityEnclosingRequest) request;
			return r.getEntity();
		} else {
			throw new HttpException("Missing entity");
		}
	}

	private String compile(String code, boolean verification, boolean counterexamples, boolean quickcheck)
			throws IOException, HttpException {
		Content.Registry registry = new Registry();
		// Determine project directory
		Path.Root localRoot = new VirtualRoot(registry);
		// Read the configuration schema
		Configuration.Schema schema = Configuration.toCombinedSchema(WyMain.LOCAL_CONFIG_SCHEMA,
				WHILEY_PLATFORM.getConfigurationSchema());
		HashMapConfiguration configuration = new HashMapConfiguration(schema);
		//
		if(verification) {
			configuration.write(Activator.VERIFY_CONFIG_OPTION, new Value.Bool(true));
		}
		if(counterexamples) {
			configuration.write(Activator.COUNTEREXAMPLE_CONFIG_OPTION, new Value.Bool(true));
		}
		// Construct environment and execute arguments
		Build.Project project = new SequentialBuildProject(localRoot);
		// Initialise the whiley platform
		wyc.Activator.WHILEY_PLATFORM.initialise(configuration, project);
		// Refresh system state
		//
		Path.Root root = project.getRoot();
		Path.ID srcID = Trie.ROOT.append("src").append("main");
		Path.ID binID = Trie.ROOT.append("bin").append("main");
		//
		Path.Entry<WhileyFile> srcFile = root.get(srcID, WhileyFile.ContentType);
		if (srcFile == null) {
			srcFile = root.create(srcID, WhileyFile.ContentType);
		}
		// Write contents into source file
		srcFile.outputStream().write(code.getBytes());
		// Refresh project to ensure changes to binary file visible
		project.refresh();
		// Create project
		HashMap<String, Object> result = new HashMap<>();
		try {
			boolean ok = project.build(ForkJoinPool.commonPool()).get();
			// Extract the binary file
			Path.Entry<WyilFile> binFile = root.get(binID, WyilFile.ContentType);
			WyilFile binary = binFile.read();
			// Perform quickcheck testing!
			if(ok && quickcheck) {
				QuickCheck.Context context = QuickCheck.DEFAULT_CONTEXT;
				ok = new QuickCheck(project, null, System.out, System.err).check(binary, context);
			}
			// Flush everything to disk
			root.flush();
			//
			if (!ok) {
				// Build failed for some reason. Need to extract the error messages.
				List<SyntaxError> errors = binary.findAll(SyntaxError.class);
				result.put("result", "errors");
				result.put("errors", toErrorMessages(srcFile, errors));
			} else {
				// All is well
				// result.put("js", extractJavaScript(file));
				result.put("result", "success");
				// result.put("js", extractJavaScript(file));
			}
		} catch (Exception e) {
			e.printStackTrace();
			// Some kind of internal failure has occurred, so simply report this.
			result.put("result", "exception");
			result.put("text", e.getMessage());<commit_msg>Update to reenable JavaScript compilation<commit_after>package com.whileyweb;

import static wyc.Activator.WHILEY_PLATFORM;
import static wyjs.Activator.JS_PLATFORM;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ForkJoinPool;

import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpException;
import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.entity.StringEntity;
import org.apache.http.message.BasicHttpEntityEnclosingRequest;
import org.apache.http.protocol.HttpContext;

import com.whileyweb.Main.Registry;

import jwebkit.http.HttpMethodDispatchHandler;
import wybs.lang.Build;
import wybs.lang.SyntacticItem;
import wybs.util.AbstractCompilationUnit;
import wybs.util.AbstractCompilationUnit.Attribute;
import wybs.util.AbstractCompilationUnit.Attribute.Span;
import wybs.util.AbstractCompilationUnit.Value;
import wybs.util.SequentialBuildProject;
import wyc.Activator;
import wyc.cmd.QuickCheck;
import wyc.lang.WhileyFile;
import wycc.WyMain;
import wycc.cfg.Configuration;
import wycc.cfg.HashMapConfiguration;
import wyfs.lang.Content;
import wyfs.lang.Path;
import wyfs.util.Trie;
import wyfs.util.VirtualRoot;
import wyil.lang.WyilFile;
import wyil.lang.WyilFile.SyntaxError;
import wyjs.core.JavaScriptFile;

public class WhileyWebCompiler extends HttpMethodDispatchHandler {
	private static String WYRT_LIB = "lib/wystd-v0.2.3.jar".replace('/', File.separatorChar);

	public WhileyWebCompiler() {
		super(HttpMethodDispatchHandler.ALLOW_POST);
	}

	@Override
	public void post(HttpRequest request, HttpResponse response, HttpContext context)
			throws HttpException, IOException {
		HttpEntity entity = checkHasEntity(request);
		List<NameValuePair> params = URLEncodedUtils.parse(entity);
		String code = null;
		boolean verification = false;
		boolean counterexamples = false;
		boolean quickcheck = false;
		for (NameValuePair p : params) {
			if (p.getName().equals("code")) {
				code = p.getValue();
			} else if (p.getName().equals("verify")) {
				verification = Boolean.parseBoolean(p.getValue());
			} else if (p.getName().equals("counterexamples")) {
				counterexamples = Boolean.parseBoolean(p.getValue());
			} else if (p.getName().equals("quickcheck")) {
				quickcheck = Boolean.parseBoolean(p.getValue());
			}
		}
		if (code == null) {
			response.setStatusCode(HttpStatus.SC_BAD_REQUEST);
		} else {
			String r = compile(code, verification, counterexamples, quickcheck);
			response.setEntity(new StringEntity(r)); // ContentType.APPLICATION_JSON fails?
			response.setStatusCode(HttpStatus.SC_OK);
		}
	}

	private HttpEntity checkHasEntity(HttpRequest request) throws HttpException {
		if (request instanceof BasicHttpEntityEnclosingRequest) {
			BasicHttpEntityEnclosingRequest r = (BasicHttpEntityEnclosingRequest) request;
			return r.getEntity();
		} else {
			throw new HttpException("Missing entity");
		}
	}

	private String compile(String code, boolean verification, boolean counterexamples, boolean quickcheck)
			throws IOException, HttpException {
		Content.Registry registry = new Registry();
		// Determine project directory
		Path.Root localRoot = new VirtualRoot(registry);
		// Read the configuration schema
		Configuration.Schema schema = Configuration.toCombinedSchema(WyMain.LOCAL_CONFIG_SCHEMA,
				WHILEY_PLATFORM.getConfigurationSchema(), JS_PLATFORM.getConfigurationSchema());
		HashMapConfiguration configuration = new HashMapConfiguration(schema);
		//
		if(verification) {
			configuration.write(Activator.VERIFY_CONFIG_OPTION, new Value.Bool(true));
		}
		if(counterexamples) {
			configuration.write(Activator.COUNTEREXAMPLE_CONFIG_OPTION, new Value.Bool(true));
		}
		// Construct environment and execute arguments
		Build.Project project = new SequentialBuildProject(localRoot);
		// Initialise the whiley platform
		WHILEY_PLATFORM.initialise(configuration, project);
		JS_PLATFORM.initialise(configuration, project);
		// Refresh system state
		//
		Path.Root root = project.getRoot();
		Path.ID srcID = Trie.ROOT.append("src").append("main");
		Path.ID binID = Trie.ROOT.append("bin").append("main");
		Path.ID jsBinID = Trie.ROOT.append("bin").append("js").append("main");
		//
		Path.Entry<WhileyFile> srcFile = root.get(srcID, WhileyFile.ContentType);
		if (srcFile == null) {
			srcFile = root.create(srcID, WhileyFile.ContentType);
		}
		// Write contents into source file
		srcFile.outputStream().write(code.getBytes());
		// Refresh project to ensure changes to binary file visible
		project.refresh();
		// Create project
		HashMap<String, Object> result = new HashMap<>();
		try {
			boolean ok = project.build(ForkJoinPool.commonPool()).get();
			// Extract the binary file
			Path.Entry<WyilFile> binFile = root.get(binID, WyilFile.ContentType);
			WyilFile binary = binFile.read();
			// Perform quickcheck testing!
			if(ok && quickcheck) {
				QuickCheck.Context context = QuickCheck.DEFAULT_CONTEXT;
				ok = new QuickCheck(project, null, System.out, System.err).check(binary, context);
			}
			// Flush everything to disk
			root.flush();
			//
			if (!ok) {
				// Build failed for some reason. Need to extract the error messages.
				List<SyntaxError> errors = binary.findAll(SyntaxError.class);
				result.put("result", "errors");
				result.put("errors", toErrorMessages(srcFile, errors));
			} else {
				// All is well
				Path.Entry<JavaScriptFile> jsFile = root.get(jsBinID, JavaScriptFile.ContentType);
				result.put("js", extractJavaScript(jsFile));
				result.put("result", "success");
			}
		} catch (Exception e) {
			e.printStackTrace();
			// Some kind of internal failure has occurred, so simply report this.
			result.put("result", "exception");
			result.put("text", e.getMessage());
{'fraction_non_alphanumeric': 0.11667913238593867, 'fraction_numerical': 0.0006648383611734397, 'mean_word_length': 4.936852491366552, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1775185', 'n_tokens_mistral': 3930, 'n_tokens_neox': 3599, 'n_words': 782}