Skip to content

Instantly share code, notes, and snippets.

@AnnaMag
Last active July 15, 2019 05:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save AnnaMag/92b4d5ab5fbf1f3229534e4262843091 to your computer and use it in GitHub Desktop.
Save AnnaMag/92b4d5ab5fbf1f3229534e4262843091 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