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

5 6 7
#include "src/d8.h"

const char* v8::Shell::stringify_source_ = R"D8(
yangguo's avatar
yangguo committed
8
(function() {
9 10
"use strict";

11 12 13 14
// A more universal stringify that supports more types than JSON.
// Used by the d8 shell to output results.
var stringifyDepthLimit = 4;  // To avoid crashing on cyclic objects

15 16 17 18 19 20
// Hacky solution to circumvent forcing --allow-natives-syntax for d8
function isProxy(o) { return false };
function JSProxyGetTarget(proxy) { };
function JSProxyGetHandler(proxy) { };

try {
21
  isProxy = Function(['object'], 'return %IsJSProxy(object)');
22 23 24 25 26 27 28
  JSProxyGetTarget = Function(['proxy'],
    'return %JSProxyGetTarget(proxy)');
  JSProxyGetHandler = Function(['proxy'],
    'return %JSProxyGetHandler(proxy)');
} catch(e) {};


29 30 31 32
function Stringify(x, depth) {
  if (depth === undefined)
    depth = stringifyDepthLimit;
  else if (depth === 0)
33 34 35 36
    return "...";
  if (isProxy(x)) {
    return StringifyProxy(x, depth);
  }
37 38 39 40 41 42
  switch (typeof x) {
    case "undefined":
      return "undefined";
    case "boolean":
    case "number":
    case "function":
43
    case "symbol":
44 45 46
      return x.toString();
    case "string":
      return "\"" + x.toString() + "\"";
47
    case "bigint":
48
      return x.toString() + "n";
49
    case "object":
50
      if (x === null) return "null";
51 52 53 54 55 56 57 58
      if (x.constructor && x.constructor.name === "Array") {
        var elems = [];
        for (var i = 0; i < x.length; ++i) {
          elems.push(
            {}.hasOwnProperty.call(x, i) ? Stringify(x[i], depth - 1) : "");
        }
        return "[" + elems.join(", ") + "]";
      }
59
      try {
60 61 62 63 64 65 66 67 68
        var string = String(x);
        if (string && string !== "[object Object]") return string;
      } catch(e) {}
      var props = [];
      var names = Object.getOwnPropertyNames(x);
      names = names.concat(Object.getOwnPropertySymbols(x));
      for (var i in names) {
        var name = names[i];
        var desc = Object.getOwnPropertyDescriptor(x, name);
69 70
        if (desc === (void 0)) continue;
        if (typeof name === 'symbol') name = "[" + Stringify(name) + "]";
71 72 73 74 75 76 77 78 79 80 81
        if ("value" in desc) {
          props.push(name + ": " + Stringify(desc.value, depth - 1));
        }
        if (desc.get) {
          var getter = Stringify(desc.get);
          props.push("get " + name + getter.slice(getter.indexOf('(')));
        }
        if (desc.set) {
          var setter = Stringify(desc.set);
          props.push("set " + name + setter.slice(setter.indexOf('(')));
        }
82
      }
83
      return "{" + props.join(", ") + "}";
84
    default:
85
      return "[crazy non-standard value]";
86 87
  }
}
88 89 90 91 92 93 94 95 96

function StringifyProxy(proxy, depth) {
  var proxy_type = typeof proxy;
  var info_object = {
    target: JSProxyGetTarget(proxy),
    handler: JSProxyGetHandler(proxy)
  }
  return '[' + proxy_type + ' Proxy ' + Stringify(info_object, depth-1) + ']';
}
yangguo's avatar
yangguo committed
97 98 99

return Stringify;
})();
100 101

)D8";