2009-03-16 09:49:14 +00:00
|
|
|
// Copyright 2006-2009 the V8 project authors. All rights reserved.
|
2008-07-03 15:10:15 +00:00
|
|
|
// Redistribution and use in source and binary forms, with or without
|
|
|
|
// modification, are permitted provided that the following conditions are
|
|
|
|
// met:
|
|
|
|
//
|
|
|
|
// * Redistributions of source code must retain the above copyright
|
|
|
|
// notice, this list of conditions and the following disclaimer.
|
|
|
|
// * Redistributions in binary form must reproduce the above
|
|
|
|
// copyright notice, this list of conditions and the following
|
|
|
|
// disclaimer in the documentation and/or other materials provided
|
|
|
|
// with the distribution.
|
|
|
|
// * Neither the name of Google Inc. nor the names of its
|
|
|
|
// contributors may be used to endorse or promote products derived
|
|
|
|
// from this software without specific prior written permission.
|
|
|
|
//
|
|
|
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
#include "v8.h"
|
|
|
|
|
|
|
|
#include "accessors.h"
|
|
|
|
#include "api.h"
|
|
|
|
#include "arguments.h"
|
|
|
|
#include "compiler.h"
|
|
|
|
#include "cpu.h"
|
|
|
|
#include "dateparser.h"
|
2009-03-17 13:27:21 +00:00
|
|
|
#include "dateparser-inl.h"
|
2008-07-03 15:10:15 +00:00
|
|
|
#include "debug.h"
|
|
|
|
#include "execution.h"
|
|
|
|
#include "jsregexp.h"
|
|
|
|
#include "platform.h"
|
|
|
|
#include "runtime.h"
|
|
|
|
#include "scopeinfo.h"
|
|
|
|
#include "v8threads.h"
|
2008-10-07 08:11:44 +00:00
|
|
|
#include "smart-pointer.h"
|
2009-03-23 07:27:47 +00:00
|
|
|
#include "parser.h"
|
2009-08-26 12:22:44 +00:00
|
|
|
#include "stub-cache.h"
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2009-05-25 10:05:56 +00:00
|
|
|
namespace v8 {
|
|
|
|
namespace internal {
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
|
2009-06-26 13:09:50 +00:00
|
|
|
#define RUNTIME_ASSERT(value) \
|
|
|
|
if (!(value)) return Top::ThrowIllegalOperation();
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
// Cast the given object to a value of the specified type and store
|
|
|
|
// it in a variable with the given name. If the object is not of the
|
|
|
|
// expected type call IllegalOperation and return.
|
|
|
|
#define CONVERT_CHECKED(Type, name, obj) \
|
|
|
|
RUNTIME_ASSERT(obj->Is##Type()); \
|
|
|
|
Type* name = Type::cast(obj);
|
|
|
|
|
|
|
|
#define CONVERT_ARG_CHECKED(Type, name, index) \
|
|
|
|
RUNTIME_ASSERT(args[index]->Is##Type()); \
|
|
|
|
Handle<Type> name = args.at<Type>(index);
|
|
|
|
|
2008-07-09 11:06:54 +00:00
|
|
|
// Cast the given object to a boolean and store it in a variable with
|
|
|
|
// the given name. If the object is not a boolean call IllegalOperation
|
|
|
|
// and return.
|
|
|
|
#define CONVERT_BOOLEAN_CHECKED(name, obj) \
|
|
|
|
RUNTIME_ASSERT(obj->IsBoolean()); \
|
|
|
|
bool name = (obj)->IsTrue();
|
|
|
|
|
2009-03-23 07:27:47 +00:00
|
|
|
// Cast the given object to a Smi and store its value in an int variable
|
|
|
|
// with the given name. If the object is not a Smi call IllegalOperation
|
|
|
|
// and return.
|
|
|
|
#define CONVERT_SMI_CHECKED(name, obj) \
|
|
|
|
RUNTIME_ASSERT(obj->IsSmi()); \
|
|
|
|
int name = Smi::cast(obj)->value();
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
// Cast the given object to a double and store it in a variable with
|
|
|
|
// the given name. If the object is not a number (as opposed to
|
|
|
|
// the number not-a-number) call IllegalOperation and return.
|
|
|
|
#define CONVERT_DOUBLE_CHECKED(name, obj) \
|
|
|
|
RUNTIME_ASSERT(obj->IsNumber()); \
|
|
|
|
double name = (obj)->Number();
|
|
|
|
|
|
|
|
// Call the specified converter on the object *comand store the result in
|
|
|
|
// a variable of the specified type with the given name. If the
|
|
|
|
// object is not a Number call IllegalOperation and return.
|
|
|
|
#define CONVERT_NUMBER_CHECKED(type, name, Type, obj) \
|
|
|
|
RUNTIME_ASSERT(obj->IsNumber()); \
|
|
|
|
type name = NumberTo##Type(obj);
|
|
|
|
|
|
|
|
// Non-reentrant string buffer for efficient general use in this file.
|
2009-03-02 09:09:07 +00:00
|
|
|
static StaticResource<StringInputBuffer> runtime_string_input_buffer;
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
|
2009-03-23 07:27:47 +00:00
|
|
|
static Object* DeepCopyBoilerplate(JSObject* boilerplate) {
|
|
|
|
StackLimitCheck check;
|
|
|
|
if (check.HasOverflowed()) return Top::StackOverflow();
|
|
|
|
|
|
|
|
Object* result = Heap::CopyJSObject(boilerplate);
|
|
|
|
if (result->IsFailure()) return result;
|
|
|
|
JSObject* copy = JSObject::cast(result);
|
|
|
|
|
|
|
|
// Deep copy local properties.
|
|
|
|
if (copy->HasFastProperties()) {
|
|
|
|
FixedArray* properties = copy->properties();
|
|
|
|
WriteBarrierMode mode = properties->GetWriteBarrierMode();
|
|
|
|
for (int i = 0; i < properties->length(); i++) {
|
|
|
|
Object* value = properties->get(i);
|
|
|
|
if (value->IsJSObject()) {
|
|
|
|
JSObject* jsObject = JSObject::cast(value);
|
|
|
|
result = DeepCopyBoilerplate(jsObject);
|
|
|
|
if (result->IsFailure()) return result;
|
|
|
|
properties->set(i, result, mode);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mode = copy->GetWriteBarrierMode();
|
2009-06-22 08:09:57 +00:00
|
|
|
int nof = copy->map()->inobject_properties();
|
|
|
|
for (int i = 0; i < nof; i++) {
|
2009-03-23 07:27:47 +00:00
|
|
|
Object* value = copy->InObjectPropertyAt(i);
|
|
|
|
if (value->IsJSObject()) {
|
|
|
|
JSObject* jsObject = JSObject::cast(value);
|
|
|
|
result = DeepCopyBoilerplate(jsObject);
|
|
|
|
if (result->IsFailure()) return result;
|
|
|
|
copy->InObjectPropertyAtPut(i, result, mode);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
result = Heap::AllocateFixedArray(copy->NumberOfLocalProperties(NONE));
|
|
|
|
if (result->IsFailure()) return result;
|
|
|
|
FixedArray* names = FixedArray::cast(result);
|
|
|
|
copy->GetLocalPropertyNames(names, 0);
|
|
|
|
for (int i = 0; i < names->length(); i++) {
|
|
|
|
ASSERT(names->get(i)->IsString());
|
|
|
|
String* keyString = String::cast(names->get(i));
|
|
|
|
PropertyAttributes attributes =
|
|
|
|
copy->GetLocalPropertyAttribute(keyString);
|
|
|
|
// Only deep copy fields from the object literal expression.
|
|
|
|
// In particular, don't try to copy the length attribute of
|
|
|
|
// an array.
|
|
|
|
if (attributes != NONE) continue;
|
|
|
|
Object* value = copy->GetProperty(keyString, &attributes);
|
|
|
|
ASSERT(!value->IsFailure());
|
|
|
|
if (value->IsJSObject()) {
|
|
|
|
JSObject* jsObject = JSObject::cast(value);
|
|
|
|
result = DeepCopyBoilerplate(jsObject);
|
|
|
|
if (result->IsFailure()) return result;
|
|
|
|
result = copy->SetProperty(keyString, result, NONE);
|
|
|
|
if (result->IsFailure()) return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Deep copy local elements.
|
2009-07-28 08:43:51 +00:00
|
|
|
// Pixel elements cannot be created using an object literal.
|
|
|
|
ASSERT(!copy->HasPixelElements());
|
|
|
|
switch (copy->GetElementsKind()) {
|
|
|
|
case JSObject::FAST_ELEMENTS: {
|
|
|
|
FixedArray* elements = FixedArray::cast(copy->elements());
|
|
|
|
WriteBarrierMode mode = elements->GetWriteBarrierMode();
|
|
|
|
for (int i = 0; i < elements->length(); i++) {
|
|
|
|
Object* value = elements->get(i);
|
2009-03-23 07:27:47 +00:00
|
|
|
if (value->IsJSObject()) {
|
|
|
|
JSObject* jsObject = JSObject::cast(value);
|
|
|
|
result = DeepCopyBoilerplate(jsObject);
|
|
|
|
if (result->IsFailure()) return result;
|
2009-07-28 08:43:51 +00:00
|
|
|
elements->set(i, result, mode);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case JSObject::DICTIONARY_ELEMENTS: {
|
|
|
|
NumberDictionary* element_dictionary = copy->element_dictionary();
|
|
|
|
int capacity = element_dictionary->Capacity();
|
|
|
|
for (int i = 0; i < capacity; i++) {
|
|
|
|
Object* k = element_dictionary->KeyAt(i);
|
|
|
|
if (element_dictionary->IsKey(k)) {
|
|
|
|
Object* value = element_dictionary->ValueAt(i);
|
|
|
|
if (value->IsJSObject()) {
|
|
|
|
JSObject* jsObject = JSObject::cast(value);
|
|
|
|
result = DeepCopyBoilerplate(jsObject);
|
|
|
|
if (result->IsFailure()) return result;
|
|
|
|
element_dictionary->ValueAtPut(i, result);
|
|
|
|
}
|
2009-03-23 07:27:47 +00:00
|
|
|
}
|
|
|
|
}
|
2009-07-28 08:43:51 +00:00
|
|
|
break;
|
2009-03-23 07:27:47 +00:00
|
|
|
}
|
2009-07-28 08:43:51 +00:00
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
break;
|
2009-03-23 07:27:47 +00:00
|
|
|
}
|
|
|
|
return copy;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static Object* Runtime_CloneLiteralBoilerplate(Arguments args) {
|
|
|
|
CONVERT_CHECKED(JSObject, boilerplate, args[0]);
|
|
|
|
return DeepCopyBoilerplate(boilerplate);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static Object* Runtime_CloneShallowLiteralBoilerplate(Arguments args) {
|
2008-07-03 15:10:15 +00:00
|
|
|
CONVERT_CHECKED(JSObject, boilerplate, args[0]);
|
2008-10-22 08:21:18 +00:00
|
|
|
return Heap::CopyJSObject(boilerplate);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-09-25 07:46:07 +00:00
|
|
|
static Handle<Map> ComputeObjectLiteralMap(
|
|
|
|
Handle<Context> context,
|
|
|
|
Handle<FixedArray> constant_properties,
|
2008-10-23 07:28:21 +00:00
|
|
|
bool* is_result_from_cache) {
|
2009-01-12 10:59:58 +00:00
|
|
|
int number_of_properties = constant_properties->length() / 2;
|
2008-09-25 07:46:07 +00:00
|
|
|
if (FLAG_canonicalize_object_literal_maps) {
|
|
|
|
// First find prefix of consecutive symbol keys.
|
|
|
|
int number_of_symbol_keys = 0;
|
|
|
|
while ((number_of_symbol_keys < number_of_properties) &&
|
|
|
|
(constant_properties->get(number_of_symbol_keys*2)->IsSymbol())) {
|
|
|
|
number_of_symbol_keys++;
|
|
|
|
}
|
|
|
|
// Based on the number of prefix symbols key we decide whether
|
|
|
|
// to use the map cache in the global context.
|
|
|
|
const int kMaxKeys = 10;
|
2008-10-23 07:04:56 +00:00
|
|
|
if ((number_of_symbol_keys == number_of_properties) &&
|
|
|
|
(number_of_symbol_keys < kMaxKeys)) {
|
2008-09-25 07:46:07 +00:00
|
|
|
// Create the fixed array with the key.
|
|
|
|
Handle<FixedArray> keys = Factory::NewFixedArray(number_of_symbol_keys);
|
|
|
|
for (int i = 0; i < number_of_symbol_keys; i++) {
|
|
|
|
keys->set(i, constant_properties->get(i*2));
|
|
|
|
}
|
2008-10-23 07:28:21 +00:00
|
|
|
*is_result_from_cache = true;
|
2008-09-25 07:46:07 +00:00
|
|
|
return Factory::ObjectLiteralMapFromCache(context, keys);
|
|
|
|
}
|
|
|
|
}
|
2008-10-23 07:28:21 +00:00
|
|
|
*is_result_from_cache = false;
|
2009-01-12 10:59:58 +00:00
|
|
|
return Factory::CopyMap(
|
|
|
|
Handle<Map>(context->object_function()->initial_map()),
|
|
|
|
number_of_properties);
|
2008-09-25 07:46:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-23 07:27:47 +00:00
|
|
|
static Handle<Object> CreateLiteralBoilerplate(
|
|
|
|
Handle<FixedArray> literals,
|
|
|
|
Handle<FixedArray> constant_properties);
|
2009-03-10 08:59:00 +00:00
|
|
|
|
2009-03-23 07:27:47 +00:00
|
|
|
|
|
|
|
static Handle<Object> CreateObjectLiteralBoilerplate(
|
|
|
|
Handle<FixedArray> literals,
|
|
|
|
Handle<FixedArray> constant_properties) {
|
2008-10-03 07:14:31 +00:00
|
|
|
// Get the global context from the literals array. This is the
|
|
|
|
// context in which the function was created and we use the object
|
|
|
|
// function from this context to create the object literal. We do
|
|
|
|
// not use the object function from the current global context
|
|
|
|
// because this might be the object function from another context
|
|
|
|
// which we should not have access to.
|
2008-09-25 07:46:07 +00:00
|
|
|
Handle<Context> context =
|
|
|
|
Handle<Context>(JSFunction::GlobalContextFromLiterals(*literals));
|
|
|
|
|
|
|
|
bool is_result_from_cache;
|
|
|
|
Handle<Map> map = ComputeObjectLiteralMap(context,
|
|
|
|
constant_properties,
|
2008-10-23 07:28:21 +00:00
|
|
|
&is_result_from_cache);
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2008-09-25 07:46:07 +00:00
|
|
|
Handle<JSObject> boilerplate = Factory::NewJSObjectFromMap(map);
|
2009-01-15 19:08:34 +00:00
|
|
|
{ // Add the constant properties to the boilerplate.
|
2008-07-03 15:10:15 +00:00
|
|
|
int length = constant_properties->length();
|
2008-09-25 07:46:07 +00:00
|
|
|
OptimizedObjectForAddingMultipleProperties opt(boilerplate,
|
2009-07-30 09:13:48 +00:00
|
|
|
length / 2,
|
2008-09-25 07:46:07 +00:00
|
|
|
!is_result_from_cache);
|
2008-07-03 15:10:15 +00:00
|
|
|
for (int index = 0; index < length; index +=2) {
|
|
|
|
Handle<Object> key(constant_properties->get(index+0));
|
|
|
|
Handle<Object> value(constant_properties->get(index+1));
|
2009-03-23 07:27:47 +00:00
|
|
|
if (value->IsFixedArray()) {
|
|
|
|
// The value contains the constant_properties of a
|
|
|
|
// simple object literal.
|
|
|
|
Handle<FixedArray> array = Handle<FixedArray>::cast(value);
|
|
|
|
value = CreateLiteralBoilerplate(literals, array);
|
|
|
|
if (value.is_null()) return value;
|
|
|
|
}
|
2009-01-26 13:10:26 +00:00
|
|
|
Handle<Object> result;
|
2008-07-03 15:10:15 +00:00
|
|
|
uint32_t element_index = 0;
|
|
|
|
if (key->IsSymbol()) {
|
|
|
|
// If key is a symbol it is not an array element.
|
|
|
|
Handle<String> name(String::cast(*key));
|
|
|
|
ASSERT(!name->AsArrayIndex(&element_index));
|
2009-01-26 13:10:26 +00:00
|
|
|
result = SetProperty(boilerplate, name, value, NONE);
|
2008-07-03 15:10:15 +00:00
|
|
|
} else if (Array::IndexFromObject(*key, &element_index)) {
|
|
|
|
// Array index (uint32).
|
2009-01-26 13:10:26 +00:00
|
|
|
result = SetElement(boilerplate, element_index, value);
|
2008-07-03 15:10:15 +00:00
|
|
|
} else {
|
|
|
|
// Non-uint32 number.
|
|
|
|
ASSERT(key->IsNumber());
|
|
|
|
double num = key->Number();
|
|
|
|
char arr[100];
|
|
|
|
Vector<char> buffer(arr, ARRAY_SIZE(arr));
|
|
|
|
const char* str = DoubleToCString(num, buffer);
|
|
|
|
Handle<String> name = Factory::NewStringFromAscii(CStrVector(str));
|
2009-01-26 13:10:26 +00:00
|
|
|
result = SetProperty(boilerplate, name, value, NONE);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
2009-01-26 13:10:26 +00:00
|
|
|
// If setting the property on the boilerplate throws an
|
|
|
|
// exception, the exception is converted to an empty handle in
|
|
|
|
// the handle based operations. In that case, we need to
|
|
|
|
// convert back to an exception.
|
2009-03-23 07:27:47 +00:00
|
|
|
if (result.is_null()) return result;
|
2009-03-10 08:59:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-03-23 07:27:47 +00:00
|
|
|
return boilerplate;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static Handle<Object> CreateArrayLiteralBoilerplate(
|
|
|
|
Handle<FixedArray> literals,
|
|
|
|
Handle<FixedArray> elements) {
|
|
|
|
// Create the JSArray.
|
|
|
|
Handle<JSFunction> constructor(
|
|
|
|
JSFunction::GlobalContextFromLiterals(*literals)->array_function());
|
|
|
|
Handle<Object> object = Factory::NewJSObject(constructor);
|
|
|
|
|
|
|
|
Handle<Object> copied_elements = Factory::CopyFixedArray(elements);
|
|
|
|
|
|
|
|
Handle<FixedArray> content = Handle<FixedArray>::cast(copied_elements);
|
|
|
|
for (int i = 0; i < content->length(); i++) {
|
|
|
|
if (content->get(i)->IsFixedArray()) {
|
|
|
|
// The value contains the constant_properties of a
|
|
|
|
// simple object literal.
|
|
|
|
Handle<FixedArray> fa(FixedArray::cast(content->get(i)));
|
|
|
|
Handle<Object> result =
|
|
|
|
CreateLiteralBoilerplate(literals, fa);
|
|
|
|
if (result.is_null()) return result;
|
|
|
|
content->set(i, *result);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the elements.
|
|
|
|
Handle<JSArray>::cast(object)->SetContent(*content);
|
|
|
|
return object;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static Handle<Object> CreateLiteralBoilerplate(
|
|
|
|
Handle<FixedArray> literals,
|
|
|
|
Handle<FixedArray> array) {
|
|
|
|
Handle<FixedArray> elements = CompileTimeValue::GetElements(array);
|
|
|
|
switch (CompileTimeValue::GetType(array)) {
|
|
|
|
case CompileTimeValue::OBJECT_LITERAL:
|
|
|
|
return CreateObjectLiteralBoilerplate(literals, elements);
|
|
|
|
case CompileTimeValue::ARRAY_LITERAL:
|
|
|
|
return CreateArrayLiteralBoilerplate(literals, elements);
|
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
return Handle<Object>::null();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static Object* Runtime_CreateObjectLiteralBoilerplate(Arguments args) {
|
|
|
|
HandleScope scope;
|
|
|
|
ASSERT(args.length() == 3);
|
|
|
|
// Copy the arguments.
|
|
|
|
CONVERT_ARG_CHECKED(FixedArray, literals, 0);
|
|
|
|
CONVERT_SMI_CHECKED(literals_index, args[1]);
|
|
|
|
CONVERT_ARG_CHECKED(FixedArray, constant_properties, 2);
|
|
|
|
|
|
|
|
Handle<Object> result =
|
|
|
|
CreateObjectLiteralBoilerplate(literals, constant_properties);
|
|
|
|
|
|
|
|
if (result.is_null()) return Failure::Exception();
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
// Update the functions literal and return the boilerplate.
|
2009-03-23 07:27:47 +00:00
|
|
|
literals->set(literals_index, *result);
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2009-03-23 07:27:47 +00:00
|
|
|
return *result;
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-23 07:27:47 +00:00
|
|
|
static Object* Runtime_CreateArrayLiteralBoilerplate(Arguments args) {
|
2008-08-22 13:33:59 +00:00
|
|
|
// Takes a FixedArray of elements containing the literal elements of
|
|
|
|
// the array literal and produces JSArray with those elements.
|
|
|
|
// Additionally takes the literals array of the surrounding function
|
2008-10-03 07:14:31 +00:00
|
|
|
// which contains the context from which to get the Array function
|
|
|
|
// to use for creating the array literal.
|
2009-03-23 07:27:47 +00:00
|
|
|
HandleScope scope;
|
|
|
|
ASSERT(args.length() == 3);
|
|
|
|
CONVERT_ARG_CHECKED(FixedArray, literals, 0);
|
|
|
|
CONVERT_SMI_CHECKED(literals_index, args[1]);
|
|
|
|
CONVERT_ARG_CHECKED(FixedArray, elements, 2);
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2009-03-23 07:27:47 +00:00
|
|
|
Handle<Object> object = CreateArrayLiteralBoilerplate(literals, elements);
|
|
|
|
if (object.is_null()) return Failure::Exception();
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2009-03-23 07:27:47 +00:00
|
|
|
// Update the functions literal and return the boilerplate.
|
|
|
|
literals->set(literals_index, *object);
|
|
|
|
return *object;
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-01-16 09:42:08 +00:00
|
|
|
static Object* Runtime_CreateCatchExtensionObject(Arguments args) {
|
|
|
|
ASSERT(args.length() == 2);
|
|
|
|
CONVERT_CHECKED(String, key, args[0]);
|
|
|
|
Object* value = args[1];
|
|
|
|
// Create a catch context extension object.
|
|
|
|
JSFunction* constructor =
|
|
|
|
Top::context()->global_context()->context_extension_function();
|
|
|
|
Object* object = Heap::AllocateJSObject(constructor);
|
|
|
|
if (object->IsFailure()) return object;
|
|
|
|
// Assign the exception value to the catch variable and make sure
|
|
|
|
// that the catch variable is DontDelete.
|
|
|
|
value = JSObject::cast(object)->SetProperty(key, value, DONT_DELETE);
|
|
|
|
if (value->IsFailure()) return value;
|
|
|
|
return object;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
static Object* Runtime_ClassOf(Arguments args) {
|
|
|
|
NoHandleAllocation ha;
|
|
|
|
ASSERT(args.length() == 1);
|
|
|
|
Object* obj = args[0];
|
|
|
|
if (!obj->IsJSObject()) return Heap::null_value();
|
|
|
|
return JSObject::cast(obj)->class_name();
|
|
|
|
}
|
|
|
|
|
2008-10-07 10:54:50 +00:00
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
static Object* Runtime_IsInPrototypeChain(Arguments args) {
|
|
|
|
NoHandleAllocation ha;
|
|
|
|
ASSERT(args.length() == 2);
|
|
|
|
// See ECMA-262, section 15.3.5.3, page 88 (steps 5 - 8).
|
|
|
|
Object* O = args[0];
|
|
|
|
Object* V = args[1];
|
|
|
|
while (true) {
|
|
|
|
Object* prototype = V->GetPrototype();
|
|
|
|
if (prototype->IsNull()) return Heap::false_value();
|
|
|
|
if (O == prototype) return Heap::true_value();
|
|
|
|
V = prototype;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-05-06 12:54:57 +00:00
|
|
|
// Inserts an object as the hidden prototype of another object.
|
|
|
|
static Object* Runtime_SetHiddenPrototype(Arguments args) {
|
|
|
|
NoHandleAllocation ha;
|
|
|
|
ASSERT(args.length() == 2);
|
|
|
|
CONVERT_CHECKED(JSObject, jsobject, args[0]);
|
|
|
|
CONVERT_CHECKED(JSObject, proto, args[1]);
|
|
|
|
|
|
|
|
// Sanity checks. The old prototype (that we are replacing) could
|
|
|
|
// theoretically be null, but if it is not null then check that we
|
|
|
|
// didn't already install a hidden prototype here.
|
|
|
|
RUNTIME_ASSERT(!jsobject->GetPrototype()->IsHeapObject() ||
|
|
|
|
!HeapObject::cast(jsobject->GetPrototype())->map()->is_hidden_prototype());
|
|
|
|
RUNTIME_ASSERT(!proto->map()->is_hidden_prototype());
|
|
|
|
|
|
|
|
// Allocate up front before we start altering state in case we get a GC.
|
|
|
|
Object* map_or_failure = proto->map()->CopyDropTransitions();
|
|
|
|
if (map_or_failure->IsFailure()) return map_or_failure;
|
|
|
|
Map* new_proto_map = Map::cast(map_or_failure);
|
|
|
|
|
|
|
|
map_or_failure = jsobject->map()->CopyDropTransitions();
|
|
|
|
if (map_or_failure->IsFailure()) return map_or_failure;
|
|
|
|
Map* new_map = Map::cast(map_or_failure);
|
|
|
|
|
|
|
|
// Set proto's prototype to be the old prototype of the object.
|
|
|
|
new_proto_map->set_prototype(jsobject->GetPrototype());
|
|
|
|
proto->set_map(new_proto_map);
|
|
|
|
new_proto_map->set_is_hidden_prototype();
|
|
|
|
|
|
|
|
// Set the object's prototype to proto.
|
|
|
|
new_map->set_prototype(proto);
|
|
|
|
jsobject->set_map(new_map);
|
|
|
|
|
|
|
|
return Heap::undefined_value();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
static Object* Runtime_IsConstructCall(Arguments args) {
|
|
|
|
NoHandleAllocation ha;
|
2008-08-13 09:32:07 +00:00
|
|
|
ASSERT(args.length() == 0);
|
2008-07-03 15:10:15 +00:00
|
|
|
JavaScriptFrameIterator it;
|
|
|
|
return Heap::ToBoolean(it.frame()->IsConstructor());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static Object* Runtime_RegExpCompile(Arguments args) {
|
2008-11-25 11:07:48 +00:00
|
|
|
HandleScope scope;
|
2008-07-03 15:10:15 +00:00
|
|
|
ASSERT(args.length() == 3);
|
2009-06-17 11:44:25 +00:00
|
|
|
CONVERT_ARG_CHECKED(JSRegExp, re, 0);
|
|
|
|
CONVERT_ARG_CHECKED(String, pattern, 1);
|
|
|
|
CONVERT_ARG_CHECKED(String, flags, 2);
|
2008-11-04 16:23:56 +00:00
|
|
|
Handle<Object> result = RegExpImpl::Compile(re, pattern, flags);
|
|
|
|
if (result.is_null()) return Failure::Exception();
|
|
|
|
return *result;
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static Object* Runtime_CreateApiFunction(Arguments args) {
|
|
|
|
HandleScope scope;
|
|
|
|
ASSERT(args.length() == 1);
|
2009-06-17 11:44:25 +00:00
|
|
|
CONVERT_ARG_CHECKED(FunctionTemplateInfo, data, 0);
|
2008-07-03 15:10:15 +00:00
|
|
|
return *Factory::CreateApiFunction(data);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static Object* Runtime_IsTemplate(Arguments args) {
|
|
|
|
ASSERT(args.length() == 1);
|
|
|
|
Object* arg = args[0];
|
2008-08-27 10:11:39 +00:00
|
|
|
bool result = arg->IsObjectTemplateInfo() || arg->IsFunctionTemplateInfo();
|
2008-07-03 15:10:15 +00:00
|
|
|
return Heap::ToBoolean(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static Object* Runtime_GetTemplateField(Arguments args) {
|
|
|
|
ASSERT(args.length() == 2);
|
|
|
|
CONVERT_CHECKED(HeapObject, templ, args[0]);
|
|
|
|
CONVERT_CHECKED(Smi, field, args[1]);
|
2008-10-29 10:37:14 +00:00
|
|
|
int index = field->value();
|
|
|
|
int offset = index * kPointerSize + HeapObject::kHeaderSize;
|
|
|
|
InstanceType type = templ->map()->instance_type();
|
|
|
|
RUNTIME_ASSERT(type == FUNCTION_TEMPLATE_INFO_TYPE ||
|
|
|
|
type == OBJECT_TEMPLATE_INFO_TYPE);
|
|
|
|
RUNTIME_ASSERT(offset > 0);
|
|
|
|
if (type == FUNCTION_TEMPLATE_INFO_TYPE) {
|
|
|
|
RUNTIME_ASSERT(offset < FunctionTemplateInfo::kSize);
|
|
|
|
} else {
|
|
|
|
RUNTIME_ASSERT(offset < ObjectTemplateInfo::kSize);
|
|
|
|
}
|
|
|
|
return *HeapObject::RawField(templ, offset);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-10-30 12:51:06 +00:00
|
|
|
static Object* Runtime_DisableAccessChecks(Arguments args) {
|
|
|
|
ASSERT(args.length() == 1);
|
|
|
|
CONVERT_CHECKED(HeapObject, object, args[0]);
|
2009-01-15 17:39:23 +00:00
|
|
|
Map* old_map = object->map();
|
|
|
|
bool needs_access_checks = old_map->is_access_check_needed();
|
|
|
|
if (needs_access_checks) {
|
|
|
|
// Copy map so it won't interfere constructor's initial map.
|
|
|
|
Object* new_map = old_map->CopyDropTransitions();
|
|
|
|
if (new_map->IsFailure()) return new_map;
|
|
|
|
|
|
|
|
Map::cast(new_map)->set_is_access_check_needed(false);
|
|
|
|
object->set_map(Map::cast(new_map));
|
|
|
|
}
|
2008-10-30 12:51:06 +00:00
|
|
|
return needs_access_checks ? Heap::true_value() : Heap::false_value();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static Object* Runtime_EnableAccessChecks(Arguments args) {
|
|
|
|
ASSERT(args.length() == 1);
|
|
|
|
CONVERT_CHECKED(HeapObject, object, args[0]);
|
2009-01-15 17:39:23 +00:00
|
|
|
Map* old_map = object->map();
|
|
|
|
if (!old_map->is_access_check_needed()) {
|
|
|
|
// Copy map so it won't interfere constructor's initial map.
|
|
|
|
Object* new_map = old_map->CopyDropTransitions();
|
|
|
|
if (new_map->IsFailure()) return new_map;
|
|
|
|
|
|
|
|
Map::cast(new_map)->set_is_access_check_needed(true);
|
|
|
|
object->set_map(Map::cast(new_map));
|
|
|
|
}
|
2008-10-30 12:51:06 +00:00
|
|
|
return Heap::undefined_value();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
static Object* ThrowRedeclarationError(const char* type, Handle<String> name) {
|
|
|
|
HandleScope scope;
|
|
|
|
Handle<Object> type_handle = Factory::NewStringFromAscii(CStrVector(type));
|
|
|
|
Handle<Object> args[2] = { type_handle, name };
|
|
|
|
Handle<Object> error =
|
|
|
|
Factory::NewTypeError("redeclaration", HandleVector(args, 2));
|
|
|
|
return Top::Throw(*error);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static Object* Runtime_DeclareGlobals(Arguments args) {
|
|
|
|
HandleScope scope;
|
|
|
|
Handle<GlobalObject> global = Handle<GlobalObject>(Top::context()->global());
|
|
|
|
|
|
|
|
CONVERT_ARG_CHECKED(FixedArray, pairs, 0);
|
|
|
|
Handle<Context> context = args.at<Context>(1);
|
|
|
|
bool is_eval = Smi::cast(args[2])->value() == 1;
|
|
|
|
|
|
|
|
// Compute the property attributes. According to ECMA-262, section
|
|
|
|
// 13, page 71, the property must be read-only and
|
|
|
|
// non-deletable. However, neither SpiderMonkey nor KJS creates the
|
|
|
|
// property as read-only, so we don't either.
|
|
|
|
PropertyAttributes base = is_eval ? NONE : DONT_DELETE;
|
|
|
|
|
|
|
|
// Traverse the name/value pairs and set the properties.
|
|
|
|
int length = pairs->length();
|
|
|
|
for (int i = 0; i < length; i += 2) {
|
|
|
|
HandleScope scope;
|
|
|
|
Handle<String> name(String::cast(pairs->get(i)));
|
|
|
|
Handle<Object> value(pairs->get(i + 1));
|
|
|
|
|
|
|
|
// We have to declare a global const property. To capture we only
|
|
|
|
// assign to it when evaluating the assignment for "const x =
|
|
|
|
// <expr>" the initial value is the hole.
|
|
|
|
bool is_const_property = value->IsTheHole();
|
|
|
|
|
|
|
|
if (value->IsUndefined() || is_const_property) {
|
|
|
|
// Lookup the property in the global object, and don't set the
|
|
|
|
// value of the variable if the property is already there.
|
|
|
|
LookupResult lookup;
|
|
|
|
global->Lookup(*name, &lookup);
|
|
|
|
if (lookup.IsProperty()) {
|
|
|
|
// Determine if the property is local by comparing the holder
|
|
|
|
// against the global object. The information will be used to
|
|
|
|
// avoid throwing re-declaration errors when declaring
|
|
|
|
// variables or constants that exist in the prototype chain.
|
|
|
|
bool is_local = (*global == lookup.holder());
|
|
|
|
// Get the property attributes and determine if the property is
|
|
|
|
// read-only.
|
|
|
|
PropertyAttributes attributes = global->GetPropertyAttribute(*name);
|
|
|
|
bool is_read_only = (attributes & READ_ONLY) != 0;
|
|
|
|
if (lookup.type() == INTERCEPTOR) {
|
|
|
|
// If the interceptor says the property is there, we
|
|
|
|
// just return undefined without overwriting the property.
|
|
|
|
// Otherwise, we continue to setting the property.
|
|
|
|
if (attributes != ABSENT) {
|
|
|
|
// Check if the existing property conflicts with regards to const.
|
|
|
|
if (is_local && (is_read_only || is_const_property)) {
|
|
|
|
const char* type = (is_read_only) ? "const" : "var";
|
|
|
|
return ThrowRedeclarationError(type, name);
|
|
|
|
};
|
|
|
|
// The property already exists without conflicting: Go to
|
|
|
|
// the next declaration.
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// Fall-through and introduce the absent property by using
|
|
|
|
// SetProperty.
|
|
|
|
} else {
|
|
|
|
if (is_local && (is_read_only || is_const_property)) {
|
|
|
|
const char* type = (is_read_only) ? "const" : "var";
|
|
|
|
return ThrowRedeclarationError(type, name);
|
|
|
|
}
|
|
|
|
// The property already exists without conflicting: Go to
|
|
|
|
// the next declaration.
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Copy the function and update its context. Use it as value.
|
|
|
|
Handle<JSFunction> boilerplate = Handle<JSFunction>::cast(value);
|
|
|
|
Handle<JSFunction> function =
|
|
|
|
Factory::NewFunctionFromBoilerplate(boilerplate, context);
|
|
|
|
value = function;
|
|
|
|
}
|
|
|
|
|
|
|
|
LookupResult lookup;
|
|
|
|
global->LocalLookup(*name, &lookup);
|
|
|
|
|
|
|
|
PropertyAttributes attributes = is_const_property
|
|
|
|
? static_cast<PropertyAttributes>(base | READ_ONLY)
|
|
|
|
: base;
|
|
|
|
|
|
|
|
if (lookup.IsProperty()) {
|
|
|
|
// There's a local property that we need to overwrite because
|
|
|
|
// we're either declaring a function or there's an interceptor
|
|
|
|
// that claims the property is absent.
|
|
|
|
|
|
|
|
// Check for conflicting re-declarations. We cannot have
|
|
|
|
// conflicting types in case of intercepted properties because
|
|
|
|
// they are absent.
|
|
|
|
if (lookup.type() != INTERCEPTOR &&
|
|
|
|
(lookup.IsReadOnly() || is_const_property)) {
|
|
|
|
const char* type = (lookup.IsReadOnly()) ? "const" : "var";
|
|
|
|
return ThrowRedeclarationError(type, name);
|
|
|
|
}
|
|
|
|
SetProperty(global, name, value, attributes);
|
|
|
|
} else {
|
|
|
|
// If a property with this name does not already exist on the
|
|
|
|
// global object add the property locally. We take special
|
|
|
|
// precautions to always add it as a local property even in case
|
|
|
|
// of callbacks in the prototype chain (this rules out using
|
|
|
|
// SetProperty). Also, we must use the handle-based version to
|
|
|
|
// avoid GC issues.
|
2008-10-02 13:45:21 +00:00
|
|
|
IgnoreAttributesAndSetLocalProperty(global, name, value, attributes);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
}
|
2008-10-06 16:57:38 +00:00
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
return Heap::undefined_value();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static Object* Runtime_DeclareContextSlot(Arguments args) {
|
|
|
|
HandleScope scope;
|
2008-10-06 16:57:38 +00:00
|
|
|
ASSERT(args.length() == 4);
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2008-10-06 16:57:38 +00:00
|
|
|
CONVERT_ARG_CHECKED(Context, context, 0);
|
|
|
|
Handle<String> name(String::cast(args[1]));
|
2008-07-03 15:10:15 +00:00
|
|
|
PropertyAttributes mode =
|
2008-10-06 16:57:38 +00:00
|
|
|
static_cast<PropertyAttributes>(Smi::cast(args[2])->value());
|
2008-07-03 15:10:15 +00:00
|
|
|
ASSERT(mode == READ_ONLY || mode == NONE);
|
2008-10-06 16:57:38 +00:00
|
|
|
Handle<Object> initial_value(args[3]);
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
// Declarations are always done in the function context.
|
|
|
|
context = Handle<Context>(context->fcontext());
|
|
|
|
|
|
|
|
int index;
|
|
|
|
PropertyAttributes attributes;
|
|
|
|
ContextLookupFlags flags = DONT_FOLLOW_CHAINS;
|
2008-10-23 08:42:22 +00:00
|
|
|
Handle<Object> holder =
|
2008-07-03 15:10:15 +00:00
|
|
|
context->Lookup(name, flags, &index, &attributes);
|
|
|
|
|
|
|
|
if (attributes != ABSENT) {
|
|
|
|
// The name was declared before; check for conflicting
|
|
|
|
// re-declarations: This is similar to the code in parser.cc in
|
|
|
|
// the AstBuildingParser::Declare function.
|
|
|
|
if (((attributes & READ_ONLY) != 0) || (mode == READ_ONLY)) {
|
|
|
|
// Functions are not read-only.
|
|
|
|
ASSERT(mode != READ_ONLY || initial_value->IsTheHole());
|
|
|
|
const char* type = ((attributes & READ_ONLY) != 0) ? "const" : "var";
|
|
|
|
return ThrowRedeclarationError(type, name);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize it if necessary.
|
|
|
|
if (*initial_value != NULL) {
|
|
|
|
if (index >= 0) {
|
|
|
|
// The variable or constant context slot should always be in
|
|
|
|
// the function context; not in any outer context nor in the
|
|
|
|
// arguments object.
|
2008-10-23 08:42:22 +00:00
|
|
|
ASSERT(holder.is_identical_to(context));
|
2008-07-03 15:10:15 +00:00
|
|
|
if (((attributes & READ_ONLY) == 0) ||
|
|
|
|
context->get(index)->IsTheHole()) {
|
|
|
|
context->set(index, *initial_value);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Slow case: The property is not in the FixedArray part of the context.
|
2008-10-23 08:42:22 +00:00
|
|
|
Handle<JSObject> context_ext = Handle<JSObject>::cast(holder);
|
2008-07-03 15:10:15 +00:00
|
|
|
SetProperty(context_ext, name, initial_value, mode);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
2008-10-06 16:57:38 +00:00
|
|
|
// The property is not in the function context. It needs to be
|
|
|
|
// "declared" in the function context's extension context, or in the
|
|
|
|
// global context.
|
|
|
|
Handle<JSObject> context_ext;
|
2008-10-24 10:59:40 +00:00
|
|
|
if (context->has_extension()) {
|
2008-10-06 16:57:38 +00:00
|
|
|
// The function context's extension context exists - use it.
|
|
|
|
context_ext = Handle<JSObject>(context->extension());
|
|
|
|
} else {
|
|
|
|
// The function context's extension context does not exists - allocate
|
|
|
|
// it.
|
|
|
|
context_ext = Factory::NewJSObject(Top::context_extension_function());
|
|
|
|
// And store it in the extension slot.
|
|
|
|
context->set_extension(*context_ext);
|
|
|
|
}
|
|
|
|
ASSERT(*context_ext != NULL);
|
|
|
|
|
|
|
|
// Declare the property by setting it to the initial value if provided,
|
|
|
|
// or undefined, and use the correct mode (e.g. READ_ONLY attribute for
|
|
|
|
// constant declarations).
|
|
|
|
ASSERT(!context_ext->HasLocalProperty(*name));
|
|
|
|
Handle<Object> value(Heap::undefined_value());
|
|
|
|
if (*initial_value != NULL) value = initial_value;
|
|
|
|
SetProperty(context_ext, name, value, mode);
|
|
|
|
ASSERT(context_ext->GetLocalPropertyAttribute(*name) == mode);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
2008-10-06 16:57:38 +00:00
|
|
|
|
|
|
|
return Heap::undefined_value();
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static Object* Runtime_InitializeVarGlobal(Arguments args) {
|
|
|
|
NoHandleAllocation nha;
|
|
|
|
|
|
|
|
// Determine if we need to assign to the variable if it already
|
|
|
|
// exists (based on the number of arguments).
|
|
|
|
RUNTIME_ASSERT(args.length() == 1 || args.length() == 2);
|
|
|
|
bool assign = args.length() == 2;
|
|
|
|
|
|
|
|
CONVERT_ARG_CHECKED(String, name, 0);
|
|
|
|
GlobalObject* global = Top::context()->global();
|
|
|
|
|
|
|
|
// According to ECMA-262, section 12.2, page 62, the property must
|
|
|
|
// not be deletable.
|
|
|
|
PropertyAttributes attributes = DONT_DELETE;
|
|
|
|
|
|
|
|
// Lookup the property locally in the global object. If it isn't
|
2009-07-02 06:28:37 +00:00
|
|
|
// there, there is a property with this name in the prototype chain.
|
|
|
|
// We follow Safari and Firefox behavior and only set the property
|
|
|
|
// locally if there is an explicit initialization value that we have
|
|
|
|
// to assign to the property. When adding the property we take
|
|
|
|
// special precautions to always add it as a local property even in
|
|
|
|
// case of callbacks in the prototype chain (this rules out using
|
|
|
|
// SetProperty). We have IgnoreAttributesAndSetLocalProperty for
|
|
|
|
// this.
|
2008-07-03 15:10:15 +00:00
|
|
|
LookupResult lookup;
|
|
|
|
global->LocalLookup(*name, &lookup);
|
|
|
|
if (!lookup.IsProperty()) {
|
2009-07-02 06:28:37 +00:00
|
|
|
if (assign) {
|
|
|
|
return global->IgnoreAttributesAndSetLocalProperty(*name,
|
|
|
|
args[1],
|
|
|
|
attributes);
|
|
|
|
}
|
|
|
|
return Heap::undefined_value();
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Determine if this is a redeclaration of something read-only.
|
|
|
|
if (lookup.IsReadOnly()) {
|
|
|
|
return ThrowRedeclarationError("const", name);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Determine if this is a redeclaration of an intercepted read-only
|
|
|
|
// property and figure out if the property exists at all.
|
|
|
|
bool found = true;
|
|
|
|
PropertyType type = lookup.type();
|
|
|
|
if (type == INTERCEPTOR) {
|
|
|
|
PropertyAttributes intercepted = global->GetPropertyAttribute(*name);
|
|
|
|
if (intercepted == ABSENT) {
|
|
|
|
// The interceptor claims the property isn't there. We need to
|
|
|
|
// make sure to introduce it.
|
|
|
|
found = false;
|
|
|
|
} else if ((intercepted & READ_ONLY) != 0) {
|
|
|
|
// The property is present, but read-only. Since we're trying to
|
|
|
|
// overwrite it with a variable declaration we must throw a
|
|
|
|
// re-declaration error.
|
|
|
|
return ThrowRedeclarationError("const", name);
|
|
|
|
}
|
|
|
|
// Restore global object from context (in case of GC).
|
|
|
|
global = Top::context()->global();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (found && !assign) {
|
|
|
|
// The global property is there and we're not assigning any value
|
|
|
|
// to it. Just return.
|
|
|
|
return Heap::undefined_value();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Assign the value (or undefined) to the property.
|
|
|
|
Object* value = (assign) ? args[1] : Heap::undefined_value();
|
|
|
|
return global->SetProperty(&lookup, *name, value, attributes);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static Object* Runtime_InitializeConstGlobal(Arguments args) {
|
|
|
|
// All constants are declared with an initial value. The name
|
|
|
|
// of the constant is the first argument and the initial value
|
|
|
|
// is the second.
|
|
|
|
RUNTIME_ASSERT(args.length() == 2);
|
|
|
|
CONVERT_ARG_CHECKED(String, name, 0);
|
|
|
|
Handle<Object> value = args.at<Object>(1);
|
|
|
|
|
|
|
|
// Get the current global object from top.
|
|
|
|
GlobalObject* global = Top::context()->global();
|
|
|
|
|
|
|
|
// According to ECMA-262, section 12.2, page 62, the property must
|
|
|
|
// not be deletable. Since it's a const, it must be READ_ONLY too.
|
|
|
|
PropertyAttributes attributes =
|
|
|
|
static_cast<PropertyAttributes>(DONT_DELETE | READ_ONLY);
|
|
|
|
|
|
|
|
// Lookup the property locally in the global object. If it isn't
|
|
|
|
// there, we add the property and take special precautions to always
|
|
|
|
// add it as a local property even in case of callbacks in the
|
|
|
|
// prototype chain (this rules out using SetProperty).
|
2008-10-03 12:14:29 +00:00
|
|
|
// We use IgnoreAttributesAndSetLocalProperty instead
|
2008-07-03 15:10:15 +00:00
|
|
|
LookupResult lookup;
|
|
|
|
global->LocalLookup(*name, &lookup);
|
|
|
|
if (!lookup.IsProperty()) {
|
2008-10-03 13:26:00 +00:00
|
|
|
return global->IgnoreAttributesAndSetLocalProperty(*name,
|
|
|
|
*value,
|
|
|
|
attributes);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Determine if this is a redeclaration of something not
|
|
|
|
// read-only. In case the result is hidden behind an interceptor we
|
|
|
|
// need to ask it for the property attributes.
|
|
|
|
if (!lookup.IsReadOnly()) {
|
|
|
|
if (lookup.type() != INTERCEPTOR) {
|
|
|
|
return ThrowRedeclarationError("var", name);
|
|
|
|
}
|
|
|
|
|
|
|
|
PropertyAttributes intercepted = global->GetPropertyAttribute(*name);
|
|
|
|
|
|
|
|
// Throw re-declaration error if the intercepted property is present
|
|
|
|
// but not read-only.
|
|
|
|
if (intercepted != ABSENT && (intercepted & READ_ONLY) == 0) {
|
|
|
|
return ThrowRedeclarationError("var", name);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Restore global object from context (in case of GC) and continue
|
|
|
|
// with setting the value because the property is either absent or
|
|
|
|
// read-only. We also have to do redo the lookup.
|
|
|
|
global = Top::context()->global();
|
|
|
|
|
|
|
|
// BUG 1213579: Handle the case where we have to set a read-only
|
|
|
|
// property through an interceptor and only do it if it's
|
|
|
|
// uninitialized, e.g. the hole. Nirk...
|
|
|
|
global->SetProperty(*name, *value, attributes);
|
|
|
|
return *value;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the value, but only we're assigning the initial value to a
|
|
|
|
// constant. For now, we determine this by checking if the
|
|
|
|
// current value is the hole.
|
|
|
|
PropertyType type = lookup.type();
|
|
|
|
if (type == FIELD) {
|
|
|
|
FixedArray* properties = global->properties();
|
|
|
|
int index = lookup.GetFieldIndex();
|
|
|
|
if (properties->get(index)->IsTheHole()) {
|
|
|
|
properties->set(index, *value);
|
|
|
|
}
|
|
|
|
} else if (type == NORMAL) {
|
2009-06-30 10:05:36 +00:00
|
|
|
if (global->GetNormalizedProperty(&lookup)->IsTheHole()) {
|
|
|
|
global->SetNormalizedProperty(&lookup, *value);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Ignore re-initialization of constants that have already been
|
|
|
|
// assigned a function value.
|
|
|
|
ASSERT(lookup.IsReadOnly() && type == CONSTANT_FUNCTION);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Use the set value as the result of the operation.
|
|
|
|
return *value;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static Object* Runtime_InitializeConstContextSlot(Arguments args) {
|
|
|
|
HandleScope scope;
|
|
|
|
ASSERT(args.length() == 3);
|
|
|
|
|
|
|
|
Handle<Object> value(args[0]);
|
|
|
|
ASSERT(!value->IsTheHole());
|
|
|
|
CONVERT_ARG_CHECKED(Context, context, 1);
|
|
|
|
Handle<String> name(String::cast(args[2]));
|
|
|
|
|
|
|
|
// Initializations are always done in the function context.
|
|
|
|
context = Handle<Context>(context->fcontext());
|
|
|
|
|
|
|
|
int index;
|
|
|
|
PropertyAttributes attributes;
|
2009-01-22 13:53:06 +00:00
|
|
|
ContextLookupFlags flags = FOLLOW_CHAINS;
|
2008-10-23 08:42:22 +00:00
|
|
|
Handle<Object> holder =
|
2008-07-03 15:10:15 +00:00
|
|
|
context->Lookup(name, flags, &index, &attributes);
|
|
|
|
|
2009-01-22 13:53:06 +00:00
|
|
|
// In most situations, the property introduced by the const
|
|
|
|
// declaration should be present in the context extension object.
|
|
|
|
// However, because declaration and initialization are separate, the
|
|
|
|
// property might have been deleted (if it was introduced by eval)
|
|
|
|
// before we reach the initialization point.
|
|
|
|
//
|
|
|
|
// Example:
|
|
|
|
//
|
|
|
|
// function f() { eval("delete x; const x;"); }
|
|
|
|
//
|
|
|
|
// In that case, the initialization behaves like a normal assignment
|
|
|
|
// to property 'x'.
|
2008-07-03 15:10:15 +00:00
|
|
|
if (index >= 0) {
|
2009-01-22 13:53:06 +00:00
|
|
|
// Property was found in a context.
|
|
|
|
if (holder->IsContext()) {
|
|
|
|
// The holder cannot be the function context. If it is, there
|
|
|
|
// should have been a const redeclaration error when declaring
|
|
|
|
// the const property.
|
|
|
|
ASSERT(!holder.is_identical_to(context));
|
|
|
|
if ((attributes & READ_ONLY) == 0) {
|
|
|
|
Handle<Context>::cast(holder)->set(index, *value);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// The holder is an arguments object.
|
|
|
|
ASSERT((attributes & READ_ONLY) == 0);
|
|
|
|
Handle<JSObject>::cast(holder)->SetElement(index, *value);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
return *value;
|
|
|
|
}
|
|
|
|
|
2009-01-22 13:53:06 +00:00
|
|
|
// The property could not be found, we introduce it in the global
|
|
|
|
// context.
|
|
|
|
if (attributes == ABSENT) {
|
|
|
|
Handle<JSObject> global = Handle<JSObject>(Top::context()->global());
|
|
|
|
SetProperty(global, name, value, NONE);
|
|
|
|
return *value;
|
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2009-01-22 13:53:06 +00:00
|
|
|
// The property was present in a context extension object.
|
|
|
|
Handle<JSObject> context_ext = Handle<JSObject>::cast(holder);
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2009-01-22 13:53:06 +00:00
|
|
|
if (*context_ext == context->extension()) {
|
|
|
|
// This is the property that was introduced by the const
|
|
|
|
// declaration. Set it if it hasn't been set before. NOTE: We
|
|
|
|
// cannot use GetProperty() to get the current value as it
|
|
|
|
// 'unholes' the value.
|
|
|
|
LookupResult lookup;
|
|
|
|
context_ext->LocalLookupRealNamedProperty(*name, &lookup);
|
|
|
|
ASSERT(lookup.IsProperty()); // the property was declared
|
|
|
|
ASSERT(lookup.IsReadOnly()); // and it was declared as read-only
|
|
|
|
|
|
|
|
PropertyType type = lookup.type();
|
|
|
|
if (type == FIELD) {
|
|
|
|
FixedArray* properties = context_ext->properties();
|
|
|
|
int index = lookup.GetFieldIndex();
|
|
|
|
if (properties->get(index)->IsTheHole()) {
|
|
|
|
properties->set(index, *value);
|
|
|
|
}
|
|
|
|
} else if (type == NORMAL) {
|
2009-06-30 10:05:36 +00:00
|
|
|
if (context_ext->GetNormalizedProperty(&lookup)->IsTheHole()) {
|
|
|
|
context_ext->SetNormalizedProperty(&lookup, *value);
|
2009-01-22 13:53:06 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// We should not reach here. Any real, named property should be
|
|
|
|
// either a field or a dictionary slot.
|
|
|
|
UNREACHABLE();
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
} else {
|
2009-01-22 13:53:06 +00:00
|
|
|
// The property was found in a different context extension object.
|
|
|
|
// Set it if it is not a read-only property.
|
|
|
|
if ((attributes & READ_ONLY) == 0) {
|
|
|
|
Handle<Object> set = SetProperty(context_ext, name, value, attributes);
|
|
|
|
// Setting a property might throw an exception. Exceptions
|
|
|
|
// are converted to empty handles in handle operations. We
|
|
|
|
// need to convert back to exceptions here.
|
|
|
|
if (set.is_null()) {
|
|
|
|
ASSERT(Top::has_pending_exception());
|
|
|
|
return Failure::Exception();
|
|
|
|
}
|
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
2009-01-22 13:53:06 +00:00
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
return *value;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-08-13 12:35:59 +00:00
|
|
|
static Object* Runtime_OptimizeObjectForAddingMultipleProperties(
|
|
|
|
Arguments args) {
|
|
|
|
HandleScope scope;
|
|
|
|
ASSERT(args.length() == 2);
|
|
|
|
CONVERT_ARG_CHECKED(JSObject, object, 0);
|
|
|
|
CONVERT_SMI_CHECKED(properties, args[1]);
|
|
|
|
if (object->HasFastProperties()) {
|
|
|
|
NormalizeProperties(object, KEEP_INOBJECT_PROPERTIES, properties);
|
|
|
|
}
|
|
|
|
return *object;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static Object* Runtime_TransformToFastProperties(Arguments args) {
|
|
|
|
HandleScope scope;
|
|
|
|
ASSERT(args.length() == 1);
|
|
|
|
CONVERT_ARG_CHECKED(JSObject, object, 0);
|
|
|
|
if (!object->HasFastProperties() && !object->IsGlobalObject()) {
|
|
|
|
TransformToFastProperties(object, 0);
|
|
|
|
}
|
|
|
|
return *object;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
static Object* Runtime_RegExpExec(Arguments args) {
|
|
|
|
HandleScope scope;
|
2009-03-11 14:00:55 +00:00
|
|
|
ASSERT(args.length() == 4);
|
2009-06-17 11:44:25 +00:00
|
|
|
CONVERT_ARG_CHECKED(JSRegExp, regexp, 0);
|
|
|
|
CONVERT_ARG_CHECKED(String, subject, 1);
|
2009-07-07 08:11:19 +00:00
|
|
|
// Due to the way the JS calls are constructed this must be less than the
|
2009-03-11 14:00:55 +00:00
|
|
|
// length of a string, i.e. it is always a Smi. We check anyway for security.
|
2009-07-07 08:11:19 +00:00
|
|
|
CONVERT_SMI_CHECKED(index, args[2]);
|
2009-06-17 11:44:25 +00:00
|
|
|
CONVERT_ARG_CHECKED(JSArray, last_match_info, 3);
|
2009-03-25 12:14:10 +00:00
|
|
|
RUNTIME_ASSERT(last_match_info->HasFastElements());
|
2009-07-07 08:11:19 +00:00
|
|
|
RUNTIME_ASSERT(index >= 0);
|
|
|
|
RUNTIME_ASSERT(index <= subject->length());
|
2009-03-11 14:00:55 +00:00
|
|
|
Handle<Object> result = RegExpImpl::Exec(regexp,
|
|
|
|
subject,
|
2009-07-07 08:11:19 +00:00
|
|
|
index,
|
2009-03-11 14:00:55 +00:00
|
|
|
last_match_info);
|
2008-11-25 11:07:48 +00:00
|
|
|
if (result.is_null()) return Failure::Exception();
|
|
|
|
return *result;
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static Object* Runtime_MaterializeRegExpLiteral(Arguments args) {
|
|
|
|
HandleScope scope;
|
|
|
|
ASSERT(args.length() == 4);
|
|
|
|
CONVERT_ARG_CHECKED(FixedArray, literals, 0);
|
|
|
|
int index = Smi::cast(args[1])->value();
|
|
|
|
Handle<String> pattern = args.at<String>(2);
|
|
|
|
Handle<String> flags = args.at<String>(3);
|
|
|
|
|
2008-10-03 07:14:31 +00:00
|
|
|
// Get the RegExp function from the context in the literals array.
|
|
|
|
// This is the RegExp function from the context in which the
|
|
|
|
// function was created. We do not use the RegExp function from the
|
|
|
|
// current global context because this might be the RegExp function
|
|
|
|
// from another context which we should not have access to.
|
2008-08-22 13:33:59 +00:00
|
|
|
Handle<JSFunction> constructor =
|
2008-09-25 07:46:07 +00:00
|
|
|
Handle<JSFunction>(
|
|
|
|
JSFunction::GlobalContextFromLiterals(*literals)->regexp_function());
|
2008-07-03 15:10:15 +00:00
|
|
|
// Compute the regular expression literal.
|
|
|
|
bool has_pending_exception;
|
|
|
|
Handle<Object> regexp =
|
2008-08-22 13:33:59 +00:00
|
|
|
RegExpImpl::CreateRegExpLiteral(constructor, pattern, flags,
|
|
|
|
&has_pending_exception);
|
2008-07-03 15:10:15 +00:00
|
|
|
if (has_pending_exception) {
|
|
|
|
ASSERT(Top::has_pending_exception());
|
|
|
|
return Failure::Exception();
|
|
|
|
}
|
|
|
|
literals->set(index, *regexp);
|
|
|
|
return *regexp;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static Object* Runtime_FunctionGetName(Arguments args) {
|
|
|
|
NoHandleAllocation ha;
|
|
|
|
ASSERT(args.length() == 1);
|
|
|
|
|
|
|
|
CONVERT_CHECKED(JSFunction, f, args[0]);
|
|
|
|
return f->shared()->name();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-09-25 07:35:45 +00:00
|
|
|
static Object* Runtime_FunctionSetName(Arguments args) {
|
|
|
|
NoHandleAllocation ha;
|
|
|
|
ASSERT(args.length() == 2);
|
|
|
|
|
|
|
|
CONVERT_CHECKED(JSFunction, f, args[0]);
|
|
|
|
CONVERT_CHECKED(String, name, args[1]);
|
|
|
|
f->shared()->set_name(name);
|
|
|
|
return Heap::undefined_value();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
static Object* Runtime_FunctionGetScript(Arguments args) {
|
|
|
|
HandleScope scope;
|
|
|
|
ASSERT(args.length() == 1);
|
|
|
|
|
|
|
|
CONVERT_CHECKED(JSFunction, fun, args[0]);
|
|
|
|
Handle<Object> script = Handle<Object>(fun->shared()->script());
|
|
|
|
if (!script->IsScript()) return Heap::undefined_value();
|
|
|
|
|
|
|
|
return *GetScriptWrapper(Handle<Script>::cast(script));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static Object* Runtime_FunctionGetSourceCode(Arguments args) {
|
|
|
|
NoHandleAllocation ha;
|
|
|
|
ASSERT(args.length() == 1);
|
|
|
|
|
|
|
|
CONVERT_CHECKED(JSFunction, f, args[0]);
|
|
|
|
return f->shared()->GetSourceCode();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static Object* Runtime_FunctionGetScriptSourcePosition(Arguments args) {
|
|
|
|
NoHandleAllocation ha;
|
|
|
|
ASSERT(args.length() == 1);
|
|
|
|
|
|
|
|
CONVERT_CHECKED(JSFunction, fun, args[0]);
|
|
|
|
int pos = fun->shared()->start_position();
|
|
|
|
return Smi::FromInt(pos);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-06-30 11:08:37 +00:00
|
|
|
static Object* Runtime_FunctionGetPositionForOffset(Arguments args) {
|
|
|
|
ASSERT(args.length() == 2);
|
|
|
|
|
|
|
|
CONVERT_CHECKED(JSFunction, fun, args[0]);
|
|
|
|
CONVERT_NUMBER_CHECKED(int, offset, Int32, args[1]);
|
|
|
|
|
|
|
|
Code* code = fun->code();
|
|
|
|
RUNTIME_ASSERT(0 <= offset && offset < code->Size());
|
|
|
|
|
|
|
|
Address pc = code->address() + offset;
|
|
|
|
return Smi::FromInt(fun->code()->SourcePosition(pc));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
static Object* Runtime_FunctionSetInstanceClassName(Arguments args) {
|
|
|
|
NoHandleAllocation ha;
|
|
|
|
ASSERT(args.length() == 2);
|
|
|
|
|
|
|
|
CONVERT_CHECKED(JSFunction, fun, args[0]);
|
|
|
|
CONVERT_CHECKED(String, name, args[1]);
|
|
|
|
fun->SetInstanceClassName(name);
|
|
|
|
return Heap::undefined_value();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static Object* Runtime_FunctionSetLength(Arguments args) {
|
|
|
|
NoHandleAllocation ha;
|
|
|
|
ASSERT(args.length() == 2);
|
|
|
|
|
|
|
|
CONVERT_CHECKED(JSFunction, fun, args[0]);
|
|
|
|
CONVERT_CHECKED(Smi, length, args[1]);
|
|
|
|
fun->shared()->set_length(length->value());
|
|
|
|
return length;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static Object* Runtime_FunctionSetPrototype(Arguments args) {
|
2008-08-27 10:11:39 +00:00
|
|
|
NoHandleAllocation ha;
|
2008-07-03 15:10:15 +00:00
|
|
|
ASSERT(args.length() == 2);
|
|
|
|
|
|
|
|
CONVERT_CHECKED(JSFunction, fun, args[0]);
|
2008-08-27 10:11:39 +00:00
|
|
|
Object* obj = Accessors::FunctionSetPrototype(fun, args[1], NULL);
|
|
|
|
if (obj->IsFailure()) return obj;
|
2008-07-03 15:10:15 +00:00
|
|
|
return args[0]; // return TOS
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-12-15 09:15:05 +00:00
|
|
|
static Object* Runtime_FunctionIsAPIFunction(Arguments args) {
|
|
|
|
NoHandleAllocation ha;
|
|
|
|
ASSERT(args.length() == 1);
|
|
|
|
|
|
|
|
CONVERT_CHECKED(JSFunction, f, args[0]);
|
|
|
|
// The function_data field of the shared function info is used exclusively by
|
|
|
|
// the API.
|
|
|
|
return !f->shared()->function_data()->IsUndefined() ? Heap::true_value()
|
|
|
|
: Heap::false_value();
|
|
|
|
}
|
|
|
|
|
2009-09-17 11:11:01 +00:00
|
|
|
static Object* Runtime_FunctionIsBuiltin(Arguments args) {
|
|
|
|
NoHandleAllocation ha;
|
|
|
|
ASSERT(args.length() == 1);
|
|
|
|
|
|
|
|
CONVERT_CHECKED(JSFunction, f, args[0]);
|
|
|
|
return f->IsBuiltin() ? Heap::true_value() : Heap::false_value();
|
|
|
|
}
|
|
|
|
|
2008-12-15 09:15:05 +00:00
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
static Object* Runtime_SetCode(Arguments args) {
|
|
|
|
HandleScope scope;
|
|
|
|
ASSERT(args.length() == 2);
|
|
|
|
|
2009-06-17 11:44:25 +00:00
|
|
|
CONVERT_ARG_CHECKED(JSFunction, target, 0);
|
2008-07-03 15:10:15 +00:00
|
|
|
Handle<Object> code = args.at<Object>(1);
|
|
|
|
|
|
|
|
Handle<Context> context(target->context());
|
|
|
|
|
|
|
|
if (!code->IsNull()) {
|
|
|
|
RUNTIME_ASSERT(code->IsJSFunction());
|
|
|
|
Handle<JSFunction> fun = Handle<JSFunction>::cast(code);
|
|
|
|
SetExpectedNofProperties(target, fun->shared()->expected_nof_properties());
|
|
|
|
if (!fun->is_compiled() && !CompileLazy(fun, KEEP_EXCEPTION)) {
|
|
|
|
return Failure::Exception();
|
|
|
|
}
|
|
|
|
// Set the code, formal parameter count, and the length of the target
|
|
|
|
// function.
|
|
|
|
target->set_code(fun->code());
|
|
|
|
target->shared()->set_length(fun->shared()->length());
|
|
|
|
target->shared()->set_formal_parameter_count(
|
2008-10-03 07:14:31 +00:00
|
|
|
fun->shared()->formal_parameter_count());
|
2008-10-15 10:22:26 +00:00
|
|
|
// Set the source code of the target function to undefined.
|
|
|
|
// SetCode is only used for built-in constructors like String,
|
|
|
|
// Array, and Object, and some web code
|
|
|
|
// doesn't like seeing source code for constructors.
|
|
|
|
target->shared()->set_script(Heap::undefined_value());
|
2009-08-26 12:22:44 +00:00
|
|
|
// Clear the optimization hints related to the compiled code as these are no
|
|
|
|
// longer valid when the code is overwritten.
|
|
|
|
target->shared()->ClearThisPropertyAssignmentsInfo();
|
2008-07-03 15:10:15 +00:00
|
|
|
context = Handle<Context>(fun->context());
|
|
|
|
|
|
|
|
// Make sure we get a fresh copy of the literal vector to avoid
|
|
|
|
// cross context contamination.
|
2008-08-22 13:33:59 +00:00
|
|
|
int number_of_literals = fun->NumberOfLiterals();
|
|
|
|
Handle<FixedArray> literals =
|
|
|
|
Factory::NewFixedArray(number_of_literals, TENURED);
|
2008-07-03 15:10:15 +00:00
|
|
|
if (number_of_literals > 0) {
|
2008-08-22 13:33:59 +00:00
|
|
|
// Insert the object, regexp and array functions in the literals
|
|
|
|
// array prefix. These are the functions that will be used when
|
|
|
|
// creating object, regexp and array literals.
|
2008-09-25 07:46:07 +00:00
|
|
|
literals->set(JSFunction::kLiteralGlobalContextIndex,
|
|
|
|
context->global_context());
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
2008-10-23 14:55:45 +00:00
|
|
|
target->set_literals(*literals, SKIP_WRITE_BARRIER);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
target->set_context(*context);
|
|
|
|
return *target;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static Object* CharCodeAt(String* subject, Object* index) {
|
|
|
|
uint32_t i = 0;
|
2008-08-27 10:11:39 +00:00
|
|
|
if (!Array::IndexFromObject(index, &i)) return Heap::nan_value();
|
2008-07-03 15:10:15 +00:00
|
|
|
// Flatten the string. If someone wants to get a char at an index
|
|
|
|
// in a cons string, it is likely that more indices will be
|
|
|
|
// accessed.
|
2009-03-17 09:33:06 +00:00
|
|
|
subject->TryFlattenIfNotFlat();
|
|
|
|
if (i >= static_cast<uint32_t>(subject->length())) {
|
2008-11-03 10:16:05 +00:00
|
|
|
return Heap::nan_value();
|
|
|
|
}
|
2009-03-17 09:33:06 +00:00
|
|
|
return Smi::FromInt(subject->Get(i));
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static Object* Runtime_StringCharCodeAt(Arguments args) {
|
|
|
|
NoHandleAllocation ha;
|
|
|
|
ASSERT(args.length() == 2);
|
|
|
|
|
|
|
|
CONVERT_CHECKED(String, subject, args[0]);
|
|
|
|
Object* index = args[1];
|
|
|
|
return CharCodeAt(subject, index);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static Object* Runtime_CharFromCode(Arguments args) {
|
|
|
|
NoHandleAllocation ha;
|
|
|
|
ASSERT(args.length() == 1);
|
|
|
|
uint32_t code;
|
|
|
|
if (Array::IndexFromObject(args[0], &code)) {
|
|
|
|
if (code <= 0xffff) {
|
|
|
|
return Heap::LookupSingleCharacterStringFromCode(code);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Heap::empty_string();
|
|
|
|
}
|
|
|
|
|
2009-03-13 10:22:38 +00:00
|
|
|
// Forward declarations.
|
|
|
|
static const int kStringBuilderConcatHelperLengthBits = 11;
|
|
|
|
static const int kStringBuilderConcatHelperPositionBits = 19;
|
|
|
|
|
|
|
|
template <typename schar>
|
|
|
|
static inline void StringBuilderConcatHelper(String*,
|
|
|
|
schar*,
|
|
|
|
FixedArray*,
|
|
|
|
int);
|
|
|
|
|
|
|
|
typedef BitField<int, 0, 11> StringBuilderSubstringLength;
|
|
|
|
typedef BitField<int, 11, 19> StringBuilderSubstringPosition;
|
|
|
|
|
|
|
|
class ReplacementStringBuilder {
|
|
|
|
public:
|
|
|
|
ReplacementStringBuilder(Handle<String> subject, int estimated_part_count)
|
|
|
|
: subject_(subject),
|
|
|
|
parts_(Factory::NewFixedArray(estimated_part_count)),
|
|
|
|
part_count_(0),
|
|
|
|
character_count_(0),
|
2009-05-01 11:16:29 +00:00
|
|
|
is_ascii_(subject->IsAsciiRepresentation()) {
|
2009-03-13 10:22:38 +00:00
|
|
|
// Require a non-zero initial size. Ensures that doubling the size to
|
|
|
|
// extend the array will work.
|
|
|
|
ASSERT(estimated_part_count > 0);
|
|
|
|
}
|
|
|
|
|
2009-03-16 09:49:14 +00:00
|
|
|
void EnsureCapacity(int elements) {
|
|
|
|
int length = parts_->length();
|
|
|
|
int required_length = part_count_ + elements;
|
|
|
|
if (length < required_length) {
|
|
|
|
int new_length = length;
|
|
|
|
do {
|
|
|
|
new_length *= 2;
|
|
|
|
} while (new_length < required_length);
|
|
|
|
Handle<FixedArray> extended_array =
|
|
|
|
Factory::NewFixedArray(new_length);
|
|
|
|
parts_->CopyTo(0, *extended_array, 0, part_count_);
|
|
|
|
parts_ = extended_array;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-03-13 10:22:38 +00:00
|
|
|
void AddSubjectSlice(int from, int to) {
|
|
|
|
ASSERT(from >= 0);
|
|
|
|
int length = to - from;
|
2009-03-16 09:49:14 +00:00
|
|
|
ASSERT(length > 0);
|
|
|
|
// Can we encode the slice in 11 bits for length and 19 bits for
|
|
|
|
// start position - as used by StringBuilderConcatHelper?
|
|
|
|
if (StringBuilderSubstringLength::is_valid(length) &&
|
|
|
|
StringBuilderSubstringPosition::is_valid(from)) {
|
|
|
|
int encoded_slice = StringBuilderSubstringLength::encode(length) |
|
|
|
|
StringBuilderSubstringPosition::encode(from);
|
|
|
|
AddElement(Smi::FromInt(encoded_slice));
|
|
|
|
} else {
|
|
|
|
Handle<String> slice = Factory::NewStringSlice(subject_, from, to);
|
|
|
|
AddElement(*slice);
|
2009-03-13 10:22:38 +00:00
|
|
|
}
|
2009-03-16 09:49:14 +00:00
|
|
|
IncrementCharacterCount(length);
|
2009-03-13 10:22:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void AddString(Handle<String> string) {
|
2009-03-17 09:33:06 +00:00
|
|
|
int length = string->length();
|
2009-03-16 09:49:14 +00:00
|
|
|
ASSERT(length > 0);
|
|
|
|
AddElement(*string);
|
2009-05-01 11:16:29 +00:00
|
|
|
if (!string->IsAsciiRepresentation()) {
|
2009-03-16 09:49:14 +00:00
|
|
|
is_ascii_ = false;
|
2009-03-13 10:22:38 +00:00
|
|
|
}
|
2009-03-16 09:49:14 +00:00
|
|
|
IncrementCharacterCount(length);
|
2009-03-13 10:22:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Handle<String> ToString() {
|
|
|
|
if (part_count_ == 0) {
|
|
|
|
return Factory::empty_string();
|
|
|
|
}
|
|
|
|
|
|
|
|
Handle<String> joined_string;
|
|
|
|
if (is_ascii_) {
|
|
|
|
joined_string = NewRawAsciiString(character_count_);
|
|
|
|
AssertNoAllocation no_alloc;
|
|
|
|
SeqAsciiString* seq = SeqAsciiString::cast(*joined_string);
|
|
|
|
char* char_buffer = seq->GetChars();
|
|
|
|
StringBuilderConcatHelper(*subject_,
|
|
|
|
char_buffer,
|
|
|
|
*parts_,
|
|
|
|
part_count_);
|
|
|
|
} else {
|
|
|
|
// Non-ASCII.
|
|
|
|
joined_string = NewRawTwoByteString(character_count_);
|
|
|
|
AssertNoAllocation no_alloc;
|
|
|
|
SeqTwoByteString* seq = SeqTwoByteString::cast(*joined_string);
|
|
|
|
uc16* char_buffer = seq->GetChars();
|
|
|
|
StringBuilderConcatHelper(*subject_,
|
|
|
|
char_buffer,
|
|
|
|
*parts_,
|
|
|
|
part_count_);
|
|
|
|
}
|
|
|
|
return joined_string;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void IncrementCharacterCount(int by) {
|
|
|
|
if (character_count_ > Smi::kMaxValue - by) {
|
|
|
|
V8::FatalProcessOutOfMemory("String.replace result too large.");
|
|
|
|
}
|
|
|
|
character_count_ += by;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
Handle<String> NewRawAsciiString(int size) {
|
|
|
|
CALL_HEAP_FUNCTION(Heap::AllocateRawAsciiString(size), String);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Handle<String> NewRawTwoByteString(int size) {
|
|
|
|
CALL_HEAP_FUNCTION(Heap::AllocateRawTwoByteString(size), String);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-16 09:49:14 +00:00
|
|
|
void AddElement(Object* element) {
|
2009-03-13 10:22:38 +00:00
|
|
|
ASSERT(element->IsSmi() || element->IsString());
|
2009-05-19 09:11:30 +00:00
|
|
|
ASSERT(parts_->length() > part_count_);
|
2009-03-16 09:49:14 +00:00
|
|
|
parts_->set(part_count_, element);
|
2009-03-13 10:22:38 +00:00
|
|
|
part_count_++;
|
|
|
|
}
|
|
|
|
|
|
|
|
Handle<String> subject_;
|
|
|
|
Handle<FixedArray> parts_;
|
|
|
|
int part_count_;
|
|
|
|
int character_count_;
|
|
|
|
bool is_ascii_;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class CompiledReplacement {
|
|
|
|
public:
|
|
|
|
CompiledReplacement()
|
|
|
|
: parts_(1), replacement_substrings_(0) {}
|
|
|
|
|
|
|
|
void Compile(Handle<String> replacement,
|
|
|
|
int capture_count,
|
|
|
|
int subject_length);
|
|
|
|
|
|
|
|
void Apply(ReplacementStringBuilder* builder,
|
|
|
|
int match_from,
|
|
|
|
int match_to,
|
|
|
|
Handle<JSArray> last_match_info);
|
|
|
|
|
|
|
|
// Number of distinct parts of the replacement pattern.
|
|
|
|
int parts() {
|
|
|
|
return parts_.length();
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
enum PartType {
|
|
|
|
SUBJECT_PREFIX = 1,
|
|
|
|
SUBJECT_SUFFIX,
|
|
|
|
SUBJECT_CAPTURE,
|
|
|
|
REPLACEMENT_SUBSTRING,
|
|
|
|
REPLACEMENT_STRING,
|
|
|
|
|
|
|
|
NUMBER_OF_PART_TYPES
|
|
|
|
};
|
|
|
|
|
|
|
|
struct ReplacementPart {
|
|
|
|
static inline ReplacementPart SubjectMatch() {
|
|
|
|
return ReplacementPart(SUBJECT_CAPTURE, 0);
|
|
|
|
}
|
|
|
|
static inline ReplacementPart SubjectCapture(int capture_index) {
|
|
|
|
return ReplacementPart(SUBJECT_CAPTURE, capture_index);
|
|
|
|
}
|
|
|
|
static inline ReplacementPart SubjectPrefix() {
|
|
|
|
return ReplacementPart(SUBJECT_PREFIX, 0);
|
|
|
|
}
|
|
|
|
static inline ReplacementPart SubjectSuffix(int subject_length) {
|
|
|
|
return ReplacementPart(SUBJECT_SUFFIX, subject_length);
|
|
|
|
}
|
|
|
|
static inline ReplacementPart ReplacementString() {
|
|
|
|
return ReplacementPart(REPLACEMENT_STRING, 0);
|
|
|
|
}
|
|
|
|
static inline ReplacementPart ReplacementSubString(int from, int to) {
|
|
|
|
ASSERT(from >= 0);
|
|
|
|
ASSERT(to > from);
|
|
|
|
return ReplacementPart(-from, to);
|
|
|
|
}
|
|
|
|
|
|
|
|
// If tag <= 0 then it is the negation of a start index of a substring of
|
|
|
|
// the replacement pattern, otherwise it's a value from PartType.
|
|
|
|
ReplacementPart(int tag, int data)
|
|
|
|
: tag(tag), data(data) {
|
|
|
|
// Must be non-positive or a PartType value.
|
|
|
|
ASSERT(tag < NUMBER_OF_PART_TYPES);
|
|
|
|
}
|
|
|
|
// Either a value of PartType or a non-positive number that is
|
|
|
|
// the negation of an index into the replacement string.
|
|
|
|
int tag;
|
|
|
|
// The data value's interpretation depends on the value of tag:
|
|
|
|
// tag == SUBJECT_PREFIX ||
|
|
|
|
// tag == SUBJECT_SUFFIX: data is unused.
|
|
|
|
// tag == SUBJECT_CAPTURE: data is the number of the capture.
|
|
|
|
// tag == REPLACEMENT_SUBSTRING ||
|
|
|
|
// tag == REPLACEMENT_STRING: data is index into array of substrings
|
|
|
|
// of the replacement string.
|
|
|
|
// tag <= 0: Temporary representation of the substring of the replacement
|
|
|
|
// string ranging over -tag .. data.
|
|
|
|
// Is replaced by REPLACEMENT_{SUB,}STRING when we create the
|
|
|
|
// substring objects.
|
|
|
|
int data;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename Char>
|
|
|
|
static void ParseReplacementPattern(ZoneList<ReplacementPart>* parts,
|
|
|
|
Vector<Char> characters,
|
|
|
|
int capture_count,
|
|
|
|
int subject_length) {
|
|
|
|
int length = characters.length();
|
|
|
|
int last = 0;
|
|
|
|
for (int i = 0; i < length; i++) {
|
|
|
|
Char c = characters[i];
|
|
|
|
if (c == '$') {
|
|
|
|
int next_index = i + 1;
|
|
|
|
if (next_index == length) { // No next character!
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
Char c2 = characters[next_index];
|
|
|
|
switch (c2) {
|
|
|
|
case '$':
|
|
|
|
if (i > last) {
|
|
|
|
// There is a substring before. Include the first "$".
|
|
|
|
parts->Add(ReplacementPart::ReplacementSubString(last, next_index));
|
|
|
|
last = next_index + 1; // Continue after the second "$".
|
|
|
|
} else {
|
|
|
|
// Let the next substring start with the second "$".
|
|
|
|
last = next_index;
|
|
|
|
}
|
|
|
|
i = next_index;
|
|
|
|
break;
|
|
|
|
case '`':
|
|
|
|
if (i > last) {
|
|
|
|
parts->Add(ReplacementPart::ReplacementSubString(last, i));
|
|
|
|
}
|
|
|
|
parts->Add(ReplacementPart::SubjectPrefix());
|
|
|
|
i = next_index;
|
|
|
|
last = i + 1;
|
|
|
|
break;
|
|
|
|
case '\'':
|
|
|
|
if (i > last) {
|
|
|
|
parts->Add(ReplacementPart::ReplacementSubString(last, i));
|
|
|
|
}
|
|
|
|
parts->Add(ReplacementPart::SubjectSuffix(subject_length));
|
|
|
|
i = next_index;
|
|
|
|
last = i + 1;
|
|
|
|
break;
|
|
|
|
case '&':
|
|
|
|
if (i > last) {
|
|
|
|
parts->Add(ReplacementPart::ReplacementSubString(last, i));
|
|
|
|
}
|
|
|
|
parts->Add(ReplacementPart::SubjectMatch());
|
|
|
|
i = next_index;
|
|
|
|
last = i + 1;
|
|
|
|
break;
|
|
|
|
case '0':
|
|
|
|
case '1':
|
|
|
|
case '2':
|
|
|
|
case '3':
|
|
|
|
case '4':
|
|
|
|
case '5':
|
|
|
|
case '6':
|
|
|
|
case '7':
|
|
|
|
case '8':
|
|
|
|
case '9': {
|
|
|
|
int capture_ref = c2 - '0';
|
|
|
|
if (capture_ref > capture_count) {
|
|
|
|
i = next_index;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
int second_digit_index = next_index + 1;
|
|
|
|
if (second_digit_index < length) {
|
|
|
|
// Peek ahead to see if we have two digits.
|
|
|
|
Char c3 = characters[second_digit_index];
|
|
|
|
if ('0' <= c3 && c3 <= '9') { // Double digits.
|
|
|
|
int double_digit_ref = capture_ref * 10 + c3 - '0';
|
|
|
|
if (double_digit_ref <= capture_count) {
|
|
|
|
next_index = second_digit_index;
|
|
|
|
capture_ref = double_digit_ref;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (capture_ref > 0) {
|
|
|
|
if (i > last) {
|
|
|
|
parts->Add(ReplacementPart::ReplacementSubString(last, i));
|
|
|
|
}
|
2009-05-19 09:11:30 +00:00
|
|
|
ASSERT(capture_ref <= capture_count);
|
2009-03-13 10:22:38 +00:00
|
|
|
parts->Add(ReplacementPart::SubjectCapture(capture_ref));
|
|
|
|
last = next_index + 1;
|
|
|
|
}
|
|
|
|
i = next_index;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
i = next_index;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (length > last) {
|
|
|
|
if (last == 0) {
|
|
|
|
parts->Add(ReplacementPart::ReplacementString());
|
|
|
|
} else {
|
|
|
|
parts->Add(ReplacementPart::ReplacementSubString(last, length));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ZoneList<ReplacementPart> parts_;
|
|
|
|
ZoneList<Handle<String> > replacement_substrings_;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
void CompiledReplacement::Compile(Handle<String> replacement,
|
|
|
|
int capture_count,
|
|
|
|
int subject_length) {
|
2009-03-17 09:33:06 +00:00
|
|
|
ASSERT(replacement->IsFlat());
|
2009-05-01 11:16:29 +00:00
|
|
|
if (replacement->IsAsciiRepresentation()) {
|
2009-03-13 10:22:38 +00:00
|
|
|
AssertNoAllocation no_alloc;
|
|
|
|
ParseReplacementPattern(&parts_,
|
|
|
|
replacement->ToAsciiVector(),
|
|
|
|
capture_count,
|
|
|
|
subject_length);
|
|
|
|
} else {
|
2009-05-01 11:16:29 +00:00
|
|
|
ASSERT(replacement->IsTwoByteRepresentation());
|
2009-03-13 10:22:38 +00:00
|
|
|
AssertNoAllocation no_alloc;
|
|
|
|
|
|
|
|
ParseReplacementPattern(&parts_,
|
|
|
|
replacement->ToUC16Vector(),
|
|
|
|
capture_count,
|
|
|
|
subject_length);
|
|
|
|
}
|
|
|
|
// Find substrings of replacement string and create them as String objects..
|
|
|
|
int substring_index = 0;
|
|
|
|
for (int i = 0, n = parts_.length(); i < n; i++) {
|
|
|
|
int tag = parts_[i].tag;
|
|
|
|
if (tag <= 0) { // A replacement string slice.
|
|
|
|
int from = -tag;
|
|
|
|
int to = parts_[i].data;
|
|
|
|
replacement_substrings_.Add(Factory::NewStringSlice(replacement,
|
|
|
|
from,
|
|
|
|
to));
|
|
|
|
parts_[i].tag = REPLACEMENT_SUBSTRING;
|
|
|
|
parts_[i].data = substring_index;
|
|
|
|
substring_index++;
|
|
|
|
} else if (tag == REPLACEMENT_STRING) {
|
|
|
|
replacement_substrings_.Add(replacement);
|
|
|
|
parts_[i].data = substring_index;
|
|
|
|
substring_index++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void CompiledReplacement::Apply(ReplacementStringBuilder* builder,
|
|
|
|
int match_from,
|
|
|
|
int match_to,
|
|
|
|
Handle<JSArray> last_match_info) {
|
|
|
|
for (int i = 0, n = parts_.length(); i < n; i++) {
|
|
|
|
ReplacementPart part = parts_[i];
|
|
|
|
switch (part.tag) {
|
|
|
|
case SUBJECT_PREFIX:
|
2009-03-16 09:49:14 +00:00
|
|
|
if (match_from > 0) builder->AddSubjectSlice(0, match_from);
|
2009-03-13 10:22:38 +00:00
|
|
|
break;
|
|
|
|
case SUBJECT_SUFFIX: {
|
|
|
|
int subject_length = part.data;
|
2009-03-16 09:49:14 +00:00
|
|
|
if (match_to < subject_length) {
|
|
|
|
builder->AddSubjectSlice(match_to, subject_length);
|
|
|
|
}
|
2009-03-13 10:22:38 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case SUBJECT_CAPTURE: {
|
|
|
|
int capture = part.data;
|
2009-07-28 08:43:51 +00:00
|
|
|
FixedArray* match_info = FixedArray::cast(last_match_info->elements());
|
2009-03-13 10:22:38 +00:00
|
|
|
int from = RegExpImpl::GetCapture(match_info, capture * 2);
|
|
|
|
int to = RegExpImpl::GetCapture(match_info, capture * 2 + 1);
|
|
|
|
if (from >= 0 && to > from) {
|
|
|
|
builder->AddSubjectSlice(from, to);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case REPLACEMENT_SUBSTRING:
|
|
|
|
case REPLACEMENT_STRING:
|
|
|
|
builder->AddString(replacement_substrings_[part.data]);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static Object* StringReplaceRegExpWithString(String* subject,
|
|
|
|
JSRegExp* regexp,
|
|
|
|
String* replacement,
|
|
|
|
JSArray* last_match_info) {
|
2009-03-17 09:33:06 +00:00
|
|
|
ASSERT(subject->IsFlat());
|
|
|
|
ASSERT(replacement->IsFlat());
|
2009-03-13 10:22:38 +00:00
|
|
|
|
|
|
|
HandleScope handles;
|
|
|
|
|
|
|
|
int length = subject->length();
|
|
|
|
Handle<String> subject_handle(subject);
|
|
|
|
Handle<JSRegExp> regexp_handle(regexp);
|
|
|
|
Handle<String> replacement_handle(replacement);
|
|
|
|
Handle<JSArray> last_match_info_handle(last_match_info);
|
|
|
|
Handle<Object> match = RegExpImpl::Exec(regexp_handle,
|
|
|
|
subject_handle,
|
|
|
|
0,
|
|
|
|
last_match_info_handle);
|
|
|
|
if (match.is_null()) {
|
|
|
|
return Failure::Exception();
|
|
|
|
}
|
|
|
|
if (match->IsNull()) {
|
|
|
|
return *subject_handle;
|
|
|
|
}
|
|
|
|
|
|
|
|
int capture_count = regexp_handle->CaptureCount();
|
|
|
|
|
|
|
|
// CompiledReplacement uses zone allocation.
|
2009-05-14 13:17:28 +00:00
|
|
|
CompilationZoneScope zone(DELETE_ON_EXIT);
|
2009-03-13 10:22:38 +00:00
|
|
|
CompiledReplacement compiled_replacement;
|
|
|
|
compiled_replacement.Compile(replacement_handle,
|
|
|
|
capture_count,
|
|
|
|
length);
|
|
|
|
|
|
|
|
bool is_global = regexp_handle->GetFlags().is_global();
|
|
|
|
|
|
|
|
// Guessing the number of parts that the final result string is built
|
|
|
|
// from. Global regexps can match any number of times, so we guess
|
|
|
|
// conservatively.
|
|
|
|
int expected_parts =
|
|
|
|
(compiled_replacement.parts() + 1) * (is_global ? 4 : 1) + 1;
|
|
|
|
ReplacementStringBuilder builder(subject_handle, expected_parts);
|
|
|
|
|
|
|
|
// Index of end of last match.
|
|
|
|
int prev = 0;
|
|
|
|
|
2009-03-16 09:49:14 +00:00
|
|
|
// Number of parts added by compiled replacement plus preceeding string
|
|
|
|
// and possibly suffix after last match.
|
|
|
|
const int parts_added_per_loop = compiled_replacement.parts() + 2;
|
2009-03-23 12:37:53 +00:00
|
|
|
bool matched = true;
|
2009-03-13 10:22:38 +00:00
|
|
|
do {
|
|
|
|
ASSERT(last_match_info_handle->HasFastElements());
|
2009-03-16 09:49:14 +00:00
|
|
|
// Increase the capacity of the builder before entering local handle-scope,
|
|
|
|
// so its internal buffer can safely allocate a new handle if it grows.
|
|
|
|
builder.EnsureCapacity(parts_added_per_loop);
|
|
|
|
|
|
|
|
HandleScope loop_scope;
|
2009-03-15 22:28:26 +00:00
|
|
|
int start, end;
|
|
|
|
{
|
|
|
|
AssertNoAllocation match_info_array_is_not_in_a_handle;
|
2009-07-28 08:43:51 +00:00
|
|
|
FixedArray* match_info_array =
|
|
|
|
FixedArray::cast(last_match_info_handle->elements());
|
2009-03-15 22:28:26 +00:00
|
|
|
|
|
|
|
ASSERT_EQ(capture_count * 2 + 2,
|
|
|
|
RegExpImpl::GetLastCaptureCount(match_info_array));
|
|
|
|
start = RegExpImpl::GetCapture(match_info_array, 0);
|
|
|
|
end = RegExpImpl::GetCapture(match_info_array, 1);
|
|
|
|
}
|
2009-03-13 10:22:38 +00:00
|
|
|
|
|
|
|
if (prev < start) {
|
|
|
|
builder.AddSubjectSlice(prev, start);
|
|
|
|
}
|
|
|
|
compiled_replacement.Apply(&builder,
|
|
|
|
start,
|
|
|
|
end,
|
|
|
|
last_match_info_handle);
|
|
|
|
prev = end;
|
|
|
|
|
|
|
|
// Only continue checking for global regexps.
|
|
|
|
if (!is_global) break;
|
|
|
|
|
|
|
|
// Continue from where the match ended, unless it was an empty match.
|
|
|
|
int next = end;
|
|
|
|
if (start == end) {
|
|
|
|
next = end + 1;
|
|
|
|
if (next > length) break;
|
|
|
|
}
|
|
|
|
|
|
|
|
match = RegExpImpl::Exec(regexp_handle,
|
|
|
|
subject_handle,
|
|
|
|
next,
|
|
|
|
last_match_info_handle);
|
|
|
|
if (match.is_null()) {
|
|
|
|
return Failure::Exception();
|
|
|
|
}
|
2009-03-16 09:49:14 +00:00
|
|
|
matched = !match->IsNull();
|
|
|
|
} while (matched);
|
2009-03-13 10:22:38 +00:00
|
|
|
|
|
|
|
if (prev < length) {
|
|
|
|
builder.AddSubjectSlice(prev, length);
|
|
|
|
}
|
|
|
|
|
|
|
|
return *(builder.ToString());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static Object* Runtime_StringReplaceRegExpWithString(Arguments args) {
|
|
|
|
ASSERT(args.length() == 4);
|
|
|
|
|
|
|
|
CONVERT_CHECKED(String, subject, args[0]);
|
2009-03-17 09:33:06 +00:00
|
|
|
if (!subject->IsFlat()) {
|
|
|
|
Object* flat_subject = subject->TryFlatten();
|
2009-03-13 10:22:38 +00:00
|
|
|
if (flat_subject->IsFailure()) {
|
|
|
|
return flat_subject;
|
|
|
|
}
|
|
|
|
subject = String::cast(flat_subject);
|
|
|
|
}
|
|
|
|
|
|
|
|
CONVERT_CHECKED(String, replacement, args[2]);
|
2009-03-17 09:33:06 +00:00
|
|
|
if (!replacement->IsFlat()) {
|
|
|
|
Object* flat_replacement = replacement->TryFlatten();
|
2009-03-13 10:22:38 +00:00
|
|
|
if (flat_replacement->IsFailure()) {
|
|
|
|
return flat_replacement;
|
|
|
|
}
|
|
|
|
replacement = String::cast(flat_replacement);
|
|
|
|
}
|
|
|
|
|
|
|
|
CONVERT_CHECKED(JSRegExp, regexp, args[1]);
|
|
|
|
CONVERT_CHECKED(JSArray, last_match_info, args[3]);
|
|
|
|
|
|
|
|
ASSERT(last_match_info->HasFastElements());
|
|
|
|
|
|
|
|
return StringReplaceRegExpWithString(subject,
|
|
|
|
regexp,
|
|
|
|
replacement,
|
|
|
|
last_match_info);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2008-10-14 08:57:31 +00:00
|
|
|
// Cap on the maximal shift in the Boyer-Moore implementation. By setting a
|
|
|
|
// limit, we can fix the size of tables.
|
|
|
|
static const int kBMMaxShift = 0xff;
|
2008-10-16 11:33:19 +00:00
|
|
|
// Reduce alphabet to this size.
|
|
|
|
static const int kBMAlphabetSize = 0x100;
|
|
|
|
// For patterns below this length, the skip length of Boyer-Moore is too short
|
|
|
|
// to compensate for the algorithmic overhead compared to simple brute force.
|
|
|
|
static const int kBMMinPatternLength = 5;
|
2008-10-14 08:57:31 +00:00
|
|
|
|
|
|
|
// Holds the two buffers used by Boyer-Moore string search's Good Suffix
|
|
|
|
// shift. Only allows the last kBMMaxShift characters of the needle
|
|
|
|
// to be indexed.
|
2008-10-17 11:15:54 +00:00
|
|
|
class BMGoodSuffixBuffers {
|
2008-10-14 08:57:31 +00:00
|
|
|
public:
|
|
|
|
BMGoodSuffixBuffers() {}
|
|
|
|
inline void init(int needle_length) {
|
|
|
|
ASSERT(needle_length > 1);
|
|
|
|
int start = needle_length < kBMMaxShift ? 0 : needle_length - kBMMaxShift;
|
|
|
|
int len = needle_length - start;
|
|
|
|
biased_suffixes_ = suffixes_ - start;
|
|
|
|
biased_good_suffix_shift_ = good_suffix_shift_ - start;
|
|
|
|
for (int i = 0; i <= len; i++) {
|
|
|
|
good_suffix_shift_[i] = len;
|
2008-10-07 08:11:44 +00:00
|
|
|
}
|
|
|
|
}
|
2008-10-14 08:57:31 +00:00
|
|
|
inline int& suffix(int index) {
|
|
|
|
ASSERT(biased_suffixes_ + index >= suffixes_);
|
|
|
|
return biased_suffixes_[index];
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
2008-10-14 08:57:31 +00:00
|
|
|
inline int& shift(int index) {
|
|
|
|
ASSERT(biased_good_suffix_shift_ + index >= good_suffix_shift_);
|
|
|
|
return biased_good_suffix_shift_[index];
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
int suffixes_[kBMMaxShift + 1];
|
|
|
|
int good_suffix_shift_[kBMMaxShift + 1];
|
2008-10-23 07:28:21 +00:00
|
|
|
int* biased_suffixes_;
|
|
|
|
int* biased_good_suffix_shift_;
|
2008-10-14 08:57:31 +00:00
|
|
|
DISALLOW_COPY_AND_ASSIGN(BMGoodSuffixBuffers);
|
|
|
|
};
|
|
|
|
|
|
|
|
// buffers reused by BoyerMoore
|
2008-12-17 09:53:06 +00:00
|
|
|
static int bad_char_occurrence[kBMAlphabetSize];
|
2008-10-14 08:57:31 +00:00
|
|
|
static BMGoodSuffixBuffers bmgs_buffers;
|
|
|
|
|
2008-10-14 13:25:54 +00:00
|
|
|
// Compute the bad-char table for Boyer-Moore in the static buffer.
|
2008-10-14 14:25:37 +00:00
|
|
|
template <typename pchar>
|
|
|
|
static void BoyerMoorePopulateBadCharTable(Vector<const pchar> pattern,
|
2008-10-14 13:25:54 +00:00
|
|
|
int start) {
|
|
|
|
// Run forwards to populate bad_char_table, so that *last* instance
|
|
|
|
// of character equivalence class is the one registered.
|
|
|
|
// Notice: Doesn't include the last character.
|
2008-10-17 11:15:54 +00:00
|
|
|
int table_size = (sizeof(pchar) == 1) ? String::kMaxAsciiCharCode + 1
|
|
|
|
: kBMAlphabetSize;
|
|
|
|
if (start == 0) { // All patterns less than kBMMaxShift in length.
|
2008-12-17 09:53:06 +00:00
|
|
|
memset(bad_char_occurrence, -1, table_size * sizeof(*bad_char_occurrence));
|
2008-10-17 11:15:54 +00:00
|
|
|
} else {
|
|
|
|
for (int i = 0; i < table_size; i++) {
|
2008-12-17 09:53:06 +00:00
|
|
|
bad_char_occurrence[i] = start - 1;
|
2008-10-17 11:15:54 +00:00
|
|
|
}
|
2008-10-14 13:25:54 +00:00
|
|
|
}
|
2008-10-16 11:33:19 +00:00
|
|
|
for (int i = start; i < pattern.length() - 1; i++) {
|
|
|
|
pchar c = pattern[i];
|
2008-10-17 11:15:54 +00:00
|
|
|
int bucket = (sizeof(pchar) ==1) ? c : c % kBMAlphabetSize;
|
2008-12-17 09:53:06 +00:00
|
|
|
bad_char_occurrence[bucket] = i;
|
2008-10-14 13:25:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename pchar>
|
|
|
|
static void BoyerMoorePopulateGoodSuffixTable(Vector<const pchar> pattern,
|
2008-10-16 11:33:19 +00:00
|
|
|
int start) {
|
2008-10-14 13:25:54 +00:00
|
|
|
int m = pattern.length();
|
2008-10-16 11:33:19 +00:00
|
|
|
int len = m - start;
|
2008-10-14 13:25:54 +00:00
|
|
|
// Compute Good Suffix tables.
|
|
|
|
bmgs_buffers.init(m);
|
|
|
|
|
|
|
|
bmgs_buffers.shift(m-1) = 1;
|
|
|
|
bmgs_buffers.suffix(m) = m + 1;
|
|
|
|
pchar last_char = pattern[m - 1];
|
|
|
|
int suffix = m + 1;
|
|
|
|
for (int i = m; i > start;) {
|
|
|
|
for (pchar c = pattern[i - 1]; suffix <= m && c != pattern[suffix - 1];) {
|
|
|
|
if (bmgs_buffers.shift(suffix) == len) {
|
|
|
|
bmgs_buffers.shift(suffix) = suffix - i;
|
|
|
|
}
|
|
|
|
suffix = bmgs_buffers.suffix(suffix);
|
|
|
|
}
|
|
|
|
i--;
|
|
|
|
suffix--;
|
|
|
|
bmgs_buffers.suffix(i) = suffix;
|
|
|
|
if (suffix == m) {
|
|
|
|
// No suffix to extend, so we check against last_char only.
|
|
|
|
while (i > start && pattern[i - 1] != last_char) {
|
|
|
|
if (bmgs_buffers.shift(m) == len) {
|
|
|
|
bmgs_buffers.shift(m) = m - i;
|
|
|
|
}
|
|
|
|
i--;
|
|
|
|
bmgs_buffers.suffix(i) = m;
|
|
|
|
}
|
|
|
|
if (i > start) {
|
|
|
|
i--;
|
|
|
|
suffix--;
|
|
|
|
bmgs_buffers.suffix(i) = suffix;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (suffix < m) {
|
|
|
|
for (int i = start; i <= m; i++) {
|
|
|
|
if (bmgs_buffers.shift(i) == len) {
|
|
|
|
bmgs_buffers.shift(i) = suffix - start;
|
|
|
|
}
|
|
|
|
if (i == suffix) {
|
|
|
|
suffix = bmgs_buffers.suffix(suffix);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-10-17 11:15:54 +00:00
|
|
|
template <typename schar, typename pchar>
|
2008-12-17 09:53:06 +00:00
|
|
|
static inline int CharOccurrence(int char_code) {
|
2008-10-17 11:15:54 +00:00
|
|
|
if (sizeof(schar) == 1) {
|
2008-12-17 09:53:06 +00:00
|
|
|
return bad_char_occurrence[char_code];
|
2008-10-17 11:15:54 +00:00
|
|
|
}
|
|
|
|
if (sizeof(pchar) == 1) {
|
|
|
|
if (char_code > String::kMaxAsciiCharCode) {
|
|
|
|
return -1;
|
|
|
|
}
|
2008-12-17 09:53:06 +00:00
|
|
|
return bad_char_occurrence[char_code];
|
2008-10-17 11:15:54 +00:00
|
|
|
}
|
2008-12-17 09:53:06 +00:00
|
|
|
return bad_char_occurrence[char_code % kBMAlphabetSize];
|
2008-10-17 11:15:54 +00:00
|
|
|
}
|
2008-10-16 11:33:19 +00:00
|
|
|
|
2008-12-17 09:53:06 +00:00
|
|
|
// Restricted simplified Boyer-Moore string matching.
|
|
|
|
// Uses only the bad-shift table of Boyer-Moore and only uses it
|
|
|
|
// for the character compared to the last character of the needle.
|
2008-10-07 13:25:49 +00:00
|
|
|
template <typename schar, typename pchar>
|
2009-03-19 09:12:24 +00:00
|
|
|
static int BoyerMooreHorspool(Vector<const schar> subject,
|
|
|
|
Vector<const pchar> pattern,
|
|
|
|
int start_index,
|
|
|
|
bool* complete) {
|
2008-10-07 13:25:49 +00:00
|
|
|
int n = subject.length();
|
2008-10-16 11:33:19 +00:00
|
|
|
int m = pattern.length();
|
2008-10-14 08:57:31 +00:00
|
|
|
// Only preprocess at most kBMMaxShift last characters of pattern.
|
|
|
|
int start = m < kBMMaxShift ? 0 : m - kBMMaxShift;
|
2008-10-07 13:25:49 +00:00
|
|
|
|
2008-10-14 14:25:37 +00:00
|
|
|
BoyerMoorePopulateBadCharTable(pattern, start);
|
2008-10-07 08:11:44 +00:00
|
|
|
|
2008-10-16 11:33:19 +00:00
|
|
|
int badness = -m; // How bad we are doing without a good-suffix table.
|
2008-10-14 12:45:06 +00:00
|
|
|
int idx; // No matches found prior to this index.
|
2008-10-16 11:33:19 +00:00
|
|
|
pchar last_char = pattern[m - 1];
|
2008-12-17 09:53:06 +00:00
|
|
|
int last_char_shift = m - 1 - CharOccurrence<schar, pchar>(last_char);
|
2008-10-14 08:57:31 +00:00
|
|
|
// Perform search
|
2008-10-14 12:45:06 +00:00
|
|
|
for (idx = start_index; idx <= n - m;) {
|
2008-10-14 08:57:31 +00:00
|
|
|
int j = m - 1;
|
2008-10-16 11:33:19 +00:00
|
|
|
int c;
|
|
|
|
while (last_char != (c = subject[idx + j])) {
|
2008-12-17 09:53:06 +00:00
|
|
|
int bc_occ = CharOccurrence<schar, pchar>(c);
|
2008-10-16 11:33:19 +00:00
|
|
|
int shift = j - bc_occ;
|
|
|
|
idx += shift;
|
|
|
|
badness += 1 - shift; // at most zero, so badness cannot increase.
|
|
|
|
if (idx > n - m) {
|
2008-10-23 07:28:21 +00:00
|
|
|
*complete = true;
|
2008-10-16 11:33:19 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
j--;
|
2008-12-17 09:53:06 +00:00
|
|
|
while (j >= 0 && pattern[j] == (subject[idx + j])) j--;
|
2008-10-14 08:57:31 +00:00
|
|
|
if (j < 0) {
|
2008-10-23 07:28:21 +00:00
|
|
|
*complete = true;
|
2008-10-14 12:45:06 +00:00
|
|
|
return idx;
|
2008-10-07 08:11:44 +00:00
|
|
|
} else {
|
2008-12-17 09:53:06 +00:00
|
|
|
idx += last_char_shift;
|
2008-10-14 12:45:06 +00:00
|
|
|
// Badness increases by the number of characters we have
|
|
|
|
// checked, and decreases by the number of characters we
|
|
|
|
// can skip by shifting. It's a measure of how we are doing
|
|
|
|
// compared to reading each character exactly once.
|
2008-12-17 09:53:06 +00:00
|
|
|
badness += (m - j) - last_char_shift;
|
2008-10-16 11:33:19 +00:00
|
|
|
if (badness > 0) {
|
2008-10-23 07:28:21 +00:00
|
|
|
*complete = false;
|
2008-10-16 11:33:19 +00:00
|
|
|
return idx;
|
|
|
|
}
|
2008-09-19 12:30:14 +00:00
|
|
|
}
|
|
|
|
}
|
2008-10-23 07:28:21 +00:00
|
|
|
*complete = true;
|
2008-10-16 11:33:19 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2008-10-14 12:45:06 +00:00
|
|
|
|
2008-10-16 11:33:19 +00:00
|
|
|
|
|
|
|
template <typename schar, typename pchar>
|
|
|
|
static int BoyerMooreIndexOf(Vector<const schar> subject,
|
|
|
|
Vector<const pchar> pattern,
|
|
|
|
int idx) {
|
|
|
|
int n = subject.length();
|
|
|
|
int m = pattern.length();
|
|
|
|
// Only preprocess at most kBMMaxShift last characters of pattern.
|
|
|
|
int start = m < kBMMaxShift ? 0 : m - kBMMaxShift;
|
|
|
|
|
|
|
|
// Build the Good Suffix table and continue searching.
|
|
|
|
BoyerMoorePopulateGoodSuffixTable(pattern, start);
|
|
|
|
pchar last_char = pattern[m - 1];
|
|
|
|
// Continue search from i.
|
2009-05-19 09:01:03 +00:00
|
|
|
while (idx <= n - m) {
|
2008-10-16 11:33:19 +00:00
|
|
|
int j = m - 1;
|
|
|
|
schar c;
|
|
|
|
while (last_char != (c = subject[idx + j])) {
|
2008-12-17 09:53:06 +00:00
|
|
|
int shift = j - CharOccurrence<schar, pchar>(c);
|
2008-10-16 11:33:19 +00:00
|
|
|
idx += shift;
|
|
|
|
if (idx > n - m) {
|
|
|
|
return -1;
|
2008-10-14 12:45:06 +00:00
|
|
|
}
|
2008-10-16 11:33:19 +00:00
|
|
|
}
|
|
|
|
while (j >= 0 && pattern[j] == (c = subject[idx + j])) j--;
|
|
|
|
if (j < 0) {
|
|
|
|
return idx;
|
|
|
|
} else if (j < start) {
|
|
|
|
// we have matched more than our tables allow us to be smart about.
|
2008-12-17 09:53:06 +00:00
|
|
|
// Fall back on BMH shift.
|
|
|
|
idx += m - 1 - CharOccurrence<schar, pchar>(last_char);
|
2008-10-16 11:33:19 +00:00
|
|
|
} else {
|
|
|
|
int gs_shift = bmgs_buffers.shift(j + 1); // Good suffix shift.
|
2008-12-17 09:53:06 +00:00
|
|
|
int bc_occ = CharOccurrence<schar, pchar>(c);
|
2008-10-16 11:33:19 +00:00
|
|
|
int shift = j - bc_occ; // Bad-char shift.
|
2008-12-17 09:53:06 +00:00
|
|
|
if (gs_shift > shift) {
|
|
|
|
shift = gs_shift;
|
|
|
|
}
|
2008-10-16 11:33:19 +00:00
|
|
|
idx += shift;
|
|
|
|
}
|
2009-05-19 09:01:03 +00:00
|
|
|
}
|
2008-10-14 12:45:06 +00:00
|
|
|
|
2008-10-14 08:57:31 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2008-09-19 12:30:14 +00:00
|
|
|
|
2008-10-16 11:33:19 +00:00
|
|
|
|
|
|
|
template <typename schar>
|
2008-10-14 08:57:31 +00:00
|
|
|
static int SingleCharIndexOf(Vector<const schar> string,
|
2008-12-02 09:50:39 +00:00
|
|
|
schar pattern_char,
|
2008-10-14 08:57:31 +00:00
|
|
|
int start_index) {
|
|
|
|
for (int i = start_index, n = string.length(); i < n; i++) {
|
|
|
|
if (pattern_char == string[i]) {
|
|
|
|
return i;
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
2008-10-14 08:57:31 +00:00
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Trivial string search for shorter strings.
|
2008-10-14 14:02:48 +00:00
|
|
|
// On return, if "complete" is set to true, the return value is the
|
|
|
|
// final result of searching for the patter in the subject.
|
|
|
|
// If "complete" is set to false, the return value is the index where
|
|
|
|
// further checking should start, i.e., it's guaranteed that the pattern
|
|
|
|
// does not occur at a position prior to the returned index.
|
2008-10-14 08:57:31 +00:00
|
|
|
template <typename pchar, typename schar>
|
|
|
|
static int SimpleIndexOf(Vector<const schar> subject,
|
|
|
|
Vector<const pchar> pattern,
|
2008-10-16 11:33:19 +00:00
|
|
|
int idx,
|
2008-10-23 07:28:21 +00:00
|
|
|
bool* complete) {
|
2008-10-16 11:33:19 +00:00
|
|
|
// Badness is a count of how much work we have done. When we have
|
|
|
|
// done enough work we decide it's probably worth switching to a better
|
|
|
|
// algorithm.
|
|
|
|
int badness = -10 - (pattern.length() << 2);
|
2008-10-14 08:57:31 +00:00
|
|
|
// We know our pattern is at least 2 characters, we cache the first so
|
|
|
|
// the common case of the first character not matching is faster.
|
|
|
|
pchar pattern_first_char = pattern[0];
|
2008-10-14 14:02:48 +00:00
|
|
|
|
2008-10-16 11:33:19 +00:00
|
|
|
for (int i = idx, n = subject.length() - pattern.length(); i <= n; i++) {
|
|
|
|
badness++;
|
|
|
|
if (badness > 0) {
|
2008-10-23 07:28:21 +00:00
|
|
|
*complete = false;
|
2008-12-17 09:53:06 +00:00
|
|
|
return i;
|
2008-10-16 11:33:19 +00:00
|
|
|
}
|
2008-10-14 08:57:31 +00:00
|
|
|
if (subject[i] != pattern_first_char) continue;
|
|
|
|
int j = 1;
|
2008-10-14 12:45:06 +00:00
|
|
|
do {
|
2008-10-14 14:02:48 +00:00
|
|
|
if (pattern[j] != subject[i+j]) {
|
2008-10-14 12:45:06 +00:00
|
|
|
break;
|
2008-10-14 08:57:31 +00:00
|
|
|
}
|
2008-10-14 12:45:06 +00:00
|
|
|
j++;
|
2008-10-16 11:33:19 +00:00
|
|
|
} while (j < pattern.length());
|
|
|
|
if (j == pattern.length()) {
|
2008-10-23 07:28:21 +00:00
|
|
|
*complete = true;
|
2008-10-14 12:45:06 +00:00
|
|
|
return i;
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
2008-10-14 14:02:48 +00:00
|
|
|
badness += j;
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
2008-10-23 07:28:21 +00:00
|
|
|
*complete = true;
|
2008-10-02 15:35:28 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2008-10-16 11:33:19 +00:00
|
|
|
// Simple indexOf that never bails out. For short patterns only.
|
|
|
|
template <typename pchar, typename schar>
|
|
|
|
static int SimpleIndexOf(Vector<const schar> subject,
|
|
|
|
Vector<const pchar> pattern,
|
|
|
|
int idx) {
|
|
|
|
pchar pattern_first_char = pattern[0];
|
|
|
|
for (int i = idx, n = subject.length() - pattern.length(); i <= n; i++) {
|
|
|
|
if (subject[i] != pattern_first_char) continue;
|
|
|
|
int j = 1;
|
|
|
|
do {
|
|
|
|
if (pattern[j] != subject[i+j]) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
j++;
|
|
|
|
} while (j < pattern.length());
|
|
|
|
if (j == pattern.length()) {
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Dispatch to different algorithms.
|
2008-10-07 08:11:44 +00:00
|
|
|
template <typename schar, typename pchar>
|
2008-10-07 13:25:49 +00:00
|
|
|
static int StringMatchStrategy(Vector<const schar> sub,
|
|
|
|
Vector<const pchar> pat,
|
|
|
|
int start_index) {
|
2008-10-14 14:02:48 +00:00
|
|
|
ASSERT(pat.length() > 1);
|
2008-10-07 08:11:44 +00:00
|
|
|
|
2008-10-14 14:25:37 +00:00
|
|
|
// We have an ASCII haystack and a non-ASCII needle. Check if there
|
|
|
|
// really is a non-ASCII character in the needle and bail out if there
|
|
|
|
// is.
|
|
|
|
if (sizeof(pchar) > 1 && sizeof(schar) == 1) {
|
|
|
|
for (int i = 0; i < pat.length(); i++) {
|
|
|
|
uc16 c = pat[i];
|
|
|
|
if (c > String::kMaxAsciiCharCode) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-10-16 11:33:19 +00:00
|
|
|
if (pat.length() < kBMMinPatternLength) {
|
|
|
|
// We don't believe fancy searching can ever be more efficient.
|
|
|
|
// The max shift of Boyer-Moore on a pattern of this length does
|
|
|
|
// not compensate for the overhead.
|
|
|
|
return SimpleIndexOf(sub, pat, start_index);
|
|
|
|
}
|
|
|
|
// Try algorithms in order of increasing setup cost and expected performance.
|
2008-10-14 14:02:48 +00:00
|
|
|
bool complete;
|
2008-10-23 07:28:21 +00:00
|
|
|
int idx = SimpleIndexOf(sub, pat, start_index, &complete);
|
2008-10-14 14:02:48 +00:00
|
|
|
if (complete) return idx;
|
2009-03-19 09:12:24 +00:00
|
|
|
idx = BoyerMooreHorspool(sub, pat, idx, &complete);
|
2008-10-16 11:33:19 +00:00
|
|
|
if (complete) return idx;
|
2008-10-14 14:02:48 +00:00
|
|
|
return BoyerMooreIndexOf(sub, pat, idx);
|
2008-10-07 08:11:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Perform string match of pattern on subject, starting at start index.
|
|
|
|
// Caller must ensure that 0 <= start_index <= sub->length(),
|
|
|
|
// and should check that pat->length() + start_index <= sub->length()
|
2008-10-07 13:25:49 +00:00
|
|
|
int Runtime::StringMatch(Handle<String> sub,
|
|
|
|
Handle<String> pat,
|
|
|
|
int start_index) {
|
2008-10-07 08:11:44 +00:00
|
|
|
ASSERT(0 <= start_index);
|
2009-03-17 09:33:06 +00:00
|
|
|
ASSERT(start_index <= sub->length());
|
2008-10-07 08:11:44 +00:00
|
|
|
|
2008-11-05 10:26:08 +00:00
|
|
|
int pattern_length = pat->length();
|
2008-10-07 13:25:49 +00:00
|
|
|
if (pattern_length == 0) return start_index;
|
|
|
|
|
2009-03-17 09:33:06 +00:00
|
|
|
int subject_length = sub->length();
|
2008-10-07 13:25:49 +00:00
|
|
|
if (start_index + pattern_length > subject_length) return -1;
|
|
|
|
|
2009-03-17 09:33:06 +00:00
|
|
|
if (!sub->IsFlat()) {
|
2008-11-03 10:16:05 +00:00
|
|
|
FlattenString(sub);
|
|
|
|
}
|
2008-10-14 08:57:31 +00:00
|
|
|
// Searching for one specific character is common. For one
|
|
|
|
// character patterns linear search is necessary, so any smart
|
|
|
|
// algorithm is unnecessary overhead.
|
|
|
|
if (pattern_length == 1) {
|
|
|
|
AssertNoAllocation no_heap_allocation; // ensure vectors stay valid
|
2009-05-01 11:16:29 +00:00
|
|
|
if (sub->IsAsciiRepresentation()) {
|
2009-03-17 09:33:06 +00:00
|
|
|
uc16 pchar = pat->Get(0);
|
2008-12-02 09:50:39 +00:00
|
|
|
if (pchar > String::kMaxAsciiCharCode) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
Vector<const char> ascii_vector =
|
|
|
|
sub->ToAsciiVector().SubVector(start_index, subject_length);
|
2008-12-02 10:19:10 +00:00
|
|
|
const void* pos = memchr(ascii_vector.start(),
|
|
|
|
static_cast<const char>(pchar),
|
|
|
|
static_cast<size_t>(ascii_vector.length()));
|
2008-12-02 09:50:39 +00:00
|
|
|
if (pos == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
2008-12-02 10:19:10 +00:00
|
|
|
return reinterpret_cast<const char*>(pos) - ascii_vector.start()
|
|
|
|
+ start_index;
|
2008-10-14 08:57:31 +00:00
|
|
|
}
|
2009-03-17 09:33:06 +00:00
|
|
|
return SingleCharIndexOf(sub->ToUC16Vector(), pat->Get(0), start_index);
|
2008-10-14 08:57:31 +00:00
|
|
|
}
|
|
|
|
|
2009-03-17 09:33:06 +00:00
|
|
|
if (!pat->IsFlat()) {
|
2008-11-03 10:16:05 +00:00
|
|
|
FlattenString(pat);
|
|
|
|
}
|
2008-10-07 08:11:44 +00:00
|
|
|
|
|
|
|
AssertNoAllocation no_heap_allocation; // ensure vectors stay valid
|
|
|
|
// dispatch on type of strings
|
2009-05-01 11:16:29 +00:00
|
|
|
if (pat->IsAsciiRepresentation()) {
|
2008-10-09 08:08:04 +00:00
|
|
|
Vector<const char> pat_vector = pat->ToAsciiVector();
|
2009-05-01 11:16:29 +00:00
|
|
|
if (sub->IsAsciiRepresentation()) {
|
2008-10-09 08:08:04 +00:00
|
|
|
return StringMatchStrategy(sub->ToAsciiVector(), pat_vector, start_index);
|
2008-10-07 08:11:44 +00:00
|
|
|
}
|
2008-10-09 08:08:04 +00:00
|
|
|
return StringMatchStrategy(sub->ToUC16Vector(), pat_vector, start_index);
|
2008-10-07 08:11:44 +00:00
|
|
|
}
|
2008-10-09 08:08:04 +00:00
|
|
|
Vector<const uc16> pat_vector = pat->ToUC16Vector();
|
2009-05-01 11:16:29 +00:00
|
|
|
if (sub->IsAsciiRepresentation()) {
|
2008-10-09 08:08:04 +00:00
|
|
|
return StringMatchStrategy(sub->ToAsciiVector(), pat_vector, start_index);
|
2008-10-07 08:11:44 +00:00
|
|
|
}
|
2008-10-09 08:08:04 +00:00
|
|
|
return StringMatchStrategy(sub->ToUC16Vector(), pat_vector, start_index);
|
2008-10-07 08:11:44 +00:00
|
|
|
}
|
|
|
|
|
2008-10-02 15:35:28 +00:00
|
|
|
|
|
|
|
static Object* Runtime_StringIndexOf(Arguments args) {
|
2008-10-07 08:11:44 +00:00
|
|
|
HandleScope scope; // create a new handle scope
|
2008-10-02 15:35:28 +00:00
|
|
|
ASSERT(args.length() == 3);
|
|
|
|
|
2008-10-07 08:11:44 +00:00
|
|
|
CONVERT_ARG_CHECKED(String, sub, 0);
|
|
|
|
CONVERT_ARG_CHECKED(String, pat, 1);
|
|
|
|
|
2008-10-02 15:35:28 +00:00
|
|
|
Object* index = args[2];
|
|
|
|
uint32_t start_index;
|
|
|
|
if (!Array::IndexFromObject(index, &start_index)) return Smi::FromInt(-1);
|
|
|
|
|
2008-10-31 08:51:26 +00:00
|
|
|
RUNTIME_ASSERT(start_index <= static_cast<uint32_t>(sub->length()));
|
2008-10-07 13:25:49 +00:00
|
|
|
int position = Runtime::StringMatch(sub, pat, start_index);
|
2008-10-07 08:11:44 +00:00
|
|
|
return Smi::FromInt(position);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static Object* Runtime_StringLastIndexOf(Arguments args) {
|
|
|
|
NoHandleAllocation ha;
|
|
|
|
ASSERT(args.length() == 3);
|
|
|
|
|
|
|
|
CONVERT_CHECKED(String, sub, args[0]);
|
|
|
|
CONVERT_CHECKED(String, pat, args[1]);
|
|
|
|
Object* index = args[2];
|
|
|
|
|
2009-03-17 09:33:06 +00:00
|
|
|
sub->TryFlattenIfNotFlat();
|
|
|
|
pat->TryFlattenIfNotFlat();
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
uint32_t start_index;
|
|
|
|
if (!Array::IndexFromObject(index, &start_index)) return Smi::FromInt(-1);
|
|
|
|
|
2009-03-17 09:33:06 +00:00
|
|
|
uint32_t pattern_length = pat->length();
|
|
|
|
uint32_t sub_length = sub->length();
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2008-07-09 11:06:54 +00:00
|
|
|
if (start_index + pattern_length > sub_length) {
|
2008-07-03 15:10:15 +00:00
|
|
|
start_index = sub_length - pattern_length;
|
2008-07-09 11:06:54 +00:00
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
for (int i = start_index; i >= 0; i--) {
|
|
|
|
bool found = true;
|
|
|
|
for (uint32_t j = 0; j < pattern_length; j++) {
|
2009-03-17 09:33:06 +00:00
|
|
|
if (sub->Get(i + j) != pat->Get(j)) {
|
2008-07-03 15:10:15 +00:00
|
|
|
found = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (found) return Smi::FromInt(i);
|
|
|
|
}
|
|
|
|
|
|
|
|
return Smi::FromInt(-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static Object* Runtime_StringLocaleCompare(Arguments args) {
|
|
|
|
NoHandleAllocation ha;
|
|
|
|
ASSERT(args.length() == 2);
|
|
|
|
|
|
|
|
CONVERT_CHECKED(String, str1, args[0]);
|
|
|
|
CONVERT_CHECKED(String, str2, args[1]);
|
|
|
|
|
|
|
|
if (str1 == str2) return Smi::FromInt(0); // Equal.
|
2009-03-17 09:33:06 +00:00
|
|
|
int str1_length = str1->length();
|
|
|
|
int str2_length = str2->length();
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
// Decide trivial cases without flattening.
|
|
|
|
if (str1_length == 0) {
|
|
|
|
if (str2_length == 0) return Smi::FromInt(0); // Equal.
|
|
|
|
return Smi::FromInt(-str2_length);
|
|
|
|
} else {
|
|
|
|
if (str2_length == 0) return Smi::FromInt(str1_length);
|
|
|
|
}
|
|
|
|
|
|
|
|
int end = str1_length < str2_length ? str1_length : str2_length;
|
|
|
|
|
|
|
|
// No need to flatten if we are going to find the answer on the first
|
|
|
|
// character. At this point we know there is at least one character
|
|
|
|
// in each string, due to the trivial case handling above.
|
2009-03-17 09:33:06 +00:00
|
|
|
int d = str1->Get(0) - str2->Get(0);
|
2008-07-03 15:10:15 +00:00
|
|
|
if (d != 0) return Smi::FromInt(d);
|
|
|
|
|
2009-03-17 09:33:06 +00:00
|
|
|
str1->TryFlattenIfNotFlat();
|
|
|
|
str2->TryFlattenIfNotFlat();
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
static StringInputBuffer buf1;
|
|
|
|
static StringInputBuffer buf2;
|
|
|
|
|
|
|
|
buf1.Reset(str1);
|
|
|
|
buf2.Reset(str2);
|
|
|
|
|
|
|
|
for (int i = 0; i < end; i++) {
|
|
|
|
uint16_t char1 = buf1.GetNext();
|
|
|
|
uint16_t char2 = buf2.GetNext();
|
|
|
|
if (char1 != char2) return Smi::FromInt(char1 - char2);
|
|
|
|
}
|
|
|
|
|
|
|
|
return Smi::FromInt(str1_length - str2_length);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static Object* Runtime_StringSlice(Arguments args) {
|
|
|
|
NoHandleAllocation ha;
|
|
|
|
ASSERT(args.length() == 3);
|
|
|
|
|
|
|
|
CONVERT_CHECKED(String, value, args[0]);
|
|
|
|
CONVERT_DOUBLE_CHECKED(from_number, args[1]);
|
|
|
|
CONVERT_DOUBLE_CHECKED(to_number, args[2]);
|
|
|
|
|
|
|
|
int start = FastD2I(from_number);
|
|
|
|
int end = FastD2I(to_number);
|
|
|
|
|
|
|
|
RUNTIME_ASSERT(end >= start);
|
|
|
|
RUNTIME_ASSERT(start >= 0);
|
2008-11-05 10:26:08 +00:00
|
|
|
RUNTIME_ASSERT(end <= value->length());
|
|
|
|
return value->Slice(start, end);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-25 12:14:10 +00:00
|
|
|
static Object* Runtime_StringMatch(Arguments args) {
|
|
|
|
ASSERT_EQ(3, args.length());
|
|
|
|
|
|
|
|
CONVERT_ARG_CHECKED(String, subject, 0);
|
|
|
|
CONVERT_ARG_CHECKED(JSRegExp, regexp, 1);
|
|
|
|
CONVERT_ARG_CHECKED(JSArray, regexp_info, 2);
|
|
|
|
HandleScope handles;
|
|
|
|
|
|
|
|
Handle<Object> match = RegExpImpl::Exec(regexp, subject, 0, regexp_info);
|
|
|
|
|
|
|
|
if (match.is_null()) {
|
|
|
|
return Failure::Exception();
|
|
|
|
}
|
|
|
|
if (match->IsNull()) {
|
|
|
|
return Heap::null_value();
|
|
|
|
}
|
|
|
|
int length = subject->length();
|
|
|
|
|
2009-05-14 13:17:28 +00:00
|
|
|
CompilationZoneScope zone_space(DELETE_ON_EXIT);
|
2009-03-25 12:14:10 +00:00
|
|
|
ZoneList<int> offsets(8);
|
|
|
|
do {
|
|
|
|
int start;
|
|
|
|
int end;
|
|
|
|
{
|
|
|
|
AssertNoAllocation no_alloc;
|
2009-07-28 08:43:51 +00:00
|
|
|
FixedArray* elements = FixedArray::cast(regexp_info->elements());
|
2009-03-25 12:14:10 +00:00
|
|
|
start = Smi::cast(elements->get(RegExpImpl::kFirstCapture))->value();
|
|
|
|
end = Smi::cast(elements->get(RegExpImpl::kFirstCapture + 1))->value();
|
|
|
|
}
|
|
|
|
offsets.Add(start);
|
|
|
|
offsets.Add(end);
|
|
|
|
int index = start < end ? end : end + 1;
|
|
|
|
if (index > length) break;
|
|
|
|
match = RegExpImpl::Exec(regexp, subject, index, regexp_info);
|
|
|
|
if (match.is_null()) {
|
|
|
|
return Failure::Exception();
|
|
|
|
}
|
|
|
|
} while (!match->IsNull());
|
|
|
|
int matches = offsets.length() / 2;
|
|
|
|
Handle<FixedArray> elements = Factory::NewFixedArray(matches);
|
|
|
|
for (int i = 0; i < matches ; i++) {
|
|
|
|
int from = offsets.at(i * 2);
|
|
|
|
int to = offsets.at(i * 2 + 1);
|
|
|
|
elements->set(i, *Factory::NewStringSlice(subject, from, to));
|
|
|
|
}
|
|
|
|
Handle<JSArray> result = Factory::NewJSArrayWithElements(elements);
|
|
|
|
result->set_length(Smi::FromInt(matches));
|
|
|
|
return *result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
static Object* Runtime_NumberToRadixString(Arguments args) {
|
|
|
|
NoHandleAllocation ha;
|
|
|
|
ASSERT(args.length() == 2);
|
|
|
|
|
2009-06-16 06:44:09 +00:00
|
|
|
// Fast case where the result is a one character string.
|
|
|
|
if (args[0]->IsSmi() && args[1]->IsSmi()) {
|
|
|
|
int value = Smi::cast(args[0])->value();
|
|
|
|
int radix = Smi::cast(args[1])->value();
|
|
|
|
if (value >= 0 && value < radix) {
|
|
|
|
RUNTIME_ASSERT(radix <= 36);
|
|
|
|
// Character array used for conversion.
|
|
|
|
static const char kCharTable[] = "0123456789abcdefghijklmnopqrstuvwxyz";
|
|
|
|
return Heap::LookupSingleCharacterStringFromCode(kCharTable[value]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Slow case.
|
2008-07-03 15:10:15 +00:00
|
|
|
CONVERT_DOUBLE_CHECKED(value, args[0]);
|
|
|
|
if (isnan(value)) {
|
|
|
|
return Heap::AllocateStringFromAscii(CStrVector("NaN"));
|
|
|
|
}
|
|
|
|
if (isinf(value)) {
|
|
|
|
if (value < 0) {
|
|
|
|
return Heap::AllocateStringFromAscii(CStrVector("-Infinity"));
|
|
|
|
}
|
|
|
|
return Heap::AllocateStringFromAscii(CStrVector("Infinity"));
|
|
|
|
}
|
|
|
|
CONVERT_DOUBLE_CHECKED(radix_number, args[1]);
|
|
|
|
int radix = FastD2I(radix_number);
|
|
|
|
RUNTIME_ASSERT(2 <= radix && radix <= 36);
|
|
|
|
char* str = DoubleToRadixCString(value, radix);
|
|
|
|
Object* result = Heap::AllocateStringFromAscii(CStrVector(str));
|
|
|
|
DeleteArray(str);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static Object* Runtime_NumberToFixed(Arguments args) {
|
|
|
|
NoHandleAllocation ha;
|
|
|
|
ASSERT(args.length() == 2);
|
|
|
|
|
|
|
|
CONVERT_DOUBLE_CHECKED(value, args[0]);
|
|
|
|
if (isnan(value)) {
|
|
|
|
return Heap::AllocateStringFromAscii(CStrVector("NaN"));
|
|
|
|
}
|
|
|
|
if (isinf(value)) {
|
|
|
|
if (value < 0) {
|
|
|
|
return Heap::AllocateStringFromAscii(CStrVector("-Infinity"));
|
|
|
|
}
|
|
|
|
return Heap::AllocateStringFromAscii(CStrVector("Infinity"));
|
|
|
|
}
|
|
|
|
CONVERT_DOUBLE_CHECKED(f_number, args[1]);
|
|
|
|
int f = FastD2I(f_number);
|
|
|
|
RUNTIME_ASSERT(f >= 0);
|
|
|
|
char* str = DoubleToFixedCString(value, f);
|
|
|
|
Object* res = Heap::AllocateStringFromAscii(CStrVector(str));
|
|
|
|
DeleteArray(str);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static Object* Runtime_NumberToExponential(Arguments args) {
|
|
|
|
NoHandleAllocation ha;
|
|
|
|
ASSERT(args.length() == 2);
|
|
|
|
|
|
|
|
CONVERT_DOUBLE_CHECKED(value, args[0]);
|
|
|
|
if (isnan(value)) {
|
|
|
|
return Heap::AllocateStringFromAscii(CStrVector("NaN"));
|
|
|
|
}
|
|
|
|
if (isinf(value)) {
|
|
|
|
if (value < 0) {
|
|
|
|
return Heap::AllocateStringFromAscii(CStrVector("-Infinity"));
|
|
|
|
}
|
|
|
|
return Heap::AllocateStringFromAscii(CStrVector("Infinity"));
|
|
|
|
}
|
|
|
|
CONVERT_DOUBLE_CHECKED(f_number, args[1]);
|
|
|
|
int f = FastD2I(f_number);
|
|
|
|
RUNTIME_ASSERT(f >= -1 && f <= 20);
|
|
|
|
char* str = DoubleToExponentialCString(value, f);
|
|
|
|
Object* res = Heap::AllocateStringFromAscii(CStrVector(str));
|
|
|
|
DeleteArray(str);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static Object* Runtime_NumberToPrecision(Arguments args) {
|
|
|
|
NoHandleAllocation ha;
|
|
|
|
ASSERT(args.length() == 2);
|
|
|
|
|
|
|
|
CONVERT_DOUBLE_CHECKED(value, args[0]);
|
|
|
|
if (isnan(value)) {
|
|
|
|
return Heap::AllocateStringFromAscii(CStrVector("NaN"));
|
|
|
|
}
|
|
|
|
if (isinf(value)) {
|
|
|
|
if (value < 0) {
|
|
|
|
return Heap::AllocateStringFromAscii(CStrVector("-Infinity"));
|
|
|
|
}
|
|
|
|
return Heap::AllocateStringFromAscii(CStrVector("Infinity"));
|
|
|
|
}
|
|
|
|
CONVERT_DOUBLE_CHECKED(f_number, args[1]);
|
|
|
|
int f = FastD2I(f_number);
|
|
|
|
RUNTIME_ASSERT(f >= 1 && f <= 21);
|
|
|
|
char* str = DoubleToPrecisionCString(value, f);
|
|
|
|
Object* res = Heap::AllocateStringFromAscii(CStrVector(str));
|
|
|
|
DeleteArray(str);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Returns a single character string where first character equals
|
|
|
|
// string->Get(index).
|
2008-08-29 08:48:06 +00:00
|
|
|
static Handle<Object> GetCharAt(Handle<String> string, uint32_t index) {
|
2009-03-17 09:33:06 +00:00
|
|
|
if (index < static_cast<uint32_t>(string->length())) {
|
|
|
|
string->TryFlattenIfNotFlat();
|
2008-11-03 10:16:05 +00:00
|
|
|
return LookupSingleCharacterStringFromCode(
|
2009-03-17 09:33:06 +00:00
|
|
|
string->Get(index));
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
2008-08-29 08:48:06 +00:00
|
|
|
return Execution::CharAt(string, index);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Object* Runtime::GetElementOrCharAt(Handle<Object> object, uint32_t index) {
|
|
|
|
// Handle [] indexing on Strings
|
|
|
|
if (object->IsString()) {
|
2008-08-29 08:48:06 +00:00
|
|
|
Handle<Object> result = GetCharAt(Handle<String>::cast(object), index);
|
|
|
|
if (!result->IsUndefined()) return *result;
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Handle [] indexing on String objects
|
|
|
|
if (object->IsStringObjectWithCharacterAt(index)) {
|
2008-08-29 08:48:06 +00:00
|
|
|
Handle<JSValue> js_value = Handle<JSValue>::cast(object);
|
|
|
|
Handle<Object> result =
|
|
|
|
GetCharAt(Handle<String>(String::cast(js_value->value())), index);
|
|
|
|
if (!result->IsUndefined()) return *result;
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (object->IsString() || object->IsNumber() || object->IsBoolean()) {
|
2008-08-29 08:48:06 +00:00
|
|
|
Handle<Object> prototype = GetPrototype(object);
|
2008-07-03 15:10:15 +00:00
|
|
|
return prototype->GetElement(index);
|
|
|
|
}
|
|
|
|
|
|
|
|
return object->GetElement(index);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-08-28 09:55:41 +00:00
|
|
|
Object* Runtime::GetObjectProperty(Handle<Object> object, Handle<Object> key) {
|
|
|
|
HandleScope scope;
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
if (object->IsUndefined() || object->IsNull()) {
|
2008-08-28 09:55:41 +00:00
|
|
|
Handle<Object> args[2] = { key, object };
|
2008-07-03 15:10:15 +00:00
|
|
|
Handle<Object> error =
|
|
|
|
Factory::NewTypeError("non_object_property_load",
|
|
|
|
HandleVector(args, 2));
|
|
|
|
return Top::Throw(*error);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if the given key is an array index.
|
|
|
|
uint32_t index;
|
2008-08-28 09:55:41 +00:00
|
|
|
if (Array::IndexFromObject(*key, &index)) {
|
2008-07-03 15:10:15 +00:00
|
|
|
return GetElementOrCharAt(object, index);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Convert the key to a string - possibly by calling back into JavaScript.
|
2008-08-28 09:55:41 +00:00
|
|
|
Handle<String> name;
|
2008-07-03 15:10:15 +00:00
|
|
|
if (key->IsString()) {
|
2008-08-28 09:55:41 +00:00
|
|
|
name = Handle<String>::cast(key);
|
2008-07-03 15:10:15 +00:00
|
|
|
} else {
|
|
|
|
bool has_pending_exception = false;
|
|
|
|
Handle<Object> converted =
|
2008-08-28 09:55:41 +00:00
|
|
|
Execution::ToString(key, &has_pending_exception);
|
2008-07-03 15:10:15 +00:00
|
|
|
if (has_pending_exception) return Failure::Exception();
|
2008-08-28 09:55:41 +00:00
|
|
|
name = Handle<String>::cast(converted);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
2009-01-15 19:08:34 +00:00
|
|
|
// Check if the name is trivially convertible to an index and get
|
2008-07-03 15:10:15 +00:00
|
|
|
// the element if so.
|
|
|
|
if (name->AsArrayIndex(&index)) {
|
|
|
|
return GetElementOrCharAt(object, index);
|
|
|
|
} else {
|
|
|
|
PropertyAttributes attr;
|
2008-08-28 09:55:41 +00:00
|
|
|
return object->GetProperty(*name, &attr);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static Object* Runtime_GetProperty(Arguments args) {
|
|
|
|
NoHandleAllocation ha;
|
|
|
|
ASSERT(args.length() == 2);
|
|
|
|
|
|
|
|
Handle<Object> object = args.at<Object>(0);
|
2008-08-28 09:55:41 +00:00
|
|
|
Handle<Object> key = args.at<Object>(1);
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
return Runtime::GetObjectProperty(object, key);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-10-10 10:27:44 +00:00
|
|
|
|
2008-10-23 07:04:56 +00:00
|
|
|
// KeyedStringGetProperty is called from KeyedLoadIC::GenerateGeneric.
|
2008-10-10 10:27:44 +00:00
|
|
|
static Object* Runtime_KeyedGetProperty(Arguments args) {
|
|
|
|
NoHandleAllocation ha;
|
|
|
|
ASSERT(args.length() == 2);
|
|
|
|
|
2008-10-23 07:04:56 +00:00
|
|
|
// Fast cases for getting named properties of the receiver JSObject
|
2008-12-10 08:05:10 +00:00
|
|
|
// itself.
|
|
|
|
//
|
|
|
|
// The global proxy objects has to be excluded since LocalLookup on
|
2009-01-15 19:08:34 +00:00
|
|
|
// the global proxy object can return a valid result even though the
|
2008-12-10 08:05:10 +00:00
|
|
|
// global proxy object never has properties. This is the case
|
|
|
|
// because the global proxy object forwards everything to its hidden
|
|
|
|
// prototype including local lookups.
|
|
|
|
//
|
|
|
|
// Additionally, we need to make sure that we do not cache results
|
|
|
|
// for objects that require access checks.
|
2008-10-23 07:04:56 +00:00
|
|
|
if (args[0]->IsJSObject() &&
|
|
|
|
!args[0]->IsJSGlobalProxy() &&
|
2008-12-10 08:05:10 +00:00
|
|
|
!args[0]->IsAccessCheckNeeded() &&
|
2008-10-23 07:04:56 +00:00
|
|
|
args[1]->IsString()) {
|
|
|
|
JSObject* receiver = JSObject::cast(args[0]);
|
|
|
|
String* key = String::cast(args[1]);
|
|
|
|
if (receiver->HasFastProperties()) {
|
|
|
|
// Attempt to use lookup cache.
|
|
|
|
Map* receiver_map = receiver->map();
|
2009-06-17 06:07:49 +00:00
|
|
|
int offset = KeyedLookupCache::Lookup(receiver_map, key);
|
|
|
|
if (offset != -1) {
|
2008-10-23 07:04:56 +00:00
|
|
|
Object* value = receiver->FastPropertyAt(offset);
|
|
|
|
return value->IsTheHole() ? Heap::undefined_value() : value;
|
|
|
|
}
|
2009-07-08 11:51:43 +00:00
|
|
|
// Lookup cache miss. Perform lookup and update the cache if appropriate.
|
2008-10-23 07:04:56 +00:00
|
|
|
LookupResult result;
|
|
|
|
receiver->LocalLookup(key, &result);
|
|
|
|
if (result.IsProperty() && result.IsLoaded() && result.type() == FIELD) {
|
|
|
|
int offset = result.GetFieldIndex();
|
2009-06-17 06:07:49 +00:00
|
|
|
KeyedLookupCache::Update(receiver_map, key, offset);
|
2009-07-08 11:51:43 +00:00
|
|
|
return receiver->FastPropertyAt(offset);
|
2008-10-23 07:04:56 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Attempt dictionary lookup.
|
2009-07-02 06:50:43 +00:00
|
|
|
StringDictionary* dictionary = receiver->property_dictionary();
|
|
|
|
int entry = dictionary->FindEntry(key);
|
|
|
|
if ((entry != StringDictionary::kNotFound) &&
|
2008-10-23 07:04:56 +00:00
|
|
|
(dictionary->DetailsAt(entry).type() == NORMAL)) {
|
2009-06-30 10:05:36 +00:00
|
|
|
Object* value = dictionary->ValueAt(entry);
|
2009-07-08 11:51:43 +00:00
|
|
|
if (!receiver->IsGlobalObject()) return value;
|
|
|
|
value = JSGlobalPropertyCell::cast(value)->value();
|
|
|
|
if (!value->IsTheHole()) return value;
|
|
|
|
// If value is the hole do the general lookup.
|
2008-10-23 07:04:56 +00:00
|
|
|
}
|
2008-10-10 10:27:44 +00:00
|
|
|
}
|
|
|
|
}
|
2008-10-23 07:04:56 +00:00
|
|
|
|
|
|
|
// Fall back to GetObjectProperty.
|
2008-10-10 10:27:44 +00:00
|
|
|
return Runtime::GetObjectProperty(args.at<Object>(0),
|
|
|
|
args.at<Object>(1));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
Object* Runtime::SetObjectProperty(Handle<Object> object,
|
|
|
|
Handle<Object> key,
|
|
|
|
Handle<Object> value,
|
|
|
|
PropertyAttributes attr) {
|
2008-08-28 09:55:41 +00:00
|
|
|
HandleScope scope;
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
if (object->IsUndefined() || object->IsNull()) {
|
2008-08-28 09:55:41 +00:00
|
|
|
Handle<Object> args[2] = { key, object };
|
2008-07-03 15:10:15 +00:00
|
|
|
Handle<Object> error =
|
|
|
|
Factory::NewTypeError("non_object_property_store",
|
|
|
|
HandleVector(args, 2));
|
|
|
|
return Top::Throw(*error);
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the object isn't a JavaScript object, we ignore the store.
|
|
|
|
if (!object->IsJSObject()) return *value;
|
|
|
|
|
2008-08-28 09:55:41 +00:00
|
|
|
Handle<JSObject> js_object = Handle<JSObject>::cast(object);
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
// Check if the given key is an array index.
|
|
|
|
uint32_t index;
|
|
|
|
if (Array::IndexFromObject(*key, &index)) {
|
|
|
|
ASSERT(attr == NONE);
|
|
|
|
|
|
|
|
// In Firefox/SpiderMonkey, Safari and Opera you can access the characters
|
|
|
|
// of a string using [] notation. We need to support this too in
|
|
|
|
// JavaScript.
|
|
|
|
// In the case of a String object we just need to redirect the assignment to
|
|
|
|
// the underlying string if the index is in range. Since the underlying
|
|
|
|
// string does nothing with the assignment then we can ignore such
|
|
|
|
// assignments.
|
2008-08-28 09:55:41 +00:00
|
|
|
if (js_object->IsStringObjectWithCharacterAt(index)) {
|
2008-07-03 15:10:15 +00:00
|
|
|
return *value;
|
2008-08-28 09:55:41 +00:00
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2008-08-28 09:55:41 +00:00
|
|
|
Handle<Object> result = SetElement(js_object, index, value);
|
|
|
|
if (result.is_null()) return Failure::Exception();
|
2008-07-03 15:10:15 +00:00
|
|
|
return *value;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (key->IsString()) {
|
2008-08-28 09:55:41 +00:00
|
|
|
Handle<Object> result;
|
|
|
|
if (Handle<String>::cast(key)->AsArrayIndex(&index)) {
|
2008-07-03 15:10:15 +00:00
|
|
|
ASSERT(attr == NONE);
|
2008-08-28 09:55:41 +00:00
|
|
|
result = SetElement(js_object, index, value);
|
2008-07-03 15:10:15 +00:00
|
|
|
} else {
|
2008-08-28 09:55:41 +00:00
|
|
|
Handle<String> key_string = Handle<String>::cast(key);
|
2009-03-17 09:33:06 +00:00
|
|
|
key_string->TryFlattenIfNotFlat();
|
2008-08-28 09:55:41 +00:00
|
|
|
result = SetProperty(js_object, key_string, value, attr);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
2008-08-28 09:55:41 +00:00
|
|
|
if (result.is_null()) return Failure::Exception();
|
2008-07-03 15:10:15 +00:00
|
|
|
return *value;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Call-back into JavaScript to convert the key to a string.
|
|
|
|
bool has_pending_exception = false;
|
|
|
|
Handle<Object> converted = Execution::ToString(key, &has_pending_exception);
|
|
|
|
if (has_pending_exception) return Failure::Exception();
|
|
|
|
Handle<String> name = Handle<String>::cast(converted);
|
|
|
|
|
|
|
|
if (name->AsArrayIndex(&index)) {
|
|
|
|
ASSERT(attr == NONE);
|
2008-08-28 09:55:41 +00:00
|
|
|
return js_object->SetElement(index, *value);
|
2008-07-03 15:10:15 +00:00
|
|
|
} else {
|
2008-08-28 09:55:41 +00:00
|
|
|
return js_object->SetProperty(*name, *value, attr);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-04-21 13:28:11 +00:00
|
|
|
Object* Runtime::ForceSetObjectProperty(Handle<JSObject> js_object,
|
|
|
|
Handle<Object> key,
|
|
|
|
Handle<Object> value,
|
|
|
|
PropertyAttributes attr) {
|
|
|
|
HandleScope scope;
|
|
|
|
|
|
|
|
// Check if the given key is an array index.
|
|
|
|
uint32_t index;
|
|
|
|
if (Array::IndexFromObject(*key, &index)) {
|
|
|
|
ASSERT(attr == NONE);
|
|
|
|
|
|
|
|
// In Firefox/SpiderMonkey, Safari and Opera you can access the characters
|
|
|
|
// of a string using [] notation. We need to support this too in
|
|
|
|
// JavaScript.
|
|
|
|
// In the case of a String object we just need to redirect the assignment to
|
|
|
|
// the underlying string if the index is in range. Since the underlying
|
|
|
|
// string does nothing with the assignment then we can ignore such
|
|
|
|
// assignments.
|
|
|
|
if (js_object->IsStringObjectWithCharacterAt(index)) {
|
|
|
|
return *value;
|
|
|
|
}
|
|
|
|
|
|
|
|
return js_object->SetElement(index, *value);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (key->IsString()) {
|
|
|
|
if (Handle<String>::cast(key)->AsArrayIndex(&index)) {
|
|
|
|
ASSERT(attr == NONE);
|
|
|
|
return js_object->SetElement(index, *value);
|
|
|
|
} else {
|
|
|
|
Handle<String> key_string = Handle<String>::cast(key);
|
|
|
|
key_string->TryFlattenIfNotFlat();
|
|
|
|
return js_object->IgnoreAttributesAndSetLocalProperty(*key_string,
|
|
|
|
*value,
|
|
|
|
attr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Call-back into JavaScript to convert the key to a string.
|
|
|
|
bool has_pending_exception = false;
|
|
|
|
Handle<Object> converted = Execution::ToString(key, &has_pending_exception);
|
|
|
|
if (has_pending_exception) return Failure::Exception();
|
|
|
|
Handle<String> name = Handle<String>::cast(converted);
|
|
|
|
|
|
|
|
if (name->AsArrayIndex(&index)) {
|
|
|
|
ASSERT(attr == NONE);
|
|
|
|
return js_object->SetElement(index, *value);
|
|
|
|
} else {
|
|
|
|
return js_object->IgnoreAttributesAndSetLocalProperty(*name, *value, attr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-06-08 09:46:09 +00:00
|
|
|
Object* Runtime::ForceDeleteObjectProperty(Handle<JSObject> js_object,
|
|
|
|
Handle<Object> key) {
|
|
|
|
HandleScope scope;
|
|
|
|
|
|
|
|
// Check if the given key is an array index.
|
|
|
|
uint32_t index;
|
|
|
|
if (Array::IndexFromObject(*key, &index)) {
|
|
|
|
// In Firefox/SpiderMonkey, Safari and Opera you can access the
|
|
|
|
// characters of a string using [] notation. In the case of a
|
|
|
|
// String object we just need to redirect the deletion to the
|
|
|
|
// underlying string if the index is in range. Since the
|
|
|
|
// underlying string does nothing with the deletion, we can ignore
|
|
|
|
// such deletions.
|
|
|
|
if (js_object->IsStringObjectWithCharacterAt(index)) {
|
|
|
|
return Heap::true_value();
|
|
|
|
}
|
|
|
|
|
|
|
|
return js_object->DeleteElement(index, JSObject::FORCE_DELETION);
|
|
|
|
}
|
|
|
|
|
|
|
|
Handle<String> key_string;
|
|
|
|
if (key->IsString()) {
|
|
|
|
key_string = Handle<String>::cast(key);
|
|
|
|
} else {
|
|
|
|
// Call-back into JavaScript to convert the key to a string.
|
|
|
|
bool has_pending_exception = false;
|
|
|
|
Handle<Object> converted = Execution::ToString(key, &has_pending_exception);
|
|
|
|
if (has_pending_exception) return Failure::Exception();
|
|
|
|
key_string = Handle<String>::cast(converted);
|
|
|
|
}
|
|
|
|
|
|
|
|
key_string->TryFlattenIfNotFlat();
|
|
|
|
return js_object->DeleteProperty(*key_string, JSObject::FORCE_DELETION);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
static Object* Runtime_SetProperty(Arguments args) {
|
|
|
|
NoHandleAllocation ha;
|
|
|
|
RUNTIME_ASSERT(args.length() == 3 || args.length() == 4);
|
|
|
|
|
|
|
|
Handle<Object> object = args.at<Object>(0);
|
|
|
|
Handle<Object> key = args.at<Object>(1);
|
|
|
|
Handle<Object> value = args.at<Object>(2);
|
|
|
|
|
|
|
|
// Compute attributes.
|
|
|
|
PropertyAttributes attributes = NONE;
|
|
|
|
if (args.length() == 4) {
|
|
|
|
CONVERT_CHECKED(Smi, value_obj, args[3]);
|
2008-10-03 12:14:29 +00:00
|
|
|
int unchecked_value = value_obj->value();
|
2008-07-03 15:10:15 +00:00
|
|
|
// Only attribute bits should be set.
|
2008-10-03 12:14:29 +00:00
|
|
|
RUNTIME_ASSERT(
|
|
|
|
(unchecked_value & ~(READ_ONLY | DONT_ENUM | DONT_DELETE)) == 0);
|
|
|
|
attributes = static_cast<PropertyAttributes>(unchecked_value);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
return Runtime::SetObjectProperty(object, key, value, attributes);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Set a local property, even if it is READ_ONLY. If the property does not
|
|
|
|
// exist, it will be added with attributes NONE.
|
|
|
|
static Object* Runtime_IgnoreAttributesAndSetProperty(Arguments args) {
|
|
|
|
NoHandleAllocation ha;
|
2008-10-03 12:14:29 +00:00
|
|
|
RUNTIME_ASSERT(args.length() == 3 || args.length() == 4);
|
2008-07-03 15:10:15 +00:00
|
|
|
CONVERT_CHECKED(JSObject, object, args[0]);
|
|
|
|
CONVERT_CHECKED(String, name, args[1]);
|
2008-10-03 12:14:29 +00:00
|
|
|
// Compute attributes.
|
|
|
|
PropertyAttributes attributes = NONE;
|
|
|
|
if (args.length() == 4) {
|
|
|
|
CONVERT_CHECKED(Smi, value_obj, args[3]);
|
|
|
|
int unchecked_value = value_obj->value();
|
|
|
|
// Only attribute bits should be set.
|
|
|
|
RUNTIME_ASSERT(
|
|
|
|
(unchecked_value & ~(READ_ONLY | DONT_ENUM | DONT_DELETE)) == 0);
|
|
|
|
attributes = static_cast<PropertyAttributes>(unchecked_value);
|
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2008-10-03 12:14:29 +00:00
|
|
|
return object->
|
|
|
|
IgnoreAttributesAndSetLocalProperty(name, args[2], attributes);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static Object* Runtime_DeleteProperty(Arguments args) {
|
|
|
|
NoHandleAllocation ha;
|
|
|
|
ASSERT(args.length() == 2);
|
|
|
|
|
|
|
|
CONVERT_CHECKED(JSObject, object, args[0]);
|
|
|
|
CONVERT_CHECKED(String, key, args[1]);
|
2009-06-08 09:46:09 +00:00
|
|
|
return object->DeleteProperty(key, JSObject::NORMAL_DELETION);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-05-08 12:39:37 +00:00
|
|
|
static Object* HasLocalPropertyImplementation(Handle<JSObject> object,
|
|
|
|
Handle<String> key) {
|
|
|
|
if (object->HasLocalProperty(*key)) return Heap::true_value();
|
|
|
|
// Handle hidden prototypes. If there's a hidden prototype above this thing
|
|
|
|
// then we have to check it for properties, because they are supposed to
|
|
|
|
// look like they are on this object.
|
|
|
|
Handle<Object> proto(object->GetPrototype());
|
|
|
|
if (proto->IsJSObject() &&
|
|
|
|
Handle<JSObject>::cast(proto)->map()->is_hidden_prototype()) {
|
|
|
|
return HasLocalPropertyImplementation(Handle<JSObject>::cast(proto), key);
|
|
|
|
}
|
|
|
|
return Heap::false_value();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static Object* Runtime_HasLocalProperty(Arguments args) {
|
|
|
|
NoHandleAllocation ha;
|
|
|
|
ASSERT(args.length() == 2);
|
|
|
|
CONVERT_CHECKED(String, key, args[1]);
|
|
|
|
|
|
|
|
Object* obj = args[0];
|
2008-07-03 15:10:15 +00:00
|
|
|
// Only JS objects can have properties.
|
2009-05-06 12:54:57 +00:00
|
|
|
if (obj->IsJSObject()) {
|
|
|
|
JSObject* object = JSObject::cast(obj);
|
2009-05-08 12:39:37 +00:00
|
|
|
// Fast case - no interceptors.
|
|
|
|
if (object->HasRealNamedProperty(key)) return Heap::true_value();
|
|
|
|
// Slow case. Either it's not there or we have an interceptor. We should
|
|
|
|
// have handles for this kind of deal.
|
|
|
|
HandleScope scope;
|
|
|
|
return HasLocalPropertyImplementation(Handle<JSObject>(object),
|
|
|
|
Handle<String>(key));
|
2009-05-06 12:54:57 +00:00
|
|
|
} else if (obj->IsString()) {
|
2008-07-03 15:10:15 +00:00
|
|
|
// Well, there is one exception: Handle [] on strings.
|
|
|
|
uint32_t index;
|
|
|
|
if (key->AsArrayIndex(&index)) {
|
2009-05-06 12:54:57 +00:00
|
|
|
String* string = String::cast(obj);
|
2008-11-05 10:26:08 +00:00
|
|
|
if (index < static_cast<uint32_t>(string->length()))
|
2008-07-03 15:10:15 +00:00
|
|
|
return Heap::true_value();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Heap::false_value();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static Object* Runtime_HasProperty(Arguments args) {
|
|
|
|
NoHandleAllocation na;
|
|
|
|
ASSERT(args.length() == 2);
|
|
|
|
|
|
|
|
// Only JS objects can have properties.
|
|
|
|
if (args[0]->IsJSObject()) {
|
|
|
|
JSObject* object = JSObject::cast(args[0]);
|
|
|
|
CONVERT_CHECKED(String, key, args[1]);
|
|
|
|
if (object->HasProperty(key)) return Heap::true_value();
|
|
|
|
}
|
|
|
|
return Heap::false_value();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static Object* Runtime_HasElement(Arguments args) {
|
|
|
|
NoHandleAllocation na;
|
|
|
|
ASSERT(args.length() == 2);
|
|
|
|
|
|
|
|
// Only JS objects can have elements.
|
|
|
|
if (args[0]->IsJSObject()) {
|
|
|
|
JSObject* object = JSObject::cast(args[0]);
|
|
|
|
CONVERT_CHECKED(Smi, index_obj, args[1]);
|
|
|
|
uint32_t index = index_obj->value();
|
|
|
|
if (object->HasElement(index)) return Heap::true_value();
|
|
|
|
}
|
|
|
|
return Heap::false_value();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static Object* Runtime_IsPropertyEnumerable(Arguments args) {
|
|
|
|
NoHandleAllocation ha;
|
|
|
|
ASSERT(args.length() == 2);
|
|
|
|
|
|
|
|
CONVERT_CHECKED(JSObject, object, args[0]);
|
|
|
|
CONVERT_CHECKED(String, key, args[1]);
|
|
|
|
|
|
|
|
uint32_t index;
|
|
|
|
if (key->AsArrayIndex(&index)) {
|
|
|
|
return Heap::ToBoolean(object->HasElement(index));
|
|
|
|
}
|
|
|
|
|
2008-10-31 09:42:14 +00:00
|
|
|
PropertyAttributes att = object->GetLocalPropertyAttribute(key);
|
2008-10-31 11:53:29 +00:00
|
|
|
return Heap::ToBoolean(att != ABSENT && (att & DONT_ENUM) == 0);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static Object* Runtime_GetPropertyNames(Arguments args) {
|
|
|
|
HandleScope scope;
|
|
|
|
ASSERT(args.length() == 1);
|
2009-06-17 11:44:25 +00:00
|
|
|
CONVERT_ARG_CHECKED(JSObject, object, 0);
|
2008-07-03 15:10:15 +00:00
|
|
|
return *GetKeysFor(object);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Returns either a FixedArray as Runtime_GetPropertyNames,
|
|
|
|
// or, if the given object has an enum cache that contains
|
|
|
|
// all enumerable properties of the object and its prototypes
|
|
|
|
// have none, the map of the object. This is used to speed up
|
|
|
|
// the check for deletions during a for-in.
|
|
|
|
static Object* Runtime_GetPropertyNamesFast(Arguments args) {
|
|
|
|
ASSERT(args.length() == 1);
|
|
|
|
|
|
|
|
CONVERT_CHECKED(JSObject, raw_object, args[0]);
|
|
|
|
|
|
|
|
if (raw_object->IsSimpleEnum()) return raw_object->map();
|
|
|
|
|
|
|
|
HandleScope scope;
|
|
|
|
Handle<JSObject> object(raw_object);
|
2009-09-15 11:51:40 +00:00
|
|
|
Handle<FixedArray> content = GetKeysInFixedArrayFor(object,
|
|
|
|
INCLUDE_PROTOS);
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
// Test again, since cache may have been built by preceding call.
|
|
|
|
if (object->IsSimpleEnum()) return object->map();
|
|
|
|
|
|
|
|
return *content;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-09-15 11:51:40 +00:00
|
|
|
static Object* Runtime_LocalKeys(Arguments args) {
|
|
|
|
ASSERT_EQ(args.length(), 1);
|
|
|
|
CONVERT_CHECKED(JSObject, raw_object, args[0]);
|
|
|
|
HandleScope scope;
|
|
|
|
Handle<JSObject> object(raw_object);
|
|
|
|
Handle<FixedArray> contents = GetKeysInFixedArrayFor(object,
|
|
|
|
LOCAL_ONLY);
|
|
|
|
// Some fast paths through GetKeysInFixedArrayFor reuse a cached
|
|
|
|
// property array and since the result is mutable we have to create
|
|
|
|
// a fresh clone on each invocation.
|
|
|
|
Handle<FixedArray> copy = Factory::NewFixedArray(contents->length());
|
|
|
|
contents->CopyTo(0, *copy, 0, contents->length());
|
|
|
|
return *Factory::NewJSArrayWithElements(copy);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
static Object* Runtime_GetArgumentsProperty(Arguments args) {
|
|
|
|
NoHandleAllocation ha;
|
|
|
|
ASSERT(args.length() == 1);
|
|
|
|
|
|
|
|
// Compute the frame holding the arguments.
|
|
|
|
JavaScriptFrameIterator it;
|
|
|
|
it.AdvanceToArgumentsFrame();
|
|
|
|
JavaScriptFrame* frame = it.frame();
|
|
|
|
|
|
|
|
// Get the actual number of provided arguments.
|
|
|
|
const uint32_t n = frame->GetProvidedParametersCount();
|
|
|
|
|
|
|
|
// Try to convert the key to an index. If successful and within
|
|
|
|
// index return the the argument from the frame.
|
|
|
|
uint32_t index;
|
|
|
|
if (Array::IndexFromObject(args[0], &index) && index < n) {
|
|
|
|
return frame->GetParameter(index);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Convert the key to a string.
|
|
|
|
HandleScope scope;
|
|
|
|
bool exception = false;
|
|
|
|
Handle<Object> converted =
|
|
|
|
Execution::ToString(args.at<Object>(0), &exception);
|
|
|
|
if (exception) return Failure::Exception();
|
|
|
|
Handle<String> key = Handle<String>::cast(converted);
|
|
|
|
|
|
|
|
// Try to convert the string key into an array index.
|
|
|
|
if (key->AsArrayIndex(&index)) {
|
|
|
|
if (index < n) {
|
|
|
|
return frame->GetParameter(index);
|
|
|
|
} else {
|
|
|
|
return Top::initial_object_prototype()->GetElement(index);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle special arguments properties.
|
|
|
|
if (key->Equals(Heap::length_symbol())) return Smi::FromInt(n);
|
|
|
|
if (key->Equals(Heap::callee_symbol())) return frame->function();
|
|
|
|
|
|
|
|
// Lookup in the initial Object.prototype object.
|
|
|
|
return Top::initial_object_prototype()->GetProperty(*key);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-02-26 07:54:22 +00:00
|
|
|
static Object* Runtime_ToFastProperties(Arguments args) {
|
|
|
|
ASSERT(args.length() == 1);
|
2009-03-04 11:57:24 +00:00
|
|
|
Handle<Object> object = args.at<Object>(0);
|
|
|
|
if (object->IsJSObject()) {
|
|
|
|
Handle<JSObject> js_object = Handle<JSObject>::cast(object);
|
|
|
|
js_object->TransformToFastProperties(0);
|
|
|
|
}
|
2009-02-26 07:54:22 +00:00
|
|
|
return *object;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static Object* Runtime_ToSlowProperties(Arguments args) {
|
|
|
|
ASSERT(args.length() == 1);
|
2009-03-04 11:57:24 +00:00
|
|
|
Handle<Object> object = args.at<Object>(0);
|
|
|
|
if (object->IsJSObject()) {
|
|
|
|
Handle<JSObject> js_object = Handle<JSObject>::cast(object);
|
2009-07-30 09:13:48 +00:00
|
|
|
js_object->NormalizeProperties(CLEAR_INOBJECT_PROPERTIES, 0);
|
2009-03-04 11:57:24 +00:00
|
|
|
}
|
2009-02-26 07:54:22 +00:00
|
|
|
return *object;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
static Object* Runtime_ToBool(Arguments args) {
|
|
|
|
NoHandleAllocation ha;
|
|
|
|
ASSERT(args.length() == 1);
|
|
|
|
|
|
|
|
return args[0]->ToBoolean();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Returns the type string of a value; see ECMA-262, 11.4.3 (p 47).
|
|
|
|
// Possible optimizations: put the type string into the oddballs.
|
|
|
|
static Object* Runtime_Typeof(Arguments args) {
|
|
|
|
NoHandleAllocation ha;
|
|
|
|
|
|
|
|
Object* obj = args[0];
|
|
|
|
if (obj->IsNumber()) return Heap::number_symbol();
|
|
|
|
HeapObject* heap_obj = HeapObject::cast(obj);
|
|
|
|
|
|
|
|
// typeof an undetectable object is 'undefined'
|
2008-08-27 10:11:39 +00:00
|
|
|
if (heap_obj->map()->is_undetectable()) return Heap::undefined_symbol();
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
InstanceType instance_type = heap_obj->map()->instance_type();
|
|
|
|
if (instance_type < FIRST_NONSTRING_TYPE) {
|
|
|
|
return Heap::string_symbol();
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (instance_type) {
|
|
|
|
case ODDBALL_TYPE:
|
|
|
|
if (heap_obj->IsTrue() || heap_obj->IsFalse()) {
|
|
|
|
return Heap::boolean_symbol();
|
|
|
|
}
|
|
|
|
if (heap_obj->IsNull()) {
|
|
|
|
return Heap::object_symbol();
|
|
|
|
}
|
|
|
|
ASSERT(heap_obj->IsUndefined());
|
|
|
|
return Heap::undefined_symbol();
|
2009-08-17 10:19:00 +00:00
|
|
|
case JS_FUNCTION_TYPE: case JS_REGEXP_TYPE:
|
2008-07-03 15:10:15 +00:00
|
|
|
return Heap::function_symbol();
|
|
|
|
default:
|
|
|
|
// For any kind of object not handled above, the spec rule for
|
|
|
|
// host objects gives that it is okay to return "object"
|
|
|
|
return Heap::object_symbol();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static Object* Runtime_StringToNumber(Arguments args) {
|
|
|
|
NoHandleAllocation ha;
|
|
|
|
ASSERT(args.length() == 1);
|
|
|
|
CONVERT_CHECKED(String, subject, args[0]);
|
2009-03-17 09:33:06 +00:00
|
|
|
subject->TryFlattenIfNotFlat();
|
2008-07-03 15:10:15 +00:00
|
|
|
return Heap::NumberFromDouble(StringToDouble(subject, ALLOW_HEX));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static Object* Runtime_StringFromCharCodeArray(Arguments args) {
|
|
|
|
NoHandleAllocation ha;
|
|
|
|
ASSERT(args.length() == 1);
|
|
|
|
|
|
|
|
CONVERT_CHECKED(JSArray, codes, args[0]);
|
|
|
|
int length = Smi::cast(codes->length())->value();
|
|
|
|
|
|
|
|
// Check if the string can be ASCII.
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < length; i++) {
|
|
|
|
Object* element = codes->GetElement(i);
|
|
|
|
CONVERT_NUMBER_CHECKED(int, chr, Int32, element);
|
|
|
|
if ((chr & 0xffff) > String::kMaxAsciiCharCode)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
Object* object = NULL;
|
|
|
|
if (i == length) { // The string is ASCII.
|
|
|
|
object = Heap::AllocateRawAsciiString(length);
|
|
|
|
} else { // The string is not ASCII.
|
|
|
|
object = Heap::AllocateRawTwoByteString(length);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (object->IsFailure()) return object;
|
|
|
|
String* result = String::cast(object);
|
|
|
|
for (int i = 0; i < length; i++) {
|
|
|
|
Object* element = codes->GetElement(i);
|
|
|
|
CONVERT_NUMBER_CHECKED(int, chr, Int32, element);
|
2009-03-17 09:33:06 +00:00
|
|
|
result->Set(i, chr & 0xffff);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// kNotEscaped is generated by the following:
|
|
|
|
//
|
|
|
|
// #!/bin/perl
|
|
|
|
// for (my $i = 0; $i < 256; $i++) {
|
|
|
|
// print "\n" if $i % 16 == 0;
|
|
|
|
// my $c = chr($i);
|
|
|
|
// my $escaped = 1;
|
|
|
|
// $escaped = 0 if $c =~ m#[A-Za-z0-9@*_+./-]#;
|
|
|
|
// print $escaped ? "0, " : "1, ";
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
static bool IsNotEscaped(uint16_t character) {
|
|
|
|
// Only for 8 bit characters, the rest are always escaped (in a different way)
|
|
|
|
ASSERT(character < 256);
|
|
|
|
static const char kNotEscaped[256] = {
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 1,
|
|
|
|
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0,
|
|
|
|
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
|
|
|
|
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1,
|
|
|
|
0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
|
|
|
|
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0,
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
};
|
|
|
|
return kNotEscaped[character] != 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static Object* Runtime_URIEscape(Arguments args) {
|
|
|
|
const char hex_chars[] = "0123456789ABCDEF";
|
|
|
|
NoHandleAllocation ha;
|
|
|
|
ASSERT(args.length() == 1);
|
|
|
|
CONVERT_CHECKED(String, source, args[0]);
|
|
|
|
|
2009-03-17 09:33:06 +00:00
|
|
|
source->TryFlattenIfNotFlat();
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
int escaped_length = 0;
|
|
|
|
int length = source->length();
|
|
|
|
{
|
2009-03-02 09:09:07 +00:00
|
|
|
Access<StringInputBuffer> buffer(&runtime_string_input_buffer);
|
2008-07-03 15:10:15 +00:00
|
|
|
buffer->Reset(source);
|
|
|
|
while (buffer->has_more()) {
|
|
|
|
uint16_t character = buffer->GetNext();
|
|
|
|
if (character >= 256) {
|
|
|
|
escaped_length += 6;
|
|
|
|
} else if (IsNotEscaped(character)) {
|
|
|
|
escaped_length++;
|
|
|
|
} else {
|
|
|
|
escaped_length += 3;
|
|
|
|
}
|
|
|
|
// We don't allow strings that are longer than Smi range.
|
|
|
|
if (!Smi::IsValid(escaped_length)) {
|
|
|
|
Top::context()->mark_out_of_memory();
|
|
|
|
return Failure::OutOfMemoryException();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// No length change implies no change. Return original string if no change.
|
|
|
|
if (escaped_length == length) {
|
|
|
|
return source;
|
|
|
|
}
|
|
|
|
Object* o = Heap::AllocateRawAsciiString(escaped_length);
|
|
|
|
if (o->IsFailure()) return o;
|
|
|
|
String* destination = String::cast(o);
|
|
|
|
int dest_position = 0;
|
|
|
|
|
2009-03-02 09:09:07 +00:00
|
|
|
Access<StringInputBuffer> buffer(&runtime_string_input_buffer);
|
2008-07-03 15:10:15 +00:00
|
|
|
buffer->Rewind();
|
|
|
|
while (buffer->has_more()) {
|
2008-11-03 10:16:05 +00:00
|
|
|
uint16_t chr = buffer->GetNext();
|
|
|
|
if (chr >= 256) {
|
2009-03-17 09:33:06 +00:00
|
|
|
destination->Set(dest_position, '%');
|
|
|
|
destination->Set(dest_position+1, 'u');
|
|
|
|
destination->Set(dest_position+2, hex_chars[chr >> 12]);
|
|
|
|
destination->Set(dest_position+3, hex_chars[(chr >> 8) & 0xf]);
|
|
|
|
destination->Set(dest_position+4, hex_chars[(chr >> 4) & 0xf]);
|
|
|
|
destination->Set(dest_position+5, hex_chars[chr & 0xf]);
|
2008-07-03 15:10:15 +00:00
|
|
|
dest_position += 6;
|
2008-11-03 10:16:05 +00:00
|
|
|
} else if (IsNotEscaped(chr)) {
|
2009-03-17 09:33:06 +00:00
|
|
|
destination->Set(dest_position, chr);
|
2008-07-03 15:10:15 +00:00
|
|
|
dest_position++;
|
|
|
|
} else {
|
2009-03-17 09:33:06 +00:00
|
|
|
destination->Set(dest_position, '%');
|
|
|
|
destination->Set(dest_position+1, hex_chars[chr >> 4]);
|
|
|
|
destination->Set(dest_position+2, hex_chars[chr & 0xf]);
|
2008-07-03 15:10:15 +00:00
|
|
|
dest_position += 3;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return destination;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static inline int TwoDigitHex(uint16_t character1, uint16_t character2) {
|
|
|
|
static const signed char kHexValue['g'] = {
|
|
|
|
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
|
|
|
|
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
|
|
|
|
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
|
|
|
|
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1,
|
|
|
|
-1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1,
|
|
|
|
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
|
|
|
|
-1, 10, 11, 12, 13, 14, 15 };
|
|
|
|
|
|
|
|
if (character1 > 'f') return -1;
|
|
|
|
int hi = kHexValue[character1];
|
|
|
|
if (hi == -1) return -1;
|
|
|
|
if (character2 > 'f') return -1;
|
|
|
|
int lo = kHexValue[character2];
|
|
|
|
if (lo == -1) return -1;
|
|
|
|
return (hi << 4) + lo;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-11-03 10:16:05 +00:00
|
|
|
static inline int Unescape(String* source,
|
|
|
|
int i,
|
|
|
|
int length,
|
|
|
|
int* step) {
|
2009-03-17 09:33:06 +00:00
|
|
|
uint16_t character = source->Get(i);
|
2008-11-03 12:08:01 +00:00
|
|
|
int32_t hi = 0;
|
|
|
|
int32_t lo = 0;
|
2008-07-03 15:10:15 +00:00
|
|
|
if (character == '%' &&
|
|
|
|
i <= length - 6 &&
|
2009-03-17 09:33:06 +00:00
|
|
|
source->Get(i + 1) == 'u' &&
|
|
|
|
(hi = TwoDigitHex(source->Get(i + 2),
|
|
|
|
source->Get(i + 3))) != -1 &&
|
|
|
|
(lo = TwoDigitHex(source->Get(i + 4),
|
|
|
|
source->Get(i + 5))) != -1) {
|
2008-07-03 15:10:15 +00:00
|
|
|
*step = 6;
|
|
|
|
return (hi << 8) + lo;
|
|
|
|
} else if (character == '%' &&
|
|
|
|
i <= length - 3 &&
|
2009-03-17 09:33:06 +00:00
|
|
|
(lo = TwoDigitHex(source->Get(i + 1),
|
|
|
|
source->Get(i + 2))) != -1) {
|
2008-07-03 15:10:15 +00:00
|
|
|
*step = 3;
|
|
|
|
return lo;
|
|
|
|
} else {
|
|
|
|
*step = 1;
|
|
|
|
return character;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static Object* Runtime_URIUnescape(Arguments args) {
|
|
|
|
NoHandleAllocation ha;
|
|
|
|
ASSERT(args.length() == 1);
|
|
|
|
CONVERT_CHECKED(String, source, args[0]);
|
|
|
|
|
2009-03-17 09:33:06 +00:00
|
|
|
source->TryFlattenIfNotFlat();
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
bool ascii = true;
|
2009-03-17 09:33:06 +00:00
|
|
|
int length = source->length();
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
int unescaped_length = 0;
|
|
|
|
for (int i = 0; i < length; unescaped_length++) {
|
|
|
|
int step;
|
2009-03-17 09:33:06 +00:00
|
|
|
if (Unescape(source, i, length, &step) > String::kMaxAsciiCharCode) {
|
2008-07-03 15:10:15 +00:00
|
|
|
ascii = false;
|
2009-03-17 09:33:06 +00:00
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
i += step;
|
|
|
|
}
|
|
|
|
|
|
|
|
// No length change implies no change. Return original string if no change.
|
|
|
|
if (unescaped_length == length)
|
|
|
|
return source;
|
|
|
|
|
|
|
|
Object* o = ascii ?
|
|
|
|
Heap::AllocateRawAsciiString(unescaped_length) :
|
|
|
|
Heap::AllocateRawTwoByteString(unescaped_length);
|
|
|
|
if (o->IsFailure()) return o;
|
|
|
|
String* destination = String::cast(o);
|
|
|
|
|
|
|
|
int dest_position = 0;
|
|
|
|
for (int i = 0; i < length; dest_position++) {
|
|
|
|
int step;
|
2009-03-17 09:33:06 +00:00
|
|
|
destination->Set(dest_position, Unescape(source, i, length, &step));
|
2008-07-03 15:10:15 +00:00
|
|
|
i += step;
|
|
|
|
}
|
|
|
|
return destination;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static Object* Runtime_StringParseInt(Arguments args) {
|
|
|
|
NoHandleAllocation ha;
|
|
|
|
|
|
|
|
CONVERT_CHECKED(String, s, args[0]);
|
|
|
|
CONVERT_DOUBLE_CHECKED(n, args[1]);
|
|
|
|
int radix = FastD2I(n);
|
|
|
|
|
2009-03-17 09:33:06 +00:00
|
|
|
s->TryFlattenIfNotFlat();
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2009-03-17 09:33:06 +00:00
|
|
|
int len = s->length();
|
2008-07-03 15:10:15 +00:00
|
|
|
int i;
|
|
|
|
|
|
|
|
// Skip leading white space.
|
2009-03-17 09:33:06 +00:00
|
|
|
for (i = 0; i < len && Scanner::kIsWhiteSpace.get(s->Get(i)); i++) ;
|
2008-07-03 15:10:15 +00:00
|
|
|
if (i == len) return Heap::nan_value();
|
|
|
|
|
|
|
|
// Compute the sign (default to +).
|
|
|
|
int sign = 1;
|
2009-03-17 09:33:06 +00:00
|
|
|
if (s->Get(i) == '-') {
|
2008-07-03 15:10:15 +00:00
|
|
|
sign = -1;
|
|
|
|
i++;
|
2009-03-17 09:33:06 +00:00
|
|
|
} else if (s->Get(i) == '+') {
|
2008-07-03 15:10:15 +00:00
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Compute the radix if 0.
|
|
|
|
if (radix == 0) {
|
|
|
|
radix = 10;
|
2009-03-17 09:33:06 +00:00
|
|
|
if (i < len && s->Get(i) == '0') {
|
2008-07-03 15:10:15 +00:00
|
|
|
radix = 8;
|
|
|
|
if (i + 1 < len) {
|
2009-03-17 09:33:06 +00:00
|
|
|
int c = s->Get(i + 1);
|
2008-07-03 15:10:15 +00:00
|
|
|
if (c == 'x' || c == 'X') {
|
|
|
|
radix = 16;
|
|
|
|
i += 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (radix == 16) {
|
|
|
|
// Allow 0x or 0X prefix if radix is 16.
|
2009-03-17 09:33:06 +00:00
|
|
|
if (i + 1 < len && s->Get(i) == '0') {
|
|
|
|
int c = s->Get(i + 1);
|
2008-07-03 15:10:15 +00:00
|
|
|
if (c == 'x' || c == 'X') i += 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
RUNTIME_ASSERT(2 <= radix && radix <= 36);
|
|
|
|
double value;
|
|
|
|
int end_index = StringToInt(s, i, radix, &value);
|
|
|
|
if (end_index != i) {
|
|
|
|
return Heap::NumberFromDouble(sign * value);
|
|
|
|
}
|
|
|
|
return Heap::nan_value();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static Object* Runtime_StringParseFloat(Arguments args) {
|
|
|
|
NoHandleAllocation ha;
|
|
|
|
CONVERT_CHECKED(String, str, args[0]);
|
|
|
|
|
|
|
|
// ECMA-262 section 15.1.2.3, empty string is NaN
|
|
|
|
double value = StringToDouble(str, ALLOW_TRAILING_JUNK, OS::nan_value());
|
|
|
|
|
|
|
|
// Create a number object from the value.
|
|
|
|
return Heap::NumberFromDouble(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static unibrow::Mapping<unibrow::ToUppercase, 128> to_upper_mapping;
|
|
|
|
static unibrow::Mapping<unibrow::ToLowercase, 128> to_lower_mapping;
|
|
|
|
|
|
|
|
|
|
|
|
template <class Converter>
|
2009-03-17 09:33:06 +00:00
|
|
|
static Object* ConvertCaseHelper(String* s,
|
|
|
|
int length,
|
|
|
|
int input_string_length,
|
|
|
|
unibrow::Mapping<Converter, 128>* mapping) {
|
|
|
|
// We try this twice, once with the assumption that the result is no longer
|
|
|
|
// than the input and, if that assumption breaks, again with the exact
|
|
|
|
// length. This may not be pretty, but it is nicer than what was here before
|
|
|
|
// and I hereby claim my vaffel-is.
|
|
|
|
//
|
2008-07-03 15:10:15 +00:00
|
|
|
// Allocate the resulting string.
|
|
|
|
//
|
|
|
|
// NOTE: This assumes that the upper/lower case of an ascii
|
|
|
|
// character is also ascii. This is currently the case, but it
|
|
|
|
// might break in the future if we implement more context and locale
|
|
|
|
// dependent upper/lower conversions.
|
2009-05-01 11:16:29 +00:00
|
|
|
Object* o = s->IsAsciiRepresentation()
|
2008-07-03 15:10:15 +00:00
|
|
|
? Heap::AllocateRawAsciiString(length)
|
|
|
|
: Heap::AllocateRawTwoByteString(length);
|
2008-08-27 10:11:39 +00:00
|
|
|
if (o->IsFailure()) return o;
|
2008-07-03 15:10:15 +00:00
|
|
|
String* result = String::cast(o);
|
|
|
|
bool has_changed_character = false;
|
|
|
|
|
|
|
|
// Convert all characters to upper case, assuming that they will fit
|
|
|
|
// in the buffer
|
2009-03-02 09:09:07 +00:00
|
|
|
Access<StringInputBuffer> buffer(&runtime_string_input_buffer);
|
2008-07-03 15:10:15 +00:00
|
|
|
buffer->Reset(s);
|
2008-11-25 11:07:48 +00:00
|
|
|
unibrow::uchar chars[Converter::kMaxWidth];
|
2008-07-03 15:10:15 +00:00
|
|
|
// We can assume that the string is not empty
|
|
|
|
uc32 current = buffer->GetNext();
|
2009-03-17 10:46:53 +00:00
|
|
|
for (int i = 0; i < length;) {
|
2008-10-14 09:13:23 +00:00
|
|
|
bool has_next = buffer->has_more();
|
|
|
|
uc32 next = has_next ? buffer->GetNext() : 0;
|
2008-07-03 15:10:15 +00:00
|
|
|
int char_length = mapping->get(current, next, chars);
|
|
|
|
if (char_length == 0) {
|
|
|
|
// The case conversion of this character is the character itself.
|
2009-03-17 09:33:06 +00:00
|
|
|
result->Set(i, current);
|
2008-07-03 15:10:15 +00:00
|
|
|
i++;
|
|
|
|
} else if (char_length == 1) {
|
|
|
|
// Common case: converting the letter resulted in one character.
|
|
|
|
ASSERT(static_cast<uc32>(chars[0]) != current);
|
2009-03-17 09:33:06 +00:00
|
|
|
result->Set(i, chars[0]);
|
2008-07-03 15:10:15 +00:00
|
|
|
has_changed_character = true;
|
|
|
|
i++;
|
2009-03-17 09:33:06 +00:00
|
|
|
} else if (length == input_string_length) {
|
2008-07-03 15:10:15 +00:00
|
|
|
// We've assumed that the result would be as long as the
|
|
|
|
// input but here is a character that converts to several
|
|
|
|
// characters. No matter, we calculate the exact length
|
|
|
|
// of the result and try the whole thing again.
|
|
|
|
//
|
|
|
|
// Note that this leaves room for optimization. We could just
|
|
|
|
// memcpy what we already have to the result string. Also,
|
|
|
|
// the result string is the last object allocated we could
|
|
|
|
// "realloc" it and probably, in the vast majority of cases,
|
|
|
|
// extend the existing string to be able to hold the full
|
|
|
|
// result.
|
2008-10-14 09:13:23 +00:00
|
|
|
int next_length = 0;
|
|
|
|
if (has_next) {
|
|
|
|
next_length = mapping->get(next, 0, chars);
|
|
|
|
if (next_length == 0) next_length = 1;
|
|
|
|
}
|
|
|
|
int current_length = i + char_length + next_length;
|
2008-07-03 15:10:15 +00:00
|
|
|
while (buffer->has_more()) {
|
|
|
|
current = buffer->GetNext();
|
2008-10-14 09:13:23 +00:00
|
|
|
// NOTE: we use 0 as the next character here because, while
|
|
|
|
// the next character may affect what a character converts to,
|
|
|
|
// it does not in any case affect the length of what it convert
|
|
|
|
// to.
|
2008-07-03 15:10:15 +00:00
|
|
|
int char_length = mapping->get(current, 0, chars);
|
|
|
|
if (char_length == 0) char_length = 1;
|
2008-10-14 09:13:23 +00:00
|
|
|
current_length += char_length;
|
2009-03-17 09:33:06 +00:00
|
|
|
if (current_length > Smi::kMaxValue) {
|
|
|
|
Top::context()->mark_out_of_memory();
|
|
|
|
return Failure::OutOfMemoryException();
|
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
2009-03-17 09:33:06 +00:00
|
|
|
// Try again with the real length.
|
|
|
|
return Smi::FromInt(current_length);
|
2008-07-03 15:10:15 +00:00
|
|
|
} else {
|
|
|
|
for (int j = 0; j < char_length; j++) {
|
2009-03-17 09:33:06 +00:00
|
|
|
result->Set(i, chars[j]);
|
2008-07-03 15:10:15 +00:00
|
|
|
i++;
|
|
|
|
}
|
|
|
|
has_changed_character = true;
|
|
|
|
}
|
|
|
|
current = next;
|
|
|
|
}
|
|
|
|
if (has_changed_character) {
|
|
|
|
return result;
|
|
|
|
} else {
|
|
|
|
// If we didn't actually change anything in doing the conversion
|
|
|
|
// we simple return the result and let the converted string
|
|
|
|
// become garbage; there is no reason to keep two identical strings
|
|
|
|
// alive.
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-17 09:33:06 +00:00
|
|
|
template <class Converter>
|
|
|
|
static Object* ConvertCase(Arguments args,
|
|
|
|
unibrow::Mapping<Converter, 128>* mapping) {
|
|
|
|
NoHandleAllocation ha;
|
|
|
|
|
|
|
|
CONVERT_CHECKED(String, s, args[0]);
|
|
|
|
s->TryFlattenIfNotFlat();
|
|
|
|
|
|
|
|
int input_string_length = s->length();
|
|
|
|
// Assume that the string is not empty; we need this assumption later
|
|
|
|
if (input_string_length == 0) return s;
|
|
|
|
int length = input_string_length;
|
|
|
|
|
|
|
|
Object* answer = ConvertCaseHelper(s, length, length, mapping);
|
|
|
|
if (answer->IsSmi()) {
|
|
|
|
// Retry with correct length.
|
|
|
|
answer = ConvertCaseHelper(s, Smi::cast(answer)->value(), length, mapping);
|
|
|
|
}
|
|
|
|
return answer; // This may be a failure.
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
static Object* Runtime_StringToLowerCase(Arguments args) {
|
|
|
|
return ConvertCase<unibrow::ToLowercase>(args, &to_lower_mapping);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static Object* Runtime_StringToUpperCase(Arguments args) {
|
|
|
|
return ConvertCase<unibrow::ToUppercase>(args, &to_upper_mapping);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-04-14 00:51:59 +00:00
|
|
|
bool Runtime::IsUpperCaseChar(uint16_t ch) {
|
|
|
|
unibrow::uchar chars[unibrow::ToUppercase::kMaxWidth];
|
|
|
|
int char_length = to_upper_mapping.get(ch, 0, chars);
|
|
|
|
return char_length == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
static Object* Runtime_NumberToString(Arguments args) {
|
|
|
|
NoHandleAllocation ha;
|
|
|
|
ASSERT(args.length() == 1);
|
|
|
|
|
|
|
|
Object* number = args[0];
|
|
|
|
RUNTIME_ASSERT(number->IsNumber());
|
|
|
|
|
|
|
|
Object* cached = Heap::GetNumberStringCache(number);
|
|
|
|
if (cached != Heap::undefined_value()) {
|
|
|
|
return cached;
|
|
|
|
}
|
|
|
|
|
|
|
|
char arr[100];
|
|
|
|
Vector<char> buffer(arr, ARRAY_SIZE(arr));
|
|
|
|
const char* str;
|
|
|
|
if (number->IsSmi()) {
|
|
|
|
int num = Smi::cast(number)->value();
|
|
|
|
str = IntToCString(num, buffer);
|
|
|
|
} else {
|
|
|
|
double num = HeapNumber::cast(number)->value();
|
|
|
|
str = DoubleToCString(num, buffer);
|
|
|
|
}
|
|
|
|
Object* result = Heap::AllocateStringFromAscii(CStrVector(str));
|
|
|
|
|
|
|
|
if (!result->IsFailure()) {
|
|
|
|
Heap::SetNumberStringCache(number, String::cast(result));
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static Object* Runtime_NumberToInteger(Arguments args) {
|
|
|
|
NoHandleAllocation ha;
|
|
|
|
ASSERT(args.length() == 1);
|
|
|
|
|
|
|
|
Object* obj = args[0];
|
2008-08-27 10:11:39 +00:00
|
|
|
if (obj->IsSmi()) return obj;
|
2008-07-03 15:10:15 +00:00
|
|
|
CONVERT_DOUBLE_CHECKED(number, obj);
|
|
|
|
return Heap::NumberFromDouble(DoubleToInteger(number));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static Object* Runtime_NumberToJSUint32(Arguments args) {
|
|
|
|
NoHandleAllocation ha;
|
|
|
|
ASSERT(args.length() == 1);
|
|
|
|
|
|
|
|
Object* obj = args[0];
|
|
|
|
if (obj->IsSmi() && Smi::cast(obj)->value() >= 0) return obj;
|
|
|
|
CONVERT_NUMBER_CHECKED(int32_t, number, Uint32, obj);
|
|
|
|
return Heap::NumberFromUint32(number);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static Object* Runtime_NumberToJSInt32(Arguments args) {
|
|
|
|
NoHandleAllocation ha;
|
|
|
|
ASSERT(args.length() == 1);
|
|
|
|
|
|
|
|
Object* obj = args[0];
|
|
|
|
if (obj->IsSmi()) return obj;
|
|
|
|
CONVERT_DOUBLE_CHECKED(number, obj);
|
|
|
|
return Heap::NumberFromInt32(DoubleToInt32(number));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-11-03 13:33:13 +00:00
|
|
|
// Converts a Number to a Smi, if possible. Returns NaN if the number is not
|
|
|
|
// a small integer.
|
|
|
|
static Object* Runtime_NumberToSmi(Arguments args) {
|
|
|
|
NoHandleAllocation ha;
|
|
|
|
ASSERT(args.length() == 1);
|
|
|
|
|
|
|
|
Object* obj = args[0];
|
|
|
|
if (obj->IsSmi()) {
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
if (obj->IsHeapNumber()) {
|
|
|
|
double value = HeapNumber::cast(obj)->value();
|
|
|
|
int int_value = FastD2I(value);
|
|
|
|
if (value == FastI2D(int_value) && Smi::IsValid(int_value)) {
|
|
|
|
return Smi::FromInt(int_value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Heap::nan_value();
|
|
|
|
}
|
|
|
|
|
2009-04-16 09:30:23 +00:00
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
static Object* Runtime_NumberAdd(Arguments args) {
|
|
|
|
NoHandleAllocation ha;
|
|
|
|
ASSERT(args.length() == 2);
|
|
|
|
|
|
|
|
CONVERT_DOUBLE_CHECKED(x, args[0]);
|
|
|
|
CONVERT_DOUBLE_CHECKED(y, args[1]);
|
|
|
|
return Heap::AllocateHeapNumber(x + y);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static Object* Runtime_NumberSub(Arguments args) {
|
|
|
|
NoHandleAllocation ha;
|
|
|
|
ASSERT(args.length() == 2);
|
|
|
|
|
|
|
|
CONVERT_DOUBLE_CHECKED(x, args[0]);
|
|
|
|
CONVERT_DOUBLE_CHECKED(y, args[1]);
|
|
|
|
return Heap::AllocateHeapNumber(x - y);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static Object* Runtime_NumberMul(Arguments args) {
|
|
|
|
NoHandleAllocation ha;
|
|
|
|
ASSERT(args.length() == 2);
|
|
|
|
|
|
|
|
CONVERT_DOUBLE_CHECKED(x, args[0]);
|
|
|
|
CONVERT_DOUBLE_CHECKED(y, args[1]);
|
|
|
|
return Heap::AllocateHeapNumber(x * y);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static Object* Runtime_NumberUnaryMinus(Arguments args) {
|
|
|
|
NoHandleAllocation ha;
|
|
|
|
ASSERT(args.length() == 1);
|
|
|
|
|
|
|
|
CONVERT_DOUBLE_CHECKED(x, args[0]);
|
|
|
|
return Heap::AllocateHeapNumber(-x);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static Object* Runtime_NumberDiv(Arguments args) {
|
|
|
|
NoHandleAllocation ha;
|
|
|
|
ASSERT(args.length() == 2);
|
|
|
|
|
|
|
|
CONVERT_DOUBLE_CHECKED(x, args[0]);
|
|
|
|
CONVERT_DOUBLE_CHECKED(y, args[1]);
|
|
|
|
return Heap::NewNumberFromDouble(x / y);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static Object* Runtime_NumberMod(Arguments args) {
|
|
|
|
NoHandleAllocation ha;
|
|
|
|
ASSERT(args.length() == 2);
|
|
|
|
|
|
|
|
CONVERT_DOUBLE_CHECKED(x, args[0]);
|
|
|
|
CONVERT_DOUBLE_CHECKED(y, args[1]);
|
|
|
|
|
2009-09-14 12:20:13 +00:00
|
|
|
#if defined WIN32 || defined _WIN64
|
2008-07-03 15:10:15 +00:00
|
|
|
// Workaround MS fmod bugs. ECMA-262 says:
|
|
|
|
// dividend is finite and divisor is an infinity => result equals dividend
|
|
|
|
// dividend is a zero and divisor is nonzero finite => result equals dividend
|
|
|
|
if (!(isfinite(x) && (!isfinite(y) && !isnan(y))) &&
|
|
|
|
!(x == 0 && (y != 0 && isfinite(y))))
|
|
|
|
#endif
|
|
|
|
x = fmod(x, y);
|
|
|
|
// NewNumberFromDouble may return a Smi instead of a Number object
|
|
|
|
return Heap::NewNumberFromDouble(x);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static Object* Runtime_StringAdd(Arguments args) {
|
|
|
|
NoHandleAllocation ha;
|
|
|
|
ASSERT(args.length() == 2);
|
|
|
|
CONVERT_CHECKED(String, str1, args[0]);
|
|
|
|
CONVERT_CHECKED(String, str2, args[1]);
|
2008-11-05 10:26:08 +00:00
|
|
|
return Heap::AllocateConsString(str1, str2);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-10-22 09:09:07 +00:00
|
|
|
template<typename sinkchar>
|
|
|
|
static inline void StringBuilderConcatHelper(String* special,
|
|
|
|
sinkchar* sink,
|
|
|
|
FixedArray* fixed_array,
|
|
|
|
int array_length) {
|
|
|
|
int position = 0;
|
|
|
|
for (int i = 0; i < array_length; i++) {
|
|
|
|
Object* element = fixed_array->get(i);
|
|
|
|
if (element->IsSmi()) {
|
2009-03-13 10:22:38 +00:00
|
|
|
int encoded_slice = Smi::cast(element)->value();
|
|
|
|
int pos = StringBuilderSubstringPosition::decode(encoded_slice);
|
|
|
|
int len = StringBuilderSubstringLength::decode(encoded_slice);
|
2008-11-03 10:16:05 +00:00
|
|
|
String::WriteToFlat(special,
|
|
|
|
sink + position,
|
|
|
|
pos,
|
|
|
|
pos + len);
|
2008-10-22 09:09:07 +00:00
|
|
|
position += len;
|
|
|
|
} else {
|
|
|
|
String* string = String::cast(element);
|
2009-03-17 09:33:06 +00:00
|
|
|
int element_length = string->length();
|
|
|
|
String::WriteToFlat(string, sink + position, 0, element_length);
|
2008-10-22 09:09:07 +00:00
|
|
|
position += element_length;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
static Object* Runtime_StringBuilderConcat(Arguments args) {
|
|
|
|
NoHandleAllocation ha;
|
|
|
|
ASSERT(args.length() == 2);
|
|
|
|
CONVERT_CHECKED(JSArray, array, args[0]);
|
|
|
|
CONVERT_CHECKED(String, special, args[1]);
|
2009-03-17 09:33:06 +00:00
|
|
|
int special_length = special->length();
|
2008-07-03 15:10:15 +00:00
|
|
|
Object* smi_array_length = array->length();
|
|
|
|
if (!smi_array_length->IsSmi()) {
|
|
|
|
Top::context()->mark_out_of_memory();
|
|
|
|
return Failure::OutOfMemoryException();
|
|
|
|
}
|
|
|
|
int array_length = Smi::cast(smi_array_length)->value();
|
|
|
|
if (!array->HasFastElements()) {
|
|
|
|
return Top::Throw(Heap::illegal_argument_symbol());
|
|
|
|
}
|
|
|
|
FixedArray* fixed_array = FixedArray::cast(array->elements());
|
2008-08-27 10:11:39 +00:00
|
|
|
if (fixed_array->length() < array_length) {
|
2008-07-03 15:10:15 +00:00
|
|
|
array_length = fixed_array->length();
|
2008-08-27 10:11:39 +00:00
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
if (array_length == 0) {
|
|
|
|
return Heap::empty_string();
|
|
|
|
} else if (array_length == 1) {
|
|
|
|
Object* first = fixed_array->get(0);
|
|
|
|
if (first->IsString()) return first;
|
|
|
|
}
|
|
|
|
|
2009-05-01 11:16:29 +00:00
|
|
|
bool ascii = special->IsAsciiRepresentation();
|
2008-07-03 15:10:15 +00:00
|
|
|
int position = 0;
|
|
|
|
for (int i = 0; i < array_length; i++) {
|
|
|
|
Object* elt = fixed_array->get(i);
|
|
|
|
if (elt->IsSmi()) {
|
|
|
|
int len = Smi::cast(elt)->value();
|
|
|
|
int pos = len >> 11;
|
|
|
|
len &= 0x7ff;
|
|
|
|
if (pos + len > special_length) {
|
|
|
|
return Top::Throw(Heap::illegal_argument_symbol());
|
|
|
|
}
|
|
|
|
position += len;
|
|
|
|
} else if (elt->IsString()) {
|
|
|
|
String* element = String::cast(elt);
|
2009-03-17 09:33:06 +00:00
|
|
|
int element_length = element->length();
|
2008-07-03 15:10:15 +00:00
|
|
|
if (!Smi::IsValid(element_length + position)) {
|
|
|
|
Top::context()->mark_out_of_memory();
|
|
|
|
return Failure::OutOfMemoryException();
|
|
|
|
}
|
|
|
|
position += element_length;
|
2009-05-01 11:16:29 +00:00
|
|
|
if (ascii && !element->IsAsciiRepresentation()) {
|
2008-07-03 15:10:15 +00:00
|
|
|
ascii = false;
|
2008-08-27 10:11:39 +00:00
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
} else {
|
|
|
|
return Top::Throw(Heap::illegal_argument_symbol());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int length = position;
|
|
|
|
Object* object;
|
2008-10-22 09:09:07 +00:00
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
if (ascii) {
|
|
|
|
object = Heap::AllocateRawAsciiString(length);
|
2008-10-22 09:09:07 +00:00
|
|
|
if (object->IsFailure()) return object;
|
|
|
|
SeqAsciiString* answer = SeqAsciiString::cast(object);
|
|
|
|
StringBuilderConcatHelper(special,
|
|
|
|
answer->GetChars(),
|
|
|
|
fixed_array,
|
|
|
|
array_length);
|
|
|
|
return answer;
|
2008-07-03 15:10:15 +00:00
|
|
|
} else {
|
|
|
|
object = Heap::AllocateRawTwoByteString(length);
|
2008-10-22 09:09:07 +00:00
|
|
|
if (object->IsFailure()) return object;
|
|
|
|
SeqTwoByteString* answer = SeqTwoByteString::cast(object);
|
|
|
|
StringBuilderConcatHelper(special,
|
|
|
|
answer->GetChars(),
|
|
|
|
fixed_array,
|
|
|
|
array_length);
|
|
|
|
return answer;
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static Object* Runtime_NumberOr(Arguments args) {
|
|
|
|
NoHandleAllocation ha;
|
|
|
|
ASSERT(args.length() == 2);
|
|
|
|
|
|
|
|
CONVERT_NUMBER_CHECKED(int32_t, x, Int32, args[0]);
|
|
|
|
CONVERT_NUMBER_CHECKED(int32_t, y, Int32, args[1]);
|
|
|
|
return Heap::NumberFromInt32(x | y);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static Object* Runtime_NumberAnd(Arguments args) {
|
|
|
|
NoHandleAllocation ha;
|
|
|
|
ASSERT(args.length() == 2);
|
|
|
|
|
|
|
|
CONVERT_NUMBER_CHECKED(int32_t, x, Int32, args[0]);
|
|
|
|
CONVERT_NUMBER_CHECKED(int32_t, y, Int32, args[1]);
|
|
|
|
return Heap::NumberFromInt32(x & y);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static Object* Runtime_NumberXor(Arguments args) {
|
|
|
|
NoHandleAllocation ha;
|
|
|
|
ASSERT(args.length() == 2);
|
|
|
|
|
|
|
|
CONVERT_NUMBER_CHECKED(int32_t, x, Int32, args[0]);
|
|
|
|
CONVERT_NUMBER_CHECKED(int32_t, y, Int32, args[1]);
|
|
|
|
return Heap::NumberFromInt32(x ^ y);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static Object* Runtime_NumberNot(Arguments args) {
|
|
|
|
NoHandleAllocation ha;
|
|
|
|
ASSERT(args.length() == 1);
|
|
|
|
|
|
|
|
CONVERT_NUMBER_CHECKED(int32_t, x, Int32, args[0]);
|
|
|
|
return Heap::NumberFromInt32(~x);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static Object* Runtime_NumberShl(Arguments args) {
|
|
|
|
NoHandleAllocation ha;
|
|
|
|
ASSERT(args.length() == 2);
|
|
|
|
|
|
|
|
CONVERT_NUMBER_CHECKED(int32_t, x, Int32, args[0]);
|
|
|
|
CONVERT_NUMBER_CHECKED(int32_t, y, Int32, args[1]);
|
|
|
|
return Heap::NumberFromInt32(x << (y & 0x1f));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static Object* Runtime_NumberShr(Arguments args) {
|
|
|
|
NoHandleAllocation ha;
|
|
|
|
ASSERT(args.length() == 2);
|
|
|
|
|
|
|
|
CONVERT_NUMBER_CHECKED(uint32_t, x, Uint32, args[0]);
|
|
|
|
CONVERT_NUMBER_CHECKED(int32_t, y, Int32, args[1]);
|
|
|
|
return Heap::NumberFromUint32(x >> (y & 0x1f));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static Object* Runtime_NumberSar(Arguments args) {
|
|
|
|
NoHandleAllocation ha;
|
|
|
|
ASSERT(args.length() == 2);
|
|
|
|
|
|
|
|
CONVERT_NUMBER_CHECKED(int32_t, x, Int32, args[0]);
|
|
|
|
CONVERT_NUMBER_CHECKED(int32_t, y, Int32, args[1]);
|
|
|
|
return Heap::NumberFromInt32(ArithmeticShiftRight(x, y & 0x1f));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static Object* Runtime_NumberEquals(Arguments args) {
|
|
|
|
NoHandleAllocation ha;
|
|
|
|
ASSERT(args.length() == 2);
|
|
|
|
|
|
|
|
CONVERT_DOUBLE_CHECKED(x, args[0]);
|
|
|
|
CONVERT_DOUBLE_CHECKED(y, args[1]);
|
|
|
|
if (isnan(x)) return Smi::FromInt(NOT_EQUAL);
|
|
|
|
if (isnan(y)) return Smi::FromInt(NOT_EQUAL);
|
|
|
|
if (x == y) return Smi::FromInt(EQUAL);
|
|
|
|
Object* result;
|
|
|
|
if ((fpclassify(x) == FP_ZERO) && (fpclassify(y) == FP_ZERO)) {
|
|
|
|
result = Smi::FromInt(EQUAL);
|
|
|
|
} else {
|
|
|
|
result = Smi::FromInt(NOT_EQUAL);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static Object* Runtime_StringEquals(Arguments args) {
|
|
|
|
NoHandleAllocation ha;
|
|
|
|
ASSERT(args.length() == 2);
|
|
|
|
|
|
|
|
CONVERT_CHECKED(String, x, args[0]);
|
|
|
|
CONVERT_CHECKED(String, y, args[1]);
|
|
|
|
|
2008-10-22 12:33:20 +00:00
|
|
|
bool not_equal = !x->Equals(y);
|
|
|
|
// This is slightly convoluted because the value that signifies
|
|
|
|
// equality is 0 and inequality is 1 so we have to negate the result
|
|
|
|
// from String::Equals.
|
|
|
|
ASSERT(not_equal == 0 || not_equal == 1);
|
|
|
|
STATIC_CHECK(EQUAL == 0);
|
|
|
|
STATIC_CHECK(NOT_EQUAL == 1);
|
|
|
|
return Smi::FromInt(not_equal);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static Object* Runtime_NumberCompare(Arguments args) {
|
|
|
|
NoHandleAllocation ha;
|
|
|
|
ASSERT(args.length() == 3);
|
|
|
|
|
|
|
|
CONVERT_DOUBLE_CHECKED(x, args[0]);
|
|
|
|
CONVERT_DOUBLE_CHECKED(y, args[1]);
|
|
|
|
if (isnan(x) || isnan(y)) return args[2];
|
|
|
|
if (x == y) return Smi::FromInt(EQUAL);
|
|
|
|
if (isless(x, y)) return Smi::FromInt(LESS);
|
|
|
|
return Smi::FromInt(GREATER);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-09-08 06:17:38 +00:00
|
|
|
// Compare two Smis as if they were converted to strings and then
|
|
|
|
// compared lexicographically.
|
|
|
|
static Object* Runtime_SmiLexicographicCompare(Arguments args) {
|
|
|
|
NoHandleAllocation ha;
|
|
|
|
ASSERT(args.length() == 2);
|
|
|
|
|
|
|
|
// Arrays for the individual characters of the two Smis. Smis are
|
|
|
|
// 31 bit integers and 10 decimal digits are therefore enough.
|
|
|
|
static int x_elms[10];
|
|
|
|
static int y_elms[10];
|
|
|
|
|
|
|
|
// Extract the integer values from the Smis.
|
|
|
|
CONVERT_CHECKED(Smi, x, args[0]);
|
|
|
|
CONVERT_CHECKED(Smi, y, args[1]);
|
|
|
|
int x_value = x->value();
|
|
|
|
int y_value = y->value();
|
|
|
|
|
|
|
|
// If the integers are equal so are the string representations.
|
|
|
|
if (x_value == y_value) return Smi::FromInt(EQUAL);
|
|
|
|
|
|
|
|
// If one of the integers are zero the normal integer order is the
|
|
|
|
// same as the lexicographic order of the string representations.
|
|
|
|
if (x_value == 0 || y_value == 0) return Smi::FromInt(x_value - y_value);
|
|
|
|
|
2009-01-15 19:08:34 +00:00
|
|
|
// If only one of the integers is negative the negative number is
|
2008-09-08 06:17:38 +00:00
|
|
|
// smallest because the char code of '-' is less than the char code
|
|
|
|
// of any digit. Otherwise, we make both values positive.
|
|
|
|
if (x_value < 0 || y_value < 0) {
|
|
|
|
if (y_value >= 0) return Smi::FromInt(LESS);
|
|
|
|
if (x_value >= 0) return Smi::FromInt(GREATER);
|
|
|
|
x_value = -x_value;
|
|
|
|
y_value = -y_value;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Convert the integers to arrays of their decimal digits.
|
|
|
|
int x_index = 0;
|
|
|
|
int y_index = 0;
|
|
|
|
while (x_value > 0) {
|
|
|
|
x_elms[x_index++] = x_value % 10;
|
|
|
|
x_value /= 10;
|
|
|
|
}
|
|
|
|
while (y_value > 0) {
|
|
|
|
y_elms[y_index++] = y_value % 10;
|
|
|
|
y_value /= 10;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Loop through the arrays of decimal digits finding the first place
|
|
|
|
// where they differ.
|
|
|
|
while (--x_index >= 0 && --y_index >= 0) {
|
|
|
|
int diff = x_elms[x_index] - y_elms[y_index];
|
|
|
|
if (diff != 0) return Smi::FromInt(diff);
|
|
|
|
}
|
|
|
|
|
|
|
|
// If one array is a suffix of the other array, the longest array is
|
|
|
|
// the representation of the largest of the Smis in the
|
|
|
|
// lexicographic ordering.
|
|
|
|
return Smi::FromInt(x_index - y_index);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
static Object* Runtime_StringCompare(Arguments args) {
|
|
|
|
NoHandleAllocation ha;
|
|
|
|
ASSERT(args.length() == 2);
|
|
|
|
|
|
|
|
CONVERT_CHECKED(String, x, args[0]);
|
|
|
|
CONVERT_CHECKED(String, y, args[1]);
|
|
|
|
|
|
|
|
// A few fast case tests before we flatten.
|
|
|
|
if (x == y) return Smi::FromInt(EQUAL);
|
2009-03-17 09:33:06 +00:00
|
|
|
if (y->length() == 0) {
|
|
|
|
if (x->length() == 0) return Smi::FromInt(EQUAL);
|
2008-07-03 15:10:15 +00:00
|
|
|
return Smi::FromInt(GREATER);
|
2009-03-17 09:33:06 +00:00
|
|
|
} else if (x->length() == 0) {
|
2008-07-03 15:10:15 +00:00
|
|
|
return Smi::FromInt(LESS);
|
|
|
|
}
|
2008-08-27 10:11:39 +00:00
|
|
|
|
2009-03-17 09:33:06 +00:00
|
|
|
int d = x->Get(0) - y->Get(0);
|
2008-08-27 10:11:39 +00:00
|
|
|
if (d < 0) return Smi::FromInt(LESS);
|
|
|
|
else if (d > 0) return Smi::FromInt(GREATER);
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2009-03-17 09:33:06 +00:00
|
|
|
x->TryFlattenIfNotFlat();
|
|
|
|
y->TryFlattenIfNotFlat();
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
static StringInputBuffer bufx;
|
|
|
|
static StringInputBuffer bufy;
|
|
|
|
bufx.Reset(x);
|
|
|
|
bufy.Reset(y);
|
|
|
|
while (bufx.has_more() && bufy.has_more()) {
|
|
|
|
int d = bufx.GetNext() - bufy.GetNext();
|
|
|
|
if (d < 0) return Smi::FromInt(LESS);
|
|
|
|
else if (d > 0) return Smi::FromInt(GREATER);
|
|
|
|
}
|
|
|
|
|
|
|
|
// x is (non-trivial) prefix of y:
|
|
|
|
if (bufy.has_more()) return Smi::FromInt(LESS);
|
|
|
|
// y is prefix of x:
|
|
|
|
return Smi::FromInt(bufx.has_more() ? GREATER : EQUAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static Object* Runtime_Math_abs(Arguments args) {
|
|
|
|
NoHandleAllocation ha;
|
|
|
|
ASSERT(args.length() == 1);
|
|
|
|
|
|
|
|
CONVERT_DOUBLE_CHECKED(x, args[0]);
|
|
|
|
return Heap::AllocateHeapNumber(fabs(x));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static Object* Runtime_Math_acos(Arguments args) {
|
|
|
|
NoHandleAllocation ha;
|
|
|
|
ASSERT(args.length() == 1);
|
|
|
|
|
|
|
|
CONVERT_DOUBLE_CHECKED(x, args[0]);
|
2009-09-01 09:03:58 +00:00
|
|
|
return TranscendentalCache::Get(TranscendentalCache::ACOS, x);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static Object* Runtime_Math_asin(Arguments args) {
|
|
|
|
NoHandleAllocation ha;
|
|
|
|
ASSERT(args.length() == 1);
|
|
|
|
|
|
|
|
CONVERT_DOUBLE_CHECKED(x, args[0]);
|
2009-09-01 09:03:58 +00:00
|
|
|
return TranscendentalCache::Get(TranscendentalCache::ASIN, x);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static Object* Runtime_Math_atan(Arguments args) {
|
|
|
|
NoHandleAllocation ha;
|
|
|
|
ASSERT(args.length() == 1);
|
|
|
|
|
|
|
|
CONVERT_DOUBLE_CHECKED(x, args[0]);
|
2009-09-01 09:03:58 +00:00
|
|
|
return TranscendentalCache::Get(TranscendentalCache::ATAN, x);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static Object* Runtime_Math_atan2(Arguments args) {
|
|
|
|
NoHandleAllocation ha;
|
|
|
|
ASSERT(args.length() == 2);
|
|
|
|
|
|
|
|
CONVERT_DOUBLE_CHECKED(x, args[0]);
|
|
|
|
CONVERT_DOUBLE_CHECKED(y, args[1]);
|
|
|
|
double result;
|
|
|
|
if (isinf(x) && isinf(y)) {
|
|
|
|
// Make sure that the result in case of two infinite arguments
|
|
|
|
// is a multiple of Pi / 4. The sign of the result is determined
|
|
|
|
// by the first argument (x) and the sign of the second argument
|
|
|
|
// determines the multiplier: one or three.
|
|
|
|
static double kPiDividedBy4 = 0.78539816339744830962;
|
|
|
|
int multiplier = (x < 0) ? -1 : 1;
|
|
|
|
if (y < 0) multiplier *= 3;
|
|
|
|
result = multiplier * kPiDividedBy4;
|
|
|
|
} else {
|
|
|
|
result = atan2(x, y);
|
|
|
|
}
|
|
|
|
return Heap::AllocateHeapNumber(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static Object* Runtime_Math_ceil(Arguments args) {
|
|
|
|
NoHandleAllocation ha;
|
|
|
|
ASSERT(args.length() == 1);
|
|
|
|
|
|
|
|
CONVERT_DOUBLE_CHECKED(x, args[0]);
|
|
|
|
return Heap::NumberFromDouble(ceiling(x));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static Object* Runtime_Math_cos(Arguments args) {
|
|
|
|
NoHandleAllocation ha;
|
|
|
|
ASSERT(args.length() == 1);
|
|
|
|
|
|
|
|
CONVERT_DOUBLE_CHECKED(x, args[0]);
|
2009-09-01 09:03:58 +00:00
|
|
|
return TranscendentalCache::Get(TranscendentalCache::COS, x);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static Object* Runtime_Math_exp(Arguments args) {
|
|
|
|
NoHandleAllocation ha;
|
|
|
|
ASSERT(args.length() == 1);
|
|
|
|
|
|
|
|
CONVERT_DOUBLE_CHECKED(x, args[0]);
|
2009-09-01 09:03:58 +00:00
|
|
|
return TranscendentalCache::Get(TranscendentalCache::EXP, x);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static Object* Runtime_Math_floor(Arguments args) {
|
|
|
|
NoHandleAllocation ha;
|
|
|
|
ASSERT(args.length() == 1);
|
|
|
|
|
|
|
|
CONVERT_DOUBLE_CHECKED(x, args[0]);
|
|
|
|
return Heap::NumberFromDouble(floor(x));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static Object* Runtime_Math_log(Arguments args) {
|
|
|
|
NoHandleAllocation ha;
|
|
|
|
ASSERT(args.length() == 1);
|
|
|
|
|
|
|
|
CONVERT_DOUBLE_CHECKED(x, args[0]);
|
2009-09-01 09:03:58 +00:00
|
|
|
return TranscendentalCache::Get(TranscendentalCache::LOG, x);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-06-17 11:06:32 +00:00
|
|
|
// Helper function to compute x^y, where y is known to be an
|
|
|
|
// integer. Uses binary decomposition to limit the number of
|
|
|
|
// multiplications; see the discussion in "Hacker's Delight" by Henry
|
|
|
|
// S. Warren, Jr., figure 11-6, page 213.
|
|
|
|
static double powi(double x, int y) {
|
|
|
|
ASSERT(y != kMinInt);
|
|
|
|
unsigned n = (y < 0) ? -y : y;
|
|
|
|
double m = x;
|
|
|
|
double p = 1;
|
|
|
|
while (true) {
|
|
|
|
if ((n & 1) != 0) p *= m;
|
|
|
|
n >>= 1;
|
|
|
|
if (n == 0) {
|
|
|
|
if (y < 0) {
|
|
|
|
// Unfortunately, we have to be careful when p has reached
|
|
|
|
// infinity in the computation, because sometimes the higher
|
|
|
|
// internal precision in the pow() implementation would have
|
|
|
|
// given us a finite p. This happens very rarely.
|
|
|
|
double result = 1.0 / p;
|
2009-06-18 05:47:31 +00:00
|
|
|
return (result == 0 && isinf(p))
|
|
|
|
? pow(x, static_cast<double>(y)) // Avoid pow(double, int).
|
|
|
|
: result;
|
2009-06-17 11:06:32 +00:00
|
|
|
} else {
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
m *= m;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
static Object* Runtime_Math_pow(Arguments args) {
|
|
|
|
NoHandleAllocation ha;
|
|
|
|
ASSERT(args.length() == 2);
|
|
|
|
|
|
|
|
CONVERT_DOUBLE_CHECKED(x, args[0]);
|
2009-06-17 11:06:32 +00:00
|
|
|
|
|
|
|
// If the second argument is a smi, it is much faster to call the
|
|
|
|
// custom powi() function than the generic pow().
|
|
|
|
if (args[1]->IsSmi()) {
|
|
|
|
int y = Smi::cast(args[1])->value();
|
|
|
|
return Heap::AllocateHeapNumber(powi(x, y));
|
|
|
|
}
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
CONVERT_DOUBLE_CHECKED(y, args[1]);
|
2009-07-07 11:57:09 +00:00
|
|
|
|
|
|
|
if (!isinf(x)) {
|
|
|
|
if (y == 0.5) {
|
|
|
|
// It's not uncommon to use Math.pow(x, 0.5) to compute the
|
|
|
|
// square root of a number. To speed up such computations, we
|
|
|
|
// explictly check for this case and use the sqrt() function
|
|
|
|
// which is faster than pow().
|
|
|
|
return Heap::AllocateHeapNumber(sqrt(x));
|
|
|
|
} else if (y == -0.5) {
|
|
|
|
// Optimized using Math.pow(x, -0.5) == 1 / Math.pow(x, 0.5).
|
|
|
|
return Heap::AllocateHeapNumber(1.0 / sqrt(x));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (y == 0) {
|
2008-07-03 15:10:15 +00:00
|
|
|
return Smi::FromInt(1);
|
2009-06-17 11:06:32 +00:00
|
|
|
} else if (isnan(y) || ((x == 1 || x == -1) && isinf(y))) {
|
|
|
|
return Heap::nan_value();
|
2008-07-03 15:10:15 +00:00
|
|
|
} else {
|
|
|
|
return Heap::AllocateHeapNumber(pow(x, y));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static Object* Runtime_Math_round(Arguments args) {
|
|
|
|
NoHandleAllocation ha;
|
|
|
|
ASSERT(args.length() == 1);
|
|
|
|
|
|
|
|
CONVERT_DOUBLE_CHECKED(x, args[0]);
|
|
|
|
if (signbit(x) && x >= -0.5) return Heap::minus_zero_value();
|
|
|
|
return Heap::NumberFromDouble(floor(x + 0.5));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static Object* Runtime_Math_sin(Arguments args) {
|
|
|
|
NoHandleAllocation ha;
|
|
|
|
ASSERT(args.length() == 1);
|
|
|
|
|
|
|
|
CONVERT_DOUBLE_CHECKED(x, args[0]);
|
2009-09-01 09:03:58 +00:00
|
|
|
return TranscendentalCache::Get(TranscendentalCache::SIN, x);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static Object* Runtime_Math_sqrt(Arguments args) {
|
|
|
|
NoHandleAllocation ha;
|
|
|
|
ASSERT(args.length() == 1);
|
|
|
|
|
|
|
|
CONVERT_DOUBLE_CHECKED(x, args[0]);
|
|
|
|
return Heap::AllocateHeapNumber(sqrt(x));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static Object* Runtime_Math_tan(Arguments args) {
|
|
|
|
NoHandleAllocation ha;
|
|
|
|
ASSERT(args.length() == 1);
|
|
|
|
|
|
|
|
CONVERT_DOUBLE_CHECKED(x, args[0]);
|
2009-09-01 09:03:58 +00:00
|
|
|
return TranscendentalCache::Get(TranscendentalCache::TAN, x);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-10-06 06:08:15 +00:00
|
|
|
// The NewArguments function is only used when constructing the
|
|
|
|
// arguments array when calling non-functions from JavaScript in
|
|
|
|
// runtime.js:CALL_NON_FUNCTION.
|
2008-07-03 15:10:15 +00:00
|
|
|
static Object* Runtime_NewArguments(Arguments args) {
|
|
|
|
NoHandleAllocation ha;
|
|
|
|
ASSERT(args.length() == 1);
|
|
|
|
|
|
|
|
// ECMA-262, 3rd., 10.1.8, p.39
|
|
|
|
CONVERT_CHECKED(JSFunction, callee, args[0]);
|
|
|
|
|
|
|
|
// Compute the frame holding the arguments.
|
|
|
|
JavaScriptFrameIterator it;
|
|
|
|
it.AdvanceToArgumentsFrame();
|
|
|
|
JavaScriptFrame* frame = it.frame();
|
|
|
|
|
|
|
|
const int length = frame->GetProvidedParametersCount();
|
|
|
|
Object* result = Heap::AllocateArgumentsObject(callee, length);
|
|
|
|
if (result->IsFailure()) return result;
|
2008-10-23 14:55:45 +00:00
|
|
|
if (length > 0) {
|
|
|
|
Object* obj = Heap::AllocateFixedArray(length);
|
|
|
|
if (obj->IsFailure()) return obj;
|
|
|
|
FixedArray* array = FixedArray::cast(obj);
|
|
|
|
ASSERT(array->length() == length);
|
|
|
|
WriteBarrierMode mode = array->GetWriteBarrierMode();
|
|
|
|
for (int i = 0; i < length; i++) {
|
|
|
|
array->set(i, frame->GetParameter(i), mode);
|
|
|
|
}
|
|
|
|
JSObject::cast(result)->set_elements(array);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-10-06 06:08:15 +00:00
|
|
|
static Object* Runtime_NewArgumentsFast(Arguments args) {
|
|
|
|
NoHandleAllocation ha;
|
|
|
|
ASSERT(args.length() == 3);
|
|
|
|
|
|
|
|
JSFunction* callee = JSFunction::cast(args[0]);
|
|
|
|
Object** parameters = reinterpret_cast<Object**>(args[1]);
|
|
|
|
const int length = Smi::cast(args[2])->value();
|
|
|
|
|
|
|
|
Object* result = Heap::AllocateArgumentsObject(callee, length);
|
|
|
|
if (result->IsFailure()) return result;
|
2008-10-23 14:55:45 +00:00
|
|
|
ASSERT(Heap::InNewSpace(result));
|
|
|
|
|
|
|
|
// Allocate the elements if needed.
|
|
|
|
if (length > 0) {
|
|
|
|
// Allocate the fixed array.
|
|
|
|
Object* obj = Heap::AllocateRawFixedArray(length);
|
|
|
|
if (obj->IsFailure()) return obj;
|
|
|
|
reinterpret_cast<Array*>(obj)->set_map(Heap::fixed_array_map());
|
|
|
|
FixedArray* array = FixedArray::cast(obj);
|
|
|
|
array->set_length(length);
|
|
|
|
WriteBarrierMode mode = array->GetWriteBarrierMode();
|
|
|
|
for (int i = 0; i < length; i++) {
|
|
|
|
array->set(i, *--parameters, mode);
|
|
|
|
}
|
|
|
|
JSObject::cast(result)->set_elements(FixedArray::cast(obj),
|
|
|
|
SKIP_WRITE_BARRIER);
|
2008-10-06 06:08:15 +00:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
static Object* Runtime_NewClosure(Arguments args) {
|
|
|
|
HandleScope scope;
|
|
|
|
ASSERT(args.length() == 2);
|
|
|
|
CONVERT_ARG_CHECKED(JSFunction, boilerplate, 0);
|
|
|
|
CONVERT_ARG_CHECKED(Context, context, 1);
|
|
|
|
|
|
|
|
Handle<JSFunction> result =
|
|
|
|
Factory::NewFunctionFromBoilerplate(boilerplate, context);
|
|
|
|
return *result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-08-26 12:22:44 +00:00
|
|
|
static Code* ComputeConstructStub(Handle<SharedFunctionInfo> shared) {
|
2009-06-19 07:36:16 +00:00
|
|
|
// TODO(385): Change this to create a construct stub specialized for
|
|
|
|
// the given map to make allocation of simple objects - and maybe
|
|
|
|
// arrays - much faster.
|
2009-08-26 12:22:44 +00:00
|
|
|
if (FLAG_inline_new
|
|
|
|
&& shared->has_only_simple_this_property_assignments()) {
|
|
|
|
ConstructStubCompiler compiler;
|
|
|
|
Object* code = compiler.CompileConstructStub(*shared);
|
|
|
|
if (code->IsFailure()) {
|
|
|
|
return Builtins::builtin(Builtins::JSConstructStubGeneric);
|
|
|
|
}
|
|
|
|
return Code::cast(code);
|
|
|
|
}
|
|
|
|
|
|
|
|
return Builtins::builtin(Builtins::JSConstructStubGeneric);
|
2009-06-19 07:36:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
static Object* Runtime_NewObject(Arguments args) {
|
2009-06-19 07:36:16 +00:00
|
|
|
HandleScope scope;
|
2008-07-03 15:10:15 +00:00
|
|
|
ASSERT(args.length() == 1);
|
|
|
|
|
2009-06-19 07:36:16 +00:00
|
|
|
Handle<Object> constructor = args.at<Object>(0);
|
|
|
|
|
|
|
|
// If the constructor isn't a proper function we throw a type error.
|
|
|
|
if (!constructor->IsJSFunction()) {
|
|
|
|
Vector< Handle<Object> > arguments = HandleVector(&constructor, 1);
|
|
|
|
Handle<Object> type_error =
|
|
|
|
Factory::NewTypeError("not_constructor", arguments);
|
|
|
|
return Top::Throw(*type_error);
|
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2009-06-19 07:36:16 +00:00
|
|
|
Handle<JSFunction> function = Handle<JSFunction>::cast(constructor);
|
2009-04-20 16:36:13 +00:00
|
|
|
#ifdef ENABLE_DEBUGGER_SUPPORT
|
2009-06-19 07:36:16 +00:00
|
|
|
// Handle stepping into constructors if step into is active.
|
|
|
|
if (Debug::StepInActive()) {
|
2009-07-10 09:57:53 +00:00
|
|
|
Debug::HandleStepIn(function, Handle<Object>::null(), 0, true);
|
2009-06-19 07:36:16 +00:00
|
|
|
}
|
2009-04-20 16:36:13 +00:00
|
|
|
#endif
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2009-06-19 07:36:16 +00:00
|
|
|
if (function->has_initial_map()) {
|
|
|
|
if (function->initial_map()->instance_type() == JS_FUNCTION_TYPE) {
|
2008-07-03 15:10:15 +00:00
|
|
|
// The 'Function' function ignores the receiver object when
|
|
|
|
// called using 'new' and creates a new JSFunction object that
|
|
|
|
// is returned. The receiver object is only used for error
|
|
|
|
// reporting if an error occurs when constructing the new
|
2009-06-19 07:36:16 +00:00
|
|
|
// JSFunction. Factory::NewJSObject() should not be used to
|
|
|
|
// allocate JSFunctions since it does not properly initialize
|
|
|
|
// the shared part of the function. Since the receiver is
|
|
|
|
// ignored anyway, we use the global object as the receiver
|
|
|
|
// instead of a new JSFunction object. This way, errors are
|
|
|
|
// reported the same way whether or not 'Function' is called
|
|
|
|
// using 'new'.
|
2008-07-03 15:10:15 +00:00
|
|
|
return Top::context()->global();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-08-26 11:03:07 +00:00
|
|
|
// The function should be compiled for the optimization hints to be available.
|
|
|
|
if (!function->shared()->is_compiled()) {
|
2009-08-26 12:22:44 +00:00
|
|
|
CompileLazyShared(Handle<SharedFunctionInfo>(function->shared()),
|
|
|
|
CLEAR_EXCEPTION,
|
|
|
|
0);
|
2009-08-26 11:03:07 +00:00
|
|
|
}
|
|
|
|
|
2009-06-19 07:36:16 +00:00
|
|
|
bool first_allocation = !function->has_initial_map();
|
|
|
|
Handle<JSObject> result = Factory::NewJSObject(function);
|
|
|
|
if (first_allocation) {
|
|
|
|
Handle<Map> map = Handle<Map>(function->initial_map());
|
2009-08-26 12:22:44 +00:00
|
|
|
Handle<Code> stub = Handle<Code>(
|
|
|
|
ComputeConstructStub(Handle<SharedFunctionInfo>(function->shared())));
|
2009-06-19 07:36:16 +00:00
|
|
|
function->shared()->set_construct_stub(*stub);
|
|
|
|
}
|
2009-08-26 12:22:44 +00:00
|
|
|
|
2009-08-19 07:30:20 +00:00
|
|
|
Counters::constructed_objects.Increment();
|
|
|
|
Counters::constructed_objects_runtime.Increment();
|
2009-08-26 12:22:44 +00:00
|
|
|
|
2009-06-19 07:36:16 +00:00
|
|
|
return *result;
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static Object* Runtime_LazyCompile(Arguments args) {
|
|
|
|
HandleScope scope;
|
|
|
|
ASSERT(args.length() == 1);
|
|
|
|
|
|
|
|
Handle<JSFunction> function = args.at<JSFunction>(0);
|
|
|
|
#ifdef DEBUG
|
|
|
|
if (FLAG_trace_lazy) {
|
|
|
|
PrintF("[lazy: ");
|
|
|
|
function->shared()->name()->Print();
|
|
|
|
PrintF("]\n");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2009-05-25 18:29:02 +00:00
|
|
|
// Compile the target function. Here we compile using CompileLazyInLoop in
|
|
|
|
// order to get the optimized version. This helps code like delta-blue
|
|
|
|
// that calls performance-critical routines through constructors. A
|
|
|
|
// constructor call doesn't use a CallIC, it uses a LoadIC followed by a
|
|
|
|
// direct call. Since the in-loop tracking takes place through CallICs
|
|
|
|
// this means that things called through constructors are never known to
|
|
|
|
// be in loops. We compile them as if they are in loops here just in case.
|
2008-07-03 15:10:15 +00:00
|
|
|
ASSERT(!function->is_compiled());
|
2009-05-25 18:29:02 +00:00
|
|
|
if (!CompileLazyInLoop(function, KEEP_EXCEPTION)) {
|
2008-07-03 15:10:15 +00:00
|
|
|
return Failure::Exception();
|
|
|
|
}
|
|
|
|
|
|
|
|
return function->code();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static Object* Runtime_GetCalledFunction(Arguments args) {
|
|
|
|
HandleScope scope;
|
2008-08-13 09:32:07 +00:00
|
|
|
ASSERT(args.length() == 0);
|
2008-07-03 15:10:15 +00:00
|
|
|
StackFrameIterator it;
|
|
|
|
// Get past the JS-to-C exit frame.
|
|
|
|
ASSERT(it.frame()->is_exit());
|
|
|
|
it.Advance();
|
|
|
|
// Get past the CALL_NON_FUNCTION activation frame.
|
|
|
|
ASSERT(it.frame()->is_java_script());
|
|
|
|
it.Advance();
|
|
|
|
// Argument adaptor frames do not copy the function; we have to skip
|
|
|
|
// past them to get to the real calling frame.
|
|
|
|
if (it.frame()->is_arguments_adaptor()) it.Advance();
|
|
|
|
// Get the function from the top of the expression stack of the
|
|
|
|
// calling frame.
|
|
|
|
StandardFrame* frame = StandardFrame::cast(it.frame());
|
|
|
|
int index = frame->ComputeExpressionsCount() - 1;
|
|
|
|
Object* result = frame->GetExpression(index);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static Object* Runtime_GetFunctionDelegate(Arguments args) {
|
|
|
|
HandleScope scope;
|
|
|
|
ASSERT(args.length() == 1);
|
|
|
|
RUNTIME_ASSERT(!args[0]->IsJSFunction());
|
|
|
|
return *Execution::GetFunctionDelegate(args.at<Object>(0));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-05-20 19:33:44 +00:00
|
|
|
static Object* Runtime_GetConstructorDelegate(Arguments args) {
|
|
|
|
HandleScope scope;
|
|
|
|
ASSERT(args.length() == 1);
|
|
|
|
RUNTIME_ASSERT(!args[0]->IsJSFunction());
|
|
|
|
return *Execution::GetConstructorDelegate(args.at<Object>(0));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
static Object* Runtime_NewContext(Arguments args) {
|
|
|
|
NoHandleAllocation ha;
|
2008-07-30 08:49:36 +00:00
|
|
|
ASSERT(args.length() == 1);
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2008-07-30 08:49:36 +00:00
|
|
|
CONVERT_CHECKED(JSFunction, function, args[0]);
|
2008-07-03 15:10:15 +00:00
|
|
|
int length = ScopeInfo<>::NumberOfContextSlots(function->code());
|
|
|
|
Object* result = Heap::AllocateFunctionContext(length, function);
|
|
|
|
if (result->IsFailure()) return result;
|
|
|
|
|
|
|
|
Top::set_context(Context::cast(result));
|
|
|
|
|
2008-07-30 08:49:36 +00:00
|
|
|
return result; // non-failure
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
2008-12-18 11:28:13 +00:00
|
|
|
static Object* PushContextHelper(Object* object, bool is_catch_context) {
|
2008-07-03 15:10:15 +00:00
|
|
|
// Convert the object to a proper JavaScript object.
|
2008-12-18 11:28:13 +00:00
|
|
|
Object* js_object = object;
|
|
|
|
if (!js_object->IsJSObject()) {
|
|
|
|
js_object = js_object->ToObject();
|
|
|
|
if (js_object->IsFailure()) {
|
|
|
|
if (!Failure::cast(js_object)->IsInternalError()) return js_object;
|
2008-07-03 15:10:15 +00:00
|
|
|
HandleScope scope;
|
2008-12-18 11:28:13 +00:00
|
|
|
Handle<Object> handle(object);
|
2008-07-03 15:10:15 +00:00
|
|
|
Handle<Object> result =
|
|
|
|
Factory::NewTypeError("with_expression", HandleVector(&handle, 1));
|
|
|
|
return Top::Throw(*result);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Object* result =
|
2008-12-18 11:28:13 +00:00
|
|
|
Heap::AllocateWithContext(Top::context(),
|
|
|
|
JSObject::cast(js_object),
|
|
|
|
is_catch_context);
|
2008-07-03 15:10:15 +00:00
|
|
|
if (result->IsFailure()) return result;
|
|
|
|
|
2008-12-18 11:28:13 +00:00
|
|
|
Context* context = Context::cast(result);
|
|
|
|
Top::set_context(context);
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2008-07-30 08:49:36 +00:00
|
|
|
return result;
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-12-18 11:28:13 +00:00
|
|
|
static Object* Runtime_PushContext(Arguments args) {
|
|
|
|
NoHandleAllocation ha;
|
|
|
|
ASSERT(args.length() == 1);
|
|
|
|
return PushContextHelper(args[0], false);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static Object* Runtime_PushCatchContext(Arguments args) {
|
|
|
|
NoHandleAllocation ha;
|
|
|
|
ASSERT(args.length() == 1);
|
|
|
|
return PushContextHelper(args[0], true);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
static Object* Runtime_LookupContext(Arguments args) {
|
|
|
|
HandleScope scope;
|
|
|
|
ASSERT(args.length() == 2);
|
|
|
|
|
|
|
|
CONVERT_ARG_CHECKED(Context, context, 0);
|
|
|
|
CONVERT_ARG_CHECKED(String, name, 1);
|
|
|
|
|
|
|
|
int index;
|
|
|
|
PropertyAttributes attributes;
|
|
|
|
ContextLookupFlags flags = FOLLOW_CHAINS;
|
2008-10-23 08:42:22 +00:00
|
|
|
Handle<Object> holder =
|
2008-07-03 15:10:15 +00:00
|
|
|
context->Lookup(name, flags, &index, &attributes);
|
|
|
|
|
2008-10-24 10:59:40 +00:00
|
|
|
if (index < 0 && !holder.is_null()) {
|
2008-10-23 08:42:22 +00:00
|
|
|
ASSERT(holder->IsJSObject());
|
|
|
|
return *holder;
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// No intermediate context found. Use global object by default.
|
|
|
|
return Top::context()->global();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-09-08 11:52:05 +00:00
|
|
|
// A mechanism to return a pair of Object pointers in registers (if possible).
|
|
|
|
// How this is achieved is calling convention-dependent.
|
|
|
|
// All currently supported x86 compiles uses calling conventions that are cdecl
|
|
|
|
// variants where a 64-bit value is returned in two 32-bit registers
|
|
|
|
// (edx:eax on ia32, r1:r0 on ARM).
|
|
|
|
// In AMD-64 calling convention a struct of two pointers is returned in rdx:rax.
|
2009-09-08 12:51:08 +00:00
|
|
|
// In Win64 calling convention, a struct of two pointers is returned in memory,
|
|
|
|
// allocated by the caller, and passed as a pointer in a hidden first parameter.
|
2009-06-30 12:11:21 +00:00
|
|
|
#ifdef V8_HOST_ARCH_64_BIT
|
|
|
|
struct ObjectPair {
|
|
|
|
Object* x;
|
|
|
|
Object* y;
|
|
|
|
};
|
2009-09-08 11:52:05 +00:00
|
|
|
|
2008-10-24 10:59:40 +00:00
|
|
|
static inline ObjectPair MakePair(Object* x, Object* y) {
|
2009-06-30 12:11:21 +00:00
|
|
|
ObjectPair result = {x, y};
|
2009-09-08 11:52:05 +00:00
|
|
|
// Pointers x and y returned in rax and rdx, in AMD-x64-abi.
|
|
|
|
// In Win64 they are assigned to a hidden first argument.
|
|
|
|
return result;
|
2009-06-30 12:11:21 +00:00
|
|
|
}
|
2009-05-12 10:33:40 +00:00
|
|
|
#else
|
2009-06-30 12:11:21 +00:00
|
|
|
typedef uint64_t ObjectPair;
|
|
|
|
static inline ObjectPair MakePair(Object* x, Object* y) {
|
2008-07-03 15:10:15 +00:00
|
|
|
return reinterpret_cast<uint32_t>(x) |
|
2008-10-24 10:59:40 +00:00
|
|
|
(reinterpret_cast<ObjectPair>(y) << 32);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
2009-06-30 12:11:21 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2008-10-24 10:59:40 +00:00
|
|
|
static inline Object* Unhole(Object* x, PropertyAttributes attributes) {
|
2008-07-03 15:10:15 +00:00
|
|
|
ASSERT(!x->IsTheHole() || (attributes & READ_ONLY) != 0);
|
|
|
|
USE(attributes);
|
|
|
|
return x->IsTheHole() ? Heap::undefined_value() : x;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-10-24 10:59:40 +00:00
|
|
|
static JSObject* ComputeReceiverForNonGlobal(JSObject* holder) {
|
|
|
|
ASSERT(!holder->IsGlobalObject());
|
2008-10-23 14:55:45 +00:00
|
|
|
Context* top = Top::context();
|
2008-10-24 10:59:40 +00:00
|
|
|
// Get the context extension function.
|
2008-10-23 08:42:22 +00:00
|
|
|
JSFunction* context_extension_function =
|
|
|
|
top->global_context()->context_extension_function();
|
2008-10-24 10:59:40 +00:00
|
|
|
// If the holder isn't a context extension object, we just return it
|
|
|
|
// as the receiver. This allows arguments objects to be used as
|
|
|
|
// receivers, but only if they are put in the context scope chain
|
|
|
|
// explicitly via a with-statement.
|
|
|
|
Object* constructor = holder->map()->constructor();
|
|
|
|
if (constructor != context_extension_function) return holder;
|
|
|
|
// Fall back to using the global object as the receiver if the
|
|
|
|
// property turns out to be a local variable allocated in a context
|
|
|
|
// extension object - introduced via eval.
|
|
|
|
return top->global()->global_receiver();
|
2008-10-23 08:42:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-10-24 10:59:40 +00:00
|
|
|
static ObjectPair LoadContextSlotHelper(Arguments args, bool throw_error) {
|
2008-07-03 15:10:15 +00:00
|
|
|
HandleScope scope;
|
2009-09-04 11:00:07 +00:00
|
|
|
ASSERT_EQ(2, args.length());
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2008-10-24 10:59:40 +00:00
|
|
|
if (!args[0]->IsContext() || !args[1]->IsString()) {
|
2009-06-26 13:09:50 +00:00
|
|
|
return MakePair(Top::ThrowIllegalOperation(), NULL);
|
2008-10-24 10:59:40 +00:00
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
Handle<Context> context = args.at<Context>(0);
|
2008-10-24 10:59:40 +00:00
|
|
|
Handle<String> name = args.at<String>(1);
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
int index;
|
|
|
|
PropertyAttributes attributes;
|
|
|
|
ContextLookupFlags flags = FOLLOW_CHAINS;
|
2008-10-23 08:42:22 +00:00
|
|
|
Handle<Object> holder =
|
2008-07-03 15:10:15 +00:00
|
|
|
context->Lookup(name, flags, &index, &attributes);
|
|
|
|
|
2008-10-24 10:59:40 +00:00
|
|
|
// If the index is non-negative, the slot has been found in a local
|
|
|
|
// variable or a parameter. Read it from the context object or the
|
|
|
|
// arguments object.
|
2008-07-03 15:10:15 +00:00
|
|
|
if (index >= 0) {
|
2008-10-24 10:59:40 +00:00
|
|
|
// If the "property" we were looking for is a local variable or an
|
|
|
|
// argument in a context, the receiver is the global object; see
|
|
|
|
// ECMA-262, 3rd., 10.1.6 and 10.2.3.
|
|
|
|
JSObject* receiver = Top::context()->global()->global_receiver();
|
|
|
|
Object* value = (holder->IsContext())
|
|
|
|
? Context::cast(*holder)->get(index)
|
|
|
|
: JSObject::cast(*holder)->GetElement(index);
|
|
|
|
return MakePair(Unhole(value, attributes), receiver);
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the holder is found, we read the property from it.
|
|
|
|
if (!holder.is_null() && holder->IsJSObject()) {
|
2008-10-30 11:38:22 +00:00
|
|
|
ASSERT(Handle<JSObject>::cast(holder)->HasProperty(*name));
|
2008-10-24 10:59:40 +00:00
|
|
|
JSObject* object = JSObject::cast(*holder);
|
2008-12-18 11:28:13 +00:00
|
|
|
JSObject* receiver;
|
|
|
|
if (object->IsGlobalObject()) {
|
|
|
|
receiver = GlobalObject::cast(object)->global_receiver();
|
|
|
|
} else if (context->is_exception_holder(*holder)) {
|
|
|
|
receiver = Top::context()->global()->global_receiver();
|
|
|
|
} else {
|
|
|
|
receiver = ComputeReceiverForNonGlobal(object);
|
|
|
|
}
|
2008-10-24 10:59:40 +00:00
|
|
|
// No need to unhole the value here. This is taken care of by the
|
|
|
|
// GetProperty function.
|
|
|
|
Object* value = object->GetProperty(*name);
|
|
|
|
return MakePair(value, receiver);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (throw_error) {
|
|
|
|
// The property doesn't exist - throw exception.
|
|
|
|
Handle<Object> reference_error =
|
2008-08-27 10:11:39 +00:00
|
|
|
Factory::NewReferenceError("not_defined", HandleVector(&name, 1));
|
2008-07-03 15:10:15 +00:00
|
|
|
return MakePair(Top::Throw(*reference_error), NULL);
|
|
|
|
} else {
|
|
|
|
// The property doesn't exist - return undefined
|
|
|
|
return MakePair(Heap::undefined_value(), Heap::undefined_value());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-10-24 10:59:40 +00:00
|
|
|
static ObjectPair Runtime_LoadContextSlot(Arguments args) {
|
2008-07-03 15:10:15 +00:00
|
|
|
return LoadContextSlotHelper(args, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-10-24 10:59:40 +00:00
|
|
|
static ObjectPair Runtime_LoadContextSlotNoReferenceError(Arguments args) {
|
2008-07-03 15:10:15 +00:00
|
|
|
return LoadContextSlotHelper(args, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static Object* Runtime_StoreContextSlot(Arguments args) {
|
|
|
|
HandleScope scope;
|
|
|
|
ASSERT(args.length() == 3);
|
|
|
|
|
|
|
|
Handle<Object> value(args[0]);
|
|
|
|
CONVERT_ARG_CHECKED(Context, context, 1);
|
2008-08-27 10:11:39 +00:00
|
|
|
CONVERT_ARG_CHECKED(String, name, 2);
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
int index;
|
|
|
|
PropertyAttributes attributes;
|
|
|
|
ContextLookupFlags flags = FOLLOW_CHAINS;
|
2008-10-23 08:42:22 +00:00
|
|
|
Handle<Object> holder =
|
2008-07-03 15:10:15 +00:00
|
|
|
context->Lookup(name, flags, &index, &attributes);
|
|
|
|
|
|
|
|
if (index >= 0) {
|
2008-10-23 08:42:22 +00:00
|
|
|
if (holder->IsContext()) {
|
2008-07-03 15:10:15 +00:00
|
|
|
// Ignore if read_only variable.
|
|
|
|
if ((attributes & READ_ONLY) == 0) {
|
2008-10-23 08:42:22 +00:00
|
|
|
Handle<Context>::cast(holder)->set(index, *value);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ASSERT((attributes & READ_ONLY) == 0);
|
|
|
|
Object* result =
|
2008-10-23 08:42:22 +00:00
|
|
|
Handle<JSObject>::cast(holder)->SetElement(index, *value);
|
2008-07-03 15:10:15 +00:00
|
|
|
USE(result);
|
|
|
|
ASSERT(!result->IsFailure());
|
|
|
|
}
|
|
|
|
return *value;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Slow case: The property is not in a FixedArray context.
|
|
|
|
// It is either in an JSObject extension context or it was not found.
|
|
|
|
Handle<JSObject> context_ext;
|
|
|
|
|
2008-10-24 10:59:40 +00:00
|
|
|
if (!holder.is_null()) {
|
2008-07-03 15:10:15 +00:00
|
|
|
// The property exists in the extension context.
|
2008-10-23 08:42:22 +00:00
|
|
|
context_ext = Handle<JSObject>::cast(holder);
|
2008-07-03 15:10:15 +00:00
|
|
|
} else {
|
|
|
|
// The property was not found. It needs to be stored in the global context.
|
|
|
|
ASSERT(attributes == ABSENT);
|
|
|
|
attributes = NONE;
|
|
|
|
context_ext = Handle<JSObject>(Top::context()->global());
|
|
|
|
}
|
|
|
|
|
2009-03-24 08:27:43 +00:00
|
|
|
// Set the property, but ignore if read_only variable on the context
|
|
|
|
// extension object itself.
|
|
|
|
if ((attributes & READ_ONLY) == 0 ||
|
|
|
|
(context_ext->GetLocalPropertyAttribute(*name) == ABSENT)) {
|
2008-07-03 15:10:15 +00:00
|
|
|
Handle<Object> set = SetProperty(context_ext, name, value, attributes);
|
|
|
|
if (set.is_null()) {
|
|
|
|
// Failure::Exception is converted to a null handle in the
|
|
|
|
// handle-based methods such as SetProperty. We therefore need
|
|
|
|
// to convert null handles back to exceptions.
|
|
|
|
ASSERT(Top::has_pending_exception());
|
|
|
|
return Failure::Exception();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return *value;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static Object* Runtime_Throw(Arguments args) {
|
|
|
|
HandleScope scope;
|
|
|
|
ASSERT(args.length() == 1);
|
|
|
|
|
|
|
|
return Top::Throw(args[0]);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static Object* Runtime_ReThrow(Arguments args) {
|
|
|
|
HandleScope scope;
|
|
|
|
ASSERT(args.length() == 1);
|
|
|
|
|
|
|
|
return Top::ReThrow(args[0]);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static Object* Runtime_ThrowReferenceError(Arguments args) {
|
|
|
|
HandleScope scope;
|
|
|
|
ASSERT(args.length() == 1);
|
|
|
|
|
|
|
|
Handle<Object> name(args[0]);
|
|
|
|
Handle<Object> reference_error =
|
|
|
|
Factory::NewReferenceError("not_defined", HandleVector(&name, 1));
|
|
|
|
return Top::Throw(*reference_error);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static Object* Runtime_StackOverflow(Arguments args) {
|
|
|
|
NoHandleAllocation na;
|
|
|
|
return Top::StackOverflow();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static Object* Runtime_StackGuard(Arguments args) {
|
|
|
|
ASSERT(args.length() == 1);
|
|
|
|
|
|
|
|
// First check if this is a real stack overflow.
|
2009-01-26 18:09:46 +00:00
|
|
|
if (StackGuard::IsStackOverflow()) {
|
|
|
|
return Runtime_StackOverflow(args);
|
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2008-12-12 10:49:00 +00:00
|
|
|
return Execution::HandleStackGuardInterrupt();
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// NOTE: These PrintXXX functions are defined for all builds (not just
|
|
|
|
// DEBUG builds) because we may want to be able to trace function
|
|
|
|
// calls in all modes.
|
|
|
|
static void PrintString(String* str) {
|
|
|
|
// not uncommon to have empty strings
|
|
|
|
if (str->length() > 0) {
|
|
|
|
SmartPointer<char> s =
|
|
|
|
str->ToCString(DISALLOW_NULLS, ROBUST_STRING_TRAVERSAL);
|
|
|
|
PrintF("%s", *s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void PrintObject(Object* obj) {
|
|
|
|
if (obj->IsSmi()) {
|
|
|
|
PrintF("%d", Smi::cast(obj)->value());
|
|
|
|
} else if (obj->IsString() || obj->IsSymbol()) {
|
|
|
|
PrintString(String::cast(obj));
|
|
|
|
} else if (obj->IsNumber()) {
|
|
|
|
PrintF("%g", obj->Number());
|
|
|
|
} else if (obj->IsFailure()) {
|
|
|
|
PrintF("<failure>");
|
|
|
|
} else if (obj->IsUndefined()) {
|
|
|
|
PrintF("<undefined>");
|
|
|
|
} else if (obj->IsNull()) {
|
|
|
|
PrintF("<null>");
|
|
|
|
} else if (obj->IsTrue()) {
|
|
|
|
PrintF("<true>");
|
|
|
|
} else if (obj->IsFalse()) {
|
|
|
|
PrintF("<false>");
|
|
|
|
} else {
|
|
|
|
PrintF("%p", obj);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int StackSize() {
|
|
|
|
int n = 0;
|
|
|
|
for (JavaScriptFrameIterator it; !it.done(); it.Advance()) n++;
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void PrintTransition(Object* result) {
|
|
|
|
// indentation
|
|
|
|
{ const int nmax = 80;
|
|
|
|
int n = StackSize();
|
|
|
|
if (n <= nmax)
|
|
|
|
PrintF("%4d:%*s", n, n, "");
|
|
|
|
else
|
|
|
|
PrintF("%4d:%*s", n, nmax, "...");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (result == NULL) {
|
|
|
|
// constructor calls
|
|
|
|
JavaScriptFrameIterator it;
|
|
|
|
JavaScriptFrame* frame = it.frame();
|
|
|
|
if (frame->IsConstructor()) PrintF("new ");
|
|
|
|
// function name
|
|
|
|
Object* fun = frame->function();
|
|
|
|
if (fun->IsJSFunction()) {
|
|
|
|
PrintObject(JSFunction::cast(fun)->shared()->name());
|
|
|
|
} else {
|
|
|
|
PrintObject(fun);
|
|
|
|
}
|
|
|
|
// function arguments
|
|
|
|
// (we are intentionally only printing the actually
|
|
|
|
// supplied parameters, not all parameters required)
|
|
|
|
PrintF("(this=");
|
|
|
|
PrintObject(frame->receiver());
|
|
|
|
const int length = frame->GetProvidedParametersCount();
|
|
|
|
for (int i = 0; i < length; i++) {
|
|
|
|
PrintF(", ");
|
|
|
|
PrintObject(frame->GetParameter(i));
|
|
|
|
}
|
|
|
|
PrintF(") {\n");
|
|
|
|
|
|
|
|
} else {
|
|
|
|
// function result
|
|
|
|
PrintF("} -> ");
|
|
|
|
PrintObject(result);
|
|
|
|
PrintF("\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static Object* Runtime_TraceEnter(Arguments args) {
|
2008-10-24 10:13:22 +00:00
|
|
|
ASSERT(args.length() == 0);
|
2008-07-03 15:10:15 +00:00
|
|
|
NoHandleAllocation ha;
|
|
|
|
PrintTransition(NULL);
|
2008-10-24 10:13:22 +00:00
|
|
|
return Heap::undefined_value();
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static Object* Runtime_TraceExit(Arguments args) {
|
|
|
|
NoHandleAllocation ha;
|
|
|
|
PrintTransition(args[0]);
|
|
|
|
return args[0]; // return TOS
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static Object* Runtime_DebugPrint(Arguments args) {
|
|
|
|
NoHandleAllocation ha;
|
|
|
|
ASSERT(args.length() == 1);
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
if (args[0]->IsString()) {
|
|
|
|
// If we have a string, assume it's a code "marker"
|
|
|
|
// and print some interesting cpu debugging info.
|
|
|
|
JavaScriptFrameIterator it;
|
|
|
|
JavaScriptFrame* frame = it.frame();
|
2009-06-10 15:08:25 +00:00
|
|
|
PrintF("fp = %p, sp = %p, caller_sp = %p: ",
|
|
|
|
frame->fp(), frame->sp(), frame->caller_sp());
|
2008-07-03 15:10:15 +00:00
|
|
|
} else {
|
|
|
|
PrintF("DebugPrint: ");
|
|
|
|
}
|
|
|
|
args[0]->Print();
|
|
|
|
#else
|
2008-09-10 10:32:33 +00:00
|
|
|
// ShortPrint is available in release mode. Print is not.
|
|
|
|
args[0]->ShortPrint();
|
2008-07-03 15:10:15 +00:00
|
|
|
#endif
|
|
|
|
PrintF("\n");
|
2008-09-18 07:31:37 +00:00
|
|
|
Flush();
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
return args[0]; // return TOS
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static Object* Runtime_DebugTrace(Arguments args) {
|
2008-10-24 10:13:22 +00:00
|
|
|
ASSERT(args.length() == 0);
|
2008-07-03 15:10:15 +00:00
|
|
|
NoHandleAllocation ha;
|
|
|
|
Top::PrintStack();
|
2008-10-24 10:13:22 +00:00
|
|
|
return Heap::undefined_value();
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-08-13 09:32:07 +00:00
|
|
|
static Object* Runtime_DateCurrentTime(Arguments args) {
|
2008-07-03 15:10:15 +00:00
|
|
|
NoHandleAllocation ha;
|
2008-08-13 09:32:07 +00:00
|
|
|
ASSERT(args.length() == 0);
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
// According to ECMA-262, section 15.9.1, page 117, the precision of
|
|
|
|
// the number in a Date object representing a particular instant in
|
|
|
|
// time is milliseconds. Therefore, we floor the result of getting
|
|
|
|
// the OS time.
|
|
|
|
double millis = floor(OS::TimeCurrentMillis());
|
|
|
|
return Heap::NumberFromDouble(millis);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static Object* Runtime_DateParseString(Arguments args) {
|
|
|
|
HandleScope scope;
|
2009-03-19 09:40:38 +00:00
|
|
|
ASSERT(args.length() == 2);
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2009-03-19 09:40:38 +00:00
|
|
|
CONVERT_ARG_CHECKED(String, str, 0);
|
2009-03-17 13:27:21 +00:00
|
|
|
FlattenString(str);
|
2009-03-19 09:40:38 +00:00
|
|
|
|
|
|
|
CONVERT_ARG_CHECKED(JSArray, output, 1);
|
|
|
|
RUNTIME_ASSERT(output->HasFastElements());
|
|
|
|
|
|
|
|
AssertNoAllocation no_allocation;
|
|
|
|
|
2009-07-28 08:43:51 +00:00
|
|
|
FixedArray* output_array = FixedArray::cast(output->elements());
|
2009-03-19 09:40:38 +00:00
|
|
|
RUNTIME_ASSERT(output_array->length() >= DateParser::OUTPUT_SIZE);
|
2009-03-17 13:27:21 +00:00
|
|
|
bool result;
|
2009-05-01 11:16:29 +00:00
|
|
|
if (str->IsAsciiRepresentation()) {
|
2009-03-19 09:40:38 +00:00
|
|
|
result = DateParser::Parse(str->ToAsciiVector(), output_array);
|
|
|
|
} else {
|
2009-05-01 11:16:29 +00:00
|
|
|
ASSERT(str->IsTwoByteRepresentation());
|
2009-03-19 09:40:38 +00:00
|
|
|
result = DateParser::Parse(str->ToUC16Vector(), output_array);
|
2009-03-17 13:27:21 +00:00
|
|
|
}
|
2009-03-19 09:40:38 +00:00
|
|
|
|
2009-03-17 13:27:21 +00:00
|
|
|
if (result) {
|
2009-03-19 09:40:38 +00:00
|
|
|
return *output;
|
2008-07-03 15:10:15 +00:00
|
|
|
} else {
|
2009-03-19 09:40:38 +00:00
|
|
|
return Heap::null_value();
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static Object* Runtime_DateLocalTimezone(Arguments args) {
|
|
|
|
NoHandleAllocation ha;
|
|
|
|
ASSERT(args.length() == 1);
|
|
|
|
|
|
|
|
CONVERT_DOUBLE_CHECKED(x, args[0]);
|
2009-07-31 13:24:40 +00:00
|
|
|
const char* zone = OS::LocalTimezone(x);
|
2008-07-03 15:10:15 +00:00
|
|
|
return Heap::AllocateStringFromUtf8(CStrVector(zone));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static Object* Runtime_DateLocalTimeOffset(Arguments args) {
|
|
|
|
NoHandleAllocation ha;
|
2008-08-13 09:32:07 +00:00
|
|
|
ASSERT(args.length() == 0);
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
return Heap::NumberFromDouble(OS::LocalTimeOffset());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static Object* Runtime_DateDaylightSavingsOffset(Arguments args) {
|
|
|
|
NoHandleAllocation ha;
|
|
|
|
ASSERT(args.length() == 1);
|
|
|
|
|
|
|
|
CONVERT_DOUBLE_CHECKED(x, args[0]);
|
|
|
|
return Heap::NumberFromDouble(OS::DaylightSavingsOffset(x));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static Object* Runtime_NumberIsFinite(Arguments args) {
|
|
|
|
NoHandleAllocation ha;
|
|
|
|
ASSERT(args.length() == 1);
|
|
|
|
|
|
|
|
CONVERT_DOUBLE_CHECKED(value, args[0]);
|
|
|
|
Object* result;
|
|
|
|
if (isnan(value) || (fpclassify(value) == FP_INFINITE)) {
|
|
|
|
result = Heap::false_value();
|
|
|
|
} else {
|
|
|
|
result = Heap::true_value();
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-11-21 15:01:41 +00:00
|
|
|
static Object* Runtime_GlobalReceiver(Arguments args) {
|
|
|
|
ASSERT(args.length() == 1);
|
|
|
|
Object* global = args[0];
|
|
|
|
if (!global->IsJSGlobalObject()) return Heap::null_value();
|
|
|
|
return JSGlobalObject::cast(global)->global_receiver();
|
Split window support from V8.
Here is a description of the background and design of split window in Chrome and V8:
https://docs.google.com/a/google.com/Doc?id=chhjkpg_47fwddxbfr
This change list splits the window object into two parts: 1) an inner window object used as the global object of contexts; 2) an outer window object exposed to JavaScript and accessible by the name 'window'. Firefox did it awhile ago, here are some discussions: https://wiki.mozilla.org/Gecko:SplitWindow. One additional benefit of splitting window in Chrome is that accessing global variables don't need security checks anymore, it can improve applications that use many global variables.
V8 support of split window:
There are a small number of changes on V8 api to support split window:
Security context is removed from V8, so does related API functions;
A global object can be detached from its context and reused by a new context;
Access checks on an object template can be turned on/off by default;
An object can turn on its access checks later;
V8 has a new object type, ApiGlobalObject, which is the outer window object type. The existing JSGlobalObject becomes the inner window object type. Security checks are moved from JSGlobalObject to ApiGlobalObject. ApiGlobalObject is the one exposed to JavaScript, it is accessible through Context::Global(). ApiGlobalObject's prototype is set to JSGlobalObject so that property lookups are forwarded to JSGlobalObject. ApiGlobalObject forwards all other property access requests to JSGlobalObject, such as SetProperty, DeleteProperty, etc.
Security token is moved to a global context, and ApiGlobalObject has a reference to its global context. JSGlobalObject has a reference to its global context as well. When accessing properties on a global object in JavaScript, the domain security check is performed by comparing the security token of the lexical context (Top::global_context()) to the token of global object's context. The check is only needed when the receiver is a window object, such as 'window.document'. Accessing global variables, such as 'var foo = 3; foo' does not need checks because the receiver is the inner window object.
When an outer window is detached from its global context (when a frame navigates away from a page), it is completely detached from the inner window. A new context is created for the new page, and the outer global object is reused. At this point, the access check on the DOMWindow wrapper of the old context is turned on. The code in old context is still able to access DOMWindow properties, but it has to go through domain security checks.
It is debatable on how to implement the outer window object. Currently each property access function has to check if the receiver is ApiGlobalObject type. This approach might be error-prone that one may forget to check the receiver when adding new functions. It is unlikely a performance issue because accessing global variables are more common than 'window.foo' style coding.
I am still working on the ARM port, and I'd like to hear comments and suggestions on the best way to support it in V8.
Review URL: http://codereview.chromium.org/7366
git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@540 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
2008-10-21 19:07:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-11-21 15:01:41 +00:00
|
|
|
static Object* Runtime_CompileString(Arguments args) {
|
|
|
|
HandleScope scope;
|
2009-05-18 13:16:48 +00:00
|
|
|
ASSERT_EQ(2, args.length());
|
2008-11-21 15:01:41 +00:00
|
|
|
CONVERT_ARG_CHECKED(String, source, 0);
|
2009-05-18 13:16:48 +00:00
|
|
|
CONVERT_ARG_CHECKED(Oddball, is_json, 1)
|
2008-11-21 15:01:41 +00:00
|
|
|
|
2009-02-24 13:11:53 +00:00
|
|
|
// Compile source string in the global context.
|
|
|
|
Handle<Context> context(Top::context()->global_context());
|
2009-08-13 10:25:35 +00:00
|
|
|
Compiler::ValidationState validate = (is_json->IsTrue())
|
|
|
|
? Compiler::VALIDATE_JSON : Compiler::DONT_VALIDATE_JSON;
|
2009-04-24 12:45:29 +00:00
|
|
|
Handle<JSFunction> boilerplate = Compiler::CompileEval(source,
|
|
|
|
context,
|
|
|
|
true,
|
2009-08-13 10:25:35 +00:00
|
|
|
validate);
|
2008-11-21 15:01:41 +00:00
|
|
|
if (boilerplate.is_null()) return Failure::Exception();
|
|
|
|
Handle<JSFunction> fun =
|
|
|
|
Factory::NewFunctionFromBoilerplate(boilerplate, context);
|
|
|
|
return *fun;
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-11-27 13:55:06 +00:00
|
|
|
static Handle<JSFunction> GetBuiltinFunction(String* name) {
|
|
|
|
LookupResult result;
|
|
|
|
Top::global_context()->builtins()->LocalLookup(name, &result);
|
|
|
|
return Handle<JSFunction>(JSFunction::cast(result.GetValue()));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static Object* CompileDirectEval(Handle<String> source) {
|
|
|
|
// Compute the eval context.
|
|
|
|
HandleScope scope;
|
|
|
|
StackFrameLocator locator;
|
|
|
|
JavaScriptFrame* frame = locator.FindJavaScriptFrame(0);
|
|
|
|
Handle<Context> context(Context::cast(frame->context()));
|
|
|
|
bool is_global = context->IsGlobalContext();
|
|
|
|
|
2009-02-24 13:11:53 +00:00
|
|
|
// Compile source string in the current context.
|
2009-08-13 10:25:35 +00:00
|
|
|
Handle<JSFunction> boilerplate = Compiler::CompileEval(
|
|
|
|
source,
|
|
|
|
context,
|
|
|
|
is_global,
|
|
|
|
Compiler::DONT_VALIDATE_JSON);
|
2008-11-27 13:55:06 +00:00
|
|
|
if (boilerplate.is_null()) return Failure::Exception();
|
|
|
|
Handle<JSFunction> fun =
|
|
|
|
Factory::NewFunctionFromBoilerplate(boilerplate, context);
|
|
|
|
return *fun;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static Object* Runtime_ResolvePossiblyDirectEval(Arguments args) {
|
|
|
|
ASSERT(args.length() == 2);
|
|
|
|
|
|
|
|
HandleScope scope;
|
|
|
|
|
|
|
|
CONVERT_ARG_CHECKED(JSFunction, callee, 0);
|
|
|
|
|
|
|
|
Handle<Object> receiver;
|
|
|
|
|
|
|
|
// Find where the 'eval' symbol is bound. It is unaliased only if
|
|
|
|
// it is bound in the global context.
|
|
|
|
StackFrameLocator locator;
|
|
|
|
JavaScriptFrame* frame = locator.FindJavaScriptFrame(0);
|
|
|
|
Handle<Context> context(Context::cast(frame->context()));
|
|
|
|
int index;
|
|
|
|
PropertyAttributes attributes;
|
|
|
|
while (!context.is_null()) {
|
|
|
|
receiver = context->Lookup(Factory::eval_symbol(), FOLLOW_PROTOTYPE_CHAIN,
|
|
|
|
&index, &attributes);
|
2009-02-02 13:18:20 +00:00
|
|
|
// Stop search when eval is found or when the global context is
|
|
|
|
// reached.
|
|
|
|
if (attributes != ABSENT || context->IsGlobalContext()) break;
|
2008-11-27 13:55:06 +00:00
|
|
|
if (context->is_function_context()) {
|
|
|
|
context = Handle<Context>(Context::cast(context->closure()->context()));
|
|
|
|
} else {
|
|
|
|
context = Handle<Context>(context->previous());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-02-02 13:18:20 +00:00
|
|
|
// If eval could not be resolved, it has been deleted and we need to
|
|
|
|
// throw a reference error.
|
|
|
|
if (attributes == ABSENT) {
|
|
|
|
Handle<Object> name = Factory::eval_symbol();
|
|
|
|
Handle<Object> reference_error =
|
|
|
|
Factory::NewReferenceError("not_defined", HandleVector(&name, 1));
|
|
|
|
return Top::Throw(*reference_error);
|
|
|
|
}
|
|
|
|
|
2008-11-27 13:55:06 +00:00
|
|
|
if (context->IsGlobalContext()) {
|
|
|
|
// 'eval' is bound in the global context, but it may have been overwritten.
|
|
|
|
// Compare it to the builtin 'GlobalEval' function to make sure.
|
|
|
|
Handle<JSFunction> global_eval =
|
|
|
|
GetBuiltinFunction(Heap::global_eval_symbol());
|
|
|
|
if (global_eval.is_identical_to(callee)) {
|
|
|
|
// A direct eval call.
|
|
|
|
if (args[1]->IsString()) {
|
|
|
|
CONVERT_ARG_CHECKED(String, source, 1);
|
|
|
|
// A normal eval call on a string. Compile it and return the
|
|
|
|
// compiled function bound in the local context.
|
|
|
|
Object* compiled_source = CompileDirectEval(source);
|
|
|
|
if (compiled_source->IsFailure()) return compiled_source;
|
|
|
|
receiver = Handle<Object>(frame->receiver());
|
|
|
|
callee = Handle<JSFunction>(JSFunction::cast(compiled_source));
|
|
|
|
} else {
|
|
|
|
// An eval call that is not called on a string. Global eval
|
|
|
|
// deals better with this.
|
|
|
|
receiver = Handle<Object>(Top::global_context()->global());
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// 'eval' is overwritten. Just call the function with the given arguments.
|
|
|
|
receiver = Handle<Object>(Top::global_context()->global());
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// 'eval' is not bound in the global context. Just call the function
|
|
|
|
// with the given arguments. This is not necessarily the global eval.
|
|
|
|
if (receiver->IsContext()) {
|
|
|
|
context = Handle<Context>::cast(receiver);
|
|
|
|
receiver = Handle<Object>(context->get(index));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Handle<FixedArray> call = Factory::NewFixedArray(2);
|
|
|
|
call->set(0, *callee);
|
|
|
|
call->set(1, *receiver);
|
|
|
|
return *call;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
static Object* Runtime_SetNewFunctionAttributes(Arguments args) {
|
|
|
|
// This utility adjusts the property attributes for newly created Function
|
|
|
|
// object ("new Function(...)") by changing the map.
|
|
|
|
// All it does is changing the prototype property to enumerable
|
|
|
|
// as specified in ECMA262, 15.3.5.2.
|
|
|
|
HandleScope scope;
|
|
|
|
ASSERT(args.length() == 1);
|
|
|
|
CONVERT_ARG_CHECKED(JSFunction, func, 0);
|
|
|
|
ASSERT(func->map()->instance_type() ==
|
|
|
|
Top::function_instance_map()->instance_type());
|
|
|
|
ASSERT(func->map()->instance_size() ==
|
|
|
|
Top::function_instance_map()->instance_size());
|
|
|
|
func->set_map(*Top::function_instance_map());
|
|
|
|
return *func;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-09-09 07:38:00 +00:00
|
|
|
// Push an array unto an array of arrays if it is not already in the
|
|
|
|
// array. Returns true if the element was pushed on the stack and
|
|
|
|
// false otherwise.
|
|
|
|
static Object* Runtime_PushIfAbsent(Arguments args) {
|
2008-09-09 08:06:27 +00:00
|
|
|
ASSERT(args.length() == 2);
|
2008-09-09 07:38:00 +00:00
|
|
|
CONVERT_CHECKED(JSArray, array, args[0]);
|
|
|
|
CONVERT_CHECKED(JSArray, element, args[1]);
|
2008-09-09 08:40:25 +00:00
|
|
|
RUNTIME_ASSERT(array->HasFastElements());
|
2008-09-09 07:38:00 +00:00
|
|
|
int length = Smi::cast(array->length())->value();
|
|
|
|
FixedArray* elements = FixedArray::cast(array->elements());
|
|
|
|
for (int i = 0; i < length; i++) {
|
|
|
|
if (elements->get(i) == element) return Heap::false_value();
|
|
|
|
}
|
|
|
|
Object* obj = array->SetFastElement(length, element);
|
|
|
|
if (obj->IsFailure()) return obj;
|
|
|
|
return Heap::true_value();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-10-28 14:47:50 +00:00
|
|
|
/**
|
|
|
|
* A simple visitor visits every element of Array's.
|
|
|
|
* The backend storage can be a fixed array for fast elements case,
|
|
|
|
* or a dictionary for sparse array. Since Dictionary is a subtype
|
|
|
|
* of FixedArray, the class can be used by both fast and slow cases.
|
|
|
|
* The second parameter of the constructor, fast_elements, specifies
|
|
|
|
* whether the storage is a FixedArray or Dictionary.
|
2008-10-28 17:23:10 +00:00
|
|
|
*
|
|
|
|
* An index limit is used to deal with the situation that a result array
|
|
|
|
* length overflows 32-bit non-negative integer.
|
2008-10-28 14:47:50 +00:00
|
|
|
*/
|
|
|
|
class ArrayConcatVisitor {
|
|
|
|
public:
|
2008-10-28 17:23:10 +00:00
|
|
|
ArrayConcatVisitor(Handle<FixedArray> storage,
|
|
|
|
uint32_t index_limit,
|
|
|
|
bool fast_elements) :
|
|
|
|
storage_(storage), index_limit_(index_limit),
|
|
|
|
fast_elements_(fast_elements), index_offset_(0) { }
|
2008-10-28 14:47:50 +00:00
|
|
|
|
|
|
|
void visit(uint32_t i, Handle<Object> elm) {
|
|
|
|
uint32_t index = i + index_offset_;
|
2008-10-28 17:23:10 +00:00
|
|
|
if (index >= index_limit_) return;
|
2008-10-28 17:32:01 +00:00
|
|
|
|
2008-10-28 14:47:50 +00:00
|
|
|
if (fast_elements_) {
|
|
|
|
ASSERT(index < static_cast<uint32_t>(storage_->length()));
|
|
|
|
storage_->set(index, *elm);
|
|
|
|
|
|
|
|
} else {
|
2009-07-02 06:50:43 +00:00
|
|
|
Handle<NumberDictionary> dict = Handle<NumberDictionary>::cast(storage_);
|
|
|
|
Handle<NumberDictionary> result =
|
2008-10-28 14:47:50 +00:00
|
|
|
Factory::DictionaryAtNumberPut(dict, index, elm);
|
|
|
|
if (!result.is_identical_to(dict))
|
|
|
|
storage_ = result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void increase_index_offset(uint32_t delta) {
|
|
|
|
index_offset_ += delta;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
Handle<FixedArray> storage_;
|
2008-10-28 17:23:10 +00:00
|
|
|
uint32_t index_limit_;
|
2008-10-28 14:47:50 +00:00
|
|
|
bool fast_elements_;
|
|
|
|
uint32_t index_offset_;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A helper function that visits elements of a JSObject. Only elements
|
|
|
|
* whose index between 0 and range (exclusive) are visited.
|
|
|
|
*
|
|
|
|
* If the third parameter, visitor, is not NULL, the visitor is called
|
|
|
|
* with parameters, 'visitor_index_offset + element index' and the element.
|
|
|
|
*
|
|
|
|
* It returns the number of visisted elements.
|
|
|
|
*/
|
|
|
|
static uint32_t IterateElements(Handle<JSObject> receiver,
|
|
|
|
uint32_t range,
|
|
|
|
ArrayConcatVisitor* visitor) {
|
|
|
|
uint32_t num_of_elements = 0;
|
|
|
|
|
2009-07-28 08:43:51 +00:00
|
|
|
switch (receiver->GetElementsKind()) {
|
|
|
|
case JSObject::FAST_ELEMENTS: {
|
|
|
|
Handle<FixedArray> elements(FixedArray::cast(receiver->elements()));
|
|
|
|
uint32_t len = elements->length();
|
|
|
|
if (range < len) {
|
|
|
|
len = range;
|
|
|
|
}
|
2009-07-28 09:05:05 +00:00
|
|
|
|
2009-07-28 08:43:51 +00:00
|
|
|
for (uint32_t j = 0; j < len; j++) {
|
|
|
|
Handle<Object> e(elements->get(j));
|
|
|
|
if (!e->IsTheHole()) {
|
|
|
|
num_of_elements++;
|
|
|
|
if (visitor) {
|
|
|
|
visitor->visit(j, e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case JSObject::PIXEL_ELEMENTS: {
|
|
|
|
Handle<PixelArray> pixels(PixelArray::cast(receiver->elements()));
|
|
|
|
uint32_t len = pixels->length();
|
|
|
|
if (range < len) {
|
|
|
|
len = range;
|
|
|
|
}
|
2008-10-28 14:47:50 +00:00
|
|
|
|
2009-07-28 08:43:51 +00:00
|
|
|
for (uint32_t j = 0; j < len; j++) {
|
2008-10-28 14:47:50 +00:00
|
|
|
num_of_elements++;
|
2009-07-28 08:43:51 +00:00
|
|
|
if (visitor != NULL) {
|
|
|
|
Handle<Smi> e(Smi::FromInt(pixels->get(j)));
|
2008-10-28 14:47:50 +00:00
|
|
|
visitor->visit(j, e);
|
2009-07-28 08:43:51 +00:00
|
|
|
}
|
2008-10-28 14:47:50 +00:00
|
|
|
}
|
2009-07-28 08:43:51 +00:00
|
|
|
break;
|
2008-10-28 14:47:50 +00:00
|
|
|
}
|
2009-07-28 08:43:51 +00:00
|
|
|
case JSObject::DICTIONARY_ELEMENTS: {
|
|
|
|
Handle<NumberDictionary> dict(receiver->element_dictionary());
|
|
|
|
uint32_t capacity = dict->Capacity();
|
|
|
|
for (uint32_t j = 0; j < capacity; j++) {
|
|
|
|
Handle<Object> k(dict->KeyAt(j));
|
|
|
|
if (dict->IsKey(*k)) {
|
|
|
|
ASSERT(k->IsNumber());
|
|
|
|
uint32_t index = static_cast<uint32_t>(k->Number());
|
|
|
|
if (index < range) {
|
|
|
|
num_of_elements++;
|
|
|
|
if (visitor) {
|
|
|
|
visitor->visit(index, Handle<Object>(dict->ValueAt(j)));
|
|
|
|
}
|
2008-10-28 14:47:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-07-28 08:43:51 +00:00
|
|
|
break;
|
2008-10-28 14:47:50 +00:00
|
|
|
}
|
2009-07-28 08:43:51 +00:00
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
break;
|
2008-10-28 14:47:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return num_of_elements;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A helper function that visits elements of an Array object, and elements
|
|
|
|
* on its prototypes.
|
|
|
|
*
|
|
|
|
* Elements on prototypes are visited first, and only elements whose indices
|
|
|
|
* less than Array length are visited.
|
|
|
|
*
|
2008-10-28 14:54:52 +00:00
|
|
|
* If a ArrayConcatVisitor object is given, the visitor is called with
|
2008-10-28 14:47:50 +00:00
|
|
|
* parameters, element's index + visitor_index_offset and the element.
|
|
|
|
*/
|
|
|
|
static uint32_t IterateArrayAndPrototypeElements(Handle<JSArray> array,
|
|
|
|
ArrayConcatVisitor* visitor) {
|
|
|
|
uint32_t range = static_cast<uint32_t>(array->length()->Number());
|
|
|
|
Handle<Object> obj = array;
|
|
|
|
|
|
|
|
static const int kEstimatedPrototypes = 3;
|
|
|
|
List< Handle<JSObject> > objects(kEstimatedPrototypes);
|
|
|
|
|
|
|
|
// Visit prototype first. If an element on the prototype is shadowed by
|
|
|
|
// the inheritor using the same index, the ArrayConcatVisitor visits
|
|
|
|
// the prototype element before the shadowing element.
|
|
|
|
// The visitor can simply overwrite the old value by new value using
|
|
|
|
// the same index. This follows Array::concat semantics.
|
2008-10-28 14:54:52 +00:00
|
|
|
while (!obj->IsNull()) {
|
2008-10-28 16:27:09 +00:00
|
|
|
objects.Add(Handle<JSObject>::cast(obj));
|
2008-10-28 14:47:50 +00:00
|
|
|
obj = Handle<Object>(obj->GetPrototype());
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t nof_elements = 0;
|
|
|
|
for (int i = objects.length() - 1; i >= 0; i--) {
|
|
|
|
Handle<JSObject> obj = objects[i];
|
|
|
|
nof_elements +=
|
|
|
|
IterateElements(Handle<JSObject>::cast(obj), range, visitor);
|
|
|
|
}
|
|
|
|
|
2008-10-28 14:54:52 +00:00
|
|
|
return nof_elements;
|
2008-10-28 14:47:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A helper function of Runtime_ArrayConcat.
|
|
|
|
*
|
2008-10-29 10:02:09 +00:00
|
|
|
* The first argument is an Array of arrays and objects. It is the
|
|
|
|
* same as the arguments array of Array::concat JS function.
|
2008-10-28 14:47:50 +00:00
|
|
|
*
|
2008-10-29 10:02:09 +00:00
|
|
|
* If an argument is an Array object, the function visits array
|
|
|
|
* elements. If an argument is not an Array object, the function
|
|
|
|
* visits the object as if it is an one-element array.
|
2008-10-28 17:23:10 +00:00
|
|
|
*
|
2008-10-29 10:02:09 +00:00
|
|
|
* If the result array index overflows 32-bit integer, the rounded
|
|
|
|
* non-negative number is used as new length. For example, if one
|
|
|
|
* array length is 2^32 - 1, second array length is 1, the
|
|
|
|
* concatenated array length is 0.
|
2008-10-28 14:47:50 +00:00
|
|
|
*/
|
|
|
|
static uint32_t IterateArguments(Handle<JSArray> arguments,
|
|
|
|
ArrayConcatVisitor* visitor) {
|
|
|
|
uint32_t visited_elements = 0;
|
|
|
|
uint32_t num_of_args = static_cast<uint32_t>(arguments->length()->Number());
|
|
|
|
|
|
|
|
for (uint32_t i = 0; i < num_of_args; i++) {
|
|
|
|
Handle<Object> obj(arguments->GetElement(i));
|
|
|
|
if (obj->IsJSArray()) {
|
|
|
|
Handle<JSArray> array = Handle<JSArray>::cast(obj);
|
|
|
|
uint32_t len = static_cast<uint32_t>(array->length()->Number());
|
|
|
|
uint32_t nof_elements =
|
|
|
|
IterateArrayAndPrototypeElements(array, visitor);
|
|
|
|
// Total elements of array and its prototype chain can be more than
|
|
|
|
// the array length, but ArrayConcat can only concatenate at most
|
|
|
|
// the array length number of elements.
|
|
|
|
visited_elements += (nof_elements > len) ? len : nof_elements;
|
|
|
|
if (visitor) visitor->increase_index_offset(len);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
if (visitor) {
|
|
|
|
visitor->visit(0, obj);
|
|
|
|
visitor->increase_index_offset(1);
|
|
|
|
}
|
|
|
|
visited_elements++;
|
2008-10-28 14:54:52 +00:00
|
|
|
}
|
2008-10-28 14:47:50 +00:00
|
|
|
}
|
|
|
|
return visited_elements;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Array::concat implementation.
|
|
|
|
* See ECMAScript 262, 15.4.4.4.
|
|
|
|
*/
|
|
|
|
static Object* Runtime_ArrayConcat(Arguments args) {
|
|
|
|
ASSERT(args.length() == 1);
|
|
|
|
HandleScope handle_scope;
|
|
|
|
|
|
|
|
CONVERT_CHECKED(JSArray, arg_arrays, args[0]);
|
|
|
|
Handle<JSArray> arguments(arg_arrays);
|
|
|
|
|
|
|
|
// Pass 1: estimate the number of elements of the result
|
|
|
|
// (it could be more than real numbers if prototype has elements).
|
|
|
|
uint32_t result_length = 0;
|
|
|
|
uint32_t num_of_args = static_cast<uint32_t>(arguments->length()->Number());
|
|
|
|
|
|
|
|
{ AssertNoAllocation nogc;
|
|
|
|
for (uint32_t i = 0; i < num_of_args; i++) {
|
|
|
|
Object* obj = arguments->GetElement(i);
|
|
|
|
if (obj->IsJSArray()) {
|
|
|
|
result_length +=
|
|
|
|
static_cast<uint32_t>(JSArray::cast(obj)->length()->Number());
|
|
|
|
} else {
|
|
|
|
result_length++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Allocate an empty array, will set length and content later.
|
|
|
|
Handle<JSArray> result = Factory::NewJSArray(0);
|
|
|
|
|
|
|
|
uint32_t estimate_nof_elements = IterateArguments(arguments, NULL);
|
2008-10-29 10:02:09 +00:00
|
|
|
// If estimated number of elements is more than half of length, a
|
|
|
|
// fixed array (fast case) is more time and space-efficient than a
|
|
|
|
// dictionary.
|
2008-10-28 14:47:50 +00:00
|
|
|
bool fast_case = (estimate_nof_elements * 2) >= result_length;
|
|
|
|
|
|
|
|
Handle<FixedArray> storage;
|
|
|
|
if (fast_case) {
|
2008-10-29 10:02:09 +00:00
|
|
|
// The backing storage array must have non-existing elements to
|
|
|
|
// preserve holes across concat operations.
|
|
|
|
storage = Factory::NewFixedArrayWithHoles(result_length);
|
2008-10-28 14:47:50 +00:00
|
|
|
|
|
|
|
} else {
|
|
|
|
// TODO(126): move 25% pre-allocation logic into Dictionary::Allocate
|
|
|
|
uint32_t at_least_space_for = estimate_nof_elements +
|
|
|
|
(estimate_nof_elements >> 2);
|
|
|
|
storage = Handle<FixedArray>::cast(
|
2009-07-02 06:50:43 +00:00
|
|
|
Factory::NewNumberDictionary(at_least_space_for));
|
2008-10-28 14:47:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Handle<Object> len = Factory::NewNumber(static_cast<double>(result_length));
|
|
|
|
|
2008-10-28 17:23:10 +00:00
|
|
|
ArrayConcatVisitor visitor(storage, result_length, fast_case);
|
2008-10-28 14:47:50 +00:00
|
|
|
|
|
|
|
IterateArguments(arguments, &visitor);
|
|
|
|
|
|
|
|
result->set_length(*len);
|
|
|
|
result->set_elements(*storage);
|
|
|
|
|
|
|
|
return *result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
// This will not allocate (flatten the string), but it may run
|
|
|
|
// very slowly for very deeply nested ConsStrings. For debugging use only.
|
|
|
|
static Object* Runtime_GlobalPrint(Arguments args) {
|
|
|
|
NoHandleAllocation ha;
|
|
|
|
ASSERT(args.length() == 1);
|
|
|
|
|
|
|
|
CONVERT_CHECKED(String, string, args[0]);
|
|
|
|
StringInputBuffer buffer(string);
|
|
|
|
while (buffer.has_more()) {
|
|
|
|
uint16_t character = buffer.GetNext();
|
|
|
|
PrintF("%c", character);
|
|
|
|
}
|
|
|
|
return string;
|
|
|
|
}
|
|
|
|
|
2009-04-27 11:16:59 +00:00
|
|
|
// Moves all own elements of an object, that are below a limit, to positions
|
|
|
|
// starting at zero. All undefined values are placed after non-undefined values,
|
|
|
|
// and are followed by non-existing element. Does not change the length
|
|
|
|
// property.
|
|
|
|
// Returns the number of non-undefined elements collected.
|
2008-07-03 15:10:15 +00:00
|
|
|
static Object* Runtime_RemoveArrayHoles(Arguments args) {
|
2009-04-27 11:16:59 +00:00
|
|
|
ASSERT(args.length() == 2);
|
|
|
|
CONVERT_CHECKED(JSObject, object, args[0]);
|
|
|
|
CONVERT_NUMBER_CHECKED(uint32_t, limit, Uint32, args[1]);
|
|
|
|
return object->PrepareElementsForSort(limit);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Move contents of argument 0 (an array) to argument 1 (an array)
|
|
|
|
static Object* Runtime_MoveArrayContents(Arguments args) {
|
|
|
|
ASSERT(args.length() == 2);
|
|
|
|
CONVERT_CHECKED(JSArray, from, args[0]);
|
|
|
|
CONVERT_CHECKED(JSArray, to, args[1]);
|
|
|
|
to->SetContent(FixedArray::cast(from->elements()));
|
|
|
|
to->set_length(from->length());
|
|
|
|
from->SetContent(Heap::empty_fixed_array());
|
|
|
|
from->set_length(0);
|
|
|
|
return to;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// How many elements does this array have?
|
|
|
|
static Object* Runtime_EstimateNumberOfElements(Arguments args) {
|
|
|
|
ASSERT(args.length() == 1);
|
|
|
|
CONVERT_CHECKED(JSArray, array, args[0]);
|
|
|
|
HeapObject* elements = array->elements();
|
|
|
|
if (elements->IsDictionary()) {
|
2009-07-02 06:50:43 +00:00
|
|
|
return Smi::FromInt(NumberDictionary::cast(elements)->NumberOfElements());
|
2008-07-03 15:10:15 +00:00
|
|
|
} else {
|
|
|
|
return array->length();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Returns an array that tells you where in the [0, length) interval an array
|
|
|
|
// might have elements. Can either return keys or intervals. Keys can have
|
|
|
|
// gaps in (undefined). Intervals can also span over some undefined keys.
|
|
|
|
static Object* Runtime_GetArrayKeys(Arguments args) {
|
|
|
|
ASSERT(args.length() == 2);
|
|
|
|
HandleScope scope;
|
2009-05-01 10:06:55 +00:00
|
|
|
CONVERT_ARG_CHECKED(JSObject, array, 0);
|
2008-07-03 15:10:15 +00:00
|
|
|
CONVERT_NUMBER_CHECKED(uint32_t, length, Uint32, args[1]);
|
2008-08-27 10:11:39 +00:00
|
|
|
if (array->elements()->IsDictionary()) {
|
2008-07-03 15:10:15 +00:00
|
|
|
// Create an array and get all the keys into it, then remove all the
|
|
|
|
// keys that are not integers in the range 0 to length-1.
|
2009-09-15 11:51:40 +00:00
|
|
|
Handle<FixedArray> keys = GetKeysInFixedArrayFor(array, INCLUDE_PROTOS);
|
2008-07-03 15:10:15 +00:00
|
|
|
int keys_length = keys->length();
|
|
|
|
for (int i = 0; i < keys_length; i++) {
|
|
|
|
Object* key = keys->get(i);
|
|
|
|
uint32_t index;
|
|
|
|
if (!Array::IndexFromObject(key, &index) || index >= length) {
|
|
|
|
// Zap invalid keys.
|
|
|
|
keys->set_undefined(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return *Factory::NewJSArrayWithElements(keys);
|
|
|
|
} else {
|
|
|
|
Handle<FixedArray> single_interval = Factory::NewFixedArray(2);
|
|
|
|
// -1 means start of array.
|
2008-10-23 08:46:32 +00:00
|
|
|
single_interval->set(0,
|
|
|
|
Smi::FromInt(-1),
|
|
|
|
SKIP_WRITE_BARRIER);
|
2009-05-01 10:06:55 +00:00
|
|
|
uint32_t actual_length = static_cast<uint32_t>(array->elements()->length());
|
|
|
|
uint32_t min_length = actual_length < length ? actual_length : length;
|
2008-07-03 15:10:15 +00:00
|
|
|
Handle<Object> length_object =
|
2009-05-01 10:06:55 +00:00
|
|
|
Factory::NewNumber(static_cast<double>(min_length));
|
2008-07-03 15:10:15 +00:00
|
|
|
single_interval->set(1, *length_object);
|
|
|
|
return *Factory::NewJSArrayWithElements(single_interval);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// DefineAccessor takes an optional final argument which is the
|
|
|
|
// property attributes (eg, DONT_ENUM, DONT_DELETE). IMPORTANT: due
|
|
|
|
// to the way accessors are implemented, it is set for both the getter
|
|
|
|
// and setter on the first call to DefineAccessor and ignored on
|
|
|
|
// subsequent calls.
|
|
|
|
static Object* Runtime_DefineAccessor(Arguments args) {
|
2009-03-19 15:06:00 +00:00
|
|
|
RUNTIME_ASSERT(args.length() == 4 || args.length() == 5);
|
2008-07-03 15:10:15 +00:00
|
|
|
// Compute attributes.
|
|
|
|
PropertyAttributes attributes = NONE;
|
2009-03-19 15:06:00 +00:00
|
|
|
if (args.length() == 5) {
|
2008-07-03 15:10:15 +00:00
|
|
|
CONVERT_CHECKED(Smi, attrs, args[4]);
|
|
|
|
int value = attrs->value();
|
|
|
|
// Only attribute bits should be set.
|
|
|
|
ASSERT((value & ~(READ_ONLY | DONT_ENUM | DONT_DELETE)) == 0);
|
|
|
|
attributes = static_cast<PropertyAttributes>(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
CONVERT_CHECKED(JSObject, obj, args[0]);
|
|
|
|
CONVERT_CHECKED(String, name, args[1]);
|
|
|
|
CONVERT_CHECKED(Smi, flag, args[2]);
|
|
|
|
CONVERT_CHECKED(JSFunction, fun, args[3]);
|
2009-03-19 15:06:00 +00:00
|
|
|
return obj->DefineAccessor(name, flag->value() == 0, fun, attributes);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static Object* Runtime_LookupAccessor(Arguments args) {
|
|
|
|
ASSERT(args.length() == 3);
|
|
|
|
CONVERT_CHECKED(JSObject, obj, args[0]);
|
|
|
|
CONVERT_CHECKED(String, name, args[1]);
|
|
|
|
CONVERT_CHECKED(Smi, flag, args[2]);
|
|
|
|
return obj->LookupAccessor(name, flag->value() == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-04-20 16:36:13 +00:00
|
|
|
#ifdef ENABLE_DEBUGGER_SUPPORT
|
|
|
|
static Object* Runtime_DebugBreak(Arguments args) {
|
|
|
|
ASSERT(args.length() == 0);
|
|
|
|
return Execution::DebugBreakHelper();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
// Helper functions for wrapping and unwrapping stack frame ids.
|
|
|
|
static Smi* WrapFrameId(StackFrame::Id id) {
|
2009-05-20 08:05:12 +00:00
|
|
|
ASSERT(IsAligned(OffsetFrom(id), static_cast<intptr_t>(4)));
|
2008-07-03 15:10:15 +00:00
|
|
|
return Smi::FromInt(id >> 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static StackFrame::Id UnwrapFrameId(Smi* wrapped) {
|
|
|
|
return static_cast<StackFrame::Id>(wrapped->value() << 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Adds a JavaScript function as a debug event listener.
|
2009-02-03 07:59:12 +00:00
|
|
|
// args[0]: debug event listener function to set or null or undefined for
|
|
|
|
// clearing the event listener function
|
2008-07-03 15:10:15 +00:00
|
|
|
// args[1]: object supplied during callback
|
2009-02-03 07:59:12 +00:00
|
|
|
static Object* Runtime_SetDebugEventListener(Arguments args) {
|
2008-07-03 15:10:15 +00:00
|
|
|
ASSERT(args.length() == 2);
|
2009-02-03 07:59:12 +00:00
|
|
|
RUNTIME_ASSERT(args[0]->IsJSFunction() ||
|
|
|
|
args[0]->IsUndefined() ||
|
|
|
|
args[0]->IsNull());
|
|
|
|
Handle<Object> callback = args.at<Object>(0);
|
|
|
|
Handle<Object> data = args.at<Object>(1);
|
|
|
|
Debugger::SetEventListener(callback, data);
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
return Heap::undefined_value();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static Object* Runtime_Break(Arguments args) {
|
2008-08-13 09:32:07 +00:00
|
|
|
ASSERT(args.length() == 0);
|
2008-07-03 15:10:15 +00:00
|
|
|
StackGuard::DebugBreak();
|
|
|
|
return Heap::undefined_value();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-01-22 13:20:31 +00:00
|
|
|
// Find the length of the prototype chain that is to to handled as one. If a
|
|
|
|
// prototype object is hidden it is to be viewed as part of the the object it
|
|
|
|
// is prototype for.
|
|
|
|
static int LocalPrototypeChainLength(JSObject* obj) {
|
|
|
|
int count = 1;
|
|
|
|
Object* proto = obj->GetPrototype();
|
|
|
|
while (proto->IsJSObject() &&
|
|
|
|
JSObject::cast(proto)->map()->is_hidden_prototype()) {
|
|
|
|
count++;
|
|
|
|
proto = JSObject::cast(proto)->GetPrototype();
|
|
|
|
}
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-05-25 15:07:21 +00:00
|
|
|
static Object* DebugLookupResultValue(Object* receiver, String* name,
|
|
|
|
LookupResult* result,
|
2009-01-13 14:38:44 +00:00
|
|
|
bool* caught_exception) {
|
2009-01-19 15:21:41 +00:00
|
|
|
Object* value;
|
2008-07-03 15:10:15 +00:00
|
|
|
switch (result->type()) {
|
2009-06-30 10:05:36 +00:00
|
|
|
case NORMAL:
|
|
|
|
value = result->holder()->GetNormalizedProperty(result);
|
2009-01-19 15:21:41 +00:00
|
|
|
if (value->IsTheHole()) {
|
|
|
|
return Heap::undefined_value();
|
|
|
|
}
|
|
|
|
return value;
|
2008-07-03 15:10:15 +00:00
|
|
|
case FIELD:
|
2009-01-19 15:21:41 +00:00
|
|
|
value =
|
|
|
|
JSObject::cast(
|
|
|
|
result->holder())->FastPropertyAt(result->GetFieldIndex());
|
|
|
|
if (value->IsTheHole()) {
|
|
|
|
return Heap::undefined_value();
|
|
|
|
}
|
|
|
|
return value;
|
2009-01-13 14:38:44 +00:00
|
|
|
case CONSTANT_FUNCTION:
|
2009-01-19 15:21:41 +00:00
|
|
|
return result->GetConstantFunction();
|
2009-01-13 14:38:44 +00:00
|
|
|
case CALLBACKS: {
|
2009-01-19 15:21:41 +00:00
|
|
|
Object* structure = result->GetCallbackObject();
|
2009-05-25 15:07:21 +00:00
|
|
|
if (structure->IsProxy() || structure->IsAccessorInfo()) {
|
2009-05-28 11:30:54 +00:00
|
|
|
value = receiver->GetPropertyWithCallback(
|
|
|
|
receiver, structure, name, result->holder());
|
2009-02-16 13:31:13 +00:00
|
|
|
if (value->IsException()) {
|
2009-01-19 15:21:41 +00:00
|
|
|
value = Top::pending_exception();
|
|
|
|
Top::clear_pending_exception();
|
|
|
|
if (caught_exception != NULL) {
|
|
|
|
*caught_exception = true;
|
|
|
|
}
|
2009-01-13 14:38:44 +00:00
|
|
|
}
|
2009-01-19 15:21:41 +00:00
|
|
|
return value;
|
|
|
|
} else {
|
|
|
|
return Heap::undefined_value();
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
2009-01-13 14:38:44 +00:00
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
case INTERCEPTOR:
|
2008-08-27 10:11:39 +00:00
|
|
|
case MAP_TRANSITION:
|
|
|
|
case CONSTANT_TRANSITION:
|
|
|
|
case NULL_DESCRIPTOR:
|
2008-07-03 15:10:15 +00:00
|
|
|
return Heap::undefined_value();
|
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
}
|
2008-08-27 10:11:39 +00:00
|
|
|
UNREACHABLE();
|
2008-07-03 15:10:15 +00:00
|
|
|
return Heap::undefined_value();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-01-13 14:38:44 +00:00
|
|
|
// Get debugger related details for an object property.
|
|
|
|
// args[0]: object holding property
|
|
|
|
// args[1]: name of the property
|
|
|
|
//
|
|
|
|
// The array returned contains the following information:
|
|
|
|
// 0: Property value
|
|
|
|
// 1: Property details
|
|
|
|
// 2: Property value is exception
|
|
|
|
// 3: Getter function if defined
|
|
|
|
// 4: Setter function if defined
|
|
|
|
// Items 2-4 are only filled if the property has either a getter or a setter
|
|
|
|
// defined through __defineGetter__ and/or __defineSetter__.
|
Split window support from V8.
Here is a description of the background and design of split window in Chrome and V8:
https://docs.google.com/a/google.com/Doc?id=chhjkpg_47fwddxbfr
This change list splits the window object into two parts: 1) an inner window object used as the global object of contexts; 2) an outer window object exposed to JavaScript and accessible by the name 'window'. Firefox did it awhile ago, here are some discussions: https://wiki.mozilla.org/Gecko:SplitWindow. One additional benefit of splitting window in Chrome is that accessing global variables don't need security checks anymore, it can improve applications that use many global variables.
V8 support of split window:
There are a small number of changes on V8 api to support split window:
Security context is removed from V8, so does related API functions;
A global object can be detached from its context and reused by a new context;
Access checks on an object template can be turned on/off by default;
An object can turn on its access checks later;
V8 has a new object type, ApiGlobalObject, which is the outer window object type. The existing JSGlobalObject becomes the inner window object type. Security checks are moved from JSGlobalObject to ApiGlobalObject. ApiGlobalObject is the one exposed to JavaScript, it is accessible through Context::Global(). ApiGlobalObject's prototype is set to JSGlobalObject so that property lookups are forwarded to JSGlobalObject. ApiGlobalObject forwards all other property access requests to JSGlobalObject, such as SetProperty, DeleteProperty, etc.
Security token is moved to a global context, and ApiGlobalObject has a reference to its global context. JSGlobalObject has a reference to its global context as well. When accessing properties on a global object in JavaScript, the domain security check is performed by comparing the security token of the lexical context (Top::global_context()) to the token of global object's context. The check is only needed when the receiver is a window object, such as 'window.document'. Accessing global variables, such as 'var foo = 3; foo' does not need checks because the receiver is the inner window object.
When an outer window is detached from its global context (when a frame navigates away from a page), it is completely detached from the inner window. A new context is created for the new page, and the outer global object is reused. At this point, the access check on the DOMWindow wrapper of the old context is turned on. The code in old context is still able to access DOMWindow properties, but it has to go through domain security checks.
It is debatable on how to implement the outer window object. Currently each property access function has to check if the receiver is ApiGlobalObject type. This approach might be error-prone that one may forget to check the receiver when adding new functions. It is unlikely a performance issue because accessing global variables are more common than 'window.foo' style coding.
I am still working on the ARM port, and I'd like to hear comments and suggestions on the best way to support it in V8.
Review URL: http://codereview.chromium.org/7366
git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@540 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
2008-10-21 19:07:58 +00:00
|
|
|
static Object* Runtime_DebugGetPropertyDetails(Arguments args) {
|
2008-07-03 15:10:15 +00:00
|
|
|
HandleScope scope;
|
|
|
|
|
|
|
|
ASSERT(args.length() == 2);
|
|
|
|
|
|
|
|
CONVERT_ARG_CHECKED(JSObject, obj, 0);
|
|
|
|
CONVERT_ARG_CHECKED(String, name, 1);
|
|
|
|
|
2009-05-28 11:30:54 +00:00
|
|
|
// Make sure to set the current context to the context before the debugger was
|
|
|
|
// entered (if the debugger is entered). The reason for switching context here
|
|
|
|
// is that for some property lookups (accessors and interceptors) callbacks
|
|
|
|
// into the embedding application can occour, and the embedding application
|
|
|
|
// could have the assumption that its own global context is the current
|
|
|
|
// context and not some internal debugger context.
|
|
|
|
SaveContext save;
|
|
|
|
if (Debug::InDebugger()) {
|
|
|
|
Top::set_context(*Debug::debugger_entry()->GetContext());
|
|
|
|
}
|
|
|
|
|
2009-01-22 13:20:31 +00:00
|
|
|
// Skip the global proxy as it has no properties and always delegates to the
|
|
|
|
// real global object.
|
|
|
|
if (obj->IsJSGlobalProxy()) {
|
|
|
|
obj = Handle<JSObject>(JSObject::cast(obj->GetPrototype()));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
// Check if the name is trivially convertible to an index and get the element
|
|
|
|
// if so.
|
|
|
|
uint32_t index;
|
|
|
|
if (name->AsArrayIndex(&index)) {
|
|
|
|
Handle<FixedArray> details = Factory::NewFixedArray(2);
|
|
|
|
details->set(0, Runtime::GetElementOrCharAt(obj, index));
|
|
|
|
details->set(1, PropertyDetails(NONE, NORMAL).AsSmi());
|
|
|
|
return *Factory::NewJSArrayWithElements(details);
|
|
|
|
}
|
|
|
|
|
2009-01-22 13:20:31 +00:00
|
|
|
// Find the number of objects making up this.
|
|
|
|
int length = LocalPrototypeChainLength(*obj);
|
|
|
|
|
|
|
|
// Try local lookup on each of the objects.
|
|
|
|
Handle<JSObject> jsproto = obj;
|
|
|
|
for (int i = 0; i < length; i++) {
|
2009-09-16 13:09:26 +00:00
|
|
|
LookupResult result;
|
2009-01-22 13:20:31 +00:00
|
|
|
jsproto->LocalLookup(*name, &result);
|
|
|
|
if (result.IsProperty()) {
|
2009-09-16 13:09:26 +00:00
|
|
|
// LookupResult is not GC safe as it holds raw object pointers.
|
|
|
|
// GC can happen later in this code so put the required fields into
|
|
|
|
// local variables using handles when required for later use.
|
|
|
|
PropertyType result_type = result.type();
|
|
|
|
Handle<Object> result_callback_obj;
|
|
|
|
if (result_type == CALLBACKS) {
|
|
|
|
result_callback_obj = Handle<Object>(result.GetCallbackObject());
|
|
|
|
}
|
|
|
|
Smi* property_details = result.GetPropertyDetails().AsSmi();
|
|
|
|
// DebugLookupResultValue can cause GC so details from LookupResult needs
|
|
|
|
// to be copied to handles before this.
|
|
|
|
bool caught_exception = false;
|
|
|
|
Object* raw_value = DebugLookupResultValue(*obj, *name, &result,
|
|
|
|
&caught_exception);
|
|
|
|
if (raw_value->IsFailure()) return raw_value;
|
|
|
|
Handle<Object> value(raw_value);
|
|
|
|
|
|
|
|
// If the callback object is a fixed array then it contains JavaScript
|
|
|
|
// getter and/or setter.
|
|
|
|
bool hasJavaScriptAccessors = result_type == CALLBACKS &&
|
|
|
|
result_callback_obj->IsFixedArray();
|
|
|
|
Handle<FixedArray> details =
|
|
|
|
Factory::NewFixedArray(hasJavaScriptAccessors ? 5 : 2);
|
|
|
|
details->set(0, *value);
|
|
|
|
details->set(1, property_details);
|
|
|
|
if (hasJavaScriptAccessors) {
|
|
|
|
details->set(2,
|
|
|
|
caught_exception ? Heap::true_value()
|
|
|
|
: Heap::false_value());
|
|
|
|
details->set(3, FixedArray::cast(*result_callback_obj)->get(0));
|
|
|
|
details->set(4, FixedArray::cast(*result_callback_obj)->get(1));
|
|
|
|
}
|
|
|
|
|
|
|
|
return *Factory::NewJSArrayWithElements(details);
|
2009-01-22 13:20:31 +00:00
|
|
|
}
|
|
|
|
if (i < length - 1) {
|
|
|
|
jsproto = Handle<JSObject>(JSObject::cast(jsproto->GetPrototype()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
return Heap::undefined_value();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static Object* Runtime_DebugGetProperty(Arguments args) {
|
|
|
|
HandleScope scope;
|
|
|
|
|
|
|
|
ASSERT(args.length() == 2);
|
|
|
|
|
|
|
|
CONVERT_ARG_CHECKED(JSObject, obj, 0);
|
|
|
|
CONVERT_ARG_CHECKED(String, name, 1);
|
|
|
|
|
|
|
|
LookupResult result;
|
|
|
|
obj->Lookup(*name, &result);
|
|
|
|
if (result.IsProperty()) {
|
2009-05-25 15:07:21 +00:00
|
|
|
return DebugLookupResultValue(*obj, *name, &result, NULL);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
return Heap::undefined_value();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Return the names of the local named properties.
|
|
|
|
// args[0]: object
|
|
|
|
static Object* Runtime_DebugLocalPropertyNames(Arguments args) {
|
|
|
|
HandleScope scope;
|
|
|
|
ASSERT(args.length() == 1);
|
|
|
|
if (!args[0]->IsJSObject()) {
|
|
|
|
return Heap::undefined_value();
|
|
|
|
}
|
|
|
|
CONVERT_ARG_CHECKED(JSObject, obj, 0);
|
|
|
|
|
2009-01-22 13:20:31 +00:00
|
|
|
// Skip the global proxy as it has no properties and always delegates to the
|
|
|
|
// real global object.
|
2009-01-21 09:32:07 +00:00
|
|
|
if (obj->IsJSGlobalProxy()) {
|
|
|
|
obj = Handle<JSObject>(JSObject::cast(obj->GetPrototype()));
|
|
|
|
}
|
2009-01-22 13:20:31 +00:00
|
|
|
|
|
|
|
// Find the number of objects making up this.
|
|
|
|
int length = LocalPrototypeChainLength(*obj);
|
|
|
|
|
|
|
|
// Find the number of local properties for each of the objects.
|
|
|
|
int* local_property_count = NewArray<int>(length);
|
|
|
|
int total_property_count = 0;
|
|
|
|
Handle<JSObject> jsproto = obj;
|
|
|
|
for (int i = 0; i < length; i++) {
|
|
|
|
int n;
|
|
|
|
n = jsproto->NumberOfLocalProperties(static_cast<PropertyAttributes>(NONE));
|
|
|
|
local_property_count[i] = n;
|
|
|
|
total_property_count += n;
|
|
|
|
if (i < length - 1) {
|
|
|
|
jsproto = Handle<JSObject>(JSObject::cast(jsproto->GetPrototype()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Allocate an array with storage for all the property names.
|
|
|
|
Handle<FixedArray> names = Factory::NewFixedArray(total_property_count);
|
|
|
|
|
|
|
|
// Get the property names.
|
|
|
|
jsproto = obj;
|
|
|
|
for (int i = 0; i < length; i++) {
|
|
|
|
jsproto->GetLocalPropertyNames(*names,
|
|
|
|
i == 0 ? 0 : local_property_count[i - 1]);
|
|
|
|
if (i < length - 1) {
|
|
|
|
jsproto = Handle<JSObject>(JSObject::cast(jsproto->GetPrototype()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
DeleteArray(local_property_count);
|
2008-07-03 15:10:15 +00:00
|
|
|
return *Factory::NewJSArrayWithElements(names);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Return the names of the local indexed properties.
|
|
|
|
// args[0]: object
|
|
|
|
static Object* Runtime_DebugLocalElementNames(Arguments args) {
|
|
|
|
HandleScope scope;
|
|
|
|
ASSERT(args.length() == 1);
|
|
|
|
if (!args[0]->IsJSObject()) {
|
|
|
|
return Heap::undefined_value();
|
|
|
|
}
|
|
|
|
CONVERT_ARG_CHECKED(JSObject, obj, 0);
|
|
|
|
|
|
|
|
int n = obj->NumberOfLocalElements(static_cast<PropertyAttributes>(NONE));
|
|
|
|
Handle<FixedArray> names = Factory::NewFixedArray(n);
|
|
|
|
obj->GetLocalElementKeys(*names, static_cast<PropertyAttributes>(NONE));
|
|
|
|
return *Factory::NewJSArrayWithElements(names);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Return the property type calculated from the property details.
|
|
|
|
// args[0]: smi with property details.
|
|
|
|
static Object* Runtime_DebugPropertyTypeFromDetails(Arguments args) {
|
|
|
|
ASSERT(args.length() == 1);
|
|
|
|
CONVERT_CHECKED(Smi, details, args[0]);
|
|
|
|
PropertyType type = PropertyDetails(details).type();
|
|
|
|
return Smi::FromInt(static_cast<int>(type));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Return the property attribute calculated from the property details.
|
|
|
|
// args[0]: smi with property details.
|
|
|
|
static Object* Runtime_DebugPropertyAttributesFromDetails(Arguments args) {
|
|
|
|
ASSERT(args.length() == 1);
|
|
|
|
CONVERT_CHECKED(Smi, details, args[0]);
|
|
|
|
PropertyAttributes attributes = PropertyDetails(details).attributes();
|
|
|
|
return Smi::FromInt(static_cast<int>(attributes));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Return the property insertion index calculated from the property details.
|
|
|
|
// args[0]: smi with property details.
|
|
|
|
static Object* Runtime_DebugPropertyIndexFromDetails(Arguments args) {
|
|
|
|
ASSERT(args.length() == 1);
|
|
|
|
CONVERT_CHECKED(Smi, details, args[0]);
|
|
|
|
int index = PropertyDetails(details).index();
|
|
|
|
return Smi::FromInt(index);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Return information on whether an object has a named or indexed interceptor.
|
|
|
|
// args[0]: object
|
|
|
|
static Object* Runtime_DebugInterceptorInfo(Arguments args) {
|
|
|
|
HandleScope scope;
|
|
|
|
ASSERT(args.length() == 1);
|
|
|
|
if (!args[0]->IsJSObject()) {
|
|
|
|
return Smi::FromInt(0);
|
|
|
|
}
|
|
|
|
CONVERT_ARG_CHECKED(JSObject, obj, 0);
|
|
|
|
|
|
|
|
int result = 0;
|
|
|
|
if (obj->HasNamedInterceptor()) result |= 2;
|
|
|
|
if (obj->HasIndexedInterceptor()) result |= 1;
|
|
|
|
|
|
|
|
return Smi::FromInt(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Return property names from named interceptor.
|
|
|
|
// args[0]: object
|
|
|
|
static Object* Runtime_DebugNamedInterceptorPropertyNames(Arguments args) {
|
|
|
|
HandleScope scope;
|
|
|
|
ASSERT(args.length() == 1);
|
|
|
|
CONVERT_ARG_CHECKED(JSObject, obj, 0);
|
|
|
|
|
2009-01-13 14:38:44 +00:00
|
|
|
if (obj->HasNamedInterceptor()) {
|
|
|
|
v8::Handle<v8::Array> result = GetKeysForNamedInterceptor(obj, obj);
|
|
|
|
if (!result.IsEmpty()) return *v8::Utils::OpenHandle(*result);
|
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
return Heap::undefined_value();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Return element names from indexed interceptor.
|
|
|
|
// args[0]: object
|
|
|
|
static Object* Runtime_DebugIndexedInterceptorElementNames(Arguments args) {
|
|
|
|
HandleScope scope;
|
|
|
|
ASSERT(args.length() == 1);
|
|
|
|
CONVERT_ARG_CHECKED(JSObject, obj, 0);
|
|
|
|
|
2009-01-13 14:38:44 +00:00
|
|
|
if (obj->HasIndexedInterceptor()) {
|
|
|
|
v8::Handle<v8::Array> result = GetKeysForIndexedInterceptor(obj, obj);
|
|
|
|
if (!result.IsEmpty()) return *v8::Utils::OpenHandle(*result);
|
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
return Heap::undefined_value();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Return property value from named interceptor.
|
|
|
|
// args[0]: object
|
|
|
|
// args[1]: property name
|
|
|
|
static Object* Runtime_DebugNamedInterceptorPropertyValue(Arguments args) {
|
|
|
|
HandleScope scope;
|
|
|
|
ASSERT(args.length() == 2);
|
|
|
|
CONVERT_ARG_CHECKED(JSObject, obj, 0);
|
|
|
|
RUNTIME_ASSERT(obj->HasNamedInterceptor());
|
|
|
|
CONVERT_ARG_CHECKED(String, name, 1);
|
|
|
|
|
|
|
|
PropertyAttributes attributes;
|
2008-08-27 10:11:39 +00:00
|
|
|
return obj->GetPropertyWithInterceptor(*obj, *name, &attributes);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Return element value from indexed interceptor.
|
|
|
|
// args[0]: object
|
|
|
|
// args[1]: index
|
|
|
|
static Object* Runtime_DebugIndexedInterceptorElementValue(Arguments args) {
|
|
|
|
HandleScope scope;
|
|
|
|
ASSERT(args.length() == 2);
|
|
|
|
CONVERT_ARG_CHECKED(JSObject, obj, 0);
|
|
|
|
RUNTIME_ASSERT(obj->HasIndexedInterceptor());
|
|
|
|
CONVERT_NUMBER_CHECKED(uint32_t, index, Uint32, args[1]);
|
|
|
|
|
2008-08-27 10:11:39 +00:00
|
|
|
return obj->GetElementWithInterceptor(*obj, index);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static Object* Runtime_CheckExecutionState(Arguments args) {
|
|
|
|
ASSERT(args.length() >= 1);
|
|
|
|
CONVERT_NUMBER_CHECKED(int, break_id, Int32, args[0]);
|
2008-12-11 08:03:24 +00:00
|
|
|
// Check that the break id is valid.
|
2009-03-06 11:03:14 +00:00
|
|
|
if (Debug::break_id() == 0 || break_id != Debug::break_id()) {
|
2008-07-03 15:10:15 +00:00
|
|
|
return Top::Throw(Heap::illegal_execution_state_symbol());
|
|
|
|
}
|
|
|
|
|
|
|
|
return Heap::true_value();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static Object* Runtime_GetFrameCount(Arguments args) {
|
|
|
|
HandleScope scope;
|
|
|
|
ASSERT(args.length() == 1);
|
|
|
|
|
|
|
|
// Check arguments.
|
|
|
|
Object* result = Runtime_CheckExecutionState(args);
|
|
|
|
if (result->IsFailure()) return result;
|
|
|
|
|
|
|
|
// Count all frames which are relevant to debugging stack trace.
|
|
|
|
int n = 0;
|
2009-03-06 11:03:14 +00:00
|
|
|
StackFrame::Id id = Debug::break_frame_id();
|
2008-12-11 08:03:24 +00:00
|
|
|
if (id == StackFrame::NO_ID) {
|
|
|
|
// If there is no JavaScript stack frame count is 0.
|
|
|
|
return Smi::FromInt(0);
|
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
for (JavaScriptFrameIterator it(id); !it.done(); it.Advance()) n++;
|
|
|
|
return Smi::FromInt(n);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static const int kFrameDetailsFrameIdIndex = 0;
|
|
|
|
static const int kFrameDetailsReceiverIndex = 1;
|
|
|
|
static const int kFrameDetailsFunctionIndex = 2;
|
|
|
|
static const int kFrameDetailsArgumentCountIndex = 3;
|
|
|
|
static const int kFrameDetailsLocalCountIndex = 4;
|
|
|
|
static const int kFrameDetailsSourcePositionIndex = 5;
|
|
|
|
static const int kFrameDetailsConstructCallIndex = 6;
|
|
|
|
static const int kFrameDetailsDebuggerFrameIndex = 7;
|
|
|
|
static const int kFrameDetailsFirstDynamicIndex = 8;
|
|
|
|
|
|
|
|
// Return an array with frame details
|
|
|
|
// args[0]: number: break id
|
|
|
|
// args[1]: number: frame index
|
|
|
|
//
|
|
|
|
// The array returned contains the following information:
|
|
|
|
// 0: Frame id
|
|
|
|
// 1: Receiver
|
|
|
|
// 2: Function
|
|
|
|
// 3: Argument count
|
|
|
|
// 4: Local count
|
|
|
|
// 5: Source position
|
|
|
|
// 6: Constructor call
|
|
|
|
// 7: Debugger frame
|
|
|
|
// Arguments name, value
|
|
|
|
// Locals name, value
|
|
|
|
static Object* Runtime_GetFrameDetails(Arguments args) {
|
|
|
|
HandleScope scope;
|
|
|
|
ASSERT(args.length() == 2);
|
|
|
|
|
|
|
|
// Check arguments.
|
2008-08-27 10:11:39 +00:00
|
|
|
Object* check = Runtime_CheckExecutionState(args);
|
|
|
|
if (check->IsFailure()) return check;
|
2008-07-03 15:10:15 +00:00
|
|
|
CONVERT_NUMBER_CHECKED(int, index, Int32, args[1]);
|
|
|
|
|
|
|
|
// Find the relevant frame with the requested index.
|
2009-03-06 11:03:14 +00:00
|
|
|
StackFrame::Id id = Debug::break_frame_id();
|
2008-12-11 08:03:24 +00:00
|
|
|
if (id == StackFrame::NO_ID) {
|
|
|
|
// If there are no JavaScript stack frames return undefined.
|
|
|
|
return Heap::undefined_value();
|
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
int count = 0;
|
|
|
|
JavaScriptFrameIterator it(id);
|
|
|
|
for (; !it.done(); it.Advance()) {
|
|
|
|
if (count == index) break;
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
if (it.done()) return Heap::undefined_value();
|
|
|
|
|
|
|
|
// Traverse the saved contexts chain to find the active context for the
|
|
|
|
// selected frame.
|
|
|
|
SaveContext* save = Top::save_context();
|
2008-11-17 17:44:16 +00:00
|
|
|
while (save != NULL && !save->below(it.frame())) {
|
2008-07-03 15:10:15 +00:00
|
|
|
save = save->prev();
|
|
|
|
}
|
2008-11-17 17:44:16 +00:00
|
|
|
ASSERT(save != NULL);
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
// Get the frame id.
|
|
|
|
Handle<Object> frame_id(WrapFrameId(it.frame()->id()));
|
|
|
|
|
|
|
|
// Find source position.
|
2009-02-25 16:52:15 +00:00
|
|
|
int position = it.frame()->code()->SourcePosition(it.frame()->pc());
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
// Check for constructor frame.
|
|
|
|
bool constructor = it.frame()->IsConstructor();
|
|
|
|
|
|
|
|
// Get code and read scope info from it for local variable information.
|
2009-02-25 16:52:15 +00:00
|
|
|
Handle<Code> code(it.frame()->code());
|
2008-07-03 15:10:15 +00:00
|
|
|
ScopeInfo<> info(*code);
|
|
|
|
|
|
|
|
// Get the context.
|
|
|
|
Handle<Context> context(Context::cast(it.frame()->context()));
|
|
|
|
|
|
|
|
// Get the locals names and values into a temporary array.
|
|
|
|
//
|
|
|
|
// TODO(1240907): Hide compiler-introduced stack variables
|
|
|
|
// (e.g. .result)? For users of the debugger, they will probably be
|
|
|
|
// confusing.
|
|
|
|
Handle<FixedArray> locals = Factory::NewFixedArray(info.NumberOfLocals() * 2);
|
|
|
|
for (int i = 0; i < info.NumberOfLocals(); i++) {
|
|
|
|
// Name of the local.
|
|
|
|
locals->set(i * 2, *info.LocalName(i));
|
|
|
|
|
|
|
|
// Fetch the value of the local - either from the stack or from a
|
|
|
|
// heap-allocated context.
|
|
|
|
if (i < info.number_of_stack_slots()) {
|
|
|
|
locals->set(i * 2 + 1, it.frame()->GetExpression(i));
|
|
|
|
} else {
|
|
|
|
Handle<String> name = info.LocalName(i);
|
|
|
|
// Traverse the context chain to the function context as all local
|
|
|
|
// variables stored in the context will be on the function context.
|
2008-10-24 10:59:40 +00:00
|
|
|
while (!context->is_function_context()) {
|
2008-07-03 15:10:15 +00:00
|
|
|
context = Handle<Context>(context->previous());
|
|
|
|
}
|
|
|
|
ASSERT(context->is_function_context());
|
|
|
|
locals->set(i * 2 + 1,
|
|
|
|
context->get(ScopeInfo<>::ContextSlotIndex(*code, *name,
|
|
|
|
NULL)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now advance to the arguments adapter frame (if any). If contains all
|
|
|
|
// the provided parameters and
|
|
|
|
|
|
|
|
// Now advance to the arguments adapter frame (if any). It contains all
|
|
|
|
// the provided parameters whereas the function frame always have the number
|
|
|
|
// of arguments matching the functions parameters. The rest of the
|
|
|
|
// information (except for what is collected above) is the same.
|
|
|
|
it.AdvanceToArgumentsFrame();
|
|
|
|
|
|
|
|
// Find the number of arguments to fill. At least fill the number of
|
|
|
|
// parameters for the function and fill more if more parameters are provided.
|
|
|
|
int argument_count = info.number_of_parameters();
|
|
|
|
if (argument_count < it.frame()->GetProvidedParametersCount()) {
|
|
|
|
argument_count = it.frame()->GetProvidedParametersCount();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Calculate the size of the result.
|
|
|
|
int details_size = kFrameDetailsFirstDynamicIndex +
|
|
|
|
2 * (argument_count + info.NumberOfLocals());
|
|
|
|
Handle<FixedArray> details = Factory::NewFixedArray(details_size);
|
|
|
|
|
|
|
|
// Add the frame id.
|
|
|
|
details->set(kFrameDetailsFrameIdIndex, *frame_id);
|
|
|
|
|
|
|
|
// Add the function (same as in function frame).
|
|
|
|
details->set(kFrameDetailsFunctionIndex, it.frame()->function());
|
|
|
|
|
|
|
|
// Add the arguments count.
|
|
|
|
details->set(kFrameDetailsArgumentCountIndex, Smi::FromInt(argument_count));
|
|
|
|
|
|
|
|
// Add the locals count
|
|
|
|
details->set(kFrameDetailsLocalCountIndex,
|
|
|
|
Smi::FromInt(info.NumberOfLocals()));
|
|
|
|
|
|
|
|
// Add the source position.
|
2008-09-22 13:57:03 +00:00
|
|
|
if (position != RelocInfo::kNoPosition) {
|
2008-07-03 15:10:15 +00:00
|
|
|
details->set(kFrameDetailsSourcePositionIndex, Smi::FromInt(position));
|
|
|
|
} else {
|
|
|
|
details->set(kFrameDetailsSourcePositionIndex, Heap::undefined_value());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add the constructor information.
|
|
|
|
details->set(kFrameDetailsConstructCallIndex, Heap::ToBoolean(constructor));
|
|
|
|
|
|
|
|
// Add information on whether this frame is invoked in the debugger context.
|
|
|
|
details->set(kFrameDetailsDebuggerFrameIndex,
|
|
|
|
Heap::ToBoolean(*save->context() == *Debug::debug_context()));
|
|
|
|
|
|
|
|
// Fill the dynamic part.
|
|
|
|
int details_index = kFrameDetailsFirstDynamicIndex;
|
|
|
|
|
|
|
|
// Add arguments name and value.
|
|
|
|
for (int i = 0; i < argument_count; i++) {
|
|
|
|
// Name of the argument.
|
|
|
|
if (i < info.number_of_parameters()) {
|
|
|
|
details->set(details_index++, *info.parameter_name(i));
|
|
|
|
} else {
|
|
|
|
details->set(details_index++, Heap::undefined_value());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parameter value.
|
|
|
|
if (i < it.frame()->GetProvidedParametersCount()) {
|
|
|
|
details->set(details_index++, it.frame()->GetParameter(i));
|
|
|
|
} else {
|
|
|
|
details->set(details_index++, Heap::undefined_value());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add locals name and value from the temporary copy from the function frame.
|
|
|
|
for (int i = 0; i < info.NumberOfLocals() * 2; i++) {
|
|
|
|
details->set(details_index++, locals->get(i));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add the receiver (same as in function frame).
|
|
|
|
// THIS MUST BE DONE LAST SINCE WE MIGHT ADVANCE
|
|
|
|
// THE FRAME ITERATOR TO WRAP THE RECEIVER.
|
|
|
|
Handle<Object> receiver(it.frame()->receiver());
|
|
|
|
if (!receiver->IsJSObject()) {
|
|
|
|
// If the receiver is NOT a JSObject we have hit an optimization
|
|
|
|
// where a value object is not converted into a wrapped JS objects.
|
|
|
|
// To hide this optimization from the debugger, we wrap the receiver
|
|
|
|
// by creating correct wrapper object based on the calling frame's
|
|
|
|
// global context.
|
|
|
|
it.Advance();
|
|
|
|
Handle<Context> calling_frames_global_context(
|
|
|
|
Context::cast(Context::cast(it.frame()->context())->global_context()));
|
|
|
|
receiver = Factory::ToObject(receiver, calling_frames_global_context);
|
|
|
|
}
|
|
|
|
details->set(kFrameDetailsReceiverIndex, *receiver);
|
|
|
|
|
|
|
|
ASSERT_EQ(details_size, details_index);
|
|
|
|
return *Factory::NewJSArrayWithElements(details);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-06-12 10:03:25 +00:00
|
|
|
// Copy all the context locals into an object used to materialize a scope.
|
|
|
|
static void CopyContextLocalsToScopeObject(Handle<Code> code,
|
|
|
|
ScopeInfo<>& scope_info,
|
|
|
|
Handle<Context> context,
|
|
|
|
Handle<JSObject> scope_object) {
|
|
|
|
// Fill all context locals to the context extension.
|
|
|
|
for (int i = Context::MIN_CONTEXT_SLOTS;
|
|
|
|
i < scope_info.number_of_context_slots();
|
|
|
|
i++) {
|
|
|
|
int context_index =
|
|
|
|
ScopeInfo<>::ContextSlotIndex(*code,
|
|
|
|
*scope_info.context_slot_name(i),
|
|
|
|
NULL);
|
|
|
|
|
|
|
|
// Don't include the arguments shadow (.arguments) context variable.
|
|
|
|
if (*scope_info.context_slot_name(i) != Heap::arguments_shadow_symbol()) {
|
|
|
|
SetProperty(scope_object,
|
|
|
|
scope_info.context_slot_name(i),
|
|
|
|
Handle<Object>(context->get(context_index)), NONE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Create a plain JSObject which materializes the local scope for the specified
|
|
|
|
// frame.
|
|
|
|
static Handle<JSObject> MaterializeLocalScope(JavaScriptFrame* frame) {
|
|
|
|
Handle<JSFunction> function(JSFunction::cast(frame->function()));
|
|
|
|
Handle<Code> code(function->code());
|
|
|
|
ScopeInfo<> scope_info(*code);
|
|
|
|
|
|
|
|
// Allocate and initialize a JSObject with all the arguments, stack locals
|
|
|
|
// heap locals and extension properties of the debugged function.
|
|
|
|
Handle<JSObject> local_scope = Factory::NewJSObject(Top::object_function());
|
|
|
|
|
|
|
|
// First fill all parameters.
|
|
|
|
for (int i = 0; i < scope_info.number_of_parameters(); ++i) {
|
|
|
|
SetProperty(local_scope,
|
|
|
|
scope_info.parameter_name(i),
|
|
|
|
Handle<Object>(frame->GetParameter(i)), NONE);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Second fill all stack locals.
|
|
|
|
for (int i = 0; i < scope_info.number_of_stack_slots(); i++) {
|
|
|
|
SetProperty(local_scope,
|
|
|
|
scope_info.stack_slot_name(i),
|
|
|
|
Handle<Object>(frame->GetExpression(i)), NONE);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Third fill all context locals.
|
|
|
|
Handle<Context> frame_context(Context::cast(frame->context()));
|
|
|
|
Handle<Context> function_context(frame_context->fcontext());
|
|
|
|
CopyContextLocalsToScopeObject(code, scope_info,
|
|
|
|
function_context, local_scope);
|
|
|
|
|
|
|
|
// Finally copy any properties from the function context extension. This will
|
|
|
|
// be variables introduced by eval.
|
|
|
|
if (function_context->closure() == *function) {
|
|
|
|
if (function_context->has_extension() &&
|
|
|
|
!function_context->IsGlobalContext()) {
|
|
|
|
Handle<JSObject> ext(JSObject::cast(function_context->extension()));
|
2009-09-15 11:51:40 +00:00
|
|
|
Handle<FixedArray> keys = GetKeysInFixedArrayFor(ext, INCLUDE_PROTOS);
|
2009-06-12 10:03:25 +00:00
|
|
|
for (int i = 0; i < keys->length(); i++) {
|
|
|
|
// Names of variables introduced by eval are strings.
|
|
|
|
ASSERT(keys->get(i)->IsString());
|
|
|
|
Handle<String> key(String::cast(keys->get(i)));
|
|
|
|
SetProperty(local_scope, key, GetProperty(ext, key), NONE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return local_scope;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Create a plain JSObject which materializes the closure content for the
|
|
|
|
// context.
|
|
|
|
static Handle<JSObject> MaterializeClosure(Handle<Context> context) {
|
|
|
|
ASSERT(context->is_function_context());
|
|
|
|
|
|
|
|
Handle<Code> code(context->closure()->code());
|
|
|
|
ScopeInfo<> scope_info(*code);
|
|
|
|
|
|
|
|
// Allocate and initialize a JSObject with all the content of theis function
|
|
|
|
// closure.
|
|
|
|
Handle<JSObject> closure_scope = Factory::NewJSObject(Top::object_function());
|
|
|
|
|
|
|
|
// Check whether the arguments shadow object exists.
|
|
|
|
int arguments_shadow_index =
|
|
|
|
ScopeInfo<>::ContextSlotIndex(*code,
|
|
|
|
Heap::arguments_shadow_symbol(),
|
|
|
|
NULL);
|
|
|
|
if (arguments_shadow_index >= 0) {
|
|
|
|
// In this case all the arguments are available in the arguments shadow
|
|
|
|
// object.
|
|
|
|
Handle<JSObject> arguments_shadow(
|
|
|
|
JSObject::cast(context->get(arguments_shadow_index)));
|
|
|
|
for (int i = 0; i < scope_info.number_of_parameters(); ++i) {
|
|
|
|
SetProperty(closure_scope,
|
|
|
|
scope_info.parameter_name(i),
|
|
|
|
Handle<Object>(arguments_shadow->GetElement(i)), NONE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fill all context locals to the context extension.
|
|
|
|
CopyContextLocalsToScopeObject(code, scope_info, context, closure_scope);
|
|
|
|
|
|
|
|
// Finally copy any properties from the function context extension. This will
|
|
|
|
// be variables introduced by eval.
|
|
|
|
if (context->has_extension()) {
|
|
|
|
Handle<JSObject> ext(JSObject::cast(context->extension()));
|
2009-09-15 11:51:40 +00:00
|
|
|
Handle<FixedArray> keys = GetKeysInFixedArrayFor(ext, INCLUDE_PROTOS);
|
2009-06-12 10:03:25 +00:00
|
|
|
for (int i = 0; i < keys->length(); i++) {
|
|
|
|
// Names of variables introduced by eval are strings.
|
|
|
|
ASSERT(keys->get(i)->IsString());
|
|
|
|
Handle<String> key(String::cast(keys->get(i)));
|
|
|
|
SetProperty(closure_scope, key, GetProperty(ext, key), NONE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return closure_scope;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Iterate over the actual scopes visible from a stack frame. All scopes are
|
|
|
|
// backed by an actual context except the local scope, which is inserted
|
|
|
|
// "artifically" in the context chain.
|
|
|
|
class ScopeIterator {
|
|
|
|
public:
|
|
|
|
enum ScopeType {
|
|
|
|
ScopeTypeGlobal = 0,
|
|
|
|
ScopeTypeLocal,
|
|
|
|
ScopeTypeWith,
|
2009-09-08 10:20:28 +00:00
|
|
|
ScopeTypeClosure,
|
|
|
|
// Every catch block contains an implicit with block (its parameter is
|
|
|
|
// a JSContextExtensionObject) that extends current scope with a variable
|
|
|
|
// holding exception object. Such with blocks are treated as scopes of their
|
|
|
|
// own type.
|
|
|
|
ScopeTypeCatch
|
2009-06-12 10:03:25 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
explicit ScopeIterator(JavaScriptFrame* frame)
|
|
|
|
: frame_(frame),
|
|
|
|
function_(JSFunction::cast(frame->function())),
|
|
|
|
context_(Context::cast(frame->context())),
|
|
|
|
local_done_(false),
|
|
|
|
at_local_(false) {
|
|
|
|
|
|
|
|
// Check whether the first scope is actually a local scope.
|
|
|
|
if (context_->IsGlobalContext()) {
|
|
|
|
// If there is a stack slot for .result then this local scope has been
|
|
|
|
// created for evaluating top level code and it is not a real local scope.
|
|
|
|
// Checking for the existence of .result seems fragile, but the scope info
|
|
|
|
// saved with the code object does not otherwise have that information.
|
|
|
|
Handle<Code> code(function_->code());
|
|
|
|
int index = ScopeInfo<>::StackSlotIndex(*code, Heap::result_symbol());
|
|
|
|
at_local_ = index < 0;
|
|
|
|
} else if (context_->is_function_context()) {
|
|
|
|
at_local_ = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// More scopes?
|
|
|
|
bool Done() { return context_.is_null(); }
|
|
|
|
|
|
|
|
// Move to the next scope.
|
|
|
|
void Next() {
|
|
|
|
// If at a local scope mark the local scope as passed.
|
|
|
|
if (at_local_) {
|
|
|
|
at_local_ = false;
|
|
|
|
local_done_ = true;
|
|
|
|
|
|
|
|
// If the current context is not associated with the local scope the
|
|
|
|
// current context is the next real scope, so don't move to the next
|
|
|
|
// context in this case.
|
|
|
|
if (context_->closure() != *function_) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// The global scope is always the last in the chain.
|
|
|
|
if (context_->IsGlobalContext()) {
|
|
|
|
context_ = Handle<Context>();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Move to the next context.
|
|
|
|
if (context_->is_function_context()) {
|
|
|
|
context_ = Handle<Context>(Context::cast(context_->closure()->context()));
|
|
|
|
} else {
|
|
|
|
context_ = Handle<Context>(context_->previous());
|
|
|
|
}
|
|
|
|
|
|
|
|
// If passing the local scope indicate that the current scope is now the
|
|
|
|
// local scope.
|
|
|
|
if (!local_done_ &&
|
|
|
|
(context_->IsGlobalContext() || (context_->is_function_context()))) {
|
|
|
|
at_local_ = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return the type of the current scope.
|
|
|
|
int Type() {
|
|
|
|
if (at_local_) {
|
|
|
|
return ScopeTypeLocal;
|
|
|
|
}
|
|
|
|
if (context_->IsGlobalContext()) {
|
|
|
|
ASSERT(context_->global()->IsGlobalObject());
|
|
|
|
return ScopeTypeGlobal;
|
|
|
|
}
|
|
|
|
if (context_->is_function_context()) {
|
|
|
|
return ScopeTypeClosure;
|
|
|
|
}
|
|
|
|
ASSERT(context_->has_extension());
|
2009-09-08 10:20:28 +00:00
|
|
|
// Current scope is either an explicit with statement or a with statement
|
|
|
|
// implicitely generated for a catch block.
|
|
|
|
// If the extension object here is a JSContextExtensionObject then
|
|
|
|
// current with statement is one frome a catch block otherwise it's a
|
|
|
|
// regular with statement.
|
|
|
|
if (context_->extension()->IsJSContextExtensionObject()) {
|
|
|
|
return ScopeTypeCatch;
|
|
|
|
}
|
2009-06-12 10:03:25 +00:00
|
|
|
return ScopeTypeWith;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return the JavaScript object with the content of the current scope.
|
|
|
|
Handle<JSObject> ScopeObject() {
|
|
|
|
switch (Type()) {
|
|
|
|
case ScopeIterator::ScopeTypeGlobal:
|
|
|
|
return Handle<JSObject>(CurrentContext()->global());
|
|
|
|
break;
|
|
|
|
case ScopeIterator::ScopeTypeLocal:
|
|
|
|
// Materialize the content of the local scope into a JSObject.
|
|
|
|
return MaterializeLocalScope(frame_);
|
|
|
|
break;
|
|
|
|
case ScopeIterator::ScopeTypeWith:
|
2009-09-08 10:20:28 +00:00
|
|
|
case ScopeIterator::ScopeTypeCatch:
|
2009-06-12 10:03:25 +00:00
|
|
|
// Return the with object.
|
|
|
|
return Handle<JSObject>(CurrentContext()->extension());
|
|
|
|
break;
|
|
|
|
case ScopeIterator::ScopeTypeClosure:
|
|
|
|
// Materialize the content of the closure scope into a JSObject.
|
|
|
|
return MaterializeClosure(CurrentContext());
|
|
|
|
break;
|
|
|
|
}
|
2009-06-12 10:25:05 +00:00
|
|
|
UNREACHABLE();
|
|
|
|
return Handle<JSObject>();
|
2009-06-12 10:03:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Return the context for this scope. For the local context there might not
|
|
|
|
// be an actual context.
|
|
|
|
Handle<Context> CurrentContext() {
|
|
|
|
if (at_local_ && context_->closure() != *function_) {
|
|
|
|
return Handle<Context>();
|
|
|
|
}
|
|
|
|
return context_;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
// Debug print of the content of the current scope.
|
|
|
|
void DebugPrint() {
|
|
|
|
switch (Type()) {
|
|
|
|
case ScopeIterator::ScopeTypeGlobal:
|
|
|
|
PrintF("Global:\n");
|
|
|
|
CurrentContext()->Print();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ScopeIterator::ScopeTypeLocal: {
|
|
|
|
PrintF("Local:\n");
|
|
|
|
Handle<Code> code(function_->code());
|
|
|
|
ScopeInfo<> scope_info(*code);
|
|
|
|
scope_info.Print();
|
|
|
|
if (!CurrentContext().is_null()) {
|
|
|
|
CurrentContext()->Print();
|
|
|
|
if (CurrentContext()->has_extension()) {
|
|
|
|
Handle<JSObject> extension =
|
|
|
|
Handle<JSObject>(CurrentContext()->extension());
|
|
|
|
if (extension->IsJSContextExtensionObject()) {
|
|
|
|
extension->Print();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case ScopeIterator::ScopeTypeWith: {
|
|
|
|
PrintF("With:\n");
|
|
|
|
Handle<JSObject> extension =
|
|
|
|
Handle<JSObject>(CurrentContext()->extension());
|
|
|
|
extension->Print();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2009-09-08 10:20:28 +00:00
|
|
|
case ScopeIterator::ScopeTypeCatch: {
|
|
|
|
PrintF("Catch:\n");
|
|
|
|
Handle<JSObject> extension =
|
|
|
|
Handle<JSObject>(CurrentContext()->extension());
|
|
|
|
extension->Print();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2009-06-12 10:03:25 +00:00
|
|
|
case ScopeIterator::ScopeTypeClosure: {
|
|
|
|
PrintF("Closure:\n");
|
|
|
|
CurrentContext()->Print();
|
|
|
|
if (CurrentContext()->has_extension()) {
|
|
|
|
Handle<JSObject> extension =
|
|
|
|
Handle<JSObject>(CurrentContext()->extension());
|
|
|
|
if (extension->IsJSContextExtensionObject()) {
|
|
|
|
extension->Print();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
}
|
|
|
|
PrintF("\n");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
private:
|
|
|
|
JavaScriptFrame* frame_;
|
|
|
|
Handle<JSFunction> function_;
|
|
|
|
Handle<Context> context_;
|
|
|
|
bool local_done_;
|
|
|
|
bool at_local_;
|
|
|
|
|
|
|
|
DISALLOW_IMPLICIT_CONSTRUCTORS(ScopeIterator);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
static Object* Runtime_GetScopeCount(Arguments args) {
|
|
|
|
HandleScope scope;
|
|
|
|
ASSERT(args.length() == 2);
|
|
|
|
|
|
|
|
// Check arguments.
|
|
|
|
Object* check = Runtime_CheckExecutionState(args);
|
|
|
|
if (check->IsFailure()) return check;
|
|
|
|
CONVERT_CHECKED(Smi, wrapped_id, args[1]);
|
|
|
|
|
|
|
|
// Get the frame where the debugging is performed.
|
|
|
|
StackFrame::Id id = UnwrapFrameId(wrapped_id);
|
|
|
|
JavaScriptFrameIterator it(id);
|
|
|
|
JavaScriptFrame* frame = it.frame();
|
|
|
|
|
|
|
|
// Count the visible scopes.
|
|
|
|
int n = 0;
|
|
|
|
for (ScopeIterator it(frame); !it.Done(); it.Next()) {
|
|
|
|
n++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Smi::FromInt(n);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static const int kScopeDetailsTypeIndex = 0;
|
|
|
|
static const int kScopeDetailsObjectIndex = 1;
|
|
|
|
static const int kScopeDetailsSize = 2;
|
|
|
|
|
|
|
|
// Return an array with scope details
|
|
|
|
// args[0]: number: break id
|
|
|
|
// args[1]: number: frame index
|
|
|
|
// args[2]: number: scope index
|
|
|
|
//
|
|
|
|
// The array returned contains the following information:
|
|
|
|
// 0: Scope type
|
|
|
|
// 1: Scope object
|
|
|
|
static Object* Runtime_GetScopeDetails(Arguments args) {
|
|
|
|
HandleScope scope;
|
|
|
|
ASSERT(args.length() == 3);
|
|
|
|
|
|
|
|
// Check arguments.
|
|
|
|
Object* check = Runtime_CheckExecutionState(args);
|
|
|
|
if (check->IsFailure()) return check;
|
|
|
|
CONVERT_CHECKED(Smi, wrapped_id, args[1]);
|
|
|
|
CONVERT_NUMBER_CHECKED(int, index, Int32, args[2]);
|
|
|
|
|
|
|
|
// Get the frame where the debugging is performed.
|
|
|
|
StackFrame::Id id = UnwrapFrameId(wrapped_id);
|
|
|
|
JavaScriptFrameIterator frame_it(id);
|
|
|
|
JavaScriptFrame* frame = frame_it.frame();
|
|
|
|
|
|
|
|
// Find the requested scope.
|
|
|
|
int n = 0;
|
|
|
|
ScopeIterator it(frame);
|
|
|
|
for (; !it.Done() && n < index; it.Next()) {
|
|
|
|
n++;
|
|
|
|
}
|
|
|
|
if (it.Done()) {
|
|
|
|
return Heap::undefined_value();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Calculate the size of the result.
|
|
|
|
int details_size = kScopeDetailsSize;
|
|
|
|
Handle<FixedArray> details = Factory::NewFixedArray(details_size);
|
|
|
|
|
|
|
|
// Fill in scope details.
|
|
|
|
details->set(kScopeDetailsTypeIndex, Smi::FromInt(it.Type()));
|
|
|
|
details->set(kScopeDetailsObjectIndex, *it.ScopeObject());
|
|
|
|
|
|
|
|
return *Factory::NewJSArrayWithElements(details);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static Object* Runtime_DebugPrintScopes(Arguments args) {
|
|
|
|
HandleScope scope;
|
|
|
|
ASSERT(args.length() == 0);
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
// Print the scopes for the top frame.
|
|
|
|
StackFrameLocator locator;
|
|
|
|
JavaScriptFrame* frame = locator.FindJavaScriptFrame(0);
|
|
|
|
for (ScopeIterator it(frame); !it.Done(); it.Next()) {
|
|
|
|
it.DebugPrint();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return Heap::undefined_value();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
static Object* Runtime_GetCFrames(Arguments args) {
|
|
|
|
HandleScope scope;
|
|
|
|
ASSERT(args.length() == 1);
|
|
|
|
Object* result = Runtime_CheckExecutionState(args);
|
|
|
|
if (result->IsFailure()) return result;
|
|
|
|
|
2009-05-12 10:33:40 +00:00
|
|
|
#if V8_HOST_ARCH_64_BIT
|
|
|
|
UNIMPLEMENTED();
|
|
|
|
return Heap::undefined_value();
|
|
|
|
#else
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
static const int kMaxCFramesSize = 200;
|
2009-04-16 21:01:05 +00:00
|
|
|
ScopedVector<OS::StackFrame> frames(kMaxCFramesSize);
|
|
|
|
int frames_count = OS::StackWalk(frames);
|
2008-07-03 15:10:15 +00:00
|
|
|
if (frames_count == OS::kStackWalkError) {
|
|
|
|
return Heap::undefined_value();
|
|
|
|
}
|
|
|
|
|
|
|
|
Handle<String> address_str = Factory::LookupAsciiSymbol("address");
|
|
|
|
Handle<String> text_str = Factory::LookupAsciiSymbol("text");
|
|
|
|
Handle<FixedArray> frames_array = Factory::NewFixedArray(frames_count);
|
|
|
|
for (int i = 0; i < frames_count; i++) {
|
|
|
|
Handle<JSObject> frame_value = Factory::NewJSObject(Top::object_function());
|
|
|
|
frame_value->SetProperty(
|
|
|
|
*address_str,
|
|
|
|
*Factory::NewNumberFromInt(reinterpret_cast<int>(frames[i].address)),
|
|
|
|
NONE);
|
|
|
|
|
|
|
|
// Get the stack walk text for this frame.
|
|
|
|
Handle<String> frame_text;
|
|
|
|
if (strlen(frames[i].text) > 0) {
|
|
|
|
Vector<const char> str(frames[i].text, strlen(frames[i].text));
|
|
|
|
frame_text = Factory::NewStringFromAscii(str);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!frame_text.is_null()) {
|
|
|
|
frame_value->SetProperty(*text_str, *frame_text, NONE);
|
|
|
|
}
|
|
|
|
|
|
|
|
frames_array->set(i, *frame_value);
|
|
|
|
}
|
|
|
|
return *Factory::NewJSArrayWithElements(frames_array);
|
2009-05-12 10:33:40 +00:00
|
|
|
#endif // V8_HOST_ARCH_64_BIT
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-17 11:40:47 +00:00
|
|
|
static Object* Runtime_GetThreadCount(Arguments args) {
|
|
|
|
HandleScope scope;
|
|
|
|
ASSERT(args.length() == 1);
|
|
|
|
|
|
|
|
// Check arguments.
|
|
|
|
Object* result = Runtime_CheckExecutionState(args);
|
|
|
|
if (result->IsFailure()) return result;
|
|
|
|
|
|
|
|
// Count all archived V8 threads.
|
|
|
|
int n = 0;
|
|
|
|
for (ThreadState* thread = ThreadState::FirstInUse();
|
|
|
|
thread != NULL;
|
|
|
|
thread = thread->Next()) {
|
|
|
|
n++;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Total number of threads is current thread and archived threads.
|
|
|
|
return Smi::FromInt(n + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static const int kThreadDetailsCurrentThreadIndex = 0;
|
|
|
|
static const int kThreadDetailsThreadIdIndex = 1;
|
|
|
|
static const int kThreadDetailsSize = 2;
|
|
|
|
|
|
|
|
// Return an array with thread details
|
|
|
|
// args[0]: number: break id
|
|
|
|
// args[1]: number: thread index
|
|
|
|
//
|
|
|
|
// The array returned contains the following information:
|
|
|
|
// 0: Is current thread?
|
|
|
|
// 1: Thread id
|
|
|
|
static Object* Runtime_GetThreadDetails(Arguments args) {
|
|
|
|
HandleScope scope;
|
|
|
|
ASSERT(args.length() == 2);
|
|
|
|
|
|
|
|
// Check arguments.
|
|
|
|
Object* check = Runtime_CheckExecutionState(args);
|
|
|
|
if (check->IsFailure()) return check;
|
|
|
|
CONVERT_NUMBER_CHECKED(int, index, Int32, args[1]);
|
|
|
|
|
|
|
|
// Allocate array for result.
|
|
|
|
Handle<FixedArray> details = Factory::NewFixedArray(kThreadDetailsSize);
|
|
|
|
|
|
|
|
// Thread index 0 is current thread.
|
|
|
|
if (index == 0) {
|
|
|
|
// Fill the details.
|
|
|
|
details->set(kThreadDetailsCurrentThreadIndex, Heap::true_value());
|
|
|
|
details->set(kThreadDetailsThreadIdIndex,
|
|
|
|
Smi::FromInt(ThreadManager::CurrentId()));
|
|
|
|
} else {
|
|
|
|
// Find the thread with the requested index.
|
|
|
|
int n = 1;
|
|
|
|
ThreadState* thread = ThreadState::FirstInUse();
|
|
|
|
while (index != n && thread != NULL) {
|
|
|
|
thread = thread->Next();
|
|
|
|
n++;
|
|
|
|
}
|
|
|
|
if (thread == NULL) {
|
|
|
|
return Heap::undefined_value();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fill the details.
|
|
|
|
details->set(kThreadDetailsCurrentThreadIndex, Heap::false_value());
|
|
|
|
details->set(kThreadDetailsThreadIdIndex, Smi::FromInt(thread->id()));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Convert to JS array and return.
|
|
|
|
return *Factory::NewJSArrayWithElements(details);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
static Object* Runtime_GetBreakLocations(Arguments args) {
|
|
|
|
HandleScope scope;
|
|
|
|
ASSERT(args.length() == 1);
|
|
|
|
|
2009-06-17 11:44:25 +00:00
|
|
|
CONVERT_ARG_CHECKED(JSFunction, fun, 0);
|
|
|
|
Handle<SharedFunctionInfo> shared(fun->shared());
|
2008-07-03 15:10:15 +00:00
|
|
|
// Find the number of break points
|
|
|
|
Handle<Object> break_locations = Debug::GetSourceBreakLocations(shared);
|
|
|
|
if (break_locations->IsUndefined()) return Heap::undefined_value();
|
|
|
|
// Return array as JS array
|
|
|
|
return *Factory::NewJSArrayWithElements(
|
|
|
|
Handle<FixedArray>::cast(break_locations));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Set a break point in a function
|
|
|
|
// args[0]: function
|
|
|
|
// args[1]: number: break source position (within the function source)
|
|
|
|
// args[2]: number: break point object
|
|
|
|
static Object* Runtime_SetFunctionBreakPoint(Arguments args) {
|
|
|
|
HandleScope scope;
|
|
|
|
ASSERT(args.length() == 3);
|
2009-06-17 11:44:25 +00:00
|
|
|
CONVERT_ARG_CHECKED(JSFunction, fun, 0);
|
|
|
|
Handle<SharedFunctionInfo> shared(fun->shared());
|
2008-07-03 15:10:15 +00:00
|
|
|
CONVERT_NUMBER_CHECKED(int32_t, source_position, Int32, args[1]);
|
|
|
|
RUNTIME_ASSERT(source_position >= 0);
|
|
|
|
Handle<Object> break_point_object_arg = args.at<Object>(2);
|
|
|
|
|
|
|
|
// Set break point.
|
|
|
|
Debug::SetBreakPoint(shared, source_position, break_point_object_arg);
|
|
|
|
|
|
|
|
return Heap::undefined_value();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-04-14 00:51:59 +00:00
|
|
|
Object* Runtime::FindSharedFunctionInfoInScript(Handle<Script> script,
|
|
|
|
int position) {
|
2008-07-03 15:10:15 +00:00
|
|
|
// Iterate the heap looking for SharedFunctionInfo generated from the
|
|
|
|
// script. The inner most SharedFunctionInfo containing the source position
|
|
|
|
// for the requested break point is found.
|
|
|
|
// NOTE: This might reqire several heap iterations. If the SharedFunctionInfo
|
|
|
|
// which is found is not compiled it is compiled and the heap is iterated
|
|
|
|
// again as the compilation might create inner functions from the newly
|
|
|
|
// compiled function and the actual requested break point might be in one of
|
|
|
|
// these functions.
|
|
|
|
bool done = false;
|
|
|
|
// The current candidate for the source position:
|
2008-09-22 13:57:03 +00:00
|
|
|
int target_start_position = RelocInfo::kNoPosition;
|
2008-07-03 15:10:15 +00:00
|
|
|
Handle<SharedFunctionInfo> target;
|
|
|
|
// The current candidate for the last function in script:
|
|
|
|
Handle<SharedFunctionInfo> last;
|
|
|
|
while (!done) {
|
|
|
|
HeapIterator iterator;
|
|
|
|
while (iterator.has_next()) {
|
|
|
|
HeapObject* obj = iterator.next();
|
|
|
|
ASSERT(obj != NULL);
|
|
|
|
if (obj->IsSharedFunctionInfo()) {
|
|
|
|
Handle<SharedFunctionInfo> shared(SharedFunctionInfo::cast(obj));
|
|
|
|
if (shared->script() == *script) {
|
|
|
|
// If the SharedFunctionInfo found has the requested script data and
|
|
|
|
// contains the source position it is a candidate.
|
|
|
|
int start_position = shared->function_token_position();
|
2008-09-22 13:57:03 +00:00
|
|
|
if (start_position == RelocInfo::kNoPosition) {
|
2008-07-03 15:10:15 +00:00
|
|
|
start_position = shared->start_position();
|
|
|
|
}
|
|
|
|
if (start_position <= position &&
|
|
|
|
position <= shared->end_position()) {
|
2009-01-15 19:08:34 +00:00
|
|
|
// If there is no candidate or this function is within the current
|
2008-07-03 15:10:15 +00:00
|
|
|
// candidate this is the new candidate.
|
|
|
|
if (target.is_null()) {
|
|
|
|
target_start_position = start_position;
|
|
|
|
target = shared;
|
|
|
|
} else {
|
2009-09-09 15:16:26 +00:00
|
|
|
if (target_start_position == start_position &&
|
|
|
|
shared->end_position() == target->end_position()) {
|
|
|
|
// If a top-level function contain only one function
|
|
|
|
// declartion the source for the top-level and the function is
|
|
|
|
// the same. In that case prefer the non top-level function.
|
|
|
|
if (!shared->is_toplevel()) {
|
|
|
|
target_start_position = start_position;
|
|
|
|
target = shared;
|
|
|
|
}
|
|
|
|
} else if (target_start_position <= start_position &&
|
|
|
|
shared->end_position() <= target->end_position()) {
|
|
|
|
// This containment check includes equality as a function inside
|
|
|
|
// a top-level function can share either start or end position
|
|
|
|
// with the top-level function.
|
2008-07-03 15:10:15 +00:00
|
|
|
target_start_position = start_position;
|
|
|
|
target = shared;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Keep track of the last function in the script.
|
|
|
|
if (last.is_null() ||
|
|
|
|
shared->end_position() > last->start_position()) {
|
|
|
|
last = shared;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure some candidate is selected.
|
|
|
|
if (target.is_null()) {
|
|
|
|
if (!last.is_null()) {
|
|
|
|
// Position after the last function - use last.
|
|
|
|
target = last;
|
|
|
|
} else {
|
|
|
|
// Unable to find function - possibly script without any function.
|
|
|
|
return Heap::undefined_value();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the candidate found is compiled we are done. NOTE: when lazy
|
|
|
|
// compilation of inner functions is introduced some additional checking
|
|
|
|
// needs to be done here to compile inner functions.
|
|
|
|
done = target->is_compiled();
|
|
|
|
if (!done) {
|
|
|
|
// If the candidate is not compiled compile it to reveal any inner
|
|
|
|
// functions which might contain the requested source position.
|
2008-11-17 05:50:52 +00:00
|
|
|
CompileLazyShared(target, KEEP_EXCEPTION, 0);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return *target;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Change the state of a break point in a script. NOTE: Regarding performance
|
|
|
|
// see the NOTE for GetScriptFromScriptData.
|
|
|
|
// args[0]: script to set break point in
|
|
|
|
// args[1]: number: break source position (within the script source)
|
|
|
|
// args[2]: number: break point object
|
|
|
|
static Object* Runtime_SetScriptBreakPoint(Arguments args) {
|
|
|
|
HandleScope scope;
|
|
|
|
ASSERT(args.length() == 3);
|
|
|
|
CONVERT_ARG_CHECKED(JSValue, wrapper, 0);
|
|
|
|
CONVERT_NUMBER_CHECKED(int32_t, source_position, Int32, args[1]);
|
|
|
|
RUNTIME_ASSERT(source_position >= 0);
|
|
|
|
Handle<Object> break_point_object_arg = args.at<Object>(2);
|
|
|
|
|
|
|
|
// Get the script from the script wrapper.
|
|
|
|
RUNTIME_ASSERT(wrapper->value()->IsScript());
|
|
|
|
Handle<Script> script(Script::cast(wrapper->value()));
|
|
|
|
|
2009-04-14 00:51:59 +00:00
|
|
|
Object* result = Runtime::FindSharedFunctionInfoInScript(
|
|
|
|
script, source_position);
|
2008-07-03 15:10:15 +00:00
|
|
|
if (!result->IsUndefined()) {
|
|
|
|
Handle<SharedFunctionInfo> shared(SharedFunctionInfo::cast(result));
|
|
|
|
// Find position within function. The script position might be before the
|
|
|
|
// source position of the first function.
|
|
|
|
int position;
|
|
|
|
if (shared->start_position() > source_position) {
|
|
|
|
position = 0;
|
|
|
|
} else {
|
|
|
|
position = source_position - shared->start_position();
|
|
|
|
}
|
|
|
|
Debug::SetBreakPoint(shared, position, break_point_object_arg);
|
|
|
|
}
|
|
|
|
return Heap::undefined_value();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Clear a break point
|
|
|
|
// args[0]: number: break point object
|
|
|
|
static Object* Runtime_ClearBreakPoint(Arguments args) {
|
|
|
|
HandleScope scope;
|
|
|
|
ASSERT(args.length() == 1);
|
|
|
|
Handle<Object> break_point_object_arg = args.at<Object>(0);
|
|
|
|
|
|
|
|
// Clear break point.
|
|
|
|
Debug::ClearBreakPoint(break_point_object_arg);
|
|
|
|
|
|
|
|
return Heap::undefined_value();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Change the state of break on exceptions
|
|
|
|
// args[0]: boolean indicating uncaught exceptions
|
|
|
|
// args[1]: boolean indicating on/off
|
|
|
|
static Object* Runtime_ChangeBreakOnException(Arguments args) {
|
|
|
|
HandleScope scope;
|
|
|
|
ASSERT(args.length() == 2);
|
|
|
|
ASSERT(args[0]->IsNumber());
|
|
|
|
ASSERT(args[1]->IsBoolean());
|
|
|
|
|
|
|
|
// Update break point state
|
|
|
|
ExceptionBreakType type =
|
|
|
|
static_cast<ExceptionBreakType>(NumberToUint32(args[0]));
|
|
|
|
bool enable = args[1]->ToBoolean()->IsTrue();
|
|
|
|
Debug::ChangeBreakOnException(type, enable);
|
|
|
|
return Heap::undefined_value();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Prepare for stepping
|
|
|
|
// args[0]: break id for checking execution state
|
|
|
|
// args[1]: step action from the enumeration StepAction
|
2009-09-09 08:40:59 +00:00
|
|
|
// args[2]: number of times to perform the step, for step out it is the number
|
|
|
|
// of frames to step down.
|
2008-07-03 15:10:15 +00:00
|
|
|
static Object* Runtime_PrepareStep(Arguments args) {
|
|
|
|
HandleScope scope;
|
|
|
|
ASSERT(args.length() == 3);
|
|
|
|
// Check arguments.
|
2008-08-27 10:11:39 +00:00
|
|
|
Object* check = Runtime_CheckExecutionState(args);
|
|
|
|
if (check->IsFailure()) return check;
|
2008-07-03 15:10:15 +00:00
|
|
|
if (!args[1]->IsNumber() || !args[2]->IsNumber()) {
|
|
|
|
return Top::Throw(Heap::illegal_argument_symbol());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the step action and check validity.
|
|
|
|
StepAction step_action = static_cast<StepAction>(NumberToInt32(args[1]));
|
|
|
|
if (step_action != StepIn &&
|
|
|
|
step_action != StepNext &&
|
|
|
|
step_action != StepOut &&
|
|
|
|
step_action != StepInMin &&
|
|
|
|
step_action != StepMin) {
|
|
|
|
return Top::Throw(Heap::illegal_argument_symbol());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the number of steps.
|
|
|
|
int step_count = NumberToInt32(args[2]);
|
|
|
|
if (step_count < 1) {
|
|
|
|
return Top::Throw(Heap::illegal_argument_symbol());
|
|
|
|
}
|
|
|
|
|
2009-09-09 08:40:59 +00:00
|
|
|
// Clear all current stepping setup.
|
|
|
|
Debug::ClearStepping();
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
// Prepare step.
|
|
|
|
Debug::PrepareStep(static_cast<StepAction>(step_action), step_count);
|
|
|
|
return Heap::undefined_value();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Clear all stepping set by PrepareStep.
|
|
|
|
static Object* Runtime_ClearStepping(Arguments args) {
|
|
|
|
HandleScope scope;
|
2008-08-13 09:32:07 +00:00
|
|
|
ASSERT(args.length() == 0);
|
2008-07-03 15:10:15 +00:00
|
|
|
Debug::ClearStepping();
|
|
|
|
return Heap::undefined_value();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Creates a copy of the with context chain. The copy of the context chain is
|
|
|
|
// is linked to the function context supplied.
|
|
|
|
static Handle<Context> CopyWithContextChain(Handle<Context> context_chain,
|
|
|
|
Handle<Context> function_context) {
|
|
|
|
// At the bottom of the chain. Return the function context to link to.
|
|
|
|
if (context_chain->is_function_context()) {
|
|
|
|
return function_context;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Recursively copy the with contexts.
|
|
|
|
Handle<Context> previous(context_chain->previous());
|
|
|
|
Handle<JSObject> extension(JSObject::cast(context_chain->extension()));
|
|
|
|
return Factory::NewWithContext(
|
2008-12-18 11:28:13 +00:00
|
|
|
CopyWithContextChain(function_context, previous),
|
|
|
|
extension,
|
|
|
|
context_chain->IsCatchContext());
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Helper function to find or create the arguments object for
|
|
|
|
// Runtime_DebugEvaluate.
|
|
|
|
static Handle<Object> GetArgumentsObject(JavaScriptFrame* frame,
|
|
|
|
Handle<JSFunction> function,
|
|
|
|
Handle<Code> code,
|
|
|
|
const ScopeInfo<>* sinfo,
|
|
|
|
Handle<Context> function_context) {
|
|
|
|
// Try to find the value of 'arguments' to pass as parameter. If it is not
|
|
|
|
// found (that is the debugged function does not reference 'arguments' and
|
|
|
|
// does not support eval) then create an 'arguments' object.
|
|
|
|
int index;
|
|
|
|
if (sinfo->number_of_stack_slots() > 0) {
|
|
|
|
index = ScopeInfo<>::StackSlotIndex(*code, Heap::arguments_symbol());
|
|
|
|
if (index != -1) {
|
|
|
|
return Handle<Object>(frame->GetExpression(index));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sinfo->number_of_context_slots() > Context::MIN_CONTEXT_SLOTS) {
|
|
|
|
index = ScopeInfo<>::ContextSlotIndex(*code, Heap::arguments_symbol(),
|
|
|
|
NULL);
|
|
|
|
if (index != -1) {
|
|
|
|
return Handle<Object>(function_context->get(index));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const int length = frame->GetProvidedParametersCount();
|
2008-10-23 14:55:45 +00:00
|
|
|
Handle<JSObject> arguments = Factory::NewArgumentsObject(function, length);
|
|
|
|
Handle<FixedArray> array = Factory::NewFixedArray(length);
|
2008-10-23 08:46:32 +00:00
|
|
|
WriteBarrierMode mode = array->GetWriteBarrierMode();
|
2008-07-03 15:10:15 +00:00
|
|
|
for (int i = 0; i < length; i++) {
|
2008-10-22 09:47:20 +00:00
|
|
|
array->set(i, frame->GetParameter(i), mode);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
2008-10-23 14:55:45 +00:00
|
|
|
arguments->set_elements(*array);
|
2008-07-03 15:10:15 +00:00
|
|
|
return arguments;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Evaluate a piece of JavaScript in the context of a stack frame for
|
2009-01-15 19:08:34 +00:00
|
|
|
// debugging. This is accomplished by creating a new context which in its
|
2008-07-03 15:10:15 +00:00
|
|
|
// extension part has all the parameters and locals of the function on the
|
|
|
|
// stack frame. A function which calls eval with the code to evaluate is then
|
|
|
|
// compiled in this context and called in this context. As this context
|
|
|
|
// replaces the context of the function on the stack frame a new (empty)
|
|
|
|
// function is created as well to be used as the closure for the context.
|
|
|
|
// This function and the context acts as replacements for the function on the
|
|
|
|
// stack frame presenting the same view of the values of parameters and
|
|
|
|
// local variables as if the piece of JavaScript was evaluated at the point
|
|
|
|
// where the function on the stack frame is currently stopped.
|
|
|
|
static Object* Runtime_DebugEvaluate(Arguments args) {
|
|
|
|
HandleScope scope;
|
|
|
|
|
|
|
|
// Check the execution state and decode arguments frame and source to be
|
|
|
|
// evaluated.
|
2008-07-09 11:06:54 +00:00
|
|
|
ASSERT(args.length() == 4);
|
2008-07-03 15:10:15 +00:00
|
|
|
Object* check_result = Runtime_CheckExecutionState(args);
|
|
|
|
if (check_result->IsFailure()) return check_result;
|
|
|
|
CONVERT_CHECKED(Smi, wrapped_id, args[1]);
|
|
|
|
CONVERT_ARG_CHECKED(String, source, 2);
|
2008-07-09 11:06:54 +00:00
|
|
|
CONVERT_BOOLEAN_CHECKED(disable_break, args[3]);
|
|
|
|
|
|
|
|
// Handle the processing of break.
|
|
|
|
DisableBreak disable_break_save(disable_break);
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
// Get the frame where the debugging is performed.
|
|
|
|
StackFrame::Id id = UnwrapFrameId(wrapped_id);
|
|
|
|
JavaScriptFrameIterator it(id);
|
|
|
|
JavaScriptFrame* frame = it.frame();
|
|
|
|
Handle<JSFunction> function(JSFunction::cast(frame->function()));
|
|
|
|
Handle<Code> code(function->code());
|
|
|
|
ScopeInfo<> sinfo(*code);
|
|
|
|
|
|
|
|
// Traverse the saved contexts chain to find the active context for the
|
|
|
|
// selected frame.
|
|
|
|
SaveContext* save = Top::save_context();
|
2008-11-17 17:44:16 +00:00
|
|
|
while (save != NULL && !save->below(frame)) {
|
2008-07-03 15:10:15 +00:00
|
|
|
save = save->prev();
|
|
|
|
}
|
|
|
|
ASSERT(save != NULL);
|
|
|
|
SaveContext savex;
|
|
|
|
Top::set_context(*(save->context()));
|
|
|
|
|
|
|
|
// Create the (empty) function replacing the function on the stack frame for
|
|
|
|
// the purpose of evaluating in the context created below. It is important
|
|
|
|
// that this function does not describe any parameters and local variables
|
|
|
|
// in the context. If it does then this will cause problems with the lookup
|
|
|
|
// in Context::Lookup, where context slots for parameters and local variables
|
|
|
|
// are looked at before the extension object.
|
|
|
|
Handle<JSFunction> go_between =
|
|
|
|
Factory::NewFunction(Factory::empty_string(), Factory::undefined_value());
|
|
|
|
go_between->set_context(function->context());
|
|
|
|
#ifdef DEBUG
|
|
|
|
ScopeInfo<> go_between_sinfo(go_between->shared()->code());
|
|
|
|
ASSERT(go_between_sinfo.number_of_parameters() == 0);
|
|
|
|
ASSERT(go_between_sinfo.number_of_context_slots() == 0);
|
|
|
|
#endif
|
|
|
|
|
2009-06-12 10:03:25 +00:00
|
|
|
// Materialize the content of the local scope into a JSObject.
|
|
|
|
Handle<JSObject> local_scope = MaterializeLocalScope(frame);
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
// Allocate a new context for the debug evaluation and set the extension
|
|
|
|
// object build.
|
|
|
|
Handle<Context> context =
|
|
|
|
Factory::NewFunctionContext(Context::MIN_CONTEXT_SLOTS, go_between);
|
2009-06-12 10:03:25 +00:00
|
|
|
context->set_extension(*local_scope);
|
2008-07-03 15:10:15 +00:00
|
|
|
// Copy any with contexts present and chain them in front of this context.
|
2009-06-12 10:03:25 +00:00
|
|
|
Handle<Context> frame_context(Context::cast(frame->context()));
|
|
|
|
Handle<Context> function_context(frame_context->fcontext());
|
2008-07-03 15:10:15 +00:00
|
|
|
context = CopyWithContextChain(frame_context, context);
|
|
|
|
|
|
|
|
// Wrap the evaluation statement in a new function compiled in the newly
|
|
|
|
// created context. The function has one parameter which has to be called
|
|
|
|
// 'arguments'. This it to have access to what would have been 'arguments' in
|
2009-01-15 19:08:34 +00:00
|
|
|
// the function being debugged.
|
2008-07-03 15:10:15 +00:00
|
|
|
// function(arguments,__source__) {return eval(__source__);}
|
|
|
|
static const char* source_str =
|
|
|
|
"function(arguments,__source__){return eval(__source__);}";
|
|
|
|
static const int source_str_length = strlen(source_str);
|
|
|
|
Handle<String> function_source =
|
|
|
|
Factory::NewStringFromAscii(Vector<const char>(source_str,
|
|
|
|
source_str_length));
|
|
|
|
Handle<JSFunction> boilerplate =
|
2009-02-24 13:11:53 +00:00
|
|
|
Compiler::CompileEval(function_source,
|
|
|
|
context,
|
2009-04-24 12:45:29 +00:00
|
|
|
context->IsGlobalContext(),
|
2009-08-13 10:25:35 +00:00
|
|
|
Compiler::DONT_VALIDATE_JSON);
|
2008-07-03 15:10:15 +00:00
|
|
|
if (boilerplate.is_null()) return Failure::Exception();
|
|
|
|
Handle<JSFunction> compiled_function =
|
|
|
|
Factory::NewFunctionFromBoilerplate(boilerplate, context);
|
|
|
|
|
|
|
|
// Invoke the result of the compilation to get the evaluation function.
|
|
|
|
bool has_pending_exception;
|
|
|
|
Handle<Object> receiver(frame->receiver());
|
|
|
|
Handle<Object> evaluation_function =
|
|
|
|
Execution::Call(compiled_function, receiver, 0, NULL,
|
|
|
|
&has_pending_exception);
|
2008-11-04 16:23:56 +00:00
|
|
|
if (has_pending_exception) return Failure::Exception();
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
Handle<Object> arguments = GetArgumentsObject(frame, function, code, &sinfo,
|
|
|
|
function_context);
|
|
|
|
|
|
|
|
// Invoke the evaluation function and return the result.
|
|
|
|
const int argc = 2;
|
|
|
|
Object** argv[argc] = { arguments.location(),
|
|
|
|
Handle<Object>::cast(source).location() };
|
|
|
|
Handle<Object> result =
|
|
|
|
Execution::Call(Handle<JSFunction>::cast(evaluation_function), receiver,
|
|
|
|
argc, argv, &has_pending_exception);
|
2008-11-04 16:23:56 +00:00
|
|
|
if (has_pending_exception) return Failure::Exception();
|
2009-06-12 10:03:25 +00:00
|
|
|
|
|
|
|
// Skip the global proxy as it has no properties and always delegates to the
|
|
|
|
// real global object.
|
|
|
|
if (result->IsJSGlobalProxy()) {
|
|
|
|
result = Handle<JSObject>(JSObject::cast(result->GetPrototype()));
|
|
|
|
}
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
return *result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static Object* Runtime_DebugEvaluateGlobal(Arguments args) {
|
|
|
|
HandleScope scope;
|
|
|
|
|
|
|
|
// Check the execution state and decode arguments frame and source to be
|
|
|
|
// evaluated.
|
2008-07-09 11:06:54 +00:00
|
|
|
ASSERT(args.length() == 3);
|
2008-07-03 15:10:15 +00:00
|
|
|
Object* check_result = Runtime_CheckExecutionState(args);
|
|
|
|
if (check_result->IsFailure()) return check_result;
|
|
|
|
CONVERT_ARG_CHECKED(String, source, 1);
|
2008-07-09 11:06:54 +00:00
|
|
|
CONVERT_BOOLEAN_CHECKED(disable_break, args[2]);
|
|
|
|
|
|
|
|
// Handle the processing of break.
|
|
|
|
DisableBreak disable_break_save(disable_break);
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
// Enter the top context from before the debugger was invoked.
|
|
|
|
SaveContext save;
|
|
|
|
SaveContext* top = &save;
|
|
|
|
while (top != NULL && *top->context() == *Debug::debug_context()) {
|
|
|
|
top = top->prev();
|
|
|
|
}
|
|
|
|
if (top != NULL) {
|
|
|
|
Top::set_context(*top->context());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the global context now set to the top context from before the
|
|
|
|
// debugger was invoked.
|
|
|
|
Handle<Context> context = Top::global_context();
|
|
|
|
|
|
|
|
// Compile the source to be evaluated.
|
2009-02-24 13:11:53 +00:00
|
|
|
Handle<JSFunction> boilerplate =
|
2009-04-24 12:45:29 +00:00
|
|
|
Handle<JSFunction>(Compiler::CompileEval(source,
|
|
|
|
context,
|
|
|
|
true,
|
2009-08-13 10:25:35 +00:00
|
|
|
Compiler::DONT_VALIDATE_JSON));
|
2008-07-03 15:10:15 +00:00
|
|
|
if (boilerplate.is_null()) return Failure::Exception();
|
|
|
|
Handle<JSFunction> compiled_function =
|
|
|
|
Handle<JSFunction>(Factory::NewFunctionFromBoilerplate(boilerplate,
|
|
|
|
context));
|
|
|
|
|
|
|
|
// Invoke the result of the compilation to get the evaluation function.
|
|
|
|
bool has_pending_exception;
|
|
|
|
Handle<Object> receiver = Top::global();
|
|
|
|
Handle<Object> result =
|
|
|
|
Execution::Call(compiled_function, receiver, 0, NULL,
|
|
|
|
&has_pending_exception);
|
2008-11-04 16:23:56 +00:00
|
|
|
if (has_pending_exception) return Failure::Exception();
|
2008-07-03 15:10:15 +00:00
|
|
|
return *result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static Object* Runtime_DebugGetLoadedScripts(Arguments args) {
|
|
|
|
HandleScope scope;
|
2008-08-13 09:32:07 +00:00
|
|
|
ASSERT(args.length() == 0);
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
// Fill the script objects.
|
2009-05-18 13:14:37 +00:00
|
|
|
Handle<FixedArray> instances = Debug::GetLoadedScripts();
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
// Convert the script objects to proper JS objects.
|
2009-05-18 13:14:37 +00:00
|
|
|
for (int i = 0; i < instances->length(); i++) {
|
2008-10-06 13:20:27 +00:00
|
|
|
Handle<Script> script = Handle<Script>(Script::cast(instances->get(i)));
|
|
|
|
// Get the script wrapper in a local handle before calling GetScriptWrapper,
|
|
|
|
// because using
|
2008-10-06 13:37:27 +00:00
|
|
|
// instances->set(i, *GetScriptWrapper(script))
|
2008-10-06 13:20:27 +00:00
|
|
|
// is unsafe as GetScriptWrapper might call GC and the C++ compiler might
|
2008-10-06 13:37:27 +00:00
|
|
|
// already have deferenced the instances handle.
|
2008-10-06 13:20:27 +00:00
|
|
|
Handle<JSValue> wrapper = GetScriptWrapper(script);
|
|
|
|
instances->set(i, *wrapper);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Return result as a JS array.
|
|
|
|
Handle<JSObject> result = Factory::NewJSObject(Top::array_function());
|
|
|
|
Handle<JSArray>::cast(result)->SetContent(*instances);
|
|
|
|
return *result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Helper function used by Runtime_DebugReferencedBy below.
|
|
|
|
static int DebugReferencedBy(JSObject* target,
|
|
|
|
Object* instance_filter, int max_references,
|
|
|
|
FixedArray* instances, int instances_size,
|
|
|
|
JSFunction* arguments_function) {
|
|
|
|
NoHandleAllocation ha;
|
|
|
|
AssertNoAllocation no_alloc;
|
|
|
|
|
|
|
|
// Iterate the heap.
|
|
|
|
int count = 0;
|
|
|
|
JSObject* last = NULL;
|
|
|
|
HeapIterator iterator;
|
|
|
|
while (iterator.has_next() &&
|
|
|
|
(max_references == 0 || count < max_references)) {
|
|
|
|
// Only look at all JSObjects.
|
|
|
|
HeapObject* heap_obj = iterator.next();
|
|
|
|
if (heap_obj->IsJSObject()) {
|
|
|
|
// Skip context extension objects and argument arrays as these are
|
|
|
|
// checked in the context of functions using them.
|
|
|
|
JSObject* obj = JSObject::cast(heap_obj);
|
2009-01-28 06:28:13 +00:00
|
|
|
if (obj->IsJSContextExtensionObject() ||
|
2008-07-03 15:10:15 +00:00
|
|
|
obj->map()->constructor() == arguments_function) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if the JS object has a reference to the object looked for.
|
|
|
|
if (obj->ReferencesObject(target)) {
|
|
|
|
// Check instance filter if supplied. This is normally used to avoid
|
|
|
|
// references from mirror objects (see Runtime_IsInPrototypeChain).
|
|
|
|
if (!instance_filter->IsUndefined()) {
|
|
|
|
Object* V = obj;
|
|
|
|
while (true) {
|
|
|
|
Object* prototype = V->GetPrototype();
|
|
|
|
if (prototype->IsNull()) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (instance_filter == prototype) {
|
|
|
|
obj = NULL; // Don't add this object.
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
V = prototype;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (obj != NULL) {
|
|
|
|
// Valid reference found add to instance array if supplied an update
|
|
|
|
// count.
|
|
|
|
if (instances != NULL && count < instances_size) {
|
|
|
|
instances->set(count, obj);
|
|
|
|
}
|
|
|
|
last = obj;
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for circular reference only. This can happen when the object is only
|
|
|
|
// referenced from mirrors and has a circular reference in which case the
|
|
|
|
// object is not really alive and would have been garbage collected if not
|
|
|
|
// referenced from the mirror.
|
|
|
|
if (count == 1 && last == target) {
|
|
|
|
count = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return the number of referencing objects found.
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Scan the heap for objects with direct references to an object
|
|
|
|
// args[0]: the object to find references to
|
|
|
|
// args[1]: constructor function for instances to exclude (Mirror)
|
|
|
|
// args[2]: the the maximum number of objects to return
|
|
|
|
static Object* Runtime_DebugReferencedBy(Arguments args) {
|
|
|
|
ASSERT(args.length() == 3);
|
|
|
|
|
|
|
|
// First perform a full GC in order to avoid references from dead objects.
|
2009-08-25 02:54:39 +00:00
|
|
|
Heap::CollectAllGarbage(false);
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
// Check parameters.
|
|
|
|
CONVERT_CHECKED(JSObject, target, args[0]);
|
|
|
|
Object* instance_filter = args[1];
|
|
|
|
RUNTIME_ASSERT(instance_filter->IsUndefined() ||
|
|
|
|
instance_filter->IsJSObject());
|
|
|
|
CONVERT_NUMBER_CHECKED(int32_t, max_references, Int32, args[2]);
|
|
|
|
RUNTIME_ASSERT(max_references >= 0);
|
|
|
|
|
|
|
|
// Get the constructor function for context extension and arguments array.
|
|
|
|
JSObject* arguments_boilerplate =
|
|
|
|
Top::context()->global_context()->arguments_boilerplate();
|
|
|
|
JSFunction* arguments_function =
|
|
|
|
JSFunction::cast(arguments_boilerplate->map()->constructor());
|
|
|
|
|
|
|
|
// Get the number of referencing objects.
|
|
|
|
int count;
|
|
|
|
count = DebugReferencedBy(target, instance_filter, max_references,
|
2009-01-28 06:28:13 +00:00
|
|
|
NULL, 0, arguments_function);
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
// Allocate an array to hold the result.
|
|
|
|
Object* object = Heap::AllocateFixedArray(count);
|
|
|
|
if (object->IsFailure()) return object;
|
|
|
|
FixedArray* instances = FixedArray::cast(object);
|
|
|
|
|
|
|
|
// Fill the referencing objects.
|
|
|
|
count = DebugReferencedBy(target, instance_filter, max_references,
|
2009-01-28 06:28:13 +00:00
|
|
|
instances, count, arguments_function);
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
// Return result as JS array.
|
|
|
|
Object* result =
|
|
|
|
Heap::AllocateJSObject(
|
|
|
|
Top::context()->global_context()->array_function());
|
|
|
|
if (!result->IsFailure()) JSArray::cast(result)->SetContent(instances);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Helper function used by Runtime_DebugConstructedBy below.
|
|
|
|
static int DebugConstructedBy(JSFunction* constructor, int max_references,
|
|
|
|
FixedArray* instances, int instances_size) {
|
|
|
|
AssertNoAllocation no_alloc;
|
|
|
|
|
|
|
|
// Iterate the heap.
|
|
|
|
int count = 0;
|
|
|
|
HeapIterator iterator;
|
|
|
|
while (iterator.has_next() &&
|
|
|
|
(max_references == 0 || count < max_references)) {
|
|
|
|
// Only look at all JSObjects.
|
|
|
|
HeapObject* heap_obj = iterator.next();
|
|
|
|
if (heap_obj->IsJSObject()) {
|
|
|
|
JSObject* obj = JSObject::cast(heap_obj);
|
|
|
|
if (obj->map()->constructor() == constructor) {
|
|
|
|
// Valid reference found add to instance array if supplied an update
|
|
|
|
// count.
|
|
|
|
if (instances != NULL && count < instances_size) {
|
|
|
|
instances->set(count, obj);
|
|
|
|
}
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return the number of referencing objects found.
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Scan the heap for objects constructed by a specific function.
|
|
|
|
// args[0]: the constructor to find instances of
|
|
|
|
// args[1]: the the maximum number of objects to return
|
|
|
|
static Object* Runtime_DebugConstructedBy(Arguments args) {
|
|
|
|
ASSERT(args.length() == 2);
|
|
|
|
|
|
|
|
// First perform a full GC in order to avoid dead objects.
|
2009-08-25 02:54:39 +00:00
|
|
|
Heap::CollectAllGarbage(false);
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
// Check parameters.
|
|
|
|
CONVERT_CHECKED(JSFunction, constructor, args[0]);
|
|
|
|
CONVERT_NUMBER_CHECKED(int32_t, max_references, Int32, args[1]);
|
|
|
|
RUNTIME_ASSERT(max_references >= 0);
|
|
|
|
|
|
|
|
// Get the number of referencing objects.
|
|
|
|
int count;
|
|
|
|
count = DebugConstructedBy(constructor, max_references, NULL, 0);
|
|
|
|
|
|
|
|
// Allocate an array to hold the result.
|
|
|
|
Object* object = Heap::AllocateFixedArray(count);
|
|
|
|
if (object->IsFailure()) return object;
|
|
|
|
FixedArray* instances = FixedArray::cast(object);
|
|
|
|
|
|
|
|
// Fill the referencing objects.
|
|
|
|
count = DebugConstructedBy(constructor, max_references, instances, count);
|
|
|
|
|
|
|
|
// Return result as JS array.
|
|
|
|
Object* result =
|
|
|
|
Heap::AllocateJSObject(
|
|
|
|
Top::context()->global_context()->array_function());
|
|
|
|
if (!result->IsFailure()) JSArray::cast(result)->SetContent(instances);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-01-22 13:20:31 +00:00
|
|
|
// Find the effective prototype object as returned by __proto__.
|
|
|
|
// args[0]: the object to find the prototype for.
|
|
|
|
static Object* Runtime_DebugGetPrototype(Arguments args) {
|
2008-07-03 15:10:15 +00:00
|
|
|
ASSERT(args.length() == 1);
|
|
|
|
|
|
|
|
CONVERT_CHECKED(JSObject, obj, args[0]);
|
|
|
|
|
2009-01-22 13:20:31 +00:00
|
|
|
// Use the __proto__ accessor.
|
|
|
|
return Accessors::ObjectPrototype.getter(obj, NULL);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static Object* Runtime_SystemBreak(Arguments args) {
|
2008-08-13 09:32:07 +00:00
|
|
|
ASSERT(args.length() == 0);
|
2008-07-03 15:10:15 +00:00
|
|
|
CPU::DebugBreak();
|
|
|
|
return Heap::undefined_value();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-08-26 12:22:44 +00:00
|
|
|
static Object* Runtime_DebugDisassembleFunction(Arguments args) {
|
2009-04-20 16:36:13 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
HandleScope scope;
|
|
|
|
ASSERT(args.length() == 1);
|
|
|
|
// Get the function and make sure it is compiled.
|
|
|
|
CONVERT_ARG_CHECKED(JSFunction, func, 0);
|
|
|
|
if (!func->is_compiled() && !CompileLazy(func, KEEP_EXCEPTION)) {
|
|
|
|
return Failure::Exception();
|
|
|
|
}
|
|
|
|
func->code()->PrintLn();
|
2009-08-26 12:22:44 +00:00
|
|
|
#endif // DEBUG
|
|
|
|
return Heap::undefined_value();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static Object* Runtime_DebugDisassembleConstructor(Arguments args) {
|
|
|
|
#ifdef DEBUG
|
|
|
|
HandleScope scope;
|
|
|
|
ASSERT(args.length() == 1);
|
|
|
|
// Get the function and make sure it is compiled.
|
|
|
|
CONVERT_ARG_CHECKED(JSFunction, func, 0);
|
|
|
|
if (!func->is_compiled() && !CompileLazy(func, KEEP_EXCEPTION)) {
|
|
|
|
return Failure::Exception();
|
|
|
|
}
|
|
|
|
func->shared()->construct_stub()->PrintLn();
|
2009-04-20 16:36:13 +00:00
|
|
|
#endif // DEBUG
|
|
|
|
return Heap::undefined_value();
|
|
|
|
}
|
2009-05-05 18:12:03 +00:00
|
|
|
|
|
|
|
|
|
|
|
static Object* Runtime_FunctionGetInferredName(Arguments args) {
|
|
|
|
NoHandleAllocation ha;
|
|
|
|
ASSERT(args.length() == 1);
|
|
|
|
|
|
|
|
CONVERT_CHECKED(JSFunction, f, args[0]);
|
|
|
|
return f->shared()->inferred_name();
|
|
|
|
}
|
2009-04-20 17:44:39 +00:00
|
|
|
#endif // ENABLE_DEBUGGER_SUPPORT
|
|
|
|
|
2009-04-20 16:36:13 +00:00
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
// Finds the script object from the script data. NOTE: This operation uses
|
|
|
|
// heap traversal to find the function generated for the source position
|
|
|
|
// for the requested break point. For lazily compiled functions several heap
|
|
|
|
// traversals might be required rendering this operation as a rather slow
|
|
|
|
// operation. However for setting break points which is normally done through
|
|
|
|
// some kind of user interaction the performance is not crucial.
|
|
|
|
static Handle<Object> Runtime_GetScriptFromScriptName(
|
|
|
|
Handle<String> script_name) {
|
|
|
|
// Scan the heap for Script objects to find the script with the requested
|
|
|
|
// script data.
|
|
|
|
Handle<Script> script;
|
|
|
|
HeapIterator iterator;
|
|
|
|
while (script.is_null() && iterator.has_next()) {
|
|
|
|
HeapObject* obj = iterator.next();
|
|
|
|
// If a script is found check if it has the script data requested.
|
|
|
|
if (obj->IsScript()) {
|
|
|
|
if (Script::cast(obj)->name()->IsString()) {
|
|
|
|
if (String::cast(Script::cast(obj)->name())->Equals(*script_name)) {
|
|
|
|
script = Handle<Script>(Script::cast(obj));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If no script with the requested script data is found return undefined.
|
|
|
|
if (script.is_null()) return Factory::undefined_value();
|
|
|
|
|
|
|
|
// Return the script found.
|
|
|
|
return GetScriptWrapper(script);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Get the script object from script data. NOTE: Regarding performance
|
|
|
|
// see the NOTE for GetScriptFromScriptData.
|
|
|
|
// args[0]: script data for the script to find the source for
|
|
|
|
static Object* Runtime_GetScript(Arguments args) {
|
|
|
|
HandleScope scope;
|
|
|
|
|
|
|
|
ASSERT(args.length() == 1);
|
|
|
|
|
|
|
|
CONVERT_CHECKED(String, script_name, args[0]);
|
|
|
|
|
|
|
|
// Find the requested script.
|
|
|
|
Handle<Object> result =
|
|
|
|
Runtime_GetScriptFromScriptName(Handle<String>(script_name));
|
|
|
|
return *result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-06-30 11:08:37 +00:00
|
|
|
// Determines whether the given stack frame should be displayed in
|
|
|
|
// a stack trace. The caller is the error constructor that asked
|
|
|
|
// for the stack trace to be collected. The first time a construct
|
|
|
|
// call to this function is encountered it is skipped. The seen_caller
|
|
|
|
// in/out parameter is used to remember if the caller has been seen
|
|
|
|
// yet.
|
|
|
|
static bool ShowFrameInStackTrace(StackFrame* raw_frame, Object* caller,
|
|
|
|
bool* seen_caller) {
|
|
|
|
// Only display JS frames.
|
|
|
|
if (!raw_frame->is_java_script())
|
|
|
|
return false;
|
|
|
|
JavaScriptFrame* frame = JavaScriptFrame::cast(raw_frame);
|
|
|
|
Object* raw_fun = frame->function();
|
|
|
|
// Not sure when this can happen but skip it just in case.
|
|
|
|
if (!raw_fun->IsJSFunction())
|
|
|
|
return false;
|
2009-07-27 12:01:32 +00:00
|
|
|
if ((raw_fun == caller) && !(*seen_caller)) {
|
2009-06-30 11:08:37 +00:00
|
|
|
*seen_caller = true;
|
|
|
|
return false;
|
|
|
|
}
|
2009-07-27 12:01:32 +00:00
|
|
|
// Skip all frames until we've seen the caller. Also, skip the most
|
|
|
|
// obvious builtin calls. Some builtin calls (such as Number.ADD
|
|
|
|
// which is invoked using 'call') are very difficult to recognize
|
|
|
|
// so we're leaving them in for now.
|
|
|
|
return *seen_caller && !frame->receiver()->IsJSBuiltinsObject();
|
2009-06-30 11:08:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Collect the raw data for a stack trace. Returns an array of three
|
|
|
|
// element segments each containing a receiver, function and native
|
|
|
|
// code offset.
|
|
|
|
static Object* Runtime_CollectStackTrace(Arguments args) {
|
2009-07-02 12:26:31 +00:00
|
|
|
ASSERT_EQ(args.length(), 2);
|
2009-07-27 13:26:43 +00:00
|
|
|
Handle<Object> caller = args.at<Object>(0);
|
2009-07-02 12:26:31 +00:00
|
|
|
CONVERT_NUMBER_CHECKED(int32_t, limit, Int32, args[1]);
|
|
|
|
|
|
|
|
HandleScope scope;
|
|
|
|
|
|
|
|
int initial_size = limit < 10 ? limit : 10;
|
|
|
|
Handle<JSArray> result = Factory::NewJSArray(initial_size * 3);
|
2009-06-30 11:08:37 +00:00
|
|
|
|
|
|
|
StackFrameIterator iter;
|
2009-07-27 12:01:32 +00:00
|
|
|
// If the caller parameter is a function we skip frames until we're
|
|
|
|
// under it before starting to collect.
|
|
|
|
bool seen_caller = !caller->IsJSFunction();
|
2009-07-02 12:26:31 +00:00
|
|
|
int cursor = 0;
|
|
|
|
int frames_seen = 0;
|
|
|
|
while (!iter.done() && frames_seen < limit) {
|
2009-06-30 11:08:37 +00:00
|
|
|
StackFrame* raw_frame = iter.frame();
|
2009-07-27 13:26:43 +00:00
|
|
|
if (ShowFrameInStackTrace(raw_frame, *caller, &seen_caller)) {
|
2009-07-02 12:26:31 +00:00
|
|
|
frames_seen++;
|
2009-06-30 11:08:37 +00:00
|
|
|
JavaScriptFrame* frame = JavaScriptFrame::cast(raw_frame);
|
2009-07-02 12:26:31 +00:00
|
|
|
Object* recv = frame->receiver();
|
|
|
|
Object* fun = frame->function();
|
2009-06-30 11:08:37 +00:00
|
|
|
Address pc = frame->pc();
|
|
|
|
Address start = frame->code()->address();
|
2009-07-02 12:26:31 +00:00
|
|
|
Smi* offset = Smi::FromInt(pc - start);
|
2009-07-28 08:43:51 +00:00
|
|
|
FixedArray* elements = FixedArray::cast(result->elements());
|
2009-07-02 12:26:31 +00:00
|
|
|
if (cursor + 2 < elements->length()) {
|
|
|
|
elements->set(cursor++, recv);
|
|
|
|
elements->set(cursor++, fun);
|
|
|
|
elements->set(cursor++, offset, SKIP_WRITE_BARRIER);
|
|
|
|
} else {
|
|
|
|
HandleScope scope;
|
2009-07-27 13:26:43 +00:00
|
|
|
Handle<Object> recv_handle(recv);
|
|
|
|
Handle<Object> fun_handle(fun);
|
|
|
|
SetElement(result, cursor++, recv_handle);
|
|
|
|
SetElement(result, cursor++, fun_handle);
|
2009-07-02 12:26:31 +00:00
|
|
|
SetElement(result, cursor++, Handle<Smi>(offset));
|
|
|
|
}
|
2009-06-30 11:08:37 +00:00
|
|
|
}
|
2009-07-02 12:26:31 +00:00
|
|
|
iter.Advance();
|
2009-06-30 11:08:37 +00:00
|
|
|
}
|
2009-07-02 12:26:31 +00:00
|
|
|
|
|
|
|
result->set_length(Smi::FromInt(cursor), SKIP_WRITE_BARRIER);
|
|
|
|
|
2009-06-30 11:08:37 +00:00
|
|
|
return *result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
static Object* Runtime_Abort(Arguments args) {
|
|
|
|
ASSERT(args.length() == 2);
|
|
|
|
OS::PrintError("abort: %s\n", reinterpret_cast<char*>(args[0]) +
|
|
|
|
Smi::cast(args[1])->value());
|
|
|
|
Top::PrintStack();
|
|
|
|
OS::Abort();
|
|
|
|
UNREACHABLE();
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-07-25 07:37:58 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
// ListNatives is ONLY used by the fuzz-natives.js in debug mode
|
|
|
|
// Exclude the code in release mode.
|
2008-07-03 15:10:15 +00:00
|
|
|
static Object* Runtime_ListNatives(Arguments args) {
|
2008-08-13 09:32:07 +00:00
|
|
|
ASSERT(args.length() == 0);
|
2008-07-03 15:10:15 +00:00
|
|
|
HandleScope scope;
|
|
|
|
Handle<JSArray> result = Factory::NewJSArray(0);
|
|
|
|
int index = 0;
|
2009-09-08 11:52:05 +00:00
|
|
|
#define ADD_ENTRY(Name, argc, ressize) \
|
2008-07-03 15:10:15 +00:00
|
|
|
{ \
|
|
|
|
HandleScope inner; \
|
|
|
|
Handle<String> name = \
|
|
|
|
Factory::NewStringFromAscii(Vector<const char>(#Name, strlen(#Name))); \
|
|
|
|
Handle<JSArray> pair = Factory::NewJSArray(0); \
|
|
|
|
SetElement(pair, 0, name); \
|
|
|
|
SetElement(pair, 1, Handle<Smi>(Smi::FromInt(argc))); \
|
|
|
|
SetElement(result, index++, pair); \
|
|
|
|
}
|
|
|
|
RUNTIME_FUNCTION_LIST(ADD_ENTRY)
|
|
|
|
#undef ADD_ENTRY
|
|
|
|
return *result;
|
|
|
|
}
|
2008-07-25 07:37:58 +00:00
|
|
|
#endif
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
|
2009-01-06 13:24:52 +00:00
|
|
|
static Object* Runtime_Log(Arguments args) {
|
|
|
|
ASSERT(args.length() == 2);
|
2009-02-17 12:49:35 +00:00
|
|
|
CONVERT_CHECKED(String, format, args[0]);
|
|
|
|
CONVERT_CHECKED(JSArray, elms, args[1]);
|
2009-01-06 13:24:52 +00:00
|
|
|
Vector<const char> chars = format->ToAsciiVector();
|
|
|
|
Logger::LogRuntime(chars, elms);
|
|
|
|
return Heap::undefined_value();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
static Object* Runtime_IS_VAR(Arguments args) {
|
|
|
|
UNREACHABLE(); // implemented as macro in the parser
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// Implementation of Runtime
|
|
|
|
|
2009-09-08 11:52:05 +00:00
|
|
|
#define F(name, nargs, ressize) \
|
2008-07-03 15:10:15 +00:00
|
|
|
{ #name, "RuntimeStub_" #name, FUNCTION_ADDR(Runtime_##name), nargs, \
|
2009-09-08 11:52:05 +00:00
|
|
|
static_cast<int>(Runtime::k##name), ressize },
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
static Runtime::Function Runtime_functions[] = {
|
|
|
|
RUNTIME_FUNCTION_LIST(F)
|
2009-09-08 11:52:05 +00:00
|
|
|
{ NULL, NULL, NULL, 0, -1, 0 }
|
2008-07-03 15:10:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#undef F
|
|
|
|
|
|
|
|
|
|
|
|
Runtime::Function* Runtime::FunctionForId(FunctionId fid) {
|
|
|
|
ASSERT(0 <= fid && fid < kNofFunctions);
|
|
|
|
return &Runtime_functions[fid];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Runtime::Function* Runtime::FunctionForName(const char* name) {
|
|
|
|
for (Function* f = Runtime_functions; f->name != NULL; f++) {
|
|
|
|
if (strcmp(f->name, name) == 0) {
|
|
|
|
return f;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Runtime::PerformGC(Object* result) {
|
|
|
|
Failure* failure = Failure::cast(result);
|
2008-10-29 12:16:34 +00:00
|
|
|
if (failure->IsRetryAfterGC()) {
|
|
|
|
// Try to do a garbage collection; ignore it if it fails. The C
|
|
|
|
// entry stub will throw an out-of-memory exception in that case.
|
|
|
|
Heap::CollectGarbage(failure->requested(), failure->allocation_space());
|
|
|
|
} else {
|
|
|
|
// Handle last resort GC and make sure to allow future allocations
|
|
|
|
// to grow the heap without causing GCs (if possible).
|
2008-10-30 09:15:58 +00:00
|
|
|
Counters::gc_last_resort_from_js.Increment();
|
2009-08-25 02:54:39 +00:00
|
|
|
Heap::CollectAllGarbage(false);
|
2008-10-29 12:16:34 +00:00
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
} } // namespace v8::internal
|