| 1 |
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- |
| 2 |
* vim: set ts=8 sts=2 et sw=2 tw=80: |
| 3 |
* This Source Code Form is subject to the terms of the Mozilla Public |
| 4 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 5 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 6 |
|
| 7 |
#include "vm/ToSource.h" |
| 8 |
|
| 9 |
#include "mozilla/Assertions.h" // MOZ_ASSERT |
| 10 |
#include "mozilla/FloatingPoint.h" // mozilla::IsNegativeZero |
| 11 |
|
| 12 |
#include <iterator> // std::size |
| 13 |
#include <stdint.h> // uint32_t |
| 14 |
|
| 15 |
#include "builtin/Array.h" // ArrayToSource |
| 16 |
#include "builtin/Boolean.h" // BooleanToString |
| 17 |
#include "builtin/Object.h" // ObjectToSource |
| 18 |
#include "gc/GCEnum.h" // CanGC |
| 19 |
#include "js/Class.h" // ESClass |
| 20 |
#include "js/friend/StackLimits.h" // js::AutoCheckRecursionLimit |
| 21 |
#include "js/Object.h" // JS::GetBuiltinClass |
| 22 |
#include "js/Printer.h" // QuoteString |
| 23 |
#include "js/Symbol.h" // SymbolCode, JS::WellKnownSymbolLimit |
| 24 |
#include "js/TypeDecls.h" // Rooted{Function, Object, String, Value}, HandleValue, Latin1Char |
| 25 |
#include "js/Utility.h" // UniqueChars |
| 26 |
#include "js/Value.h" // JS::Value |
| 27 |
#include "util/StringBuilder.h" // JSStringBuilder |
| 28 |
#include "vm/ErrorObject.h" // ErrorObject, ErrorToSource |
| 29 |
#include "vm/Interpreter.h" // Call |
| 30 |
#include "vm/JSContext.h" // JSContext |
| 31 |
#include "vm/JSFunction.h" // JSFunction, fun_toStringHelper |
| 32 |
#include "vm/SelfHosting.h" // CallSelfHostedFunction |
| 33 |
#include "vm/Stack.h" // FixedInvokeArgs |
| 34 |
#include "vm/StaticStrings.h" // StaticStrings |
| 35 |
#include "vm/StringType.h" // NewStringCopy{N,Z}, ToString |
| 36 |
#include "vm/SymbolType.h" // Symbol |
| 37 |
#include "vm/JSContext-inl.h" // JSContext::check |
| 38 |
#include "vm/JSObject-inl.h" // IsCallable |
| 39 |
#include "vm/ObjectOperations-inl.h" // GetProperty |
| 40 |
|
| 41 |
using namespace js; |
| 42 |
|
| 43 |
using mozilla::IsNegativeZero; |
| 44 |
|
| 45 |
using JS::GetBuiltinClass; |
| 46 |
|
| 47 |
/* |
| 48 |
* Convert a JSString to its source expression; returns null after reporting an |
| 49 |
* error, otherwise returns a new string reference. No Handle needed since the |
| 50 |
* input is dead after the GC. |
| 51 |
*/ |
| 52 |
static JSString* StringToSource(JSContext* cx, JSString* str) { |
| 53 |
UniqueChars chars = QuoteString(cx, str, '"'); |
| 54 |
if (!chars) { |
| 55 |
return nullptr; |
| 56 |
} |
| 57 |
return NewStringCopyZ<CanGC>(cx, chars.get()); |
| 58 |
} |
| 59 |
|
| 60 |
static JSString* SymbolToSource(JSContext* cx, JS::Symbol* symbol) { |
| 61 |
using JS::SymbolCode; |
| 62 |
|
| 63 |
RootedString desc(cx, symbol->description()); |
| 64 |
SymbolCode code = symbol->code(); |
| 65 |
if (symbol->isWellKnownSymbol()) { |
| 66 |
// Well-known symbol. |
| 67 |
return desc; |
| 68 |
} |
| 69 |
|
| 70 |
if (code == SymbolCode::PrivateNameSymbol) { |
| 71 |
MOZ_ASSERT(desc); |
| 72 |
return desc; |
| 73 |
} |
| 74 |
|
| 75 |
MOZ_ASSERT(code == SymbolCode::InSymbolRegistry || |
| 76 |
code == SymbolCode::UniqueSymbol); |
| 77 |
|
| 78 |
JSStringBuilder buf(cx); |
| 79 |
if (code == SymbolCode::InSymbolRegistry ? !buf.append("Symbol.for(") |
| 80 |
: !buf.append("Symbol(")) { |
| 81 |
return nullptr; |
| 82 |
} |
| 83 |
if (desc) { |
| 84 |
UniqueChars quoted = QuoteString(cx, desc, '"'); |
| 85 |
if (!quoted || !buf.append(quoted.get(), strlen(quoted.get()))) { |
| 86 |
return nullptr; |
| 87 |
} |
| 88 |
} |
| 89 |
if (!buf.append(')')) { |
| 90 |
return nullptr; |
| 91 |
} |
| 92 |
return buf.finishString(); |
| 93 |
} |
| 94 |
|
| 95 |
static JSString* BoxedToSource(JSContext* cx, HandleObject obj, |
| 96 |
const char* constructor) { |
| 97 |
RootedValue value(cx); |
| 98 |
if (!Unbox(cx, obj, &value)) { |
| 99 |
return nullptr; |
| 100 |
} |
| 101 |
MOZ_ASSERT(!value.isUndefined()); |
| 102 |
|
| 103 |
RootedString str(cx, ValueToSource(cx, value)); |
| 104 |
if (!str) { |
| 105 |
return nullptr; |
| 106 |
} |
| 107 |
|
| 108 |
JSStringBuilder buf(cx); |
| 109 |
if (!buf.append("new ") || !buf.append(constructor, strlen(constructor)) || |
| 110 |
!buf.append('(') || !buf.append(str) || !buf.append(')')) { |
| 111 |
return nullptr; |
| 112 |
} |
| 113 |
|
| 114 |
return buf.finishString(); |
| 115 |
} |
| 116 |
|
| 117 |
JSString* js::ValueToSource(JSContext* cx, HandleValue v) { |
| 118 |
AutoCheckRecursionLimit recursion(cx); |
| 119 |
if (!recursion.check(cx)) { |
| 120 |
return nullptr; |
| 121 |
} |
| 122 |
cx->check(v); |
| 123 |
|
| 124 |
switch (v.type()) { |
| 125 |
case JS::ValueType::Undefined: |
| 126 |
return cx->names().void_0_; |
| 127 |
|
| 128 |
case JS::ValueType::String: |
| 129 |
return StringToSource(cx, v.toString()); |
| 130 |
|
| 131 |
case JS::ValueType::Symbol: |
| 132 |
return SymbolToSource(cx, v.toSymbol()); |
| 133 |
|
| 134 |
case JS::ValueType::Null: |
| 135 |
return cx->names().null; |
| 136 |
|
| 137 |
case JS::ValueType::Boolean: |
| 138 |
return BooleanToString(cx, v.toBoolean()); |
| 139 |
|
| 140 |
case JS::ValueType::Double: |
| 141 |
/* Special case to preserve negative zero, _contra_ toString. */ |
| 142 |
if (IsNegativeZero(v.toDouble())) { |
| 143 |
static const Latin1Char negativeZero[] = {'-', '0'}; |
| 144 |
|
| 145 |
return NewStringCopyN<CanGC>(cx, negativeZero, std::size(negativeZero)); |
| 146 |
} |
| 147 |
[[fallthrough]]; |
| 148 |
case JS::ValueType::Int32: |
| 149 |
return ToString<CanGC>(cx, v); |
| 150 |
|
| 151 |
case JS::ValueType::BigInt: { |
| 152 |
RootedString str(cx, ToString<CanGC>(cx, v)); |
| 153 |
if (!str) { |
| 154 |
return nullptr; |
| 155 |
} |
| 156 |
|
| 157 |
RootedString n(cx, cx->staticStrings().getUnit('n')); |
| 158 |
|
| 159 |
return ConcatStrings<CanGC>(cx, str, n); |
| 160 |
} |
| 161 |
|
| 162 |
case JS::ValueType::Object: { |
| 163 |
RootedValue fval(cx); |
| 164 |
RootedObject obj(cx, &v.toObject()); |
| 165 |
if (!GetProperty(cx, obj, obj, cx->names().toSource, &fval)) { |
| 166 |
return nullptr; |
| 167 |
} |
| 168 |
if (IsCallable(fval)) { |
| 169 |
RootedValue v(cx); |
| 170 |
if (!js::Call(cx, fval, obj, &v)) { |
| 171 |
return nullptr; |
| 172 |
} |
| 173 |
|
| 174 |
return ToString<CanGC>(cx, v); |
| 175 |
} |
| 176 |
|
| 177 |
ESClass cls; |
| 178 |
if (!GetBuiltinClass(cx, obj, &cls)) { |
| 179 |
return nullptr; |
| 180 |
} |
| 181 |
|
| 182 |
// All ToSource functions must be able to handle wrapped objects! |
| 183 |
switch (cls) { |
| 184 |
case ESClass::Function: |
| 185 |
return fun_toStringHelper(cx, obj, true); |
| 186 |
|
| 187 |
case ESClass::Array: |
| 188 |
return ArrayToSource(cx, obj); |
| 189 |
|
| 190 |
case ESClass::Error: |
| 191 |
return ErrorToSource(cx, obj); |
| 192 |
|
| 193 |
case ESClass::RegExp: { |
| 194 |
FixedInvokeArgs<0> args(cx); |
| 195 |
RootedValue rval(cx); |
| 196 |
if (!CallSelfHostedFunction(cx, cx->names().dollar_RegExpToString_, v, |
| 197 |
args, &rval)) { |
| 198 |
return nullptr; |
| 199 |
} |
| 200 |
return ToString<CanGC>(cx, rval); |
| 201 |
} |
| 202 |
|
| 203 |
case ESClass::Boolean: |
| 204 |
return BoxedToSource(cx, obj, "Boolean"); |
| 205 |
|
| 206 |
case ESClass::Number: |
| 207 |
return BoxedToSource(cx, obj, "Number"); |
| 208 |
|
| 209 |
case ESClass::String: |
| 210 |
return BoxedToSource(cx, obj, "String"); |
| 211 |
|
| 212 |
case ESClass::Date: |
| 213 |
return BoxedToSource(cx, obj, "Date"); |
| 214 |
|
| 215 |
default: |
| 216 |
return ObjectToSource(cx, obj); |
| 217 |
} |
| 218 |
} |
| 219 |
|
| 220 |
case JS::ValueType::PrivateGCThing: |
| 221 |
case JS::ValueType::Magic: |
| 222 |
MOZ_ASSERT_UNREACHABLE( |
| 223 |
"internal value types shouldn't leak into places " |
| 224 |
"wanting source representations"); |
| 225 |
return nullptr; |
| 226 |
} |
| 227 |
|
| 228 |
MOZ_ASSERT_UNREACHABLE("shouldn't see an unrecognized value type"); |
| 229 |
return nullptr; |
| 230 |
} |