From 5234d9977d637668e66b9e2e9f0456c6f97f749e Mon Sep 17 00:00:00 2001 From: dcarney Date: Wed, 11 Mar 2015 02:32:57 -0700 Subject: [PATCH] convert most remaining api functions needing context to maybes BUG= Review URL: https://codereview.chromium.org/993223003 Cr-Commit-Position: refs/heads/master@{#27126} --- include/v8-debug.h | 15 +++- include/v8.h | 16 +++- src/api.cc | 216 ++++++++++++++++++++++++++--------------------------- 3 files changed, 128 insertions(+), 119 deletions(-) diff --git a/include/v8-debug.h b/include/v8-debug.h index 6abf4e0..ae11a2a 100644 --- a/include/v8-debug.h +++ b/include/v8-debug.h @@ -202,13 +202,22 @@ class V8_EXPORT Debug { * } * \endcode */ - static Local Call(v8::Handle fun, - Handle data = Handle()); + static V8_DEPRECATE_SOON( + "Use maybe version", + Local Call(v8::Handle fun, + Handle data = Handle())); + // TODO(dcarney): data arg should be a MaybeLocal + static MaybeLocal Call(Local context, + v8::Handle fun, + Handle data = Handle()); /** * Returns a mirror object for the given object. */ - static Local GetMirror(v8::Handle obj); + static V8_DEPRECATE_SOON("Use maybe version", + Local GetMirror(v8::Handle obj)); + static MaybeLocal GetMirror(Local context, + v8::Handle obj); /** * Makes V8 process all pending debug messages. diff --git a/include/v8.h b/include/v8.h index 904e078..aaebf51 100644 --- a/include/v8.h +++ b/include/v8.h @@ -2926,7 +2926,9 @@ class V8_EXPORT Array : public Object { * Clones an element at index |index|. Returns an empty * handle if cloning fails (for any reason). */ - Local CloneElementAt(uint32_t index); + V8_DEPRECATE_SOON("Use maybe version", + Local CloneElementAt(uint32_t index)); + MaybeLocal CloneElementAt(Local context, uint32_t index); /** * Creates a JavaScript array with the given length. If the length @@ -3682,7 +3684,11 @@ class V8_EXPORT RegExp : public Object { * static_cast(kGlobal | kMultiline)) * is equivalent to evaluating "/foo/gm". */ - static Local New(Handle pattern, Flags flags); + static V8_DEPRECATE_SOON("Use maybe version", + Local New(Handle pattern, + Flags flags)); + static MaybeLocal New(Local context, Handle pattern, + Flags flags); /** * Returns the value of the source property: a string representing @@ -4066,7 +4072,8 @@ class V8_EXPORT FunctionTemplate : public Template { int length = 0); /** Returns the unique function instance in the current execution context.*/ - Local GetFunction(); + V8_DEPRECATE_SOON("Use maybe version", Local GetFunction()); + MaybeLocal GetFunction(Local context); /** * Set the call-handler callback for a FunctionTemplate. This @@ -4209,7 +4216,8 @@ class V8_EXPORT ObjectTemplate : public Template { static V8_DEPRECATE_SOON("Use isolate version", Local New()); /** Creates a new instance of this template.*/ - Local NewInstance(); + V8_DEPRECATE_SOON("Use maybe version", Local NewInstance()); + MaybeLocal NewInstance(Local context); /** * Sets an accessor on the object template. diff --git a/src/api.cc b/src/api.cc index 020335a..e7667ba 100644 --- a/src/api.cc +++ b/src/api.cc @@ -59,13 +59,6 @@ namespace v8 { -#define ON_BAILOUT(isolate, location, code) \ - if (IsExecutionTerminatingCheck(isolate)) { \ - code; \ - UNREACHABLE(); \ - } - - #define EXCEPTION_PREAMBLE(isolate) \ (isolate)->handle_scope_implementer()->IncrementCallDepth(); \ DCHECK(!(isolate)->external_caught_exception()); \ @@ -5485,7 +5478,6 @@ Local v8::Context::New( v8::Handle global_object) { i::Isolate* isolate = reinterpret_cast(external_isolate); LOG_API(isolate, "Context::New"); - ON_BAILOUT(isolate, "v8::Context::New()", return Local()); i::HandleScope scope(isolate); ExtensionConfiguration no_extensions; if (extensions == NULL) extensions = &no_extensions; @@ -5569,44 +5561,45 @@ void Context::SetErrorMessageForCodeGenerationFromStrings( } +MaybeLocal ObjectTemplate::NewInstance(Local context) { + PREPARE_FOR_EXECUTION(context, "v8::ObjectTemplate::NewInstance()", Object); + auto self = Utils::OpenHandle(this); + Local result; + has_pending_exception = + !ToLocal(i::ApiNatives::InstantiateObject(self), &result); + RETURN_ON_FAILED_EXECUTION(Object); + RETURN_ESCAPED(result); +} + + Local ObjectTemplate::NewInstance() { - i::Handle info = Utils::OpenHandle(this); - i::Isolate* isolate = info->GetIsolate(); - ON_BAILOUT(isolate, "v8::ObjectTemplate::NewInstance()", - return Local()); - LOG_API(isolate, "ObjectTemplate::NewInstance"); - ENTER_V8(isolate); - EXCEPTION_PREAMBLE(isolate); - i::Handle obj; + auto context = ContextFromHeapObject(Utils::OpenHandle(this)); + RETURN_TO_LOCAL_UNCHECKED(NewInstance(context), Object); +} + + +MaybeLocal FunctionTemplate::GetFunction(Local context) { + PREPARE_FOR_EXECUTION(context, "v8::FunctionTemplate::GetFunction()", + Function); + auto self = Utils::OpenHandle(this); + Local result; has_pending_exception = - !i::ApiNatives::InstantiateObject(info).ToHandle(&obj); - EXCEPTION_BAILOUT_CHECK(isolate, Local()); - return Utils::ToLocal(i::Handle::cast(obj)); + !ToLocal(i::ApiNatives::InstantiateFunction(self), &result); + RETURN_ON_FAILED_EXECUTION(Function); + RETURN_ESCAPED(result); } Local FunctionTemplate::GetFunction() { - i::Handle info = Utils::OpenHandle(this); - i::Isolate* isolate = info->GetIsolate(); - ON_BAILOUT(isolate, "v8::FunctionTemplate::GetFunction()", - return Local()); - LOG_API(isolate, "FunctionTemplate::GetFunction"); - ENTER_V8(isolate); - EXCEPTION_PREAMBLE(isolate); - i::Handle obj; - has_pending_exception = - !i::ApiNatives::InstantiateFunction(info).ToHandle(&obj); - EXCEPTION_BAILOUT_CHECK(isolate, Local()); - return Utils::ToLocal(i::Handle::cast(obj)); + auto context = ContextFromHeapObject(Utils::OpenHandle(this)); + RETURN_TO_LOCAL_UNCHECKED(GetFunction(context), Function); } bool FunctionTemplate::HasInstance(v8::Handle value) { - i::Handle info = Utils::OpenHandle(this); - i::Isolate* isolate = info->GetIsolate(); - ON_BAILOUT(isolate, "v8::FunctionTemplate::HasInstanceOf()", return false); - i::Object* obj = *Utils::OpenHandle(*value); - return info->IsTemplateFor(obj); + auto self = Utils::OpenHandle(this); + auto obj = Utils::OpenHandle(*value); + return self->IsTemplateFor(*obj); } @@ -5687,7 +5680,6 @@ inline Local NewString(Isolate* v8_isolate, String::NewStringType type, int length) { i::Isolate* isolate = reinterpret_cast(v8_isolate); - ON_BAILOUT(isolate, location, return Local()); LOG_API(isolate, env); if (length == 0) { return String::Empty(v8_isolate); @@ -6000,13 +5992,9 @@ double v8::Date::ValueOf() const { void v8::Date::DateTimeConfigurationChangeNotification(Isolate* isolate) { i::Isolate* i_isolate = reinterpret_cast(isolate); - ON_BAILOUT(i_isolate, "v8::Date::DateTimeConfigurationChangeNotification()", - return); LOG_API(i_isolate, "Date::DateTimeConfigurationChangeNotification"); ENTER_V8(i_isolate); - i_isolate->date_cache()->ResetDateCache(); - if (!i_isolate->eternal_handles()->Exists( i::EternalHandles::DATE_CACHE_VERSION)) { return; @@ -6035,18 +6023,24 @@ static i::Handle RegExpFlagsToString(RegExp::Flags flags) { } -Local v8::RegExp::New(Handle pattern, - Flags flags) { - i::Isolate* isolate = Utils::OpenHandle(*pattern)->GetIsolate(); - LOG_API(isolate, "RegExp::New"); - ENTER_V8(isolate); - EXCEPTION_PREAMBLE(isolate); - i::Handle obj; - has_pending_exception = !i::Execution::NewJSRegExp( - Utils::OpenHandle(*pattern), - RegExpFlagsToString(flags)).ToHandle(&obj); - EXCEPTION_BAILOUT_CHECK(isolate, Local()); - return Utils::ToLocal(i::Handle::cast(obj)); +MaybeLocal v8::RegExp::New(Local context, + Handle pattern, Flags flags) { + PREPARE_FOR_EXECUTION(context, "RegExp::New", RegExp); + Local result; + has_pending_exception = + !ToLocal(i::Execution::NewJSRegExp(Utils::OpenHandle(*pattern), + RegExpFlagsToString(flags)), + &result); + RETURN_ON_FAILED_EXECUTION(RegExp); + RETURN_ESCAPED(result); +} + + +Local v8::RegExp::New(Handle pattern, Flags flags) { + auto isolate = + reinterpret_cast(Utils::OpenHandle(*pattern)->GetIsolate()); + auto context = isolate->GetCurrentContext(); + RETURN_TO_LOCAL_UNCHECKED(New(context, pattern, flags), RegExp); } @@ -6096,26 +6090,26 @@ uint32_t v8::Array::Length() const { } -Local Array::CloneElementAt(uint32_t index) { - i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate(); - ON_BAILOUT(isolate, "v8::Array::CloneElementAt()", return Local()); - i::Handle self = Utils::OpenHandle(this); - if (!self->HasFastObjectElements()) { - return Local(); - } +MaybeLocal Array::CloneElementAt(Local context, + uint32_t index) { + PREPARE_FOR_EXECUTION(context, "v8::Array::CloneElementAt()", Object); + auto self = Utils::OpenHandle(this); + if (!self->HasFastObjectElements()) return Local(); i::FixedArray* elms = i::FixedArray::cast(self->elements()); i::Object* paragon = elms->get(index); - if (!paragon->IsJSObject()) { - return Local(); - } + if (!paragon->IsJSObject()) return Local(); i::Handle paragon_handle(i::JSObject::cast(paragon)); - EXCEPTION_PREAMBLE(isolate); - ENTER_V8(isolate); - i::Handle result = - isolate->factory()->CopyJSObject(paragon_handle); - has_pending_exception = result.is_null(); - EXCEPTION_BAILOUT_CHECK(isolate, Local()); - return Utils::ToLocal(result); + Local result; + has_pending_exception = ToLocal( + isolate->factory()->CopyJSObject(paragon_handle), &result); + RETURN_ON_FAILED_EXECUTION(Object); + RETURN_ESCAPED(result); +} + + +Local Array::CloneElementAt(uint32_t index) { + auto context = ContextFromHeapObject(Utils::OpenHandle(this)); + RETURN_TO_LOCAL_UNCHECKED(CloneElementAt(context, index), Object); } @@ -7054,7 +7048,6 @@ bool Isolate::IsDead() { bool Isolate::AddMessageListener(MessageCallback that, Handle data) { i::Isolate* isolate = reinterpret_cast(this); - ON_BAILOUT(isolate, "v8::V8::AddMessageListener()", return false); ENTER_V8(isolate); i::HandleScope scope(isolate); NeanderArray listeners(isolate->factory()->message_listeners()); @@ -7069,7 +7062,6 @@ bool Isolate::AddMessageListener(MessageCallback that, Handle data) { void Isolate::RemoveMessageListeners(MessageCallback that) { i::Isolate* isolate = reinterpret_cast(this); - ON_BAILOUT(isolate, "v8::V8::RemoveMessageListeners()", return); ENTER_V8(isolate); i::HandleScope scope(isolate); NeanderArray listeners(isolate->factory()->message_listeners()); @@ -7187,7 +7179,6 @@ String::Value::~Value() { Local Exception::NAME(v8::Handle raw_message) { \ i::Isolate* isolate = i::Isolate::Current(); \ LOG_API(isolate, #NAME); \ - ON_BAILOUT(isolate, "v8::Exception::" #NAME "()", return Local()); \ ENTER_V8(isolate); \ i::Object* error; \ { \ @@ -7233,7 +7224,6 @@ Local Exception::GetStackTrace(Handle exception) { bool Debug::SetDebugEventListener(EventCallback that, Handle data) { i::Isolate* isolate = i::Isolate::Current(); - ON_BAILOUT(isolate, "v8::Debug::SetDebugEventListener()", return false); ENTER_V8(isolate); i::HandleScope scope(isolate); i::Handle foreign = isolate->factory()->undefined_value(); @@ -7286,52 +7276,54 @@ void Debug::SendCommand(Isolate* isolate, } -Local Debug::Call(v8::Handle fun, - v8::Handle data) { - i::Isolate* isolate = i::Isolate::Current(); - ON_BAILOUT(isolate, "v8::Debug::Call()", return Local()); - ENTER_V8(isolate); - i::MaybeHandle maybe_result; - EXCEPTION_PREAMBLE(isolate); +MaybeLocal Debug::Call(Local context, + v8::Handle fun, + v8::Handle data) { + PREPARE_FOR_EXECUTION(context, "v8::Debug::Call()", Value); + i::Handle data_obj; if (data.IsEmpty()) { - maybe_result = isolate->debug()->Call( - Utils::OpenHandle(*fun), isolate->factory()->undefined_value()); + data_obj = isolate->factory()->undefined_value(); } else { - maybe_result = isolate->debug()->Call( - Utils::OpenHandle(*fun), Utils::OpenHandle(*data)); + data_obj = Utils::OpenHandle(*data); } - i::Handle result; - has_pending_exception = !maybe_result.ToHandle(&result); - EXCEPTION_BAILOUT_CHECK(isolate, Local()); - return Utils::ToLocal(result); + Local result; + has_pending_exception = + !ToLocal(isolate->debug()->Call(Utils::OpenHandle(*fun), data_obj), + &result); + RETURN_ON_FAILED_EXECUTION(Value); + RETURN_ESCAPED(result); } -Local Debug::GetMirror(v8::Handle obj) { - i::Isolate* isolate = i::Isolate::Current(); - ON_BAILOUT(isolate, "v8::Debug::GetMirror()", return Local()); - ENTER_V8(isolate); - v8::EscapableHandleScope scope(reinterpret_cast(isolate)); +Local Debug::Call(v8::Handle fun, + v8::Handle data) { + auto context = ContextFromHeapObject(Utils::OpenHandle(*fun)); + RETURN_TO_LOCAL_UNCHECKED(Call(context, fun, data), Value); +} + + +MaybeLocal Debug::GetMirror(Local context, + v8::Handle obj) { + PREPARE_FOR_EXECUTION(context, "v8::Debug::GetMirror()", Value); i::Debug* isolate_debug = isolate->debug(); - EXCEPTION_PREAMBLE(isolate); has_pending_exception = !isolate_debug->Load(); - v8::Local result; - if (!has_pending_exception) { - i::Handle debug( - isolate_debug->debug_context()->global_object()); - i::Handle name = isolate->factory()->InternalizeOneByteString( - STATIC_CHAR_VECTOR("MakeMirror")); - i::Handle fun_obj = - i::Object::GetProperty(debug, name).ToHandleChecked(); - i::Handle fun = i::Handle::cast(fun_obj); - v8::Handle v8_fun = Utils::ToLocal(fun); - const int kArgc = 1; - v8::Handle argv[kArgc] = { obj }; - result = v8_fun->Call(Utils::ToLocal(debug), kArgc, argv); - has_pending_exception = result.IsEmpty(); - } - EXCEPTION_BAILOUT_CHECK(isolate, Local()); - return scope.Escape(result); + RETURN_ON_FAILED_EXECUTION(Value); + i::Handle debug(isolate_debug->debug_context()->global_object()); + auto name = isolate->factory()->NewStringFromStaticChars("MakeMirror"); + auto fun_obj = i::Object::GetProperty(debug, name).ToHandleChecked(); + auto v8_fun = Utils::ToLocal(i::Handle::cast(fun_obj)); + const int kArgc = 1; + v8::Handle argv[kArgc] = {obj}; + Local result; + has_pending_exception = !v8_fun->Call(context, Utils::ToLocal(debug), kArgc, + argv).ToLocal(&result); + RETURN_ON_FAILED_EXECUTION(Value); + RETURN_ESCAPED(result); +} + + +Local Debug::GetMirror(v8::Handle obj) { + RETURN_TO_LOCAL_UNCHECKED(GetMirror(Local(), obj), Value); } -- 2.7.4