<commit_before>var nqp = require('nqp-runtime');
var Null = nqp.Null;
var CodeRef = require('nqp-runtime/code-ref');
var op = {};

var Scalar, True, False, Int, Num, Str, Code, Mu, Any, ContainerDescriptor;

var defaultContainerDescriptor;

op.p6settypes = function(types) {
  Scalar = types.content.get('Scalar');
  True = types.content.get('True');
  False = types.content.get('False');
  Int = types.content.get('Int');
  Num = types.content.get('Num');
  Str = types.content.get('Str');
  Code = types.content.get('Code');
  Mu = types.content.get('Mu');
  Any = types.content.get('Any');
  ContainerDescriptor = types.content.get('ContainerDescriptor');

  defaultContainerDescriptor = ContainerDescriptor._STable.REPR.allocate(ContainerDescriptor._STable);

  defaultContainerDescriptor.$$bindattr(ContainerDescriptor, '$!of', Mu);

  defaultContainerDescriptor.$$bindattr_s(ContainerDescriptor, '$!name', "<element>");
  defaultContainerDescriptor.$$bindattr_i(ContainerDescriptor, '$!rw', 1);
  defaultContainerDescriptor.$$bindattr(ContainerDescriptor, '$!default', Any);

  return types;
};

op.p6bool = function(value) {
  return value ? True : False;
};

op.p6definite = function(obj) {
  return obj.typeObject_ ? False : True;
};

op.p6typecheckrv = function(rv, routine, bypassType) {
  // STUB
  return rv;
};

op.p6decontrv = function(rountine, cont) {
  // STUB
  return cont;
};

op.p6box_i = function(int) {
  var repr = Int._STable.REPR;
  var boxed = repr.allocate(Int._STable);
  boxed.$$setInt(int);
  return boxed;
};

op.p6box_n = function(num) {
  var repr = Num._STable.REPR;
  var boxed = repr.allocate(Num._STable);
  boxed.$$setNum(num);
  return boxed;
};

op.p6box_s = function(str) {
  var repr = Str._STable.REPR;
  var boxed = repr.allocate(Str._STable);
  boxed.$$setStr(str);
  return boxed;
};

op.p6captureouters2 = function(ctx, capList, target) {
  var cf = (target.outerCtx || closure.forcedOuter);

  if (cf === Null) {
    return capList;
  }

  var elems = capList.$$elems();

  for (var i = 0; i < elems; i++) {
      var codeObj = capList.$$atpos(i);

      var closure = codeObj.$$getattr(Code, "$!do");

      var ctxToDiddle = (closure.outerCtx || closure.forcedOuter);
      if (ctxToDiddle) {
        ctxToDiddle.$$outer = cf;
        if (ctxToDiddle.closuresUsingThis) {
          for (var closure of ctxToDiddle.closuresUsingThis) {
            let closureCtx = closure.outerCtx;
            let updatedCtxs = [];
            while (updatedCtxs.length < closure.staticCode.closureTemplate.length) {
              updatedCtxs.unshift(closureCtx);
              closureCtx = closureCtx.$$outer;
            }
            closure.capture(closure.staticCode.closureTemplate.apply(null, updatedCtxs));
          }
        }
      } else {
        console.log("can't diddle", closure);
      }
  }

  return capList;
};

op.p6capturelex = function(ctx, codeObj) {
  var closure = codeObj.$$getattr(Code, "$!do");
  var wantedStaticInfo = closure.staticCode.outerCodeRef;

  if (ctx.codeRef().staticCode === wantedStaticInfo) {
    closure.forcedOuter = ctx;
    closure.outerCtx = ctx;

    let closureCtx = ctx;
    let updatedCtxs = [];
    while (updatedCtxs.length < closure.staticCode.closureTemplate.length) {
      updatedCtxs.unshift(closureCtx);
      closureCtx = closureCtx.$$outer;
    }
    closure.capture(closure.staticCode.closureTemplate.apply(null, updatedCtxs));
  } else if (ctx.$$outer.codeRef().staticCode === wantedStaticInfo) {
    closure.outerCtx = ctx;
    closure.forcedOuter = ctx.$$outer;
  }

  return codeObj;
};
<commit_msg>[js] Do the workaround in nqp::p6capturelex in the same way as the moar backend does it.<commit_after>var nqp = require('nqp-runtime');
var Null = nqp.Null;
var CodeRef = require('nqp-runtime/code-ref');
var op = {};

var Scalar, True, False, Int, Num, Str, Code, Mu, Any, ContainerDescriptor;

var defaultContainerDescriptor;

op.p6settypes = function(types) {
  Scalar = types.content.get('Scalar');
  True = types.content.get('True');
  False = types.content.get('False');
  Int = types.content.get('Int');
  Num = types.content.get('Num');
  Str = types.content.get('Str');
  Code = types.content.get('Code');
  Mu = types.content.get('Mu');
  Any = types.content.get('Any');
  ContainerDescriptor = types.content.get('ContainerDescriptor');

  defaultContainerDescriptor = ContainerDescriptor._STable.REPR.allocate(ContainerDescriptor._STable);

  defaultContainerDescriptor.$$bindattr(ContainerDescriptor, '$!of', Mu);

  defaultContainerDescriptor.$$bindattr_s(ContainerDescriptor, '$!name', "<element>");
  defaultContainerDescriptor.$$bindattr_i(ContainerDescriptor, '$!rw', 1);
  defaultContainerDescriptor.$$bindattr(ContainerDescriptor, '$!default', Any);

  return types;
};

op.p6bool = function(value) {
  return value ? True : False;
};

op.p6definite = function(obj) {
  return obj.typeObject_ ? False : True;
};

op.p6typecheckrv = function(rv, routine, bypassType) {
  // STUB
  return rv;
};

op.p6decontrv = function(rountine, cont) {
  // STUB
  return cont;
};

op.p6box_i = function(int) {
  var repr = Int._STable.REPR;
  var boxed = repr.allocate(Int._STable);
  boxed.$$setInt(int);
  return boxed;
};

op.p6box_n = function(num) {
  var repr = Num._STable.REPR;
  var boxed = repr.allocate(Num._STable);
  boxed.$$setNum(num);
  return boxed;
};

op.p6box_s = function(str) {
  var repr = Str._STable.REPR;
  var boxed = repr.allocate(Str._STable);
  boxed.$$setStr(str);
  return boxed;
};

op.p6captureouters2 = function(ctx, capList, target) {
  var cf = (target.outerCtx || closure.forcedOuter);

  if (cf === Null) {
    return capList;
  }

  var elems = capList.$$elems();

  for (var i = 0; i < elems; i++) {
      var codeObj = capList.$$atpos(i);

      var closure = codeObj.$$getattr(Code, "$!do");

      var ctxToDiddle = (closure.outerCtx || closure.forcedOuter);
      if (ctxToDiddle) {
        ctxToDiddle.$$outer = cf;
        if (ctxToDiddle.closuresUsingThis) {
          for (var closure of ctxToDiddle.closuresUsingThis) {
            let closureCtx = closure.outerCtx;
            let updatedCtxs = [];
            while (updatedCtxs.length < closure.staticCode.closureTemplate.length) {
              updatedCtxs.unshift(closureCtx);
              closureCtx = closureCtx.$$outer;
            }
            closure.capture(closure.staticCode.closureTemplate.apply(null, updatedCtxs));
          }
        }
      } else {
        console.log("can't diddle", closure);
      }
  }

  return capList;
};

op.p6capturelex = function(ctx, codeObj) {
  var closure = codeObj.$$getattr(Code, "$!do");
  var wantedStaticInfo = closure.staticCode.outerCodeRef;

  if (ctx.codeRef().staticCode === wantedStaticInfo) {
    closure.forcedOuter = ctx;
    closure.outerCtx = ctx;

    let closureCtx = ctx;
    let updatedCtxs = [];
    while (updatedCtxs.length < closure.staticCode.closureTemplate.length) {
      updatedCtxs.unshift(closureCtx);
      closureCtx = closureCtx.$$outer;
    }
    closure.capture(closure.staticCode.closureTemplate.apply(null, updatedCtxs));
  } else {
    /* HACK - workaround for rakudo bugs */
    console.log("HORRIBLE hack - p6capturelex will do nothing");
  }

  return codeObj;
};

{'fraction_non_alphanumeric': 0.14639515685195378, 'fraction_numerical': 0.003852504127682994, 'mean_word_length': 3.5630885122410545, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 5, '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': '2536856', 'n_tokens_mistral': 2440, 'n_tokens_neox': 2370, 'n_words': 467}