Brian Love
Angular + TypeScript Developer in Denver, CO

Miscellaneous V8 Helpers

Miscellaneous V8 Helpers

### Nan::Utf8String

Converts an object to a UTF-8-encoded character array. If conversion to a string fails (e.g. due to an exception in the toString() method of the object) then the length() method returns 0 and the * operator returns NULL. The underlying memory used for this object is managed by the object.

An implementation of v8::String::Utf8Value that is consistent across all supported versions of V8.


class Nan::Utf8String {
  Nan::Utf8String(v8::Local<v8::Value> from);

  int length() const;

  char* operator*();
  const char* operator*() const;

### Nan::GetCurrentContext()

A call to v8::Isolate::GetCurrent()->GetCurrentContext() that works across all supported versions of V8.


v8::Local<v8::Context> Nan::GetCurrentContext()

### Nan::SetIsolateData()

A helper to provide a consistent API to v8::Isolate#SetData().


void Nan::SetIsolateData(v8::Isolate *isolate, T *data)

### Nan::GetIsolateData()

A helper to provide a consistent API to v8::Isolate#GetData().


T *Nan::GetIsolateData(v8::Isolate *isolate)

### Nan::TypedArrayContents

A helper class for accessing the contents of an ArrayBufferView (aka a typedarray) from C++. If the input array is not a valid typedarray, then the data pointer of TypedArrayContents will default to NULL and the length will be 0. If the data pointer is not compatible with the alignment requirements of type, an assertion error will fail.

Note that you must store a reference to the array object while you are accessing its contents.


template<typename T>
class Nan::TypedArrayContents {
  TypedArrayContents(v8::Local<Value> array);

  size_t length() const;

  T* const operator*();
  const T* const operator*() const;