summaryrefslogtreecommitdiffstats
path: root/js/src/vm/ToSource.cpp
blob: af789166de6d5f41628bfcf2a9e7468a19166303 (plain)
1
2
3
4
5
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
55
56
57
58
59
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
 * vim: set ts=8 sts=2 et sw=2 tw=80:
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "vm/ToSource.h"

#include "mozilla/Assertions.h"     // MOZ_ASSERT
#include "mozilla/FloatingPoint.h"  // mozilla::IsNegativeZero

#include <iterator>  // std::size
#include <stdint.h>  // uint32_t

#include "builtin/Array.h"          // ArrayToSource
#include "builtin/Boolean.h"        // BooleanToString
#include "builtin/Object.h"         // ObjectToSource
#include "gc/Allocator.h"           // CanGC
#include "js/Class.h"               // ESClass
#include "js/friend/StackLimits.h"  // js::AutoCheckRecursionLimit
#include "js/Object.h"              // JS::GetBuiltinClass
#include "js/Printer.h"             // QuoteString
#include "js/Symbol.h"              // SymbolCode, JS::WellKnownSymbolLimit
#include "js/TypeDecls.h"  // Rooted{Function, Object, String, Value}, HandleValue, Latin1Char
#include "js/Utility.h"         // UniqueChars
#include "js/Value.h"           // JS::Value
#include "util/StringBuffer.h"  // JSStringBuilder
#include "vm/ErrorObject.h"     // ErrorObject, ErrorToSource
#include "vm/Interpreter.h"     // Call
#include "vm/JSContext.h"       // JSContext
#include "vm/JSFunction.h"      // JSFunction, fun_toStringHelper
#include "vm/SelfHosting.h"     // CallSelfHostedFunction
#include "vm/Stack.h"           // FixedInvokeArgs
#include "vm/StaticStrings.h"   // StaticStrings
#include "vm/StringType.h"      // NewStringCopy{N,Z}, ToString
#include "vm/SymbolType.h"      // Symbol
#ifdef ENABLE_RECORD_TUPLE
#  include "vm/RecordType.h"
#  include "vm/TupleType.h"
#endif

#include "vm/JSContext-inl.h"         // JSContext::check
#include "vm/JSObject-inl.h"          // IsCallable
#include "vm/ObjectOperations-inl.h"  // GetProperty

using namespace js;

using mozilla::IsNegativeZero;

using JS::GetBuiltinClass;

/*
 * Convert a JSString to its source expression; returns null after reporting an
 * error, otherwise returns a new string reference. No Handle needed since the
 * input is dead after the GC.
 */
static JSString* StringToSource(JSContext* cx, JSString* str) {
  UniqueChars chars = QuoteString(cx, str, '"');
  if (!chars) {
    return nullptr;
  }
  return NewStringCopyZ<CanGC>(cx, chars.get());
}

static JSString* SymbolToSource(JSContext* cx, JS::Symbol* symbol) {
  using JS::SymbolCode;

  RootedString desc(cx, symbol->description());
  SymbolCode code = symbol->code();
  if (symbol->isWellKnownSymbol()) {
    // Well-known symbol.
    return desc;
  }

  if (code == SymbolCode::PrivateNameSymbol) {
    MOZ_ASSERT(desc);
    return desc;
  }

  MOZ_ASSERT(code == SymbolCode::InSymbolRegistry ||
             code == SymbolCode::UniqueSymbol);

  JSStringBuilder buf(cx);
  if (code == SymbolCode::InSymbolRegistry ? !buf.append("Symbol.for(")
                                           : !buf.append("Symbol(")) {
    return nullptr;
  }
  if (desc) {
    UniqueChars quoted = QuoteString(cx, desc, '"');
    if (!quoted || !buf.append(quoted.get(), strlen(quoted.get()))) {
      return nullptr;
    }
  }
  if (!buf.append(')')) {
    return nullptr;
  }
  return buf.finishString();
}

static JSString* BoxedToSource(JSContext* cx, HandleObject obj,
                               const char* constructor) {
  RootedValue value(cx);
  if (!Unbox(cx, obj, &value)) {
    return nullptr;
  }
  MOZ_ASSERT(!value.isUndefined());

  RootedString str(cx, ValueToSource(cx, value));
  if (!str) {
    return nullptr;
  }

  JSStringBuilder buf(cx);
  if (!buf.append("new ") || !buf.append(constructor, strlen(constructor)) ||
      !buf.append('(') || !buf.append(str) || !buf.append(')')) {
    return nullptr;
  }

  return buf.finishString();
}

JSString* js::ValueToSource(JSContext* cx, HandleValue v) {
  AutoCheckRecursionLimit recursion(cx);
  if (!recursion.check(cx)) {
    return nullptr;
  }
  cx->check(v);

  switch (v.type()) {
    case JS::ValueType::Undefined:
      return cx->names().void0;

    case JS::ValueType::String:
      return StringToSource(cx, v.toString());

    case JS::ValueType::Symbol:
      return SymbolToSource(cx, v.toSymbol());

    case JS::ValueType::Null:
      return cx->names().null;

    case JS::ValueType::Boolean:
      return BooleanToString(cx, v.toBoolean());

    case JS::ValueType::Double:
      /* Special case to preserve negative zero, _contra_ toString. */
      if (IsNegativeZero(v.toDouble())) {
        static const Latin1Char negativeZero[] = {'-', '0'};

        return NewStringCopyN<CanGC>(cx, negativeZero, std::size(negativeZero));
      }
      [[fallthrough]];
    case JS::ValueType::Int32:
      return ToString<CanGC>(cx, v);

    case JS::ValueType::BigInt: {
      RootedString str(cx, ToString<CanGC>(cx, v));
      if (!str) {
        return nullptr;
      }

      RootedString n(cx, cx->staticStrings().getUnit('n'));

      return ConcatStrings<CanGC>(cx, str, n);
    }

#ifdef ENABLE_RECORD_TUPLE
    case ValueType::ExtendedPrimitive: {
      RootedObject obj(cx, &v.toExtendedPrimitive());
      if (obj->is<TupleType>()) {
        Rooted<TupleType*> tup(cx, &obj->as<TupleType>());
        return TupleToSource(cx, tup);
      }
      if (obj->is<RecordType>()) {
        return RecordToSource(cx, obj.as<RecordType>());
      }
      MOZ_CRASH("Unsupported ExtendedPrimitive");
    }
#endif

    case JS::ValueType::Object: {
      RootedValue fval(cx);
      RootedObject obj(cx, &v.toObject());
      if (!GetProperty(cx, obj, obj, cx->names().toSource, &fval)) {
        return nullptr;
      }
      if (IsCallable(fval)) {
        RootedValue v(cx);
        if (!js::Call(cx, fval, obj, &v)) {
          return nullptr;
        }

        return ToString<CanGC>(cx, v);
      }

      ESClass cls;
      if (!GetBuiltinClass(cx, obj, &cls)) {
        return nullptr;
      }

      // All ToSource functions must be able to handle wrapped objects!
      switch (cls) {
        case ESClass::Function:
          return fun_toStringHelper(cx, obj, true);

        case ESClass::Array:
          return ArrayToSource(cx, obj);

        case ESClass::Error:
          return ErrorToSource(cx, obj);

        case ESClass::RegExp: {
          FixedInvokeArgs<0> args(cx);
          RootedValue rval(cx);
          if (!CallSelfHostedFunction(cx, cx->names().RegExpToString, v, args,
                                      &rval)) {
            return nullptr;
          }
          return ToString<CanGC>(cx, rval);
        }

        case ESClass::Boolean:
          return BoxedToSource(cx, obj, "Boolean");

        case ESClass::Number:
          return BoxedToSource(cx, obj, "Number");

        case ESClass::String:
          return BoxedToSource(cx, obj, "String");

        case ESClass::Date:
          return BoxedToSource(cx, obj, "Date");

        default:
          return ObjectToSource(cx, obj);
      }
    }

    case JS::ValueType::PrivateGCThing:
    case JS::ValueType::Magic:
      MOZ_ASSERT_UNREACHABLE(
          "internal value types shouldn't leak into places "
          "wanting source representations");
      return nullptr;
  }

  MOZ_ASSERT_UNREACHABLE("shouldn't see an unrecognized value type");
  return nullptr;
}