This file contains 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
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)); |
This file contains 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
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 |
This file contains 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
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); | |
} |
This file contains 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
// 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 |
This file contains 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
// 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 { |
This file contains 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
#pragma once | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <functional> | |
#include <iostream> | |
#include <map> | |
#include <vector> |
This file contains 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 <SDL.h> // include SDL header | |
int main(int argc, char* argv[]) | |
{ | |
SDL_Surface *screen; // even with SDL2, we can still bring ancient code back | |
SDL_Window *window; | |
SDL_Surface *image; | |
SDL_Init(SDL_INIT_VIDEO); // init video |
This file contains 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 <SDL.h> // include SDL header | |
int main(int argc, char* argv[]) | |
{ | |
SDL_Surface *screen; // even with SDL2, we can still bring ancient code back | |
SDL_Window *window; | |
SDL_Surface *image; | |
SDL_Init(SDL_INIT_VIDEO); // init video |
This file contains 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
int string_length(string s) | |
{ | |
int length; | |
length = s.length(); | |
return length; | |
} | |
int main() | |
{ | |
string my_string = "hello"; |
This file contains 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 <string> | |
using namespace std; | |
class Animal { | |
public: // everything below here can be used by everyone, not just inside this class | |
Animal(string input_name):name(input_name){} // **constructor**, takes a string and stores it in the new object being created | |
string name; // **data member**. A variable in each Animal object that stores the name | |