From 50114c27eb673f1d1bee2c616fbc51b982ef2170 Mon Sep 17 00:00:00 2001 From: "yangguo@chromium.org" Date: Mon, 7 Apr 2014 11:32:32 +0000 Subject: [PATCH] Handlify LiveEdit. R=ulan@chromium.org Review URL: https://codereview.chromium.org/227483007 git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@20540 ce2b1a6d-e550-0410-aec6-3dcde31c8c00 --- src/liveedit.cc | 264 +++++++++++++++----------------------------------------- src/liveedit.h | 172 +++++++++++++++++++++++++++++++++--- src/runtime.cc | 35 ++++---- 3 files changed, 250 insertions(+), 221 deletions(-) diff --git a/src/liveedit.cc b/src/liveedit.cc index f70394d..6fb917c 100644 --- a/src/liveedit.cc +++ b/src/liveedit.cc @@ -635,168 +635,72 @@ static int GetArrayLength(Handle array) { } -// Simple helper class that creates more or less typed structures over -// JSArray object. This is an adhoc method of passing structures from C++ -// to JavaScript. -template -class JSArrayBasedStruct { - public: - static S Create(Isolate* isolate) { - Factory* factory = isolate->factory(); - Handle array = factory->NewJSArray(S::kSize_); - return S(array); - } - static S cast(Object* object) { - JSArray* array = JSArray::cast(object); - Handle array_handle(array); - return S(array_handle); - } - explicit JSArrayBasedStruct(Handle array) : array_(array) { - } - Handle GetJSArray() { - return array_; - } - Isolate* isolate() const { - return array_->GetIsolate(); - } +void FunctionInfoWrapper::SetInitialProperties(Handle name, + int start_position, + int end_position, + int param_num, + int literal_count, + int parent_index) { + HandleScope scope(isolate()); + this->SetField(kFunctionNameOffset_, name); + this->SetSmiValueField(kStartPositionOffset_, start_position); + this->SetSmiValueField(kEndPositionOffset_, end_position); + this->SetSmiValueField(kParamNumOffset_, param_num); + this->SetSmiValueField(kLiteralNumOffset_, literal_count); + this->SetSmiValueField(kParentIndexOffset_, parent_index); +} - protected: - void SetField(int field_position, Handle value) { - SetElementSloppy(array_, field_position, value); - } - void SetSmiValueField(int field_position, int value) { - SetElementSloppy(array_, - field_position, - Handle(Smi::FromInt(value), isolate())); - } - Handle GetField(int field_position) { - return Object::GetElementNoExceptionThrown( - isolate(), array_, field_position); - } - int GetSmiValueField(int field_position) { - Handle res = GetField(field_position); - return Handle::cast(res)->value(); - } - private: - Handle array_; -}; +void FunctionInfoWrapper::SetFunctionCode(Handle function_code, + Handle code_scope_info) { + Handle code_wrapper = WrapInJSValue(function_code); + this->SetField(kCodeOffset_, code_wrapper); + Handle scope_wrapper = WrapInJSValue(code_scope_info); + this->SetField(kCodeScopeInfoOffset_, scope_wrapper); +} -// Represents some function compilation details. This structure will be used -// from JavaScript. It contains Code object, which is kept wrapped -// into a BlindReference for sanitizing reasons. -class FunctionInfoWrapper : public JSArrayBasedStruct { - public: - explicit FunctionInfoWrapper(Handle array) - : JSArrayBasedStruct(array) { - } - void SetInitialProperties(Handle name, int start_position, - int end_position, int param_num, - int literal_count, int parent_index) { - HandleScope scope(isolate()); - this->SetField(kFunctionNameOffset_, name); - this->SetSmiValueField(kStartPositionOffset_, start_position); - this->SetSmiValueField(kEndPositionOffset_, end_position); - this->SetSmiValueField(kParamNumOffset_, param_num); - this->SetSmiValueField(kLiteralNumOffset_, literal_count); - this->SetSmiValueField(kParentIndexOffset_, parent_index); - } - void SetFunctionCode(Handle function_code, - Handle code_scope_info) { - Handle code_wrapper = WrapInJSValue(function_code); - this->SetField(kCodeOffset_, code_wrapper); - Handle scope_wrapper = WrapInJSValue(code_scope_info); - this->SetField(kCodeScopeInfoOffset_, scope_wrapper); - } - void SetFunctionScopeInfo(Handle scope_info_array) { - this->SetField(kFunctionScopeInfoOffset_, scope_info_array); - } - void SetSharedFunctionInfo(Handle info) { - Handle info_holder = WrapInJSValue(info); - this->SetField(kSharedFunctionInfoOffset_, info_holder); - } - int GetLiteralCount() { - return this->GetSmiValueField(kLiteralNumOffset_); - } - int GetParentIndex() { - return this->GetSmiValueField(kParentIndexOffset_); - } - Handle GetFunctionCode() { - Handle element = this->GetField(kCodeOffset_); - Handle value_wrapper = Handle::cast(element); - Handle raw_result = UnwrapJSValue(value_wrapper); - CHECK(raw_result->IsCode()); - return Handle::cast(raw_result); - } - Handle GetCodeScopeInfo() { - Handle element = this->GetField(kCodeScopeInfoOffset_); - return UnwrapJSValue(Handle::cast(element)); - } - int GetStartPosition() { - return this->GetSmiValueField(kStartPositionOffset_); - } - int GetEndPosition() { - return this->GetSmiValueField(kEndPositionOffset_); - } +void FunctionInfoWrapper::SetSharedFunctionInfo( + Handle info) { + Handle info_holder = WrapInJSValue(info); + this->SetField(kSharedFunctionInfoOffset_, info_holder); +} - private: - static const int kFunctionNameOffset_ = 0; - static const int kStartPositionOffset_ = 1; - static const int kEndPositionOffset_ = 2; - static const int kParamNumOffset_ = 3; - static const int kCodeOffset_ = 4; - static const int kCodeScopeInfoOffset_ = 5; - static const int kFunctionScopeInfoOffset_ = 6; - static const int kParentIndexOffset_ = 7; - static const int kSharedFunctionInfoOffset_ = 8; - static const int kLiteralNumOffset_ = 9; - static const int kSize_ = 10; - - friend class JSArrayBasedStruct; -}; +Handle FunctionInfoWrapper::GetFunctionCode() { + Handle element = this->GetField(kCodeOffset_); + Handle value_wrapper = Handle::cast(element); + Handle raw_result = UnwrapJSValue(value_wrapper); + CHECK(raw_result->IsCode()); + return Handle::cast(raw_result); +} -// Wraps SharedFunctionInfo along with some of its fields for passing it -// back to JavaScript. SharedFunctionInfo object itself is additionally -// wrapped into BlindReference for sanitizing reasons. -class SharedInfoWrapper : public JSArrayBasedStruct { - public: - static bool IsInstance(Handle array) { - return array->length() == Smi::FromInt(kSize_) && - Object::GetElementNoExceptionThrown( - array->GetIsolate(), array, kSharedInfoOffset_)->IsJSValue(); - } - explicit SharedInfoWrapper(Handle array) - : JSArrayBasedStruct(array) { - } +Handle FunctionInfoWrapper::GetCodeScopeInfo() { + Handle element = this->GetField(kCodeScopeInfoOffset_); + return UnwrapJSValue(Handle::cast(element)); +} - void SetProperties(Handle name, int start_position, int end_position, - Handle info) { - HandleScope scope(isolate()); - this->SetField(kFunctionNameOffset_, name); - Handle info_holder = WrapInJSValue(info); - this->SetField(kSharedInfoOffset_, info_holder); - this->SetSmiValueField(kStartPositionOffset_, start_position); - this->SetSmiValueField(kEndPositionOffset_, end_position); - } - Handle GetInfo() { - Handle element = this->GetField(kSharedInfoOffset_); - Handle value_wrapper = Handle::cast(element); - return UnwrapSharedFunctionInfoFromJSValue(value_wrapper); - } - private: - static const int kFunctionNameOffset_ = 0; - static const int kStartPositionOffset_ = 1; - static const int kEndPositionOffset_ = 2; - static const int kSharedInfoOffset_ = 3; - static const int kSize_ = 4; +void SharedInfoWrapper::SetProperties(Handle name, + int start_position, + int end_position, + Handle info) { + HandleScope scope(isolate()); + this->SetField(kFunctionNameOffset_, name); + Handle info_holder = WrapInJSValue(info); + this->SetField(kSharedInfoOffset_, info_holder); + this->SetSmiValueField(kStartPositionOffset_, start_position); + this->SetSmiValueField(kEndPositionOffset_, end_position); +} - friend class JSArrayBasedStruct; -}; + +Handle SharedInfoWrapper::GetInfo() { + Handle element = this->GetField(kSharedInfoOffset_); + Handle value_wrapper = Handle::cast(element); + return UnwrapSharedFunctionInfoFromJSValue(value_wrapper); +} class FunctionInfoListener { @@ -854,8 +758,7 @@ class FunctionInfoListener { Handle(shared->scope_info())); info.SetSharedFunctionInfo(shared); - Handle scope_info_list(SerializeFunctionScope(scope, zone), - isolate()); + Handle scope_info_list = SerializeFunctionScope(scope, zone); info.SetFunctionScopeInfo(scope_info_list); } @@ -864,9 +767,7 @@ class FunctionInfoListener { private: Isolate* isolate() const { return result_->GetIsolate(); } - Object* SerializeFunctionScope(Scope* scope, Zone* zone) { - HandleScope handle_scope(isolate()); - + Handle SerializeFunctionScope(Scope* scope, Zone* zone) { Handle scope_info_list = isolate()->factory()->NewJSArray(10); int scope_info_length = 0; @@ -875,6 +776,7 @@ class FunctionInfoListener { // scopes of this chain. Scope* current_scope = scope; while (current_scope != NULL) { + HandleScope handle_scope(isolate()); ZoneList stack_list(current_scope->StackLocalCount(), zone); ZoneList context_list( current_scope->ContextLocalCount(), zone); @@ -901,7 +803,7 @@ class FunctionInfoListener { current_scope = current_scope->outer_scope(); } - return *scope_info_list; + return scope_info_list; } Handle result_; @@ -910,8 +812,8 @@ class FunctionInfoListener { }; -JSArray* LiveEdit::GatherCompileInfo(Handle