Remove usage of to-be-deprecated APIs from v8 core
authorjochen <jochen@chromium.org>
Wed, 3 Jun 2015 10:27:46 +0000 (03:27 -0700)
committerCommit bot <commit-bot@chromium.org>
Wed, 3 Jun 2015 10:27:56 +0000 (10:27 +0000)
Also turn on the macro to disable to-be-deprecated APIs for core

BUG=v8:4134
R=vogelheim@chromium.org
LOG=n

Review URL: https://codereview.chromium.org/1162363005

Cr-Commit-Position: refs/heads/master@{#28783}

include/v8.h
src/accessors.cc
src/api.cc
src/extensions/externalize-string-extension.cc
src/extensions/gc-extension.cc
src/extensions/statistics-extension.cc
src/full-codegen.cc
src/objects.cc
src/runtime/runtime-test.cc
tools/gyp/v8.gyp

index 3bd3dfbbd83cdf82c195aff85e4ae9c04b13861f..ab17d12b1c3b0bc49ed2e0675ef3306bef616e06 100644 (file)
@@ -7710,41 +7710,51 @@ template <class T> Value* Value::Cast(T* value) {
 
 
 Local<Boolean> Value::ToBoolean() const {
-  return ToBoolean(Isolate::GetCurrent());
+  return ToBoolean(Isolate::GetCurrent()->GetCurrentContext())
+      .FromMaybe(Local<Boolean>());
 }
 
 
 Local<Number> Value::ToNumber() const {
-  return ToNumber(Isolate::GetCurrent());
+  return ToNumber(Isolate::GetCurrent()->GetCurrentContext())
+      .FromMaybe(Local<Number>());
 }
 
 
 Local<String> Value::ToString() const {
-  return ToString(Isolate::GetCurrent());
+  return ToString(Isolate::GetCurrent()->GetCurrentContext())
+      .FromMaybe(Local<String>());
 }
 
 
 Local<String> Value::ToDetailString() const {
-  return ToDetailString(Isolate::GetCurrent());
+  return ToDetailString(Isolate::GetCurrent()->GetCurrentContext())
+      .FromMaybe(Local<String>());
 }
 
 
 Local<Object> Value::ToObject() const {
-  return ToObject(Isolate::GetCurrent());
+  return ToObject(Isolate::GetCurrent()->GetCurrentContext())
+      .FromMaybe(Local<Object>());
 }
 
 
 Local<Integer> Value::ToInteger() const {
-  return ToInteger(Isolate::GetCurrent());
+  return ToInteger(Isolate::GetCurrent()->GetCurrentContext())
+      .FromMaybe(Local<Integer>());
 }
 
 
 Local<Uint32> Value::ToUint32() const {
-  return ToUint32(Isolate::GetCurrent());
+  return ToUint32(Isolate::GetCurrent()->GetCurrentContext())
+      .FromMaybe(Local<Uint32>());
 }
 
 
-Local<Int32> Value::ToInt32() const { return ToInt32(Isolate::GetCurrent()); }
+Local<Int32> Value::ToInt32() const {
+  return ToInt32(Isolate::GetCurrent()->GetCurrentContext())
+      .FromMaybe(Local<Int32>());
+}
 
 
 Boolean* Boolean::Cast(v8::Value* value) {
index 4527001fc11c3d254f820fe1799f505f582d4ccb..711d6dc11f3321b3d075186d0281e588b93a7e29 100644 (file)
@@ -1402,9 +1402,19 @@ static void ModuleGetExport(
   JSModule* instance = JSModule::cast(*v8::Utils::OpenHandle(*info.Holder()));
   Context* context = Context::cast(instance->context());
   DCHECK(context->IsModuleContext());
-  int slot = info.Data()->Int32Value();
-  Object* value = context->get(slot);
   Isolate* isolate = instance->GetIsolate();
+  int slot = info.Data()
+                 ->Int32Value(info.GetIsolate()->GetCurrentContext())
+                 .FromMaybe(-1);
+  if (slot < 0 || slot >= context->length()) {
+    Handle<String> name = v8::Utils::OpenHandle(*property);
+
+    Handle<Object> exception = isolate->factory()->NewReferenceError(
+        MessageTemplate::kNotDefined, name);
+    isolate->ScheduleThrow(*exception);
+    return;
+  }
+  Object* value = context->get(slot);
   if (value->IsTheHole()) {
     Handle<String> name = v8::Utils::OpenHandle(*property);
 
@@ -1424,9 +1434,18 @@ static void ModuleSetExport(
   JSModule* instance = JSModule::cast(*v8::Utils::OpenHandle(*info.Holder()));
   Context* context = Context::cast(instance->context());
   DCHECK(context->IsModuleContext());
-  int slot = info.Data()->Int32Value();
+  Isolate* isolate = instance->GetIsolate();
+  int slot = info.Data()
+                 ->Int32Value(info.GetIsolate()->GetCurrentContext())
+                 .FromMaybe(-1);
+  if (slot < 0 || slot >= context->length()) {
+    Handle<String> name = v8::Utils::OpenHandle(*property);
+    Handle<Object> exception = isolate->factory()->NewReferenceError(
+        MessageTemplate::kNotDefined, name);
+    isolate->ScheduleThrow(*exception);
+    return;
+  }
   Object* old_value = context->get(slot);
-  Isolate* isolate = context->GetIsolate();
   if (old_value->IsTheHole()) {
     Handle<String> name = v8::Utils::OpenHandle(*property);
     Handle<Object> exception = isolate->factory()->NewReferenceError(
index 5140cd50604ecf88f736135ce1eae9022af7de8b..6f9a745ab44e5fe4d9d92c469066d6c375515e5a 100644 (file)
@@ -309,24 +309,32 @@ void V8::SetSnapshotDataBlob(StartupData* snapshot_blob) {
 }
 
 
-bool RunExtraCode(Isolate* isolate, const char* utf8_source) {
+bool RunExtraCode(Isolate* isolate, Local<Context> context,
+                  const char* utf8_source) {
   // Run custom script if provided.
   base::ElapsedTimer timer;
   timer.Start();
   TryCatch try_catch(isolate);
-  Local<String> source_string = String::NewFromUtf8(isolate, utf8_source);
-  if (try_catch.HasCaught()) return false;
-  ScriptOrigin origin(String::NewFromUtf8(isolate, "<embedded script>"));
+  Local<String> source_string;
+  if (!String::NewFromUtf8(isolate, utf8_source, NewStringType::kNormal)
+           .ToLocal(&source_string)) {
+    return false;
+  }
+  Local<String> resource_name =
+      String::NewFromUtf8(isolate, "<embedded script>", NewStringType::kNormal)
+          .ToLocalChecked();
+  ScriptOrigin origin(resource_name);
   ScriptCompiler::Source source(source_string, origin);
-  Local<Script> script = ScriptCompiler::Compile(isolate, &source);
-  if (try_catch.HasCaught()) return false;
-  script->Run();
+  Local<Script> script;
+  if (!ScriptCompiler::Compile(context, &source).ToLocal(&script)) return false;
+  if (script->Run(context).IsEmpty()) return false;
   if (i::FLAG_profile_deserialization) {
     i::PrintF("Executing custom snapshot script took %0.3f ms\n",
               timer.Elapsed().InMillisecondsF());
   }
   timer.Stop();
-  return !try_catch.HasCaught();
+  CHECK(!try_catch.HasCaught());
+  return true;
 }
 
 
@@ -367,7 +375,7 @@ StartupData V8::CreateSnapshotDataBlob(const char* custom_source) {
       if (custom_source != NULL) {
         metadata.set_embeds_script(true);
         Context::Scope context_scope(new_context);
-        if (!RunExtraCode(isolate, custom_source)) context.Reset();
+        if (!RunExtraCode(isolate, new_context, custom_source)) context.Reset();
       }
     }
     if (!context.IsEmpty()) {
@@ -2058,8 +2066,10 @@ Local<Script> Script::Compile(v8::Handle<String> source,
 
 Local<Script> Script::Compile(v8::Handle<String> source,
                               v8::Handle<String> file_name) {
+  auto str = Utils::OpenHandle(*source);
+  auto context = ContextFromHeapObject(str);
   ScriptOrigin origin(file_name);
-  return Compile(source, &origin);
+  return Compile(context, source, &origin).FromMaybe(Local<Script>());
 }
 
 
@@ -3629,8 +3639,19 @@ Maybe<bool> v8::Object::ForceSet(v8::Local<v8::Context> context,
 
 bool v8::Object::ForceSet(v8::Handle<Value> key, v8::Handle<Value> value,
                           v8::PropertyAttribute attribs) {
-  auto context = ContextFromHeapObject(Utils::OpenHandle(this));
-  return ForceSet(context, key, value, attribs).FromMaybe(false);
+  i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
+  PREPARE_FOR_EXECUTION_GENERIC(isolate, Local<Context>(),
+                                "v8::Object::ForceSet", false, i::HandleScope,
+                                false);
+  i::Handle<i::JSObject> self = Utils::OpenHandle(this);
+  i::Handle<i::Object> key_obj = Utils::OpenHandle(*key);
+  i::Handle<i::Object> value_obj = Utils::OpenHandle(*value);
+  has_pending_exception =
+      i::Runtime::DefineObjectProperty(self, key_obj, value_obj,
+                                       static_cast<PropertyAttributes>(attribs))
+          .is_null();
+  EXCEPTION_BAILOUT_CHECK_SCOPED(isolate, false);
+  return true;
 }
 
 
@@ -3867,11 +3888,13 @@ MaybeLocal<String> v8::Object::ObjectProtoToString(Local<Context> context) {
   //   return "[object " + c + "]";
 
   if (!name->IsString()) {
-    return v8::String::NewFromUtf8(v8_isolate, "[object ]");
+    return v8::String::NewFromUtf8(v8_isolate, "[object ]",
+                                   NewStringType::kNormal);
   }
   auto class_name = i::Handle<i::String>::cast(name);
   if (i::String::Equals(class_name, isolate->factory()->Arguments_string())) {
-    return v8::String::NewFromUtf8(v8_isolate, "[object Object]");
+    return v8::String::NewFromUtf8(v8_isolate, "[object Object]",
+                                   NewStringType::kNormal);
   }
   if (internal::FLAG_harmony_tostring) {
     PREPARE_FOR_EXECUTION(context, "v8::Object::ObjectProtoToString()", String);
@@ -3907,8 +3930,8 @@ MaybeLocal<String> v8::Object::ObjectProtoToString(Local<Context> context) {
   i::MemCopy(ptr, postfix, postfix_len * v8::internal::kCharSize);
 
   // Copy the buffer into a heap-allocated string and return it.
-  return v8::String::NewFromUtf8(v8_isolate, buf.start(), String::kNormalString,
-                                 buf_len);
+  return v8::String::NewFromUtf8(v8_isolate, buf.start(),
+                                 NewStringType::kNormal, buf_len);
 }
 
 
@@ -4472,7 +4495,8 @@ Local<Function> Function::New(Isolate* v8_isolate, FunctionCallback callback,
 
 
 Local<v8::Object> Function::NewInstance() const {
-  return NewInstance(0, NULL);
+  return NewInstance(Isolate::GetCurrent()->GetCurrentContext(), 0, NULL)
+      .FromMaybe(Local<Object>());
 }
 
 
@@ -5402,7 +5426,6 @@ void v8::Object::SetInternalField(int index, v8::Handle<Value> value) {
   if (!InternalFieldOK(obj, index, location)) return;
   i::Handle<i::Object> val = Utils::OpenHandle(*value);
   obj->SetInternalField(index, *val);
-  DCHECK(value->Equals(GetInternalField(index)));
 }
 
 
@@ -7506,14 +7529,15 @@ void Isolate::VisitHandlesForPartialDependence(
 
 String::Utf8Value::Utf8Value(v8::Handle<v8::Value> obj)
     : str_(NULL), length_(0) {
+  if (obj.IsEmpty()) return;
   i::Isolate* isolate = i::Isolate::Current();
   Isolate* v8_isolate = reinterpret_cast<Isolate*>(isolate);
-  if (obj.IsEmpty()) return;
   ENTER_V8(isolate);
   i::HandleScope scope(isolate);
+  Local<Context> context = v8_isolate->GetCurrentContext();
   TryCatch try_catch(v8_isolate);
-  Handle<String> str = obj->ToString(v8_isolate);
-  if (str.IsEmpty()) return;
+  Handle<String> str;
+  if (!obj->ToString(context).ToLocal(&str)) return;
   i::Handle<i::String> i_str = Utils::OpenHandle(*str);
   length_ = v8::Utf8Length(*i_str, isolate);
   str_ = i::NewArray<char>(length_ + 1);
@@ -7528,14 +7552,15 @@ String::Utf8Value::~Utf8Value() {
 
 String::Value::Value(v8::Handle<v8::Value> obj)
     : str_(NULL), length_(0) {
+  if (obj.IsEmpty()) return;
   i::Isolate* isolate = i::Isolate::Current();
   Isolate* v8_isolate = reinterpret_cast<Isolate*>(isolate);
-  if (obj.IsEmpty()) return;
   ENTER_V8(isolate);
   i::HandleScope scope(isolate);
+  Local<Context> context = v8_isolate->GetCurrentContext();
   TryCatch try_catch(v8_isolate);
-  Handle<String> str = obj->ToString(v8_isolate);
-  if (str.IsEmpty()) return;
+  Handle<String> str;
+  if (!obj->ToString(context).ToLocal(&str)) return;
   length_ = str->Length();
   str_ = i::NewArray<uint16_t>(length_ + 1);
   str->Write(str_);
index e45cb67efa899dd2cf0661dc96bcbb8eb9548793..e2213b0bd4b65c2455e8d86a8b911a06eece49d4 100644 (file)
@@ -54,28 +54,36 @@ ExternalizeStringExtension::GetNativeFunctionTemplate(
 void ExternalizeStringExtension::Externalize(
     const v8::FunctionCallbackInfo<v8::Value>& args) {
   if (args.Length() < 1 || !args[0]->IsString()) {
-    args.GetIsolate()->ThrowException(v8::String::NewFromUtf8(
-        args.GetIsolate(),
-        "First parameter to externalizeString() must be a string."));
+    args.GetIsolate()->ThrowException(
+        v8::String::NewFromUtf8(
+            args.GetIsolate(),
+            "First parameter to externalizeString() must be a string.",
+            NewStringType::kNormal).ToLocalChecked());
     return;
   }
   bool force_two_byte = false;
   if (args.Length() >= 2) {
     if (args[1]->IsBoolean()) {
-      force_two_byte = args[1]->BooleanValue();
+      force_two_byte =
+          args[1]
+              ->BooleanValue(args.GetIsolate()->GetCurrentContext())
+              .FromJust();
     } else {
-      args.GetIsolate()->ThrowException(v8::String::NewFromUtf8(
-        args.GetIsolate(),
-        "Second parameter to externalizeString() must be a boolean."));
+      args.GetIsolate()->ThrowException(
+          v8::String::NewFromUtf8(
+              args.GetIsolate(),
+              "Second parameter to externalizeString() must be a boolean.",
+              NewStringType::kNormal).ToLocalChecked());
       return;
     }
   }
   bool result = false;
   Handle<String> string = Utils::OpenHandle(*args[0].As<v8::String>());
   if (string->IsExternalString()) {
-    args.GetIsolate()->ThrowException(v8::String::NewFromUtf8(
-        args.GetIsolate(),
-        "externalizeString() can't externalize twice."));
+    args.GetIsolate()->ThrowException(
+        v8::String::NewFromUtf8(args.GetIsolate(),
+                                "externalizeString() can't externalize twice.",
+                                NewStringType::kNormal).ToLocalChecked());
     return;
   }
   if (string->IsOneByteRepresentation() && !force_two_byte) {
@@ -102,8 +110,10 @@ void ExternalizeStringExtension::Externalize(
     if (!result) delete resource;
   }
   if (!result) {
-    args.GetIsolate()->ThrowException(v8::String::NewFromUtf8(
-        args.GetIsolate(), "externalizeString() failed."));
+    args.GetIsolate()->ThrowException(
+        v8::String::NewFromUtf8(args.GetIsolate(),
+                                "externalizeString() failed.",
+                                NewStringType::kNormal).ToLocalChecked());
     return;
   }
 }
@@ -112,9 +122,11 @@ void ExternalizeStringExtension::Externalize(
 void ExternalizeStringExtension::IsOneByte(
     const v8::FunctionCallbackInfo<v8::Value>& args) {
   if (args.Length() != 1 || !args[0]->IsString()) {
-    args.GetIsolate()->ThrowException(v8::String::NewFromUtf8(
-        args.GetIsolate(),
-        "isOneByteString() requires a single string argument."));
+    args.GetIsolate()->ThrowException(
+        v8::String::NewFromUtf8(
+            args.GetIsolate(),
+            "isOneByteString() requires a single string argument.",
+            NewStringType::kNormal).ToLocalChecked());
     return;
   }
   bool is_one_byte =
index fbe40a6195397be966c60dfadebd571649162377..f4026f57dcb477ac3033122d90ef1c719d71579f 100644 (file)
@@ -19,8 +19,11 @@ v8::Handle<v8::FunctionTemplate> GCExtension::GetNativeFunctionTemplate(
 
 void GCExtension::GC(const v8::FunctionCallbackInfo<v8::Value>& args) {
   args.GetIsolate()->RequestGarbageCollectionForTesting(
-      args[0]->BooleanValue() ? v8::Isolate::kMinorGarbageCollection
-                              : v8::Isolate::kFullGarbageCollection);
+      args[0]
+              ->BooleanValue(args.GetIsolate()->GetCurrentContext())
+              .FromMaybe(false)
+          ? v8::Isolate::kMinorGarbageCollection
+          : v8::Isolate::kFullGarbageCollection);
 }
 
 }  // namespace internal
index d1bb0918403f6c02ca0983d1a723bac5432ac30d..7093625270ee35ac1d087bd0c222bc038a289cef 100644 (file)
@@ -24,8 +24,11 @@ static void AddCounter(v8::Isolate* isolate,
                        StatsCounter* counter,
                        const char* name) {
   if (counter->Enabled()) {
-    object->Set(v8::String::NewFromUtf8(isolate, name),
-                v8::Number::New(isolate, *counter->GetInternalPointer()));
+    object->Set(isolate->GetCurrentContext(),
+                v8::String::NewFromUtf8(isolate, name, NewStringType::kNormal)
+                    .ToLocalChecked(),
+                v8::Number::New(isolate, *counter->GetInternalPointer()))
+        .FromJust();
   }
 }
 
@@ -33,8 +36,10 @@ static void AddNumber(v8::Isolate* isolate,
                       v8::Local<v8::Object> object,
                       intptr_t value,
                       const char* name) {
-  object->Set(v8::String::NewFromUtf8(isolate, name),
-              v8::Number::New(isolate, static_cast<double>(value)));
+  object->Set(isolate->GetCurrentContext(),
+              v8::String::NewFromUtf8(isolate, name, NewStringType::kNormal)
+                  .ToLocalChecked(),
+              v8::Number::New(isolate, static_cast<double>(value))).FromJust();
 }
 
 
@@ -42,8 +47,10 @@ static void AddNumber64(v8::Isolate* isolate,
                         v8::Local<v8::Object> object,
                         int64_t value,
                         const char* name) {
-  object->Set(v8::String::NewFromUtf8(isolate, name),
-              v8::Number::New(isolate, static_cast<double>(value)));
+  object->Set(isolate->GetCurrentContext(),
+              v8::String::NewFromUtf8(isolate, name, NewStringType::kNormal)
+                  .ToLocalChecked(),
+              v8::Number::New(isolate, static_cast<double>(value))).FromJust();
 }
 
 
@@ -54,7 +61,9 @@ void StatisticsExtension::GetCounters(
 
   if (args.Length() > 0) {  // GC if first argument evaluates to true.
     if (args[0]->IsBoolean() &&
-        args[0]->ToBoolean(args.GetIsolate())->Value()) {
+        args[0]
+            ->BooleanValue(args.GetIsolate()->GetCurrentContext())
+            .FromMaybe(false)) {
       heap->CollectAllGarbage(Heap::kNoGCFlags, "counters extension");
     }
   }
index 8c6347d7d1702195de9f4cb9d8f432d21c0d8fa2..5103f3ac89de75f49fd18a3823a4848a33da3601 100644 (file)
@@ -1410,15 +1410,19 @@ void FullCodeGenerator::VisitNativeFunctionLiteral(
     NativeFunctionLiteral* expr) {
   Comment cmnt(masm_, "[ NativeFunctionLiteral");
 
+  v8::Isolate* v8_isolate = reinterpret_cast<v8::Isolate*>(isolate());
+
   // Compute the function template for the native function.
   Handle<String> name = expr->name();
   v8::Handle<v8::FunctionTemplate> fun_template =
-      expr->extension()->GetNativeFunctionTemplate(
-          reinterpret_cast<v8::Isolate*>(isolate()), v8::Utils::ToLocal(name));
+      expr->extension()->GetNativeFunctionTemplate(v8_isolate,
+                                                   v8::Utils::ToLocal(name));
   DCHECK(!fun_template.IsEmpty());
 
   // Instantiate the function and create a shared function info from it.
-  Handle<JSFunction> fun = Utils::OpenHandle(*fun_template->GetFunction());
+  Handle<JSFunction> fun = Utils::OpenHandle(
+      *fun_template->GetFunction(v8_isolate->GetCurrentContext())
+           .ToLocalChecked());
   const int literals = fun->NumberOfLiterals();
   Handle<Code> code = Handle<Code>(fun->shared()->code());
   Handle<Code> construct_stub = Handle<Code>(fun->shared()->construct_stub());
index 2390802777914bfe9b110c9e724b47a76f6bdb76..b0222f660f79d6adb3d8e75d3210133353bb96f6 100644 (file)
@@ -4290,7 +4290,9 @@ Maybe<PropertyAttributes> JSObject::GetPropertyAttributesWithInterceptor(
     }
     if (!result.IsEmpty()) {
       DCHECK(result->IsInt32());
-      return Just(static_cast<PropertyAttributes>(result->Int32Value()));
+      return Just(static_cast<PropertyAttributes>(
+          result->Int32Value(reinterpret_cast<v8::Isolate*>(isolate)
+                                 ->GetCurrentContext()).FromJust()));
     }
   } else if (!interceptor->getter()->IsUndefined()) {
     // TODO(verwaest): Use GetPropertyWithInterceptor?
index 2c38f12eb77c47f8606d9354d67d575f498d4e51..59b695c6f7803d74e0dd4b508d8d0aae2598ff13 100644 (file)
@@ -216,11 +216,14 @@ RUNTIME_FUNCTION(Runtime_GetOptimizationCount) {
 RUNTIME_FUNCTION(Runtime_GetUndetectable) {
   HandleScope scope(isolate);
   DCHECK(args.length() == 0);
+  v8::Isolate* v8_isolate = reinterpret_cast<v8::Isolate*>(isolate);
 
-  Local<v8::ObjectTemplate> desc =
-      v8::ObjectTemplate::New((v8::Isolate*)isolate);
-  desc->MarkAsUndetectable();  // undetectable
-  Local<v8::Object> obj = desc->NewInstance();
+  Local<v8::ObjectTemplate> desc = v8::ObjectTemplate::New(v8_isolate);
+  desc->MarkAsUndetectable();
+  Local<v8::Object> obj;
+  if (!desc->NewInstance(v8_isolate->GetCurrentContext()).ToLocal(&obj)) {
+    return nullptr;
+  }
   return *Utils::OpenHandle(*obj);
 }
 
index f2a8dd39b8787fcdc7501ebd0a0f9320df934f5c..59f7ca1392a6f6663a14cc3a5cd51fae7489ab50 100644 (file)
       'include_dirs+': [
         '../..',
       ],
+      'defines': [
+        # TODO(jochen): Remove again after this is globally turned on.
+        'V8_IMMINENT_DEPRECATION_WARNINGS',
+      ],
       'sources': [  ### gcmole(all) ###
         '../../src/accessors.cc',
         '../../src/accessors.h',