2012-01-25 16:31:25 +00:00
|
|
|
// Copyright 2012 the V8 project authors. All rights reserved.
|
2014-04-29 06:42:26 +00:00
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
#ifndef V8_API_H_
|
|
|
|
#define V8_API_H_
|
|
|
|
|
2014-06-03 08:12:43 +00:00
|
|
|
#include "src/v8.h"
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2014-06-03 08:12:43 +00:00
|
|
|
#include "include/v8-testing.h"
|
|
|
|
#include "src/contexts.h"
|
|
|
|
#include "src/factory.h"
|
|
|
|
#include "src/isolate.h"
|
|
|
|
#include "src/list-inl.h"
|
2010-12-07 11:31:57 +00:00
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
namespace v8 {
|
|
|
|
|
|
|
|
// Constants used in the implementation of the API. The most natural thing
|
|
|
|
// would usually be to place these with the classes that use them, but
|
|
|
|
// we want to keep them out of v8.h because it is an externally
|
|
|
|
// visible file.
|
|
|
|
class Consts {
|
|
|
|
public:
|
|
|
|
enum TemplateType {
|
|
|
|
FUNCTION_TEMPLATE = 0,
|
|
|
|
OBJECT_TEMPLATE = 1
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// Utilities for working with neander-objects, primitive
|
|
|
|
// env-independent JSObjects used by the api.
|
|
|
|
class NeanderObject {
|
|
|
|
public:
|
2014-01-08 14:44:30 +00:00
|
|
|
explicit NeanderObject(v8::internal::Isolate* isolate, int size);
|
2011-04-11 11:38:34 +00:00
|
|
|
explicit inline NeanderObject(v8::internal::Handle<v8::internal::Object> obj);
|
|
|
|
explicit inline NeanderObject(v8::internal::Object* obj);
|
2008-07-03 15:10:15 +00:00
|
|
|
inline v8::internal::Object* get(int index);
|
|
|
|
inline void set(int index, v8::internal::Object* value);
|
|
|
|
inline v8::internal::Handle<v8::internal::JSObject> value() { return value_; }
|
|
|
|
int size();
|
|
|
|
private:
|
|
|
|
v8::internal::Handle<v8::internal::JSObject> value_;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// Utilities for working with neander-arrays, a simple extensible
|
|
|
|
// array abstraction built on neander-objects.
|
|
|
|
class NeanderArray {
|
|
|
|
public:
|
2014-01-08 14:44:30 +00:00
|
|
|
explicit NeanderArray(v8::internal::Isolate* isolate);
|
2011-04-11 11:38:34 +00:00
|
|
|
explicit inline NeanderArray(v8::internal::Handle<v8::internal::Object> obj);
|
2008-07-03 15:10:15 +00:00
|
|
|
inline v8::internal::Handle<v8::internal::JSObject> value() {
|
|
|
|
return obj_.value();
|
|
|
|
}
|
|
|
|
|
2014-10-30 09:54:37 +00:00
|
|
|
void add(internal::Isolate* isolate,
|
|
|
|
v8::internal::Handle<v8::internal::Object> value);
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
int length();
|
|
|
|
|
|
|
|
v8::internal::Object* get(int index);
|
|
|
|
// Change the value at an index to undefined value. If the index is
|
|
|
|
// out of bounds, the request is ignored. Returns the old value.
|
|
|
|
void set(int index, v8::internal::Object* value);
|
|
|
|
private:
|
|
|
|
NeanderObject obj_;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
NeanderObject::NeanderObject(v8::internal::Handle<v8::internal::Object> obj)
|
|
|
|
: value_(v8::internal::Handle<v8::internal::JSObject>::cast(obj)) { }
|
|
|
|
|
|
|
|
|
|
|
|
NeanderObject::NeanderObject(v8::internal::Object* obj)
|
|
|
|
: value_(v8::internal::Handle<v8::internal::JSObject>(
|
|
|
|
v8::internal::JSObject::cast(obj))) { }
|
|
|
|
|
|
|
|
|
|
|
|
NeanderArray::NeanderArray(v8::internal::Handle<v8::internal::Object> obj)
|
|
|
|
: obj_(obj) { }
|
|
|
|
|
|
|
|
|
|
|
|
v8::internal::Object* NeanderObject::get(int offset) {
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(value()->HasFastObjectElements());
|
2008-07-03 15:10:15 +00:00
|
|
|
return v8::internal::FixedArray::cast(value()->elements())->get(offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void NeanderObject::set(int offset, v8::internal::Object* value) {
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(value_->HasFastObjectElements());
|
2008-07-03 15:10:15 +00:00
|
|
|
v8::internal::FixedArray::cast(value_->elements())->set(offset, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-11-29 10:56:11 +00:00
|
|
|
template <typename T> inline T ToCData(v8::internal::Object* obj) {
|
2008-07-03 15:10:15 +00:00
|
|
|
STATIC_ASSERT(sizeof(T) == sizeof(v8::internal::Address));
|
|
|
|
return reinterpret_cast<T>(
|
2011-10-28 12:37:29 +00:00
|
|
|
reinterpret_cast<intptr_t>(
|
|
|
|
v8::internal::Foreign::cast(obj)->foreign_address()));
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template <typename T>
|
2013-09-02 09:27:27 +00:00
|
|
|
inline v8::internal::Handle<v8::internal::Object> FromCData(
|
|
|
|
v8::internal::Isolate* isolate, T obj) {
|
2008-07-03 15:10:15 +00:00
|
|
|
STATIC_ASSERT(sizeof(T) == sizeof(v8::internal::Address));
|
2013-06-04 10:30:05 +00:00
|
|
|
return isolate->factory()->NewForeign(
|
2008-10-03 15:53:44 +00:00
|
|
|
reinterpret_cast<v8::internal::Address>(reinterpret_cast<intptr_t>(obj)));
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-11-04 08:51:48 +00:00
|
|
|
class ApiFunction {
|
|
|
|
public:
|
|
|
|
explicit ApiFunction(v8::internal::Address addr) : addr_(addr) { }
|
|
|
|
v8::internal::Address address() { return addr_; }
|
|
|
|
private:
|
|
|
|
v8::internal::Address addr_;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
class RegisteredExtension {
|
|
|
|
public:
|
|
|
|
explicit RegisteredExtension(Extension* extension);
|
|
|
|
static void Register(RegisteredExtension* that);
|
2012-03-29 09:45:46 +00:00
|
|
|
static void UnregisterAll();
|
2008-07-03 15:10:15 +00:00
|
|
|
Extension* extension() { return extension_; }
|
|
|
|
RegisteredExtension* next() { return next_; }
|
|
|
|
static RegisteredExtension* first_extension() { return first_extension_; }
|
|
|
|
private:
|
|
|
|
Extension* extension_;
|
|
|
|
RegisteredExtension* next_;
|
|
|
|
static RegisteredExtension* first_extension_;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2015-01-07 16:37:26 +00:00
|
|
|
#define OPEN_HANDLE_LIST(V) \
|
|
|
|
V(Template, TemplateInfo) \
|
|
|
|
V(FunctionTemplate, FunctionTemplateInfo) \
|
|
|
|
V(ObjectTemplate, ObjectTemplateInfo) \
|
2015-01-19 12:17:09 +00:00
|
|
|
V(Signature, FunctionTemplateInfo) \
|
2015-01-07 16:37:26 +00:00
|
|
|
V(AccessorSignature, FunctionTemplateInfo) \
|
|
|
|
V(TypeSwitch, TypeSwitchInfo) \
|
|
|
|
V(Data, Object) \
|
|
|
|
V(RegExp, JSRegExp) \
|
|
|
|
V(Object, JSObject) \
|
|
|
|
V(Array, JSArray) \
|
|
|
|
V(ArrayBuffer, JSArrayBuffer) \
|
|
|
|
V(ArrayBufferView, JSArrayBufferView) \
|
|
|
|
V(TypedArray, JSTypedArray) \
|
|
|
|
V(Uint8Array, JSTypedArray) \
|
|
|
|
V(Uint8ClampedArray, JSTypedArray) \
|
|
|
|
V(Int8Array, JSTypedArray) \
|
|
|
|
V(Uint16Array, JSTypedArray) \
|
|
|
|
V(Int16Array, JSTypedArray) \
|
|
|
|
V(Uint32Array, JSTypedArray) \
|
|
|
|
V(Int32Array, JSTypedArray) \
|
|
|
|
V(Float32Array, JSTypedArray) \
|
|
|
|
V(Float64Array, JSTypedArray) \
|
|
|
|
V(DataView, JSDataView) \
|
|
|
|
V(Name, Name) \
|
|
|
|
V(String, String) \
|
|
|
|
V(Symbol, Symbol) \
|
|
|
|
V(Script, JSFunction) \
|
|
|
|
V(UnboundScript, SharedFunctionInfo) \
|
|
|
|
V(Function, JSFunction) \
|
|
|
|
V(Message, JSMessageObject) \
|
|
|
|
V(Context, Context) \
|
|
|
|
V(External, Object) \
|
|
|
|
V(StackTrace, JSArray) \
|
|
|
|
V(StackFrame, JSObject)
|
2012-09-05 16:06:53 +00:00
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
class Utils {
|
|
|
|
public:
|
2014-01-13 09:42:23 +00:00
|
|
|
static inline bool ApiCheck(bool condition,
|
|
|
|
const char* location,
|
|
|
|
const char* message) {
|
|
|
|
if (!condition) Utils::ReportApiFailure(location, message);
|
|
|
|
return condition;
|
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
static Local<FunctionTemplate> ToFunctionTemplate(NeanderObject obj);
|
|
|
|
static Local<ObjectTemplate> ToObjectTemplate(NeanderObject obj);
|
|
|
|
|
|
|
|
static inline Local<Context> ToLocal(
|
|
|
|
v8::internal::Handle<v8::internal::Context> obj);
|
|
|
|
static inline Local<Value> ToLocal(
|
|
|
|
v8::internal::Handle<v8::internal::Object> obj);
|
|
|
|
static inline Local<Function> ToLocal(
|
|
|
|
v8::internal::Handle<v8::internal::JSFunction> obj);
|
2014-08-20 15:25:13 +00:00
|
|
|
static inline Local<Name> ToLocal(
|
|
|
|
v8::internal::Handle<v8::internal::Name> obj);
|
2008-07-03 15:10:15 +00:00
|
|
|
static inline Local<String> ToLocal(
|
|
|
|
v8::internal::Handle<v8::internal::String> obj);
|
2013-04-10 12:16:29 +00:00
|
|
|
static inline Local<Symbol> ToLocal(
|
|
|
|
v8::internal::Handle<v8::internal::Symbol> obj);
|
2010-10-04 15:04:16 +00:00
|
|
|
static inline Local<RegExp> ToLocal(
|
|
|
|
v8::internal::Handle<v8::internal::JSRegExp> obj);
|
2008-07-03 15:10:15 +00:00
|
|
|
static inline Local<Object> ToLocal(
|
|
|
|
v8::internal::Handle<v8::internal::JSObject> obj);
|
|
|
|
static inline Local<Array> ToLocal(
|
|
|
|
v8::internal::Handle<v8::internal::JSArray> obj);
|
2013-04-25 12:02:23 +00:00
|
|
|
static inline Local<ArrayBuffer> ToLocal(
|
|
|
|
v8::internal::Handle<v8::internal::JSArrayBuffer> obj);
|
2013-06-24 11:23:50 +00:00
|
|
|
static inline Local<ArrayBufferView> ToLocal(
|
|
|
|
v8::internal::Handle<v8::internal::JSArrayBufferView> obj);
|
|
|
|
static inline Local<DataView> ToLocal(
|
|
|
|
v8::internal::Handle<v8::internal::JSDataView> obj);
|
2013-04-29 11:09:03 +00:00
|
|
|
|
|
|
|
static inline Local<TypedArray> ToLocal(
|
|
|
|
v8::internal::Handle<v8::internal::JSTypedArray> obj);
|
|
|
|
static inline Local<Uint8Array> ToLocalUint8Array(
|
|
|
|
v8::internal::Handle<v8::internal::JSTypedArray> obj);
|
2013-05-02 11:36:48 +00:00
|
|
|
static inline Local<Uint8ClampedArray> ToLocalUint8ClampedArray(
|
|
|
|
v8::internal::Handle<v8::internal::JSTypedArray> obj);
|
2013-04-29 11:09:03 +00:00
|
|
|
static inline Local<Int8Array> ToLocalInt8Array(
|
|
|
|
v8::internal::Handle<v8::internal::JSTypedArray> obj);
|
|
|
|
static inline Local<Uint16Array> ToLocalUint16Array(
|
|
|
|
v8::internal::Handle<v8::internal::JSTypedArray> obj);
|
|
|
|
static inline Local<Int16Array> ToLocalInt16Array(
|
|
|
|
v8::internal::Handle<v8::internal::JSTypedArray> obj);
|
|
|
|
static inline Local<Uint32Array> ToLocalUint32Array(
|
|
|
|
v8::internal::Handle<v8::internal::JSTypedArray> obj);
|
|
|
|
static inline Local<Int32Array> ToLocalInt32Array(
|
|
|
|
v8::internal::Handle<v8::internal::JSTypedArray> obj);
|
|
|
|
static inline Local<Float32Array> ToLocalFloat32Array(
|
|
|
|
v8::internal::Handle<v8::internal::JSTypedArray> obj);
|
|
|
|
static inline Local<Float64Array> ToLocalFloat64Array(
|
|
|
|
v8::internal::Handle<v8::internal::JSTypedArray> obj);
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
static inline Local<Message> MessageToLocal(
|
|
|
|
v8::internal::Handle<v8::internal::Object> obj);
|
2014-09-30 15:29:08 +00:00
|
|
|
static inline Local<Promise> PromiseToLocal(
|
|
|
|
v8::internal::Handle<v8::internal::JSObject> obj);
|
2010-05-06 07:32:44 +00:00
|
|
|
static inline Local<StackTrace> StackTraceToLocal(
|
|
|
|
v8::internal::Handle<v8::internal::JSArray> obj);
|
|
|
|
static inline Local<StackFrame> StackFrameToLocal(
|
|
|
|
v8::internal::Handle<v8::internal::JSObject> obj);
|
2008-07-03 15:10:15 +00:00
|
|
|
static inline Local<Number> NumberToLocal(
|
|
|
|
v8::internal::Handle<v8::internal::Object> obj);
|
|
|
|
static inline Local<Integer> IntegerToLocal(
|
|
|
|
v8::internal::Handle<v8::internal::Object> obj);
|
|
|
|
static inline Local<Uint32> Uint32ToLocal(
|
|
|
|
v8::internal::Handle<v8::internal::Object> obj);
|
|
|
|
static inline Local<FunctionTemplate> ToLocal(
|
|
|
|
v8::internal::Handle<v8::internal::FunctionTemplateInfo> obj);
|
|
|
|
static inline Local<ObjectTemplate> ToLocal(
|
|
|
|
v8::internal::Handle<v8::internal::ObjectTemplateInfo> obj);
|
2015-01-19 12:17:09 +00:00
|
|
|
static inline Local<Signature> SignatureToLocal(
|
|
|
|
v8::internal::Handle<v8::internal::FunctionTemplateInfo> obj);
|
2012-06-08 07:45:11 +00:00
|
|
|
static inline Local<AccessorSignature> AccessorSignatureToLocal(
|
|
|
|
v8::internal::Handle<v8::internal::FunctionTemplateInfo> obj);
|
2008-07-03 15:10:15 +00:00
|
|
|
static inline Local<TypeSwitch> ToLocal(
|
|
|
|
v8::internal::Handle<v8::internal::TypeSwitchInfo> obj);
|
2012-11-13 12:27:03 +00:00
|
|
|
static inline Local<External> ExternalToLocal(
|
|
|
|
v8::internal::Handle<v8::internal::JSObject> obj);
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2012-09-05 16:06:53 +00:00
|
|
|
#define DECLARE_OPEN_HANDLE(From, To) \
|
|
|
|
static inline v8::internal::Handle<v8::internal::To> \
|
|
|
|
OpenHandle(const From* that, bool allow_empty_handle = false);
|
|
|
|
|
|
|
|
OPEN_HANDLE_LIST(DECLARE_OPEN_HANDLE)
|
|
|
|
|
|
|
|
#undef DECLARE_OPEN_HANDLE
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2013-06-13 09:27:09 +00:00
|
|
|
template<class From, class To>
|
|
|
|
static inline Local<To> Convert(v8::internal::Handle<From> obj) {
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(obj.is_null() || !obj->IsTheHole());
|
2013-06-13 09:27:09 +00:00
|
|
|
return Local<To>(reinterpret_cast<To*>(obj.location()));
|
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2013-06-13 09:27:09 +00:00
|
|
|
template <class T>
|
|
|
|
static inline v8::internal::Handle<v8::internal::Object> OpenPersistent(
|
|
|
|
const v8::Persistent<T>& persistent) {
|
|
|
|
return v8::internal::Handle<v8::internal::Object>(
|
|
|
|
reinterpret_cast<v8::internal::Object**>(persistent.val_));
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
static inline v8::internal::Handle<v8::internal::Object> OpenPersistent(
|
|
|
|
v8::Persistent<T>* persistent) {
|
|
|
|
return OpenPersistent(*persistent);
|
|
|
|
}
|
2013-06-13 10:39:44 +00:00
|
|
|
|
|
|
|
template <class From, class To>
|
|
|
|
static inline v8::internal::Handle<To> OpenHandle(v8::Local<From> handle) {
|
|
|
|
return OpenHandle(*handle);
|
|
|
|
}
|
2014-01-13 09:42:23 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
static void ReportApiFailure(const char* location, const char* message);
|
2013-06-13 09:27:09 +00:00
|
|
|
};
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
v8::internal::Handle<T> v8::internal::Handle<T>::EscapeFrom(
|
2013-12-02 18:12:01 +00:00
|
|
|
v8::EscapableHandleScope* scope) {
|
2010-03-25 14:07:17 +00:00
|
|
|
v8::internal::Handle<T> handle;
|
|
|
|
if (!is_null()) {
|
|
|
|
handle = *this;
|
|
|
|
}
|
2013-12-02 18:12:01 +00:00
|
|
|
return Utils::OpenHandle(*scope->Escape(Utils::ToLocal(handle)), true);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-13 09:27:09 +00:00
|
|
|
template <class T>
|
|
|
|
inline T* ToApi(v8::internal::Handle<v8::internal::Object> obj) {
|
|
|
|
return reinterpret_cast<T*>(obj.location());
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
inline v8::Local<T> ToApiHandle(
|
|
|
|
v8::internal::Handle<v8::internal::Object> obj) {
|
|
|
|
return Utils::Convert<v8::internal::Object, T>(obj);
|
|
|
|
}
|
2013-05-02 20:18:42 +00:00
|
|
|
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
// Implementations of ToLocal
|
|
|
|
|
2009-07-13 09:17:04 +00:00
|
|
|
#define MAKE_TO_LOCAL(Name, From, To) \
|
2008-07-03 15:10:15 +00:00
|
|
|
Local<v8::To> Utils::Name(v8::internal::Handle<v8::internal::From> obj) { \
|
2013-06-13 09:27:09 +00:00
|
|
|
return Convert<v8::internal::From, v8::To>(obj); \
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
2013-04-29 11:09:03 +00:00
|
|
|
|
2014-01-24 16:01:15 +00:00
|
|
|
#define MAKE_TO_LOCAL_TYPED_ARRAY(Type, typeName, TYPE, ctype, size) \
|
|
|
|
Local<v8::Type##Array> Utils::ToLocal##Type##Array( \
|
2013-04-29 11:09:03 +00:00
|
|
|
v8::internal::Handle<v8::internal::JSTypedArray> obj) { \
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(obj->type() == kExternal##Type##Array); \
|
2014-01-24 16:01:15 +00:00
|
|
|
return Convert<v8::internal::JSTypedArray, v8::Type##Array>(obj); \
|
2013-04-29 11:09:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
MAKE_TO_LOCAL(ToLocal, Context, Context)
|
|
|
|
MAKE_TO_LOCAL(ToLocal, Object, Value)
|
|
|
|
MAKE_TO_LOCAL(ToLocal, JSFunction, Function)
|
2014-08-20 15:25:13 +00:00
|
|
|
MAKE_TO_LOCAL(ToLocal, Name, Name)
|
2008-07-03 15:10:15 +00:00
|
|
|
MAKE_TO_LOCAL(ToLocal, String, String)
|
2013-04-10 12:16:29 +00:00
|
|
|
MAKE_TO_LOCAL(ToLocal, Symbol, Symbol)
|
2010-10-04 15:04:16 +00:00
|
|
|
MAKE_TO_LOCAL(ToLocal, JSRegExp, RegExp)
|
2008-07-03 15:10:15 +00:00
|
|
|
MAKE_TO_LOCAL(ToLocal, JSObject, Object)
|
|
|
|
MAKE_TO_LOCAL(ToLocal, JSArray, Array)
|
2013-04-25 12:02:23 +00:00
|
|
|
MAKE_TO_LOCAL(ToLocal, JSArrayBuffer, ArrayBuffer)
|
2013-06-24 11:23:50 +00:00
|
|
|
MAKE_TO_LOCAL(ToLocal, JSArrayBufferView, ArrayBufferView)
|
|
|
|
MAKE_TO_LOCAL(ToLocal, JSDataView, DataView)
|
2013-04-29 11:09:03 +00:00
|
|
|
MAKE_TO_LOCAL(ToLocal, JSTypedArray, TypedArray)
|
|
|
|
|
2014-01-24 16:01:15 +00:00
|
|
|
TYPED_ARRAYS(MAKE_TO_LOCAL_TYPED_ARRAY)
|
2013-04-29 11:09:03 +00:00
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
MAKE_TO_LOCAL(ToLocal, FunctionTemplateInfo, FunctionTemplate)
|
|
|
|
MAKE_TO_LOCAL(ToLocal, ObjectTemplateInfo, ObjectTemplate)
|
2015-01-19 12:17:09 +00:00
|
|
|
MAKE_TO_LOCAL(SignatureToLocal, FunctionTemplateInfo, Signature)
|
2012-06-08 07:45:11 +00:00
|
|
|
MAKE_TO_LOCAL(AccessorSignatureToLocal, FunctionTemplateInfo, AccessorSignature)
|
2008-07-03 15:10:15 +00:00
|
|
|
MAKE_TO_LOCAL(ToLocal, TypeSwitchInfo, TypeSwitch)
|
|
|
|
MAKE_TO_LOCAL(MessageToLocal, Object, Message)
|
2014-09-30 15:29:08 +00:00
|
|
|
MAKE_TO_LOCAL(PromiseToLocal, JSObject, Promise)
|
2010-05-06 07:32:44 +00:00
|
|
|
MAKE_TO_LOCAL(StackTraceToLocal, JSArray, StackTrace)
|
|
|
|
MAKE_TO_LOCAL(StackFrameToLocal, JSObject, StackFrame)
|
2008-07-03 15:10:15 +00:00
|
|
|
MAKE_TO_LOCAL(NumberToLocal, Object, Number)
|
|
|
|
MAKE_TO_LOCAL(IntegerToLocal, Object, Integer)
|
|
|
|
MAKE_TO_LOCAL(Uint32ToLocal, Object, Uint32)
|
2012-11-13 12:27:03 +00:00
|
|
|
MAKE_TO_LOCAL(ExternalToLocal, JSObject, External)
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2013-04-29 11:09:03 +00:00
|
|
|
#undef MAKE_TO_LOCAL_TYPED_ARRAY
|
2008-07-03 15:10:15 +00:00
|
|
|
#undef MAKE_TO_LOCAL
|
|
|
|
|
|
|
|
|
|
|
|
// Implementations of OpenHandle
|
|
|
|
|
2015-01-08 16:29:27 +00:00
|
|
|
#define MAKE_OPEN_HANDLE(From, To) \
|
|
|
|
v8::internal::Handle<v8::internal::To> Utils::OpenHandle( \
|
|
|
|
const v8::From* that, bool allow_empty_handle) { \
|
|
|
|
DCHECK(allow_empty_handle || that != NULL); \
|
|
|
|
DCHECK(that == NULL || \
|
|
|
|
(*reinterpret_cast<v8::internal::Object* const*>(that))->Is##To()); \
|
|
|
|
return v8::internal::Handle<v8::internal::To>( \
|
|
|
|
reinterpret_cast<v8::internal::To**>(const_cast<v8::From*>(that))); \
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
2012-09-05 16:06:53 +00:00
|
|
|
OPEN_HANDLE_LIST(MAKE_OPEN_HANDLE)
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
#undef MAKE_OPEN_HANDLE
|
2012-09-05 16:06:53 +00:00
|
|
|
#undef OPEN_HANDLE_LIST
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace internal {
|
|
|
|
|
2011-03-18 20:35:07 +00:00
|
|
|
// Tracks string usage to help make better decisions when
|
|
|
|
// externalizing strings.
|
|
|
|
//
|
|
|
|
// Implementation note: internally this class only tracks fresh
|
|
|
|
// strings and keeps a single use counter for them.
|
|
|
|
class StringTracker {
|
|
|
|
public:
|
|
|
|
// Records that the given string's characters were copied to some
|
|
|
|
// external buffer. If this happens often we should honor
|
|
|
|
// externalization requests for the string.
|
|
|
|
void RecordWrite(Handle<String> string) {
|
|
|
|
Address address = reinterpret_cast<Address>(*string);
|
|
|
|
Address top = isolate_->heap()->NewSpaceTop();
|
|
|
|
if (IsFreshString(address, top)) {
|
|
|
|
IncrementUseCount(top);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Estimates freshness and use frequency of the given string based
|
|
|
|
// on how close it is to the new space top and the recorded usage
|
|
|
|
// history.
|
|
|
|
inline bool IsFreshUnusedString(Handle<String> string) {
|
|
|
|
Address address = reinterpret_cast<Address>(*string);
|
|
|
|
Address top = isolate_->heap()->NewSpaceTop();
|
|
|
|
return IsFreshString(address, top) && IsUseCountLow(top);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
StringTracker() : use_count_(0), last_top_(NULL), isolate_(NULL) { }
|
|
|
|
|
|
|
|
static inline bool IsFreshString(Address string, Address top) {
|
|
|
|
return top - kFreshnessLimit <= string && string <= top;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool IsUseCountLow(Address top) {
|
|
|
|
if (last_top_ != top) return true;
|
|
|
|
return use_count_ < kUseLimit;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void IncrementUseCount(Address top) {
|
|
|
|
if (last_top_ != top) {
|
|
|
|
use_count_ = 0;
|
|
|
|
last_top_ = top;
|
|
|
|
}
|
|
|
|
++use_count_;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Single use counter shared by all fresh strings.
|
|
|
|
int use_count_;
|
|
|
|
|
|
|
|
// Last new space top when the use count above was valid.
|
|
|
|
Address last_top_;
|
|
|
|
|
|
|
|
Isolate* isolate_;
|
|
|
|
|
|
|
|
// How close to the new space top a fresh string has to be.
|
|
|
|
static const int kFreshnessLimit = 1024;
|
|
|
|
|
|
|
|
// The number of uses required to consider a string useful.
|
|
|
|
static const int kUseLimit = 32;
|
|
|
|
|
|
|
|
friend class Isolate;
|
|
|
|
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(StringTracker);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2012-07-06 09:31:31 +00:00
|
|
|
class DeferredHandles {
|
|
|
|
public:
|
|
|
|
~DeferredHandles();
|
|
|
|
|
|
|
|
private:
|
2012-07-18 14:15:02 +00:00
|
|
|
DeferredHandles(Object** first_block_limit, Isolate* isolate)
|
|
|
|
: next_(NULL),
|
2012-07-12 07:59:41 +00:00
|
|
|
previous_(NULL),
|
|
|
|
first_block_limit_(first_block_limit),
|
2012-07-18 14:15:02 +00:00
|
|
|
isolate_(isolate) {
|
|
|
|
isolate->LinkDeferredHandles(this);
|
2012-07-12 07:59:41 +00:00
|
|
|
}
|
2012-07-06 09:31:31 +00:00
|
|
|
|
|
|
|
void Iterate(ObjectVisitor* v);
|
|
|
|
|
|
|
|
List<Object**> blocks_;
|
|
|
|
DeferredHandles* next_;
|
|
|
|
DeferredHandles* previous_;
|
2012-07-06 14:09:11 +00:00
|
|
|
Object** first_block_limit_;
|
2012-07-18 14:15:02 +00:00
|
|
|
Isolate* isolate_;
|
2012-07-06 09:31:31 +00:00
|
|
|
|
|
|
|
friend class HandleScopeImplementer;
|
2012-07-18 14:15:02 +00:00
|
|
|
friend class Isolate;
|
2012-07-06 09:31:31 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
// This class is here in order to be able to declare it a friend of
|
|
|
|
// HandleScope. Moving these methods to be members of HandleScope would be
|
2011-03-18 20:35:07 +00:00
|
|
|
// neat in some ways, but it would expose internal implementation details in
|
2008-07-03 15:10:15 +00:00
|
|
|
// our public header file, which is undesirable.
|
|
|
|
//
|
2011-03-18 20:35:07 +00:00
|
|
|
// An isolate has a single instance of this class to hold the current thread's
|
|
|
|
// data. In multithreaded V8 programs this data is copied in and out of storage
|
2008-07-03 15:10:15 +00:00
|
|
|
// so that the currently executing thread always has its own copy of this
|
|
|
|
// data.
|
2011-05-23 12:59:02 +00:00
|
|
|
class HandleScopeImplementer {
|
2008-07-03 15:10:15 +00:00
|
|
|
public:
|
2011-05-05 18:55:31 +00:00
|
|
|
explicit HandleScopeImplementer(Isolate* isolate)
|
|
|
|
: isolate_(isolate),
|
|
|
|
blocks_(0),
|
2008-07-25 07:37:58 +00:00
|
|
|
entered_contexts_(0),
|
2009-09-28 12:25:21 +00:00
|
|
|
saved_contexts_(0),
|
|
|
|
spare_(NULL),
|
2012-07-06 09:31:31 +00:00
|
|
|
call_depth_(0),
|
2012-07-18 14:15:02 +00:00
|
|
|
last_handle_before_deferred_block_(NULL) { }
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2011-08-18 12:14:12 +00:00
|
|
|
~HandleScopeImplementer() {
|
|
|
|
DeleteArray(spare_);
|
|
|
|
}
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
// Threading support for handle data.
|
|
|
|
static int ArchiveSpacePerThread();
|
2011-03-18 20:35:07 +00:00
|
|
|
char* RestoreThread(char* from);
|
|
|
|
char* ArchiveThread(char* to);
|
|
|
|
void FreeThreadResources();
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
// Garbage collection support.
|
2011-03-18 20:35:07 +00:00
|
|
|
void Iterate(v8::internal::ObjectVisitor* v);
|
2008-07-03 15:10:15 +00:00
|
|
|
static char* Iterate(v8::internal::ObjectVisitor* v, char* data);
|
|
|
|
|
|
|
|
|
2009-08-26 10:33:11 +00:00
|
|
|
inline internal::Object** GetSpareOrNewBlock();
|
2010-10-21 14:21:00 +00:00
|
|
|
inline void DeleteExtensions(internal::Object** prev_limit);
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2009-09-28 12:25:21 +00:00
|
|
|
inline void IncrementCallDepth() {call_depth_++;}
|
|
|
|
inline void DecrementCallDepth() {call_depth_--;}
|
|
|
|
inline bool CallDepthIsZero() { return call_depth_ == 0; }
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2013-09-24 11:40:28 +00:00
|
|
|
inline void EnterContext(Handle<Context> context);
|
2014-01-14 09:37:45 +00:00
|
|
|
inline void LeaveContext();
|
|
|
|
inline bool LastEnteredContextWas(Handle<Context> context);
|
2008-07-25 07:37:58 +00:00
|
|
|
|
|
|
|
// Returns the last entered context or an empty handle if no
|
|
|
|
// contexts have been entered.
|
2013-09-24 11:40:28 +00:00
|
|
|
inline Handle<Context> LastEnteredContext();
|
2008-07-25 07:37:58 +00:00
|
|
|
|
2009-09-04 07:34:25 +00:00
|
|
|
inline void SaveContext(Context* context);
|
|
|
|
inline Context* RestoreContext();
|
2008-07-25 07:37:58 +00:00
|
|
|
inline bool HasSavedContexts();
|
|
|
|
|
2009-09-28 12:25:21 +00:00
|
|
|
inline List<internal::Object**>* blocks() { return &blocks_; }
|
2012-07-06 09:31:31 +00:00
|
|
|
Isolate* isolate() const { return isolate_; }
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2012-07-18 14:15:02 +00:00
|
|
|
void ReturnBlock(Object** block) {
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(block != NULL);
|
2012-07-18 14:15:02 +00:00
|
|
|
if (spare_ != NULL) DeleteArray(spare_);
|
|
|
|
spare_ = block;
|
|
|
|
}
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
private:
|
2009-09-28 12:25:21 +00:00
|
|
|
void ResetAfterArchive() {
|
|
|
|
blocks_.Initialize(0);
|
|
|
|
entered_contexts_.Initialize(0);
|
|
|
|
saved_contexts_.Initialize(0);
|
|
|
|
spare_ = NULL;
|
2012-07-06 14:09:11 +00:00
|
|
|
last_handle_before_deferred_block_ = NULL;
|
2009-09-28 12:25:21 +00:00
|
|
|
call_depth_ = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Free() {
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(blocks_.length() == 0);
|
|
|
|
DCHECK(entered_contexts_.length() == 0);
|
|
|
|
DCHECK(saved_contexts_.length() == 0);
|
2009-09-28 13:53:03 +00:00
|
|
|
blocks_.Free();
|
|
|
|
entered_contexts_.Free();
|
|
|
|
saved_contexts_.Free();
|
2009-09-28 12:25:21 +00:00
|
|
|
if (spare_ != NULL) {
|
|
|
|
DeleteArray(spare_);
|
|
|
|
spare_ = NULL;
|
|
|
|
}
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(call_depth_ == 0);
|
2009-09-28 12:25:21 +00:00
|
|
|
}
|
|
|
|
|
2012-07-06 09:31:31 +00:00
|
|
|
void BeginDeferredScope();
|
|
|
|
DeferredHandles* Detach(Object** prev_limit);
|
|
|
|
|
2011-05-05 18:55:31 +00:00
|
|
|
Isolate* isolate_;
|
2009-09-28 12:25:21 +00:00
|
|
|
List<internal::Object**> blocks_;
|
2008-07-25 07:37:58 +00:00
|
|
|
// Used as a stack to keep track of entered contexts.
|
2013-09-24 11:40:28 +00:00
|
|
|
List<Context*> entered_contexts_;
|
2008-07-25 07:37:58 +00:00
|
|
|
// Used as a stack to keep track of saved contexts.
|
2009-09-04 07:34:25 +00:00
|
|
|
List<Context*> saved_contexts_;
|
2009-09-28 12:25:21 +00:00
|
|
|
Object** spare_;
|
|
|
|
int call_depth_;
|
2012-07-06 09:31:31 +00:00
|
|
|
Object** last_handle_before_deferred_block_;
|
2008-07-03 15:10:15 +00:00
|
|
|
// This is only used for threading support.
|
2014-01-16 08:17:40 +00:00
|
|
|
HandleScopeData handle_scope_data_;
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2009-09-04 07:34:25 +00:00
|
|
|
void IterateThis(ObjectVisitor* v);
|
2008-07-03 15:10:15 +00:00
|
|
|
char* RestoreThreadHelper(char* from);
|
|
|
|
char* ArchiveThreadHelper(char* to);
|
|
|
|
|
2012-07-06 09:31:31 +00:00
|
|
|
friend class DeferredHandles;
|
|
|
|
friend class DeferredHandleScope;
|
|
|
|
|
2008-08-28 09:55:41 +00:00
|
|
|
DISALLOW_COPY_AND_ASSIGN(HandleScopeImplementer);
|
2008-07-03 15:10:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2011-11-29 10:56:11 +00:00
|
|
|
const int kHandleBlockSize = v8::internal::KB - 2; // fit in one page
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
|
2009-09-04 07:34:25 +00:00
|
|
|
void HandleScopeImplementer::SaveContext(Context* context) {
|
2008-07-25 07:37:58 +00:00
|
|
|
saved_contexts_.Add(context);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-09-04 07:34:25 +00:00
|
|
|
Context* HandleScopeImplementer::RestoreContext() {
|
2008-07-25 07:37:58 +00:00
|
|
|
return saved_contexts_.RemoveLast();
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-07-25 07:37:58 +00:00
|
|
|
bool HandleScopeImplementer::HasSavedContexts() {
|
|
|
|
return !saved_contexts_.is_empty();
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
2008-07-25 07:37:58 +00:00
|
|
|
|
2013-09-24 11:40:28 +00:00
|
|
|
void HandleScopeImplementer::EnterContext(Handle<Context> context) {
|
|
|
|
entered_contexts_.Add(*context);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-01-14 09:37:45 +00:00
|
|
|
void HandleScopeImplementer::LeaveContext() {
|
2008-07-25 07:37:58 +00:00
|
|
|
entered_contexts_.RemoveLast();
|
2014-01-14 09:37:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool HandleScopeImplementer::LastEnteredContextWas(Handle<Context> context) {
|
|
|
|
return !entered_contexts_.is_empty() && entered_contexts_.last() == *context;
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-09-24 11:40:28 +00:00
|
|
|
Handle<Context> HandleScopeImplementer::LastEnteredContext() {
|
|
|
|
if (entered_contexts_.is_empty()) return Handle<Context>::null();
|
|
|
|
return Handle<Context>(entered_contexts_.last());
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// If there's a spare block, use it for growing the current scope.
|
2009-08-26 10:33:11 +00:00
|
|
|
internal::Object** HandleScopeImplementer::GetSpareOrNewBlock() {
|
2009-09-28 12:25:21 +00:00
|
|
|
internal::Object** block = (spare_ != NULL) ?
|
|
|
|
spare_ :
|
2009-08-26 10:33:11 +00:00
|
|
|
NewArray<internal::Object*>(kHandleBlockSize);
|
2009-09-28 12:25:21 +00:00
|
|
|
spare_ = NULL;
|
2008-07-03 15:10:15 +00:00
|
|
|
return block;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-10-21 14:21:00 +00:00
|
|
|
void HandleScopeImplementer::DeleteExtensions(internal::Object** prev_limit) {
|
|
|
|
while (!blocks_.is_empty()) {
|
|
|
|
internal::Object** block_start = blocks_.last();
|
|
|
|
internal::Object** block_limit = block_start + kHandleBlockSize;
|
2008-07-03 15:10:15 +00:00
|
|
|
#ifdef DEBUG
|
2013-06-03 15:32:22 +00:00
|
|
|
// SealHandleScope may make the prev_limit to point inside the block.
|
2013-05-29 11:09:01 +00:00
|
|
|
if (block_start <= prev_limit && prev_limit <= block_limit) {
|
2013-09-30 11:56:52 +00:00
|
|
|
#ifdef ENABLE_HANDLE_ZAPPING
|
2013-05-29 11:09:01 +00:00
|
|
|
internal::HandleScope::ZapRange(prev_limit, block_limit);
|
|
|
|
#endif
|
|
|
|
break;
|
|
|
|
}
|
2010-10-21 14:21:00 +00:00
|
|
|
#else
|
2013-07-08 15:28:25 +00:00
|
|
|
if (prev_limit == block_limit) break;
|
2008-07-03 15:10:15 +00:00
|
|
|
#endif
|
2010-10-21 14:21:00 +00:00
|
|
|
|
|
|
|
blocks_.RemoveLast();
|
2013-09-30 11:56:52 +00:00
|
|
|
#ifdef ENABLE_HANDLE_ZAPPING
|
2013-03-22 13:40:13 +00:00
|
|
|
internal::HandleScope::ZapRange(block_start, block_limit);
|
2008-07-03 15:10:15 +00:00
|
|
|
#endif
|
2010-10-21 14:21:00 +00:00
|
|
|
if (spare_ != NULL) {
|
|
|
|
DeleteArray(spare_);
|
|
|
|
}
|
|
|
|
spare_ = block_start;
|
|
|
|
}
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK((blocks_.is_empty() && prev_limit == NULL) ||
|
2010-10-21 14:21:00 +00:00
|
|
|
(!blocks_.is_empty() && prev_limit != NULL));
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
2010-12-07 11:31:57 +00:00
|
|
|
|
2013-06-13 19:16:35 +00:00
|
|
|
// Interceptor functions called from generated inline caches to notify
|
|
|
|
// CPU profiler that external callbacks are invoked.
|
|
|
|
void InvokeAccessorGetterCallback(
|
2014-08-20 15:25:13 +00:00
|
|
|
v8::Local<v8::Name> property,
|
2013-06-13 19:16:35 +00:00
|
|
|
const v8::PropertyCallbackInfo<v8::Value>& info,
|
2014-08-20 15:25:13 +00:00
|
|
|
v8::AccessorNameGetterCallback getter);
|
2013-06-13 19:16:35 +00:00
|
|
|
|
|
|
|
void InvokeFunctionCallback(const v8::FunctionCallbackInfo<v8::Value>& info,
|
|
|
|
v8::FunctionCallback callback);
|
|
|
|
|
2010-12-07 11:31:57 +00:00
|
|
|
class Testing {
|
|
|
|
public:
|
|
|
|
static v8::Testing::StressType stress_type() { return stress_type_; }
|
|
|
|
static void set_stress_type(v8::Testing::StressType stress_type) {
|
|
|
|
stress_type_ = stress_type;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
static v8::Testing::StressType stress_type_;
|
|
|
|
};
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
} } // namespace v8::internal
|
|
|
|
|
|
|
|
#endif // V8_API_H_
|