{"id":19098881,"url":"https://github.com/ddliu/motto","last_synced_at":"2025-08-21T06:30:58.675Z","repository":{"id":57479893,"uuid":"20996150","full_name":"ddliu/motto","owner":"ddliu","description":"Nodejs module environment in golang(based on otto)","archived":false,"fork":false,"pushed_at":"2020-03-08T03:41:19.000Z","size":51,"stargazers_count":94,"open_issues_count":8,"forks_count":25,"subscribers_count":6,"default_branch":"master","last_synced_at":"2024-12-11T12:23:59.328Z","etag":null,"topics":["golang","javascript","nodejs","npm","otto"],"latest_commit_sha":null,"homepage":"","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/ddliu.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":"2014-06-19T10:10:17.000Z","updated_at":"2024-11-30T05:45:58.000Z","dependencies_parsed_at":"2022-09-26T17:41:33.128Z","dependency_job_id":null,"html_url":"https://github.com/ddliu/motto","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ddliu%2Fmotto","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ddliu%2Fmotto/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ddliu%2Fmotto/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ddliu%2Fmotto/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ddliu","download_url":"https://codeload.github.com/ddliu/motto/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":230494921,"owners_count":18235046,"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":["golang","javascript","nodejs","npm","otto"],"created_at":"2024-11-09T03:47:51.031Z","updated_at":"2024-12-19T20:08:24.218Z","avatar_url":"https://github.com/ddliu.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# motto\n\n[![Build Status](https://travis-ci.org/ddliu/motto.png)](https://travis-ci.org/ddliu/motto)\n\nModular [otto](https://github.com/robertkrimen/otto)\n\nMotto provide a Nodejs like module environment to run javascript files in golang.\n\n## Installation\n\n```bash\ngo get github.com/ddliu/motto\n```\n\n## Usage\n\n```js\nvar _ = require('underscore');\nvar data = require('./data.json'); // [3, 2, 1, 4, 6]\nmodule.exports = _.min(data);\n```\n\n```go\npackage main\n\nimport (\n    \"github.com/ddliu/motto\"\n    _ \"github.com/ddliu/motto/underscore\"\n)\n\nfunc main() {\n    motto.Run(\"path/to/index.js\")\n}\n```\n\nYou can also install the motto command line tool to run it directly:\n\n```bash\ngo install github.com/ddliu/motto/motto\nmotto path/to/index.js\n```\n\n## Modules\n\nThe module environment is almost capable with Nodejs [spec](http://nodejs.org/api/modules.html).\n\nSome Nodejs modules(without core module dependencies) can be used directly in Motto.\n\n## Addons\n\nMotto can be extended with addons, below is an example addon which implement part of the \"fs\" module of Nodejs:\n\n```go\npackage fs\n\nimport (\n    \"github.com/ddliu/motto\"\n    \"github.com/robertkrimen/otto\"\n)\n\nfunc fsModuleLoader(vm *motto.Motto) (otto.Value, error) {\n    fs, _ := vm.Object(`({})`)\n    fs.Set(\"readFileSync\", func(call otto.FunctionCall) otto.Value {\n        filename, _ := call.Argument(0).ToString()\n        bytes, err := ioutil.ReadFile(filename)\n        if err != nil {\n            return otto.UndefinedValue()\n        }\n\n        v, _ := call.Otto.ToValue(string(bytes))\n        return v\n    })\n\n    return vm.ToValue(fs)\n}\n\nfunc init() {\n    motto.AddModule(\"fs\", fsModuleLoader)\n}\n```\n\nAfter import this package, you can `require` it directly in your js file: \n\n```js\nvar fs = require('fs');\nvar content = fs.readFileSync('/path/to/data');\n```\n\n## Nodejs in Golang?\n\n[nodego](https://github.com/ddliu/nodego) implements some features and core modules\nof Nodejs.\n\n## Performance\n\nSimple benchmark shows below for furthur performance optimize:\n\n```bash\nstrace -c -Ttt motto tests/index.js\nstrace -c -Ttt node tests/index.js\n```\n\nMotto:\n\n```\n% time     seconds  usecs/call     calls    errors syscall\n------ ----------- ----------- --------- --------- ----------------\n 20.20    0.000144           2        59           rt_sigaction\n 15.71    0.000112           7        15           mmap\n 11.92    0.000085          11         8           futex\n 10.10    0.000072           6        13         4 stat\n  7.43    0.000053           7         8           read\n  5.89    0.000042          21         2           clone\n  5.75    0.000041          10         4           open\n  4.77    0.000034           7         5           rt_sigprocmask\n  4.63    0.000033          33         1           execve\n  3.23    0.000023          12         2           write\n  2.24    0.000016           4         4           fstat\n  1.82    0.000013           3         4           close\n  1.82    0.000013          13         1           sched_getaffinity\n  1.68    0.000012          12         1           sched_yield\n  0.98    0.000007           7         1           munmap\n  0.98    0.000007           7         1           arch_prctl\n  0.42    0.000003           3         1           getcwd\n  0.42    0.000003           3         1           sigaltstack\n------ ----------- ----------- --------- --------- ----------------\n100.00    0.000713                   131         4 total\n```\n\nNodejs:\n\n```\n% time     seconds  usecs/call     calls    errors syscall\n------ ----------- ----------- --------- --------- ----------------\n 20.15    0.000636           7        92           mmap\n 17.78    0.000561          16        36           munmap\n 13.97    0.000441          18        24           read\n  7.73    0.000244           7        35           mprotect\n  7.70    0.000243          15        16           brk\n  7.32    0.000231           7        34         1 futex\n  4.56    0.000144           7        22           open\n  3.61    0.000114           5        22        15 ioctl\n  2.15    0.000068           3        21           close\n  2.03    0.000064           3        21           fstat\n  2.00    0.000063           5        14        14 access\n  1.58    0.000050           4        12           lstat\n  1.24    0.000039           8         5           write\n  1.24    0.000039           6         7           rt_sigaction\n  1.05    0.000033           6         6         2 stat\n  0.89    0.000028          28         1           readlink\n  0.76    0.000024           5         5           rt_sigprocmask\n  0.70    0.000022           6         4           getcwd\n  0.67    0.000021          11         2           pipe2\n  0.63    0.000020          20         1           clone\n  0.38    0.000012           6         2           getrlimit\n  0.29    0.000009           9         1           epoll_create1\n  0.25    0.000008           8         1           eventfd2\n  0.22    0.000007           7         1           clock_gettime\n  0.22    0.000007           4         2           epoll_ctl\n  0.19    0.000006           6         1           gettid\n  0.19    0.000006           6         1           set_tid_address\n  0.19    0.000006           6         1           set_robust_list\n  0.13    0.000004           4         1           execve\n  0.10    0.000003           3         1           arch_prctl\n  0.10    0.000003           3         1           epoll_wait\n------ ----------- ----------- --------- --------- ----------------\n100.00    0.003156                   393        32 total\n```\n\n## Changelog\n\n### v0.1.0 (2014-06-22)\n\nInitial release\n\n### v0.2.0 (2014-06-24)\n\nMake module capable with Nodejs\n\n### v0.3.0 (2014-06-26)\n\nRewrite module.\n\nMake it easier to write addons.\n\nAdd underscore addon as an example.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fddliu%2Fmotto","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fddliu%2Fmotto","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fddliu%2Fmotto/lists"}