<commit_before>/*
 *    Copyright 2012 <NAME>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package jj.resource.document;

import static jj.resource.document.ScriptExecutionState.*;

import org.mozilla.javascript.ScriptableObject;

import jj.engine.EngineAPI;
import jj.event.Publisher;
import jj.resource.AbstractResourceBase;
import jj.resource.ResourceCacheKey;
import jj.script.RhinoContext;
import jj.script.RhinoContextMaker;

/**
 * @author jason
 *
 */
public abstract class AbstractScriptEnvironment extends AbstractResourceBase implements ScriptEnvironment {


	protected final Publisher publisher;
	
	protected final RhinoContextMaker contextMaker;
	
	protected final EngineAPI api;
	
	protected ScriptExecutionState state = Unitialized;
	
	/**
	 * @param cacheKey
	 */
	protected AbstractScriptEnvironment(
		final ResourceCacheKey cacheKey,
		final Publisher publisher,
		final RhinoContextMaker contextMaker,
		final EngineAPI api
	) {
		super(cacheKey);
		this.publisher = publisher;
		this.contextMaker = contextMaker;
		this.api = api;
	}
	
	@Override
	public ScriptableObject newObject() {
		try (RhinoContext context = contextMaker.context()) {
			return context.newObject(scope());
		}
	}

	@Override
	public boolean initialized() {
		return state == Initialized;
	}

	@Override
	public void initialized(boolean initialized) {
		if (initialized) {
			state = Initialized;
			publisher.publish(new ExecutionEnvironmentInitialized(this));
		}
	}

	@Override
	public boolean initializing() {
		return state == Initializing;
	}

	@Override
	public void initializing(boolean initializing) {
		if (initializing && state == Unitialized) {
			state = Initializing;
		}
	}

	/**
	 * @return
	 */
	public Object exports() {
		try (RhinoContext context = contextMaker.context()) {
			return context.evaluateString(scope(), "module.exports", "returning exports"); 
		}
	}

	public String toString() {
		return new StringBuilder(getClass().getName())
			.append("[")
			.append(baseName()).append("/").append(scriptName())
			.append("@").append(sha1())
			.append("] {")
			.append("state=").append(state)
			.append("}")
			.toString();
	}

	protected ScriptableObject createLocalScope(final String moduleIdentifier) {
		try (RhinoContext context = contextMaker.context()) {
			
			// this technique allows for a "local" global scope that wraps a read-only global
			// scope that provides a server-wide API installation - effectively sealing the
			// API to prevent it from being manipulated and allowing it to be shared in a safe
			// manner
			ScriptableObject local = context.newObject(api.global());
			local.setPrototype(api.global());
			local.setParentScope(null);

			// setting up the 'module' property as described in 
			// the commonjs module 1.1.1 specification
			// in the case of the top-level server script, the id
			// will be the baseName, which fortunately happens to be
			// exactly what is required
			ScriptableObject module = context.newObject(local);
			ScriptableObject exports = context.newObject(local);
			module.defineProperty("id", moduleIdentifier, ScriptableObject.CONST);
			module.defineProperty("exports", exports, ScriptableObject.EMPTY);
			
			local.defineProperty("module", module, ScriptableObject.CONST);
			local.defineProperty("exports", exports, ScriptableObject.CONST);
			
			// define the require method and the exports object here as well.
			// follow the node.js concept of module.exports === exports, and
			// assigning to module.exports changes the exports object,
			// potentially to a function
			Object require = context.evaluateString(local, "global['//makeRequire'](module);", "making require");
			local.defineProperty(
				"require",
				require,
				ScriptableObject.CONST
			);
			
			return local;
		}
	}

}
<commit_msg>making the toString nicer on script environments<commit_after>/*
 *    Copyright 2012 <NAME>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package jj.resource.document;

import static jj.resource.document.ScriptExecutionState.*;

import org.mozilla.javascript.ScriptableObject;

import jj.engine.EngineAPI;
import jj.event.Publisher;
import jj.resource.AbstractResourceBase;
import jj.resource.ResourceCacheKey;
import jj.script.RhinoContext;
import jj.script.RhinoContextMaker;

/**
 * @author jason
 *
 */
public abstract class AbstractScriptEnvironment extends AbstractResourceBase implements ScriptEnvironment {


	protected final Publisher publisher;
	
	protected final RhinoContextMaker contextMaker;
	
	protected final EngineAPI api;
	
	protected ScriptExecutionState state = Unitialized;
	
	/**
	 * @param cacheKey
	 */
	protected AbstractScriptEnvironment(
		final ResourceCacheKey cacheKey,
		final Publisher publisher,
		final RhinoContextMaker contextMaker,
		final EngineAPI api
	) {
		super(cacheKey);
		this.publisher = publisher;
		this.contextMaker = contextMaker;
		this.api = api;
	}
	
	@Override
	public ScriptableObject newObject() {
		try (RhinoContext context = contextMaker.context()) {
			return context.newObject(scope());
		}
	}

	@Override
	public boolean initialized() {
		return state == Initialized;
	}

	@Override
	public void initialized(boolean initialized) {
		if (initialized) {
			state = Initialized;
			publisher.publish(new ExecutionEnvironmentInitialized(this));
		}
	}

	@Override
	public boolean initializing() {
		return state == Initializing;
	}

	@Override
	public void initializing(boolean initializing) {
		if (initializing && state == Unitialized) {
			state = Initializing;
		}
	}

	/**
	 * @return
	 */
	public Object exports() {
		try (RhinoContext context = contextMaker.context()) {
			return context.evaluateString(scope(), "module.exports", "returning exports"); 
		}
	}

	public String toString() {
		return new StringBuilder(getClass().getSimpleName())
			.append("[")
			.append(baseName())
			.append("@").append(sha1())
			.append("] {")
			.append("state=").append(state)
			.append("}")
			.toString();
	}

	protected ScriptableObject createLocalScope(final String moduleIdentifier) {
		try (RhinoContext context = contextMaker.context()) {
			
			// this technique allows for a "local" global scope that wraps a read-only global
			// scope that provides a server-wide API installation - effectively sealing the
			// API to prevent it from being manipulated and allowing it to be shared in a safe
			// manner
			ScriptableObject local = context.newObject(api.global());
			local.setPrototype(api.global());
			local.setParentScope(null);

			// setting up the 'module' property as described in 
			// the commonjs module 1.1.1 specification
			// in the case of the top-level server script, the id
			// will be the baseName, which fortunately happens to be
			// exactly what is required
			ScriptableObject module = context.newObject(local);
			ScriptableObject exports = context.newObject(local);
			module.defineProperty("id", moduleIdentifier, ScriptableObject.CONST);
			module.defineProperty("exports", exports, ScriptableObject.EMPTY);
			
			local.defineProperty("module", module, ScriptableObject.CONST);
			local.defineProperty("exports", exports, ScriptableObject.CONST);
			
			// define the require method and the exports object here as well.
			// follow the node.js concept of module.exports === exports, and
			// assigning to module.exports changes the exports object,
			// potentially to a function
			Object require = context.evaluateString(local, "global['//makeRequire'](module);", "making require");
			local.defineProperty(
				"require",
				require,
				ScriptableObject.CONST
			);
			
			return local;
		}
	}

}

{'fraction_non_alphanumeric': 0.1008335262792313, 'fraction_numerical': 0.0027784209307710117, 'mean_word_length': 4.046144859813084, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '2410562', 'n_tokens_mistral': 2636, 'n_tokens_neox': 2331, 'n_words': 750}