Skip to content

Instantly share code, notes, and snippets.

@veryjos
Last active August 22, 2017 07:03
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save veryjos/3103953f8a7d02056562a83cef7ab110 to your computer and use it in GitHub Desktop.
Save veryjos/3103953f8a7d02056562a83cef7ab110 to your computer and use it in GitHub Desktop.
#include "main.hpp"
int main() {
const char* script = R"(
(
() => {
class Extended extends CppClass {
constructor() {
super(10, 10);
}
// Override SomeVirtualFunc with a JS method
SomeVirtualFunc() {
this.Print("Called SomeVirtualFunc overidden by Javascript");
}
};
return new Extended();
}
)()
)";
// ... ChakraCore initialization ...
// Initialize CurryChakra bindings
CC::InitializeBindings();
// Run the script.
JsValueRef result;
JsRun(scriptSource, currentSourceContext++, fname, JsParseScriptAttributeNone,
&result);
// Get the returned class casted to CppClass
CppClass* testClass;
CC::Internal::BindingInitializer::JsToCpp(result, testClass);
// Call SomeVirtualFunc on on the type returned from JS
testClass->SomeVirtualFunc();
// ... ChakraCore cleanup ...
return 0;
}
#pragma once
#include <cstdio>
#include <string>
#include "MetaPlug.hpp"
Meta(CC::Bind, CC::Virtual)
class CppClass : public CC::JsBound {
public:
Meta(CC::Bind)
CppClass(float x, float y) : x(x), y(y){};
Meta(CC::Bind)
virtual void SomeVirtualFunc() {
Print("Called SomeVirtualFunc from C++ base definition");
};
Meta(CC::Bind)
void Print(std::string printText) {
printf("%s\n", printText.c_str());
};
Meta(CC::Bind)
float x;
Meta(CC::Bind)
float y;
};
Called SomeVirtualFunc overidden by Javascript
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment