Skip to content

Instantly share code, notes, and snippets.

// prints out information about the guts of an object
void printobj(const v8::FunctionCallbackInfo<v8::Value>& args) {
auto isolate = args.GetIsolate();
auto context = isolate->GetCurrentContext();
for (int i = 0; i < args.Length(); i++) {
auto object = args[i]->ToObject(context).ToLocalChecked();
if(object->InternalFieldCount() > 0) {
v8::Local<v8::External> wrap = v8::Local<v8::External>::Cast(object->GetInternalField(0));
printf(">>> Object %p: %s\n", wrap->Value(), *v8::String::Utf8Value(args[i]));
} else {
// CALLED WITH THIS:
int foo(int i){return i+1;}
add_function(isolate, global_templ, "foo", &foo);
then, in javascript, you can say "var result = foo(5);"
// the following code doesn't work with functions that take parameters, but I've already solved that
// elsewhere and just need to copy it over
template<class T>
struct MethodTypesHelper{};
template<class RETURN_TYPE, class ... PARAMETERS>
struct MethodTypesHelper<RETURN_TYPE(*)(PARAMETERS...)>{
typedef RETURN_TYPE(*FUNCTION_TYPE)(PARAMETERS...);
std::function<RETURN_TYPE(PARAMETERS...)> operator()(FUNCTION_TYPE function) {
return std::function<RETURN_TYPE(PARAMETERS...)>(function);
}
@xaxxon
xaxxon / foo.cpp
Last active January 20, 2016 01:33
printf("***************** Creating wrapped object for %p\n", cpp_object);
auto foo = [isolate, cpp_object]() {
printf("***************** IN GLOBAL SET WEAK CALLBACK for %p\n", cpp_object);
BEHAVIOR()(isolate, cpp_object);
};
global_set_weak(isolate, js_object, foo);
// The address printed out in the lambda doesn't match the value printed out above the lambda
template<class T>
class V8ClassWrapper {
private:
static std::map<v8::Isolate *, V8ClassWrapper<T> *> isolate_to_wrapper_map;
// Common tasks to do for any new js object regardless of how it is created
template<class BEHAVIOR>
static void _initialize_new_js_object(v8::Isolate * isolate, v8::Local<v8::Object> js_object, T * cpp_object) {
js_object->SetInternalField(0, v8::External::New(isolate, (void*) cpp_object));
/**
* Specialization for methods that don't return void. Sets v8::FunctionCallbackInfo::GetReturnValue to value returned by the method
*/
template<typename RETURN_TYPE, typename CLASS_TYPE, typename ... PARAMETERS>
struct RunMethod<RETURN_TYPE(CLASS_TYPE::*)(PARAMETERS...)>{
typedef RETURN_TYPE(CLASS_TYPE::*METHOD_TYPE)(PARAMETERS...);
void operator()(CLASS_TYPE & object, METHOD_TYPE method, const v8::FunctionCallbackInfo<v8::Value> & info, PARAMETERS... parameters) {
RETURN_TYPE return_value = (object.*method)(parameters...);
auto casted_result = CastToJS<RETURN_TYPE>()(info.GetIsolate(), return_value);
// Responsible for calling the actual method and populating the return type for non-void return type methods
template<typename METHOD_TYPE>
struct RunMethod {};
/**
* Specialization for methods that don't return void. Sets v8::FunctionCallbackInfo::GetReturnValue to value returned by the method
*/
// Responsible for calling the actual method and populating the return type for non-void return type methods
template<typename METHOD_TYPE>
struct RunMethod {};
/**
* Specialization for methods that don't return void. Sets v8::FunctionCallbackInfo::GetReturnValue to value returned by the method
*/
template<typename RETURN_TYPE, typename CLASS_TYPE, typename ... PARAMETERS>
struct RunMethod<RETURN_TYPE(CLASS_TYPE::*)(PARAMETERS...)>{
typedef RETURN_TYPE(CLASS_TYPE::*METHOD_TYPE)(PARAMETERS...);
template<int depth, typename METHOD_TYPE, typename RET, typename CLASS_TYPE>
struct Caller<depth, METHOD_TYPE, RET(CLASS_TYPE::*)() const> {
public:
// the final class in the call chain stores the actual method to be called
enum {DEPTH=depth, ARITY=0};
// This call method actually calls the method with the specified object and the
// parameter pack that was built up via the chain of calls between templated types
template<typename ... Ts>
Stacktrace (bbbbbbbb-bbbbbbbb) 0x1f4614304129 0x0:
==== JS stack trace =========================================
Security context: 0x1f46143d4261 <JS Object>#0#
1: /* anonymous */ [0x1f4614304189 <undefined>:1] [pc=0x29feac53c9b3] (this=0x1287d8004101 <JS Global Object>#1#)
==== Details ================================================
[1]: /* anonymous */ [0x1f4614304189 <undefined>:1] [pc=0x29feac53c9b3] (this=0x1287d8004101 <JS Global Object>#1#) {
// stack-allocated locals