<commit_before>describe "cached runInThisContext", ->
  main = null

  beforeEach ->
    main = require("../src/main")

  it "runs arbitrary functions as vm.runInThisContext", ->
    vm = require("vm")
    fn = "(function(a, b, c) { return a + b + c; })"

    expect(
      vm.runInThisContext(fn, "filename")(1, 2, 3)
    ).toBe main.runInThisContext(fn, "filename").result(1, 2, 3)

  it "returns a cache that can be used to speed up future compilations", ->
    fn = "(function(a, b, c) { return a; })"

    nonCached = main.runInThisContext(fn, "filename")
    cached = main.runInThisContextCached(fn, "filename", nonCached.cacheBuffer)

    expect(nonCached.result(1)).toBe(cached.result(1))
    expect(cached.wasRejected).toBe(false)

  it "rejects the cache when it doesn't match the supplied source code", ->
    fn = "(function(a, b, c) { return a + b; })"

    cached = main.runInThisContextCached(fn, "filename", new Buffer("invalid cache"))

    expect(cached.result(10, 20)).toBe(30)
    expect(cached.wasRejected).toBe(true)

  it "doesn't return a cache when the same function gets run 3 or more times within the same context", ->
    # this seems like a v8 optimization
    fn = "(function(a, b, c) { return 42; })"

    expect(main.runInThisContext(fn, "file").cacheBuffer).toBeTruthy()
    expect(main.runInThisContext(fn, "file").cacheBuffer).toBeTruthy()
    expect(main.runInThisContext(fn, "file").cacheBuffer).toBeFalsy()<commit_msg>:art:<commit_after>describe "cached runInThisContext", ->
  main = null

  beforeEach ->
    main = require("../src/main")

  it "runs arbitrary functions as vm.runInThisContext does", ->
    fn = "(function(a, b, c) { return a + b + c; })"

    expect(
      require("vm").runInThisContext(fn, "filename-1")(1, 2, 3)
    ).toBe main.runInThisContext(fn, "filename-1").result(1, 2, 3)

  it "returns a cache that can be used to speed up future compilations", ->
    fn = "(function(a, b, c) { return a; })"

    nonCached = main.runInThisContext(fn, "filename-2")
    cached = main.runInThisContextCached(fn, "filename-2", nonCached.cacheBuffer)

    expect(nonCached.result(1)).toBe(cached.result(1))
    expect(cached.wasRejected).toBe(false)

  it "rejects the cache when it doesn't match the supplied source code", ->
    fn = "(function(a, b, c) { return a + b; })"

    cached = main.runInThisContextCached(fn, "filename", new Buffer("invalid cache"))

    expect(cached.result(10, 20)).toBe(30)
    expect(cached.wasRejected).toBe(true)

  it "doesn't return a cache when the same function gets run 3 or more times within the same context", ->
    # this spec documents what seems like a v8 optimization
    fn = "(function(a, b, c) { return 42; })"

    expect(main.runInThisContext(fn, "file").cacheBuffer).toBeTruthy()
    expect(main.runInThisContext(fn, "file").cacheBuffer).toBeTruthy()
    expect(main.runInThisContext(fn, "file").cacheBuffer).toBeFalsy()
{'fraction_non_alphanumeric': 0.15213675213675212, 'fraction_numerical': 0.013675213675213675, 'mean_word_length': 3.925925925925926, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 15, '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': '2447399', 'n_tokens_mistral': 1006, 'n_tokens_neox': 949, 'n_words': 255}