<commit_before>
import R from 'ramda';
import transform from './transformer';

const joinLines = R.join('\n');
const joinLineBlocks = R.join('\n\n');

function transpileImpl(impl) {
  const items = R.compose(
    joinLineBlocks,
    R.values,
    R.mapObjIndexed(
      (code, implId) => {
        const itemRef = `impl['${implId}']`;
        return joinLines([
          '// ---------------------------------------------------------------------',
          `${itemRef} = {};`,
          `(function(module, exports) {${code}})({exports: ${itemRef}}, ${itemRef});`,
        ]);
      }
    )
  );

  return joinLineBlocks([
    'var impl = {};',
    items(impl),
  ]);
}

function transpileNodes(nodes) {
  // __IMPLREF__ is a voodoo to work around JSON safety. For function references
  // we should actually refer to `impl` global variable. JSON.stringify would
  // enclose it in quoutes. So we place these special tokens to unquote the values
  // and refer to actual `impl` with a regex find/replace in an additional step.
  const injectFuncRefs = node => R.merge(node, {
    setup: `__IMPLREF__setup#${node.implId}__`,
    upkeep: `__IMPLREF__upkeep#${node.implId}__`,
    evaluate: `__IMPLREF__evaluate#${node.implId}__`,
  });

  const items = R.compose(
    joinLineBlocks,
    R.values,
    R.mapObjIndexed(
      (node, nodeId) => {
        const nodeJson = JSON.stringify(injectFuncRefs(node), null, 2);
        const template = `nodes['${nodeId}'] = new Node(${nodeJson});`;
        const statement = template.replace(/"__IMPLREF__(.+)#(.+)__"/g, "impl['$2'].$1");
        return statement;
      }
    )
  );<commit_msg>fix(runtime): preserve pin types while transpiling<commit_after>
import R from 'ramda';
import transform from './transformer';

const joinLines = R.join('\n');
const joinLineBlocks = R.join('\n\n');

function transpileImpl(impl) {
  const items = R.compose(
    joinLineBlocks,
    R.values,
    R.mapObjIndexed(
      (code, implId) => {
        const itemRef = `impl['${implId}']`;
        return joinLines([
          '// ---------------------------------------------------------------------',
          `${itemRef} = {};`,
          `(function(module, exports) {${code}})({exports: ${itemRef}}, ${itemRef});`,
        ]);
      }
    )
  );

  return joinLineBlocks([
    'var impl = {};',
    items(impl),
  ]);
}

function transpileNodes(nodes) {
  // __PLACEHOLDER__ is a voodoo to work around JSON safety. We can’t reference
  // real JS symbols in JSON string so we leave a placeholder and use
  // find-n-replace over result string to substitute proper references.
  const injectFuncRefs = node => R.merge(node, {
    setup: `__PLACEHOLDER__@@impl['${node.implId}'].setup@@__`,
    upkeep: `__PLACEHOLDER__@@impl['${node.implId}'].upkeep@@__`,
    evaluate: `__PLACEHOLDER__@@impl['${node.implId}'].evaluate@@__`,
    nodes: '__PLACEHOLDER__@@nodes@@__',
  });

  function replacer(key, val) {
    if (val === Boolean || val === Number || val === String) {
      // leave type as is
      return `__PLACEHOLDER__@@${val.name}@@__`;
    }

    return val;
  }

  const items = R.compose(
    joinLineBlocks,
    R.values,
    R.mapObjIndexed(
      (node, nodeId) => {
        const nodeJson = JSON.stringify(injectFuncRefs(node), replacer, 2);
        const template = `nodes['${nodeId}'] = new Node(${nodeJson});`;
        const statement = template .replace(/"__PLACEHOLDER__@@(.+)@@__"/g, "$1");
        return statement;
      }
    )
  );
{'fraction_non_alphanumeric': 0.20166379804934023, 'fraction_numerical': 0.001434308663224326, 'mean_word_length': 2.565439672801636, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5376764', 'n_tokens_mistral': 1122, 'n_tokens_neox': 1109, 'n_words': 262}