Skip to content

Instantly share code, notes, and snippets.

@NickNaso
Forked from AnnaMag/helpers.cc
Created September 6, 2017 22:27
Show Gist options
  • Save NickNaso/ea1c38804483fc76f5925b8012a7f112 to your computer and use it in GitHub Desktop.
Save NickNaso/ea1c38804483fc76f5925b8012a7f112 to your computer and use it in GitHub Desktop.
Helper functions for printing V8 Local-wrapped objects
#include "v8.h"
#include "helpers.h"
#include <iostream>
using v8::Local;
using v8::String;
using v8::Array;
void PrintLocalString(v8::Local<v8::String> key){
uint32_t utf8_length = key->Utf8Length();
char* buffer = new char[utf8_length];
key->WriteUtf8(buffer);
std::cout << buffer << std::endl;
// smart pointers can't be used as WriteUtf8 takes plain char *
// e.g. std::unique_ptr<char*> buffer = new char[utf8_length];
// so we have to clean memory explicitly
delete[] buffer;
}
void PrintLocalArray(v8::Local<v8::Array> arr){
int length = arr->Length();
for (int i = 0; i < length; i++) {
Local<String> key = arr->Get(i)->ToString();
PrintLocalString(key);
}
std::cout << std::endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment