v8-util.h   v8-util.h 
skipping to change at line 32 skipping to change at line 32
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#ifndef V8_UTIL_H_ #ifndef V8_UTIL_H_
#define V8_UTIL_H_ #define V8_UTIL_H_
#include "v8.h" #include "v8.h"
#include <map>
/** /**
* Support for Persistent containers. * Support for Persistent containers.
* *
* C++11 embedders can use STL containers with UniquePersistent values, * C++11 embedders can use STL containers with UniquePersistent values,
* but pre-C++11 does not support the required move semantic and hence * but pre-C++11 does not support the required move semantic and hence
* may want these container classes. * may want these container classes.
*/ */
namespace v8 { namespace v8 {
typedef uintptr_t PersistentContainerValue; typedef uintptr_t PersistentContainerValue;
static const uintptr_t kPersistentContainerNotFound = 0; static const uintptr_t kPersistentContainerNotFound = 0;
/** /**
* A default trait implemenation for PersistentValueMap which uses std::map
* as a backing map.
*
* Users will have to implement their own weak callbacks & dispose traits.
*/
template<typename K, typename V>
class StdMapTraits {
public:
// STL map & related:
typedef std::map<K, PersistentContainerValue> Impl;
typedef typename Impl::iterator Iterator;
static bool Empty(Impl* impl) { return impl->empty(); }
static size_t Size(Impl* impl) { return impl->size(); }
static void Swap(Impl& a, Impl& b) { std::swap(a, b); } // NOLINT
static Iterator Begin(Impl* impl) { return impl->begin(); }
static Iterator End(Impl* impl) { return impl->end(); }
static K Key(Iterator it) { return it->first; }
static PersistentContainerValue Value(Iterator it) { return it->second; }
static PersistentContainerValue Set(Impl* impl, K key,
PersistentContainerValue value) {
std::pair<Iterator, bool> res = impl->insert(std::make_pair(key, value)
);
PersistentContainerValue old_value = kPersistentContainerNotFound;
if (!res.second) {
old_value = res.first->second;
res.first->second = value;
}
return old_value;
}
static PersistentContainerValue Get(Impl* impl, K key) {
Iterator it = impl->find(key);
if (it == impl->end()) return kPersistentContainerNotFound;
return it->second;
}
static PersistentContainerValue Remove(Impl* impl, K key) {
Iterator it = impl->find(key);
if (it == impl->end()) return kPersistentContainerNotFound;
PersistentContainerValue value = it->second;
impl->erase(it);
return value;
}
};
/**
* A default trait implementation for PersistentValueMap, which inherits
* a std:map backing map from StdMapTraits and holds non-weak persistent
* objects.
*
* Users have to implement their own dispose trait.
*/
template<typename K, typename V>
class StrongMapTraits : public StdMapTraits<K, V> {
public:
// Weak callback & friends:
static const bool kIsWeak = false;
typedef typename StdMapTraits<K, V>::Impl Impl;
typedef void WeakCallbackDataType;
static WeakCallbackDataType* WeakCallbackParameter(
Impl* impl, const K& key, Local<V> value);
static Impl* ImplFromWeakCallbackData(
const WeakCallbackData<V, WeakCallbackDataType>& data);
static K KeyFromWeakCallbackData(
const WeakCallbackData<V, WeakCallbackDataType>& data);
static void DisposeCallbackData(WeakCallbackDataType* data);
};
/**
* A default trait implementation for PersistentValueMap, with a std::map
* backing map, non-weak persistents as values, and no special dispose
* handling. Can be used as-is.
*/
template<typename K, typename V>
class DefaultPersistentValueMapTraits : public StrongMapTraits<K, V> {
public:
typedef typename StrongMapTraits<K, V>::Impl Impl;
static void Dispose(Isolate* isolate, UniquePersistent<V> value,
Impl* impl, K key) { }
};
/**
* A map wrapper that allows using UniquePersistent as a mapped value. * A map wrapper that allows using UniquePersistent as a mapped value.
* C++11 embedders don't need this class, as they can use UniquePersistent * C++11 embedders don't need this class, as they can use UniquePersistent
* directly in std containers. * directly in std containers.
* *
* The map relies on a backing map, whose type and accessors are described * The map relies on a backing map, whose type and accessors are described
* by the Traits class. The backing map will handle values of type * by the Traits class. The backing map will handle values of type
* PersistentContainerValue, with all conversion into and out of V8 * PersistentContainerValue, with all conversion into and out of V8
* handles being transparently handled by this class. * handles being transparently handled by this class.
*/ */
template<class K, class V, class Traits> template<typename K, typename V, typename Traits>
class PersistentValueMap { class PersistentValueMap {
public: public:
V8_INLINE explicit PersistentValueMap(Isolate* isolate) : isolate_(isolat e) {} V8_INLINE explicit PersistentValueMap(Isolate* isolate) : isolate_(isolat e) {}
V8_INLINE ~PersistentValueMap() { Clear(); } V8_INLINE ~PersistentValueMap() { Clear(); }
V8_INLINE Isolate* GetIsolate() { return isolate_; } V8_INLINE Isolate* GetIsolate() { return isolate_; }
/** /**
* Return size of the map. * Return size of the map.
*/ */
V8_INLINE size_t Size() { return Traits::Size(&impl_); } V8_INLINE size_t Size() { return Traits::Size(&impl_); }
/** /**
* Return whether the map holds weak persistents.
*/
V8_INLINE bool IsWeak() { return Traits::kIsWeak; }
/**
* Get value stored in map. * Get value stored in map.
*/ */
V8_INLINE Local<V> Get(const K& key) { V8_INLINE Local<V> Get(const K& key) {
return Local<V>::New(isolate_, FromVal(Traits::Get(&impl_, key))); return Local<V>::New(isolate_, FromVal(Traits::Get(&impl_, key)));
} }
/** /**
* Check whether a value is contained in the map. * Check whether a value is contained in the map.
*/ */
V8_INLINE bool Contains(const K& key) { V8_INLINE bool Contains(const K& key) {
return Traits::Get(&impl_, key) != 0; return Traits::Get(&impl_, key) != 0;
} }
/** /**
* Get value stored in map and set it in returnValue. * Get value stored in map and set it in returnValue.
* Return true if a value was found. * Return true if a value was found.
*/ */
V8_INLINE bool SetReturnValue(const K& key, V8_INLINE bool SetReturnValue(const K& key,
ReturnValue<Value>& returnValue); ReturnValue<Value>& returnValue) {
PersistentContainerValue value = Traits::Get(&impl_, key);
bool hasValue = value != 0;
if (hasValue) {
returnValue.SetInternal(
*reinterpret_cast<internal::Object**>(FromVal(value)));
}
return hasValue;
}
/** /**
* Call Isolate::SetReference with the given parent and the map value. * Call Isolate::SetReference with the given parent and the map value.
*/ */
V8_INLINE void SetReference(const K& key, V8_INLINE void SetReference(const K& key,
const v8::Persistent<v8::Object>& parent) { const Persistent<Object>& parent) {
GetIsolate()->SetReference( GetIsolate()->SetReference(
reinterpret_cast<internal::Object**>(parent.val_), reinterpret_cast<internal::Object**>(parent.val_),
reinterpret_cast<internal::Object**>(FromVal(Traits::Get(&impl_, key) ))); reinterpret_cast<internal::Object**>(FromVal(Traits::Get(&impl_, key) )));
} }
/** /**
* Put value into map. Depending on Traits::kIsWeak, the value will be he ld * Put value into map. Depending on Traits::kIsWeak, the value will be he ld
* by the map strongly or weakly. * by the map strongly or weakly.
* Returns old value as UniquePersistent. * Returns old value as UniquePersistent.
*/ */
skipping to change at line 128 skipping to change at line 222
* Return value for key and remove it from the map. * Return value for key and remove it from the map.
*/ */
V8_INLINE UniquePersistent<V> Remove(const K& key) { V8_INLINE UniquePersistent<V> Remove(const K& key) {
return Release(Traits::Remove(&impl_, key)).Pass(); return Release(Traits::Remove(&impl_, key)).Pass();
} }
/** /**
* Traverses the map repeatedly, * Traverses the map repeatedly,
* in case side effects of disposal cause insertions. * in case side effects of disposal cause insertions.
**/ **/
void Clear(); void Clear() {
typedef typename Traits::Iterator It;
HandleScope handle_scope(isolate_);
// TODO(dcarney): figure out if this swap and loop is necessary.
while (!Traits::Empty(&impl_)) {
typename Traits::Impl impl;
Traits::Swap(impl_, impl);
for (It i = Traits::Begin(&impl); i != Traits::End(&impl); ++i) {
Traits::Dispose(isolate_, Release(Traits::Value(i)).Pass(), &impl,
Traits::Key(i));
}
}
}
private: private:
PersistentValueMap(PersistentValueMap&); PersistentValueMap(PersistentValueMap&);
void operator=(PersistentValueMap&); void operator=(PersistentValueMap&);
/** /**
* Put the value into the map, and set the 'weak' callback when demanded * Put the value into the map, and set the 'weak' callback when demanded
* by the Traits class. * by the Traits class.
*/ */
UniquePersistent<V> SetUnique(const K& key, UniquePersistent<V>* persiste nt) { UniquePersistent<V> SetUnique(const K& key, UniquePersistent<V>* persiste nt) {
skipping to change at line 150 skipping to change at line 256
Local<V> value(Local<V>::New(isolate_, *persistent)); Local<V> value(Local<V>::New(isolate_, *persistent));
persistent->template SetWeak<typename Traits::WeakCallbackDataType>( persistent->template SetWeak<typename Traits::WeakCallbackDataType>(
Traits::WeakCallbackParameter(&impl_, key, value), WeakCallback); Traits::WeakCallbackParameter(&impl_, key, value), WeakCallback);
} }
PersistentContainerValue old_value = PersistentContainerValue old_value =
Traits::Set(&impl_, key, ClearAndLeak(persistent)); Traits::Set(&impl_, key, ClearAndLeak(persistent));
return Release(old_value).Pass(); return Release(old_value).Pass();
} }
static void WeakCallback( static void WeakCallback(
const WeakCallbackData<V, typename Traits::WeakCallbackDataType>& dat const WeakCallbackData<V, typename Traits::WeakCallbackDataType>& dat
a); a) {
if (Traits::kIsWeak) {
typename Traits::Impl* impl = Traits::ImplFromWeakCallbackData(data);
K key = Traits::KeyFromWeakCallbackData(data);
PersistentContainerValue value = Traits::Remove(impl, key);
Traits::Dispose(data.GetIsolate(), Release(value).Pass(), impl, key);
}
}
V8_INLINE static V* FromVal(PersistentContainerValue v) { V8_INLINE static V* FromVal(PersistentContainerValue v) {
return reinterpret_cast<V*>(v); return reinterpret_cast<V*>(v);
} }
V8_INLINE static PersistentContainerValue ClearAndLeak( V8_INLINE static PersistentContainerValue ClearAndLeak(
UniquePersistent<V>* persistent) { UniquePersistent<V>* persistent) {
V* v = persistent->val_; V* v = persistent->val_;
persistent->val_ = 0; persistent->val_ = 0;
return reinterpret_cast<PersistentContainerValue>(v); return reinterpret_cast<PersistentContainerValue>(v);
} }
/** /**
* Return a container value as UniquePersistent and make sure the weak * Return a container value as UniquePersistent and make sure the weak
* callback is properly disposed of. All remove functionality should go * callback is properly disposed of. All remove functionality should go
skipping to change at line 180 skipping to change at line 295
Traits::DisposeCallbackData( Traits::DisposeCallbackData(
p.template ClearWeak<typename Traits::WeakCallbackDataType>()); p.template ClearWeak<typename Traits::WeakCallbackDataType>());
} }
return p.Pass(); return p.Pass();
} }
Isolate* isolate_; Isolate* isolate_;
typename Traits::Impl impl_; typename Traits::Impl impl_;
}; };
template <class K, class V, class Traits> /**
bool PersistentValueMap<K, V, Traits>::SetReturnValue(const K& key, * A map that uses UniquePersistent as value and std::map as the backing
ReturnValue<Value>& returnValue) { * implementation. Persistents are held non-weak.
PersistentContainerValue value = Traits::Get(&impl_, key); *
bool hasValue = value != 0; * C++11 embedders don't need this class, as they can use
if (hasValue) { * UniquePersistent directly in std containers.
returnValue.SetInternal( */
*reinterpret_cast<internal::Object**>(FromVal(value))); template<typename K, typename V,
} typename Traits = DefaultPersistentValueMapTraits<K, V> >
return hasValue; class StdPersistentValueMap : public PersistentValueMap<K, V, Traits> {
public:
explicit StdPersistentValueMap(Isolate* isolate)
: PersistentValueMap<K, V, Traits>(isolate) {}
};
/**
* Empty default implementations for StrongTraits methods.
*
* These should not be necessary, since they're only used in code that
* is surrounded by if(Traits::kIsWeak), which for StrongMapTraits is
* compile-time false. Most compilers can live without them; however
* the compiler we use from 64-bit Win differs.
*
* TODO(vogelheim): Remove these once they're no longer necessary.
*/
template<typename K, typename V>
typename StrongMapTraits<K, V>::WeakCallbackDataType*
StrongMapTraits<K, V>::WeakCallbackParameter(
Impl* impl, const K& key, Local<V> value) {
return NULL;
} }
template <class K, class V, class Traits> template<typename K, typename V>
void PersistentValueMap<K, V, Traits>::Clear() { typename StrongMapTraits<K, V>::Impl*
typedef typename Traits::Iterator It; StrongMapTraits<K, V>::ImplFromWeakCallbackData(
HandleScope handle_scope(isolate_); const WeakCallbackData<V, WeakCallbackDataType>& data) {
// TODO(dcarney): figure out if this swap and loop is necessary. return NULL;
while (!Traits::Empty(&impl_)) { }
typename Traits::Impl impl;
Traits::Swap(impl_, impl); template<typename K, typename V>
for (It i = Traits::Begin(&impl); i != Traits::End(&impl); ++i) { K StrongMapTraits<K, V>::KeyFromWeakCallbackData(
Traits::Dispose(isolate_, Release(Traits::Value(i)).Pass(), &impl, const WeakCallbackData<V, WeakCallbackDataType>& data) {
Traits::Key(i)); return K();
}
}
} }
template <class K, class V, class Traits> template<typename K, typename V>
void PersistentValueMap<K, V, Traits>::WeakCallback( void StrongMapTraits<K, V>::DisposeCallbackData(WeakCallbackDataType* data)
const WeakCallbackData<V, typename Traits::WeakCallbackDataType>& data) {
{
typename Traits::Impl* impl = Traits::ImplFromWeakCallbackData(data);
K key = Traits::KeyFromWeakCallbackData(data);
PersistentContainerValue value = Traits::Remove(impl, key);
Traits::Dispose(data.GetIsolate(), Release(value).Pass(), impl, key);
} }
} // namespace v8 } // namespace v8
#endif // V8_UTIL_H_ #endif // V8_UTIL_H_
 End of changes. 12 change blocks. 
37 lines changed or deleted 166 lines changed or added


 v8.h   v8.h 
skipping to change at line 1079 skipping to change at line 1079
class V8_EXPORT ScriptCompiler { class V8_EXPORT ScriptCompiler {
public: public:
/** /**
* Compilation data that the embedder can cache and pass back to speed up * Compilation data that the embedder can cache and pass back to speed up
* future compilations. The data is produced if the CompilerOptions passe d to * future compilations. The data is produced if the CompilerOptions passe d to
* the compilation functions in ScriptCompiler contains produce_data_to_c ache * the compilation functions in ScriptCompiler contains produce_data_to_c ache
* = true. The data to cache can then can be retrieved from * = true. The data to cache can then can be retrieved from
* UnboundScript. * UnboundScript.
*/ */
struct V8_EXPORT CachedData { struct V8_EXPORT CachedData {
CachedData() : data(NULL), length(0) {} enum BufferPolicy {
// Caller keeps the ownership of data and guarantees that the data stay BufferNotOwned,
s BufferOwned
// alive long enough. };
CachedData(const uint8_t* data, int length) : data(data), length(length
) {} CachedData() : data(NULL), length(0), buffer_policy(BufferNotOwned) {}
// If buffer_policy is BufferNotOwned, the caller keeps the ownership o
f
// data and guarantees that it stays alive until the CachedData object
is
// destroyed. If the policy is BufferOwned, the given data will be dele
ted
// (with delete[]) when the CachedData object is destroyed.
CachedData(const uint8_t* data, int length,
BufferPolicy buffer_policy = BufferNotOwned);
~CachedData();
// TODO(marja): Async compilation; add constructors which take a callba ck // TODO(marja): Async compilation; add constructors which take a callba ck
// which will be called when V8 no longer needs the data. // which will be called when V8 no longer needs the data.
const uint8_t* data; const uint8_t* data;
int length; int length;
BufferPolicy buffer_policy;
private:
// Prevent copying. Not implemented.
CachedData(const CachedData&);
}; };
/** /**
* Source code which can be then compiled to a UnboundScript or * Source code which can be then compiled to a UnboundScript or
* BoundScript. * BoundScript.
*/ */
struct V8_EXPORT Source { class V8_EXPORT Source {
public:
// Source takes ownership of CachedData.
Source(Local<String> source_string, const ScriptOrigin& origin, Source(Local<String> source_string, const ScriptOrigin& origin,
const CachedData& cached_data = CachedData()); CachedData* cached_data = NULL);
Source(Local<String> source_string, Source(Local<String> source_string, CachedData* cached_data = NULL);
const CachedData& cached_data = CachedData()); ~Source();
// Ownership of the CachedData or its buffers is *not* transferred to t
he
// caller. The CachedData object is alive as long as the Source object
is
// alive.
const CachedData* GetCachedData() const;
private:
friend class ScriptCompiler;
// Prevent copying. Not implemented.
Source(const Source&);
Local<String> source_string; Local<String> source_string;
// Origin information // Origin information
Handle<Value> resource_name; Handle<Value> resource_name;
Handle<Integer> resource_line_offset; Handle<Integer> resource_line_offset;
Handle<Integer> resource_column_offset; Handle<Integer> resource_column_offset;
Handle<Boolean> resource_is_shared_cross_origin; Handle<Boolean> resource_is_shared_cross_origin;
// Cached data from previous compilation (if any). // Cached data from previous compilation (if any), or generated during
CachedData cached_data; // compilation (if the generate_cached_data flag is passed to
// ScriptCompiler).
CachedData* cached_data;
}; };
enum CompileOptions { enum CompileOptions {
kNoCompileOptions, kNoCompileOptions,
kProduceDataToCache = 1 << 0 kProduceDataToCache = 1 << 0
}; };
/** /**
* Compiles the specified script (context-independent). * Compiles the specified script (context-independent).
* *
* \param source Script source code. * \param source Script source code.
* \return Compiled script object (context independent; for running it mu st be * \return Compiled script object (context independent; for running it mu st be
* bound to a context). * bound to a context).
*/ */
static Local<UnboundScript> CompileUnbound( static Local<UnboundScript> CompileUnbound(
Isolate* isolate, const Source& source, Isolate* isolate, Source* source,
CompileOptions options = kNoCompileOptions); CompileOptions options = kNoCompileOptions);
/** /**
* Compiles the specified script (bound to current context). * Compiles the specified script (bound to current context).
* *
* \param source Script source code. * \param source Script source code.
* \param pre_data Pre-parsing data, as obtained by ScriptData::PreCompil e() * \param pre_data Pre-parsing data, as obtained by ScriptData::PreCompil e()
* using pre_data speeds compilation if it's done multiple times. * using pre_data speeds compilation if it's done multiple times.
* Owned by caller, no references are kept when this function returns. * Owned by caller, no references are kept when this function returns.
* \return Compiled script object, bound to the context that was active * \return Compiled script object, bound to the context that was active
* when this function was called. When run it will always use this * when this function was called. When run it will always use this
* context. * context.
*/ */
static Local<Script> Compile( static Local<Script> Compile(
Isolate* isolate, const Source& source, Isolate* isolate, Source* source,
CompileOptions options = kNoCompileOptions); CompileOptions options = kNoCompileOptions);
}; };
/** /**
* An error message. * An error message.
*/ */
class V8_EXPORT Message { class V8_EXPORT Message {
public: public:
Local<String> Get() const; Local<String> Get() const;
Local<String> GetSourceLine() const; Local<String> GetSourceLine() const;
 End of changes. 7 change blocks. 
14 lines changed or deleted 46 lines changed or added

This html diff was produced by rfcdiff 1.41. The latest version is available from http://tools.ietf.org/tools/rfcdiff/