|
| 1 | +import JavaScriptEventLoop |
| 2 | +import JavaScriptKit |
| 3 | + |
| 4 | +// Simple full-text search service |
| 5 | +actor SearchService { |
| 6 | + struct Error: Swift.Error, CustomStringConvertible { |
| 7 | + let message: String |
| 8 | + |
| 9 | + var description: String { |
| 10 | + return self.message |
| 11 | + } |
| 12 | + } |
| 13 | + |
| 14 | + let serialExecutor: any SerialExecutor |
| 15 | + |
| 16 | + // Simple in-memory index: word -> positions |
| 17 | + var index: [String: [Int]] = [:] |
| 18 | + var originalContent: String = "" |
| 19 | + lazy var console: JSValue = { |
| 20 | + JSObject.global.console |
| 21 | + }() |
| 22 | + |
| 23 | + nonisolated var unownedExecutor: UnownedSerialExecutor { |
| 24 | + return self.serialExecutor.asUnownedSerialExecutor() |
| 25 | + } |
| 26 | + |
| 27 | + init(serialExecutor: any SerialExecutor) { |
| 28 | + self.serialExecutor = serialExecutor |
| 29 | + } |
| 30 | + |
| 31 | + // Utility function for fetch |
| 32 | + func fetch(_ url: String) -> JSPromise { |
| 33 | + let jsFetch = JSObject.global.fetch.function! |
| 34 | + return JSPromise(jsFetch(url).object!)! |
| 35 | + } |
| 36 | + |
| 37 | + func fetchAndIndex(url: String) async throws { |
| 38 | + let response = try await fetch(url).value() |
| 39 | + if response.status != 200 { |
| 40 | + throw Error(message: "Failed to fetch content") |
| 41 | + } |
| 42 | + let text = try await JSPromise(response.text().object!)!.value() |
| 43 | + let content = text.string! |
| 44 | + index(content) |
| 45 | + } |
| 46 | + |
| 47 | + func index(_ contents: String) { |
| 48 | + self.originalContent = contents |
| 49 | + self.index = [:] |
| 50 | + |
| 51 | + // Simple tokenization and indexing |
| 52 | + var position = 0 |
| 53 | + let words = contents.lowercased().split(whereSeparator: { !$0.isLetter && !$0.isNumber }) |
| 54 | + |
| 55 | + for word in words { |
| 56 | + let wordStr = String(word) |
| 57 | + if wordStr.count > 1 { // Skip single-character words |
| 58 | + if index[wordStr] == nil { |
| 59 | + index[wordStr] = [] |
| 60 | + } |
| 61 | + index[wordStr]?.append(position) |
| 62 | + } |
| 63 | + position += 1 |
| 64 | + } |
| 65 | + |
| 66 | + _ = console.log("Indexing complete with", index.count, "unique words") |
| 67 | + } |
| 68 | + |
| 69 | + func search(_ query: String) -> [SearchResult] { |
| 70 | + let queryWords = query.lowercased().split(whereSeparator: { !$0.isLetter && !$0.isNumber }) |
| 71 | + |
| 72 | + if queryWords.isEmpty { |
| 73 | + return [] |
| 74 | + } |
| 75 | + |
| 76 | + var results: [SearchResult] = [] |
| 77 | + |
| 78 | + // Start with the positions of the first query word |
| 79 | + guard let firstWord = queryWords.first, |
| 80 | + let firstWordPositions = index[String(firstWord)] |
| 81 | + else { |
| 82 | + return [] |
| 83 | + } |
| 84 | + |
| 85 | + for position in firstWordPositions { |
| 86 | + // Extract context around this position |
| 87 | + let words = originalContent.lowercased().split(whereSeparator: { |
| 88 | + !$0.isLetter && !$0.isNumber |
| 89 | + }) |
| 90 | + var contextWords: [String] = [] |
| 91 | + |
| 92 | + // Get words for context (5 words before, 10 words after) |
| 93 | + let contextStart = max(0, position - 5) |
| 94 | + let contextEnd = min(position + 10, words.count - 1) |
| 95 | + |
| 96 | + if contextStart <= contextEnd && contextStart < words.count { |
| 97 | + for i in contextStart...contextEnd { |
| 98 | + if i < words.count { |
| 99 | + contextWords.append(String(words[i])) |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + let context = contextWords.joined(separator: " ") |
| 105 | + results.append(SearchResult(position: position, context: context)) |
| 106 | + } |
| 107 | + |
| 108 | + return results |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +struct SearchResult { |
| 113 | + let position: Int |
| 114 | + let context: String |
| 115 | +} |
| 116 | + |
| 117 | +@MainActor |
| 118 | +final class App { |
| 119 | + private let document = JSObject.global.document |
| 120 | + private let alert = JSObject.global.alert.function! |
| 121 | + |
| 122 | + // UI elements |
| 123 | + private var container: JSValue |
| 124 | + private var urlInput: JSValue |
| 125 | + private var indexButton: JSValue |
| 126 | + private var searchInput: JSValue |
| 127 | + private var searchButton: JSValue |
| 128 | + private var statusElement: JSValue |
| 129 | + private var resultsElement: JSValue |
| 130 | + |
| 131 | + // Search service |
| 132 | + private let service: SearchService |
| 133 | + |
| 134 | + init(service: SearchService) { |
| 135 | + self.service = service |
| 136 | + container = document.getElementById("container") |
| 137 | + urlInput = document.getElementById("urlInput") |
| 138 | + indexButton = document.getElementById("indexButton") |
| 139 | + searchInput = document.getElementById("searchInput") |
| 140 | + searchButton = document.getElementById("searchButton") |
| 141 | + statusElement = document.getElementById("status") |
| 142 | + resultsElement = document.getElementById("results") |
| 143 | + setupEventHandlers() |
| 144 | + } |
| 145 | + |
| 146 | + private func setupEventHandlers() { |
| 147 | + indexButton.onclick = .object(JSClosure { [weak self] _ in |
| 148 | + guard let self else { return .undefined } |
| 149 | + self.performIndex() |
| 150 | + return .undefined |
| 151 | + }) |
| 152 | + |
| 153 | + searchButton.onclick = .object(JSClosure { [weak self] _ in |
| 154 | + guard let self else { return .undefined } |
| 155 | + self.performSearch() |
| 156 | + return .undefined |
| 157 | + }) |
| 158 | + } |
| 159 | + |
| 160 | + private func performIndex() { |
| 161 | + let url = urlInput.value.string! |
| 162 | + |
| 163 | + if url.isEmpty { |
| 164 | + alert("Please enter a URL") |
| 165 | + return |
| 166 | + } |
| 167 | + |
| 168 | + updateStatus("Downloading and indexing content...") |
| 169 | + |
| 170 | + Task { [weak self] in |
| 171 | + guard let self else { return } |
| 172 | + do { |
| 173 | + try await self.service.fetchAndIndex(url: url) |
| 174 | + await MainActor.run { |
| 175 | + self.updateStatus("Indexing complete!") |
| 176 | + } |
| 177 | + } catch { |
| 178 | + await MainActor.run { |
| 179 | + self.updateStatus("Error: \(error)") |
| 180 | + } |
| 181 | + } |
| 182 | + } |
| 183 | + } |
| 184 | + |
| 185 | + private func performSearch() { |
| 186 | + let query = searchInput.value.string! |
| 187 | + |
| 188 | + if query.isEmpty { |
| 189 | + alert("Please enter a search query") |
| 190 | + return |
| 191 | + } |
| 192 | + |
| 193 | + updateStatus("Searching...") |
| 194 | + |
| 195 | + Task { [weak self] in |
| 196 | + guard let self else { return } |
| 197 | + let searchResults = await self.service.search(query) |
| 198 | + await MainActor.run { |
| 199 | + self.displaySearchResults(searchResults) |
| 200 | + } |
| 201 | + } |
| 202 | + } |
| 203 | + |
| 204 | + private func updateStatus(_ message: String) { |
| 205 | + statusElement.innerText = .string(message) |
| 206 | + } |
| 207 | + |
| 208 | + private func displaySearchResults(_ results: [SearchResult]) { |
| 209 | + statusElement.innerText = .string("Search complete! Found \(results.count) results.") |
| 210 | + resultsElement.innerHTML = .string("") |
| 211 | + |
| 212 | + if results.isEmpty { |
| 213 | + var noResults = document.createElement("p") |
| 214 | + noResults.innerText = .string("No results found.") |
| 215 | + _ = resultsElement.appendChild(noResults) |
| 216 | + } else { |
| 217 | + // Display up to 10 results |
| 218 | + for (index, result) in results.prefix(10).enumerated() { |
| 219 | + var resultItem = document.createElement("div") |
| 220 | + resultItem.style = .string( |
| 221 | + "padding: 10px; margin: 5px 0; background: #f5f5f5; border-left: 3px solid blue;" |
| 222 | + ) |
| 223 | + resultItem.innerHTML = .string( |
| 224 | + "<strong>Result \(index + 1):</strong> \(result.context)") |
| 225 | + _ = resultsElement.appendChild(resultItem) |
| 226 | + } |
| 227 | + } |
| 228 | + } |
| 229 | +} |
| 230 | + |
| 231 | +@main struct Main { |
| 232 | + @MainActor static var app: App? |
| 233 | + |
| 234 | + static func main() { |
| 235 | + JavaScriptEventLoop.installGlobalExecutor() |
| 236 | + WebWorkerTaskExecutor.installGlobalExecutor() |
| 237 | + |
| 238 | + Task { |
| 239 | + // Create dedicated worker and search service |
| 240 | + let dedicatedWorker = try await WebWorkerDedicatedExecutor() |
| 241 | + let service = SearchService(serialExecutor: dedicatedWorker) |
| 242 | + app = App(service: service) |
| 243 | + } |
| 244 | + } |
| 245 | +} |
| 246 | + |
| 247 | +#if canImport(wasi_pthread) |
| 248 | + import wasi_pthread |
| 249 | + import WASILibc |
| 250 | + |
| 251 | + /// Trick to avoid blocking the main thread. pthread_mutex_lock function is used by |
| 252 | + /// the Swift concurrency runtime. |
| 253 | + @_cdecl("pthread_mutex_lock") |
| 254 | + func pthread_mutex_lock(_ mutex: UnsafeMutablePointer<pthread_mutex_t>) -> Int32 { |
| 255 | + // DO NOT BLOCK MAIN THREAD |
| 256 | + var ret: Int32 |
| 257 | + repeat { |
| 258 | + ret = pthread_mutex_trylock(mutex) |
| 259 | + } while ret == EBUSY |
| 260 | + return ret |
| 261 | + } |
| 262 | +#endif |
0 commit comments