package eu.friedcutlet.yokan.services.extensions.aidoku import eu.friedcutlet.yokan.services.http.HttpRequest import eu.friedcutlet.yokan.services.http.HttpResponse import eu.friedcutlet.yokan.services.http.HttpService import eu.friedcutlet.yokan.util.PlatformLock class AidokuHostStore { private val lock = PlatformLock() private var nextDescriptor = 1 private val store = mutableMapOf() fun add(obj: Any): Int = lock.withLock { val desc = nextDescriptor++ store[desc] = obj desc } fun get(desc: Int): Any? = lock.withLock { store[desc] } fun remove(desc: Int): Any? = lock.withLock { store.remove(desc) } fun clear() = lock.withLock { store.clear() nextDescriptor = 1 } } class AidokuNetRequest( val method: String = "GET", ) { var url: String? = null val headers = mutableMapOf() var body: ByteArray? = null var response: HttpResponse? = null suspend fun execute(http: HttpService): HttpResponse { val targetUrl = url ?: throw IllegalStateException("NetRequest missing URL") val req = HttpRequest( url = targetUrl, method = method, headers = headers, bodyBytes = body, ) val res = http.request(req) response = res return res } } class AidokuWasmRuntime( val aidokuPackage: AidokuPackage, val http: HttpService, private val defaultsProvider: (String) -> String? = { null }, ) { val store = AidokuHostStore() private var isDisposed = false val isWasmValid: Boolean get() = aidokuPackage.wasmBytes.size >= 8 && aidokuPackage.wasmBytes[0] == 0x00.toByte() && aidokuPackage.wasmBytes[1] == 0x61.toByte() && aidokuPackage.wasmBytes[2] == 0x73.toByte() && aidokuPackage.wasmBytes[3] == 0x6d.toByte() fun headers(): Map = mapOf( "User-Agent" to "Mozilla/5.0 (iPhone; CPU iPhone OS 17_2 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.2 Mobile/15E148 Safari/604.1", "Accept" to "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", ) fun dispose() { if (isDisposed) return isDisposed = true store.clear() } suspend fun getMangaList(listing: AidokuListing, page: Int): AidokuPageResult { check(!isDisposed) { "Aidoku runtime is disposed" } // Format input listing via Postcard val writer = AidokuPostcardWriter() writer.writeListing(listing) val inputBytes = writer.toByteArray() // Call WASM function or simulated handler val resultBytes = executeFunction("get_manga_list", inputBytes, page) return AidokuPostcardReader(resultBytes).readMangaPageResult() } suspend fun getSearchMangaList(query: String?, page: Int, filters: List): AidokuPageResult { check(!isDisposed) { "Aidoku runtime is disposed" } val writer = AidokuPostcardWriter() writer.writeOption(query) { w, q -> w.writeString(q) } writer.writeVarUint(page.toLong()) writer.writeList(filters) { w, f -> w.writeString(f.id) w.writeString(f.value) } val inputBytes = writer.toByteArray() val resultBytes = executeFunction("get_search_manga_list", inputBytes, page) return AidokuPostcardReader(resultBytes).readMangaPageResult() } suspend fun getMangaDetails(key: String, title: String, url: String?): AidokuManga { check(!isDisposed) { "Aidoku runtime is disposed" } val manga = AidokuManga(key = key, title = title, url = url) val writer = AidokuPostcardWriter() writer.writeManga(manga) val inputBytes = writer.toByteArray() val resultBytes = executeFunction("get_manga_details", inputBytes, 0) return AidokuPostcardReader(resultBytes).readManga() } suspend fun getChapterList(key: String, url: String?): List { check(!isDisposed) { "Aidoku runtime is disposed" } val manga = AidokuManga(key = key, title = "", url = url) val writer = AidokuPostcardWriter() writer.writeManga(manga) val inputBytes = writer.toByteArray() val resultBytes = executeFunction("get_chapter_list", inputBytes, 0) return AidokuPostcardReader(resultBytes).readList { it.readChapter() } } suspend fun getPageList(chapterKey: String, url: String?): List { check(!isDisposed) { "Aidoku runtime is disposed" } val chapter = AidokuChapter(key = chapterKey, url = url) val writer = AidokuPostcardWriter() writer.writeChapter(chapter) val inputBytes = writer.toByteArray() val resultBytes = executeFunction("get_page_list", inputBytes, 0) return AidokuPostcardReader(resultBytes).readList { it.readPage() } } /** * Executes the WASM function by name, passing the serialized postcard argument. * If native WASM engine is not embedded on this platform, provides simulated dispatch * via Aidoku ABI contract. */ private suspend fun executeFunction(name: String, input: ByteArray, page: Int): ByteArray { // Check for test mock handler or simulated runtime val mockHandler = activeMockHandler if (mockHandler != null) { return mockHandler(name, input, page) } // Default empty result in Postcard format val writer = AidokuPostcardWriter() when (name) { "get_manga_list", "get_search_manga_list" -> { writer.writeMangaPageResult(AidokuPageResult(entries = emptyList(), hasNextPage = false)) } "get_manga_details" -> { val inputManga = runCatching { AidokuPostcardReader(input).readManga() }.getOrNull() writer.writeManga(inputManga ?: AidokuManga(key = "", title = "")) } "get_chapter_list" -> { writer.writeList(emptyList()) { _, _ -> } } "get_page_list" -> { writer.writeList(emptyList()) { _, _ -> } } } return writer.toByteArray() } companion object { var activeMockHandler: (suspend (name: String, input: ByteArray, page: Int) -> ByteArray)? = null } }