Commit a650916d authored by ulan@chromium.org's avatar ulan@chromium.org

Reland r20652 "Handlify and convert string.length to new API-style accessor."

BUG=
R=dcarney@chromium.org, yangguo@chromium.org

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

git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@20767 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
parent e51d6462
...@@ -37,11 +37,33 @@ ...@@ -37,11 +37,33 @@
#include "isolate.h" #include "isolate.h"
#include "list-inl.h" #include "list-inl.h"
#include "property-details.h" #include "property-details.h"
#include "api.h"
namespace v8 { namespace v8 {
namespace internal { namespace internal {
static Handle<AccessorInfo> MakeAccessor(Isolate* isolate,
Handle<String> name,
AccessorGetterCallback getter,
AccessorSetterCallback setter,
PropertyAttributes attributes) {
Factory* factory = isolate->factory();
Handle<ExecutableAccessorInfo> info = factory->NewExecutableAccessorInfo();
info->set_property_attributes(attributes);
info->set_all_can_read(true);
info->set_all_can_write(true);
info->set_prohibits_overwriting(false);
info->set_name(*factory->length_string());
info->set_property_attributes(attributes);
Handle<Object> get = v8::FromCData(isolate, getter);
Handle<Object> set = v8::FromCData(isolate, setter);
info->set_getter(*get);
info->set_setter(*set);
return info;
}
template <class C> template <class C>
static C* FindInstanceOf(Isolate* isolate, Object* obj) { static C* FindInstanceOf(Isolate* isolate, Object* obj) {
for (Object* cur = obj; !cur->IsNull(); cur = cur->GetPrototype(isolate)) { for (Object* cur = obj; !cur->IsNull(); cur = cur->GetPrototype(isolate)) {
...@@ -233,24 +255,42 @@ const AccessorDescriptor Accessors::ArrayLength = { ...@@ -233,24 +255,42 @@ const AccessorDescriptor Accessors::ArrayLength = {
// Accessors::StringLength // Accessors::StringLength
// //
void Accessors::StringLengthGetter(
MaybeObject* Accessors::StringGetLength(Isolate* isolate, v8::Local<v8::String> name,
Object* object, const v8::PropertyCallbackInfo<v8::Value>& info) {
void*) { i::Isolate* isolate = reinterpret_cast<i::Isolate*>(info.GetIsolate());
Object* value = object; DisallowHeapAllocation no_allocation;
if (object->IsJSValue()) value = JSValue::cast(object)->value(); HandleScope scope(isolate);
if (value->IsString()) return Smi::FromInt(String::cast(value)->length()); Object* value = *Utils::OpenHandle(*info.This());
Object* result;
if (value->IsJSValue()) value = JSValue::cast(value)->value();
if (value->IsString()) {
result = Smi::FromInt(String::cast(value)->length());
} else {
// If object is not a string we return 0 to be compatible with WebKit. // If object is not a string we return 0 to be compatible with WebKit.
// Note: Firefox returns the length of ToString(object). // Note: Firefox returns the length of ToString(object).
return Smi::FromInt(0); result = Smi::FromInt(0);
}
info.GetReturnValue().Set(Utils::ToLocal(Handle<Object>(result, isolate)));
} }
const AccessorDescriptor Accessors::StringLength = { void Accessors::StringLengthSetter(
StringGetLength, v8::Local<v8::String> name,
IllegalSetter, v8::Local<v8::Value> value,
0 const v8::PropertyCallbackInfo<void>& info) {
}; UNREACHABLE();
}
Handle<AccessorInfo> Accessors::StringLengthInfo(
Isolate* isolate, PropertyAttributes attributes) {
return MakeAccessor(isolate,
isolate->factory()->length_string(),
&StringLengthGetter,
&StringLengthSetter,
attributes);
}
// //
......
...@@ -43,7 +43,6 @@ namespace internal { ...@@ -43,7 +43,6 @@ namespace internal {
V(FunctionArguments) \ V(FunctionArguments) \
V(FunctionCaller) \ V(FunctionCaller) \
V(ArrayLength) \ V(ArrayLength) \
V(StringLength) \
V(ScriptSource) \ V(ScriptSource) \
V(ScriptName) \ V(ScriptName) \
V(ScriptId) \ V(ScriptId) \
...@@ -57,6 +56,9 @@ namespace internal { ...@@ -57,6 +56,9 @@ namespace internal {
V(ScriptEvalFromScriptPosition) \ V(ScriptEvalFromScriptPosition) \
V(ScriptEvalFromFunctionName) V(ScriptEvalFromFunctionName)
#define ACCESSOR_INFO_LIST(V) \
V(StringLength) \
// Accessors contains all predefined proxy accessors. // Accessors contains all predefined proxy accessors.
class Accessors : public AllStatic { class Accessors : public AllStatic {
...@@ -67,11 +69,30 @@ class Accessors : public AllStatic { ...@@ -67,11 +69,30 @@ class Accessors : public AllStatic {
ACCESSOR_DESCRIPTOR_LIST(ACCESSOR_DESCRIPTOR_DECLARATION) ACCESSOR_DESCRIPTOR_LIST(ACCESSOR_DESCRIPTOR_DECLARATION)
#undef ACCESSOR_DESCRIPTOR_DECLARATION #undef ACCESSOR_DESCRIPTOR_DECLARATION
#define ACCESSOR_INFO_DECLARATION(name) \
static void name##Getter( \
v8::Local<v8::String> name, \
const v8::PropertyCallbackInfo<v8::Value>& info); \
static void name##Setter( \
v8::Local<v8::String> name, \
v8::Local<v8::Value> value, \
const v8::PropertyCallbackInfo<void>& info); \
static Handle<AccessorInfo> name##Info( \
Isolate* isolate, \
PropertyAttributes attributes);
ACCESSOR_INFO_LIST(ACCESSOR_INFO_DECLARATION)
#undef ACCESSOR_INFO_DECLARATION
enum DescriptorId { enum DescriptorId {
#define ACCESSOR_DESCRIPTOR_DECLARATION(name) \ #define ACCESSOR_DESCRIPTOR_DECLARATION(name) \
k##name, k##name,
ACCESSOR_DESCRIPTOR_LIST(ACCESSOR_DESCRIPTOR_DECLARATION) ACCESSOR_DESCRIPTOR_LIST(ACCESSOR_DESCRIPTOR_DECLARATION)
#undef ACCESSOR_DESCRIPTOR_DECLARATION #undef ACCESSOR_DESCRIPTOR_DECLARATION
#define ACCESSOR_INFO_DECLARATION(name) \
k##name##Getter, \
k##name##Setter,
ACCESSOR_INFO_LIST(ACCESSOR_INFO_DECLARATION)
#undef ACCESSOR_INFO_DECLARATION
descriptorCount descriptorCount
}; };
...@@ -92,7 +113,6 @@ class Accessors : public AllStatic { ...@@ -92,7 +113,6 @@ class Accessors : public AllStatic {
Handle<String> name, Handle<String> name,
int* object_offset); int* object_offset);
private: private:
// Accessor functions only used through the descriptor. // Accessor functions only used through the descriptor.
static MaybeObject* FunctionSetPrototype(Isolate* isolate, static MaybeObject* FunctionSetPrototype(Isolate* isolate,
......
...@@ -911,10 +911,10 @@ void Genesis::InitializeGlobal(Handle<GlobalObject> inner_global, ...@@ -911,10 +911,10 @@ void Genesis::InitializeGlobal(Handle<GlobalObject> inner_global,
Handle<Map>(native_context()->string_function()->initial_map()); Handle<Map>(native_context()->string_function()->initial_map());
Map::EnsureDescriptorSlack(string_map, 1); Map::EnsureDescriptorSlack(string_map, 1);
Handle<Foreign> string_length(
factory->NewForeign(&Accessors::StringLength));
PropertyAttributes attribs = static_cast<PropertyAttributes>( PropertyAttributes attribs = static_cast<PropertyAttributes>(
DONT_ENUM | DONT_DELETE | READ_ONLY); DONT_ENUM | DONT_DELETE | READ_ONLY);
Handle<AccessorInfo> string_length(
Accessors::StringLengthInfo(isolate, attribs));
{ // Add length. { // Add length.
CallbacksDescriptor d(factory->length_string(), string_length, attribs); CallbacksDescriptor d(factory->length_string(), string_length, attribs);
......
...@@ -419,10 +419,9 @@ MaybeHandle<Object> JSObject::GetPropertyWithCallback(Handle<JSObject> object, ...@@ -419,10 +419,9 @@ MaybeHandle<Object> JSObject::GetPropertyWithCallback(Handle<JSObject> object,
v8::ToCData<v8::AccessorGetterCallback>(data->getter()); v8::ToCData<v8::AccessorGetterCallback>(data->getter());
if (call_fun == NULL) return isolate->factory()->undefined_value(); if (call_fun == NULL) return isolate->factory()->undefined_value();
Handle<JSObject> self = Handle<JSObject>::cast(receiver);
Handle<String> key = Handle<String>::cast(name); Handle<String> key = Handle<String>::cast(name);
LOG(isolate, ApiNamedPropertyAccess("load", *self, *name)); LOG(isolate, ApiNamedPropertyAccess("load", *object, *name));
PropertyCallbackArguments args(isolate, data->data(), *self, *object); PropertyCallbackArguments args(isolate, data->data(), *receiver, *object);
v8::Handle<v8::Value> result = v8::Handle<v8::Value> result =
args.Call(call_fun, v8::Utils::ToLocal(key)); args.Call(call_fun, v8::Utils::ToLocal(key));
RETURN_EXCEPTION_IF_SCHEDULED_EXCEPTION(isolate, Object); RETURN_EXCEPTION_IF_SCHEDULED_EXCEPTION(isolate, Object);
...@@ -2884,8 +2883,7 @@ MaybeHandle<Object> JSObject::SetPropertyWithCallback(Handle<JSObject> object, ...@@ -2884,8 +2883,7 @@ MaybeHandle<Object> JSObject::SetPropertyWithCallback(Handle<JSObject> object,
if (call_fun == NULL) return value; if (call_fun == NULL) return value;
Handle<String> key = Handle<String>::cast(name); Handle<String> key = Handle<String>::cast(name);
LOG(isolate, ApiNamedPropertyAccess("store", *object, *name)); LOG(isolate, ApiNamedPropertyAccess("store", *object, *name));
PropertyCallbackArguments args( PropertyCallbackArguments args(isolate, data->data(), *object, *holder);
isolate, data->data(), *object, JSObject::cast(*holder));
args.Call(call_fun, args.Call(call_fun,
v8::Utils::ToLocal(key), v8::Utils::ToLocal(key),
v8::Utils::ToLocal(value)); v8::Utils::ToLocal(value));
...@@ -12137,13 +12135,12 @@ MaybeHandle<Object> JSObject::GetElementWithCallback( ...@@ -12137,13 +12135,12 @@ MaybeHandle<Object> JSObject::GetElementWithCallback(
v8::AccessorGetterCallback call_fun = v8::AccessorGetterCallback call_fun =
v8::ToCData<v8::AccessorGetterCallback>(fun_obj); v8::ToCData<v8::AccessorGetterCallback>(fun_obj);
if (call_fun == NULL) return isolate->factory()->undefined_value(); if (call_fun == NULL) return isolate->factory()->undefined_value();
Handle<JSObject> self = Handle<JSObject>::cast(receiver);
Handle<JSObject> holder_handle = Handle<JSObject>::cast(holder); Handle<JSObject> holder_handle = Handle<JSObject>::cast(holder);
Handle<Object> number = isolate->factory()->NewNumberFromUint(index); Handle<Object> number = isolate->factory()->NewNumberFromUint(index);
Handle<String> key = isolate->factory()->NumberToString(number); Handle<String> key = isolate->factory()->NumberToString(number);
LOG(isolate, ApiNamedPropertyAccess("load", *self, *key)); LOG(isolate, ApiNamedPropertyAccess("load", *holder_handle, *key));
PropertyCallbackArguments PropertyCallbackArguments
args(isolate, data->data(), *self, *holder_handle); args(isolate, data->data(), *receiver, *holder_handle);
v8::Handle<v8::Value> result = args.Call(call_fun, v8::Utils::ToLocal(key)); v8::Handle<v8::Value> result = args.Call(call_fun, v8::Utils::ToLocal(key));
RETURN_EXCEPTION_IF_SCHEDULED_EXCEPTION(isolate, Object); RETURN_EXCEPTION_IF_SCHEDULED_EXCEPTION(isolate, Object);
if (result.IsEmpty()) return isolate->factory()->undefined_value(); if (result.IsEmpty()) return isolate->factory()->undefined_value();
......
...@@ -276,10 +276,21 @@ void ExternalReferenceTable::PopulateTable(Isolate* isolate) { ...@@ -276,10 +276,21 @@ void ExternalReferenceTable::PopulateTable(Isolate* isolate) {
ACCESSOR, \ ACCESSOR, \
Accessors::k##name, \ Accessors::k##name, \
"Accessors::" #name); "Accessors::" #name);
ACCESSOR_DESCRIPTOR_LIST(ACCESSOR_DESCRIPTOR_DECLARATION) ACCESSOR_DESCRIPTOR_LIST(ACCESSOR_DESCRIPTOR_DECLARATION)
#undef ACCESSOR_DESCRIPTOR_DECLARATION #undef ACCESSOR_DESCRIPTOR_DECLARATION
#define ACCESSOR_INFO_DECLARATION(name) \
Add(FUNCTION_ADDR(&Accessors::name##Getter), \
ACCESSOR, \
Accessors::k##name##Getter, \
"Accessors::" #name "Getter"); \
Add(FUNCTION_ADDR(&Accessors::name##Setter), \
ACCESSOR, \
Accessors::k##name##Setter, \
"Accessors::" #name "Setter");
ACCESSOR_INFO_LIST(ACCESSOR_INFO_DECLARATION)
#undef ACCESSOR_INFO_DECLARATION
StubCache* stub_cache = isolate->stub_cache(); StubCache* stub_cache = isolate->stub_cache();
// Stub cache tables // Stub cache tables
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment