{"id":13659489,"url":"https://github.com/ialex32x/duktape-unity","last_synced_at":"2025-09-30T12:31:01.839Z","repository":{"id":47786882,"uuid":"169722627","full_name":"ialex32x/duktape-unity","owner":"ialex32x","description":"provide typescript support for unity dynamic scripting","archived":true,"fork":false,"pushed_at":"2022-12-07T18:20:49.000Z","size":220128,"stargazers_count":104,"open_issues_count":13,"forks_count":17,"subscribers_count":12,"default_branch":"master","last_synced_at":"2024-09-27T23:21:02.391Z","etag":null,"topics":["duktape-unity3d","typescript","typescript-unity3d","unity-javascript","unity-plugin","unity-scripts","unity-typescript"],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ialex32x.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-02-08T11:06:35.000Z","updated_at":"2023-10-16T02:41:50.000Z","dependencies_parsed_at":"2023-01-24T20:15:57.027Z","dependency_job_id":null,"html_url":"https://github.com/ialex32x/duktape-unity","commit_stats":null,"previous_names":[],"tags_count":32,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ialex32x%2Fduktape-unity","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ialex32x%2Fduktape-unity/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ialex32x%2Fduktape-unity/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ialex32x%2Fduktape-unity/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ialex32x","download_url":"https://codeload.github.com/ialex32x/duktape-unity/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":234737799,"owners_count":18879179,"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":["duktape-unity3d","typescript","typescript-unity3d","unity-javascript","unity-plugin","unity-scripts","unity-typescript"],"created_at":"2024-08-02T05:01:09.335Z","updated_at":"2025-09-30T12:30:51.827Z","avatar_url":"https://github.com/ialex32x.png","language":"JavaScript","funding_links":[],"categories":["JavaScript"],"sub_categories":[],"readme":"![logo](res/logo.png \"duktape-unity\")\r\n\r\n\u003e this duktape binding project will be discontinued. a quickjs binding version is under development, it supports more modern javascript features. [unity-jsb](https://github.com/ialex32x/unity-jsb)\r\n\r\n[中文说明](README_CN.md)\r\n\r\n[![Build Status](https://travis-ci.org/ialex32x/duktape-unity.svg?branch=master)](https://travis-ci.org/ialex32x/duktape-unity)\r\n\r\n# Brief\r\nIntegerate duktape (an embedded javascript engine) into unity, you can load and run javascript at runtime. \u003cbr/\u003e\r\nTypescript is a preferred choice, it provides type checks.\r\n\r\n![editing script](res/ts_editing_1.png \"so brilliant!\")\r\n\r\n# Features \r\n* nodejs-like module or browser-like singular js depends on you\r\n* generate C# to js type binding code, and coresponding d.ts type definition files\r\n* setTimeout/setInterval/clearTimeout/clearInterval compatible\r\n* c# delegates\r\n* optimized unity common valuetypes (Vector2/3,Quaternion,Color...)\r\n* fixed-point math support ([libfixmath](https://github.com/PetteriAimonen/libfixmath))\r\n* websocket ([libwebsockets](https://github.com/warmcat/libwebsockets))\r\n* iOS (64bit, bitcode)\r\n* Android (v7a, v8a, x86)\r\n* remote debugger (vscode)\r\n* promise (bluebird.js)\r\n* coroutine (duktape thread)\r\n* socket (tcp/udp)\r\n* kcp (not implemented)\r\n\r\nYou can use lots of pure js libraries in your project, such as protobufjs.\r\n![protobufjs](res/test_protobufjs.png)\r\n\r\n# Type definition files\r\nThe generated d.ts files will improve *auto-complete*. It will give the information of exposed types from *C#* and *C*.\r\n\r\n- delegate type information\r\n![type definition files](res/type_definition_1.png)\r\n- generic constraints for out/ref parameter\r\n![type definition files](res/type_definition_2.png)\r\n- friendly interface for AddComponent/GetComponent\r\n![type definition files](res/type_definition_3.png)\r\n\r\n# Environments\r\nIf you use typescript, install typescript at first\r\n```shell\r\nnpm install -g typescript\r\n```\r\n\r\n# Clone\r\n```shell\r\n# --depth=1 is highly recommended\r\ngit clone https://github.com/ialex32x/duktape-unity --depth=1\r\n```\r\n\r\n# Build\r\n## prebuilt libraries already in /prebuilt/debug and /prebuilt/release directories.\r\n\r\npython/pip/pyyaml is prerequisites.\r\n```shell\r\npip install pyyaml\r\n```\r\n\r\nduktape original source code is at /duktape-\u003cversion\u003e/src-input\r\n```shell\r\n./configure_duktape.bat # combined duktape source code will be generated \r\n# at:\r\n# /build/src-debug (with debugger)\r\n# /build/src-release (without debugger)\r\n./make_duktape_\u003cplatform\u003e.bat # or ./make_duktape_\u003cplatform\u003e.sh in osx\r\n```\r\nif you build duktape for android in windows, run make_duktape_android.bat in visual studio  cross tools commandline (e.g VS2015 x64 ARM Cross Tools Command Prompt).\u003cbr/\u003e\u003cbr/\u003e\r\n\r\n'./scratch' is a playground for duktape testing in a simple command line app.\r\n```shell\r\n./configure_duktape_scratch.bat\r\n./make_duktape_scratch.bat\r\n```\r\n\r\n# Sample code\r\n\r\n```ts\r\n\r\n// if your tsconfig.json defined {\"module\": \"commonjs\"}, you can 'import' modules like node-js. \r\n\r\n// import module\r\nimport { B } from \"base/b\"\r\n// import module with relative path (. or ..)\r\nimport { C } from \"./base/c\"\r\n\r\nclass MyPlayer {\r\n    Start() {\r\n        console.log(\"MyPlayer.Start\")\r\n        this.Jump()\r\n    }\r\n\r\n    // Update() {\r\n    // }\r\n\r\n    Jump() {\r\n        console.log(\"MyPlayer.Jump\")\r\n    }\r\n}\r\n\r\nexport class A {\r\n    private go: GameObject\r\n    constructor () {\r\n        this.go = new GameObject(\"test go\")\r\n        this.go.transform.localPosition = new Vector3(1, 2, 3) \r\n        // use Bridge to receive Enable/Disable/Update \r\n        // don't use the approach a lot, it's better to dispatch futher logic in a single Bridge\r\n        this.go.AddComponent(DuktapeJS.Bridge).SetBridge(new MyPlayer()) \r\n\r\n        let f = new Custom()\r\n        // you can assign function to c# delegate\r\n        f.onopen = function () {\r\n            // ...\r\n        }\r\n        // if you want to register multiple listener, use DuktapeJS.Dispather \r\n        // you can also use typed Dispatcher generated in _DuktapeDelegates.d.ts, it provides type checks\r\n        f.onload = new DuktapeJS.Dispatcher1\u003cvoid, string\u003e() \r\n        f.onload.on(this, this.onload)  // add listener\r\n        f.onload.off(this, this.onload) // remove listener\r\n        f.onload.off(this)              // clear all listeners of this\r\n        f.onload.clear()                // clear all\r\n\r\n        // 'out' parameter in c#\r\n        let v = {}\r\n        if (System.Int32.TryParse(\"123\", v)) {\r\n            console.log(v.target)\r\n        }\r\n    }\r\n\r\n    private onload(ev: string) {\r\n        let timer1 = setInterval(() =\u003e {\r\n            console.log(\"interval\")\r\n        }, 1000)\r\n\r\n        setTimeout((a, b) =\u003e {\r\n            console.log(\"timeout\", a, b)\r\n            clearInterval(timer1)\r\n        }, 5000, \"Arg1\", 123)\r\n    }\r\n\r\n    square() {\r\n        console.log(\"A.square\")\r\n    }\r\n}\r\n\r\n// websocket\r\nlet ws = DuktapeJS.WebSocket()\r\nws.on(\"open\", () =\u003e {\r\n    console.log(\"connected\")\r\n    setInterval(() =\u003e {\r\n        ws.send(\"hello, world\") // string or buffer \r\n    }, 1000)\r\n})\r\nws.on(\"data\", data =\u003e {\r\n    console.log(\"ws receive data\", data) // string or buffer (depends on websocket message type you use)\r\n})\r\nws.on(\"close\", () =\u003e {\r\n    console.log(\"connection lost\")\r\n})\r\nws.connect(\"ws://127.0.0.1:8080/echo\")\r\nsetInterval(() =\u003e {\r\n    ws.poll()\r\n}, 50)\r\n\r\n```\r\n\r\n```ts\r\n// http request example\r\nconsole.log(\"http requesting...\");\r\nHttpRequest.GET(\"http://t.weather.sojson.com/api/weather/city/101030100\", null, (status, res) =\u003e {\r\n    console.warn(\"http response:\", status, res);\r\n    if (status) {\r\n        let obj = JSON.parse(res);\r\n        console.log(\"as object\", obj.message);\r\n    }\r\n});\r\n```\r\n\r\n```ts\r\n// coroutine example\r\nlet co = new Coroutine(function (x) {\r\n    console.log(\"duktape thread, start:\", x);\r\n    for (var i = 1; i \u003c= 5; ++i) {\r\n        let r = Coroutine.yield(i);\r\n        console.log(\"duktape thread, yield:\", r);\r\n    }\r\n    // Coroutine.break();\r\n    return \"all done!\";\r\n});\r\nlet c = 'A'.charCodeAt(0);\r\nwhile (co.next(String.fromCharCode(c++))) {\r\n    console.log(\"duktape thread, next:\", co.value);\r\n}\r\nconsole.log(\"duktape thread, done:\", co.value);\r\n```\r\n\r\n# Dev status \r\nIt's not stable enough, do not use it in production environment.  \u003cbr/\u003e\r\nVector2/Matrix3x3/Matrix4x4/Quaternion valuetypes optimization is partially written in c, and not fully tested. \u003cbr/\u003e\r\n\r\n# Usage\r\n\r\nExecute menu item [Duktape -\u003e Generate Bindings] to generate binding code.\r\nTypescript source files will be compiled into js after your modification and switch back to Unity Editor.\r\n\r\n\r\n## How to customize exported types\r\n\r\n* duktape.json\r\nmodify the basic configuration at ./duktape.json (details in Assets/Duktape/Editor/Prefs.cs)\r\n```javascript\r\n{\r\n    \"outDir\": \"Assets/Generated\",\r\n    \"typescriptDir\": \"Assets/Generated\",\r\n    \"extraExt\": \"\",\r\n    // rootpath of ts/js project\r\n    \"workspace\": \"\",\r\n    \"logPath\": \"Temp/duktape.log\",\r\n    // auto, cr, lf, crlf\r\n    \"newLineStyle\": \"auto\",\r\n    \"implicitAssemblies\": [\r\n        \"UnityEngine\",\r\n        \"UnityEngine.CoreModule\", \r\n        \"UnityEngine.UI\", \r\n        \"UnityEngine.UIModule\"\r\n    ], \r\n    \"explicitAssemblies\": [\r\n        \"Assembly-CSharp\"\r\n    ],\r\n    // types in blacklist will not be exported\r\n    \"typePrefixBlacklist\": [\r\n        \"JetBrains.\",\r\n        \"Unity.Collections.\",\r\n        \"Unity.Jobs.\",\r\n        \"Unity.Profiling.\",\r\n        \"UnityEditor.\",\r\n        \"UnityEditorInternal.\",\r\n        \"UnityEngineInternal.\",\r\n        \"UnityEditor.Experimental.\",\r\n        \"UnityEngine.Experimental.\",\r\n        \"Unity.IO.LowLevel.\",\r\n        \"Unity.Burst.\",\r\n        // more types ...\r\n        \"UnityEngine.Assertions.\"\r\n    ], \r\n    \"ns\": \"DuktapeJS\",\r\n    \"tab\": \"    \"\r\n}\r\n```\r\n* implements Duktape.IBindingProcess interface or extends AbstractBindingProcess class\r\n```c#\r\npublic class MyCustomBinding : AbstractBindingProcess\r\n{\r\n    public override void OnPreCollectTypes(BindingManager bindingManager)\r\n    {\r\n        /*\r\n        bindingManager.AddExportedType(typeof(MyCustomClass));\r\n        bindingManager.TransformType(typeof(MyCustomClass))\r\n            .SetMethodBlocked(\"AMethodName\")\r\n            .SetMethodBlocked(\"AMethodNameWithParameters\", typeof(string), typeof(int))\r\n        */\r\n    }\r\n\r\n    public override void OnCleanup(BindingManager bindingManager)\r\n    {\r\n        Debug.Log($\"finish @ {DateTime.Now}\");\r\n    }\r\n}\r\n```\r\n\r\n## Sample scene\r\nAssets/Scenes/main.unity (Sample.cs) demonstrate the basic usage.\u003cbr/\u003e\r\n\r\n# Debugger\r\n\r\nSupport js/ts remote debug.\r\n\r\n## the libraries in unity project is built with debugger support. if you want to use a release version without debugger support, checkout /prebuilt/release. or compile them by yourself.\r\n\r\n![debugger](res/debugger.png)\r\n\r\n```yml\r\nName: Duktape Debugger\r\nId: haroldbrenes.duk-debug\r\nDescription: Debug Adapter for Duktape runtimes.\r\nVersion: 0.5.6\r\nPublisher: HaroldBrenes\r\nVS Marketplace Link: https://marketplace.visualstudio.com/items?itemName=HaroldBrenes.duk-debug\r\n```\r\nIf you are debugging ts sources, this extension has some path-resolve issues break the debug process, you can try [the modified version](https://github.com/ialex32x/vscode-duktape-debug/releases/download/v0.5.6ts/duk-debug-0.5.6.vsix).\r\n\r\n# Referenced libraries\r\n\r\n* [duktape](https://github.com/svaarala/duktape)\r\n* [slua](https://github.com/pangweiwei/slua)\r\n* [xLua](https://github.com/Tencent/xLua)\r\n* [typescript-for-unity](https://github.com/SpiralP/typescript-for-unity)\r\n* [godot](https://github.com/godotengine/godot)\r\n* [libwebsockets](https://github.com/warmcat/libwebsockets)\r\n* [mbedtls](https://github.com/ARMmbed/mbedtls)\r\n* [libfixmath](https://github.com/PetteriAimonen/libfixmath)\r\n* [bluebird.js](https://github.com/petkaantonov/bluebird)\r\n\r\n# Misc.\r\n\r\n* [vscode-duktape-debug](https://github.com/harold-b/vscode-duktape-debug)\r\n* [duktape-doc-debugger](https://github.com/svaarala/duktape/blob/master/doc/debugger.rst)\r\n\r\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fialex32x%2Fduktape-unity","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fialex32x%2Fduktape-unity","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fialex32x%2Fduktape-unity/lists"}