harmony-templates.js 2.09 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
"use strict";
6

7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
var callSiteCache = new $Map;

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) {
  var obj = %MapGet(callSiteCache, hash);

  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) {
  var obj = %MapGet(callSiteCache, hash);
  var array;

  if (IS_UNDEFINED(obj)) {
    array = new InternalArray(1);
    array[0] = siteObj;
    %MapSet(callSiteCache, hash, array);
  } else {
    obj.push(siteObj);
  }

  return siteObj;
}


function GetTemplateCallSite(siteObj, rawStrings, hash) {
  var cached = GetCachedCallSite(rawStrings, hash);

  if (!IS_UNDEFINED(cached)) return cached;
55 56 57 58

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

59
  return SetCachedCallSite(%ObjectFreeze(siteObj), hash);
60
}
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94


// ES6 Draft 10-14-2014, section 21.1.2.4
function StringRaw(callSite) {
  // TODO(caitp): Use rest parameters when implemented
  var numberOfSubstitutions = %_ArgumentsLength();
  var cooked = ToObject(callSite);
  var raw = ToObject(cooked.raw);
  var literalSegments = ToLength(raw.length);
  if (literalSegments <= 0) return "";

  var result = ToString(raw[0]);

  for (var i = 1; i < literalSegments; ++i) {
    if (i < numberOfSubstitutions) {
      result += ToString(%_Arguments(i));
    }
    result += ToString(raw[i]);
  }

  return result;
}


function ExtendStringForTemplates() {
  %CheckIsBootstrapping();

  // Set up the non-enumerable functions on the String object.
  InstallFunctions($String, DONT_ENUM, $Array(
    "raw", StringRaw
  ));
}

ExtendStringForTemplates();