{"id":18559223,"url":"https://github.com/linux-china/kotlin-wasm-demo","last_synced_at":"2025-04-10T02:30:42.474Z","repository":{"id":136595672,"uuid":"189659006","full_name":"linux-china/kotlin-wasm-demo","owner":"linux-china","description":"Kotlin WebAssembly Demo","archived":false,"fork":false,"pushed_at":"2019-11-26T12:53:52.000Z","size":100,"stargazers_count":15,"open_issues_count":0,"forks_count":2,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-02T02:51:10.887Z","etag":null,"topics":["kotlin","webassembly"],"latest_commit_sha":null,"homepage":"","language":"Kotlin","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/linux-china.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-05-31T21:11:52.000Z","updated_at":"2024-01-03T09:39:25.000Z","dependencies_parsed_at":null,"dependency_job_id":"0bd26c23-80e4-42dd-8ae2-fb655a80433e","html_url":"https://github.com/linux-china/kotlin-wasm-demo","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/linux-china%2Fkotlin-wasm-demo","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/linux-china%2Fkotlin-wasm-demo/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/linux-china%2Fkotlin-wasm-demo/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/linux-china%2Fkotlin-wasm-demo/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/linux-china","download_url":"https://codeload.github.com/linux-china/kotlin-wasm-demo/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248144158,"owners_count":21054876,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["kotlin","webassembly"],"created_at":"2024-11-06T21:42:20.139Z","updated_at":"2025-04-10T02:30:42.461Z","avatar_url":"https://github.com/linux-china.png","language":"Kotlin","funding_links":[],"categories":[],"sub_categories":[],"readme":"WebAssembly Demo with Kotlin\n============================\nWebAssembly demo with Kotlin Multiplatform.\n\n# Features\n\n* DOM operation: jsinterop for wasm32 and API is here https://kotlinlang.org/api/latest/jvm/stdlib/kotlinx.wasm.jsinterop/index.html\n* Canvas drawing\n* JQuery DSL\n\n# How to run\nPlease refer justfile.\n\n* Run build:  ./gradlew clean build\n* In IDEA, right click index.html and open in Chrome\n\n# Call customized JS functions from WebAssembly\n\n* Create js functions and call konan.libraries.push() to push them into libraries in xxx.wasm.js as following:\n```\n konan.libraries.push ({\n            knjs__alert: function(textPtr, textLen) {\n                  var text = toUTF16String(textPtr, textLen);\n                  window.alert(text)\n              }\n        });\n```\n\n* Then create Kotlin stubs for JS functions.\n```\nfun alert(text: String) {\n    knjs__alert(stringPointer(text), stringLengthBytes(text))\n}\n\n@SymbolName(\"knjs__alert\")\nexternal fun knjs__alert(textPtr: Int, textLen: Int): Unit\n\n```\n\n# Export WebAssembly functions for JS\n\n* Create a Kotlin function with @Retain annotation\n\n```\n/**\n * argument types:  int, double https://github.com/WebAssembly/design/blob/master/Semantics.md#types\n */\n@Retain\nfun hello() {\n    println(\"Hello from Kotlin\")\n}\n```\n* Add global exports from WebAssembly in xxx.wasm.js\n\n```javascript\nvar konan = { libraries: [], exports:{} };\n```\n\n* Bind the exported functions in invokeModule to konan.exports\n\n```\n\nfor(name in instance.exports) {\n           if(name.startsWith(\"kfun:\"))  {\n               fn_name = name.substring(5,name.indexOf(\"(\"));\n               konan.exports[fn_name] = instance.exports[name];\n           }\n        }\n\n```\n\n* Invoke js function\n\n```\nkonan.exports.hello();\n```\n\n\n### JS communicated with Kotlin WebAssembly(wasm32)\n\n* JS to refer Kotlin JsValue Object:\n\n```\nfunction kotlinObject(arenaIndex, objectIndex)\n```\n\n* Kotlin refer JS Object:  JsValue(arena,object)\n* Data Type: Int, Double(twoIntsToDouble), String: stringPointer(id), stringLengthBytes(id), JsString\n\n### JsString class\n\n```\n//js\nKonan_js_getStringChar: function (arenaIndex, objIndex, charIndex) {\n        var arena = konan_dependencies.env.arenas.get(arenaIndex);\n        var value = arena[objIndex].charCodeAt(charIndex);\n        return value;\n    }\n\n//kotlin\n\n@SymbolName(\"Konan_js_getStringChar\")\nexternal fun getStringChar(arena: Arena, obj: Object, index: Int ): Int\n```\n\n# References\n\n* WebAssembly Home: https://webassembly.org/\n* Web APIs: https://developer.mozilla.org/en-US/docs/Web/API\n* Kotlin Multiplatform Programming: https://kotlinlang.org/docs/reference/multiplatform.html\n* WebAssembly Specifications: https://webassembly.github.io/spec/\n* wabt: The WebAssembly Binary Toolkit https://github.com/WebAssembly/wabt\n* Awesome WebAssembly: https://github.com/mbasso/awesome-wasm\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flinux-china%2Fkotlin-wasm-demo","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flinux-china%2Fkotlin-wasm-demo","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flinux-china%2Fkotlin-wasm-demo/lists"}