ꝏ - Encode JavaScript with only one symbol

ꝏ ("long-o") is a JavaScript implementation of the esoteric programming language Len(language,encoding) using the 7-bit ASCII encoding. The code to execute only contains a long sequence of the character o. The length is converted to binary, split into 7-bit groups, encoded as ASCII text and evaluated.

Created by Martin Kleppe aka @aemkei.

Note: The resulting code will be very (very!) long even for short programs. ECMAScript 2016 established a maximum string length of 2**53 - 1 elements. In Chrome and Firefox strings currently have a maximum lengthof 2**30 (~1GB).

Example

  1. 594985
  2. 10010001010000101001
  3. ['0100100', '0101000', '0101001']
  4. [36, 40, 41 ]
  5. ['$', '(', ')']
  6. '$()'

Here is the code that will run the call $(). The encoded output has a length of 594,985 characters. Click here to run the code or view the full source.

// example function that we want to call
const $ = function(){ alert('works'); }

with (())
// encoded `$()`
ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo
ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo
ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo
ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo
ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo
ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo ...
// the statement above has a length of 594,985 characters

function (){
  
  function decode(name) {
  
    // get length of string
    const length = name.length;
    // > 594985

    // convert length to binary
    const binary = length.toString(2);
    // > '10010001010000101001'

    // get 7 bits per character
    const size7 = Math.ceil(binary.length / 7) * 7;
    const binary7 = binary.padStart(size7, '0');
    const pairs = binary7.match(/(\d{7})/g);
    // > [ '0100100', '0101000', '0101001' ]

    // convert binary to ascii
    const ascii = pairs.map(b => parseInt(b, 2));
    // > [ 36, 40, 41 ]

    // convert character codes to chars
    const chars = ascii.map(c => String.fromCharCode(c));
    // > [ '$', '(', ')' ]

    // join characters to code
    const code = chars.join('');
    // > '$()'

    // execute the code
    eval(code);
  }

  // handle undefined expressions
  return new Proxy({}, {
    has (target, name) {
      decode(name);
      return true;
    }
  });
}


Encoder

The following function will convert the given code into a (very large) number:

function encode(code) {
  const binary = code.split('').map(c => c
    .charCodeAt(0)           // ASCII
    .toString(2)             // binary
    .padStart(7, 0)          // padded
  ).join('');

  const bigInt = BigInt('0b' + binary);
  return bigInt.toString();  // length
}

20XX - Martin Kleppe