runtime-symbol.cc 2.15 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 6
#include "src/execution/arguments-inl.h"
#include "src/execution/isolate-inl.h"
7
#include "src/heap/heap-inl.h"  // For ToBoolean. TODO(jkummerow): Drop.
8
#include "src/logging/counters.h"
9
#include "src/objects/objects-inl.h"
10
#include "src/runtime/runtime-utils.h"
11
#include "src/strings/string-builder-inl.h"
12 13 14 15 16 17

namespace v8 {
namespace internal {

RUNTIME_FUNCTION(Runtime_CreatePrivateSymbol) {
  HandleScope scope(isolate);
18
  DCHECK_GE(1, args.length());
19
  Handle<Symbol> symbol = isolate->factory()->NewPrivateSymbol();
20
  if (args.length() == 1) {
21
    Handle<Object> description = args.at(0);
22 23 24
    CHECK(description->IsString() || description->IsUndefined(isolate));
    if (description->IsString())
      symbol->set_description(String::cast(*description));
25
  }
26
  return *symbol;
27 28
}

29 30 31
RUNTIME_FUNCTION(Runtime_CreatePrivateBrandSymbol) {
  HandleScope scope(isolate);
  DCHECK_EQ(1, args.length());
32
  Handle<String> name = args.at<String>(0);
33 34 35 36 37
  Handle<Symbol> symbol = isolate->factory()->NewPrivateNameSymbol(name);
  symbol->set_is_private_brand();
  return *symbol;
}

38
RUNTIME_FUNCTION(Runtime_CreatePrivateNameSymbol) {
39
  HandleScope scope(isolate);
40
  DCHECK_EQ(1, args.length());
41
  Handle<String> name = args.at<String>(0);
42
  Handle<Symbol> symbol = isolate->factory()->NewPrivateNameSymbol(name);
43 44
  return *symbol;
}
45

46 47 48
RUNTIME_FUNCTION(Runtime_SymbolDescriptiveString) {
  HandleScope scope(isolate);
  DCHECK_EQ(1, args.length());
49
  Handle<Symbol> symbol = args.at<Symbol>(0);
50
  IncrementalStringBuilder builder(isolate);
51
  builder.AppendCStringLiteral("Symbol(");
52 53
  if (symbol->description().IsString()) {
    builder.AppendString(handle(String::cast(symbol->description()), isolate));
54 55 56
  }
  builder.AppendCharacter(')');
  RETURN_RESULT_OR_FAILURE(isolate, builder.Finish());
57 58
}

59

60 61
RUNTIME_FUNCTION(Runtime_SymbolIsPrivate) {
  SealHandleScope shs(isolate);
62
  DCHECK_EQ(1, args.length());
63
  auto symbol = Symbol::cast(args[0]);
64
  return isolate->heap()->ToBoolean(symbol.is_private());
65
}
66 67
}  // namespace internal
}  // namespace v8