From c462aa226be59df6a6366081fbce2c6d2ae5937f Mon Sep 17 00:00:00 2001 From: mybios Date: Fri, 11 Sep 2015 04:22:16 +0800 Subject: [PATCH] support newest v8 style api. implement Inherit fix some bugs... --- include/v8like-debug.h | 8 +- include/v8like-profiler.h | 6 + include/v8like.h | 1596 ++++++-- src/v8-internal.h | 2 +- src/v8like.cc | 8186 +++++++++++++++++++------------------ 5 files changed, 5603 insertions(+), 4195 deletions(-) diff --git a/include/v8like-debug.h b/include/v8like-debug.h index 0844e91..ca1c3ba 100644 --- a/include/v8like-debug.h +++ b/include/v8like-debug.h @@ -83,6 +83,7 @@ class EXPORT Debug class Message { public: + virtual Isolate* GetIsolate() const = 0; virtual bool IsEvent() const = 0; virtual bool IsResponse() const = 0; virtual DebugEvent GetEvent() const = 0; @@ -108,7 +109,8 @@ class EXPORT Debug typedef void (*EventCallback)(DebugEvent event, Handle exec_state, Handle event_data, Handle data); typedef void (*EventCallback2)(const EventDetails& event_details); - typedef void (*MessageHandler)(const uint16_t* message, int length, ClientData* client_data); + typedef void(*MessageHandler)(const Message& message); +// typedef void (*MessageHandler)(const uint16_t* message, int length, ClientData* client_data); typedef void (*MessageHandler2)(const Message& message); typedef void (*HostDispatchHandler)(); typedef void (*DebugMessageDispatchHandler)(); @@ -121,6 +123,10 @@ class EXPORT Debug static void SetMessageHandler(MessageHandler handler, bool message_handler_thread = false); static void SetMessageHandler2(MessageHandler2 handler); static void SendCommand(const uint16_t* command, int length, ClientData* client_data = NULL, Isolate* isolate = NULL); + static void SendCommand(Isolate* isolate, const uint16_t* command, int length, ClientData* client_data = NULL, Isolate* isolate1 = NULL) + { + SendCommand(command, length, client_data, isolate); + } static void SetHostDispatchHandler(HostDispatchHandler handler, int period = 100); static void SetDebugMessageDispatchHandler(DebugMessageDispatchHandler handler, bool provide_locker = false); static Local Call(v8::Handle fun, Handle data = Handle()); diff --git a/include/v8like-profiler.h b/include/v8like-profiler.h index f39ba67..26a10cf 100644 --- a/include/v8like-profiler.h +++ b/include/v8like-profiler.h @@ -92,6 +92,8 @@ class V8EXPORT CpuProfiler static void StartProfiling(Handle title); static const CpuProfile* StopProfiling(Handle title, Handle security_token = Handle()); static void DeleteAllProfiles(); + + void SetIdle(bool v); }; class HeapGraphNode; @@ -168,6 +170,7 @@ class RetainedObjectInfo; class V8EXPORT HeapProfiler { public: + HeapProfiler(){} typedef RetainedObjectInfo* (*WrapperInfoCallback)(uint16_t class_id, Handle wrapper); static int GetSnapshotsCount(); static const HeapSnapshot* GetSnapshot(int index); @@ -183,6 +186,9 @@ class V8EXPORT HeapProfiler static const uint16_t kPersistentHandleNoClassId = 0; static int GetPersistentHandleCount(); static size_t GetMemorySizeUsedByProfiler(); + void SetWrapperClassInfoProvider( + uint16_t class_id, + WrapperInfoCallback callback); }; class V8EXPORT RetainedObjectInfo diff --git a/include/v8like.h b/include/v8like.h index 42353cf..bfbf6cd 100644 --- a/include/v8like.h +++ b/include/v8like.h @@ -33,42 +33,43 @@ #undef Handle #undef Boolean +// A macro used to make better inlining. Don't bother for debug builds. +// Use like: +// V8_INLINE int GetZero() { return 0; } +#if !defined(DEBUG) && V8_HAS_ATTRIBUTE_ALWAYS_INLINE +# define V8_INLINE inline __attribute__((always_inline)) +#elif !defined(DEBUG) && V8_HAS___FORCEINLINE +# define V8_INLINE __forceinline +#else +# define V8_INLINE inline +#endif + #include "v8stdint.h" +#include "assert.h" + #include #include +#include -#ifdef _WIN32 - -#if defined(BUILDING_V8_SHARED) && defined(USING_V8_SHARED) -#error both BUILDING_V8_SHARED and USING_V8_SHARED are set -#endif - -#ifdef BUILDING_V8_SHARED -#define V8EXPORT __declspec(dllexport) -#elif USING_V8_SHARED -#define V8EXPORT __declspec(dllimport) -#else #define V8EXPORT -#endif // BUILDING_V8_SHARED -#else // _WIN32 - -#if defined(__GNUC__) && ((__GNUC__ >= 4) || (__GNUC__ == 3 && __GNUC_MINOR__ >= 3)) && defined(V8_SHARED) -#ifdef BUILDING_V8_SHARED -#define V8EXPORT __attribute__ ((visibility("default"))) -#else -#define V8EXPORT -#endif +// A macro to mark classes or functions as deprecated. +#if defined(V8_DEPRECATION_WARNINGS) && V8_HAS_ATTRIBUTE_DEPRECATED_MESSAGE +# define V8_DEPRECATED(message, declarator) \ +declarator __attribute__((deprecated(message))) +#elif defined(V8_DEPRECATION_WARNINGS) && V8_HAS_ATTRIBUTE_DEPRECATED +# define V8_DEPRECATED(message, declarator) \ +declarator __attribute__((deprecated)) +#elif defined(V8_DEPRECATION_WARNINGS) && V8_HAS_DECLSPEC_DEPRECATED +# define V8_DEPRECATED(message, declarator) __declspec(deprecated) declarator #else -#define V8EXPORT +# define V8_DEPRECATED(message, declarator) declarator #endif -#endif // _WIN32 - namespace v8 { -#define TODO() printf("%s:%d TODO: %s\n", __FILE__, __LINE__, __PRETTY_FUNCTION__) +#define TODO() printf("%s:%d TODO: %s\n", __FILE__, __LINE__, __FUNCTION__) class Context; class Isolate; @@ -99,9 +100,89 @@ class ObjectTemplate; class FunctionTemplate; class StackFrame; class StackTrace; +class Value; +class HeapStatistics; +class Platform; namespace internal { + //added 6/6 + const int kApiPointerSize = sizeof(void*); // NOLINT + const int kApiIntSize = sizeof(int); // NOLINT + const int kApiInt64Size = sizeof(int64_t); // NOLINT + + // Tag information for HeapObject. + const int kHeapObjectTag = 1; + const int kHeapObjectTagSize = 2; + const intptr_t kHeapObjectTagMask = (1 << kHeapObjectTagSize) - 1; + + // Tag information for Smi. + const int kSmiTag = 0; + const int kSmiTagSize = 1; + const intptr_t kSmiTagMask = (1 << kSmiTagSize) - 1; + + template struct SmiTagging; + + class Internals { + public: + // These values match non-compiler-dependent values defined within + // the implementation of v8. + static const int kHeapObjectMapOffset = 0; + static const int kMapInstanceTypeAndBitFieldOffset = + 1 * kApiPointerSize + kApiIntSize; + static const int kStringResourceOffset = 3 * kApiPointerSize; + + static const int kOddballKindOffset = 3 * kApiPointerSize; + static const int kForeignAddressOffset = kApiPointerSize; + static const int kJSObjectHeaderSize = 3 * kApiPointerSize; + static const int kFixedArrayHeaderSize = 2 * kApiPointerSize; + static const int kContextHeaderSize = 2 * kApiPointerSize; + static const int kContextEmbedderDataIndex = 95; + static const int kFullStringRepresentationMask = 0x07; + static const int kStringEncodingMask = 0x4; + static const int kExternalTwoByteRepresentationTag = 0x02; + static const int kExternalAsciiRepresentationTag = 0x06; + + static const int kIsolateEmbedderDataOffset = 0 * kApiPointerSize; + static const int kAmountOfExternalAllocatedMemoryOffset = + 4 * kApiPointerSize; + static const int kAmountOfExternalAllocatedMemoryAtLastGlobalGCOffset = + kAmountOfExternalAllocatedMemoryOffset + kApiInt64Size; + static const int kIsolateRootsOffset = + kAmountOfExternalAllocatedMemoryAtLastGlobalGCOffset + kApiInt64Size + + kApiPointerSize; + static const int kUndefinedValueRootIndex = 5; + static const int kNullValueRootIndex = 7; + static const int kTrueValueRootIndex = 8; + static const int kFalseValueRootIndex = 9; + static const int kEmptyStringRootIndex = 164; + + // The external allocation limit should be below 256 MB on all architectures + // to avoid that resource-constrained embedders run low on memory. + static const int kExternalAllocationLimit = 192 * 1024 * 1024; + + static const int kNodeClassIdOffset = 1 * kApiPointerSize; + static const int kNodeFlagsOffset = 1 * kApiPointerSize + 3; + static const int kNodeStateMask = 0xf; + static const int kNodeStateIsWeakValue = 2; + static const int kNodeStateIsPendingValue = 3; + static const int kNodeStateIsNearDeathValue = 4; + static const int kNodeIsIndependentShift = 4; + static const int kNodeIsPartiallyDependentShift = 5; + + static const int kJSObjectType = 0xbc; + static const int kFirstNonstringType = 0x80; + static const int kOddballType = 0x83; + static const int kForeignType = 0x88; + + static const int kUndefinedOddballKind = 5; + static const int kNullOddballKind = 3; + + static const uint32_t kNumIsolateDataSlots = 4; + + }; + + /// v8::internal::RTTI class RTTI @@ -213,9 +294,10 @@ class SmartObject unsigned int GetRef() const { return m_ref; } public: + virtual void _OnCreate() {} virtual void _OnDelete() {} - virtual void _MakeWeak(void* parameter, void (*callback)(Persistent object, void* parameter)) {} + virtual void _MakeWeak(void* parameter, void * callback) {} virtual void _ClearWeak() {} virtual void _MarkIndependent() {} virtual bool _IsIndependent() const { return false; } @@ -233,6 +315,16 @@ template class SmartPointer private: T* m_ptr; // the managed pointer (derived from SmartObject) +protected: + void setPtr(T* ptr) + { + if (m_ptr != ptr) + { + if (m_ptr) { m_ptr->DecRef(); } + m_ptr = ptr; + if (m_ptr) { m_ptr->IncRef(); } + } + } public: // construction and destruction SmartPointer(T* ptr = (T*) 0) : m_ptr(ptr) @@ -256,22 +348,12 @@ template class SmartPointer // assignment SmartPointer& operator=(T* ptr) { - if (m_ptr != ptr) - { - if (m_ptr) { m_ptr->DecRef(); } - m_ptr = ptr; - if (m_ptr) { m_ptr->IncRef(); } - } + setPtr(ptr); return *this; } SmartPointer& operator=(const SmartPointer& other) { - if (m_ptr != other.m_ptr) - { - if (m_ptr) { m_ptr->DecRef(); } - m_ptr = other.m_ptr; - if (m_ptr) { m_ptr->IncRef(); } - } + setPtr(other.m_ptr); return *this; } @@ -284,6 +366,7 @@ template class SmartPointer T* GetPointer() const { return m_ptr; } bool IsEmpty() const { return (m_ptr == ((T*) 0)); } void _Clear() { if (m_ptr) { m_ptr->DecRef(); m_ptr = (T*) 0; } } + void Clear(){ _Clear(); } }; class JSStringWrap @@ -324,6 +407,28 @@ Handle ImportObject(JSContextRef js_ctx, JSObjectRef js_object); /// v8::Isolate + +class HeapProfiler; +class CpuProfiler; + +enum GCType +{ + kGCTypeScavenge = 1 << 0, + kGCTypeMarkSweepCompact = 1 << 1, + kGCTypeAll = kGCTypeScavenge | kGCTypeMarkSweepCompact +}; + +enum GCCallbackFlags +{ + kNoGCCallbackFlags = 0, + kGCCallbackFlagCompacted = 1 << 0 +}; + +typedef void(*GCPrologueCallback)(Isolate* isolate,GCType type, GCCallbackFlags flags); +typedef void(*GCEpilogueCallback)(Isolate* isolate,GCType type, GCCallbackFlags flags); + +typedef void(*GCCallback)(); + class V8EXPORT Isolate { public: @@ -343,29 +448,63 @@ class V8EXPORT Isolate private: Isolate* m_previous_isolate; void* m_data; - + HeapProfiler* heapProfiler; + CpuProfiler* cpuProfiler; + void* embedder_data_[internal::Internals::kNumIsolateDataSlots]; private: - Isolate() : m_previous_isolate(NULL), m_data(NULL) {} - ~Isolate() {} + Isolate(); + ~Isolate(); public: typedef bool (*abort_on_uncaught_exception_t)(); void SetAbortOnUncaughtException(abort_on_uncaught_exception_t callback); - + void GetHeapStatistics(HeapStatistics* heap_statistics); + void AddGCPrologueCallback( + GCPrologueCallback callback, GCType gc_type_filter = kGCTypeAll); + void AddGCEpilogueCallback( + GCEpilogueCallback callback, GCType gc_type_filter = kGCTypeAll); + void RemoveGCPrologueCallback(GCPrologueCallback callback); + void RemoveGCEpilogueCallback(GCEpilogueCallback callback); + Local ThrowException(Local exception); void Enter(); void Exit(); - + Local GetCurrentContext(); void Dispose(); + HeapProfiler* GetHeapProfiler(); + CpuProfiler* GetCpuProfiler(); - void SetData(void* data) { m_data = data; } - void* GetData() { return m_data; } + void RunMicrotasks(); + void SetAutorunMicrotasks(bool autorun); + //void SetData(void* data) { m_data = data; } + //void* GetData() { return m_data; } + + void SetData(uint32_t slot, void* data) { + if (slot < internal::Internals::kNumIsolateDataSlots) + embedder_data_[slot] = data; + } + void* GetData(uint32_t slot) { + if (slot < internal::Internals::kNumIsolateDataSlots) + return embedder_data_[slot]; + return nullptr; + } + V8_INLINE int64_t + AdjustAmountOfExternalAllocatedMemory(int64_t change_in_bytes) + { + return 0; + } + + bool IdleNotification(int hint) + { + return true; + } private: static Isolate* sm_current_isolate; public: static Isolate* New(); static Isolate* GetCurrent(); + }; /// v8::Handle @@ -378,6 +517,7 @@ template class Handle : public internal::SmartPointer Handle(T* ptr = (T*) 0) : internal::SmartPointer(ptr) {} Handle(const Handle& other) : internal::SmartPointer(other) {} template Handle(const Handle& other) : internal::SmartPointer(reinterpret_cast(other.GetPointer())) { TYPE_CHECK(T, S); } + template Handle(const Local& other) : internal::SmartPointer(reinterpret_cast(other.GetPointer())) { TYPE_CHECK(T, S); } virtual ~Handle() {} // override dereference operator so SmartPointer behaves like a Handle @@ -396,25 +536,48 @@ template class Local : public Handle public: Local(T* ptr = (T*) 0) : Handle(ptr) {} Local(const Local& other) : Handle(other) {} + Local(const Handle& other) : Handle(other) {} template Local(const Local& other) : Handle(reinterpret_cast(other.GetPointer())) { TYPE_CHECK(T, S); } + template Local(const Handle& other) : Handle(reinterpret_cast(other.GetPointer())) { TYPE_CHECK(T, S); } virtual ~Local() {} template inline Local As() { return Local::Cast(*this); } public: static Local New(Handle that) { return Local(that); } + static Local New(Isolate* isolate, Handle that) { return New(that); } template static inline Local Cast(Local that) { return Local(T::Cast(*that)); } }; /// v8::Persistent -typedef void (*WeakReferenceCallback)(Persistent object, void* parameter); +/// WeakCallbackData +template +class WeakCallbackData { +public: + typedef void(*Callback)(const WeakCallbackData& data); + + V8_INLINE Isolate* GetIsolate() const { return isolate_; } + V8_INLINE Local GetValue() const { return handle_; } + V8_INLINE P* GetParameter() const { return parameter_; } + + WeakCallbackData(Isolate* isolate, Local handle, P* parameter) + : isolate_(isolate), handle_(handle), parameter_(parameter) { } + Isolate* isolate_; + Local handle_; + P* parameter_; +}; + +typedef WeakCallbackData::Callback WeakCallback; + +typedef WeakCallback WeakReferenceCallback; template class Persistent : public Handle { public: Persistent(T* ptr = (T*) 0) : Handle(ptr) {} Persistent(const Persistent& other) : Handle(other) {} + Persistent(Isolate* isolate,const Handle& other) : Handle(other) {} template Persistent(const Persistent& other) : Handle(reinterpret_cast(other.GetPointer())) { TYPE_CHECK(T, S); } virtual ~Persistent() {} @@ -424,9 +587,45 @@ template class Persistent : public Handle { internal::SmartPointer::_Clear(); } - void Clear() {} - void MakeWeak(void* parameter, WeakReferenceCallback callback) { internal::SmartPointer::GetPointer()->_MakeWeak(parameter, callback); } + template + void Reset(Isolate* isolate, const Handle& other) { + TYPE_CHECK(T, S); + if (other.IsEmpty()) return; + internal::SmartPointer::setPtr(other.GetPointer()); + } + void Clear() { internal::SmartPointer::_Clear(); } + void Reset() { + if (this->IsEmpty()) return; + internal::SmartPointer::_Clear(); + } + + void MakeWeak(void* parameter, WeakReferenceCallback callback) { internal::SmartPointer::GetPointer()->_MakeWeak(parameter, (void*)callback); } + + template + V8_INLINE void SetWeak( + P* parameter, + typename WeakCallbackData::Callback callback) + { + MakeWeak((void*)parameter, (WeakReferenceCallback)callback); + } +// V8_INLINE void SetWeak( +// void* parameter, +// WeakReferenceCallback callback) +// { +// MakeWeak(parameter, callback); +// } void ClearWeak() { internal::SmartPointer::GetPointer()->_ClearWeak(); } + + + template + V8_INLINE P* ClearWeak() + { + P* p = (P*)internal::SmartPointer::GetPointer()->m_weak_parameter; + internal::SmartPointer::GetPointer()->_ClearWeak(); + return p; + } + + void MarkIndependent() { internal::SmartPointer::GetPointer()->_MarkIndependent(); } bool IsIndependent() const { return internal::SmartPointer::GetPointer()->_IsIndependent(); } bool IsNearDeath() const { return internal::SmartPointer::GetPointer()->_IsNearDeath(); } @@ -439,17 +638,104 @@ template class Persistent : public Handle template static inline Persistent Cast(Persistent that) { return Persistent(T::Cast(*that)); } }; + + + + + + + + + +template +class UniquePersistent : public Persistent { + struct RValue { + V8_INLINE explicit RValue(UniquePersistent* obj) : object(obj) {} + UniquePersistent* object; + }; + +public: + /** + * A UniquePersistent with no storage cell. + */ + V8_INLINE UniquePersistent() : Persistent(0) { } + /** + * Construct a UniquePersistent from a Handle. + * When the Handle is non-empty, a new storage cell is created + * pointing to the same object, and no flags are set. + */ + template + V8_INLINE UniquePersistent(Isolate* isolate, Handle that) + : Persistent(Persistent::New(isolate, *that)) { + TYPE_CHECK(T, S); + } + /** + * Construct a UniquePersistent from a PersistentBase. + * When the Persistent is non-empty, a new storage cell is created + * pointing to the same object, and no flags are set. + */ + template + V8_INLINE UniquePersistent(Isolate* isolate, const Persistent& that) + : Persistent(Persistent::New(isolate, that.m_ptr)) { + TYPE_CHECK(T, S); + } + /** + * Move constructor. + */ + V8_INLINE UniquePersistent(RValue rvalue) + : Persistent(rvalue.object->m_ptr) { + rvalue.object->m_ptr = 0; + } + V8_INLINE ~UniquePersistent() { this->Reset(); } + /** + * Move via assignment. + */ + template + V8_INLINE UniquePersistent& operator=(UniquePersistent rhs) { + TYPE_CHECK(T, S); + this->Reset(); + this->m_ptr = rhs.m_ptr; + rhs.m_ptr = 0; + return *this; + } + /** + * Cast operator for moves. + */ + V8_INLINE operator RValue() { return RValue(this); } + /** + * Pass allows returning uniques from functions, etc. + */ + UniquePersistent Pass() { return UniquePersistent(RValue(this)); } + +private: + UniquePersistent(const UniquePersistent&); + void operator=(UniquePersistent&); +}; + + + + /// v8::HandleScope class HandleScope { public: - HandleScope() {} + HandleScope(Isolate *isolate = 0) {} ~HandleScope() {} template Local Close(Handle value) { return Local(value); } }; +class EscapableHandleScope : public HandleScope +{ +public: + EscapableHandleScope(Isolate* isolate){} + EscapableHandleScope() {} + ~EscapableHandleScope() {} + + template Local Escape(Handle value) { return Local(value); } +}; + /// v8::Data class V8EXPORT Data : public internal::SmartObject @@ -486,7 +772,7 @@ class V8EXPORT Value : public Data virtual void DecRef(); virtual void _OnCreate(); virtual void _OnDelete(); - virtual void _MakeWeak(void* parameter, void (*callback)(Persistent object, void* parameter)); + virtual void _MakeWeak(void* parameter, void*); virtual void _ClearWeak(); virtual void _MarkIndependent(); virtual bool _IsIndependent() const; @@ -522,6 +808,8 @@ class V8EXPORT Value : public Data bool IsBooleanObject(); bool IsNumberObject(); bool IsStringObject(); + virtual bool IsTypedArray(); + virtual bool IsArrayBuffer(); Local ToBoolean() const; Local ToNumber() const; @@ -540,6 +828,9 @@ class V8EXPORT Value : public Data uint32_t Uint32Value() const; bool Equals(Handle that) const; bool StrictEquals(Handle that) const; + + + V8EXPORT static Value* Cast(v8::Value* value); }; /// v8::Primitive @@ -549,6 +840,7 @@ class V8EXPORT Primitive : public Value RTTI_DECLARE(); public: V8EXPORT Primitive() {} + V8EXPORT Primitive(const Value & other):Value(other) {} V8EXPORT Primitive(JSContextRef js_ctx, JSValueRef js_value) : Value(js_ctx, js_value) {} virtual ~Primitive() {} }; @@ -565,6 +857,10 @@ class V8EXPORT Boolean : public Primitive V8EXPORT bool Value() const; public: V8EXPORT static Handle New(bool value); + V8EXPORT static Handle New(Isolate* isolate, bool value) + { + return New(value); + } V8EXPORT static Boolean* Cast(v8::Value* value); }; @@ -580,6 +876,10 @@ class V8EXPORT Number : public Primitive V8EXPORT double Value() const; public: V8EXPORT static Local New(double value); + V8EXPORT static Local New(Isolate* isolate, double value) + { + return New(value); + } V8EXPORT static Number* Cast(v8::Value* value); }; @@ -594,8 +894,16 @@ class V8EXPORT Integer : public Number public: V8EXPORT int64_t Value() const; public: + V8EXPORT static Local New(v8::Isolate* isolate, int32_t value) + { + return New(value); + } V8EXPORT static Local New(int32_t value); V8EXPORT static Local NewFromUnsigned(uint32_t value); + V8EXPORT static Local NewFromUnsigned(Isolate* isolate, uint32_t value) + { + return New(value); + } //V8EXPORT static Local New(int32_t value, Isolate*); //V8EXPORT static Local NewFromUnsigned(uint32_t value, Isolate*); V8EXPORT static Integer* Cast(v8::Value* value); @@ -743,6 +1051,7 @@ class V8EXPORT String : public Primitive public: V8EXPORT String(JSContextRef js_ctx, JSValueRef js_value); + V8EXPORT String(const Primitive & other) :Primitive(other){}; V8EXPORT virtual ~String(); public: @@ -752,6 +1061,15 @@ class V8EXPORT String : public Primitive V8EXPORT int Write(uint16_t* buffer, int start = 0, int length = -1, int options = NO_OPTIONS) const; V8EXPORT int WriteAscii(char* buffer, int start = 0, int length = -1, int options = NO_OPTIONS) const; + + // One byte characters. + int WriteOneByte(uint8_t* buffer, + int start = 0, + int length = -1, + int options = NO_OPTIONS) const + { + return WriteAscii((char*)buffer, start, length, options); + } V8EXPORT int WriteUtf8(char* buffer, int length = -1, int* nchars_ref = NULL, int options = NO_OPTIONS) const; V8EXPORT bool IsExternal() const; @@ -765,7 +1083,7 @@ class V8EXPORT String : public Primitive V8EXPORT bool CanMakeExternal(); public: - V8EXPORT static Local Empty(); + V8EXPORT static Local Empty(Isolate* isolate = nullptr); V8EXPORT static String* Cast(v8::Value* value); public: @@ -773,14 +1091,277 @@ class V8EXPORT String : public Primitive V8EXPORT static Local New(const uint16_t* data, int length = -1); V8EXPORT static Local NewSymbol(const char* data, int length = -1); V8EXPORT static Local Concat(Handle left, Handle right); + V8EXPORT static Local NewExternal(Isolate* isolate, ExternalStringResource* resource); + V8EXPORT static Local NewExternal(Isolate* isolate, ExternalAsciiStringResource* resource); V8EXPORT static Local NewExternal(ExternalStringResource* resource); V8EXPORT static Local NewExternal(ExternalAsciiStringResource* resource); V8EXPORT static Local NewUndetectable(const char* data, int length = -1); V8EXPORT static Local NewUndetectable(const uint16_t* data, int length = -1); + + + enum NewStringType { + kNormalString, kInternalizedString, kUndetectableString + }; + /** Allocates a new string from UTF-8 data.*/ + static Local NewFromUtf8(Isolate* isolate, + const char* data, + NewStringType type = kNormalString, + int length = -1) + { + return New(data, length); + } + + /** Allocates a new string from Latin-1 data.*/ + static Local NewFromOneByte( + Isolate* isolate, + const uint8_t* data, + NewStringType type = kNormalString, + int length = -1) + { + return New((const char*)data, length); + } + + /** Allocates a new string from UTF-16 data.*/ + static Local NewFromTwoByte( + Isolate* isolate, + const uint16_t* data, + NewStringType type = kNormalString, + int length = -1) + { + return New(data, length); + } + }; +/// v8 -/// v8::AccessorInfo +Local V8EXPORT Undefined(v8::Isolate* isolate = 0); +Local V8EXPORT Null(v8::Isolate* isolate = 0); +Local V8EXPORT True(v8::Isolate* isolate = 0); +Local V8EXPORT False(v8::Isolate* isolate = 0); + +template +class ReturnValue { +public: + class SmartValue + { + public: + SmartValue() + :_val(0) + { + + } + SmartValue(T * val) + : _val(val) + { + retain(); + } + ~SmartValue() + { + release(); + } + T *get()const + { + return _val; + } + void set(T* val) + { + release(); + _val = val; + retain(); + } + private: + T *_val; + void release() + { + if (_val) + { + static_cast(_val)->DecRef(); + } + } + void retain() + { + if (_val) + { + static_cast(_val)->IncRef(); + } + } + }; + + + ReturnValue() + : value_(new std::shared_ptr()) + { + initDefaultValue(); + } + ~ReturnValue() + { + } + + + template ReturnValue(const ReturnValue& that) + : value_(that.value_) { + TYPE_CHECK(T, S); + if (!(*value_).get()) + { + initDefaultValue(); + } + } + + ReturnValue(const ReturnValue& that) + : value_(that.value_) { + if (!(*value_).get()) + { + initDefaultValue(); + } + } + + // Handle setters + template void Set(const Persistent& handle){ + TYPE_CHECK(T, S); + T *v = static_cast(handle.GetPointer()); + (*value_).reset(new SmartValue(v)); + } + + template void Set(const Local handle) { + TYPE_CHECK(T, S); + T *v = static_cast(handle.GetPointer()); + (*value_).reset(new SmartValue(v)); + } + + template void Set(const Handle handle) { + TYPE_CHECK(T, S); + T *v = static_cast(handle.GetPointer()); + (*value_).reset(new SmartValue(v)); + } + // Fast primitive setters + void Set(bool value) { + TYPE_CHECK(T, Boolean); + Set(Boolean::New(value)); + } + + void Set(double i){ + TYPE_CHECK(T, Number); + Set(Number::New(i)); + } + + void Set(int32_t i){ + TYPE_CHECK(T, Int32); + Set(Int32::New(i)); + } + void Set(uint32_t i){ + TYPE_CHECK(T, Uint32); + Set(Uint32::NewFromUnsigned(i)); + } + // Fast JS primitive setters + void SetNull(){ + TYPE_CHECK(T, Primitive); + Set(Null()); + } + + void SetUndefined(){ + TYPE_CHECK(T, Primitive); + Set(Undefined()); + } + + void SetEmptyString(){ + TYPE_CHECK(T, String); + Set(String::Empty()); + } + + Local getValue() + { + SmartValue * v = value_.get()->get(); + return (v && v->get()) ? v->get() : _defaultValue.get(); + } + + void initDefaultValue() + { + _defaultValue.set(static_cast(Undefined().GetPointer())); + } + + void SetDefaultValue(T* val) + { + _defaultValue.set(val); + } + // Convenience getter for Isolate + // Isolate* GetIsolate(){ + // // Isolate is always the pointer below the default value on the stack. + // return *reinterpret_cast(&value_[-2]); + // } + + // Pointer setter: Uncompilable to prevent inadvertent misuse. + template + void Set(S* whatever){ + // Uncompilable to prevent inadvertent misuse. + TYPE_CHECK(S*, Primitive); + } + +private: + std::shared_ptr< std::shared_ptr > value_; + SmartValue _defaultValue; +}; + + +template +class PropertyCallbackInfo { + Isolate* m_isolate; + Handle m_that; + Handle m_holder; + Handle m_data; + ReturnValue m_return_value; + typename ReturnValue::SmartValue defaultRetValue; +public: + + V8_INLINE Isolate* GetIsolate() const{ + return m_isolate; + } + V8_INLINE Local Data() const{ + return m_data; + } + V8_INLINE Local This() const{ + return m_that; + } + V8_INLINE Local Holder() const{ + return m_holder; + } + V8_INLINE ReturnValue GetReturnValue() const{ + return m_return_value; + } + // This shouldn't be public, but the arm compiler needs it. + static const int kArgsLength = 6; + V8EXPORT PropertyCallbackInfo(Isolate* isolate, Handle that, Handle holder, Handle data) + { + m_isolate = isolate; + m_that = that; + m_holder = holder; + m_data = data; + } +// V8_INLINE PropertyCallbackInfo(void** args) +// { +// args_ = args; +// m_holder = Local(reinterpret_cast(&args[kHolderIndex])); +// m_isolate = *reinterpret_cast(&args[kIsolateIndex]); +//// defaultRetValue.set(reinterpret_cast(&args[kReturnValueDefaultValueIndex])); +// m_return_value = ReturnValue(&args[kReturnValueIndex]); +// m_data = Local(reinterpret_cast(&args[kDataIndex])); +// m_that = Local(reinterpret_cast(&args[kThisIndex])); +// +// m_return_value.SetDefaultValue(reinterpret_cast(&args[kReturnValueDefaultValueIndex])); +// } +protected: + static const int kHolderIndex = 0; + static const int kIsolateIndex = 1; + static const int kReturnValueDefaultValueIndex = 2; + static const int kReturnValueIndex = 3; + static const int kDataIndex = 4; + static const int kThisIndex = 5; + + void ** args_; +}; + +/// v8::AccessorInfo +/* class V8EXPORT AccessorInfo { public: @@ -797,20 +1378,22 @@ class V8EXPORT AccessorInfo Local Holder() const; Local Data() const; }; +*/ +typedef PropertyCallbackInfo AccessorInfo; /// -typedef Handle (*NamedPropertyGetter)(Local property, const AccessorInfo& info); -typedef Handle (*NamedPropertySetter)(Local property, Local value, const AccessorInfo& info); -typedef Handle (*NamedPropertyQuery)(Local property, const AccessorInfo& info); -typedef Handle (*NamedPropertyDeleter)(Local property, const AccessorInfo& info); -typedef Handle (*NamedPropertyEnumerator)(const AccessorInfo& info); +typedef void (*NamedPropertyGetter)(Local property, const AccessorInfo& info); +typedef void (*NamedPropertySetter)(Local property, Local value, const AccessorInfo& info); +typedef void (*NamedPropertyQuery)(Local property, const PropertyCallbackInfo& info); +typedef void(*NamedPropertyDeleter)(Local property, const PropertyCallbackInfo& info); +typedef void(*NamedPropertyEnumerator)(const PropertyCallbackInfo& info); -typedef Handle (*IndexedPropertyGetter)(uint32_t index, const AccessorInfo& info); -typedef Handle (*IndexedPropertySetter)(uint32_t index, Local value, const AccessorInfo& info); -typedef Handle (*IndexedPropertyQuery)(uint32_t index, const AccessorInfo& info); -typedef Handle (*IndexedPropertyDeleter)(uint32_t index, const AccessorInfo& info); -typedef Handle (*IndexedPropertyEnumerator)(const AccessorInfo& info); +typedef void (*IndexedPropertyGetter)(uint32_t index, const AccessorInfo& info); +typedef void (*IndexedPropertySetter)(uint32_t index, Local value, const AccessorInfo& info); +typedef void (*IndexedPropertyQuery)(uint32_t index, const AccessorInfo& info); +typedef void (*IndexedPropertyDeleter)(uint32_t index, const AccessorInfo& info); +typedef void (*IndexedPropertyEnumerator)(const AccessorInfo& info); enum AccessType { @@ -835,19 +1418,31 @@ enum PropertyAttribute enum ExternalArrayType { - kExternalByteArray = 1, - kExternalUnsignedByteArray, - kExternalShortArray, - kExternalUnsignedShortArray, - kExternalIntArray, - kExternalUnsignedIntArray, - kExternalFloatArray, - kExternalDoubleArray, - kExternalPixelArray -}; - -typedef Handle (*AccessorGetter)(Local property, const AccessorInfo& info); -typedef void (*AccessorSetter)(Local property, Local value, const AccessorInfo& info); + kExternalInt8Array = 1, + kExternalUint8Array, + kExternalInt16Array, + kExternalUint16Array, + kExternalInt32Array, + kExternalUint32Array, + kExternalFloat32Array, + kExternalFloat64Array, + kExternalUint8ClampedArray, + + // Legacy constant names + kExternalByteArray = kExternalInt8Array, + kExternalUnsignedByteArray = kExternalUint8Array, + kExternalShortArray = kExternalInt16Array, + kExternalUnsignedShortArray = kExternalUint16Array, + kExternalIntArray = kExternalInt32Array, + kExternalUnsignedIntArray = kExternalUint32Array, + kExternalFloatArray = kExternalFloat32Array, + kExternalDoubleArray = kExternalFloat64Array, + kExternalPixelArray = kExternalUint8ClampedArray +}; + +//typedef Handle (*AccessorGetter)(Local property, const AccessorInfo& info); +typedef void (*AccessorGetter)(Local property, const AccessorInfo& info); +typedef void(*AccessorSetter)(Local property, Local value, const PropertyCallbackInfo& info); enum AccessControl { @@ -859,156 +1454,156 @@ enum AccessControl namespace internal { -class Property -{ -public: - Handle m_value; - PropertyAttribute m_attrib; -public: - Property() : m_attrib(None) {} - Property(Handle value, PropertyAttribute attrib) : - m_value(value), m_attrib(attrib) {} - Property(const Property& other) : - m_value(other.m_value), m_attrib(other.m_attrib) {} - Property& operator=(const Property& other) + class Property { - m_value = other.m_value; - m_attrib = other.m_attrib; - return *this; - } -}; + public: + Handle m_value; + PropertyAttribute m_attrib; + public: + Property() : m_attrib(None) {} + Property(Handle value, PropertyAttribute attrib) : + m_value(value), m_attrib(attrib) {} + Property(const Property& other) : + m_value(other.m_value), m_attrib(other.m_attrib) {} + Property& operator=(const Property& other) + { + m_value = other.m_value; + m_attrib = other.m_attrib; + return *this; + } + }; -class Accessor -{ -public: - AccessorGetter m_getter; - AccessorSetter m_setter; - Handle m_data; - AccessControl m_settings; - PropertyAttribute m_attrib; -public: - Accessor() : m_getter(NULL), m_setter(NULL), m_settings(DEFAULT), m_attrib(None) {} - Accessor(AccessorGetter getter, AccessorSetter setter, Handle data, - AccessControl settings, PropertyAttribute attrib) : - m_getter(getter), m_setter(setter), m_data(data), - m_settings(settings), m_attrib(attrib) {} - Accessor(const Accessor& other) : - m_getter(other.m_getter), m_setter(other.m_setter), m_data(other.m_data), - m_settings(other.m_settings), m_attrib(other.m_attrib) {} - Accessor& operator=(const Accessor& other) - { - m_getter = other.m_getter; - m_setter = other.m_setter; - m_data = other.m_data; - m_settings = other.m_settings; - m_attrib = other.m_attrib; - return *this; - } -}; + class Accessor + { + public: + AccessorGetter m_getter; + AccessorSetter m_setter; + Handle m_data; + AccessControl m_settings; + PropertyAttribute m_attrib; + public: + Accessor() : m_getter(NULL), m_setter(NULL), m_settings(DEFAULT), m_attrib(None) {} + Accessor(AccessorGetter getter, AccessorSetter setter, Handle data, + AccessControl settings, PropertyAttribute attrib) : + m_getter(getter), m_setter(setter), m_data(data), + m_settings(settings), m_attrib(attrib) {} + Accessor(const Accessor& other) : + m_getter(other.m_getter), m_setter(other.m_setter), m_data(other.m_data), + m_settings(other.m_settings), m_attrib(other.m_attrib) {} + Accessor& operator=(const Accessor& other) + { + m_getter = other.m_getter; + m_setter = other.m_setter; + m_data = other.m_data; + m_settings = other.m_settings; + m_attrib = other.m_attrib; + return *this; + } + }; -class TemplateProperty -{ -public: - Handle m_value; - PropertyAttribute m_attrib; -public: - TemplateProperty() : m_attrib(None) {} - TemplateProperty(Handle value, PropertyAttribute attrib) : - m_value(value), m_attrib(attrib) {} - TemplateProperty(const TemplateProperty& other) : - m_value(other.m_value), m_attrib(other.m_attrib) {} - TemplateProperty& operator=(const TemplateProperty& other) + class TemplateProperty { - m_value = other.m_value; - m_attrib = other.m_attrib; - return *this; - } -}; + public: + Handle m_value; + PropertyAttribute m_attrib; + public: + TemplateProperty() : m_attrib(None) {} + TemplateProperty(Handle value, PropertyAttribute attrib) : + m_value(value), m_attrib(attrib) {} + TemplateProperty(const TemplateProperty& other) : + m_value(other.m_value), m_attrib(other.m_attrib) {} + TemplateProperty& operator=(const TemplateProperty& other) + { + m_value = other.m_value; + m_attrib = other.m_attrib; + return *this; + } + }; -class TemplateAccessor -{ -public: - AccessorGetter m_getter; - AccessorSetter m_setter; - Handle m_data; - AccessControl m_settings; - PropertyAttribute m_attrib; - Handle m_signature; -public: - TemplateAccessor() {} - TemplateAccessor(AccessorGetter getter, AccessorSetter setter, Handle data, - AccessControl settings, PropertyAttribute attrib, - Handle signature) : - m_getter(getter), m_setter(setter), m_data(data), - m_settings(settings), m_attrib(attrib), - m_signature(signature) {} - TemplateAccessor(const TemplateAccessor& other) : - m_getter(other.m_getter), m_setter(other.m_setter), m_data(other.m_data), - m_settings(other.m_settings), m_attrib(other.m_attrib), - m_signature(other.m_signature) {} - TemplateAccessor& operator=(const TemplateAccessor& other) - { - m_getter = other.m_getter; - m_setter = other.m_setter; - m_data = other.m_data; - m_settings = other.m_settings; - m_attrib = other.m_attrib; - m_signature = other.m_signature; - return *this; - } -}; + class TemplateAccessor + { + public: + AccessorGetter m_getter; + AccessorSetter m_setter; + Handle m_data; + AccessControl m_settings; + PropertyAttribute m_attrib; + Handle m_signature; + public: + TemplateAccessor() {} + TemplateAccessor(AccessorGetter getter, AccessorSetter setter, Handle data, + AccessControl settings, PropertyAttribute attrib, + Handle signature) : + m_getter(getter), m_setter(setter), m_data(data), + m_settings(settings), m_attrib(attrib), + m_signature(signature) {} + TemplateAccessor(const TemplateAccessor& other) : + m_getter(other.m_getter), m_setter(other.m_setter), m_data(other.m_data), + m_settings(other.m_settings), m_attrib(other.m_attrib), + m_signature(other.m_signature) {} + TemplateAccessor& operator=(const TemplateAccessor& other) + { + m_getter = other.m_getter; + m_setter = other.m_setter; + m_data = other.m_data; + m_settings = other.m_settings; + m_attrib = other.m_attrib; + m_signature = other.m_signature; + return *this; + } + }; -struct StringCompare { bool operator()(const Handle& key1, const Handle& key2) const; }; + struct StringCompare { bool operator()(const Handle& key1, const Handle& key2) const; }; #if 0 // V8LIKE_PROPERTY_MAP -class PropertyMap : public std::map, Property, StringCompare> {}; + class PropertyMap : public std::map, Property, StringCompare> {}; #endif -class AccessorMap : public std::map, Accessor, StringCompare> {}; + class AccessorMap : public std::map, Accessor, StringCompare> {}; -class TemplatePropertyMap : public std::map, TemplateProperty, StringCompare> {}; + class TemplatePropertyMap : public std::map, TemplateProperty, StringCompare> {}; -class TemplateAccessorMap : public std::map, TemplateAccessor, StringCompare> {}; + class TemplateAccessorMap : public std::map, TemplateAccessor, StringCompare> {}; -class Helper -{ -private: - class InternalField + class Helper { - public: - Handle m_value; - }; + private: + class InternalField + { + public: + Handle m_value; + }; -public: - Handle m_object_template; // for Objects created by ObjectTemplate::NewInstance - Handle m_function; // for Objects created by Function::NewInstance + public: + Handle m_object_template; // for Objects created by ObjectTemplate::NewInstance + Handle m_function; // for Objects created by Function::NewInstance - #if 0 // V8LIKE_PROPERTY_MAP - internal::PropertyMap m_property_map; - #endif +#if 0 // V8LIKE_PROPERTY_MAP + internal::PropertyMap m_property_map; +#endif - internal::AccessorMap m_accessor_map; + internal::AccessorMap m_accessor_map; - InternalField* m_internal_field_array; - size_t m_internal_field_count; + InternalField* m_internal_field_array; + size_t m_internal_field_count; - uint8_t* m_pixel_data; - int m_pixel_data_length; + uint8_t* m_pixel_data; + int m_pixel_data_length; - void* m_external_array_data; - ExternalArrayType m_external_array_data_type; - int m_external_array_data_length; + void* m_external_array_data; + ExternalArrayType m_external_array_data_type; + int m_external_array_data_length; -public: - Helper(); - virtual ~Helper(); + public: + Helper(); + virtual ~Helper(); -public: - void SetInternalFieldCount(int internal_field_count); + public: + void SetInternalFieldCount(int internal_field_count); -public: - static void WeakFree(Persistent object, void* parameter); -}; + public: + static void WeakFree(const WeakCallbackData& data); + }; } // namespace internal @@ -1075,7 +1670,16 @@ class V8EXPORT Object : public Primitive V8EXPORT void SetInternalField(int index, Handle value); V8EXPORT void* GetPointerFromInternalField(int index); + V8EXPORT void* GetAlignedPointerFromInternalField(int index) + { + return GetPointerFromInternalField(index); + } + V8EXPORT void SetPointerInInternalField(int index, void* value); + V8EXPORT void SetAlignedPointerInInternalField(int index, void* value) + { + SetPointerInInternalField(index, value); + } V8EXPORT bool HasOwnProperty(Handle key); V8EXPORT bool HasRealNamedProperty(Handle key); @@ -1113,14 +1717,14 @@ class V8EXPORT Object : public Primitive V8EXPORT int GetIndexedPropertiesExternalArrayDataLength(); V8EXPORT bool IsCallable(); - V8EXPORT Local CallAsFunction(Handle that, int argc, Handle argv[]); - V8EXPORT Local CallAsConstructor(int argc, Handle argv[]); + V8EXPORT Local CallAsFunction(Handle that, int argc, Handle argv[]); + //V8EXPORT Local CallAsConstructor(int argc, Handle argv[]); public: static JSClassRef sm_js_class; public: - V8EXPORT static Local New(); + V8EXPORT static Local New(Isolate* isolate = 0); V8EXPORT static Object* Cast(Value* value); public: static JSClassRef GetJSClass(); @@ -1152,6 +1756,10 @@ class V8EXPORT Array : public Object ///public: /// static JSClassRef sm_js_class; public: + V8EXPORT static Local New(v8::Isolate* isolate, int length = 0) + { + return New(length); + } V8EXPORT static Local New(int length = 0); V8EXPORT static Array* Cast(Value* value); ///public: @@ -1185,9 +1793,152 @@ class V8EXPORT ScriptData { }; +/**/ +#ifndef V8_ARRAY_BUFFER_INTERNAL_FIELD_COUNT +// The number of required internal fields can be defined by embedder. +#define V8_ARRAY_BUFFER_INTERNAL_FIELD_COUNT 2 +#endif + +class V8EXPORT ArrayBuffer : public Primitive { +public: + + class V8EXPORT Allocator { // NOLINT + public: + virtual ~Allocator() {} + + virtual void* Allocate(size_t length) = 0; + + virtual void* AllocateUninitialized(size_t length) = 0; + + virtual void Free(void* data, size_t length) = 0; + }; + + class V8EXPORT Contents { // NOLINT + public: + Contents() : data_(NULL), byte_length_(0), isOwnData(false) {} + ~Contents(); + + void* Data() const { return data_; } + size_t ByteLength() const { return byte_length_; } + + private: + void setData(size_t byte_length, void* data = nullptr); + void* data_; + size_t byte_length_; + bool isOwnData; + friend class ArrayBuffer; + }; + + size_t ByteLength() const; + + static Local New(Isolate* isolate, size_t byte_length); + + static Local New(Isolate* isolate, void* data, + size_t byte_length); + + bool IsExternal() const; + + void Neuter(){} + + Contents Externalize(); + + static ArrayBuffer* Cast(Value* obj); + + static const int kInternalFieldCount = V8_ARRAY_BUFFER_INTERNAL_FIELD_COUNT; + + virtual bool IsArrayBuffer() override; +private: + Contents content; + ArrayBuffer(){}; + static void CheckCast(Value* obj){} +}; + + + + /// v8::Arguments -class V8EXPORT Arguments +//template +///** +//* The argument information given to function call callbacks. This +//* class provides access to information about the context of the call, +//* including the receiver, the number and values of arguments, and +//* the holder of the function. +//*/ +//template +//class FunctionCallbackInfo { +//public: +// Isolate* m_isolate; +// Handle m_callee; +// Handle m_that; +// Handle m_holder; +// Handle m_data; +// int m_argc; +// Handle* m_argv; +// bool m_is_construct_call; +// +// ReturnValue m_returnValue; +//public: +// FunctionCallbackInfo(Isolate* isolate, Handle callee, Handle that, Handle holder, Handle data, int argc, Handle* argv, bool is_construct_call) : +// m_isolate(isolate), m_callee(callee), m_that(that), m_holder(holder), m_data(data), m_argc(argc), m_argv(argv), m_is_construct_call(is_construct_call) {} +// ~FunctionCallbackInfo() +// { +// m_isolate = NULL; +// m_argc = 0; +// m_argv = NULL; +// m_is_construct_call = false; +// } +// int Length() const +// { +// return m_argc; +// } +// Local operator[](int i) const +// { +// if ((0 <= i) && (i < Length())) +// { +// return Local(m_argv[i]); +// } +// return Local(Undefined()); +// } +// Local Callee() const +// { +// return Local(m_callee); +// } +// Local This() const +// { +// return Local(m_that); +// } +// Local Holder() const +// { +// return Local(m_holder); +// } +// +// bool IsConstructCall() const +// { +// return m_is_construct_call; +// } +// +// Local Data() const +// { +// return Local(m_data); +// } +// +// Isolate* GetIsolate() const +// { +// return m_isolate; +// } +// ReturnValue GetReturnValue() const +// { +// return m_returnValue; +// } +//} + + + + + +template +class V8EXPORT FunctionCallbackInfo { public: Isolate* m_isolate; @@ -1200,23 +1951,87 @@ class V8EXPORT Arguments bool m_is_construct_call; public: - V8EXPORT Arguments(Isolate* isolate, Handle callee, Handle that, Handle holder, Handle data, int argc, Handle* argv, bool is_construct_call); - ~Arguments(); + Isolate* GetIsolate() const + { + return m_isolate; + } + int Length() const + { + return m_argc; + } + Local operator[](int i) const + { + if ((0 <= i) && (i < Length())) + { + return Local(m_argv[i]); + } + return Local(Undefined()); + } + Local Callee() const + { + return Local(m_callee); + } + Local This() const + { + return Local(m_that); + } + Local Holder() const + { + return Local(m_holder); + } + bool IsConstructCall() const + { + return m_is_construct_call; + } + Local Data() const + { + return Local(m_data); + } +public: + ReturnValue m_returnValue; +public: + V8EXPORT FunctionCallbackInfo(Isolate* isolate, Handle callee, Handle that, Handle holder, Handle data, int argc, Handle* argv, bool is_construct_call) : + m_isolate(isolate), m_callee(callee), m_that(that), m_holder(holder), m_data(data), m_argc(argc), m_argv(argv), m_is_construct_call(is_construct_call) + { + if (m_is_construct_call) + { + m_returnValue.Set(holder); + } + } + ~FunctionCallbackInfo() + { + m_isolate = NULL; + m_argc = 0; + m_argv = NULL; + m_is_construct_call = false; + } public: - Isolate* GetIsolate() const; - int Length() const; - Local operator[](int i) const; - Local Callee() const; - Local This() const; - Local Holder() const; - bool IsConstructCall() const; - Local Data() const; + ReturnValue GetReturnValue() const + { + return m_returnValue; + } +}; +/* +class V8EXPORT Arguments : FunctionCallbackInfo +{ +public: + V8EXPORT Arguments(Isolate* isolate, Handle callee, Handle that, Handle holder, Handle data, int argc, Handle* argv, bool is_construct_call); + ~Arguments(); }; +*/ +typedef FunctionCallbackInfo Arguments; + +//template +//using FunctionCallbackInfo = Arguments; /// v8::Function -typedef Handle (*InvocationCallback)(const Arguments& args); +//typedef Handle (*InvocationCallback)(const Arguments& args); + +typedef void(*FunctionCallback)(const FunctionCallbackInfo& info); +typedef void(*InvocationCallback)(const Arguments& info); + class V8EXPORT Function : public Object { @@ -1237,7 +2052,7 @@ class V8EXPORT Function : public Object void _FunctionGetPropertyNames(JSPropertyNameAccumulatorRef js_name_accumulator); public: - V8EXPORT Local Call(Handle that, int argc, Handle argv[]); + V8EXPORT Local Call(Handle that, int argc, Handle argv[]); V8EXPORT Local NewInstance() /*const*/ { return NewInstance(0, NULL); } V8EXPORT Local NewInstance(int argc, Handle argv[]) /*const*/; @@ -1406,6 +2221,10 @@ class V8EXPORT External : public Value V8EXPORT static Local Wrap(void* data); V8EXPORT static void* Unwrap(Handle obj); V8EXPORT static Local New(void* value); + V8EXPORT static Local New(Isolate* isolate, void* value) + { + return New(value); + } V8EXPORT static External* Cast(v8::Value* value); public: static JSClassRef GetJSClass(); @@ -1454,8 +2273,12 @@ class V8EXPORT StackTrace : public internal::SmartObject Local GetFrame(uint32_t index) const; int GetFrameCount() const; Local AsArray(); - - static Local CurrentStackTrace(int frame_limit, StackTraceOptions options = kOverview); + + static Local CurrentStackTrace(int frame_limit, StackTraceOptions options = kOverview); + static Local CurrentStackTrace(Isolate *iso , int frame_limit, StackTraceOptions options = kOverview) + { + return CurrentStackTrace(frame_limit , options); + } }; /// v8::Message @@ -1498,6 +2321,7 @@ class V8EXPORT TryCatch void Reset(); void SetVerbose(bool value); void SetCaptureMessage(bool value); + bool HasTerminated() const; }; /// v8::Signature @@ -1509,6 +2333,10 @@ class V8EXPORT Signature : public internal::SmartObject virtual ~Signature() {} public: static Local New(Handle receiver = Handle(), int argc = 0, Handle argv[] = 0); + static Local New(Isolate *iso, Handle receiver = Handle(), int argc = 0, Handle argv[] = 0) + { + return New(receiver , argc , argv); + } }; /// v8::AccessorSignature @@ -1537,7 +2365,15 @@ class V8EXPORT Template : public Data public: void Set(Handle name, Handle value, PropertyAttribute attrib = None); + void Set(Isolate* isolate, Handle name, Handle value, PropertyAttribute attrib = None) + { + Set(name, value, attrib); + } void Set(const char* name, Handle value); + void Set(Isolate* isolate, const char* name, Handle value) + { + Set(name, value); + } virtual void ApplyToObject(Handle object); }; @@ -1605,7 +2441,7 @@ class V8EXPORT ObjectTemplate : public Template virtual void ApplyToObject(Handle object); public: - static Local New(); + static Local New(Isolate* isolate = 0); }; /// v8::FunctionTemplate @@ -1616,6 +2452,7 @@ class V8EXPORT FunctionTemplate : public Template public: Handle m_instance_template; + Handle m_inherit_template; InvocationCallback m_callback; Handle m_data; @@ -1638,8 +2475,11 @@ class V8EXPORT FunctionTemplate : public Template bool _FunctionTemplateSetProperty(Handle function, Handle name, Handle value); bool _FunctionTemplateDeleteProperty(Handle function, Handle name); void _FunctionTemplateGetPropertyNames(Handle function, JSPropertyNameAccumulatorRef js_name_accumulator); + void _ApplyMemberFunctionToObject(Handle prototype); public: + + Local GetFunction(); void Inherit(Handle parent); Local InstanceTemplate(); @@ -1652,9 +2492,26 @@ class V8EXPORT FunctionTemplate : public Template public: static Local New( - InvocationCallback callback = 0, - Handle data = Handle(), - Handle signature = Handle()); + InvocationCallback callback = 0, + Handle data = Handle(), + Handle signature = Handle()); + + static Local New( + Isolate* isolate, + FunctionCallback callback = 0, + Handle data = Handle(), + Handle signature = Handle(), + int length = 0); + ///** Creates a function template.*/ + //static Local New( + // Isolate* isolate, + // FunctionCallback callback = 0, + // Handle data = Handle(), + // Handle signature = Handle(), + // int length = 0) + //{ + // return New(callback, data, signature, length); + //} }; /// v8::Script @@ -1680,6 +2537,7 @@ class V8EXPORT Script : public internal::SmartObject Local Id() { TODO(); return Local(); } void SetData(Handle data) { m_script_data = data; } + void bindContext(); public: static Local