<commit_before>'use strict';
let Q = require('q');
let errorWithMetadata = require('./util/error-with-metadata');
let commandQueue = require('./command-queue');
commandQueue.registerCommandHandler (
	'label', function() {
		return 'done';
	}
);
commandQueue.registerCommandHandler (
	'goTo', function(label, command) {
		command.context.goTo(label);
		return 'done';
	}
);
commandQueue.registerCommandHandler (
	'exit', function(unused, command) {
		command.context.exit();
		return 'done';
	}
);
exports = module.exports = function(commands, more) {
	more = more || {};
	this.commands = commands;
	this.labels = more.labels || [];
	this.rewind();
};
exports.prototype.rewind = function() {
	this.done = false;
	this.executionOffset = 0;
};
exports.prototype.run = function(startingLabel) {
	let script = this;
	if(this.running) {
		throw new Error (
			"The script is already running"
		);
	}
	if(startingLabel) {
		this.goTo(startingLabel);
	}
	return Q.async(function*() {
		script.running = true;
		while(!script.done) {
			let nextCommand = getNextCommand(script);
			if(nextCommand === undefined) {
				break;
			}
			yield executeCommand(script, nextCommand);
		}
		script.running = false;
	})();
};
function getNextCommand(script) {
	return script.commands[script.executionOffset];
}
function executeCommand(script, command) {
	let deferred = Q.defer();
	commandQueue.push(command, function() {
		if(script.jumping) {
			script.jumping = false;
		}
		else {
			++script.executionOffset;
		}
		deferred.resolve();
		return 'done';
	});
	return deferred.promise;
}
exports.prototype.goTo = function(labelName) {
	if(this.running) {
		this.jumping = true;
	}
	let label = this.labels[labelName];
	if(label === undefined) {
		throw errorWithMetadata (
			new Error (
				"Non-existent label '" + labelName + "'"
			), {
				labelName,
			}
		);
	}
	if(label > this.commands.length) {
		this.executionOffset = this.commands.length;
	}
	else {
		this.executionOffset = label;
	}
};
exports.prototype.exit = function() {
	this.done = true;
};
<commit_msg>low-level-script: Set context.latestLabel variable on progress label commands.<commit_after>'use strict';
let Q = require('q');
let errorWithMetadata = require('./util/error-with-metadata');
let commandQueue = require('./command-queue');
commandQueue.registerCommandHandler (
	'label', function(name, command) {
		if(!command['no-progress']) {
			command.context.latestLabel = name;
		}
		return 'done';
	}
);
commandQueue.registerCommandHandler (
	'goTo', function(label, command) {
		command.context.goTo(label);
		return 'done';
	}
);
commandQueue.registerCommandHandler (
	'exit', function(unused, command) {
		command.context.exit();
		return 'done';
	}
);
exports = module.exports = function(commands, more) {
	more = more || {};
	this.commands = commands;
	this.labels = more.labels || [];
	this.rewind();
};
exports.prototype.rewind = function() {
	this.done = false;
	this.executionOffset = 0;
};
exports.prototype.run = function(startingLabel) {
	let script = this;
	if(this.running) {
		throw new Error (
			"The script is already running"
		);
	}
	if(startingLabel) {
		this.goTo(startingLabel);
	}
	return Q.async(function*() {
		script.running = true;
		while(!script.done) {
			let nextCommand = getNextCommand(script);
			if(nextCommand === undefined) {
				break;
			}
			yield executeCommand(script, nextCommand);
		}
		script.running = false;
	})();
};
function getNextCommand(script) {
	return script.commands[script.executionOffset];
}
function executeCommand(script, command) {
	let deferred = Q.defer();
	commandQueue.push(command, function() {
		if(script.jumping) {
			script.jumping = false;
		}
		else {
			++script.executionOffset;
		}
		deferred.resolve();
		return 'done';
	});
	return deferred.promise;
}
exports.prototype.goTo = function(labelName) {
	if(this.running) {
		this.jumping = true;
	}
	let label = this.labels[labelName];
	if(label === undefined) {
		throw errorWithMetadata (
			new Error (
				"Non-existent label '" + labelName + "'"
			), {
				labelName,
			}
		);
	}
	if(label > this.commands.length) {
		this.executionOffset = this.commands.length;
	}
	else {
		this.executionOffset = label;
	}
};
exports.prototype.exit = function() {
	this.done = true;
};

{'fraction_non_alphanumeric': 0.15568581477139506, 'fraction_numerical': 0.0004689331770222743, 'mean_word_length': 4.12124849939976, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '1261642', 'n_tokens_mistral': 1535, 'n_tokens_neox': 1396, 'n_words': 271}