{"id":16847632,"url":"https://github.com/fabiosantoscode/dumbjs","last_synced_at":"2025-04-11T06:33:42.878Z","repository":{"id":52146303,"uuid":"43595486","full_name":"fabiosantoscode/dumbjs","owner":"fabiosantoscode","description":"A first pass for js2cpp. Uses browserify's dependencies to flatten the dependency tree into a single file, then makes the file not use closures at all by implementing closures in pure javascript.","archived":false,"fork":false,"pushed_at":"2023-01-11T00:47:54.000Z","size":362,"stargazers_count":7,"open_issues_count":10,"forks_count":2,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-25T04:27:18.991Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","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/fabiosantoscode.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}},"created_at":"2015-10-03T10:26:43.000Z","updated_at":"2023-03-12T11:53:05.000Z","dependencies_parsed_at":"2023-02-08T21:00:22.158Z","dependency_job_id":null,"html_url":"https://github.com/fabiosantoscode/dumbjs","commit_stats":null,"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fabiosantoscode%2Fdumbjs","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fabiosantoscode%2Fdumbjs/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fabiosantoscode%2Fdumbjs/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fabiosantoscode%2Fdumbjs/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/fabiosantoscode","download_url":"https://codeload.github.com/fabiosantoscode/dumbjs/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248357422,"owners_count":21090398,"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":[],"created_at":"2024-10-13T13:08:35.506Z","updated_at":"2025-04-11T06:33:42.827Z","avatar_url":"https://github.com/fabiosantoscode.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# dumbjs\n\n[![Build Status](https://travis-ci.org/fabiosantoscode/dumbjs.svg?branch=master)](https://travis-ci.org/fabiosantoscode/dumbjs)\n\nWIP: Parts of this repo don't work properly yet. If something is crashing dumbjs and it doesn't seem like an intentional dumbjs-originated limitation, file an issue!\n\nA first pass for js2cpp. Uses browserify's dependencies to flatten the dependency tree into a single file, then makes the file not use closures at all by implementing closures in pure javascript.\n\nThis was created because I found it too hard to implement closures in js2cpp, then it became clear that it would be much better to implement them at the javascript level, and make the current js2cpp just work on a simple subset of javascript.\n\nSo I made dumbjs. It turns javascript into a simpler subset of itself. The most important transformations:\n\n * Make the compiled javascript work in an environment without closures\n * Flatten the dependency tree like browserify does, turning it into a single file so js2cpp doesn't need to care about more than one file.\n * Without any name collisions, unwrap functions so as to leave no nested function.\n * Separate definitions from declarations, so that var x = 3 in the global scope becomes var x; x = 3, then put every statement that performs any action (assignment or function call) in the global scope, in order, in a single function called \"main\", whose last statement will be \"return 0\".\n\n\n# How to install and run\n\nInstallation is simply `npm install dumbjs -g`\n\nRunning it is as easy as `dumbjs \u003c input.js \u003e output.dumb.js`\n\nThere are currently no command line options :(\n\n\n# API docs\n\n## `require('dumbify')(javascriptCode, [options])`\n\nDumbify a javascript string and return the resulting javascript code as a string.\n\n## `require('dumbify').dumbifyAST(javascriptAST, [options])`\n\nDumbify a parsed javascript AST as returned by `esprima`, `acorn` or whatever parser you prefer.\n\n## `options`\n\nThe options you can pass are written down in dumbifyAST, which is a pretty straightforward function in the `index` file.\n\nThey turn several passes on and off, and these passes are not documented as of yet :P\n\n\n# What do I need to run dumbified code?\n\nSo you want to transplile javascript to something huh? That sounds like fun!\n\nYou have to implement some functions in your target environment!\n\n\n## BIND(func, closure) -\u003e functionBoundToClosure\n\nIn plain javascript: `function BIND(func,closure){return func.bind(null, closure)}`\n\nThis function takes a function and an object, and returns a function that takes that object as the first argument. Simple right? A single-argument curry. This is used to pass closures in environments that don't have them.\n\nFor js2cpp, for example, I had to turn every function that `BIND` was called on (these functions take `_closure` as the first argument so they're easy to find), into a callable class, and turn all `BIND` calls into `new ThatCallableClass(theClosure)`.\n\n\n## JS_ADD(a, b) -\u003e Number|String\n\nIn plain javascript: `function JS_ADD(a, b) { return a + b }`\n\nWhen trying to do type conversions, if the type of `a` and/or `b` is not known at compile time, `a + b` turns into `JS_ADD(a, b)`. This is because it is not clear whether to concatenate `a` and `b` as strings ( `String(a) + String(b)`) or to convert them to numbers and add their values ( `Number(a) + Number(b)` ), as this depends on dumbjs being privvy of both types!\n\nThey can be stactically removed if there is enough knowledge about the types (maybe dumbjs didn't know of some function's existence), or you can use the `typeof` operator's equivalent to know what kind of variable it is, and perform the addition or concatenation.\n\nThis is specified in great detail in the [spec](https://tc39.github.io/ecma262/#sec-addition-operator-plus-runtime-semantics-evaluation). Make sure to read into the toPrimitive part, it becomes super unpredictable if the object has a toString or valueOf method. They are both used, and they don't need to return strings ;)\n\n# Recommended reading\n\n[This document](http://dspace.mit.edu/bitstream/handle/1721.1/5854/AIM-199.pdf) has been invaluable in understanding the difficulties and nuances of implementing closures, and describes complicated problems in a way that's easily understandable.\n\n[This wikipedia article](https://en.wikipedia.org/wiki/Funarg_problem) describes the main problem of implementing closures, and why you can't store functions and their closures on the stack if you want functions in your language to be first-class.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffabiosantoscode%2Fdumbjs","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffabiosantoscode%2Fdumbjs","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffabiosantoscode%2Fdumbjs/lists"}