<commit_before>module.exports = function bridge(nativeWindow, win, unwrapEphemeral){
  function isObject(o){
    if (o == null) return o !== null && o !== undefined;
    return typeof o === 'object' || typeof o === 'function';
  }

  function encode(msg){
    if (msg == null)
      return '{}';
    else if (typeof msg === 'object')
      return JSON.stringify(msg);
    else if (typeof msg === 'string')
      return msg;
    else
      throw new TypeError("Tried to encode invalid type");
  }

  function decode(msg){
    try {
      return JSON.parse(msg);
    } catch (e) {
      return { error: e };
    }
  }


  var _call   = Function.prototype.call,
      _apply  = Function.prototype.apply,
      _bind   = Function.prototype.bind,
      _slice  = Array.prototype.slice,
      _hasOwn = Object.prototype.hasOwnProperty,
      char    = String.fromCharCode;

  var foreignObjects = new WeakMap,
      foreignIDs = new WeakMap,
      localObjects = new WeakMap,
      localIDs = new WeakMap,
      localCanonicals = [],
      foreignCanonicals = Object.create(null);

  var TOKEN       = char(1),
      LITERAL     = char(2),
      TRUE        = [TOKEN, 1],
      FALSE       = [TOKEN, 2],
      UNDEFINED   = [TOKEN, 3],
      NULL        = [TOKEN, 4],
      NAN         = [TOKEN, 5],
      INFINITY    = [TOKEN, 6],
      NEGINFINITY = [TOKEN, 7],
      NEGZERO     = [TOKEN, 8],
      ZERO        = [TOKEN, 9];

  var tokens = [, true, false, undefined, null, NaN, Infinity, -Infinity, -0, 0];

  var pipe = function(source, listeners){
    source.onmessage = function onmessage(msg){
      var res = {};
      msg = decode(msg);
      listeners[msg.type] && listeners[msg.type](msg.msg, res);
      return encode(res.result === undefined ? {} : res.result);
    };

    return {
      send: function send(type, msg){
        msg = { type: type, msg: msg };
        return decode(source.send(encode(msg)));
      },
      listen: function listen(type, handler){
        listeners[type] = handler;
      }
    };
  }(nativeWindow || appjs, {});

  if (!nativeWindow) {
    var LOCAL = char(15), FOREIGN = char(16);
    var logger = function logger(type, error){
      if (typeof node !== 'undefined') {
        logger = function logger(type, error){
          node.process.AppjsEmitter.emit('log', type, 'browser', error);
        };
        logger(type, context, error);
      }
    }
  } else {
    var LOCAL = char(16), FOREIGN = char(15);
    var logger = function logger(type, error){
      process.AppjsEmitter.emit('log', type, 'node', error);
    }
    nativeWindow.once('context-released', function released(){
      nativeWindow.onmessage = function(){ return ''; }
      unwrap = wrap = pipe.send = pipe.listen = function(){ return {} };
      mirror = {
        type      : function(){ return ['object'] },
        keys      : function(){ return [] },
        enumerate : function(){ return [] },
        props     : function(){ return [] },
        get       : function(){ return UNDEFINED },
        set       : function(){ return TRUE },
        proto     : function(){ return NULL },
        has       : function(){ return FALSE },
        hasOwn    : function(){ return FALSE },
        delete    : function(){ return TRUE },
        define    : function(){ return TRUE },
        describe  : function(){ return UNDEFINED },
        construct : function(){ return UNDEFINED },
        apply     : function(){ return UNDEFINED }
      };
      nativeWindow = foreignObjects = foreignIDs = localObjects = localIDs = localCanonicals = foreignCanonicals = null;
    });
  }



  wrap(nativeWindow ? global : window);

  function initForeign(obj, proxy){
    foreignObjects.set(proxy, obj);
    foreignIDs.set(obj, proxy);
    foreignCanonicals[obj[1]] = obj;
    return proxy;
  }

  function unwrap(obj){
    if (!isObject(obj)) {
      return obj;
    } else if (obj[0] === TOKEN) {
      return tokens[obj[1]];
    } else if (obj[0] === LITERAL) {
      return obj[1];
    } else if (obj[0] === FOREIGN) {
      if (obj[1] in foreignCanonicals) {
        return foreignIDs.get(foreignCanonicals[obj[1]]);
      } else {
        return initForeign(obj, new Mirror(obj));
      }
    } else if (obj[0] === LOCAL && localCanonicals[obj[1]]) {
      return localObjects.get(localCanonicals[obj[1]]);
    } else {<commit_msg>fix the bridge logger to reflect removal of AppjsEmitter<commit_after>module.exports = function bridge(nativeWindow, win, unwrapEphemeral){
  function isObject(o){
    if (o == null) return o !== null && o !== undefined;
    return typeof o === 'object' || typeof o === 'function';
  }

  function encode(msg){
    if (msg == null)
      return '{}';
    else if (typeof msg === 'object')
      return JSON.stringify(msg);
    else if (typeof msg === 'string')
      return msg;
    else
      throw new TypeError("Tried to encode invalid type");
  }

  function decode(msg){
    try {
      return JSON.parse(msg);
    } catch (e) {
      return { error: e };
    }
  }


  var _call   = Function.prototype.call,
      _apply  = Function.prototype.apply,
      _bind   = Function.prototype.bind,
      _slice  = Array.prototype.slice,
      _hasOwn = Object.prototype.hasOwnProperty,
      char    = String.fromCharCode;

  var foreignObjects = new WeakMap,
      foreignIDs = new WeakMap,
      localObjects = new WeakMap,
      localIDs = new WeakMap,
      localCanonicals = [],
      foreignCanonicals = Object.create(null);

  var TOKEN       = char(1),
      LITERAL     = char(2),
      TRUE        = [TOKEN, 1],
      FALSE       = [TOKEN, 2],
      UNDEFINED   = [TOKEN, 3],
      NULL        = [TOKEN, 4],
      NAN         = [TOKEN, 5],
      INFINITY    = [TOKEN, 6],
      NEGINFINITY = [TOKEN, 7],
      NEGZERO     = [TOKEN, 8],
      ZERO        = [TOKEN, 9];

  var tokens = [, true, false, undefined, null, NaN, Infinity, -Infinity, -0, 0];

  var pipe = function(source, listeners){
    source.onmessage = function onmessage(msg){
      var res = {};
      msg = decode(msg);
      listeners[msg.type] && listeners[msg.type](msg.msg, res);
      return encode(res.result === undefined ? {} : res.result);
    };

    return {
      send: function send(type, msg){
        msg = { type: type, msg: msg };
        return decode(source.send(encode(msg)));
      },
      listen: function listen(type, handler){
        listeners[type] = handler;
      }
    };
  }(nativeWindow || appjs, {});

  if (!nativeWindow) {
    var LOCAL = char(15), FOREIGN = char(16);
    var logger = function logger(type, error){
      if (typeof node !== 'undefined') {
        logger = function logger(type, error){
          node.process.emit('appjs-log', type, 'browser', error);
        };
        logger(type, context, error);
      }
    }
  } else {
    var LOCAL = char(16), FOREIGN = char(15);
    var logger = function logger(type, error){
      process.emit('appjs-log', type, 'node', error);
    };
    nativeWindow.once('context-released', function released(){
      mirror = {
        type      : function(){ return ['object'] },
        keys      : function(){ return [] },
        enumerate : function(){ return [] },
        props     : function(){ return [] },
        get       : function(){ return UNDEFINED },
        set       : function(){ return TRUE },
        proto     : function(){ return NULL },
        has       : function(){ return FALSE },
        hasOwn    : function(){ return FALSE },
        delete    : function(){ return TRUE },
        define    : function(){ return TRUE },
        describe  : function(){ return UNDEFINED },
        construct : function(){ return UNDEFINED },
        apply     : function(){ return UNDEFINED }
      };
      unwrap = wrap = pipe.send = pipe.listen = function(){ return {} };
      nativeWindow.onmessage = function(){ return ''; }
      nativeWindow = null;
      foreignObjects = foreignIDs = foreignCanonicals = null;
      localObjects = localIDs = localCanonicals = null;
    });
  }



  wrap(nativeWindow ? global : window);

  function initForeign(obj, proxy){
    foreignObjects.set(proxy, obj);
    foreignIDs.set(obj, proxy);
    foreignCanonicals[obj[1]] = obj;
    return proxy;
  }

  function unwrap(obj){
    if (!isObject(obj)) {
      return obj;
    } else if (obj[0] === TOKEN) {
      return tokens[obj[1]];
    } else if (obj[0] === LITERAL) {
      return obj[1];
    } else if (obj[0] === FOREIGN) {
      if (obj[1] in foreignCanonicals) {
        return foreignIDs.get(foreignCanonicals[obj[1]]);
      } else {
        return initForeign(obj, new Mirror(obj));
      }
    } else if (obj[0] === LOCAL && localCanonicals[obj[1]]) {
      return localObjects.get(localCanonicals[obj[1]]);
    } else {
{'fraction_non_alphanumeric': 0.1446375485936428, 'fraction_numerical': 0.007317630916990624, 'mean_word_length': 2.314513073133763, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '5645034', 'n_tokens_mistral': 2673, 'n_tokens_neox': 2611, 'n_words': 692}