Last active
July 15, 2019 05:59
-
-
Save AnnaMag/92b4d5ab5fbf1f3229534e4262843091 to your computer and use it in GitHub Desktop.
Helper functions for printing V8 Local-wrapped objects
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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