<commit_before>/* Redline Smalltalk, Copyright (c) <NAME>. All rights reserved. See LICENSE in the root of this distribution */
package st.redline;

import st.redline.bootstrap.ClassSubclassMethod;

import java.io.File;

public class Bootstrapper {

	private ProtoObject protoObject;
	private ProtoObject object;
	private ProtoObject collection;
	private ProtoObject sequenceableCollection;
	private ProtoObject arrayedCollection;
	private ProtoObject string;
	private ProtoObject symbol;

	protected Bootstrapper(ProtoObject protoObject) {
		this.protoObject = protoObject;
	}

	public void bootstrap() {
		markBootstrapping(true);
		mapPackages();
		registerRootClasses();
		instantiateSingletons();
		createClasses();
		markBootstrapping(false);
	}

	private void tearDownProtoObject() {
		protoObject.cls(null);
	}

	private void setupProtoObject() {
		protoObject.cls(protoObject);
		protoObject.methodAtPut("<", new ClassSubclassMethod());
	}

	private void markBootstrapping(boolean bootstrapping) {
		Primitives.bootstrapping = bootstrapping;
	}

	private void loadUsing(String name, SmalltalkClassLoader smalltalk) {
		try {
			smalltalk.findClass(name).newInstance();
		} catch (Exception e) {
			markBootstrapping(false);
			throw RedlineException.withCauseAndMessage(String.format("Unable to load class %s", name), e);
		}
	}

	private void instantiateSingletons() {
		ProtoObject.METACLASS_INSTANCE = createTemporaryMetaclassInstance();
	}

	private ProtoObject createTemporaryMetaclassInstance() {
		ProtoObject classClass = new ProtoObject();
		ProtoObject cls = new ProtoObject(classClass);
		cls.name("Metaclass(Bootstrapped)");
		return cls;
	}

	private void createClasses() {
		SmalltalkClassLoader smalltalk = currentClassLoader();
		setupProtoObject();
		loadUsing("st.redline.Object", smalltalk);
		tearDownProtoObject();
		loadUsing("st.redline.Collection", smalltalk);
	}

	private SmalltalkClassLoader currentClassLoader() {
		return (SmalltalkClassLoader) Thread.currentThread().getContextClassLoader();
	}

	private void registerRootClasses() {
		Primitives.registerAs(protoObject, "st.redline.ProtoObject");
	}

	private void mapPackages() {
		ProtoObject.packageMap.put("ProtoObject", "st.redline.ProtoObject");
		for (String sourceFile : SourceFileFinder.findIn("st" + File.separator + "redline")) {
			String packageName = ClassPathUtilities.filenameWithExtensionToPackageName(sourceFile);
			String name = ClassPathUtilities.filenameToClassName(sourceFile);
			ProtoObject.packageMap.put(name, packageName + "." + name);
		}
	}
}<commit_msg>Simplified bootstrapping, use more Smalltalk than Java.<commit_after>/* Redline Smalltalk, Copyright (c) <NAME>. All rights reserved. See LICENSE in the root of this distribution */
package st.redline;

import st.redline.bootstrap.ClassSubclassMethod;

import java.io.File;

public class Bootstrapper {

	private ProtoObject protoObject;
	private ProtoObject object;
	private ProtoObject collection;
	private ProtoObject sequenceableCollection;
	private ProtoObject arrayedCollection;
	private ProtoObject string;
	private ProtoObject symbol;

	protected Bootstrapper(ProtoObject protoObject) {
		this.protoObject = protoObject;
	}

	public void bootstrap() {
		markBootstrapping(true);
		mapPackages();
		registerRootClasses();
		instantiateSingletons();
		createClasses();
		makeClassSuperclassOfObjectsClass();
		markBootstrapping(false);
	}

	private void makeClassSuperclassOfObjectsClass() {
		// TODO.JCL - 
	}

	private void tearDownProtoObject() {
		protoObject.cls(null);
	}

	private void setupProtoObject() {
		protoObject.cls(protoObject);
		protoObject.methodAtPut("<", new ClassSubclassMethod());
	}

	private void markBootstrapping(boolean bootstrapping) {
		Primitives.bootstrapping = bootstrapping;
	}

	private void loadUsing(String name, SmalltalkClassLoader smalltalk) {
		try {
			smalltalk.findClass(name).newInstance();
		} catch (Exception e) {
			markBootstrapping(false);
			throw RedlineException.withCauseAndMessage(String.format("Unable to load class %s", name), e);
		}
	}

	private void instantiateSingletons() {
		ProtoObject.METACLASS_INSTANCE = createTemporaryMetaclassInstance();
	}

	private ProtoObject createTemporaryMetaclassInstance() {
		ProtoObject classClass = new ProtoObject();
		ProtoObject cls = new ProtoObject(classClass);
		cls.name("Metaclass(Bootstrapped)");
		return cls;
	}

	private void createClasses() {
		SmalltalkClassLoader smalltalk = currentClassLoader();
		setupProtoObject();
		loadUsing("st.redline.Object", smalltalk);
		tearDownProtoObject();
		loadUsing("st.redline.Symbol", smalltalk);
		loadUsing("st.redline.Class", smalltalk);
	}

	private SmalltalkClassLoader currentClassLoader() {
		return (SmalltalkClassLoader) Thread.currentThread().getContextClassLoader();
	}

	private void registerRootClasses() {
		Primitives.registerAs(protoObject, "st.redline.ProtoObject");
	}

	private void mapPackages() {
		ProtoObject.packageMap.put("ProtoObject", "st.redline.ProtoObject");
		for (String sourceFile : SourceFileFinder.findIn("st" + File.separator + "redline")) {
			String packageName = ClassPathUtilities.filenameWithExtensionToPackageName(sourceFile);
			String name = ClassPathUtilities.filenameToClassName(sourceFile);
			ProtoObject.packageMap.put(name, packageName + "." + name);
		}
	}
}
{'fraction_non_alphanumeric': 0.10314488955447398, 'fraction_numerical': 0.0, 'mean_word_length': 5.894193548387097, 'pattern_counts': {'":': 0, '<': 7, '<?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': '2755156', 'n_tokens_mistral': 1675, 'n_tokens_neox': 1554, 'n_words': 333}