<commit_before><?php

namespace o;

class ModuleManager {
	
	static $fileToNameSpace = [];
    static $moduleRegistry = [];

    static function init() {
    	self::initAutoloading();
		LibModules::load();
    }

    static function isStdLib ($lib) {
        return LibModules::isa($lib);
    }

    static function getNamespace($relPath) {
    	$relPath = Tht::getRelativePath('app', $relPath);
        if (!isset(self::$fileToNameSpace[$relPath])) {
        	self::$fileToNameSpace[$relPath] = self::pathToNamespace($relPath);  
        }
        return self::$fileToNameSpace[$relPath];
    }

    static function pathToNamespace($path) {
    	$relPath = Tht::getRelativePath('app', $path);
    	$ns = $relPath;
		$ns = str_replace('/', '\\', $ns);
    	$ns = str_replace('.tht', '', $ns);
    	$ns = 'tht\\' . $ns;
    	return $ns;
    }

    static function namespaceToModulePath($ns) {
    	$path = $ns;
    	$path = preg_replace('#.*tht\\\\modules\\\\#', '', $path);
    	$path = str_replace('\\', '/', $path);
    	$parts = explode('/', $path);
    	$mod = array_pop($parts);

    	// remove redundant filename in namespace
    	array_pop($parts);

    	$mod = ucfirst(unu_($mod));
    	$path = implode('/', $parts);
    	$path .= '/' . $mod;

    	return $path;
    }

    static function getNamespacedPackage ($relPath) {
        // todo: handle subdirs
        return '\\' . self::getNamespace('modules/' . $relPath) . '\\' . u_($relPath);
    }   

	static function registerUserModule ($file, $nameSpace) {
        $relPath = Tht::getRelativePath('app', $file);
        self::$fileToNameSpace[$relPath] = $nameSpace;
        self::registerModule($nameSpace, $relPath);
    }

    static function registerStdModule ($name, $obj = -1) {
        self::$moduleRegistry[$name] = $obj;
    }

    static function registerModule ($nameSpace, $path) {
        self::$moduleRegistry[$path] = new OModule ($nameSpace, $path);
    }

    static function getModuleFromNamespace($ns) {
        $parts = explode('\\', $ns);
        $name = array_pop($parts);
        return self::getModule($name);
    }

    static function getModule ($modName) {

        // Already loaded user module
        $cacheKey = 'u//' . $modName;
        if (isset(self::$moduleRegistry[$cacheKey])) {
            return self::$moduleRegistry[$cacheKey];
        }
        // Built-in module
        else if (isset(self::$moduleRegistry[$modName])) {
            return self::loadBuiltinModule($modName);
        } 
        
        // User module
        return self::loadUserModule($modName);
    }

    static function loadBuiltinModule($modName) {
    	if (self::$moduleRegistry[$modName] === -1) {
            // triggers auto-load
            $modClass = '\\o\\u_' . $modName;
            self::$moduleRegistry[$modName] = new $modClass ();
        }
        return self::$moduleRegistry[$modName];
    }

    // Entry point for `import()`
    static function loadUserModule($relPath) {

        $fullPath = Tht::path('modules', $relPath . '.' . Tht::getExt());
        Source::process($fullPath);

        $relPath = Tht::getRelativePath('app', $fullPath);
        if (!isset(self::$moduleRegistry[$relPath])) {
            Tht::error("Can't find module for `$relPath`");
        }

        // Create local alias
        $baseName = basename($relPath, '.' . Tht::getExt());
        $cacheKey = 'u//' . $baseName;
        self::$moduleRegistry[$cacheKey] = self::$moduleRegistry[$relPath];

        return self::$moduleRegistry[$relPath];
    }

    // Entry point for `new Object ()`
    static function newObject($className, $args) {
        $mod = self::getModule($className);
        return $mod->newObject($className, $args);   
    }

    static function initAutoloading() {

		// Lazy Load.  Saves ~250kb
		spl_autoload_register(function ($aclass) {

		    $class = str_replace('o\\u_', '', $aclass);

		    if (LibModules::isa($class)) {

		        if ($class !== 'Perf') { Tht::module('Perf')->u_start('tht.loadModule', $class); }

		        if ($class == 'System') {
		            $class = 'SystemX';
		        }
		        Tht::loadLib('../modules/' . $class . '.php');

		        if ($class !== 'Perf') { Tht::module('Perf')->u_stop(); }

		    } else if (strpos($aclass, 'tht\\') === 0) {

		    	self::loadUserModule(self::namespaceToModulePath($aclass));

		    } else {

		        Tht::error("Can not autoload PHP class: `$aclass`");
		    }

		});
	}

}

<commit_msg>workaround PHP reserved words in namespaces, allow autoloading for PHP interop<commit_after><?php

namespace o;

class ModuleManager {
	
	static $fileToNameSpace = [];
    static $moduleRegistry = [];

    static function init() {
    	self::initAutoloading();
		LibModules::load();
    }

    static function isStdLib ($lib) {
        return LibModules::isa($lib);
    }

    static function getNamespace($relPath) {
    	$relPath = Tht::getRelativePath('app', $relPath);
        if (!isset(self::$fileToNameSpace[$relPath])) {
        	self::$fileToNameSpace[$relPath] = self::pathToNamespace($relPath);  
        }
        return self::$fileToNameSpace[$relPath];
    }

    static function pathToNamespace($path) {
    	$relPath = Tht::getRelativePath('app', $path);
    	$ns = $relPath;
		$ns = str_replace('/', '\\', $ns);
    	$ns = str_replace('.tht', '', $ns);
    	$ns = 'tht\\' . $ns . '_x'; // work around reserved PHP words
    	return $ns;
    }

    static function namespaceToModulePath($ns) {
    	$path = $ns;
        $path = str_replace('_x', '', $path);
    	$path = preg_replace('#.*tht\\\\modules\\\\#', '', $path);
    	$path = str_replace('\\', '/', $path);
    	$parts = explode('/', $path);
    	$mod = array_pop($parts);

    	// remove redundant filename in namespace
    	array_pop($parts);

    	$mod = ucfirst(unu_($mod));
    	$path = implode('/', $parts);
    	$path .= '/' . $mod;

    	return $path;
    }

    static function getNamespacedPackage ($relPath) {
        // todo: handle subdirs
        return '\\' . self::getNamespace('modules/' . $relPath) . '\\' . u_($relPath);
    }   

	static function registerUserModule ($file, $nameSpace) {
        $relPath = Tht::getRelativePath('app', $file);
        self::$fileToNameSpace[$relPath] = $nameSpace;
        self::registerModule($nameSpace, $relPath);
    }

    static function registerStdModule ($name, $obj = -1) {
        self::$moduleRegistry[$name] = $obj;
    }

    static function registerModule ($nameSpace, $path) {
        self::$moduleRegistry[$path] = new OModule ($nameSpace, $path);
    }

    static function getModuleFromNamespace($ns) {
        $parts = explode('\\', $ns);
        $name = array_pop($parts);
        return self::getModule($name);
    }

    static function getModule ($modName) {

        $modName = preg_replace('/_x$/', '', $modName);

        // Already loaded user module
        $cacheKey = 'u//' . $modName;
        if (isset(self::$moduleRegistry[$cacheKey])) {
            return self::$moduleRegistry[$cacheKey];
        }
        // Built-in module
        else if (isset(self::$moduleRegistry[$modName])) {
            return self::loadBuiltinModule($modName);
        } 
        
        // User module
        return self::loadUserModule($modName);
    }

    static function loadBuiltinModule($modName) {
    	if (self::$moduleRegistry[$modName] === -1) {
            // triggers auto-load
            $modClass = '\\o\\u_' . $modName;
            self::$moduleRegistry[$modName] = new $modClass ();
        }
        return self::$moduleRegistry[$modName];
    }

    // Entry point for `import()`
    static function loadUserModule($relPath) {

        $fullPath = Tht::path('modules', $relPath . '.' . Tht::getExt());
        Source::process($fullPath);

        $relPath = Tht::getRelativePath('app', $fullPath);
        if (!isset(self::$moduleRegistry[$relPath])) {
            Tht::error("Can't find module for `$relPath`");
        }

        // Create local alias
        $baseName = basename($relPath, '.' . Tht::getExt());
        $cacheKey = 'u//' . $baseName;
        self::$moduleRegistry[$cacheKey] = self::$moduleRegistry[$relPath];

        return self::$moduleRegistry[$relPath];
    }

    // Entry point for `new Object ()`
    static function newObject($className, $args) {
        $mod = self::getModule($className);
        return $mod->newObject($className, $args);   
    }

    static function initAutoloading() {

		// Lazy Load.  Saves ~250kb
		spl_autoload_register(function ($aclass) {

		    $class = str_replace('o\\u_', '', $aclass);

		    if (LibModules::isa($class)) {

		        if ($class !== 'Perf') { Tht::module('Perf')->u_start('tht.loadModule', $class); }

		        if ($class == 'System') {
		            $class = 'SystemX';
		        }
		        Tht::loadLib('../modules/' . $class . '.php');

		        if ($class !== 'Perf') { Tht::module('Perf')->u_stop(); }

		    } else if (strpos($aclass, 'tht\\') === 0) {

		    	self::loadUserModule(self::namespaceToModulePath($aclass));

		    } else {
		        // Tht::error("Can not autoload PHP class: `$aclass`");
                // UPDATE: Allow pass through for PHP intrerop 
		    }

		});
	}

}


{'fraction_non_alphanumeric': 0.1783813145029875, 'fraction_numerical': 0.0013036393264530147, 'mean_word_length': 2.5083841463414633, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, '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': '6791118', 'n_tokens_mistral': 2987, 'n_tokens_neox': 2859, 'n_words': 565}