templates.js 1.93 KB
Newer Older
1 2 3 4
// Copyright 2014 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5
// Called from a desugaring in the parser.
6

7
(function(global, utils) {
8

9 10
"use strict";

11 12
%CheckIsBootstrapping();

13 14 15 16
// -------------------------------------------------------------------
// Imports

var GlobalMap = global.Map;
17
var InternalArray = utils.InternalArray;
18 19 20 21 22 23

// -------------------------------------------------------------------

var callSiteCache = new GlobalMap;
var mapGetFn = GlobalMap.prototype.get;
var mapSetFn = GlobalMap.prototype.set;
24

25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40

function SameCallSiteElements(rawStrings, other) {
  var length = rawStrings.length;
  var other = other.raw;

  if (length !== other.length) return false;

  for (var i = 0; i < length; ++i) {
    if (rawStrings[i] !== other[i]) return false;
  }

  return true;
}


function GetCachedCallSite(siteObj, hash) {
41
  var obj = %_CallFunction(callSiteCache, hash, mapGetFn);
42 43 44 45 46 47 48 49 50 51 52

  if (IS_UNDEFINED(obj)) return;

  var length = obj.length;
  for (var i = 0; i < length; ++i) {
    if (SameCallSiteElements(siteObj, obj[i])) return obj[i];
  }
}


function SetCachedCallSite(siteObj, hash) {
53
  var obj = %_CallFunction(callSiteCache, hash, mapGetFn);
54 55 56 57 58
  var array;

  if (IS_UNDEFINED(obj)) {
    array = new InternalArray(1);
    array[0] = siteObj;
59
    %_CallFunction(callSiteCache, hash, array, mapSetFn);
60 61 62 63 64 65 66 67
  } else {
    obj.push(siteObj);
  }

  return siteObj;
}


68
function GetTemplateCallSite(siteObj, rawStrings, hash) {
69 70 71
  var cached = GetCachedCallSite(rawStrings, hash);

  if (!IS_UNDEFINED(cached)) return cached;
72 73 74 75

  %AddNamedProperty(siteObj, "raw", %ObjectFreeze(rawStrings),
      READ_ONLY | DONT_ENUM | DONT_DELETE);

76
  return SetCachedCallSite(%ObjectFreeze(siteObj), hash);
77
}
78

79 80 81 82 83
// ----------------------------------------------------------------------------
// Exports

%InstallToContext(["get_template_call_site", GetTemplateCallSite]);

84
})