| 63 | } |
| 64 | |
| 65 | ScriptContextType ScriptLoader::AddScriptFromFile(StringView path, const HashMap<String, bool>& definedSymbols) |
| 66 | { |
| 67 | String absolutePath = fs::GetAbsolutePath(path); |
| 68 | if (absolutePath.empty()) { |
| 69 | return ScriptContextType::Unknown; |
| 70 | } |
| 71 | |
| 72 | // Include each file only once |
| 73 | auto it = _includedFiles.find(absolutePath); |
| 74 | if (it != _includedFiles.end()) { |
| 75 | return ScriptContextType::AlreadyIncluded; |
| 76 | } |
| 77 | _includedFiles.emplace(absolutePath, true); |
| 78 | |
| 79 | auto s = fs::Open(absolutePath, FileAccess::Read); |
| 80 | if (s->GetSize() <= 0) { |
| 81 | return ScriptContextType::Unknown; |
| 82 | } |
| 83 | |
| 84 | String scriptContent(NoInit, s->GetSize()); |
| 85 | s->Read(scriptContent.data(), s->GetSize()); |
| 86 | s->Dispose(); |
| 87 | |
| 88 | ScriptContextType contextType = ScriptContextType::Legacy; |
| 89 | SmallVector<String, 4> metadata; |
| 90 | SmallVector<String, 0> includes; |
| 91 | String currentClass, currentNamespace, metadataName, metadataDeclaration; |
| 92 | std::int32_t scriptSize = (std::int32_t)scriptContent.size(); |
| 93 | |
| 94 | // First perform the checks for #if directives to exclude code that shouldn't be compiled |
| 95 | std::int32_t pos = 0; |
| 96 | std::int32_t nested = 0; |
| 97 | while (pos < scriptSize) { |
| 98 | std::uint32_t len = 0; |
| 99 | asETokenClass t = _engine->ParseToken(&scriptContent[pos], scriptSize - pos, &len); |
| 100 | if (t == asTC_UNKNOWN && scriptContent[pos] == '#' && (pos + 1 < scriptSize)) { |
| 101 | std::int32_t start = pos++; |
| 102 | |
| 103 | t = _engine->ParseToken(&scriptContent[pos], scriptSize - pos, &len); |
| 104 | |
| 105 | StringView token = scriptContent.slice(pos, pos + len); |
| 106 | pos += len; |
| 107 | |
| 108 | if (token == "if"_s) { |
| 109 | t = _engine->ParseToken(&scriptContent[pos], scriptSize - pos, &len); |
| 110 | if (t == asTC_WHITESPACE) { |
| 111 | pos += len; |
| 112 | t = _engine->ParseToken(&scriptContent[pos], scriptSize - pos, &len); |
| 113 | } |
| 114 | |
| 115 | if (t == asTC_IDENTIFIER) { |
| 116 | StringView word = scriptContent.slice(pos, pos + len); |
| 117 | pos += len; |
| 118 | |
| 119 | auto it = definedSymbols.find(String::nullTerminatedView(word)); |
| 120 | bool defined = (it != definedSymbols.end() && it->second); |
| 121 | |
| 122 | for (std::int32_t i = start; i < pos; i++) { |
nothing calls this directly
no test coverage detected