Skip to content

Instantly share code, notes, and snippets.

@xaxxon
xaxxon / foo.cpp
Last active January 1, 2016 03:43
template<typename ... CONSTRUCTOR_PARAMETER_TYPES>
static void v8_constructor(const v8::FunctionCallbackInfo<v8::Value>& args) {
T * new_cpp_object = call_cpp_constructor<CONSTRUCTOR_PARAMETER_TYPES...>(args, std::index_sequence_for<CONSTRUCTOR_PARAMETER_TYPES...>());
// other stuff in here removed
}
template <typename ...Fs, size_t...ns, class WHY>
static T * call_cpp_constructor(const WHY & args, std::index_sequence<ns...>){
return new T(CastToNative<Fs>()(args[ns])...);
JavascriptEngine::JavascriptEngine()
{
printf("platform %p\n", platform.get());
assert(initialized);
// Create a new Isolate and make it the current one.
v8::Isolate::CreateParams create_params;
create_params.array_buffer_allocator = (v8::ArrayBuffer::Allocator *) &JavascriptEngine::allocator;
FOO
<unknown>:0: Uncaught ReferenceError: bar is not defined
#
# Fatal error in v8::ToLocalChecked
# Empty MaybeLocal.
#
Illegal instruction: 4
@xaxxon
xaxxon / foo.cpp
Last active January 14, 2016 11:11
// if this is called repeatedly
javascript_engine.wrap_class<World>();
javascript_engine.wrap_class<World>();
javascript_engine.wrap_class<World>();
// wrap class is just this:
return V8ClassWrapper<T>::get_instance(this->isolate);
frame #48830: 0x000000010024eca0 game`v8::internal::(anonymous namespace)::InstantiateFunction(isolate=0x000000010200aa00, data=<unavailable>, name=<unavailable>) + 512 at api-natives.cc:268
frame #48831: 0x000000010024f3be game`v8::internal::(anonymous namespace)::InstantiateObject(isolate=0x000000010200aa00, data=Handle<v8::internal::ObjectTemplateInfo> @ r15) + 222 at api-natives.cc:219
frame #48832: 0x000000010024eca0 game`v8::internal::(anonymous namespace)::InstantiateFunction(isolate=0x000000010200aa00, data=<unavailable>, name=<unavailable>) + 512 at api-natives.cc:268
frame #48833: 0x000000010024f3be game`v8::internal::(anonymous namespace)::InstantiateObject(isolate=0x000000010200aa00, data=Handle<v8::internal::ObjectTemplateInfo> @ r15) + 222 at api-natives.cc:219
frame #48834: 0x000000010024eca0 game`v8::internal::(anonymous namespace)::InstantiateFunction(isolate=0x000000010200aa00, data=<unavailable>, name=<unavailable>) + 512 at api-natives.cc:268
frame #48835: 0x00000001002
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
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>
// 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...);
// 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
*/
/**
* 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);